aboutsummaryrefslogtreecommitdiffstats
path: root/dexpreopt/dexpreopt_test.go
blob: 949f91f3a926c802a243b57a6c85c24d444facaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2018 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dexpreopt

import (
	"android/soong/android"
	"reflect"
	"strings"
	"testing"
)

var testGlobalConfig = GlobalConfig{
	DefaultNoStripping:                 false,
	DisablePreoptModules:               nil,
	OnlyPreoptBootImageAndSystemServer: false,
	HasSystemOther:                     false,
	PatternsOnSystemOther:              nil,
	DisableGenerateProfile:             false,
	BootJars:                           nil,
	RuntimeApexJars:                    nil,
	ProductUpdatableBootModules:        nil,
	ProductUpdatableBootLocations:      nil,
	SystemServerJars:                   nil,
	SystemServerApps:                   nil,
	SpeedApps:                          nil,
	PreoptFlags:                        nil,
	DefaultCompilerFilter:              "",
	SystemServerCompilerFilter:         "",
	GenerateDMFiles:                    false,
	NeverAllowStripping:                false,
	NoDebugInfo:                        false,
	AlwaysSystemServerDebugInfo:        false,
	NeverSystemServerDebugInfo:         false,
	AlwaysOtherDebugInfo:               false,
	NeverOtherDebugInfo:                false,
	MissingUsesLibraries:               nil,
	IsEng:                              false,
	SanitizeLite:                       false,
	DefaultAppImages:                   false,
	Dex2oatXmx:                         "",
	Dex2oatXms:                         "",
	EmptyDirectory:                     "",
	CpuVariant:                         nil,
	InstructionSetFeatures:             nil,
	DirtyImageObjects:                  "",
	PreloadedClasses:                   "",
	BootImageProfiles:                  nil,
	BootFlags:                          "",
	Dex2oatImageXmx:                    "",
	Dex2oatImageXms:                    "",
	Tools: Tools{
		Profman:             "profman",
		Dex2oat:             "dex2oat",
		Aapt:                "aapt",
		SoongZip:            "soong_zip",
		Zip2zip:             "zip2zip",
		VerifyUsesLibraries: "verify_uses_libraries.sh",
		ConstructContext:    "construct_context.sh",
	},
}

var testModuleConfig = ModuleConfig{
	Name:                            "",
	DexLocation:                     "",
	BuildPath:                       "",
	DexPath:                         "",
	UncompressedDex:                 false,
	HasApkLibraries:                 false,
	PreoptFlags:                     nil,
	ProfileClassListing:             "",
	ProfileIsTextListing:            false,
	EnforceUsesLibraries:            false,
	OptionalUsesLibraries:           nil,
	UsesLibraries:                   nil,
	LibraryPaths:                    nil,
	Archs:                           []android.ArchType{android.Arm},
	DexPreoptImages:                 []string{"system/framework/arm/boot.art"},
	PreoptBootClassPathDexFiles:     nil,
	PreoptBootClassPathDexLocations: nil,
	PreoptExtractedApk:              false,
	NoCreateAppImage:                false,
	ForceCreateAppImage:             false,
	PresignedPrebuilt:               false,
	NoStripping:                     false,
	StripInputPath:                  "",
	StripOutputPath:                 "",
}

func TestDexPreopt(t *testing.T) {
	global, module := testGlobalConfig, testModuleConfig

	module.Name = "test"
	module.DexLocation = "/system/app/test/test.apk"
	module.BuildPath = "out/test/test.apk"

	rule, err := GenerateDexpreoptRule(global, module)
	if err != nil {
		t.Error(err)
	}

	wantInstalls := android.RuleBuilderInstalls{
		{"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
		{"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
	}

	if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
		t.Errorf("\nwant installs:\n   %v\ngot:\n   %v", wantInstalls, rule.Installs())
	}
}

func TestDexPreoptStrip(t *testing.T) {
	// Test that we panic if we strip in a configuration where stripping is not allowed.
	global, module := testGlobalConfig, testModuleConfig

	global.NeverAllowStripping = true
	module.NoStripping = false
	module.Name = "test"
	module.DexLocation = "/system/app/test/test.apk"
	module.BuildPath = "out/test/test.apk"

	_, err := GenerateStripRule(global, module)
	if err == nil {
		t.Errorf("Expected an error when calling GenerateStripRule on a stripped module")
	}
}

func TestDexPreoptSystemOther(t *testing.T) {
	global, module := testGlobalConfig, testModuleConfig

	global.HasSystemOther = true
	global.PatternsOnSystemOther = []string{"app/%"}

	module.Name = "test"
	module.DexLocation = "/system/app/test/test.apk"
	module.BuildPath = "out/test/test.apk"

	rule, err := GenerateDexpreoptRule(global, module)
	if err != nil {
		t.Error(err)
	}

	wantInstalls := android.RuleBuilderInstalls{
		{"out/test/oat/arm/package.odex", "/system_other/app/test/oat/arm/test.odex"},
		{"out/test/oat/arm/package.vdex", "/system_other/app/test/oat/arm/test.vdex"},
	}

	if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
		t.Errorf("\nwant installs:\n   %v\ngot:\n   %v", wantInstalls, rule.Installs())
	}
}

func TestDexPreoptProfile(t *testing.T) {
	global, module := testGlobalConfig, testModuleConfig

	module.Name = "test"
	module.DexLocation = "/system/app/test/test.apk"
	module.BuildPath = "out/test/test.apk"
	module.ProfileClassListing = "profile"

	rule, err := GenerateDexpreoptRule(global, module)
	if err != nil {
		t.Error(err)
	}

	wantInstalls := android.RuleBuilderInstalls{
		{"out/test/profile.prof", "/system/app/test/test.apk.prof"},
		{"out/test/oat/arm/package.art", "/system/app/test/oat/arm/test.art"},
		{"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
		{"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
	}

	if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
		t.Errorf("\nwant installs:\n   %v\ngot:\n   %v", wantInstalls, rule.Installs())
	}
}

func TestStripDex(t *testing.T) {
	tests := []struct {
		name  string
		setup func(global *GlobalConfig, module *ModuleConfig)
		strip bool
	}{
		{
			name:  "default strip",
			setup: func(global *GlobalConfig, module *ModuleConfig) {},
			strip: true,
		},
		{
			name:  "global no stripping",
			setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true },
			strip: false,
		},
		{
			name:  "module no stripping",
			setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true },
			strip: false,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {

			global, module := testGlobalConfig, testModuleConfig

			module.Name = "test"
			module.DexLocation = "/system/app/test/test.apk"
			module.BuildPath = "out/test/test.apk"
			module.StripInputPath = "$1"
			module.StripOutputPath = "$2"

			test.setup(&global, &module)

			rule, err := GenerateStripRule(global, module)
			if err != nil {
				t.Error(err)
			}

			if test.strip {
				want := `zip2zip -i $1 -o $2 -x "classes*.dex"`
				if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) {
					t.Errorf("\nwant commands[0] to have:\n   %v\ngot:\n   %v", want, rule.Commands()[0])
				}
			} else {
				wantCommands := []string{
					"cp -f $1 $2",
				}
				if !reflect.DeepEqual(rule.Commands(), wantCommands) {
					t.Errorf("\nwant commands:\n   %v\ngot:\n   %v", wantCommands, rule.Commands())
				}
			}
		})
	}
}