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, |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 46 | EnforceUsesLibrariesStatusFile: android.PathForOutput(ctx, fmt.Sprintf("%s/enforce_uses_libraries.status", name)), |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 47 | EnforceUsesLibraries: false, |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 48 | ClassLoaderContexts: nil, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 49 | Archs: []android.ArchType{android.Arm}, |
| 50 | DexPreoptImages: android.Paths{android.PathForTesting("system/framework/arm/boot.art")}, |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 51 | DexPreoptImagesDeps: []android.OutputPaths{android.OutputPaths{}}, |
| 52 | DexPreoptImageLocations: []string{}, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 53 | PreoptBootClassPathDexFiles: nil, |
| 54 | PreoptBootClassPathDexLocations: nil, |
| 55 | PreoptExtractedApk: false, |
| 56 | NoCreateAppImage: false, |
| 57 | ForceCreateAppImage: false, |
| 58 | PresignedPrebuilt: false, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 59 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | func TestDexPreopt(t *testing.T) { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 63 | config := android.TestConfig("out", nil, "", nil) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 64 | ctx := android.BuilderContextForTesting(config) |
Paul Duffin | 9f04524 | 2021-01-21 15:05:11 +0000 | [diff] [blame] | 65 | globalSoong := globalSoongConfigForTests() |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 66 | global := GlobalConfigForTests(ctx) |
| 67 | module := testSystemModuleConfig(ctx, "test") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 68 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 69 | rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 70 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 71 | t.Fatal(err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 74 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 75 | {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"}, |
| 76 | {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] | 77 | } |
| 78 | |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 79 | if rule.Installs().String() != wantInstalls.String() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 80 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestDexPreoptSystemOther(t *testing.T) { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 85 | config := android.TestConfig("out", nil, "", nil) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 86 | ctx := android.BuilderContextForTesting(config) |
Paul Duffin | 9f04524 | 2021-01-21 15:05:11 +0000 | [diff] [blame] | 87 | globalSoong := globalSoongConfigForTests() |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 88 | global := GlobalConfigForTests(ctx) |
| 89 | systemModule := testSystemModuleConfig(ctx, "Stest") |
| 90 | systemProductModule := testSystemProductModuleConfig(ctx, "SPtest") |
| 91 | productModule := testProductModuleConfig(ctx, "Ptest") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 92 | |
| 93 | global.HasSystemOther = true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 94 | |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 95 | type moduleTest struct { |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 96 | module *ModuleConfig |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 97 | expectedPartition string |
| 98 | } |
| 99 | tests := []struct { |
| 100 | patterns []string |
| 101 | moduleTests []moduleTest |
| 102 | }{ |
| 103 | { |
| 104 | patterns: []string{"app/%"}, |
| 105 | moduleTests: []moduleTest{ |
Anton Hansson | 43ab0bc | 2019-10-03 14:18:45 +0100 | [diff] [blame] | 106 | {module: systemModule, expectedPartition: "system_other/system"}, |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 107 | {module: systemProductModule, expectedPartition: "system/product"}, |
| 108 | {module: productModule, expectedPartition: "product"}, |
| 109 | }, |
| 110 | }, |
Anton Hansson | da4d9d9 | 2020-09-15 09:28:55 +0000 | [diff] [blame] | 111 | // product/app/% only applies to product apps inside the system partition |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 112 | { |
| 113 | patterns: []string{"app/%", "product/app/%"}, |
| 114 | moduleTests: []moduleTest{ |
Anton Hansson | 43ab0bc | 2019-10-03 14:18:45 +0100 | [diff] [blame] | 115 | {module: systemModule, expectedPartition: "system_other/system"}, |
| 116 | {module: systemProductModule, expectedPartition: "system_other/system/product"}, |
Anton Hansson | da4d9d9 | 2020-09-15 09:28:55 +0000 | [diff] [blame] | 117 | {module: productModule, expectedPartition: "product"}, |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 118 | }, |
| 119 | }, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 122 | for _, test := range tests { |
| 123 | global.PatternsOnSystemOther = test.patterns |
| 124 | for _, mt := range test.moduleTests { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 125 | rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, mt.module) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 126 | if err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | |
| 130 | name := mt.module.Name |
| 131 | wantInstalls := android.RuleBuilderInstalls{ |
| 132 | {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)}, |
| 133 | {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)}, |
| 134 | } |
| 135 | |
| 136 | if rule.Installs().String() != wantInstalls.String() { |
Anton Hansson | da4d9d9 | 2020-09-15 09:28:55 +0000 | [diff] [blame] | 137 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | func TestDexPreoptProfile(t *testing.T) { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 145 | config := android.TestConfig("out", nil, "", nil) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 146 | ctx := android.BuilderContextForTesting(config) |
Paul Duffin | 9f04524 | 2021-01-21 15:05:11 +0000 | [diff] [blame] | 147 | globalSoong := globalSoongConfigForTests() |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 148 | global := GlobalConfigForTests(ctx) |
| 149 | module := testSystemModuleConfig(ctx, "test") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 150 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 151 | module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile")) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 152 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 153 | rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 154 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 155 | t.Fatal(err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 158 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 159 | {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"}, |
| 160 | {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"}, |
| 161 | {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"}, |
| 162 | {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] | 163 | } |
| 164 | |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 165 | if rule.Installs().String() != wantInstalls.String() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 166 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 167 | } |
| 168 | } |
Jeongik Cha | c624667 | 2021-04-08 00:00:19 +0900 | [diff] [blame^] | 169 | |
| 170 | func TestDexPreoptConfigToJson(t *testing.T) { |
| 171 | config := android.TestConfig("out", nil, "", nil) |
| 172 | ctx := android.BuilderContextForTesting(config) |
| 173 | module := testSystemModuleConfig(ctx, "test") |
| 174 | data, err := moduleConfigToJSON(module) |
| 175 | if err != nil { |
| 176 | t.Errorf("Failed to convert module config data to JSON, %v", err) |
| 177 | } |
| 178 | parsed, err := ParseModuleConfig(ctx, data) |
| 179 | if err != nil { |
| 180 | t.Errorf("Failed to parse JSON, %v", err) |
| 181 | } |
| 182 | before := fmt.Sprintf("%v", module) |
| 183 | after := fmt.Sprintf("%v", parsed) |
| 184 | android.AssertStringEquals(t, "The result must be the same as the original after marshalling and unmarshalling it.", before, after) |
| 185 | } |