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 | |
| 26 | func testConfigWithBootJars(bp string, bootJars []string) android.Config { |
| 27 | config := testConfig(nil, bp, nil) |
| 28 | config.TestProductVariables.BootJars = bootJars |
| 29 | return config |
| 30 | } |
| 31 | |
| 32 | func testContextWithHiddenAPI() *android.TestContext { |
| 33 | ctx := testContext() |
| 34 | ctx.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory) |
| 35 | return ctx |
| 36 | } |
| 37 | |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 38 | func testHiddenAPIWithConfig(t *testing.T, config android.Config) *android.TestContext { |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 39 | t.Helper() |
| 40 | |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 41 | ctx := testContextWithHiddenAPI() |
| 42 | |
| 43 | run(t, ctx, config) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 44 | return ctx |
| 45 | } |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 46 | |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 47 | func testHiddenAPIBootJars(t *testing.T, bp string, bootJars []string) (*android.TestContext, android.Config) { |
| 48 | config := testConfigWithBootJars(bp, bootJars) |
| 49 | |
| 50 | return testHiddenAPIWithConfig(t, config), config |
| 51 | } |
| 52 | |
| 53 | func testHiddenAPIUnbundled(t *testing.T, unbundled bool) (*android.TestContext, android.Config) { |
| 54 | config := testConfig(nil, ``, nil) |
| 55 | config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(unbundled) |
| 56 | |
| 57 | return testHiddenAPIWithConfig(t, config), config |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | func TestHiddenAPISingleton(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 61 | t.Parallel() |
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 | } |
| 68 | `, []string{":foo"}) |
| 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) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 79 | t.Parallel() |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 80 | ctx, _ := testHiddenAPIBootJars(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 81 | java_import { |
| 82 | name: "foo", |
| 83 | jars: ["a.jar"], |
| 84 | compile_dex: true, |
| 85 | } |
| 86 | `, []string{":foo"}) |
| 87 | |
| 88 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 89 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 90 | want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar" |
| 91 | if !strings.Contains(hiddenapiRule.RuleParams.Command, want) { |
| 92 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 97 | t.Parallel() |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 98 | ctx, _ := testHiddenAPIBootJars(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 99 | java_library { |
| 100 | name: "foo", |
| 101 | srcs: ["a.java"], |
| 102 | compile_dex: true, |
| 103 | } |
| 104 | |
| 105 | java_import { |
| 106 | name: "foo", |
| 107 | jars: ["a.jar"], |
| 108 | compile_dex: true, |
| 109 | prefer: false, |
| 110 | } |
| 111 | `, []string{":foo"}) |
| 112 | |
| 113 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 114 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 115 | fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" |
| 116 | if !strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) { |
| 117 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command) |
| 118 | } |
| 119 | |
| 120 | prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar" |
| 121 | if strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) { |
| 122 | t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 127 | t.Parallel() |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 128 | ctx, _ := testHiddenAPIBootJars(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 129 | java_library { |
| 130 | name: "foo", |
| 131 | srcs: ["a.java"], |
| 132 | compile_dex: true, |
| 133 | } |
| 134 | |
| 135 | java_import { |
| 136 | name: "foo", |
| 137 | jars: ["a.jar"], |
| 138 | compile_dex: true, |
| 139 | prefer: true, |
| 140 | } |
| 141 | `, []string{":foo"}) |
| 142 | |
| 143 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 144 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 145 | prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar" |
| 146 | if !strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) { |
| 147 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command) |
| 148 | } |
| 149 | |
| 150 | fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" |
| 151 | if strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) { |
| 152 | t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command) |
| 153 | } |
| 154 | } |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 155 | |
| 156 | func TestHiddenAPISingletonSdks(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 157 | t.Parallel() |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 158 | testCases := []struct { |
| 159 | name string |
| 160 | unbundledBuild bool |
| 161 | publicStub string |
| 162 | systemStub string |
| 163 | testStub string |
| 164 | corePlatformStub string |
| 165 | }{ |
| 166 | { |
| 167 | name: "testBundled", |
| 168 | unbundledBuild: false, |
| 169 | publicStub: "android_stubs_current", |
| 170 | systemStub: "android_system_stubs_current", |
| 171 | testStub: "android_test_stubs_current", |
| 172 | corePlatformStub: "legacy.core.platform.api.stubs", |
| 173 | }, { |
| 174 | name: "testUnbundled", |
| 175 | unbundledBuild: true, |
| 176 | publicStub: "sdk_public_current_android", |
| 177 | systemStub: "sdk_system_current_android", |
| 178 | testStub: "sdk_test_current_android", |
| 179 | corePlatformStub: "legacy.core.platform.api.stubs", |
| 180 | }, |
| 181 | } |
| 182 | for _, tc := range testCases { |
| 183 | t.Run(tc.name, func(t *testing.T) { |
| 184 | ctx, _ := testHiddenAPIUnbundled(t, tc.unbundledBuild) |
| 185 | |
| 186 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 187 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 188 | wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild) |
| 189 | if !strings.Contains(hiddenapiRule.RuleParams.Command, wantPublicStubs) { |
| 190 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantPublicStubs, hiddenapiRule.RuleParams.Command) |
| 191 | } |
| 192 | |
| 193 | wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild) |
| 194 | if !strings.Contains(hiddenapiRule.RuleParams.Command, wantSystemStubs) { |
| 195 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantSystemStubs, hiddenapiRule.RuleParams.Command) |
| 196 | } |
| 197 | |
| 198 | wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild) |
| 199 | if !strings.Contains(hiddenapiRule.RuleParams.Command, wantTestStubs) { |
| 200 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantTestStubs, hiddenapiRule.RuleParams.Command) |
| 201 | } |
| 202 | |
| 203 | wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(tc.corePlatformStub) |
| 204 | if !strings.Contains(hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) { |
| 205 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantCorePlatformStubs, hiddenapiRule.RuleParams.Command) |
| 206 | } |
| 207 | }) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | func generateDexedPath(subDir, dex, module string) string { |
| 212 | return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module) |
| 213 | } |
| 214 | |
| 215 | func generateDexPath(module string) string { |
| 216 | return generateDexedPath(module, "dex", module) |
| 217 | } |
| 218 | |
| 219 | func generateSdkDexPath(module string, unbundled bool) string { |
| 220 | if unbundled { |
| 221 | return generateDexedPath("prebuilts/sdk/"+module, "dex", module) |
| 222 | } |
| 223 | return generateDexPath(module) |
| 224 | } |