blob: 230fbb4ded91dd6d9b56d4d3b9c86093b9c13f22 [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,
Colin Cross69f59a32019-02-15 10:39:37 -080090 NoCreateAppImage: false,
91 ForceCreateAppImage: false,
92 PresignedPrebuilt: false,
Colin Cross69f59a32019-02-15 10:39:37 -080093 }
Colin Cross43f08db2018-11-12 10:13:39 -080094}
95
96func TestDexPreopt(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000097 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -080098 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +000099 globalSoong := globalSoongConfigForTests()
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000100 global := GlobalConfigForTests(ctx)
101 module := testSystemModuleConfig(ctx, "test")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100102 productPackages := android.PathForTesting("product_packages.txt")
Colin Cross43f08db2018-11-12 10:13:39 -0800103
Jiakai Zhanga4496782023-05-17 16:57:30 +0100104 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages)
Colin Cross43f08db2018-11-12 10:13:39 -0800105 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800106 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800107 }
108
Colin Crossdeabb942019-02-11 14:11:09 -0800109 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800110 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
111 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800112 }
113
Colin Cross2cdd5df2019-02-25 10:25:24 -0800114 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800115 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
116 }
117}
118
119func TestDexPreoptSystemOther(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000120 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800121 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +0000122 globalSoong := globalSoongConfigForTests()
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100123 global := GlobalConfigForTests(ctx)
124 systemModule := testSystemModuleConfig(ctx, "Stest")
125 systemProductModule := testSystemProductModuleConfig(ctx, "SPtest")
126 productModule := testProductModuleConfig(ctx, "Ptest")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100127 productPackages := android.PathForTesting("product_packages.txt")
Colin Cross43f08db2018-11-12 10:13:39 -0800128
129 global.HasSystemOther = true
Colin Cross43f08db2018-11-12 10:13:39 -0800130
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100131 type moduleTest struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000132 module *ModuleConfig
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100133 expectedPartition string
134 }
135 tests := []struct {
136 patterns []string
137 moduleTests []moduleTest
138 }{
139 {
140 patterns: []string{"app/%"},
141 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100142 {module: systemModule, expectedPartition: "system_other/system"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100143 {module: systemProductModule, expectedPartition: "system/product"},
144 {module: productModule, expectedPartition: "product"},
145 },
146 },
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000147 // product/app/% only applies to product apps inside the system partition
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100148 {
149 patterns: []string{"app/%", "product/app/%"},
150 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100151 {module: systemModule, expectedPartition: "system_other/system"},
152 {module: systemProductModule, expectedPartition: "system_other/system/product"},
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000153 {module: productModule, expectedPartition: "product"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100154 },
155 },
Colin Cross43f08db2018-11-12 10:13:39 -0800156 }
157
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100158 for _, test := range tests {
159 global.PatternsOnSystemOther = test.patterns
160 for _, mt := range test.moduleTests {
Jiakai Zhanga4496782023-05-17 16:57:30 +0100161 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, mt.module, productPackages)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100162 if err != nil {
163 t.Fatal(err)
164 }
165
166 name := mt.module.Name
167 wantInstalls := android.RuleBuilderInstalls{
168 {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)},
169 {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)},
170 }
171
172 if rule.Installs().String() != wantInstalls.String() {
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000173 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100174 }
175 }
Colin Cross43f08db2018-11-12 10:13:39 -0800176 }
177
Colin Cross43f08db2018-11-12 10:13:39 -0800178}
179
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000180func TestDexPreoptApexSystemServerJars(t *testing.T) {
181 config := android.TestConfig("out", nil, "", nil)
182 ctx := android.BuilderContextForTesting(config)
183 globalSoong := globalSoongConfigForTests()
184 global := GlobalConfigForTests(ctx)
185 module := testApexModuleConfig(ctx, "service-A", "com.android.apex1")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100186 productPackages := android.PathForTesting("product_packages.txt")
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000187
188 global.ApexSystemServerJars = android.CreateTestConfiguredJarList(
189 []string{"com.android.apex1:service-A"})
190
Jiakai Zhanga4496782023-05-17 16:57:30 +0100191 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000192 if err != nil {
193 t.Fatal(err)
194 }
195
196 wantInstalls := android.RuleBuilderInstalls{
197 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.odex"},
198 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.vdex"},
199 }
200
201 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
202}
203
Jiakai Zhang389a6472021-12-14 18:54:06 +0000204func TestDexPreoptStandaloneSystemServerJars(t *testing.T) {
205 config := android.TestConfig("out", nil, "", nil)
206 ctx := android.BuilderContextForTesting(config)
207 globalSoong := globalSoongConfigForTests()
208 global := GlobalConfigForTests(ctx)
209 module := testPlatformSystemServerModuleConfig(ctx, "service-A")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100210 productPackages := android.PathForTesting("product_packages.txt")
Jiakai Zhang389a6472021-12-14 18:54:06 +0000211
212 global.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(
213 []string{"platform:service-A"})
214
Jiakai Zhanga4496782023-05-17 16:57:30 +0100215 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages)
Jiakai Zhang389a6472021-12-14 18:54:06 +0000216 if err != nil {
217 t.Fatal(err)
218 }
219
220 wantInstalls := android.RuleBuilderInstalls{
221 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/service-A.odex"},
222 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/service-A.vdex"},
223 }
224
225 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
226}
227
liulvping76d56ee2022-05-07 14:40:13 +0800228func TestDexPreoptSystemExtSystemServerJars(t *testing.T) {
229 config := android.TestConfig("out", nil, "", nil)
230 ctx := android.BuilderContextForTesting(config)
231 globalSoong := globalSoongConfigForTests()
232 global := GlobalConfigForTests(ctx)
233 module := testSystemExtSystemServerModuleConfig(ctx, "service-A")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100234 productPackages := android.PathForTesting("product_packages.txt")
liulvping76d56ee2022-05-07 14:40:13 +0800235
236 global.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(
237 []string{"system_ext:service-A"})
238
Jiakai Zhanga4496782023-05-17 16:57:30 +0100239 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages)
liulvping76d56ee2022-05-07 14:40:13 +0800240 if err != nil {
241 t.Fatal(err)
242 }
243
244 wantInstalls := android.RuleBuilderInstalls{
245 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system_ext/framework/oat/arm/service-A.odex"},
246 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system_ext/framework/oat/arm/service-A.vdex"},
247 }
248
249 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
250}
251
Jiakai Zhang389a6472021-12-14 18:54:06 +0000252func TestDexPreoptApexStandaloneSystemServerJars(t *testing.T) {
253 config := android.TestConfig("out", nil, "", nil)
254 ctx := android.BuilderContextForTesting(config)
255 globalSoong := globalSoongConfigForTests()
256 global := GlobalConfigForTests(ctx)
257 module := testApexModuleConfig(ctx, "service-A", "com.android.apex1")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100258 productPackages := android.PathForTesting("product_packages.txt")
Jiakai Zhang389a6472021-12-14 18:54:06 +0000259
260 global.ApexStandaloneSystemServerJars = android.CreateTestConfiguredJarList(
261 []string{"com.android.apex1:service-A"})
262
Jiakai Zhanga4496782023-05-17 16:57:30 +0100263 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages)
Jiakai Zhang389a6472021-12-14 18:54:06 +0000264 if err != nil {
265 t.Fatal(err)
266 }
267
268 wantInstalls := android.RuleBuilderInstalls{
269 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.odex"},
270 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.vdex"},
271 }
272
273 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
274}
275
Colin Cross43f08db2018-11-12 10:13:39 -0800276func TestDexPreoptProfile(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000277 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800278 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +0000279 globalSoong := globalSoongConfigForTests()
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000280 global := GlobalConfigForTests(ctx)
281 module := testSystemModuleConfig(ctx, "test")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100282 productPackages := android.PathForTesting("product_packages.txt")
Colin Cross43f08db2018-11-12 10:13:39 -0800283
Colin Cross69f59a32019-02-15 10:39:37 -0800284 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800285
Jiakai Zhanga4496782023-05-17 16:57:30 +0100286 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages)
Colin Cross43f08db2018-11-12 10:13:39 -0800287 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800288 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800289 }
290
Colin Crossdeabb942019-02-11 14:11:09 -0800291 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800292 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
293 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
294 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
295 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800296 }
297
Colin Cross2cdd5df2019-02-25 10:25:24 -0800298 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800299 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
300 }
301}
Jeongik Chac6246672021-04-08 00:00:19 +0900302
303func TestDexPreoptConfigToJson(t *testing.T) {
304 config := android.TestConfig("out", nil, "", nil)
305 ctx := android.BuilderContextForTesting(config)
306 module := testSystemModuleConfig(ctx, "test")
307 data, err := moduleConfigToJSON(module)
308 if err != nil {
309 t.Errorf("Failed to convert module config data to JSON, %v", err)
310 }
311 parsed, err := ParseModuleConfig(ctx, data)
312 if err != nil {
313 t.Errorf("Failed to parse JSON, %v", err)
314 }
315 before := fmt.Sprintf("%v", module)
316 after := fmt.Sprintf("%v", parsed)
317 android.AssertStringEquals(t, "The result must be the same as the original after marshalling and unmarshalling it.", before, after)
318}