| 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, |
| 39 | NoDebugInfo: false, |
| 40 | AlwaysSystemServerDebugInfo: false, |
| 41 | NeverSystemServerDebugInfo: false, |
| 42 | AlwaysOtherDebugInfo: false, |
| 43 | NeverOtherDebugInfo: false, |
| 44 | MissingUsesLibraries: nil, |
| 45 | IsEng: false, |
| 46 | SanitizeLite: false, |
| 47 | DefaultAppImages: false, |
| 48 | Dex2oatXmx: "", |
| 49 | Dex2oatXms: "", |
| 50 | EmptyDirectory: "", |
| 51 | DefaultDexPreoptImageLocation: nil, |
| 52 | CpuVariant: nil, |
| 53 | InstructionSetFeatures: nil, |
| 54 | Tools: Tools{ |
| 55 | Profman: "profman", |
| 56 | Dex2oat: "dex2oat", |
| 57 | Aapt: "aapt", |
| 58 | SoongZip: "soong_zip", |
| 59 | Zip2zip: "zip2zip", |
| 60 | VerifyUsesLibraries: "verify_uses_libraries.sh", |
| 61 | ConstructContext: "construct_context.sh", |
| 62 | }, |
| 63 | } |
| 64 | |
| 65 | var testModuleConfig = ModuleConfig{ |
| 66 | Name: "", |
| 67 | DexLocation: "", |
| 68 | BuildPath: "", |
| 69 | DexPath: "", |
| Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 70 | UseEmbeddedDex: false, |
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 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, |
| 80 | Archs: nil, |
| 81 | DexPreoptImageLocation: "", |
| 82 | PreoptExtractedApk: false, |
| 83 | NoCreateAppImage: false, |
| 84 | ForceCreateAppImage: false, |
| 85 | PresignedPrebuilt: false, |
| Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 86 | NoStripping: false, |
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 87 | StripInputPath: "", |
| 88 | StripOutputPath: "", |
| 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" |
| 97 | module.Archs = []string{"arm"} |
| 98 | |
| 99 | rule, err := GenerateDexpreoptRule(global, module) |
| 100 | if err != nil { |
| 101 | t.Error(err) |
| 102 | } |
| 103 | |
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 104 | wantInstalls := []android.RuleBuilderInstall{ |
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 105 | {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"}, |
| 106 | {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"}, |
| 107 | } |
| 108 | |
| 109 | if !reflect.DeepEqual(rule.Installs(), wantInstalls) { |
| 110 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func TestDexPreoptSystemOther(t *testing.T) { |
| 115 | global, module := testGlobalConfig, testModuleConfig |
| 116 | |
| 117 | global.HasSystemOther = true |
| 118 | global.PatternsOnSystemOther = []string{"app/%"} |
| 119 | |
| 120 | module.Name = "test" |
| 121 | module.DexLocation = "/system/app/test/test.apk" |
| 122 | module.BuildPath = "out/test/test.apk" |
| 123 | module.Archs = []string{"arm"} |
| 124 | |
| 125 | rule, err := GenerateDexpreoptRule(global, module) |
| 126 | if err != nil { |
| 127 | t.Error(err) |
| 128 | } |
| 129 | |
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 130 | wantInstalls := []android.RuleBuilderInstall{ |
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 131 | {"out/test/oat/arm/package.odex", "/system_other/app/test/oat/arm/test.odex"}, |
| 132 | {"out/test/oat/arm/package.vdex", "/system_other/app/test/oat/arm/test.vdex"}, |
| 133 | } |
| 134 | |
| 135 | if !reflect.DeepEqual(rule.Installs(), wantInstalls) { |
| 136 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func TestDexPreoptProfile(t *testing.T) { |
| 141 | global, module := testGlobalConfig, testModuleConfig |
| 142 | |
| 143 | module.Name = "test" |
| 144 | module.DexLocation = "/system/app/test/test.apk" |
| 145 | module.BuildPath = "out/test/test.apk" |
| 146 | module.ProfileClassListing = "profile" |
| 147 | module.Archs = []string{"arm"} |
| 148 | |
| 149 | rule, err := GenerateDexpreoptRule(global, module) |
| 150 | if err != nil { |
| 151 | t.Error(err) |
| 152 | } |
| 153 | |
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 154 | wantInstalls := []android.RuleBuilderInstall{ |
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 155 | {"out/test/profile.prof", "/system/app/test/test.apk.prof"}, |
| 156 | {"out/test/oat/arm/package.art", "/system/app/test/oat/arm/test.art"}, |
| 157 | {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"}, |
| 158 | {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"}, |
| 159 | } |
| 160 | |
| 161 | if !reflect.DeepEqual(rule.Installs(), wantInstalls) { |
| 162 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func TestStripDex(t *testing.T) { |
| Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 167 | tests := []struct { |
| 168 | name string |
| 169 | setup func(global *GlobalConfig, module *ModuleConfig) |
| 170 | strip bool |
| 171 | }{ |
| 172 | { |
| 173 | name: "default strip", |
| 174 | setup: func(global *GlobalConfig, module *ModuleConfig) {}, |
| 175 | strip: true, |
| 176 | }, |
| 177 | { |
| 178 | name: "global no stripping", |
| 179 | setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true }, |
| 180 | strip: false, |
| 181 | }, |
| 182 | { |
| 183 | name: "module no stripping", |
| 184 | setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true }, |
| 185 | strip: false, |
| 186 | }, |
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 189 | for _, test := range tests { |
| 190 | t.Run(test.name, func(t *testing.T) { |
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 191 | |
| Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 192 | global, module := testGlobalConfig, testModuleConfig |
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 193 | |
| Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 194 | module.Name = "test" |
| 195 | module.DexLocation = "/system/app/test/test.apk" |
| 196 | module.BuildPath = "out/test/test.apk" |
| 197 | module.Archs = []string{"arm"} |
| 198 | module.StripInputPath = "$1" |
| 199 | module.StripOutputPath = "$2" |
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 200 | |
| Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 201 | test.setup(&global, &module) |
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 202 | |
| Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 203 | rule, err := GenerateStripRule(global, module) |
| 204 | if err != nil { |
| 205 | t.Error(err) |
| 206 | } |
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 207 | |
| Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 208 | if test.strip { |
| 209 | want := `zip2zip -i $1 -o $2 -x "classes*.dex"` |
| 210 | if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) { |
| 211 | t.Errorf("\nwant commands[0] to have:\n %v\ngot:\n %v", want, rule.Commands()[0]) |
| 212 | } |
| 213 | } else { |
| 214 | wantCommands := []string{ |
| 215 | "cp -f $1 $2", |
| 216 | } |
| 217 | if !reflect.DeepEqual(rule.Commands(), wantCommands) { |
| 218 | t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands()) |
| 219 | } |
| 220 | } |
| 221 | }) |
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 222 | } |
| 223 | } |