blob: 429b5ff1e5104529354755d5fce6d901f3ed95f2 [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")
Colin Cross43f08db2018-11-12 10:13:39 -0800103
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000104 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
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")
Colin Cross43f08db2018-11-12 10:13:39 -0800127
128 global.HasSystemOther = true
Colin Cross43f08db2018-11-12 10:13:39 -0800129
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100130 type moduleTest struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000131 module *ModuleConfig
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100132 expectedPartition string
133 }
134 tests := []struct {
135 patterns []string
136 moduleTests []moduleTest
137 }{
138 {
139 patterns: []string{"app/%"},
140 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100141 {module: systemModule, expectedPartition: "system_other/system"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100142 {module: systemProductModule, expectedPartition: "system/product"},
143 {module: productModule, expectedPartition: "product"},
144 },
145 },
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000146 // product/app/% only applies to product apps inside the system partition
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100147 {
148 patterns: []string{"app/%", "product/app/%"},
149 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100150 {module: systemModule, expectedPartition: "system_other/system"},
151 {module: systemProductModule, expectedPartition: "system_other/system/product"},
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000152 {module: productModule, expectedPartition: "product"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100153 },
154 },
Colin Cross43f08db2018-11-12 10:13:39 -0800155 }
156
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100157 for _, test := range tests {
158 global.PatternsOnSystemOther = test.patterns
159 for _, mt := range test.moduleTests {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000160 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, mt.module)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100161 if err != nil {
162 t.Fatal(err)
163 }
164
165 name := mt.module.Name
166 wantInstalls := android.RuleBuilderInstalls{
167 {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)},
168 {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)},
169 }
170
171 if rule.Installs().String() != wantInstalls.String() {
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000172 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100173 }
174 }
Colin Cross43f08db2018-11-12 10:13:39 -0800175 }
176
Colin Cross43f08db2018-11-12 10:13:39 -0800177}
178
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000179func TestDexPreoptApexSystemServerJars(t *testing.T) {
180 config := android.TestConfig("out", nil, "", nil)
181 ctx := android.BuilderContextForTesting(config)
182 globalSoong := globalSoongConfigForTests()
183 global := GlobalConfigForTests(ctx)
184 module := testApexModuleConfig(ctx, "service-A", "com.android.apex1")
185
186 global.ApexSystemServerJars = android.CreateTestConfiguredJarList(
187 []string{"com.android.apex1:service-A"})
188
189 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
190 if err != nil {
191 t.Fatal(err)
192 }
193
194 wantInstalls := android.RuleBuilderInstalls{
195 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.odex"},
196 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.vdex"},
197 }
198
199 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
200}
201
Jiakai Zhang389a6472021-12-14 18:54:06 +0000202func TestDexPreoptStandaloneSystemServerJars(t *testing.T) {
203 config := android.TestConfig("out", nil, "", nil)
204 ctx := android.BuilderContextForTesting(config)
205 globalSoong := globalSoongConfigForTests()
206 global := GlobalConfigForTests(ctx)
207 module := testPlatformSystemServerModuleConfig(ctx, "service-A")
208
209 global.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(
210 []string{"platform:service-A"})
211
212 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
213 if err != nil {
214 t.Fatal(err)
215 }
216
217 wantInstalls := android.RuleBuilderInstalls{
218 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/service-A.odex"},
219 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/service-A.vdex"},
220 }
221
222 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
223}
224
liulvping76d56ee2022-05-07 14:40:13 +0800225func TestDexPreoptSystemExtSystemServerJars(t *testing.T) {
226 config := android.TestConfig("out", nil, "", nil)
227 ctx := android.BuilderContextForTesting(config)
228 globalSoong := globalSoongConfigForTests()
229 global := GlobalConfigForTests(ctx)
230 module := testSystemExtSystemServerModuleConfig(ctx, "service-A")
231
232 global.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(
233 []string{"system_ext:service-A"})
234
235 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
236 if err != nil {
237 t.Fatal(err)
238 }
239
240 wantInstalls := android.RuleBuilderInstalls{
241 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system_ext/framework/oat/arm/service-A.odex"},
242 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system_ext/framework/oat/arm/service-A.vdex"},
243 }
244
245 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
246}
247
Jiakai Zhang389a6472021-12-14 18:54:06 +0000248func TestDexPreoptApexStandaloneSystemServerJars(t *testing.T) {
249 config := android.TestConfig("out", nil, "", nil)
250 ctx := android.BuilderContextForTesting(config)
251 globalSoong := globalSoongConfigForTests()
252 global := GlobalConfigForTests(ctx)
253 module := testApexModuleConfig(ctx, "service-A", "com.android.apex1")
254
255 global.ApexStandaloneSystemServerJars = android.CreateTestConfiguredJarList(
256 []string{"com.android.apex1:service-A"})
257
258 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
259 if err != nil {
260 t.Fatal(err)
261 }
262
263 wantInstalls := android.RuleBuilderInstalls{
264 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.odex"},
265 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.vdex"},
266 }
267
268 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
269}
270
Colin Cross43f08db2018-11-12 10:13:39 -0800271func TestDexPreoptProfile(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000272 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800273 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +0000274 globalSoong := globalSoongConfigForTests()
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000275 global := GlobalConfigForTests(ctx)
276 module := testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -0800277
Colin Cross69f59a32019-02-15 10:39:37 -0800278 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800279
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000280 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -0800281 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800282 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800283 }
284
Colin Crossdeabb942019-02-11 14:11:09 -0800285 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800286 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
287 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
288 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
289 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800290 }
291
Colin Cross2cdd5df2019-02-25 10:25:24 -0800292 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800293 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
294 }
295}
Jeongik Chac6246672021-04-08 00:00:19 +0900296
297func TestDexPreoptConfigToJson(t *testing.T) {
298 config := android.TestConfig("out", nil, "", nil)
299 ctx := android.BuilderContextForTesting(config)
300 module := testSystemModuleConfig(ctx, "test")
301 data, err := moduleConfigToJSON(module)
302 if err != nil {
303 t.Errorf("Failed to convert module config data to JSON, %v", err)
304 }
305 parsed, err := ParseModuleConfig(ctx, data)
306 if err != nil {
307 t.Errorf("Failed to parse JSON, %v", err)
308 }
309 before := fmt.Sprintf("%v", module)
310 after := fmt.Sprintf("%v", parsed)
311 android.AssertStringEquals(t, "The result must be the same as the original after marshalling and unmarshalling it.", before, after)
312}