blob: 949f91f3a926c802a243b57a6c85c24d444facaf [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"
Colin Cross43f08db2018-11-12 10:13:39 -080019 "reflect"
20 "strings"
21 "testing"
22)
23
24var testGlobalConfig = GlobalConfig{
25 DefaultNoStripping: false,
26 DisablePreoptModules: nil,
27 OnlyPreoptBootImageAndSystemServer: false,
28 HasSystemOther: false,
29 PatternsOnSystemOther: nil,
30 DisableGenerateProfile: false,
31 BootJars: nil,
Nicolas Geoffray39fe5742019-02-20 10:00:47 +000032 RuntimeApexJars: nil,
Colin Cross800fe132019-02-11 14:21:24 -080033 ProductUpdatableBootModules: nil,
34 ProductUpdatableBootLocations: nil,
Colin Cross43f08db2018-11-12 10:13:39 -080035 SystemServerJars: nil,
36 SystemServerApps: nil,
37 SpeedApps: nil,
38 PreoptFlags: nil,
39 DefaultCompilerFilter: "",
40 SystemServerCompilerFilter: "",
41 GenerateDMFiles: false,
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000042 NeverAllowStripping: false,
Colin Cross43f08db2018-11-12 10:13:39 -080043 NoDebugInfo: false,
44 AlwaysSystemServerDebugInfo: false,
45 NeverSystemServerDebugInfo: false,
46 AlwaysOtherDebugInfo: false,
47 NeverOtherDebugInfo: false,
48 MissingUsesLibraries: nil,
49 IsEng: false,
50 SanitizeLite: false,
51 DefaultAppImages: false,
52 Dex2oatXmx: "",
53 Dex2oatXms: "",
54 EmptyDirectory: "",
Colin Cross43f08db2018-11-12 10:13:39 -080055 CpuVariant: nil,
56 InstructionSetFeatures: nil,
Colin Cross800fe132019-02-11 14:21:24 -080057 DirtyImageObjects: "",
58 PreloadedClasses: "",
59 BootImageProfiles: nil,
60 BootFlags: "",
61 Dex2oatImageXmx: "",
62 Dex2oatImageXms: "",
Colin Cross43f08db2018-11-12 10:13:39 -080063 Tools: Tools{
64 Profman: "profman",
65 Dex2oat: "dex2oat",
66 Aapt: "aapt",
67 SoongZip: "soong_zip",
68 Zip2zip: "zip2zip",
69 VerifyUsesLibraries: "verify_uses_libraries.sh",
70 ConstructContext: "construct_context.sh",
71 },
72}
73
74var testModuleConfig = ModuleConfig{
Colin Cross800fe132019-02-11 14:21:24 -080075 Name: "",
76 DexLocation: "",
77 BuildPath: "",
78 DexPath: "",
79 UncompressedDex: false,
80 HasApkLibraries: false,
81 PreoptFlags: nil,
82 ProfileClassListing: "",
83 ProfileIsTextListing: false,
84 EnforceUsesLibraries: false,
85 OptionalUsesLibraries: nil,
86 UsesLibraries: nil,
87 LibraryPaths: nil,
88 Archs: []android.ArchType{android.Arm},
89 DexPreoptImages: []string{"system/framework/arm/boot.art"},
90 PreoptBootClassPathDexFiles: nil,
91 PreoptBootClassPathDexLocations: nil,
92 PreoptExtractedApk: false,
93 NoCreateAppImage: false,
94 ForceCreateAppImage: false,
95 PresignedPrebuilt: false,
96 NoStripping: false,
97 StripInputPath: "",
98 StripOutputPath: "",
Colin Cross43f08db2018-11-12 10:13:39 -080099}
100
101func TestDexPreopt(t *testing.T) {
102 global, module := testGlobalConfig, testModuleConfig
103
104 module.Name = "test"
105 module.DexLocation = "/system/app/test/test.apk"
106 module.BuildPath = "out/test/test.apk"
Colin Cross43f08db2018-11-12 10:13:39 -0800107
108 rule, err := GenerateDexpreoptRule(global, module)
109 if err != nil {
110 t.Error(err)
111 }
112
Colin Crossdeabb942019-02-11 14:11:09 -0800113 wantInstalls := android.RuleBuilderInstalls{
Colin Cross43f08db2018-11-12 10:13:39 -0800114 {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
115 {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
116 }
117
118 if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
119 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
120 }
121}
122
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000123func TestDexPreoptStrip(t *testing.T) {
124 // Test that we panic if we strip in a configuration where stripping is not allowed.
125 global, module := testGlobalConfig, testModuleConfig
126
127 global.NeverAllowStripping = true
128 module.NoStripping = false
129 module.Name = "test"
130 module.DexLocation = "/system/app/test/test.apk"
131 module.BuildPath = "out/test/test.apk"
132
133 _, err := GenerateStripRule(global, module)
134 if err == nil {
135 t.Errorf("Expected an error when calling GenerateStripRule on a stripped module")
136 }
137}
138
Colin Cross43f08db2018-11-12 10:13:39 -0800139func TestDexPreoptSystemOther(t *testing.T) {
140 global, module := testGlobalConfig, testModuleConfig
141
142 global.HasSystemOther = true
143 global.PatternsOnSystemOther = []string{"app/%"}
144
145 module.Name = "test"
146 module.DexLocation = "/system/app/test/test.apk"
147 module.BuildPath = "out/test/test.apk"
Colin Cross43f08db2018-11-12 10:13:39 -0800148
149 rule, err := GenerateDexpreoptRule(global, module)
150 if err != nil {
151 t.Error(err)
152 }
153
Colin Crossdeabb942019-02-11 14:11:09 -0800154 wantInstalls := android.RuleBuilderInstalls{
Colin Cross43f08db2018-11-12 10:13:39 -0800155 {"out/test/oat/arm/package.odex", "/system_other/app/test/oat/arm/test.odex"},
156 {"out/test/oat/arm/package.vdex", "/system_other/app/test/oat/arm/test.vdex"},
157 }
158
159 if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
160 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
161 }
162}
163
164func TestDexPreoptProfile(t *testing.T) {
165 global, module := testGlobalConfig, testModuleConfig
166
167 module.Name = "test"
168 module.DexLocation = "/system/app/test/test.apk"
169 module.BuildPath = "out/test/test.apk"
170 module.ProfileClassListing = "profile"
Colin Cross43f08db2018-11-12 10:13:39 -0800171
172 rule, err := GenerateDexpreoptRule(global, module)
173 if err != nil {
174 t.Error(err)
175 }
176
Colin Crossdeabb942019-02-11 14:11:09 -0800177 wantInstalls := android.RuleBuilderInstalls{
Colin Cross43f08db2018-11-12 10:13:39 -0800178 {"out/test/profile.prof", "/system/app/test/test.apk.prof"},
179 {"out/test/oat/arm/package.art", "/system/app/test/oat/arm/test.art"},
180 {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
181 {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
182 }
183
184 if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
185 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
186 }
187}
188
189func TestStripDex(t *testing.T) {
Colin Cross8c6d2502019-01-09 21:09:14 -0800190 tests := []struct {
191 name string
192 setup func(global *GlobalConfig, module *ModuleConfig)
193 strip bool
194 }{
195 {
196 name: "default strip",
197 setup: func(global *GlobalConfig, module *ModuleConfig) {},
198 strip: true,
199 },
200 {
201 name: "global no stripping",
202 setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true },
203 strip: false,
204 },
205 {
206 name: "module no stripping",
207 setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true },
208 strip: false,
209 },
Colin Cross43f08db2018-11-12 10:13:39 -0800210 }
211
Colin Cross8c6d2502019-01-09 21:09:14 -0800212 for _, test := range tests {
213 t.Run(test.name, func(t *testing.T) {
Colin Cross43f08db2018-11-12 10:13:39 -0800214
Colin Cross8c6d2502019-01-09 21:09:14 -0800215 global, module := testGlobalConfig, testModuleConfig
Colin Cross43f08db2018-11-12 10:13:39 -0800216
Colin Cross8c6d2502019-01-09 21:09:14 -0800217 module.Name = "test"
218 module.DexLocation = "/system/app/test/test.apk"
219 module.BuildPath = "out/test/test.apk"
Colin Cross8c6d2502019-01-09 21:09:14 -0800220 module.StripInputPath = "$1"
221 module.StripOutputPath = "$2"
Colin Cross43f08db2018-11-12 10:13:39 -0800222
Colin Cross8c6d2502019-01-09 21:09:14 -0800223 test.setup(&global, &module)
Colin Cross43f08db2018-11-12 10:13:39 -0800224
Colin Cross8c6d2502019-01-09 21:09:14 -0800225 rule, err := GenerateStripRule(global, module)
226 if err != nil {
227 t.Error(err)
228 }
Colin Cross43f08db2018-11-12 10:13:39 -0800229
Colin Cross8c6d2502019-01-09 21:09:14 -0800230 if test.strip {
231 want := `zip2zip -i $1 -o $2 -x "classes*.dex"`
232 if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) {
233 t.Errorf("\nwant commands[0] to have:\n %v\ngot:\n %v", want, rule.Commands()[0])
234 }
235 } else {
236 wantCommands := []string{
237 "cp -f $1 $2",
238 }
239 if !reflect.DeepEqual(rule.Commands(), wantCommands) {
240 t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands())
241 }
242 }
243 })
Colin Cross43f08db2018-11-12 10:13:39 -0800244 }
245}