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