blob: 07e4fad012250ad7f18ee23bf2a4e70eaadd1b2f [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
Jiakai Zhang519c5c82021-09-16 06:15:39 +000062func createTestModuleConfig(name, dexLocation string, buildPath, dexPath, enforceUsesLibrariesStatusFile android.OutputPath) *ModuleConfig {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000063 return &ModuleConfig{
Anton Hansson4ec91dd2019-10-02 17:42:44 +010064 Name: name,
Jiakai Zhang519c5c82021-09-16 06:15:39 +000065 DexLocation: dexLocation,
66 BuildPath: buildPath,
67 DexPath: dexPath,
Colin Cross69f59a32019-02-15 10:39:37 -080068 UncompressedDex: false,
69 HasApkLibraries: false,
70 PreoptFlags: nil,
71 ProfileClassListing: android.OptionalPath{},
72 ProfileIsTextListing: false,
Jiakai Zhang519c5c82021-09-16 06:15:39 +000073 EnforceUsesLibrariesStatusFile: enforceUsesLibrariesStatusFile,
Colin Cross69f59a32019-02-15 10:39:37 -080074 EnforceUsesLibraries: false,
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000075 ClassLoaderContexts: nil,
Colin Cross69f59a32019-02-15 10:39:37 -080076 Archs: []android.ArchType{android.Arm},
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000077 DexPreoptImagesDeps: []android.OutputPaths{android.OutputPaths{}},
Jeongik Chaa5969092021-05-07 18:53:21 +090078 DexPreoptImageLocationsOnHost: []string{},
Colin Cross69f59a32019-02-15 10:39:37 -080079 PreoptBootClassPathDexFiles: nil,
80 PreoptBootClassPathDexLocations: nil,
81 PreoptExtractedApk: false,
82 NoCreateAppImage: false,
83 ForceCreateAppImage: false,
84 PresignedPrebuilt: false,
Colin Cross69f59a32019-02-15 10:39:37 -080085 }
Colin Cross43f08db2018-11-12 10:13:39 -080086}
87
88func TestDexPreopt(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000089 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -080090 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +000091 globalSoong := globalSoongConfigForTests()
Martin Stjernholm75a48d82020-01-10 20:32:59 +000092 global := GlobalConfigForTests(ctx)
93 module := testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -080094
Martin Stjernholm75a48d82020-01-10 20:32:59 +000095 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -080096 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080097 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -080098 }
99
Colin Crossdeabb942019-02-11 14:11:09 -0800100 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800101 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
102 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800103 }
104
Colin Cross2cdd5df2019-02-25 10:25:24 -0800105 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800106 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
107 }
108}
109
110func TestDexPreoptSystemOther(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000111 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800112 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +0000113 globalSoong := globalSoongConfigForTests()
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100114 global := GlobalConfigForTests(ctx)
115 systemModule := testSystemModuleConfig(ctx, "Stest")
116 systemProductModule := testSystemProductModuleConfig(ctx, "SPtest")
117 productModule := testProductModuleConfig(ctx, "Ptest")
Colin Cross43f08db2018-11-12 10:13:39 -0800118
119 global.HasSystemOther = true
Colin Cross43f08db2018-11-12 10:13:39 -0800120
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100121 type moduleTest struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000122 module *ModuleConfig
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100123 expectedPartition string
124 }
125 tests := []struct {
126 patterns []string
127 moduleTests []moduleTest
128 }{
129 {
130 patterns: []string{"app/%"},
131 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100132 {module: systemModule, expectedPartition: "system_other/system"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100133 {module: systemProductModule, expectedPartition: "system/product"},
134 {module: productModule, expectedPartition: "product"},
135 },
136 },
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000137 // product/app/% only applies to product apps inside the system partition
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100138 {
139 patterns: []string{"app/%", "product/app/%"},
140 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100141 {module: systemModule, expectedPartition: "system_other/system"},
142 {module: systemProductModule, expectedPartition: "system_other/system/product"},
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000143 {module: productModule, expectedPartition: "product"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100144 },
145 },
Colin Cross43f08db2018-11-12 10:13:39 -0800146 }
147
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100148 for _, test := range tests {
149 global.PatternsOnSystemOther = test.patterns
150 for _, mt := range test.moduleTests {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000151 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, mt.module)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100152 if err != nil {
153 t.Fatal(err)
154 }
155
156 name := mt.module.Name
157 wantInstalls := android.RuleBuilderInstalls{
158 {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)},
159 {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)},
160 }
161
162 if rule.Installs().String() != wantInstalls.String() {
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000163 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100164 }
165 }
Colin Cross43f08db2018-11-12 10:13:39 -0800166 }
167
Colin Cross43f08db2018-11-12 10:13:39 -0800168}
169
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000170func TestDexPreoptApexSystemServerJars(t *testing.T) {
171 config := android.TestConfig("out", nil, "", nil)
172 ctx := android.BuilderContextForTesting(config)
173 globalSoong := globalSoongConfigForTests()
174 global := GlobalConfigForTests(ctx)
175 module := testApexModuleConfig(ctx, "service-A", "com.android.apex1")
176
177 global.ApexSystemServerJars = android.CreateTestConfiguredJarList(
178 []string{"com.android.apex1:service-A"})
179
180 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
181 if err != nil {
182 t.Fatal(err)
183 }
184
185 wantInstalls := android.RuleBuilderInstalls{
186 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.odex"},
187 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.vdex"},
188 }
189
190 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
191}
192
Jiakai Zhang389a6472021-12-14 18:54:06 +0000193func TestDexPreoptStandaloneSystemServerJars(t *testing.T) {
194 config := android.TestConfig("out", nil, "", nil)
195 ctx := android.BuilderContextForTesting(config)
196 globalSoong := globalSoongConfigForTests()
197 global := GlobalConfigForTests(ctx)
198 module := testPlatformSystemServerModuleConfig(ctx, "service-A")
199
200 global.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(
201 []string{"platform:service-A"})
202
203 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
204 if err != nil {
205 t.Fatal(err)
206 }
207
208 wantInstalls := android.RuleBuilderInstalls{
209 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/service-A.odex"},
210 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/service-A.vdex"},
211 }
212
213 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
214}
215
216func TestDexPreoptApexStandaloneSystemServerJars(t *testing.T) {
217 config := android.TestConfig("out", nil, "", nil)
218 ctx := android.BuilderContextForTesting(config)
219 globalSoong := globalSoongConfigForTests()
220 global := GlobalConfigForTests(ctx)
221 module := testApexModuleConfig(ctx, "service-A", "com.android.apex1")
222
223 global.ApexStandaloneSystemServerJars = android.CreateTestConfiguredJarList(
224 []string{"com.android.apex1:service-A"})
225
226 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
227 if err != nil {
228 t.Fatal(err)
229 }
230
231 wantInstalls := android.RuleBuilderInstalls{
232 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.odex"},
233 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.vdex"},
234 }
235
236 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
237}
238
Colin Cross43f08db2018-11-12 10:13:39 -0800239func TestDexPreoptProfile(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000240 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800241 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +0000242 globalSoong := globalSoongConfigForTests()
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000243 global := GlobalConfigForTests(ctx)
244 module := testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -0800245
Colin Cross69f59a32019-02-15 10:39:37 -0800246 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800247
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000248 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -0800249 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800250 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800251 }
252
Colin Crossdeabb942019-02-11 14:11:09 -0800253 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800254 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
255 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
256 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
257 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800258 }
259
Colin Cross2cdd5df2019-02-25 10:25:24 -0800260 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800261 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
262 }
263}
Jeongik Chac6246672021-04-08 00:00:19 +0900264
265func TestDexPreoptConfigToJson(t *testing.T) {
266 config := android.TestConfig("out", nil, "", nil)
267 ctx := android.BuilderContextForTesting(config)
268 module := testSystemModuleConfig(ctx, "test")
269 data, err := moduleConfigToJSON(module)
270 if err != nil {
271 t.Errorf("Failed to convert module config data to JSON, %v", err)
272 }
273 parsed, err := ParseModuleConfig(ctx, data)
274 if err != nil {
275 t.Errorf("Failed to parse JSON, %v", err)
276 }
277 before := fmt.Sprintf("%v", module)
278 after := fmt.Sprintf("%v", parsed)
279 android.AssertStringEquals(t, "The result must be the same as the original after marshalling and unmarshalling it.", before, after)
280}