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 | "testing" |
| 21 | ) |
| 22 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 23 | func testSystemModuleConfig(ctx android.PathContext, name string) *ModuleConfig { |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 24 | return testModuleConfig(ctx, name, "system") |
| 25 | } |
| 26 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 27 | func testSystemProductModuleConfig(ctx android.PathContext, name string) *ModuleConfig { |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 28 | return testModuleConfig(ctx, name, "system/product") |
| 29 | } |
| 30 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 31 | func testProductModuleConfig(ctx android.PathContext, name string) *ModuleConfig { |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 32 | return testModuleConfig(ctx, name, "product") |
| 33 | } |
| 34 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 35 | func testModuleConfig(ctx android.PathContext, name, partition string) *ModuleConfig { |
| 36 | return &ModuleConfig{ |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 37 | Name: name, |
| 38 | DexLocation: fmt.Sprintf("/%s/app/test/%s.apk", partition, name), |
| 39 | BuildPath: android.PathForOutput(ctx, fmt.Sprintf("%s/%s.apk", name, name)), |
| 40 | DexPath: android.PathForOutput(ctx, fmt.Sprintf("%s/dex/%s.jar", name, name)), |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 41 | UncompressedDex: false, |
| 42 | HasApkLibraries: false, |
| 43 | PreoptFlags: nil, |
| 44 | ProfileClassListing: android.OptionalPath{}, |
| 45 | ProfileIsTextListing: false, |
| 46 | EnforceUsesLibraries: false, |
Ulya Trafimovich | 6e82748 | 2020-06-12 14:32:24 +0100 | [diff] [blame] | 47 | OptionalUsesLibraries: nil, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 48 | UsesLibraries: nil, |
| 49 | LibraryPaths: nil, |
| 50 | Archs: []android.ArchType{android.Arm}, |
| 51 | DexPreoptImages: android.Paths{android.PathForTesting("system/framework/arm/boot.art")}, |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 52 | DexPreoptImagesDeps: []android.OutputPaths{android.OutputPaths{}}, |
| 53 | DexPreoptImageLocations: []string{}, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 54 | PreoptBootClassPathDexFiles: nil, |
| 55 | PreoptBootClassPathDexLocations: nil, |
| 56 | PreoptExtractedApk: false, |
| 57 | NoCreateAppImage: false, |
| 58 | ForceCreateAppImage: false, |
| 59 | PresignedPrebuilt: false, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 60 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | func TestDexPreopt(t *testing.T) { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 64 | config := android.TestConfig("out", nil, "", nil) |
| 65 | ctx := android.PathContextForTesting(config) |
| 66 | globalSoong := GlobalSoongConfigForTests(config) |
| 67 | global := GlobalConfigForTests(ctx) |
| 68 | module := testSystemModuleConfig(ctx, "test") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 69 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 70 | rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 71 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 72 | t.Fatal(err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 75 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 76 | {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"}, |
| 77 | {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] | 78 | } |
| 79 | |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 80 | if rule.Installs().String() != wantInstalls.String() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 81 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestDexPreoptSystemOther(t *testing.T) { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 86 | config := android.TestConfig("out", nil, "", nil) |
| 87 | ctx := android.PathContextForTesting(config) |
| 88 | globalSoong := GlobalSoongConfigForTests(config) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 89 | global := GlobalConfigForTests(ctx) |
| 90 | systemModule := testSystemModuleConfig(ctx, "Stest") |
| 91 | systemProductModule := testSystemProductModuleConfig(ctx, "SPtest") |
| 92 | productModule := testProductModuleConfig(ctx, "Ptest") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 93 | |
| 94 | global.HasSystemOther = true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 95 | |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 96 | type moduleTest struct { |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 97 | module *ModuleConfig |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 98 | expectedPartition string |
| 99 | } |
| 100 | tests := []struct { |
| 101 | patterns []string |
| 102 | moduleTests []moduleTest |
| 103 | }{ |
| 104 | { |
| 105 | patterns: []string{"app/%"}, |
| 106 | moduleTests: []moduleTest{ |
Anton Hansson | 43ab0bc | 2019-10-03 14:18:45 +0100 | [diff] [blame] | 107 | {module: systemModule, expectedPartition: "system_other/system"}, |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 108 | {module: systemProductModule, expectedPartition: "system/product"}, |
| 109 | {module: productModule, expectedPartition: "product"}, |
| 110 | }, |
| 111 | }, |
Anton Hansson | da4d9d9 | 2020-09-15 09:28:55 +0000 | [diff] [blame^] | 112 | // product/app/% only applies to product apps inside the system partition |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 113 | { |
| 114 | patterns: []string{"app/%", "product/app/%"}, |
| 115 | moduleTests: []moduleTest{ |
Anton Hansson | 43ab0bc | 2019-10-03 14:18:45 +0100 | [diff] [blame] | 116 | {module: systemModule, expectedPartition: "system_other/system"}, |
| 117 | {module: systemProductModule, expectedPartition: "system_other/system/product"}, |
Anton Hansson | da4d9d9 | 2020-09-15 09:28:55 +0000 | [diff] [blame^] | 118 | {module: productModule, expectedPartition: "product"}, |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 119 | }, |
| 120 | }, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 123 | for _, test := range tests { |
| 124 | global.PatternsOnSystemOther = test.patterns |
| 125 | for _, mt := range test.moduleTests { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 126 | rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, mt.module) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 127 | if err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | |
| 131 | name := mt.module.Name |
| 132 | wantInstalls := android.RuleBuilderInstalls{ |
| 133 | {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)}, |
| 134 | {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)}, |
| 135 | } |
| 136 | |
| 137 | if rule.Installs().String() != wantInstalls.String() { |
Anton Hansson | da4d9d9 | 2020-09-15 09:28:55 +0000 | [diff] [blame^] | 138 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 139 | } |
| 140 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | func TestDexPreoptProfile(t *testing.T) { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 146 | config := android.TestConfig("out", nil, "", nil) |
| 147 | ctx := android.PathContextForTesting(config) |
| 148 | globalSoong := GlobalSoongConfigForTests(config) |
| 149 | global := GlobalConfigForTests(ctx) |
| 150 | module := testSystemModuleConfig(ctx, "test") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 151 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 152 | module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile")) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 153 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 154 | rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 155 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 156 | t.Fatal(err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 159 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 160 | {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"}, |
| 161 | {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"}, |
| 162 | {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"}, |
| 163 | {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] | 164 | } |
| 165 | |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 166 | if rule.Installs().String() != wantInstalls.String() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 167 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 168 | } |
| 169 | } |