blob: 40c694f1884a0fd37b67e2b11f74a75620c320ad [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,
32 SystemServerJars: nil,
33 SystemServerApps: nil,
34 SpeedApps: nil,
35 PreoptFlags: nil,
36 DefaultCompilerFilter: "",
37 SystemServerCompilerFilter: "",
38 GenerateDMFiles: false,
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000039 NeverAllowStripping: false,
Colin Cross43f08db2018-11-12 10:13:39 -080040 NoDebugInfo: false,
41 AlwaysSystemServerDebugInfo: false,
42 NeverSystemServerDebugInfo: false,
43 AlwaysOtherDebugInfo: false,
44 NeverOtherDebugInfo: false,
45 MissingUsesLibraries: nil,
46 IsEng: false,
47 SanitizeLite: false,
48 DefaultAppImages: false,
49 Dex2oatXmx: "",
50 Dex2oatXms: "",
51 EmptyDirectory: "",
Colin Crossc7e40aa2019-02-08 21:37:00 -080052 DefaultDexPreoptImage: nil,
Colin Cross43f08db2018-11-12 10:13:39 -080053 CpuVariant: nil,
54 InstructionSetFeatures: nil,
55 Tools: Tools{
56 Profman: "profman",
57 Dex2oat: "dex2oat",
58 Aapt: "aapt",
59 SoongZip: "soong_zip",
60 Zip2zip: "zip2zip",
61 VerifyUsesLibraries: "verify_uses_libraries.sh",
62 ConstructContext: "construct_context.sh",
63 },
64}
65
66var testModuleConfig = ModuleConfig{
Colin Crossc7e40aa2019-02-08 21:37:00 -080067 Name: "",
68 DexLocation: "",
69 BuildPath: "",
70 DexPath: "",
71 UncompressedDex: false,
72 HasApkLibraries: false,
73 PreoptFlags: nil,
74 ProfileClassListing: "",
75 ProfileIsTextListing: false,
76 EnforceUsesLibraries: false,
77 OptionalUsesLibraries: nil,
78 UsesLibraries: nil,
79 LibraryPaths: nil,
Colin Cross74ba9622019-02-11 15:11:14 -080080 Archs: []android.ArchType{android.Arm},
Colin Crossc7e40aa2019-02-08 21:37:00 -080081 DexPreoptImages: []string{"system/framework/arm/boot.art"},
82 PreoptExtractedApk: false,
83 NoCreateAppImage: false,
84 ForceCreateAppImage: false,
85 PresignedPrebuilt: false,
86 NoStripping: false,
87 StripInputPath: "",
88 StripOutputPath: "",
Colin Cross43f08db2018-11-12 10:13:39 -080089}
90
91func TestDexPreopt(t *testing.T) {
92 global, module := testGlobalConfig, testModuleConfig
93
94 module.Name = "test"
95 module.DexLocation = "/system/app/test/test.apk"
96 module.BuildPath = "out/test/test.apk"
Colin Cross43f08db2018-11-12 10:13:39 -080097
98 rule, err := GenerateDexpreoptRule(global, module)
99 if err != nil {
100 t.Error(err)
101 }
102
Colin Crossdeabb942019-02-11 14:11:09 -0800103 wantInstalls := android.RuleBuilderInstalls{
Colin Cross43f08db2018-11-12 10:13:39 -0800104 {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
105 {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
106 }
107
108 if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
109 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
110 }
111}
112
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000113func TestDexPreoptStrip(t *testing.T) {
114 // Test that we panic if we strip in a configuration where stripping is not allowed.
115 global, module := testGlobalConfig, testModuleConfig
116
117 global.NeverAllowStripping = true
118 module.NoStripping = false
119 module.Name = "test"
120 module.DexLocation = "/system/app/test/test.apk"
121 module.BuildPath = "out/test/test.apk"
122
123 _, err := GenerateStripRule(global, module)
124 if err == nil {
125 t.Errorf("Expected an error when calling GenerateStripRule on a stripped module")
126 }
127}
128
Colin Cross43f08db2018-11-12 10:13:39 -0800129func TestDexPreoptSystemOther(t *testing.T) {
130 global, module := testGlobalConfig, testModuleConfig
131
132 global.HasSystemOther = true
133 global.PatternsOnSystemOther = []string{"app/%"}
134
135 module.Name = "test"
136 module.DexLocation = "/system/app/test/test.apk"
137 module.BuildPath = "out/test/test.apk"
Colin Cross43f08db2018-11-12 10:13:39 -0800138
139 rule, err := GenerateDexpreoptRule(global, module)
140 if err != nil {
141 t.Error(err)
142 }
143
Colin Crossdeabb942019-02-11 14:11:09 -0800144 wantInstalls := android.RuleBuilderInstalls{
Colin Cross43f08db2018-11-12 10:13:39 -0800145 {"out/test/oat/arm/package.odex", "/system_other/app/test/oat/arm/test.odex"},
146 {"out/test/oat/arm/package.vdex", "/system_other/app/test/oat/arm/test.vdex"},
147 }
148
149 if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
150 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
151 }
152}
153
154func TestDexPreoptProfile(t *testing.T) {
155 global, module := testGlobalConfig, testModuleConfig
156
157 module.Name = "test"
158 module.DexLocation = "/system/app/test/test.apk"
159 module.BuildPath = "out/test/test.apk"
160 module.ProfileClassListing = "profile"
Colin Cross43f08db2018-11-12 10:13:39 -0800161
162 rule, err := GenerateDexpreoptRule(global, module)
163 if err != nil {
164 t.Error(err)
165 }
166
Colin Crossdeabb942019-02-11 14:11:09 -0800167 wantInstalls := android.RuleBuilderInstalls{
Colin Cross43f08db2018-11-12 10:13:39 -0800168 {"out/test/profile.prof", "/system/app/test/test.apk.prof"},
169 {"out/test/oat/arm/package.art", "/system/app/test/oat/arm/test.art"},
170 {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
171 {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
172 }
173
174 if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
175 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
176 }
177}
178
179func TestStripDex(t *testing.T) {
Colin Cross8c6d2502019-01-09 21:09:14 -0800180 tests := []struct {
181 name string
182 setup func(global *GlobalConfig, module *ModuleConfig)
183 strip bool
184 }{
185 {
186 name: "default strip",
187 setup: func(global *GlobalConfig, module *ModuleConfig) {},
188 strip: true,
189 },
190 {
191 name: "global no stripping",
192 setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true },
193 strip: false,
194 },
195 {
196 name: "module no stripping",
197 setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true },
198 strip: false,
199 },
Colin Cross43f08db2018-11-12 10:13:39 -0800200 }
201
Colin Cross8c6d2502019-01-09 21:09:14 -0800202 for _, test := range tests {
203 t.Run(test.name, func(t *testing.T) {
Colin Cross43f08db2018-11-12 10:13:39 -0800204
Colin Cross8c6d2502019-01-09 21:09:14 -0800205 global, module := testGlobalConfig, testModuleConfig
Colin Cross43f08db2018-11-12 10:13:39 -0800206
Colin Cross8c6d2502019-01-09 21:09:14 -0800207 module.Name = "test"
208 module.DexLocation = "/system/app/test/test.apk"
209 module.BuildPath = "out/test/test.apk"
Colin Cross8c6d2502019-01-09 21:09:14 -0800210 module.StripInputPath = "$1"
211 module.StripOutputPath = "$2"
Colin Cross43f08db2018-11-12 10:13:39 -0800212
Colin Cross8c6d2502019-01-09 21:09:14 -0800213 test.setup(&global, &module)
Colin Cross43f08db2018-11-12 10:13:39 -0800214
Colin Cross8c6d2502019-01-09 21:09:14 -0800215 rule, err := GenerateStripRule(global, module)
216 if err != nil {
217 t.Error(err)
218 }
Colin Cross43f08db2018-11-12 10:13:39 -0800219
Colin Cross8c6d2502019-01-09 21:09:14 -0800220 if test.strip {
221 want := `zip2zip -i $1 -o $2 -x "classes*.dex"`
222 if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) {
223 t.Errorf("\nwant commands[0] to have:\n %v\ngot:\n %v", want, rule.Commands()[0])
224 }
225 } else {
226 wantCommands := []string{
227 "cp -f $1 $2",
228 }
229 if !reflect.DeepEqual(rule.Commands(), wantCommands) {
230 t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands())
231 }
232 }
233 })
Colin Cross43f08db2018-11-12 10:13:39 -0800234 }
235}