Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package dexpreopt |
| 16 | |
| 17 | import ( |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 18 | "android/soong/android" |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame^] | 19 | "fmt" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 20 | "reflect" |
| 21 | "strings" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame^] | 25 | func testSystemModuleConfig(ctx android.PathContext, name string) ModuleConfig { |
| 26 | return testModuleConfig(ctx, name, "system") |
| 27 | } |
| 28 | |
| 29 | func testSystemProductModuleConfig(ctx android.PathContext, name string) ModuleConfig { |
| 30 | return testModuleConfig(ctx, name, "system/product") |
| 31 | } |
| 32 | |
| 33 | func testProductModuleConfig(ctx android.PathContext, name string) ModuleConfig { |
| 34 | return testModuleConfig(ctx, name, "product") |
| 35 | } |
| 36 | |
| 37 | func testModuleConfig(ctx android.PathContext, name, partition string) ModuleConfig { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 38 | return ModuleConfig{ |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame^] | 39 | Name: name, |
| 40 | DexLocation: fmt.Sprintf("/%s/app/test/%s.apk", partition, name), |
| 41 | BuildPath: android.PathForOutput(ctx, fmt.Sprintf("%s/%s.apk", name, name)), |
| 42 | DexPath: android.PathForOutput(ctx, fmt.Sprintf("%s/dex/%s.jar", name, name)), |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 43 | UncompressedDex: false, |
| 44 | HasApkLibraries: false, |
| 45 | PreoptFlags: nil, |
| 46 | ProfileClassListing: android.OptionalPath{}, |
| 47 | ProfileIsTextListing: false, |
| 48 | EnforceUsesLibraries: false, |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 49 | PresentOptionalUsesLibraries: nil, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 50 | UsesLibraries: nil, |
| 51 | LibraryPaths: nil, |
| 52 | Archs: []android.ArchType{android.Arm}, |
| 53 | DexPreoptImages: android.Paths{android.PathForTesting("system/framework/arm/boot.art")}, |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 54 | DexPreoptImagesDeps: []android.Paths{android.Paths{}}, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 55 | PreoptBootClassPathDexFiles: nil, |
| 56 | PreoptBootClassPathDexLocations: nil, |
| 57 | PreoptExtractedApk: false, |
| 58 | NoCreateAppImage: false, |
| 59 | ForceCreateAppImage: false, |
| 60 | PresignedPrebuilt: false, |
| 61 | NoStripping: false, |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame^] | 62 | StripInputPath: android.PathForOutput(ctx, fmt.Sprintf("unstripped/%s.apk", name)), |
| 63 | StripOutputPath: android.PathForOutput(ctx, fmt.Sprintf("stripped/%s.apk", name)), |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 64 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | func TestDexPreopt(t *testing.T) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 68 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame^] | 69 | global, module := GlobalConfigForTests(ctx), testSystemModuleConfig(ctx, "test") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 70 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 71 | rule, err := GenerateDexpreoptRule(ctx, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 72 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 73 | t.Fatal(err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 76 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 77 | {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"}, |
| 78 | {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"}, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 81 | if rule.Installs().String() != wantInstalls.String() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 82 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 83 | } |
| 84 | } |
| 85 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 86 | func TestDexPreoptStrip(t *testing.T) { |
| 87 | // Test that we panic if we strip in a configuration where stripping is not allowed. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 88 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame^] | 89 | global, module := GlobalConfigForTests(ctx), testSystemModuleConfig(ctx, "test") |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 90 | |
| 91 | global.NeverAllowStripping = true |
| 92 | module.NoStripping = false |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 93 | |
| 94 | _, err := GenerateStripRule(global, module) |
| 95 | if err == nil { |
| 96 | t.Errorf("Expected an error when calling GenerateStripRule on a stripped module") |
| 97 | } |
| 98 | } |
| 99 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 100 | func TestDexPreoptSystemOther(t *testing.T) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 101 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame^] | 102 | global := GlobalConfigForTests(ctx) |
| 103 | systemModule := testSystemModuleConfig(ctx, "Stest") |
| 104 | systemProductModule := testSystemProductModuleConfig(ctx, "SPtest") |
| 105 | productModule := testProductModuleConfig(ctx, "Ptest") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 106 | |
| 107 | global.HasSystemOther = true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 108 | |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame^] | 109 | type moduleTest struct { |
| 110 | module ModuleConfig |
| 111 | expectedPartition string |
| 112 | } |
| 113 | tests := []struct { |
| 114 | patterns []string |
| 115 | moduleTests []moduleTest |
| 116 | }{ |
| 117 | { |
| 118 | patterns: []string{"app/%"}, |
| 119 | moduleTests: []moduleTest{ |
| 120 | {module: systemModule, expectedPartition: "system_other"}, |
| 121 | {module: systemProductModule, expectedPartition: "system/product"}, |
| 122 | {module: productModule, expectedPartition: "product"}, |
| 123 | }, |
| 124 | }, |
| 125 | // product/app/% only applies to product apps inside the system partition |
| 126 | { |
| 127 | patterns: []string{"app/%", "product/app/%"}, |
| 128 | moduleTests: []moduleTest{ |
| 129 | {module: systemModule, expectedPartition: "system_other"}, |
| 130 | {module: systemProductModule, expectedPartition: "system_other/product"}, |
| 131 | {module: productModule, expectedPartition: "product"}, |
| 132 | }, |
| 133 | }, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame^] | 136 | for _, test := range tests { |
| 137 | global.PatternsOnSystemOther = test.patterns |
| 138 | for _, mt := range test.moduleTests { |
| 139 | rule, err := GenerateDexpreoptRule(ctx, global, mt.module) |
| 140 | if err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | |
| 144 | name := mt.module.Name |
| 145 | wantInstalls := android.RuleBuilderInstalls{ |
| 146 | {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)}, |
| 147 | {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)}, |
| 148 | } |
| 149 | |
| 150 | if rule.Installs().String() != wantInstalls.String() { |
| 151 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 152 | } |
| 153 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | func TestDexPreoptProfile(t *testing.T) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 159 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame^] | 160 | global, module := GlobalConfigForTests(ctx), testSystemModuleConfig(ctx, "test") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 161 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 162 | module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile")) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 163 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 164 | rule, err := GenerateDexpreoptRule(ctx, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 165 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 166 | t.Fatal(err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 169 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 170 | {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"}, |
| 171 | {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"}, |
| 172 | {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"}, |
| 173 | {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"}, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 176 | if rule.Installs().String() != wantInstalls.String() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 177 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func TestStripDex(t *testing.T) { |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 182 | tests := []struct { |
| 183 | name string |
| 184 | setup func(global *GlobalConfig, module *ModuleConfig) |
| 185 | strip bool |
| 186 | }{ |
| 187 | { |
| 188 | name: "default strip", |
| 189 | setup: func(global *GlobalConfig, module *ModuleConfig) {}, |
| 190 | strip: true, |
| 191 | }, |
| 192 | { |
| 193 | name: "global no stripping", |
| 194 | setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true }, |
| 195 | strip: false, |
| 196 | }, |
| 197 | { |
| 198 | name: "module no stripping", |
| 199 | setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true }, |
| 200 | strip: false, |
| 201 | }, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 204 | for _, test := range tests { |
| 205 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 206 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 207 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame^] | 208 | global, module := GlobalConfigForTests(ctx), testSystemModuleConfig(ctx, "test") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 209 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 210 | test.setup(&global, &module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 211 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 212 | rule, err := GenerateStripRule(global, module) |
| 213 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 214 | t.Fatal(err) |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 215 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 216 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 217 | if test.strip { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 218 | want := `zip2zip -i out/unstripped/test.apk -o out/stripped/test.apk -x "classes*.dex"` |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 219 | if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) { |
| 220 | t.Errorf("\nwant commands[0] to have:\n %v\ngot:\n %v", want, rule.Commands()[0]) |
| 221 | } |
| 222 | } else { |
| 223 | wantCommands := []string{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 224 | "cp -f out/unstripped/test.apk out/stripped/test.apk", |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 225 | } |
| 226 | if !reflect.DeepEqual(rule.Commands(), wantCommands) { |
| 227 | t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands()) |
| 228 | } |
| 229 | } |
| 230 | }) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 231 | } |
| 232 | } |