blob: be861901b9875e5a9fc08aedd962b97cd8eb5f03 [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,
39 NoDebugInfo: false,
40 AlwaysSystemServerDebugInfo: false,
41 NeverSystemServerDebugInfo: false,
42 AlwaysOtherDebugInfo: false,
43 NeverOtherDebugInfo: false,
44 MissingUsesLibraries: nil,
45 IsEng: false,
46 SanitizeLite: false,
47 DefaultAppImages: false,
48 Dex2oatXmx: "",
49 Dex2oatXms: "",
50 EmptyDirectory: "",
Colin Crossc7e40aa2019-02-08 21:37:00 -080051 DefaultDexPreoptImage: nil,
Colin Cross43f08db2018-11-12 10:13:39 -080052 CpuVariant: nil,
53 InstructionSetFeatures: nil,
54 Tools: Tools{
55 Profman: "profman",
56 Dex2oat: "dex2oat",
57 Aapt: "aapt",
58 SoongZip: "soong_zip",
59 Zip2zip: "zip2zip",
60 VerifyUsesLibraries: "verify_uses_libraries.sh",
61 ConstructContext: "construct_context.sh",
62 },
63}
64
65var testModuleConfig = ModuleConfig{
Colin Crossc7e40aa2019-02-08 21:37:00 -080066 Name: "",
67 DexLocation: "",
68 BuildPath: "",
69 DexPath: "",
70 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,
Colin Cross74ba9622019-02-11 15:11:14 -080079 Archs: []android.ArchType{android.Arm},
Colin Crossc7e40aa2019-02-08 21:37:00 -080080 DexPreoptImages: []string{"system/framework/arm/boot.art"},
81 PreoptExtractedApk: false,
82 NoCreateAppImage: false,
83 ForceCreateAppImage: false,
84 PresignedPrebuilt: false,
85 NoStripping: false,
86 StripInputPath: "",
87 StripOutputPath: "",
Colin Cross43f08db2018-11-12 10:13:39 -080088}
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"
Colin Cross43f08db2018-11-12 10:13:39 -080096
97 rule, err := GenerateDexpreoptRule(global, module)
98 if err != nil {
99 t.Error(err)
100 }
101
Colin Crossfeec25b2019-01-30 17:32:39 -0800102 wantInstalls := []android.RuleBuilderInstall{
Colin Cross43f08db2018-11-12 10:13:39 -0800103 {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
104 {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
105 }
106
107 if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
108 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
109 }
110}
111
112func TestDexPreoptSystemOther(t *testing.T) {
113 global, module := testGlobalConfig, testModuleConfig
114
115 global.HasSystemOther = true
116 global.PatternsOnSystemOther = []string{"app/%"}
117
118 module.Name = "test"
119 module.DexLocation = "/system/app/test/test.apk"
120 module.BuildPath = "out/test/test.apk"
Colin Cross43f08db2018-11-12 10:13:39 -0800121
122 rule, err := GenerateDexpreoptRule(global, module)
123 if err != nil {
124 t.Error(err)
125 }
126
Colin Crossfeec25b2019-01-30 17:32:39 -0800127 wantInstalls := []android.RuleBuilderInstall{
Colin Cross43f08db2018-11-12 10:13:39 -0800128 {"out/test/oat/arm/package.odex", "/system_other/app/test/oat/arm/test.odex"},
129 {"out/test/oat/arm/package.vdex", "/system_other/app/test/oat/arm/test.vdex"},
130 }
131
132 if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
133 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
134 }
135}
136
137func TestDexPreoptProfile(t *testing.T) {
138 global, module := testGlobalConfig, testModuleConfig
139
140 module.Name = "test"
141 module.DexLocation = "/system/app/test/test.apk"
142 module.BuildPath = "out/test/test.apk"
143 module.ProfileClassListing = "profile"
Colin Cross43f08db2018-11-12 10:13:39 -0800144
145 rule, err := GenerateDexpreoptRule(global, module)
146 if err != nil {
147 t.Error(err)
148 }
149
Colin Crossfeec25b2019-01-30 17:32:39 -0800150 wantInstalls := []android.RuleBuilderInstall{
Colin Cross43f08db2018-11-12 10:13:39 -0800151 {"out/test/profile.prof", "/system/app/test/test.apk.prof"},
152 {"out/test/oat/arm/package.art", "/system/app/test/oat/arm/test.art"},
153 {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
154 {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
155 }
156
157 if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
158 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
159 }
160}
161
162func TestStripDex(t *testing.T) {
Colin Cross8c6d2502019-01-09 21:09:14 -0800163 tests := []struct {
164 name string
165 setup func(global *GlobalConfig, module *ModuleConfig)
166 strip bool
167 }{
168 {
169 name: "default strip",
170 setup: func(global *GlobalConfig, module *ModuleConfig) {},
171 strip: true,
172 },
173 {
174 name: "global no stripping",
175 setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true },
176 strip: false,
177 },
178 {
179 name: "module no stripping",
180 setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true },
181 strip: false,
182 },
Colin Cross43f08db2018-11-12 10:13:39 -0800183 }
184
Colin Cross8c6d2502019-01-09 21:09:14 -0800185 for _, test := range tests {
186 t.Run(test.name, func(t *testing.T) {
Colin Cross43f08db2018-11-12 10:13:39 -0800187
Colin Cross8c6d2502019-01-09 21:09:14 -0800188 global, module := testGlobalConfig, testModuleConfig
Colin Cross43f08db2018-11-12 10:13:39 -0800189
Colin Cross8c6d2502019-01-09 21:09:14 -0800190 module.Name = "test"
191 module.DexLocation = "/system/app/test/test.apk"
192 module.BuildPath = "out/test/test.apk"
Colin Cross8c6d2502019-01-09 21:09:14 -0800193 module.StripInputPath = "$1"
194 module.StripOutputPath = "$2"
Colin Cross43f08db2018-11-12 10:13:39 -0800195
Colin Cross8c6d2502019-01-09 21:09:14 -0800196 test.setup(&global, &module)
Colin Cross43f08db2018-11-12 10:13:39 -0800197
Colin Cross8c6d2502019-01-09 21:09:14 -0800198 rule, err := GenerateStripRule(global, module)
199 if err != nil {
200 t.Error(err)
201 }
Colin Cross43f08db2018-11-12 10:13:39 -0800202
Colin Cross8c6d2502019-01-09 21:09:14 -0800203 if test.strip {
204 want := `zip2zip -i $1 -o $2 -x "classes*.dex"`
205 if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) {
206 t.Errorf("\nwant commands[0] to have:\n %v\ngot:\n %v", want, rule.Commands()[0])
207 }
208 } else {
209 wantCommands := []string{
210 "cp -f $1 $2",
211 }
212 if !reflect.DeepEqual(rule.Commands(), wantCommands) {
213 t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands())
214 }
215 }
216 })
Colin Cross43f08db2018-11-12 10:13:39 -0800217 }
218}