blob: 798d776048110afc7bade2f8b9a8f356fff85a0f [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
53func createTestModuleConfig(name, dexLocation string, buildPath, dexPath, enforceUsesLibrariesStatusFile android.OutputPath) *ModuleConfig {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000054 return &ModuleConfig{
Anton Hansson4ec91dd2019-10-02 17:42:44 +010055 Name: name,
Jiakai Zhang519c5c82021-09-16 06:15:39 +000056 DexLocation: dexLocation,
57 BuildPath: buildPath,
58 DexPath: dexPath,
Colin Cross69f59a32019-02-15 10:39:37 -080059 UncompressedDex: false,
60 HasApkLibraries: false,
61 PreoptFlags: nil,
62 ProfileClassListing: android.OptionalPath{},
63 ProfileIsTextListing: false,
Jiakai Zhang519c5c82021-09-16 06:15:39 +000064 EnforceUsesLibrariesStatusFile: enforceUsesLibrariesStatusFile,
Colin Cross69f59a32019-02-15 10:39:37 -080065 EnforceUsesLibraries: false,
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000066 ClassLoaderContexts: nil,
Colin Cross69f59a32019-02-15 10:39:37 -080067 Archs: []android.ArchType{android.Arm},
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000068 DexPreoptImagesDeps: []android.OutputPaths{android.OutputPaths{}},
Jeongik Chaa5969092021-05-07 18:53:21 +090069 DexPreoptImageLocationsOnHost: []string{},
Colin Cross69f59a32019-02-15 10:39:37 -080070 PreoptBootClassPathDexFiles: nil,
71 PreoptBootClassPathDexLocations: nil,
72 PreoptExtractedApk: false,
73 NoCreateAppImage: false,
74 ForceCreateAppImage: false,
75 PresignedPrebuilt: false,
Colin Cross69f59a32019-02-15 10:39:37 -080076 }
Colin Cross43f08db2018-11-12 10:13:39 -080077}
78
79func TestDexPreopt(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000080 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -080081 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +000082 globalSoong := globalSoongConfigForTests()
Martin Stjernholm75a48d82020-01-10 20:32:59 +000083 global := GlobalConfigForTests(ctx)
84 module := testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -080085
Martin Stjernholm75a48d82020-01-10 20:32:59 +000086 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -080087 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080088 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -080089 }
90
Colin Crossdeabb942019-02-11 14:11:09 -080091 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -080092 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
93 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -080094 }
95
Colin Cross2cdd5df2019-02-25 10:25:24 -080096 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -080097 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
98 }
99}
100
101func TestDexPreoptSystemOther(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000102 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800103 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +0000104 globalSoong := globalSoongConfigForTests()
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100105 global := GlobalConfigForTests(ctx)
106 systemModule := testSystemModuleConfig(ctx, "Stest")
107 systemProductModule := testSystemProductModuleConfig(ctx, "SPtest")
108 productModule := testProductModuleConfig(ctx, "Ptest")
Colin Cross43f08db2018-11-12 10:13:39 -0800109
110 global.HasSystemOther = true
Colin Cross43f08db2018-11-12 10:13:39 -0800111
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100112 type moduleTest struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000113 module *ModuleConfig
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100114 expectedPartition string
115 }
116 tests := []struct {
117 patterns []string
118 moduleTests []moduleTest
119 }{
120 {
121 patterns: []string{"app/%"},
122 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100123 {module: systemModule, expectedPartition: "system_other/system"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100124 {module: systemProductModule, expectedPartition: "system/product"},
125 {module: productModule, expectedPartition: "product"},
126 },
127 },
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000128 // product/app/% only applies to product apps inside the system partition
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100129 {
130 patterns: []string{"app/%", "product/app/%"},
131 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100132 {module: systemModule, expectedPartition: "system_other/system"},
133 {module: systemProductModule, expectedPartition: "system_other/system/product"},
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000134 {module: productModule, expectedPartition: "product"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100135 },
136 },
Colin Cross43f08db2018-11-12 10:13:39 -0800137 }
138
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100139 for _, test := range tests {
140 global.PatternsOnSystemOther = test.patterns
141 for _, mt := range test.moduleTests {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000142 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, mt.module)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100143 if err != nil {
144 t.Fatal(err)
145 }
146
147 name := mt.module.Name
148 wantInstalls := android.RuleBuilderInstalls{
149 {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)},
150 {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)},
151 }
152
153 if rule.Installs().String() != wantInstalls.String() {
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000154 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100155 }
156 }
Colin Cross43f08db2018-11-12 10:13:39 -0800157 }
158
Colin Cross43f08db2018-11-12 10:13:39 -0800159}
160
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000161func TestDexPreoptApexSystemServerJars(t *testing.T) {
162 config := android.TestConfig("out", nil, "", nil)
163 ctx := android.BuilderContextForTesting(config)
164 globalSoong := globalSoongConfigForTests()
165 global := GlobalConfigForTests(ctx)
166 module := testApexModuleConfig(ctx, "service-A", "com.android.apex1")
167
168 global.ApexSystemServerJars = android.CreateTestConfiguredJarList(
169 []string{"com.android.apex1:service-A"})
170
171 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
172 if err != nil {
173 t.Fatal(err)
174 }
175
176 wantInstalls := android.RuleBuilderInstalls{
177 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.odex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.odex"},
178 {android.PathForOutput(ctx, "service-A/dexpreopt/oat/arm/javalib.vdex"), "/system/framework/oat/arm/apex@com.android.apex1@javalib@service-A.jar@classes.vdex"},
179 }
180
181 android.AssertStringEquals(t, "installs", wantInstalls.String(), rule.Installs().String())
182}
183
Colin Cross43f08db2018-11-12 10:13:39 -0800184func TestDexPreoptProfile(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000185 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800186 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +0000187 globalSoong := globalSoongConfigForTests()
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000188 global := GlobalConfigForTests(ctx)
189 module := testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -0800190
Colin Cross69f59a32019-02-15 10:39:37 -0800191 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800192
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000193 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -0800194 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800195 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800196 }
197
Colin Crossdeabb942019-02-11 14:11:09 -0800198 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800199 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
200 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
201 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
202 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800203 }
204
Colin Cross2cdd5df2019-02-25 10:25:24 -0800205 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800206 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
207 }
208}
Jeongik Chac6246672021-04-08 00:00:19 +0900209
210func TestDexPreoptConfigToJson(t *testing.T) {
211 config := android.TestConfig("out", nil, "", nil)
212 ctx := android.BuilderContextForTesting(config)
213 module := testSystemModuleConfig(ctx, "test")
214 data, err := moduleConfigToJSON(module)
215 if err != nil {
216 t.Errorf("Failed to convert module config data to JSON, %v", err)
217 }
218 parsed, err := ParseModuleConfig(ctx, data)
219 if err != nil {
220 t.Errorf("Failed to parse JSON, %v", err)
221 }
222 before := fmt.Sprintf("%v", module)
223 after := fmt.Sprintf("%v", parsed)
224 android.AssertStringEquals(t, "The result must be the same as the original after marshalling and unmarshalling it.", before, after)
225}