Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1 | // Copyright 2020 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 java |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 19 | "fmt" |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 20 | "strings" |
| 21 | "testing" |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 22 | |
| 23 | "github.com/google/blueprint/proptools" |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 26 | func testConfigWithBootJars(bp string, bootJars []string, prebuiltHiddenApiDir *string) android.Config { |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 27 | config := testConfig(nil, bp, nil) |
Paul Duffin | e10dfa4 | 2020-10-23 21:23:44 +0100 | [diff] [blame] | 28 | config.TestProductVariables.BootJars = android.CreateTestConfiguredJarList(bootJars) |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 29 | config.TestProductVariables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 30 | return config |
| 31 | } |
| 32 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 33 | func testContextWithHiddenAPI(config android.Config) *android.TestContext { |
| 34 | ctx := testContext(config) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 35 | ctx.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory) |
| 36 | return ctx |
| 37 | } |
| 38 | |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 39 | func testHiddenAPIWithConfig(t *testing.T, config android.Config) *android.TestContext { |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 40 | t.Helper() |
| 41 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 42 | ctx := testContextWithHiddenAPI(config) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 43 | |
| 44 | run(t, ctx, config) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 45 | return ctx |
| 46 | } |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 47 | |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 48 | func testHiddenAPIBootJars(t *testing.T, bp string, bootJars []string, prebuiltHiddenApiDir *string) (*android.TestContext, android.Config) { |
| 49 | config := testConfigWithBootJars(bp, bootJars, prebuiltHiddenApiDir) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 50 | |
| 51 | return testHiddenAPIWithConfig(t, config), config |
| 52 | } |
| 53 | |
| 54 | func testHiddenAPIUnbundled(t *testing.T, unbundled bool) (*android.TestContext, android.Config) { |
| 55 | config := testConfig(nil, ``, nil) |
| 56 | config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(unbundled) |
| 57 | |
| 58 | return testHiddenAPIWithConfig(t, config), config |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func TestHiddenAPISingleton(t *testing.T) { |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 62 | ctx, _ := testHiddenAPIBootJars(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 63 | java_library { |
| 64 | name: "foo", |
| 65 | srcs: ["a.java"], |
| 66 | compile_dex: true, |
| 67 | } |
Paul Duffin | 9c3ac96 | 2021-02-03 14:11:27 +0000 | [diff] [blame^] | 68 | `, []string{"platform:foo"}, nil) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 69 | |
| 70 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 71 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 72 | want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" |
| 73 | if !strings.Contains(hiddenapiRule.RuleParams.Command, want) { |
| 74 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestHiddenAPISingletonWithPrebuilt(t *testing.T) { |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 79 | ctx, _ := testHiddenAPIBootJars(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 80 | java_import { |
| 81 | name: "foo", |
| 82 | jars: ["a.jar"], |
| 83 | compile_dex: true, |
| 84 | } |
Paul Duffin | 9c3ac96 | 2021-02-03 14:11:27 +0000 | [diff] [blame^] | 85 | `, []string{"platform:foo"}, nil) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 86 | |
| 87 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 88 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
Bill Peckham | ff89ffa | 2020-12-23 16:13:04 -0800 | [diff] [blame] | 89 | want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 90 | if !strings.Contains(hiddenapiRule.RuleParams.Command, want) { |
| 91 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) { |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 96 | ctx, _ := testHiddenAPIBootJars(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 97 | java_library { |
| 98 | name: "foo", |
| 99 | srcs: ["a.java"], |
| 100 | compile_dex: true, |
| 101 | } |
| 102 | |
| 103 | java_import { |
| 104 | name: "foo", |
| 105 | jars: ["a.jar"], |
| 106 | compile_dex: true, |
| 107 | prefer: false, |
| 108 | } |
Paul Duffin | 9c3ac96 | 2021-02-03 14:11:27 +0000 | [diff] [blame^] | 109 | `, []string{"platform:foo"}, nil) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 110 | |
| 111 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 112 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 113 | fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" |
| 114 | if !strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) { |
| 115 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command) |
| 116 | } |
| 117 | |
| 118 | prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar" |
| 119 | if strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) { |
| 120 | t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) { |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 125 | ctx, _ := testHiddenAPIBootJars(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 126 | java_library { |
| 127 | name: "foo", |
| 128 | srcs: ["a.java"], |
| 129 | compile_dex: true, |
| 130 | } |
| 131 | |
| 132 | java_import { |
| 133 | name: "foo", |
| 134 | jars: ["a.jar"], |
| 135 | compile_dex: true, |
| 136 | prefer: true, |
| 137 | } |
Paul Duffin | 9c3ac96 | 2021-02-03 14:11:27 +0000 | [diff] [blame^] | 138 | `, []string{"platform:foo"}, nil) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 139 | |
| 140 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 141 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 142 | prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar" |
| 143 | if !strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) { |
| 144 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command) |
| 145 | } |
| 146 | |
| 147 | fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" |
| 148 | if strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) { |
| 149 | t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command) |
| 150 | } |
| 151 | } |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 152 | |
| 153 | func TestHiddenAPISingletonSdks(t *testing.T) { |
| 154 | testCases := []struct { |
| 155 | name string |
| 156 | unbundledBuild bool |
| 157 | publicStub string |
| 158 | systemStub string |
| 159 | testStub string |
| 160 | corePlatformStub string |
| 161 | }{ |
| 162 | { |
| 163 | name: "testBundled", |
| 164 | unbundledBuild: false, |
| 165 | publicStub: "android_stubs_current", |
| 166 | systemStub: "android_system_stubs_current", |
| 167 | testStub: "android_test_stubs_current", |
| 168 | corePlatformStub: "legacy.core.platform.api.stubs", |
| 169 | }, { |
| 170 | name: "testUnbundled", |
| 171 | unbundledBuild: true, |
| 172 | publicStub: "sdk_public_current_android", |
| 173 | systemStub: "sdk_system_current_android", |
| 174 | testStub: "sdk_test_current_android", |
| 175 | corePlatformStub: "legacy.core.platform.api.stubs", |
| 176 | }, |
| 177 | } |
| 178 | for _, tc := range testCases { |
| 179 | t.Run(tc.name, func(t *testing.T) { |
| 180 | ctx, _ := testHiddenAPIUnbundled(t, tc.unbundledBuild) |
| 181 | |
| 182 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 183 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 184 | wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild) |
| 185 | if !strings.Contains(hiddenapiRule.RuleParams.Command, wantPublicStubs) { |
| 186 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantPublicStubs, hiddenapiRule.RuleParams.Command) |
| 187 | } |
| 188 | |
| 189 | wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild) |
| 190 | if !strings.Contains(hiddenapiRule.RuleParams.Command, wantSystemStubs) { |
| 191 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantSystemStubs, hiddenapiRule.RuleParams.Command) |
| 192 | } |
| 193 | |
| 194 | wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild) |
| 195 | if !strings.Contains(hiddenapiRule.RuleParams.Command, wantTestStubs) { |
| 196 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantTestStubs, hiddenapiRule.RuleParams.Command) |
| 197 | } |
| 198 | |
| 199 | wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(tc.corePlatformStub) |
| 200 | if !strings.Contains(hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) { |
| 201 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantCorePlatformStubs, hiddenapiRule.RuleParams.Command) |
| 202 | } |
| 203 | }) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | func generateDexedPath(subDir, dex, module string) string { |
| 208 | return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module) |
| 209 | } |
| 210 | |
| 211 | func generateDexPath(module string) string { |
| 212 | return generateDexedPath(module, "dex", module) |
| 213 | } |
| 214 | |
| 215 | func generateSdkDexPath(module string, unbundled bool) string { |
| 216 | if unbundled { |
| 217 | return generateDexedPath("prebuilts/sdk/"+module, "dex", module) |
| 218 | } |
| 219 | return generateDexPath(module) |
| 220 | } |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 221 | |
| 222 | func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) { |
| 223 | |
| 224 | // The idea behind this test is to ensure that when the build is |
| 225 | // confugured with a PrebuiltHiddenApiDir that the rules for the |
| 226 | // hiddenapi singleton copy the prebuilts to the typical output |
| 227 | // location, and then use that output location for the hiddenapi encode |
| 228 | // dex step. |
| 229 | |
| 230 | // Where to find the prebuilt hiddenapi files: |
| 231 | prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi" |
| 232 | |
| 233 | ctx, _ := testHiddenAPIBootJars(t, ` |
| 234 | java_import { |
| 235 | name: "foo", |
| 236 | jars: ["a.jar"], |
| 237 | compile_dex: true, |
| 238 | } |
Paul Duffin | 9c3ac96 | 2021-02-03 14:11:27 +0000 | [diff] [blame^] | 239 | `, []string{"platform:foo"}, &prebuiltHiddenApiDir) |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 240 | |
| 241 | expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv" |
| 242 | expectedCpOutput := buildDir + "/hiddenapi/hiddenapi-flags.csv" |
| 243 | expectedFlagsCsv := buildDir + "/hiddenapi/hiddenapi-flags.csv" |
| 244 | |
| 245 | foo := ctx.ModuleForTests("foo", "android_common") |
| 246 | |
| 247 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 248 | cpRule := hiddenAPI.Rule("Cp") |
| 249 | actualCpInput := cpRule.BuildParams.Input |
| 250 | actualCpOutput := cpRule.BuildParams.Output |
| 251 | encodeDexRule := foo.Rule("hiddenAPIEncodeDex") |
| 252 | actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"] |
| 253 | |
| 254 | if actualCpInput.String() != expectedCpInput { |
| 255 | t.Errorf("Prebuilt hiddenapi cp rule input mismatch, actual: %s, expected: %s", actualCpInput, expectedCpInput) |
| 256 | } |
| 257 | |
| 258 | if actualCpOutput.String() != expectedCpOutput { |
| 259 | t.Errorf("Prebuilt hiddenapi cp rule output mismatch, actual: %s, expected: %s", actualCpOutput, expectedCpOutput) |
| 260 | } |
| 261 | |
| 262 | if actualFlagsCsv != expectedFlagsCsv { |
| 263 | t.Errorf("Prebuilt hiddenapi encode dex rule flags csv mismatch, actual: %s, expected: %s", actualFlagsCsv, expectedFlagsCsv) |
| 264 | } |
| 265 | } |