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 | |
| 24 | var testGlobalConfig = GlobalConfig{ |
| 25 | DefaultNoStripping: false, |
| 26 | DisablePreoptModules: nil, |
| 27 | OnlyPreoptBootImageAndSystemServer: false, |
| 28 | HasSystemOther: false, |
| 29 | PatternsOnSystemOther: nil, |
| 30 | DisableGenerateProfile: false, |
| 31 | BootJars: nil, |
| 32 | SystemServerJars: nil, |
| 33 | SystemServerApps: nil, |
| 34 | SpeedApps: nil, |
| 35 | PreoptFlags: nil, |
| 36 | DefaultCompilerFilter: "", |
| 37 | SystemServerCompilerFilter: "", |
| 38 | GenerateDMFiles: false, |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 39 | NeverAllowStripping: false, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 40 | NoDebugInfo: false, |
| 41 | AlwaysSystemServerDebugInfo: false, |
| 42 | NeverSystemServerDebugInfo: false, |
| 43 | AlwaysOtherDebugInfo: false, |
| 44 | NeverOtherDebugInfo: false, |
| 45 | MissingUsesLibraries: nil, |
| 46 | IsEng: false, |
| 47 | SanitizeLite: false, |
| 48 | DefaultAppImages: false, |
| 49 | Dex2oatXmx: "", |
| 50 | Dex2oatXms: "", |
| 51 | EmptyDirectory: "", |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 52 | DefaultDexPreoptImage: nil, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 53 | CpuVariant: nil, |
| 54 | InstructionSetFeatures: nil, |
| 55 | Tools: Tools{ |
| 56 | Profman: "profman", |
| 57 | Dex2oat: "dex2oat", |
| 58 | Aapt: "aapt", |
| 59 | SoongZip: "soong_zip", |
| 60 | Zip2zip: "zip2zip", |
| 61 | VerifyUsesLibraries: "verify_uses_libraries.sh", |
| 62 | ConstructContext: "construct_context.sh", |
| 63 | }, |
| 64 | } |
| 65 | |
| 66 | var testModuleConfig = ModuleConfig{ |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 67 | Name: "", |
| 68 | DexLocation: "", |
| 69 | BuildPath: "", |
| 70 | DexPath: "", |
| 71 | UncompressedDex: false, |
| 72 | HasApkLibraries: false, |
| 73 | PreoptFlags: nil, |
| 74 | ProfileClassListing: "", |
| 75 | ProfileIsTextListing: false, |
| 76 | EnforceUsesLibraries: false, |
| 77 | OptionalUsesLibraries: nil, |
| 78 | UsesLibraries: nil, |
| 79 | LibraryPaths: nil, |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 80 | Archs: []android.ArchType{android.Arm}, |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 81 | DexPreoptImages: []string{"system/framework/arm/boot.art"}, |
| 82 | PreoptExtractedApk: false, |
| 83 | NoCreateAppImage: false, |
| 84 | ForceCreateAppImage: false, |
| 85 | PresignedPrebuilt: false, |
| 86 | NoStripping: false, |
| 87 | StripInputPath: "", |
| 88 | StripOutputPath: "", |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | func TestDexPreopt(t *testing.T) { |
| 92 | global, module := testGlobalConfig, testModuleConfig |
| 93 | |
| 94 | module.Name = "test" |
| 95 | module.DexLocation = "/system/app/test/test.apk" |
| 96 | module.BuildPath = "out/test/test.apk" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 97 | |
| 98 | rule, err := GenerateDexpreoptRule(global, module) |
| 99 | if err != nil { |
| 100 | t.Error(err) |
| 101 | } |
| 102 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame^] | 103 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 104 | {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"}, |
| 105 | {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"}, |
| 106 | } |
| 107 | |
| 108 | if !reflect.DeepEqual(rule.Installs(), wantInstalls) { |
| 109 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 110 | } |
| 111 | } |
| 112 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 113 | func TestDexPreoptStrip(t *testing.T) { |
| 114 | // Test that we panic if we strip in a configuration where stripping is not allowed. |
| 115 | global, module := testGlobalConfig, testModuleConfig |
| 116 | |
| 117 | global.NeverAllowStripping = true |
| 118 | module.NoStripping = false |
| 119 | module.Name = "test" |
| 120 | module.DexLocation = "/system/app/test/test.apk" |
| 121 | module.BuildPath = "out/test/test.apk" |
| 122 | |
| 123 | _, err := GenerateStripRule(global, module) |
| 124 | if err == nil { |
| 125 | t.Errorf("Expected an error when calling GenerateStripRule on a stripped module") |
| 126 | } |
| 127 | } |
| 128 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 129 | func TestDexPreoptSystemOther(t *testing.T) { |
| 130 | global, module := testGlobalConfig, testModuleConfig |
| 131 | |
| 132 | global.HasSystemOther = true |
| 133 | global.PatternsOnSystemOther = []string{"app/%"} |
| 134 | |
| 135 | module.Name = "test" |
| 136 | module.DexLocation = "/system/app/test/test.apk" |
| 137 | module.BuildPath = "out/test/test.apk" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 138 | |
| 139 | rule, err := GenerateDexpreoptRule(global, module) |
| 140 | if err != nil { |
| 141 | t.Error(err) |
| 142 | } |
| 143 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame^] | 144 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 145 | {"out/test/oat/arm/package.odex", "/system_other/app/test/oat/arm/test.odex"}, |
| 146 | {"out/test/oat/arm/package.vdex", "/system_other/app/test/oat/arm/test.vdex"}, |
| 147 | } |
| 148 | |
| 149 | if !reflect.DeepEqual(rule.Installs(), wantInstalls) { |
| 150 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func TestDexPreoptProfile(t *testing.T) { |
| 155 | global, module := testGlobalConfig, testModuleConfig |
| 156 | |
| 157 | module.Name = "test" |
| 158 | module.DexLocation = "/system/app/test/test.apk" |
| 159 | module.BuildPath = "out/test/test.apk" |
| 160 | module.ProfileClassListing = "profile" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 161 | |
| 162 | rule, err := GenerateDexpreoptRule(global, module) |
| 163 | if err != nil { |
| 164 | t.Error(err) |
| 165 | } |
| 166 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame^] | 167 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 168 | {"out/test/profile.prof", "/system/app/test/test.apk.prof"}, |
| 169 | {"out/test/oat/arm/package.art", "/system/app/test/oat/arm/test.art"}, |
| 170 | {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"}, |
| 171 | {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"}, |
| 172 | } |
| 173 | |
| 174 | if !reflect.DeepEqual(rule.Installs(), wantInstalls) { |
| 175 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func TestStripDex(t *testing.T) { |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 180 | tests := []struct { |
| 181 | name string |
| 182 | setup func(global *GlobalConfig, module *ModuleConfig) |
| 183 | strip bool |
| 184 | }{ |
| 185 | { |
| 186 | name: "default strip", |
| 187 | setup: func(global *GlobalConfig, module *ModuleConfig) {}, |
| 188 | strip: true, |
| 189 | }, |
| 190 | { |
| 191 | name: "global no stripping", |
| 192 | setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true }, |
| 193 | strip: false, |
| 194 | }, |
| 195 | { |
| 196 | name: "module no stripping", |
| 197 | setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true }, |
| 198 | strip: false, |
| 199 | }, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 202 | for _, test := range tests { |
| 203 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 204 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 205 | global, module := testGlobalConfig, testModuleConfig |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 206 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 207 | module.Name = "test" |
| 208 | module.DexLocation = "/system/app/test/test.apk" |
| 209 | module.BuildPath = "out/test/test.apk" |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 210 | module.StripInputPath = "$1" |
| 211 | module.StripOutputPath = "$2" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 212 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 213 | test.setup(&global, &module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 214 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 215 | rule, err := GenerateStripRule(global, module) |
| 216 | if err != nil { |
| 217 | t.Error(err) |
| 218 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 219 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 220 | if test.strip { |
| 221 | want := `zip2zip -i $1 -o $2 -x "classes*.dex"` |
| 222 | if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) { |
| 223 | t.Errorf("\nwant commands[0] to have:\n %v\ngot:\n %v", want, rule.Commands()[0]) |
| 224 | } |
| 225 | } else { |
| 226 | wantCommands := []string{ |
| 227 | "cp -f $1 $2", |
| 228 | } |
| 229 | if !reflect.DeepEqual(rule.Commands(), wantCommands) { |
| 230 | t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands()) |
| 231 | } |
| 232 | } |
| 233 | }) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 234 | } |
| 235 | } |