2018-11-13 02:13:39 +08:00
|
|
|
// 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 (
|
2019-01-31 09:32:39 +08:00
|
|
|
"android/soong/android"
|
2018-11-13 02:13:39 +08:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-02-16 02:39:37 +08:00
|
|
|
func testModuleConfig(ctx android.PathContext) ModuleConfig {
|
|
|
|
return ModuleConfig{
|
|
|
|
Name: "test",
|
|
|
|
DexLocation: "/system/app/test/test.apk",
|
|
|
|
BuildPath: android.PathForOutput(ctx, "test/test.apk"),
|
|
|
|
DexPath: android.PathForOutput(ctx, "test/dex/test.jar"),
|
|
|
|
UncompressedDex: false,
|
|
|
|
HasApkLibraries: false,
|
|
|
|
PreoptFlags: nil,
|
|
|
|
ProfileClassListing: android.OptionalPath{},
|
|
|
|
ProfileIsTextListing: false,
|
|
|
|
EnforceUsesLibraries: false,
|
2019-05-17 03:28:22 +08:00
|
|
|
PresentOptionalUsesLibraries: nil,
|
2019-02-16 02:39:37 +08:00
|
|
|
UsesLibraries: nil,
|
|
|
|
LibraryPaths: nil,
|
|
|
|
Archs: []android.ArchType{android.Arm},
|
|
|
|
DexPreoptImages: android.Paths{android.PathForTesting("system/framework/arm/boot.art")},
|
2019-06-14 05:44:53 +08:00
|
|
|
DexPreoptImagesDeps: []android.Paths{android.Paths{}},
|
2019-02-16 02:39:37 +08:00
|
|
|
PreoptBootClassPathDexFiles: nil,
|
|
|
|
PreoptBootClassPathDexLocations: nil,
|
|
|
|
PreoptExtractedApk: false,
|
|
|
|
NoCreateAppImage: false,
|
|
|
|
ForceCreateAppImage: false,
|
|
|
|
PresignedPrebuilt: false,
|
|
|
|
NoStripping: false,
|
|
|
|
StripInputPath: android.PathForOutput(ctx, "unstripped/test.apk"),
|
|
|
|
StripOutputPath: android.PathForOutput(ctx, "stripped/test.apk"),
|
|
|
|
}
|
2018-11-13 02:13:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDexPreopt(t *testing.T) {
|
2019-02-16 02:39:37 +08:00
|
|
|
ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
|
|
|
|
global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
|
2018-11-13 02:13:39 +08:00
|
|
|
|
2019-02-16 02:39:37 +08:00
|
|
|
rule, err := GenerateDexpreoptRule(ctx, global, module)
|
2018-11-13 02:13:39 +08:00
|
|
|
if err != nil {
|
2019-02-16 02:39:37 +08:00
|
|
|
t.Fatal(err)
|
2018-11-13 02:13:39 +08:00
|
|
|
}
|
|
|
|
|
2019-02-12 06:11:09 +08:00
|
|
|
wantInstalls := android.RuleBuilderInstalls{
|
2019-02-16 02:39:37 +08:00
|
|
|
{android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
|
|
|
|
{android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
|
2018-11-13 02:13:39 +08:00
|
|
|
}
|
|
|
|
|
2019-02-26 02:25:24 +08:00
|
|
|
if rule.Installs().String() != wantInstalls.String() {
|
2018-11-13 02:13:39 +08:00
|
|
|
t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 21:12:16 +08:00
|
|
|
func TestDexPreoptStrip(t *testing.T) {
|
|
|
|
// Test that we panic if we strip in a configuration where stripping is not allowed.
|
2019-02-16 02:39:37 +08:00
|
|
|
ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
|
|
|
|
global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
|
2019-02-12 21:12:16 +08:00
|
|
|
|
|
|
|
global.NeverAllowStripping = true
|
|
|
|
module.NoStripping = false
|
|
|
|
|
|
|
|
_, err := GenerateStripRule(global, module)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected an error when calling GenerateStripRule on a stripped module")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 02:13:39 +08:00
|
|
|
func TestDexPreoptSystemOther(t *testing.T) {
|
2019-02-16 02:39:37 +08:00
|
|
|
ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
|
|
|
|
global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
|
2018-11-13 02:13:39 +08:00
|
|
|
|
|
|
|
global.HasSystemOther = true
|
|
|
|
global.PatternsOnSystemOther = []string{"app/%"}
|
|
|
|
|
2019-02-16 02:39:37 +08:00
|
|
|
rule, err := GenerateDexpreoptRule(ctx, global, module)
|
2018-11-13 02:13:39 +08:00
|
|
|
if err != nil {
|
2019-02-16 02:39:37 +08:00
|
|
|
t.Fatal(err)
|
2018-11-13 02:13:39 +08:00
|
|
|
}
|
|
|
|
|
2019-02-12 06:11:09 +08:00
|
|
|
wantInstalls := android.RuleBuilderInstalls{
|
2019-02-16 02:39:37 +08:00
|
|
|
{android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system_other/app/test/oat/arm/test.odex"},
|
|
|
|
{android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system_other/app/test/oat/arm/test.vdex"},
|
2018-11-13 02:13:39 +08:00
|
|
|
}
|
|
|
|
|
2019-02-26 02:25:24 +08:00
|
|
|
if rule.Installs().String() != wantInstalls.String() {
|
2018-11-13 02:13:39 +08:00
|
|
|
t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDexPreoptProfile(t *testing.T) {
|
2019-02-16 02:39:37 +08:00
|
|
|
ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
|
|
|
|
global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
|
2018-11-13 02:13:39 +08:00
|
|
|
|
2019-02-16 02:39:37 +08:00
|
|
|
module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
|
2018-11-13 02:13:39 +08:00
|
|
|
|
2019-02-16 02:39:37 +08:00
|
|
|
rule, err := GenerateDexpreoptRule(ctx, global, module)
|
2018-11-13 02:13:39 +08:00
|
|
|
if err != nil {
|
2019-02-16 02:39:37 +08:00
|
|
|
t.Fatal(err)
|
2018-11-13 02:13:39 +08:00
|
|
|
}
|
|
|
|
|
2019-02-12 06:11:09 +08:00
|
|
|
wantInstalls := android.RuleBuilderInstalls{
|
2019-02-16 02:39:37 +08:00
|
|
|
{android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
|
|
|
|
{android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
|
|
|
|
{android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
|
|
|
|
{android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
|
2018-11-13 02:13:39 +08:00
|
|
|
}
|
|
|
|
|
2019-02-26 02:25:24 +08:00
|
|
|
if rule.Installs().String() != wantInstalls.String() {
|
2018-11-13 02:13:39 +08:00
|
|
|
t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStripDex(t *testing.T) {
|
2019-01-10 13:09:14 +08:00
|
|
|
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,
|
|
|
|
},
|
2018-11-13 02:13:39 +08:00
|
|
|
}
|
|
|
|
|
2019-01-10 13:09:14 +08:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
|
2019-02-16 02:39:37 +08:00
|
|
|
ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
|
|
|
|
global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
|
2019-01-10 13:09:14 +08:00
|
|
|
|
|
|
|
test.setup(&global, &module)
|
|
|
|
|
|
|
|
rule, err := GenerateStripRule(global, module)
|
|
|
|
if err != nil {
|
2019-02-16 02:39:37 +08:00
|
|
|
t.Fatal(err)
|
2019-01-10 13:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if test.strip {
|
2019-02-16 02:39:37 +08:00
|
|
|
want := `zip2zip -i out/unstripped/test.apk -o out/stripped/test.apk -x "classes*.dex"`
|
2019-01-10 13:09:14 +08:00
|
|
|
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{
|
2019-02-16 02:39:37 +08:00
|
|
|
"cp -f out/unstripped/test.apk out/stripped/test.apk",
|
2019-01-10 13:09:14 +08:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(rule.Commands(), wantCommands) {
|
|
|
|
t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2018-11-13 02:13:39 +08:00
|
|
|
}
|
|
|
|
}
|