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 | |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 23 | func testSystemModuleConfig(ctx android.PathContext, name string) ModuleConfig { |
| 24 | return testModuleConfig(ctx, name, "system") |
| 25 | } |
| 26 | |
| 27 | func testSystemProductModuleConfig(ctx android.PathContext, name string) ModuleConfig { |
| 28 | return testModuleConfig(ctx, name, "system/product") |
| 29 | } |
| 30 | |
| 31 | func testProductModuleConfig(ctx android.PathContext, name string) ModuleConfig { |
| 32 | return testModuleConfig(ctx, name, "product") |
| 33 | } |
| 34 | |
| 35 | func testModuleConfig(ctx android.PathContext, name, partition string) ModuleConfig { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 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, |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 47 | PresentOptionalUsesLibraries: 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) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 64 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 65 | global, module := GlobalConfigForTests(ctx), testSystemModuleConfig(ctx, "test") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 66 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 67 | rule, err := GenerateDexpreoptRule(ctx, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 68 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 69 | t.Fatal(err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 72 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 73 | {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"}, |
| 74 | {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] | 75 | } |
| 76 | |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 77 | if rule.Installs().String() != wantInstalls.String() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 78 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestDexPreoptSystemOther(t *testing.T) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 83 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 84 | global := GlobalConfigForTests(ctx) |
| 85 | systemModule := testSystemModuleConfig(ctx, "Stest") |
| 86 | systemProductModule := testSystemProductModuleConfig(ctx, "SPtest") |
| 87 | productModule := testProductModuleConfig(ctx, "Ptest") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 88 | |
| 89 | global.HasSystemOther = true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 90 | |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 91 | type moduleTest struct { |
| 92 | module ModuleConfig |
| 93 | expectedPartition string |
| 94 | } |
| 95 | tests := []struct { |
| 96 | patterns []string |
| 97 | moduleTests []moduleTest |
| 98 | }{ |
| 99 | { |
| 100 | patterns: []string{"app/%"}, |
| 101 | moduleTests: []moduleTest{ |
Anton Hansson | 43ab0bc | 2019-10-03 14:18:45 +0100 | [diff] [blame] | 102 | {module: systemModule, expectedPartition: "system_other/system"}, |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 103 | {module: systemProductModule, expectedPartition: "system/product"}, |
| 104 | {module: productModule, expectedPartition: "product"}, |
| 105 | }, |
| 106 | }, |
Anton Hansson | d57bd3c | 2019-10-14 16:53:02 +0100 | [diff] [blame^] | 107 | // product/app/% only applies to product apps inside the system partition |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 108 | { |
| 109 | patterns: []string{"app/%", "product/app/%"}, |
| 110 | moduleTests: []moduleTest{ |
Anton Hansson | 43ab0bc | 2019-10-03 14:18:45 +0100 | [diff] [blame] | 111 | {module: systemModule, expectedPartition: "system_other/system"}, |
| 112 | {module: systemProductModule, expectedPartition: "system_other/system/product"}, |
Anton Hansson | d57bd3c | 2019-10-14 16:53:02 +0100 | [diff] [blame^] | 113 | {module: productModule, expectedPartition: "product"}, |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 114 | }, |
| 115 | }, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 118 | for _, test := range tests { |
| 119 | global.PatternsOnSystemOther = test.patterns |
| 120 | for _, mt := range test.moduleTests { |
| 121 | rule, err := GenerateDexpreoptRule(ctx, global, mt.module) |
| 122 | if err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | |
| 126 | name := mt.module.Name |
| 127 | wantInstalls := android.RuleBuilderInstalls{ |
| 128 | {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)}, |
| 129 | {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)}, |
| 130 | } |
| 131 | |
| 132 | if rule.Installs().String() != wantInstalls.String() { |
Anton Hansson | d57bd3c | 2019-10-14 16:53:02 +0100 | [diff] [blame^] | 133 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 134 | } |
| 135 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | func TestDexPreoptProfile(t *testing.T) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 141 | ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil) |
Anton Hansson | 4ec91dd | 2019-10-02 17:42:44 +0100 | [diff] [blame] | 142 | global, module := GlobalConfigForTests(ctx), testSystemModuleConfig(ctx, "test") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 143 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 144 | module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile")) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 145 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 146 | rule, err := GenerateDexpreoptRule(ctx, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 147 | if err != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 148 | t.Fatal(err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 151 | wantInstalls := android.RuleBuilderInstalls{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 152 | {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"}, |
| 153 | {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"}, |
| 154 | {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"}, |
| 155 | {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] | 156 | } |
| 157 | |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 158 | if rule.Installs().String() != wantInstalls.String() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 159 | t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs()) |
| 160 | } |
| 161 | } |