blob: 78f2f3fb5a930eb34fba71b8b9103419a236caf6 [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 "reflect"
21 "strings"
22 "testing"
23)
24
Anton Hansson4ec91dd2019-10-02 17:42:44 +010025func testSystemModuleConfig(ctx android.PathContext, name string) ModuleConfig {
26 return testModuleConfig(ctx, name, "system")
27}
28
29func testSystemProductModuleConfig(ctx android.PathContext, name string) ModuleConfig {
30 return testModuleConfig(ctx, name, "system/product")
31}
32
33func testProductModuleConfig(ctx android.PathContext, name string) ModuleConfig {
34 return testModuleConfig(ctx, name, "product")
35}
36
37func testModuleConfig(ctx android.PathContext, name, partition string) ModuleConfig {
Colin Cross69f59a32019-02-15 10:39:37 -080038 return ModuleConfig{
Anton Hansson4ec91dd2019-10-02 17:42:44 +010039 Name: name,
40 DexLocation: fmt.Sprintf("/%s/app/test/%s.apk", partition, name),
41 BuildPath: android.PathForOutput(ctx, fmt.Sprintf("%s/%s.apk", name, name)),
42 DexPath: android.PathForOutput(ctx, fmt.Sprintf("%s/dex/%s.jar", name, name)),
Colin Cross69f59a32019-02-15 10:39:37 -080043 UncompressedDex: false,
44 HasApkLibraries: false,
45 PreoptFlags: nil,
46 ProfileClassListing: android.OptionalPath{},
47 ProfileIsTextListing: false,
48 EnforceUsesLibraries: false,
Colin Cross50ddcc42019-05-16 12:28:22 -070049 PresentOptionalUsesLibraries: nil,
Colin Cross69f59a32019-02-15 10:39:37 -080050 UsesLibraries: nil,
51 LibraryPaths: nil,
52 Archs: []android.ArchType{android.Arm},
53 DexPreoptImages: android.Paths{android.PathForTesting("system/framework/arm/boot.art")},
Dan Willemsen0f416782019-06-13 21:44:53 +000054 DexPreoptImagesDeps: []android.Paths{android.Paths{}},
Colin Cross69f59a32019-02-15 10:39:37 -080055 PreoptBootClassPathDexFiles: nil,
56 PreoptBootClassPathDexLocations: nil,
57 PreoptExtractedApk: false,
58 NoCreateAppImage: false,
59 ForceCreateAppImage: false,
60 PresignedPrebuilt: false,
61 NoStripping: false,
Anton Hansson4ec91dd2019-10-02 17:42:44 +010062 StripInputPath: android.PathForOutput(ctx, fmt.Sprintf("unstripped/%s.apk", name)),
63 StripOutputPath: android.PathForOutput(ctx, fmt.Sprintf("stripped/%s.apk", name)),
Colin Cross69f59a32019-02-15 10:39:37 -080064 }
Colin Cross43f08db2018-11-12 10:13:39 -080065}
66
67func TestDexPreopt(t *testing.T) {
Colin Cross69f59a32019-02-15 10:39:37 -080068 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
Anton Hansson4ec91dd2019-10-02 17:42:44 +010069 global, module := GlobalConfigForTests(ctx), testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -080070
Colin Cross69f59a32019-02-15 10:39:37 -080071 rule, err := GenerateDexpreoptRule(ctx, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -080072 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080073 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -080074 }
75
Colin Crossdeabb942019-02-11 14:11:09 -080076 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -080077 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
78 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -080079 }
80
Colin Cross2cdd5df2019-02-25 10:25:24 -080081 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -080082 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
83 }
84}
85
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000086func TestDexPreoptStrip(t *testing.T) {
87 // Test that we panic if we strip in a configuration where stripping is not allowed.
Colin Cross69f59a32019-02-15 10:39:37 -080088 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
Anton Hansson4ec91dd2019-10-02 17:42:44 +010089 global, module := GlobalConfigForTests(ctx), testSystemModuleConfig(ctx, "test")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000090
91 global.NeverAllowStripping = true
92 module.NoStripping = false
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000093
94 _, err := GenerateStripRule(global, module)
95 if err == nil {
96 t.Errorf("Expected an error when calling GenerateStripRule on a stripped module")
97 }
98}
99
Colin Cross43f08db2018-11-12 10:13:39 -0800100func TestDexPreoptSystemOther(t *testing.T) {
Colin Cross69f59a32019-02-15 10:39:37 -0800101 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100102 global := GlobalConfigForTests(ctx)
103 systemModule := testSystemModuleConfig(ctx, "Stest")
104 systemProductModule := testSystemProductModuleConfig(ctx, "SPtest")
105 productModule := testProductModuleConfig(ctx, "Ptest")
Colin Cross43f08db2018-11-12 10:13:39 -0800106
107 global.HasSystemOther = true
Colin Cross43f08db2018-11-12 10:13:39 -0800108
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100109 type moduleTest struct {
110 module ModuleConfig
111 expectedPartition string
112 }
113 tests := []struct {
114 patterns []string
115 moduleTests []moduleTest
116 }{
117 {
118 patterns: []string{"app/%"},
119 moduleTests: []moduleTest{
120 {module: systemModule, expectedPartition: "system_other"},
121 {module: systemProductModule, expectedPartition: "system/product"},
122 {module: productModule, expectedPartition: "product"},
123 },
124 },
125 // product/app/% only applies to product apps inside the system partition
126 {
127 patterns: []string{"app/%", "product/app/%"},
128 moduleTests: []moduleTest{
129 {module: systemModule, expectedPartition: "system_other"},
130 {module: systemProductModule, expectedPartition: "system_other/product"},
131 {module: productModule, expectedPartition: "product"},
132 },
133 },
Colin Cross43f08db2018-11-12 10:13:39 -0800134 }
135
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100136 for _, test := range tests {
137 global.PatternsOnSystemOther = test.patterns
138 for _, mt := range test.moduleTests {
139 rule, err := GenerateDexpreoptRule(ctx, global, mt.module)
140 if err != nil {
141 t.Fatal(err)
142 }
143
144 name := mt.module.Name
145 wantInstalls := android.RuleBuilderInstalls{
146 {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)},
147 {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)},
148 }
149
150 if rule.Installs().String() != wantInstalls.String() {
151 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
152 }
153 }
Colin Cross43f08db2018-11-12 10:13:39 -0800154 }
155
Colin Cross43f08db2018-11-12 10:13:39 -0800156}
157
158func TestDexPreoptProfile(t *testing.T) {
Colin Cross69f59a32019-02-15 10:39:37 -0800159 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100160 global, module := GlobalConfigForTests(ctx), testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -0800161
Colin Cross69f59a32019-02-15 10:39:37 -0800162 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800163
Colin Cross69f59a32019-02-15 10:39:37 -0800164 rule, err := GenerateDexpreoptRule(ctx, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -0800165 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800166 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800167 }
168
Colin Crossdeabb942019-02-11 14:11:09 -0800169 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800170 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
171 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
172 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
173 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800174 }
175
Colin Cross2cdd5df2019-02-25 10:25:24 -0800176 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800177 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
178 }
179}
180
181func TestStripDex(t *testing.T) {
Colin Cross8c6d2502019-01-09 21:09:14 -0800182 tests := []struct {
183 name string
184 setup func(global *GlobalConfig, module *ModuleConfig)
185 strip bool
186 }{
187 {
188 name: "default strip",
189 setup: func(global *GlobalConfig, module *ModuleConfig) {},
190 strip: true,
191 },
192 {
193 name: "global no stripping",
194 setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true },
195 strip: false,
196 },
197 {
198 name: "module no stripping",
199 setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true },
200 strip: false,
201 },
Colin Cross43f08db2018-11-12 10:13:39 -0800202 }
203
Colin Cross8c6d2502019-01-09 21:09:14 -0800204 for _, test := range tests {
205 t.Run(test.name, func(t *testing.T) {
Colin Cross43f08db2018-11-12 10:13:39 -0800206
Colin Cross69f59a32019-02-15 10:39:37 -0800207 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100208 global, module := GlobalConfigForTests(ctx), testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -0800209
Colin Cross8c6d2502019-01-09 21:09:14 -0800210 test.setup(&global, &module)
Colin Cross43f08db2018-11-12 10:13:39 -0800211
Colin Cross8c6d2502019-01-09 21:09:14 -0800212 rule, err := GenerateStripRule(global, module)
213 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800214 t.Fatal(err)
Colin Cross8c6d2502019-01-09 21:09:14 -0800215 }
Colin Cross43f08db2018-11-12 10:13:39 -0800216
Colin Cross8c6d2502019-01-09 21:09:14 -0800217 if test.strip {
Colin Cross69f59a32019-02-15 10:39:37 -0800218 want := `zip2zip -i out/unstripped/test.apk -o out/stripped/test.apk -x "classes*.dex"`
Colin Cross8c6d2502019-01-09 21:09:14 -0800219 if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) {
220 t.Errorf("\nwant commands[0] to have:\n %v\ngot:\n %v", want, rule.Commands()[0])
221 }
222 } else {
223 wantCommands := []string{
Colin Cross69f59a32019-02-15 10:39:37 -0800224 "cp -f out/unstripped/test.apk out/stripped/test.apk",
Colin Cross8c6d2502019-01-09 21:09:14 -0800225 }
226 if !reflect.DeepEqual(rule.Commands(), wantCommands) {
227 t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands())
228 }
229 }
230 })
Colin Cross43f08db2018-11-12 10:13:39 -0800231 }
232}