blob: 0402f87549e289a65e193a20dbd77cf8cd130cc5 [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")},
41 PreoptBootClassPathDexFiles: nil,
42 PreoptBootClassPathDexLocations: nil,
43 PreoptExtractedApk: false,
44 NoCreateAppImage: false,
45 ForceCreateAppImage: false,
46 PresignedPrebuilt: false,
47 NoStripping: false,
48 StripInputPath: android.PathForOutput(ctx, "unstripped/test.apk"),
49 StripOutputPath: android.PathForOutput(ctx, "stripped/test.apk"),
50 }
Colin Cross43f08db2018-11-12 10:13:39 -080051}
52
53func TestDexPreopt(t *testing.T) {
Colin Cross69f59a32019-02-15 10:39:37 -080054 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
55 global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
Colin Cross43f08db2018-11-12 10:13:39 -080056
Colin Cross69f59a32019-02-15 10:39:37 -080057 rule, err := GenerateDexpreoptRule(ctx, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -080058 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080059 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -080060 }
61
Colin Crossdeabb942019-02-11 14:11:09 -080062 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -080063 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
64 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -080065 }
66
Colin Cross2cdd5df2019-02-25 10:25:24 -080067 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -080068 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
69 }
70}
71
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000072func TestDexPreoptStrip(t *testing.T) {
73 // Test that we panic if we strip in a configuration where stripping is not allowed.
Colin Cross69f59a32019-02-15 10:39:37 -080074 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
75 global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000076
77 global.NeverAllowStripping = true
78 module.NoStripping = false
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000079
80 _, err := GenerateStripRule(global, module)
81 if err == nil {
82 t.Errorf("Expected an error when calling GenerateStripRule on a stripped module")
83 }
84}
85
Colin Cross43f08db2018-11-12 10:13:39 -080086func TestDexPreoptSystemOther(t *testing.T) {
Colin Cross69f59a32019-02-15 10:39:37 -080087 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
88 global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
Colin Cross43f08db2018-11-12 10:13:39 -080089
90 global.HasSystemOther = true
91 global.PatternsOnSystemOther = []string{"app/%"}
92
Colin Cross69f59a32019-02-15 10:39:37 -080093 rule, err := GenerateDexpreoptRule(ctx, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -080094 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080095 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -080096 }
97
Colin Crossdeabb942019-02-11 14:11:09 -080098 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -080099 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system_other/app/test/oat/arm/test.odex"},
100 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system_other/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800101 }
102
Colin Cross2cdd5df2019-02-25 10:25:24 -0800103 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800104 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
105 }
106}
107
108func TestDexPreoptProfile(t *testing.T) {
Colin Cross69f59a32019-02-15 10:39:37 -0800109 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
110 global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
Colin Cross43f08db2018-11-12 10:13:39 -0800111
Colin Cross69f59a32019-02-15 10:39:37 -0800112 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800113
Colin Cross69f59a32019-02-15 10:39:37 -0800114 rule, err := GenerateDexpreoptRule(ctx, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -0800115 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800116 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800117 }
118
Colin Crossdeabb942019-02-11 14:11:09 -0800119 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800120 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
121 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
122 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
123 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800124 }
125
Colin Cross2cdd5df2019-02-25 10:25:24 -0800126 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800127 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
128 }
129}
130
131func TestStripDex(t *testing.T) {
Colin Cross8c6d2502019-01-09 21:09:14 -0800132 tests := []struct {
133 name string
134 setup func(global *GlobalConfig, module *ModuleConfig)
135 strip bool
136 }{
137 {
138 name: "default strip",
139 setup: func(global *GlobalConfig, module *ModuleConfig) {},
140 strip: true,
141 },
142 {
143 name: "global no stripping",
144 setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true },
145 strip: false,
146 },
147 {
148 name: "module no stripping",
149 setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true },
150 strip: false,
151 },
Colin Cross43f08db2018-11-12 10:13:39 -0800152 }
153
Colin Cross8c6d2502019-01-09 21:09:14 -0800154 for _, test := range tests {
155 t.Run(test.name, func(t *testing.T) {
Colin Cross43f08db2018-11-12 10:13:39 -0800156
Colin Cross69f59a32019-02-15 10:39:37 -0800157 ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
158 global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
Colin Cross43f08db2018-11-12 10:13:39 -0800159
Colin Cross8c6d2502019-01-09 21:09:14 -0800160 test.setup(&global, &module)
Colin Cross43f08db2018-11-12 10:13:39 -0800161
Colin Cross8c6d2502019-01-09 21:09:14 -0800162 rule, err := GenerateStripRule(global, module)
163 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800164 t.Fatal(err)
Colin Cross8c6d2502019-01-09 21:09:14 -0800165 }
Colin Cross43f08db2018-11-12 10:13:39 -0800166
Colin Cross8c6d2502019-01-09 21:09:14 -0800167 if test.strip {
Colin Cross69f59a32019-02-15 10:39:37 -0800168 want := `zip2zip -i out/unstripped/test.apk -o out/stripped/test.apk -x "classes*.dex"`
Colin Cross8c6d2502019-01-09 21:09:14 -0800169 if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) {
170 t.Errorf("\nwant commands[0] to have:\n %v\ngot:\n %v", want, rule.Commands()[0])
171 }
172 } else {
173 wantCommands := []string{
Colin Cross69f59a32019-02-15 10:39:37 -0800174 "cp -f out/unstripped/test.apk out/stripped/test.apk",
Colin Cross8c6d2502019-01-09 21:09:14 -0800175 }
176 if !reflect.DeepEqual(rule.Commands(), wantCommands) {
177 t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands())
178 }
179 }
180 })
Colin Cross43f08db2018-11-12 10:13:39 -0800181 }
182}