blob: bf692feb739091e8ebfb115d94b2a08e52ad59db [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{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100120 {module: systemModule, expectedPartition: "system_other/system"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100121 {module: systemProductModule, expectedPartition: "system/product"},
122 {module: productModule, expectedPartition: "product"},
123 },
124 },
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100125 {
126 patterns: []string{"app/%", "product/app/%"},
127 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100128 {module: systemModule, expectedPartition: "system_other/system"},
129 {module: systemProductModule, expectedPartition: "system_other/system/product"},
Anton Hansson12b8d422019-10-02 18:14:02 +0100130 {module: productModule, expectedPartition: "system_other/product"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100131 },
132 },
Colin Cross43f08db2018-11-12 10:13:39 -0800133 }
134
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100135 for _, test := range tests {
136 global.PatternsOnSystemOther = test.patterns
137 for _, mt := range test.moduleTests {
138 rule, err := GenerateDexpreoptRule(ctx, global, mt.module)
139 if err != nil {
140 t.Fatal(err)
141 }
142
143 name := mt.module.Name
144 wantInstalls := android.RuleBuilderInstalls{
145 {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)},
146 {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)},
147 }
148
149 if rule.Installs().String() != wantInstalls.String() {
Anton Hansson12b8d422019-10-02 18:14:02 +0100150 t.Errorf("\npatterns: %v\nwant installs:\n %v\ngot:\n %v", test.patterns, wantInstalls, rule.Installs())
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100151 }
152 }
Colin Cross43f08db2018-11-12 10:13:39 -0800153 }
154
Colin Cross43f08db2018-11-12 10:13:39 -0800155}
156
157func TestDexPreoptProfile(t *testing.T) {
Colin Cross69f59a32019-02-15 10:39:37 -0800158 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100159 global, module := GlobalConfigForTests(ctx), testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -0800160
Colin Cross69f59a32019-02-15 10:39:37 -0800161 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800162
Colin Cross69f59a32019-02-15 10:39:37 -0800163 rule, err := GenerateDexpreoptRule(ctx, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -0800164 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800165 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800166 }
167
Colin Crossdeabb942019-02-11 14:11:09 -0800168 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800169 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
170 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
171 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
172 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800173 }
174
Colin Cross2cdd5df2019-02-25 10:25:24 -0800175 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800176 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
177 }
178}
179
180func TestStripDex(t *testing.T) {
Colin Cross8c6d2502019-01-09 21:09:14 -0800181 tests := []struct {
182 name string
183 setup func(global *GlobalConfig, module *ModuleConfig)
184 strip bool
185 }{
186 {
187 name: "default strip",
188 setup: func(global *GlobalConfig, module *ModuleConfig) {},
189 strip: true,
190 },
191 {
192 name: "global no stripping",
193 setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true },
194 strip: false,
195 },
196 {
197 name: "module no stripping",
198 setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true },
199 strip: false,
200 },
Colin Cross43f08db2018-11-12 10:13:39 -0800201 }
202
Colin Cross8c6d2502019-01-09 21:09:14 -0800203 for _, test := range tests {
204 t.Run(test.name, func(t *testing.T) {
Colin Cross43f08db2018-11-12 10:13:39 -0800205
Colin Cross69f59a32019-02-15 10:39:37 -0800206 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100207 global, module := GlobalConfigForTests(ctx), testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -0800208
Colin Cross8c6d2502019-01-09 21:09:14 -0800209 test.setup(&global, &module)
Colin Cross43f08db2018-11-12 10:13:39 -0800210
Colin Cross8c6d2502019-01-09 21:09:14 -0800211 rule, err := GenerateStripRule(global, module)
212 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800213 t.Fatal(err)
Colin Cross8c6d2502019-01-09 21:09:14 -0800214 }
Colin Cross43f08db2018-11-12 10:13:39 -0800215
Colin Cross8c6d2502019-01-09 21:09:14 -0800216 if test.strip {
Colin Cross69f59a32019-02-15 10:39:37 -0800217 want := `zip2zip -i out/unstripped/test.apk -o out/stripped/test.apk -x "classes*.dex"`
Colin Cross8c6d2502019-01-09 21:09:14 -0800218 if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) {
219 t.Errorf("\nwant commands[0] to have:\n %v\ngot:\n %v", want, rule.Commands()[0])
220 }
221 } else {
222 wantCommands := []string{
Colin Cross69f59a32019-02-15 10:39:37 -0800223 "cp -f out/unstripped/test.apk out/stripped/test.apk",
Colin Cross8c6d2502019-01-09 21:09:14 -0800224 }
225 if !reflect.DeepEqual(rule.Commands(), wantCommands) {
226 t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands())
227 }
228 }
229 })
Colin Cross43f08db2018-11-12 10:13:39 -0800230 }
231}