blob: 2b19c9db557372bd3fb7df5f6142cf41321e0232 [file] [log] [blame]
Colin Cross43f08db2018-11-12 10:13:39 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package dexpreopt
16
17import (
Colin Crossfeec25b2019-01-30 17:32:39 -080018 "android/soong/android"
Anton Hansson4ec91dd2019-10-02 17:42:44 +010019 "fmt"
Colin Cross43f08db2018-11-12 10:13:39 -080020 "testing"
21)
22
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000023func testSystemModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
Anton Hansson4ec91dd2019-10-02 17:42:44 +010024 return testModuleConfig(ctx, name, "system")
25}
26
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000027func testSystemProductModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
Anton Hansson4ec91dd2019-10-02 17:42:44 +010028 return testModuleConfig(ctx, name, "system/product")
29}
30
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000031func testProductModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
Anton Hansson4ec91dd2019-10-02 17:42:44 +010032 return testModuleConfig(ctx, name, "product")
33}
34
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000035func testModuleConfig(ctx android.PathContext, name, partition string) *ModuleConfig {
Jiakai Zhang519c5c82021-09-16 06:15:39 +000036 return createTestModuleConfig(
37 name,
38 fmt.Sprintf("/%s/app/test/%s.apk", partition, name),
39 android.PathForOutput(ctx, fmt.Sprintf("%s/%s.apk", name, name)),
40 android.PathForOutput(ctx, fmt.Sprintf("%s/dex/%s.jar", name, name)),
41 android.PathForOutput(ctx, fmt.Sprintf("%s/enforce_uses_libraries.status", name)))
42}
43
44func testApexModuleConfig(ctx android.PathContext, name, apexName string) *ModuleConfig {
45 return createTestModuleConfig(
46 name,
47 fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, name),
48 android.PathForOutput(ctx, fmt.Sprintf("%s/dexpreopt/%s.jar", name, name)),
49 android.PathForOutput(ctx, fmt.Sprintf("%s/aligned/%s.jar", name, name)),
50 android.PathForOutput(ctx, fmt.Sprintf("%s/enforce_uses_libraries.status", name)))
51}
52
Jiakai Zhang389a6472021-12-14 18:54:06 +000053func testPlatformSystemServerModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
54 return createTestModuleConfig(
55 name,
56 fmt.Sprintf("/system/framework/%s.jar", name),
57 android.PathForOutput(ctx, fmt.Sprintf("%s/dexpreopt/%s.jar", name, name)),
58 android.PathForOutput(ctx, fmt.Sprintf("%s/aligned/%s.jar", name, name)),
59 android.PathForOutput(ctx, fmt.Sprintf("%s/enforce_uses_libraries.status", name)))
60}
61
liulvping76d56ee2022-05-07 14:40:13 +080062func testSystemExtSystemServerModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
63 return createTestModuleConfig(
64 name,
65 fmt.Sprintf("/system_ext/framework/%s.jar", name),
66 android.PathForOutput(ctx, fmt.Sprintf("%s/dexpreopt/%s.jar", name, name)),
67 android.PathForOutput(ctx, fmt.Sprintf("%s/aligned/%s.jar", name, name)),
68 android.PathForOutput(ctx, fmt.Sprintf("%s/enforce_uses_libraries.status", name)))
69}
70
Jiakai Zhang519c5c82021-09-16 06:15:39 +000071func createTestModuleConfig(name, dexLocation string, buildPath, dexPath, enforceUsesLibrariesStatusFile android.OutputPath) *ModuleConfig {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000072 return &ModuleConfig{
Anton Hansson4ec91dd2019-10-02 17:42:44 +010073 Name: name,
Jiakai Zhang519c5c82021-09-16 06:15:39 +000074 DexLocation: dexLocation,
75 BuildPath: buildPath,
76 DexPath: dexPath,
Colin Cross69f59a32019-02-15 10:39:37 -080077 UncompressedDex: false,
78 HasApkLibraries: false,
79 PreoptFlags: nil,
80 ProfileClassListing: android.OptionalPath{},
81 ProfileIsTextListing: false,
Jiakai Zhang519c5c82021-09-16 06:15:39 +000082 EnforceUsesLibrariesStatusFile: enforceUsesLibrariesStatusFile,
Colin Cross69f59a32019-02-15 10:39:37 -080083 EnforceUsesLibraries: false,
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000084 ClassLoaderContexts: nil,
Colin Cross69f59a32019-02-15 10:39:37 -080085 Archs: []android.ArchType{android.Arm},
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000086 DexPreoptImagesDeps: []android.OutputPaths{android.OutputPaths{}},
Jeongik Chaa5969092021-05-07 18:53:21 +090087 DexPreoptImageLocationsOnHost: []string{},
Colin Cross69f59a32019-02-15 10:39:37 -080088 PreoptBootClassPathDexFiles: nil,
89 PreoptBootClassPathDexLocations: nil,
90 PreoptExtractedApk: false,
91 NoCreateAppImage: false,
92 ForceCreateAppImage: false,
93 PresignedPrebuilt: false,
Colin Cross69f59a32019-02-15 10:39:37 -080094 }
Colin Cross43f08db2018-11-12 10:13:39 -080095}
96
97func TestDexPreopt(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000098 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -080099 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +0000100 globalSoong := globalSoongConfigForTests()
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000101 global := GlobalConfigForTests(ctx)
102 module := testSystemModuleConfig(ctx, "test")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100103 productPackages := android.PathForTesting("product_packages.txt")
Colin Cross43f08db2018-11-12 10:13:39 -0800104
Jiakai Zhanga4496782023-05-17 16:57:30 +0100105 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages)
Colin Cross43f08db2018-11-12 10:13:39 -0800106 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800107 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800108 }
109
Colin Crossdeabb942019-02-11 14:11:09 -0800110 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800111 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
112 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800113 }
114
Colin Cross2cdd5df2019-02-25 10:25:24 -0800115 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800116 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
117 }
118}
119
120func TestDexPreoptSystemOther(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000121 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800122 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +0000123 globalSoong := globalSoongConfigForTests()
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100124 global := GlobalConfigForTests(ctx)
125 systemModule := testSystemModuleConfig(ctx, "Stest")
126 systemProductModule := testSystemProductModuleConfig(ctx, "SPtest")
127 productModule := testProductModuleConfig(ctx, "Ptest")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100128 productPackages := android.PathForTesting("product_packages.txt")
Colin Cross43f08db2018-11-12 10:13:39 -0800129
130 global.HasSystemOther = true
Colin Cross43f08db2018-11-12 10:13:39 -0800131
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100132 type moduleTest struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000133 module *ModuleConfig
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100134 expectedPartition string
135 }
136 tests := []struct {
137 patterns []string
138 moduleTests []moduleTest
139 }{
140 {
141 patterns: []string{"app/%"},
142 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100143 {module: systemModule, expectedPartition: "system_other/system"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100144 {module: systemProductModule, expectedPartition: "system/product"},
145 {module: productModule, expectedPartition: "product"},
146 },
147 },
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000148 // product/app/% only applies to product apps inside the system partition
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100149 {
150 patterns: []string{"app/%", "product/app/%"},
151 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100152 {module: systemModule, expectedPartition: "system_other/system"},
153 {module: systemProductModule, expectedPartition: "system_other/system/product"},
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000154 {module: productModule, expectedPartition: "product"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100155 },
156 },
Colin Cross43f08db2018-11-12 10:13:39 -0800157 }
158
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100159 for _, test := range tests {
160 global.PatternsOnSystemOther = test.patterns
161 for _, mt := range test.moduleTests {
Jiakai Zhanga4496782023-05-17 16:57:30 +0100162 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, mt.module, productPackages)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100163 if err != nil {
164 t.Fatal(err)
165 }
166
167 name := mt.module.Name
168 wantInstalls := android.RuleBuilderInstalls{
169 {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)},
170 {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)},
171 }
172
173 if rule.Installs().String() != wantInstalls.String() {
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000174 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100175 }
176 }
Colin Cross43f08db2018-11-12 10:13:39 -0800177 }
178
Colin Cross43f08db2018-11-12 10:13:39 -0800179}
180
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000181func TestDexPreoptApexSystemServerJars(t *testing.T) {
182 config := android.TestConfig("out", nil, "", nil)
183 ctx := android.BuilderContextForTesting(config)
184 globalSoong := globalSoongConfigForTests()
185 global := GlobalConfigForTests(ctx)
186 module := testApexModuleConfig(ctx, "service-A", "com.android.apex1")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100187 productPackages := android.PathForTesting("product_packages.txt")
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000188
189 global.ApexSystemServerJars = android.CreateTestConfiguredJarList(
190 []string{"com.android.apex1:service-A"})
191
Jiakai Zhanga4496782023-05-17 16:57:30 +0100192 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000193 if err != nil {
194 t.Fatal(err)
195 }
196
197 wantInstalls := android.RuleBuilderInstalls{
198 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.odex"},
199 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.vdex"},
200 }
201
202 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
203}
204
Jiakai Zhang389a6472021-12-14 18:54:06 +0000205func TestDexPreoptStandaloneSystemServerJars(t *testing.T) {
206 config := android.TestConfig("out", nil, "", nil)
207 ctx := android.BuilderContextForTesting(config)
208 globalSoong := globalSoongConfigForTests()
209 global := GlobalConfigForTests(ctx)
210 module := testPlatformSystemServerModuleConfig(ctx, "service-A")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100211 productPackages := android.PathForTesting("product_packages.txt")
Jiakai Zhang389a6472021-12-14 18:54:06 +0000212
213 global.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(
214 []string{"platform:service-A"})
215
Jiakai Zhanga4496782023-05-17 16:57:30 +0100216 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages)
Jiakai Zhang389a6472021-12-14 18:54:06 +0000217 if err != nil {
218 t.Fatal(err)
219 }
220
221 wantInstalls := android.RuleBuilderInstalls{
222 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/service-A.odex"},
223 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/service-A.vdex"},
224 }
225
226 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
227}
228
liulvping76d56ee2022-05-07 14:40:13 +0800229func TestDexPreoptSystemExtSystemServerJars(t *testing.T) {
230 config := android.TestConfig("out", nil, "", nil)
231 ctx := android.BuilderContextForTesting(config)
232 globalSoong := globalSoongConfigForTests()
233 global := GlobalConfigForTests(ctx)
234 module := testSystemExtSystemServerModuleConfig(ctx, "service-A")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100235 productPackages := android.PathForTesting("product_packages.txt")
liulvping76d56ee2022-05-07 14:40:13 +0800236
237 global.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(
238 []string{"system_ext:service-A"})
239
Jiakai Zhanga4496782023-05-17 16:57:30 +0100240 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages)
liulvping76d56ee2022-05-07 14:40:13 +0800241 if err != nil {
242 t.Fatal(err)
243 }
244
245 wantInstalls := android.RuleBuilderInstalls{
246 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system_ext/framework/oat/arm/service-A.odex"},
247 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system_ext/framework/oat/arm/service-A.vdex"},
248 }
249
250 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
251}
252
Jiakai Zhang389a6472021-12-14 18:54:06 +0000253func TestDexPreoptApexStandaloneSystemServerJars(t *testing.T) {
254 config := android.TestConfig("out", nil, "", nil)
255 ctx := android.BuilderContextForTesting(config)
256 globalSoong := globalSoongConfigForTests()
257 global := GlobalConfigForTests(ctx)
258 module := testApexModuleConfig(ctx, "service-A", "com.android.apex1")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100259 productPackages := android.PathForTesting("product_packages.txt")
Jiakai Zhang389a6472021-12-14 18:54:06 +0000260
261 global.ApexStandaloneSystemServerJars = android.CreateTestConfiguredJarList(
262 []string{"com.android.apex1:service-A"})
263
Jiakai Zhanga4496782023-05-17 16:57:30 +0100264 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages)
Jiakai Zhang389a6472021-12-14 18:54:06 +0000265 if err != nil {
266 t.Fatal(err)
267 }
268
269 wantInstalls := android.RuleBuilderInstalls{
270 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.odex"},
271 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.vdex"},
272 }
273
274 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
275}
276
Colin Cross43f08db2018-11-12 10:13:39 -0800277func TestDexPreoptProfile(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000278 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800279 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +0000280 globalSoong := globalSoongConfigForTests()
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000281 global := GlobalConfigForTests(ctx)
282 module := testSystemModuleConfig(ctx, "test")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100283 productPackages := android.PathForTesting("product_packages.txt")
Colin Cross43f08db2018-11-12 10:13:39 -0800284
Colin Cross69f59a32019-02-15 10:39:37 -0800285 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800286
Jiakai Zhanga4496782023-05-17 16:57:30 +0100287 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages)
Colin Cross43f08db2018-11-12 10:13:39 -0800288 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800289 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800290 }
291
Colin Crossdeabb942019-02-11 14:11:09 -0800292 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800293 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
294 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
295 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
296 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800297 }
298
Colin Cross2cdd5df2019-02-25 10:25:24 -0800299 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800300 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
301 }
302}
Jeongik Chac6246672021-04-08 00:00:19 +0900303
304func TestDexPreoptConfigToJson(t *testing.T) {
305 config := android.TestConfig("out", nil, "", nil)
306 ctx := android.BuilderContextForTesting(config)
307 module := testSystemModuleConfig(ctx, "test")
308 data, err := moduleConfigToJSON(module)
309 if err != nil {
310 t.Errorf("Failed to convert module config data to JSON, %v", err)
311 }
312 parsed, err := ParseModuleConfig(ctx, data)
313 if err != nil {
314 t.Errorf("Failed to parse JSON, %v", err)
315 }
316 before := fmt.Sprintf("%v", module)
317 after := fmt.Sprintf("%v", parsed)
318 android.AssertStringEquals(t, "The result must be the same as the original after marshalling and unmarshalling it.", before, after)
319}