blob: 7b0f51fbdc130ea94a6b247016ca21689b62a14a [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 {
Spandan Das950deca2024-10-01 18:35:23 +000045 ret := createTestModuleConfig(
Jiakai Zhang519c5c82021-09-16 06:15:39 +000046 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)))
Spandan Das950deca2024-10-01 18:35:23 +000051 ret.ApexPartition = "/system"
52 return ret
Jiakai Zhang519c5c82021-09-16 06:15:39 +000053}
54
Jiakai Zhang389a6472021-12-14 18:54:06 +000055func testPlatformSystemServerModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
56 return createTestModuleConfig(
57 name,
58 fmt.Sprintf("/system/framework/%s.jar", name),
59 android.PathForOutput(ctx, fmt.Sprintf("%s/dexpreopt/%s.jar", name, name)),
60 android.PathForOutput(ctx, fmt.Sprintf("%s/aligned/%s.jar", name, name)),
61 android.PathForOutput(ctx, fmt.Sprintf("%s/enforce_uses_libraries.status", name)))
62}
63
liulvping76d56ee2022-05-07 14:40:13 +080064func testSystemExtSystemServerModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
65 return createTestModuleConfig(
66 name,
67 fmt.Sprintf("/system_ext/framework/%s.jar", name),
68 android.PathForOutput(ctx, fmt.Sprintf("%s/dexpreopt/%s.jar", name, name)),
69 android.PathForOutput(ctx, fmt.Sprintf("%s/aligned/%s.jar", name, name)),
70 android.PathForOutput(ctx, fmt.Sprintf("%s/enforce_uses_libraries.status", name)))
71}
72
Jiakai Zhang519c5c82021-09-16 06:15:39 +000073func createTestModuleConfig(name, dexLocation string, buildPath, dexPath, enforceUsesLibrariesStatusFile android.OutputPath) *ModuleConfig {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000074 return &ModuleConfig{
Anton Hansson4ec91dd2019-10-02 17:42:44 +010075 Name: name,
Jiakai Zhang519c5c82021-09-16 06:15:39 +000076 DexLocation: dexLocation,
77 BuildPath: buildPath,
78 DexPath: dexPath,
Colin Cross69f59a32019-02-15 10:39:37 -080079 UncompressedDex: false,
80 HasApkLibraries: false,
81 PreoptFlags: nil,
82 ProfileClassListing: android.OptionalPath{},
83 ProfileIsTextListing: false,
Jiakai Zhang519c5c82021-09-16 06:15:39 +000084 EnforceUsesLibrariesStatusFile: enforceUsesLibrariesStatusFile,
Colin Cross69f59a32019-02-15 10:39:37 -080085 EnforceUsesLibraries: false,
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000086 ClassLoaderContexts: nil,
Colin Cross69f59a32019-02-15 10:39:37 -080087 Archs: []android.ArchType{android.Arm},
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000088 DexPreoptImagesDeps: []android.OutputPaths{android.OutputPaths{}},
Jeongik Chaa5969092021-05-07 18:53:21 +090089 DexPreoptImageLocationsOnHost: []string{},
Colin Cross69f59a32019-02-15 10:39:37 -080090 PreoptBootClassPathDexFiles: nil,
91 PreoptBootClassPathDexLocations: nil,
Colin Cross69f59a32019-02-15 10:39:37 -080092 NoCreateAppImage: false,
93 ForceCreateAppImage: false,
94 PresignedPrebuilt: false,
Colin Cross69f59a32019-02-15 10:39:37 -080095 }
Colin Cross43f08db2018-11-12 10:13:39 -080096}
97
98func TestDexPreopt(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000099 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800100 ctx := android.BuilderContextForTesting(config)
Jiakai Zhang7d292222024-01-18 17:27:42 +0000101 globalSoong := globalSoongConfigForTests(ctx)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000102 global := GlobalConfigForTests(ctx)
103 module := testSystemModuleConfig(ctx, "test")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100104 productPackages := android.PathForTesting("product_packages.txt")
Colin Cross43f08db2018-11-12 10:13:39 -0800105
Spandan Das5ae65ee2024-04-16 22:03:26 +0000106 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages, true)
Colin Cross43f08db2018-11-12 10:13:39 -0800107 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800108 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800109 }
110
Colin Crossdeabb942019-02-11 14:11:09 -0800111 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800112 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
113 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800114 }
115
Colin Cross2cdd5df2019-02-25 10:25:24 -0800116 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800117 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
118 }
Jiakai Zhang7d292222024-01-18 17:27:42 +0000119
120 android.AssertStringListContains(t, "", rule.Inputs().RelativeToTop().Strings(),
121 "out/soong/dexpreopt_test/uffd_gc_flag.txt")
Colin Cross43f08db2018-11-12 10:13:39 -0800122}
123
124func TestDexPreoptSystemOther(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000125 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800126 ctx := android.BuilderContextForTesting(config)
Jiakai Zhang7d292222024-01-18 17:27:42 +0000127 globalSoong := globalSoongConfigForTests(ctx)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100128 global := GlobalConfigForTests(ctx)
129 systemModule := testSystemModuleConfig(ctx, "Stest")
130 systemProductModule := testSystemProductModuleConfig(ctx, "SPtest")
131 productModule := testProductModuleConfig(ctx, "Ptest")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100132 productPackages := android.PathForTesting("product_packages.txt")
Colin Cross43f08db2018-11-12 10:13:39 -0800133
134 global.HasSystemOther = true
Colin Cross43f08db2018-11-12 10:13:39 -0800135
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100136 type moduleTest struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000137 module *ModuleConfig
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100138 expectedPartition string
139 }
140 tests := []struct {
141 patterns []string
142 moduleTests []moduleTest
143 }{
144 {
145 patterns: []string{"app/%"},
146 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100147 {module: systemModule, expectedPartition: "system_other/system"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100148 {module: systemProductModule, expectedPartition: "system/product"},
149 {module: productModule, expectedPartition: "product"},
150 },
151 },
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000152 // product/app/% only applies to product apps inside the system partition
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100153 {
154 patterns: []string{"app/%", "product/app/%"},
155 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100156 {module: systemModule, expectedPartition: "system_other/system"},
157 {module: systemProductModule, expectedPartition: "system_other/system/product"},
Spandan Dasa2e7d232024-06-24 21:07:26 +0000158 {module: productModule, expectedPartition: "system_other/product"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100159 },
160 },
Colin Cross43f08db2018-11-12 10:13:39 -0800161 }
162
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100163 for _, test := range tests {
164 global.PatternsOnSystemOther = test.patterns
165 for _, mt := range test.moduleTests {
Spandan Das5ae65ee2024-04-16 22:03:26 +0000166 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, mt.module, productPackages, true)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100167 if err != nil {
168 t.Fatal(err)
169 }
170
171 name := mt.module.Name
172 wantInstalls := android.RuleBuilderInstalls{
173 {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)},
174 {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)},
175 }
176
177 if rule.Installs().String() != wantInstalls.String() {
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000178 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100179 }
180 }
Colin Cross43f08db2018-11-12 10:13:39 -0800181 }
182
Colin Cross43f08db2018-11-12 10:13:39 -0800183}
184
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000185func TestDexPreoptApexSystemServerJars(t *testing.T) {
Spandan Das5ae65ee2024-04-16 22:03:26 +0000186 // modify the global variable for test
187 var oldDexpreoptRunningInSoong = DexpreoptRunningInSoong
188 DexpreoptRunningInSoong = true
189
190 // test begin
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000191 config := android.TestConfig("out", nil, "", nil)
192 ctx := android.BuilderContextForTesting(config)
Jiakai Zhang7d292222024-01-18 17:27:42 +0000193 globalSoong := globalSoongConfigForTests(ctx)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000194 global := GlobalConfigForTests(ctx)
195 module := testApexModuleConfig(ctx, "service-A", "com.android.apex1")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100196 productPackages := android.PathForTesting("product_packages.txt")
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000197
198 global.ApexSystemServerJars = android.CreateTestConfiguredJarList(
199 []string{"com.android.apex1:service-A"})
200
Spandan Das5ae65ee2024-04-16 22:03:26 +0000201 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages, true)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000202 if err != nil {
203 t.Fatal(err)
204 }
205
206 wantInstalls := android.RuleBuilderInstalls{
207 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.odex"},
208 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.vdex"},
209 }
210
211 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
Spandan Das5ae65ee2024-04-16 22:03:26 +0000212
213 android.AssertStringListContains(t, "apex sscp jar copy", rule.Outputs().Strings(), "out/soong/system_server_dexjars/service-A.jar")
214
215 // rule with apex sscp cp as false
216 rule, err = GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages, false)
217 if err != nil {
218 t.Fatal(err)
219 }
220 android.AssertStringListDoesNotContain(t, "apex sscp jar copy", rule.Outputs().Strings(), "out/soong/system_server_dexjars/service-A.jar")
221
222 // cleanup the global variable for test
223 DexpreoptRunningInSoong = oldDexpreoptRunningInSoong
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000224}
225
Spandan Das950deca2024-10-01 18:35:23 +0000226// Same as `TestDexPreoptApexSystemServerJars`, but the apex jar is in /system_ext
227func TestDexPreoptApexSystemServerJarsSystemExt(t *testing.T) {
228 // modify the global variable for test
229 var oldDexpreoptRunningInSoong = DexpreoptRunningInSoong
230 DexpreoptRunningInSoong = true
231
232 // test begin
233 config := android.TestConfig("out", nil, "", nil)
234 ctx := android.BuilderContextForTesting(config)
235 globalSoong := globalSoongConfigForTests(ctx)
236 global := GlobalConfigForTests(ctx)
237 module := testApexModuleConfig(ctx, "service-A", "com.android.apex1")
238 module.ApexPartition = "/system_ext"
239 productPackages := android.PathForTesting("product_packages.txt")
240
241 global.ApexSystemServerJars = android.CreateTestConfiguredJarList(
242 []string{"com.android.apex1:service-A"})
243
244 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages, true)
245 if err != nil {
246 t.Fatal(err)
247 }
248
249 wantInstalls := android.RuleBuilderInstalls{
250 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system_ext/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.odex"},
251 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system_ext/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.vdex"},
252 }
253
254 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
255
256 android.AssertStringListContains(t, "apex sscp jar copy", rule.Outputs().Strings(), "out/soong/system_server_dexjars/service-A.jar")
257
258 // rule with apex sscp cp as false
259 rule, err = GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages, false)
260 if err != nil {
261 t.Fatal(err)
262 }
263 android.AssertStringListDoesNotContain(t, "apex sscp jar copy", rule.Outputs().Strings(), "out/soong/system_server_dexjars/service-A.jar")
264
265 // cleanup the global variable for test
266 DexpreoptRunningInSoong = oldDexpreoptRunningInSoong
267}
268
Jiakai Zhang389a6472021-12-14 18:54:06 +0000269func TestDexPreoptStandaloneSystemServerJars(t *testing.T) {
270 config := android.TestConfig("out", nil, "", nil)
271 ctx := android.BuilderContextForTesting(config)
Jiakai Zhang7d292222024-01-18 17:27:42 +0000272 globalSoong := globalSoongConfigForTests(ctx)
Jiakai Zhang389a6472021-12-14 18:54:06 +0000273 global := GlobalConfigForTests(ctx)
274 module := testPlatformSystemServerModuleConfig(ctx, "service-A")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100275 productPackages := android.PathForTesting("product_packages.txt")
Jiakai Zhang389a6472021-12-14 18:54:06 +0000276
277 global.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(
278 []string{"platform:service-A"})
279
Spandan Das5ae65ee2024-04-16 22:03:26 +0000280 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages, true)
Jiakai Zhang389a6472021-12-14 18:54:06 +0000281 if err != nil {
282 t.Fatal(err)
283 }
284
285 wantInstalls := android.RuleBuilderInstalls{
286 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/service-A.odex"},
287 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/service-A.vdex"},
288 }
289
290 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
291}
292
liulvping76d56ee2022-05-07 14:40:13 +0800293func TestDexPreoptSystemExtSystemServerJars(t *testing.T) {
294 config := android.TestConfig("out", nil, "", nil)
295 ctx := android.BuilderContextForTesting(config)
Jiakai Zhang7d292222024-01-18 17:27:42 +0000296 globalSoong := globalSoongConfigForTests(ctx)
liulvping76d56ee2022-05-07 14:40:13 +0800297 global := GlobalConfigForTests(ctx)
298 module := testSystemExtSystemServerModuleConfig(ctx, "service-A")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100299 productPackages := android.PathForTesting("product_packages.txt")
liulvping76d56ee2022-05-07 14:40:13 +0800300
301 global.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(
302 []string{"system_ext:service-A"})
303
Spandan Das5ae65ee2024-04-16 22:03:26 +0000304 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages, true)
liulvping76d56ee2022-05-07 14:40:13 +0800305 if err != nil {
306 t.Fatal(err)
307 }
308
309 wantInstalls := android.RuleBuilderInstalls{
310 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system_ext/framework/oat/arm/service-A.odex"},
311 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system_ext/framework/oat/arm/service-A.vdex"},
312 }
313
314 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
315}
316
Jiakai Zhang389a6472021-12-14 18:54:06 +0000317func TestDexPreoptApexStandaloneSystemServerJars(t *testing.T) {
318 config := android.TestConfig("out", nil, "", nil)
319 ctx := android.BuilderContextForTesting(config)
Jiakai Zhang7d292222024-01-18 17:27:42 +0000320 globalSoong := globalSoongConfigForTests(ctx)
Jiakai Zhang389a6472021-12-14 18:54:06 +0000321 global := GlobalConfigForTests(ctx)
322 module := testApexModuleConfig(ctx, "service-A", "com.android.apex1")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100323 productPackages := android.PathForTesting("product_packages.txt")
Jiakai Zhang389a6472021-12-14 18:54:06 +0000324
325 global.ApexStandaloneSystemServerJars = android.CreateTestConfiguredJarList(
326 []string{"com.android.apex1:service-A"})
327
Spandan Das5ae65ee2024-04-16 22:03:26 +0000328 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages, true)
Jiakai Zhang389a6472021-12-14 18:54:06 +0000329 if err != nil {
330 t.Fatal(err)
331 }
332
333 wantInstalls := android.RuleBuilderInstalls{
334 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.odex"},
335 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.vdex"},
336 }
337
338 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
339}
340
Colin Cross43f08db2018-11-12 10:13:39 -0800341func TestDexPreoptProfile(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000342 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800343 ctx := android.BuilderContextForTesting(config)
Jiakai Zhang7d292222024-01-18 17:27:42 +0000344 globalSoong := globalSoongConfigForTests(ctx)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000345 global := GlobalConfigForTests(ctx)
346 module := testSystemModuleConfig(ctx, "test")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100347 productPackages := android.PathForTesting("product_packages.txt")
Colin Cross43f08db2018-11-12 10:13:39 -0800348
Colin Cross69f59a32019-02-15 10:39:37 -0800349 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800350
Spandan Das5ae65ee2024-04-16 22:03:26 +0000351 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module, productPackages, true)
Colin Cross43f08db2018-11-12 10:13:39 -0800352 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800353 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800354 }
355
Colin Crossdeabb942019-02-11 14:11:09 -0800356 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800357 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
358 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
359 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
360 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800361 }
362
Colin Cross2cdd5df2019-02-25 10:25:24 -0800363 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800364 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
365 }
366}
Jeongik Chac6246672021-04-08 00:00:19 +0900367
368func TestDexPreoptConfigToJson(t *testing.T) {
369 config := android.TestConfig("out", nil, "", nil)
370 ctx := android.BuilderContextForTesting(config)
371 module := testSystemModuleConfig(ctx, "test")
372 data, err := moduleConfigToJSON(module)
373 if err != nil {
374 t.Errorf("Failed to convert module config data to JSON, %v", err)
375 }
376 parsed, err := ParseModuleConfig(ctx, data)
377 if err != nil {
378 t.Errorf("Failed to parse JSON, %v", err)
379 }
380 before := fmt.Sprintf("%v", module)
381 after := fmt.Sprintf("%v", parsed)
382 android.AssertStringEquals(t, "The result must be the same as the original after marshalling and unmarshalling it.", before, after)
383}
Jiakai Zhang7d292222024-01-18 17:27:42 +0000384
385func TestUffdGcFlagForce(t *testing.T) {
386 for _, enableUffdGc := range []string{"true", "false"} {
387 t.Run(enableUffdGc, func(t *testing.T) {
388 preparers := android.GroupFixturePreparers(
389 PrepareForTestWithFakeDex2oatd,
390 PrepareForTestWithDexpreoptConfig,
391 FixtureSetEnableUffdGc(enableUffdGc),
392 )
393
394 result := preparers.RunTest(t)
395 ctx := result.TestContext
396
397 ctx.SingletonForTests("dexpreopt-soong-config").Output("out/soong/dexpreopt/uffd_gc_flag.txt")
398 })
399 }
400}
401
402func TestUffdGcFlagDefault(t *testing.T) {
403 preparers := android.GroupFixturePreparers(
404 PrepareForTestWithFakeDex2oatd,
405 PrepareForTestWithDexpreoptConfig,
406 FixtureSetEnableUffdGc("default"),
407 )
408
409 result := preparers.RunTest(t)
410 ctx := result.TestContext
411 config := ctx.Config()
412
413 rule := ctx.SingletonForTests("dexpreopt-soong-config").Rule("dexpreopt_uffd_gc_flag")
414
415 android.AssertStringDoesContain(t, "", rule.RuleParams.Command, "construct_uffd_gc_flag")
416 android.AssertStringPathsRelativeToTopEquals(t, "", config, []string{
417 "out/soong/dexpreopt/uffd_gc_flag.txt",
418 }, rule.AllOutputs())
419 android.AssertPathsRelativeToTopEquals(t, "", []string{
420 "out/soong/dexpreopt/kernel_version_for_uffd_gc.txt",
421 }, rule.Implicits)
422}
423
424func TestUffdGcFlagBogus(t *testing.T) {
425 preparers := android.GroupFixturePreparers(
426 PrepareForTestWithFakeDex2oatd,
427 PrepareForTestWithDexpreoptConfig,
428 FixtureSetEnableUffdGc("bogus"),
429 )
430
431 preparers.
432 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
433 "Unknown value of PRODUCT_ENABLE_UFFD_GC: bogus")).
434 RunTest(t)
435}