blob: 7f1fe4276d078b8aca8c19b0770a0ded095758cd [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
Colin Cross69f59a32019-02-15 10:39:37 -080024func testModuleConfig(ctx android.PathContext) ModuleConfig {
25 return ModuleConfig{
26 Name: "test",
27 DexLocation: "/system/app/test/test.apk",
28 BuildPath: android.PathForOutput(ctx, "test/test.apk"),
29 DexPath: android.PathForOutput(ctx, "test/dex/test.jar"),
30 UncompressedDex: false,
31 HasApkLibraries: false,
32 PreoptFlags: nil,
33 ProfileClassListing: android.OptionalPath{},
34 ProfileIsTextListing: false,
35 EnforceUsesLibraries: false,
Colin Cross50ddcc42019-05-16 12:28:22 -070036 PresentOptionalUsesLibraries: nil,
Colin Cross69f59a32019-02-15 10:39:37 -080037 UsesLibraries: nil,
38 LibraryPaths: nil,
39 Archs: []android.ArchType{android.Arm},
40 DexPreoptImages: android.Paths{android.PathForTesting("system/framework/arm/boot.art")},
Dan Willemsen0f416782019-06-13 21:44:53 +000041 DexPreoptImagesDeps: []android.Paths{android.Paths{}},
Colin Cross69f59a32019-02-15 10:39:37 -080042 PreoptBootClassPathDexFiles: nil,
43 PreoptBootClassPathDexLocations: nil,
44 PreoptExtractedApk: false,
45 NoCreateAppImage: false,
46 ForceCreateAppImage: false,
47 PresignedPrebuilt: false,
48 NoStripping: false,
49 StripInputPath: android.PathForOutput(ctx, "unstripped/test.apk"),
50 StripOutputPath: android.PathForOutput(ctx, "stripped/test.apk"),
51 }
Colin Cross43f08db2018-11-12 10:13:39 -080052}
53
54func TestDexPreopt(t *testing.T) {
Colin Cross69f59a32019-02-15 10:39:37 -080055 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
56 global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
Colin Cross43f08db2018-11-12 10:13:39 -080057
Colin Cross69f59a32019-02-15 10:39:37 -080058 rule, err := GenerateDexpreoptRule(ctx, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -080059 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080060 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -080061 }
62
Colin Crossdeabb942019-02-11 14:11:09 -080063 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -080064 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
65 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -080066 }
67
Colin Cross2cdd5df2019-02-25 10:25:24 -080068 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -080069 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
70 }
71}
72
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000073func TestDexPreoptStrip(t *testing.T) {
74 // Test that we panic if we strip in a configuration where stripping is not allowed.
Colin Cross69f59a32019-02-15 10:39:37 -080075 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
76 global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000077
78 global.NeverAllowStripping = true
79 module.NoStripping = false
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000080
81 _, err := GenerateStripRule(global, module)
82 if err == nil {
83 t.Errorf("Expected an error when calling GenerateStripRule on a stripped module")
84 }
85}
86
Colin Cross43f08db2018-11-12 10:13:39 -080087func TestDexPreoptSystemOther(t *testing.T) {
Colin Cross69f59a32019-02-15 10:39:37 -080088 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
89 global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
Colin Cross43f08db2018-11-12 10:13:39 -080090
91 global.HasSystemOther = true
92 global.PatternsOnSystemOther = []string{"app/%"}
93
Colin Cross69f59a32019-02-15 10:39:37 -080094 rule, err := GenerateDexpreoptRule(ctx, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -080095 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080096 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -080097 }
98
Colin Crossdeabb942019-02-11 14:11:09 -080099 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800100 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system_other/app/test/oat/arm/test.odex"},
101 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system_other/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800102 }
103
Colin Cross2cdd5df2019-02-25 10:25:24 -0800104 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800105 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
106 }
107}
108
109func TestDexPreoptProfile(t *testing.T) {
Colin Cross69f59a32019-02-15 10:39:37 -0800110 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
111 global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
Colin Cross43f08db2018-11-12 10:13:39 -0800112
Colin Cross69f59a32019-02-15 10:39:37 -0800113 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800114
Colin Cross69f59a32019-02-15 10:39:37 -0800115 rule, err := GenerateDexpreoptRule(ctx, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -0800116 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800117 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800118 }
119
Colin Crossdeabb942019-02-11 14:11:09 -0800120 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800121 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
122 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
123 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
124 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800125 }
126
Colin Cross2cdd5df2019-02-25 10:25:24 -0800127 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800128 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
129 }
130}
131
132func TestStripDex(t *testing.T) {
Colin Cross8c6d2502019-01-09 21:09:14 -0800133 tests := []struct {
134 name string
135 setup func(global *GlobalConfig, module *ModuleConfig)
136 strip bool
137 }{
138 {
139 name: "default strip",
140 setup: func(global *GlobalConfig, module *ModuleConfig) {},
141 strip: true,
142 },
143 {
144 name: "global no stripping",
145 setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true },
146 strip: false,
147 },
148 {
149 name: "module no stripping",
150 setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true },
151 strip: false,
152 },
Colin Cross43f08db2018-11-12 10:13:39 -0800153 }
154
Colin Cross8c6d2502019-01-09 21:09:14 -0800155 for _, test := range tests {
156 t.Run(test.name, func(t *testing.T) {
Colin Cross43f08db2018-11-12 10:13:39 -0800157
Colin Cross69f59a32019-02-15 10:39:37 -0800158 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
159 global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
Colin Cross43f08db2018-11-12 10:13:39 -0800160
Colin Cross8c6d2502019-01-09 21:09:14 -0800161 test.setup(&global, &module)
Colin Cross43f08db2018-11-12 10:13:39 -0800162
Colin Cross8c6d2502019-01-09 21:09:14 -0800163 rule, err := GenerateStripRule(global, module)
164 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800165 t.Fatal(err)
Colin Cross8c6d2502019-01-09 21:09:14 -0800166 }
Colin Cross43f08db2018-11-12 10:13:39 -0800167
Colin Cross8c6d2502019-01-09 21:09:14 -0800168 if test.strip {
Colin Cross69f59a32019-02-15 10:39:37 -0800169 want := `zip2zip -i out/unstripped/test.apk -o out/stripped/test.apk -x "classes*.dex"`
Colin Cross8c6d2502019-01-09 21:09:14 -0800170 if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) {
171 t.Errorf("\nwant commands[0] to have:\n %v\ngot:\n %v", want, rule.Commands()[0])
172 }
173 } else {
174 wantCommands := []string{
Colin Cross69f59a32019-02-15 10:39:37 -0800175 "cp -f out/unstripped/test.apk out/stripped/test.apk",
Colin Cross8c6d2502019-01-09 21:09:14 -0800176 }
177 if !reflect.DeepEqual(rule.Commands(), wantCommands) {
178 t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands())
179 }
180 }
181 })
Colin Cross43f08db2018-11-12 10:13:39 -0800182 }
183}