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" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 19 | "reflect" |
| 20 | "strings" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 24 | func 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 Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 36 | PresentOptionalUsesLibraries: nil, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 37 | UsesLibraries: nil, |
| 38 | LibraryPaths: nil, |
| 39 | Archs: []android.ArchType{android.Arm}, |
| 40 | DexPreoptImages: android.Paths{android.PathForTesting("system/framework/arm/boot.art")}, |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 41 | DexPreoptImagesDeps: []android.Paths{android.Paths{}}, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 42 | 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 Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | func TestDexPreopt(t *testing.T) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 55 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
| 56 | global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 57 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 58 | rule, err := GenerateDexpreoptRule(ctx, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 59 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 60 | t.Fatal(err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 63 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 64 | {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 Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 68 | if rule.Installs().String() != wantInstalls.String() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 69 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 70 | } |
| 71 | } |
| 72 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 73 | func TestDexPreoptStrip(t *testing.T) { |
| 74 | // 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] | 75 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
| 76 | global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx) |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 77 | |
| 78 | global.NeverAllowStripping = true |
| 79 | module.NoStripping = false |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 80 | |
| 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 Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 87 | func TestDexPreoptSystemOther(t *testing.T) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 88 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
| 89 | global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 90 | |
| 91 | global.HasSystemOther = true |
| 92 | global.PatternsOnSystemOther = []string{"app/%"} |
| 93 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 94 | rule, err := GenerateDexpreoptRule(ctx, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 95 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 96 | t.Fatal(err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 99 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 100 | {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 Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 104 | if rule.Installs().String() != wantInstalls.String() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 105 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestDexPreoptProfile(t *testing.T) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 110 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
| 111 | global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 112 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 113 | module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile")) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 114 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 115 | rule, err := GenerateDexpreoptRule(ctx, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 116 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 117 | t.Fatal(err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 120 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 121 | {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 Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 127 | if rule.Installs().String() != wantInstalls.String() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 128 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestStripDex(t *testing.T) { |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 133 | 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 Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 155 | for _, test := range tests { |
| 156 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 157 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 158 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
| 159 | global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 160 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 161 | test.setup(&global, &module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 162 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 163 | rule, err := GenerateStripRule(global, module) |
| 164 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 165 | t.Fatal(err) |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 166 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 167 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 168 | if test.strip { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 169 | 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] | 170 | 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 Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 175 | "cp -f out/unstripped/test.apk out/stripped/test.apk", |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 176 | } |
| 177 | if !reflect.DeepEqual(rule.Commands(), wantCommands) { |
| 178 | t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands()) |
| 179 | } |
| 180 | } |
| 181 | }) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 182 | } |
| 183 | } |