blob: f218d015d493ba9b89b3ad99702cd4ea25bdcc06 [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 (
18 "reflect"
19 "strings"
20 "testing"
21)
22
23var testGlobalConfig = GlobalConfig{
24 DefaultNoStripping: false,
25 DisablePreoptModules: nil,
26 OnlyPreoptBootImageAndSystemServer: false,
27 HasSystemOther: false,
28 PatternsOnSystemOther: nil,
29 DisableGenerateProfile: false,
30 BootJars: nil,
31 SystemServerJars: nil,
32 SystemServerApps: nil,
33 SpeedApps: nil,
34 PreoptFlags: nil,
35 DefaultCompilerFilter: "",
36 SystemServerCompilerFilter: "",
37 GenerateDMFiles: false,
38 NoDebugInfo: false,
39 AlwaysSystemServerDebugInfo: false,
40 NeverSystemServerDebugInfo: false,
41 AlwaysOtherDebugInfo: false,
42 NeverOtherDebugInfo: false,
43 MissingUsesLibraries: nil,
44 IsEng: false,
45 SanitizeLite: false,
46 DefaultAppImages: false,
47 Dex2oatXmx: "",
48 Dex2oatXms: "",
49 EmptyDirectory: "",
50 DefaultDexPreoptImageLocation: nil,
51 CpuVariant: nil,
52 InstructionSetFeatures: nil,
53 Tools: Tools{
54 Profman: "profman",
55 Dex2oat: "dex2oat",
56 Aapt: "aapt",
57 SoongZip: "soong_zip",
58 Zip2zip: "zip2zip",
59 VerifyUsesLibraries: "verify_uses_libraries.sh",
60 ConstructContext: "construct_context.sh",
61 },
62}
63
64var testModuleConfig = ModuleConfig{
65 Name: "",
66 DexLocation: "",
67 BuildPath: "",
68 DexPath: "",
Victor Hsieha2c16c12019-01-02 14:50:56 -080069 PreferCodeIntegrity: false,
Colin Cross43f08db2018-11-12 10:13:39 -080070 UncompressedDex: false,
71 HasApkLibraries: false,
72 PreoptFlags: nil,
73 ProfileClassListing: "",
74 ProfileIsTextListing: false,
75 EnforceUsesLibraries: false,
76 OptionalUsesLibraries: nil,
77 UsesLibraries: nil,
78 LibraryPaths: nil,
79 Archs: nil,
80 DexPreoptImageLocation: "",
81 PreoptExtractedApk: false,
82 NoCreateAppImage: false,
83 ForceCreateAppImage: false,
84 PresignedPrebuilt: false,
Colin Cross8c6d2502019-01-09 21:09:14 -080085 NoStripping: false,
Colin Cross43f08db2018-11-12 10:13:39 -080086 StripInputPath: "",
87 StripOutputPath: "",
88}
89
90func TestDexPreopt(t *testing.T) {
91 global, module := testGlobalConfig, testModuleConfig
92
93 module.Name = "test"
94 module.DexLocation = "/system/app/test/test.apk"
95 module.BuildPath = "out/test/test.apk"
96 module.Archs = []string{"arm"}
97
98 rule, err := GenerateDexpreoptRule(global, module)
99 if err != nil {
100 t.Error(err)
101 }
102
103 wantInstalls := []Install{
104 {"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
113func TestDexPreoptSystemOther(t *testing.T) {
114 global, module := testGlobalConfig, testModuleConfig
115
116 global.HasSystemOther = true
117 global.PatternsOnSystemOther = []string{"app/%"}
118
119 module.Name = "test"
120 module.DexLocation = "/system/app/test/test.apk"
121 module.BuildPath = "out/test/test.apk"
122 module.Archs = []string{"arm"}
123
124 rule, err := GenerateDexpreoptRule(global, module)
125 if err != nil {
126 t.Error(err)
127 }
128
129 wantInstalls := []Install{
130 {"out/test/oat/arm/package.odex", "/system_other/app/test/oat/arm/test.odex"},
131 {"out/test/oat/arm/package.vdex", "/system_other/app/test/oat/arm/test.vdex"},
132 }
133
134 if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
135 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
136 }
137}
138
139func TestDexPreoptProfile(t *testing.T) {
140 global, module := testGlobalConfig, testModuleConfig
141
142 module.Name = "test"
143 module.DexLocation = "/system/app/test/test.apk"
144 module.BuildPath = "out/test/test.apk"
145 module.ProfileClassListing = "profile"
146 module.Archs = []string{"arm"}
147
148 rule, err := GenerateDexpreoptRule(global, module)
149 if err != nil {
150 t.Error(err)
151 }
152
153 wantInstalls := []Install{
154 {"out/test/profile.prof", "/system/app/test/test.apk.prof"},
155 {"out/test/oat/arm/package.art", "/system/app/test/oat/arm/test.art"},
156 {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
157 {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
158 }
159
160 if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
161 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
162 }
163}
164
165func TestStripDex(t *testing.T) {
Colin Cross8c6d2502019-01-09 21:09:14 -0800166 tests := []struct {
167 name string
168 setup func(global *GlobalConfig, module *ModuleConfig)
169 strip bool
170 }{
171 {
172 name: "default strip",
173 setup: func(global *GlobalConfig, module *ModuleConfig) {},
174 strip: true,
175 },
176 {
177 name: "global no stripping",
178 setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true },
179 strip: false,
180 },
181 {
182 name: "module no stripping",
183 setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true },
184 strip: false,
185 },
Colin Cross43f08db2018-11-12 10:13:39 -0800186 }
187
Colin Cross8c6d2502019-01-09 21:09:14 -0800188 for _, test := range tests {
189 t.Run(test.name, func(t *testing.T) {
Colin Cross43f08db2018-11-12 10:13:39 -0800190
Colin Cross8c6d2502019-01-09 21:09:14 -0800191 global, module := testGlobalConfig, testModuleConfig
Colin Cross43f08db2018-11-12 10:13:39 -0800192
Colin Cross8c6d2502019-01-09 21:09:14 -0800193 module.Name = "test"
194 module.DexLocation = "/system/app/test/test.apk"
195 module.BuildPath = "out/test/test.apk"
196 module.Archs = []string{"arm"}
197 module.StripInputPath = "$1"
198 module.StripOutputPath = "$2"
Colin Cross43f08db2018-11-12 10:13:39 -0800199
Colin Cross8c6d2502019-01-09 21:09:14 -0800200 test.setup(&global, &module)
Colin Cross43f08db2018-11-12 10:13:39 -0800201
Colin Cross8c6d2502019-01-09 21:09:14 -0800202 rule, err := GenerateStripRule(global, module)
203 if err != nil {
204 t.Error(err)
205 }
Colin Cross43f08db2018-11-12 10:13:39 -0800206
Colin Cross8c6d2502019-01-09 21:09:14 -0800207 if test.strip {
208 want := `zip2zip -i $1 -o $2 -x "classes*.dex"`
209 if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) {
210 t.Errorf("\nwant commands[0] to have:\n %v\ngot:\n %v", want, rule.Commands()[0])
211 }
212 } else {
213 wantCommands := []string{
214 "cp -f $1 $2",
215 }
216 if !reflect.DeepEqual(rule.Commands(), wantCommands) {
217 t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands())
218 }
219 }
220 })
Colin Cross43f08db2018-11-12 10:13:39 -0800221 }
222}