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 ( |
| 18 | "reflect" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | var testGlobalConfig = GlobalConfig{ |
| 24 | DefaultNoStripping: false, |
| 25 | DisablePreoptModules: nil, |
| 26 | OnlyPreoptBootImageAndSystemServer: false, |
| 27 | HasSystemOther: false, |
| 28 | PatternsOnSystemOther: nil, |
| 29 | DisableGenerateProfile: false, |
| 30 | BootJars: nil, |
| 31 | SystemServerJars: nil, |
| 32 | SystemServerApps: nil, |
| 33 | SpeedApps: nil, |
| 34 | PreoptFlags: nil, |
| 35 | DefaultCompilerFilter: "", |
| 36 | SystemServerCompilerFilter: "", |
| 37 | GenerateDMFiles: false, |
| 38 | NoDebugInfo: false, |
| 39 | AlwaysSystemServerDebugInfo: false, |
| 40 | NeverSystemServerDebugInfo: false, |
| 41 | AlwaysOtherDebugInfo: false, |
| 42 | NeverOtherDebugInfo: false, |
| 43 | MissingUsesLibraries: nil, |
| 44 | IsEng: false, |
| 45 | SanitizeLite: false, |
| 46 | DefaultAppImages: false, |
| 47 | Dex2oatXmx: "", |
| 48 | Dex2oatXms: "", |
| 49 | EmptyDirectory: "", |
| 50 | DefaultDexPreoptImageLocation: nil, |
| 51 | CpuVariant: nil, |
| 52 | InstructionSetFeatures: nil, |
| 53 | Tools: Tools{ |
| 54 | Profman: "profman", |
| 55 | Dex2oat: "dex2oat", |
| 56 | Aapt: "aapt", |
| 57 | SoongZip: "soong_zip", |
| 58 | Zip2zip: "zip2zip", |
| 59 | VerifyUsesLibraries: "verify_uses_libraries.sh", |
| 60 | ConstructContext: "construct_context.sh", |
| 61 | }, |
| 62 | } |
| 63 | |
| 64 | var testModuleConfig = ModuleConfig{ |
| 65 | Name: "", |
| 66 | DexLocation: "", |
| 67 | BuildPath: "", |
| 68 | DexPath: "", |
| 69 | PreferIntegrity: false, |
| 70 | UncompressedDex: false, |
| 71 | HasApkLibraries: false, |
| 72 | PreoptFlags: nil, |
| 73 | ProfileClassListing: "", |
| 74 | ProfileIsTextListing: false, |
| 75 | EnforceUsesLibraries: false, |
| 76 | OptionalUsesLibraries: nil, |
| 77 | UsesLibraries: nil, |
| 78 | LibraryPaths: nil, |
| 79 | Archs: nil, |
| 80 | DexPreoptImageLocation: "", |
| 81 | PreoptExtractedApk: false, |
| 82 | NoCreateAppImage: false, |
| 83 | ForceCreateAppImage: false, |
| 84 | PresignedPrebuilt: false, |
| 85 | StripInputPath: "", |
| 86 | StripOutputPath: "", |
| 87 | } |
| 88 | |
| 89 | func TestDexPreopt(t *testing.T) { |
| 90 | global, module := testGlobalConfig, testModuleConfig |
| 91 | |
| 92 | module.Name = "test" |
| 93 | module.DexLocation = "/system/app/test/test.apk" |
| 94 | module.BuildPath = "out/test/test.apk" |
| 95 | module.Archs = []string{"arm"} |
| 96 | |
| 97 | rule, err := GenerateDexpreoptRule(global, module) |
| 98 | if err != nil { |
| 99 | t.Error(err) |
| 100 | } |
| 101 | |
| 102 | wantInstalls := []Install{ |
| 103 | {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"}, |
| 104 | {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"}, |
| 105 | } |
| 106 | |
| 107 | if !reflect.DeepEqual(rule.Installs(), wantInstalls) { |
| 108 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestDexPreoptSystemOther(t *testing.T) { |
| 113 | global, module := testGlobalConfig, testModuleConfig |
| 114 | |
| 115 | global.HasSystemOther = true |
| 116 | global.PatternsOnSystemOther = []string{"app/%"} |
| 117 | |
| 118 | module.Name = "test" |
| 119 | module.DexLocation = "/system/app/test/test.apk" |
| 120 | module.BuildPath = "out/test/test.apk" |
| 121 | module.Archs = []string{"arm"} |
| 122 | |
| 123 | rule, err := GenerateDexpreoptRule(global, module) |
| 124 | if err != nil { |
| 125 | t.Error(err) |
| 126 | } |
| 127 | |
| 128 | wantInstalls := []Install{ |
| 129 | {"out/test/oat/arm/package.odex", "/system_other/app/test/oat/arm/test.odex"}, |
| 130 | {"out/test/oat/arm/package.vdex", "/system_other/app/test/oat/arm/test.vdex"}, |
| 131 | } |
| 132 | |
| 133 | if !reflect.DeepEqual(rule.Installs(), wantInstalls) { |
| 134 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func TestDexPreoptProfile(t *testing.T) { |
| 139 | global, module := testGlobalConfig, testModuleConfig |
| 140 | |
| 141 | module.Name = "test" |
| 142 | module.DexLocation = "/system/app/test/test.apk" |
| 143 | module.BuildPath = "out/test/test.apk" |
| 144 | module.ProfileClassListing = "profile" |
| 145 | module.Archs = []string{"arm"} |
| 146 | |
| 147 | rule, err := GenerateDexpreoptRule(global, module) |
| 148 | if err != nil { |
| 149 | t.Error(err) |
| 150 | } |
| 151 | |
| 152 | wantInstalls := []Install{ |
| 153 | {"out/test/profile.prof", "/system/app/test/test.apk.prof"}, |
| 154 | {"out/test/oat/arm/package.art", "/system/app/test/oat/arm/test.art"}, |
| 155 | {"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"}, |
| 156 | {"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"}, |
| 157 | } |
| 158 | |
| 159 | if !reflect.DeepEqual(rule.Installs(), wantInstalls) { |
| 160 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func TestStripDex(t *testing.T) { |
| 165 | global, module := testGlobalConfig, testModuleConfig |
| 166 | |
| 167 | module.Name = "test" |
| 168 | module.DexLocation = "/system/app/test/test.apk" |
| 169 | module.BuildPath = "out/test/test.apk" |
| 170 | module.Archs = []string{"arm"} |
| 171 | module.StripInputPath = "$1" |
| 172 | module.StripOutputPath = "$2" |
| 173 | |
| 174 | rule, err := GenerateStripRule(global, module) |
| 175 | if err != nil { |
| 176 | t.Error(err) |
| 177 | } |
| 178 | |
| 179 | want := `zip2zip -i $1 -o $2 -x "classes*.dex"` |
| 180 | if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) { |
| 181 | t.Errorf("\nwant commands[0] to have:\n %v\ngot:\n %v", want, rule.Commands()[0]) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func TestNoStripDex(t *testing.T) { |
| 186 | global, module := testGlobalConfig, testModuleConfig |
| 187 | |
| 188 | global.DefaultNoStripping = true |
| 189 | |
| 190 | module.Name = "test" |
| 191 | module.DexLocation = "/system/app/test/test.apk" |
| 192 | module.BuildPath = "out/test/test.apk" |
| 193 | module.Archs = []string{"arm"} |
| 194 | module.StripInputPath = "$1" |
| 195 | module.StripOutputPath = "$2" |
| 196 | |
| 197 | rule, err := GenerateStripRule(global, module) |
| 198 | if err != nil { |
| 199 | t.Error(err) |
| 200 | } |
| 201 | |
| 202 | wantCommands := []string{ |
| 203 | "cp -f $1 $2", |
| 204 | } |
| 205 | if !reflect.DeepEqual(rule.Commands(), wantCommands) { |
| 206 | t.Errorf("\nwant commands:\n %v\ngot:\n %v", wantCommands, rule.Commands()) |
| 207 | } |
| 208 | } |