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