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) { |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame^] | 61 | ctx, _ := testHiddenAPIBootJars(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 62 | java_library { |
| 63 | name: "foo", |
| 64 | srcs: ["a.java"], |
| 65 | compile_dex: true, |
| 66 | } |
| 67 | `, []string{":foo"}) |
| 68 | |
| 69 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 70 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 71 | want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" |
| 72 | if !strings.Contains(hiddenapiRule.RuleParams.Command, want) { |
| 73 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestHiddenAPISingletonWithPrebuilt(t *testing.T) { |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame^] | 78 | ctx, _ := testHiddenAPIBootJars(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 79 | java_import { |
| 80 | name: "foo", |
| 81 | jars: ["a.jar"], |
| 82 | compile_dex: true, |
| 83 | } |
| 84 | `, []string{":foo"}) |
| 85 | |
| 86 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 87 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 88 | want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar" |
| 89 | if !strings.Contains(hiddenapiRule.RuleParams.Command, want) { |
| 90 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) { |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame^] | 95 | ctx, _ := testHiddenAPIBootJars(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 96 | java_library { |
| 97 | name: "foo", |
| 98 | srcs: ["a.java"], |
| 99 | compile_dex: true, |
| 100 | } |
| 101 | |
| 102 | java_import { |
| 103 | name: "foo", |
| 104 | jars: ["a.jar"], |
| 105 | compile_dex: true, |
| 106 | prefer: false, |
| 107 | } |
| 108 | `, []string{":foo"}) |
| 109 | |
| 110 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 111 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 112 | fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" |
| 113 | if !strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) { |
| 114 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command) |
| 115 | } |
| 116 | |
| 117 | prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar" |
| 118 | if strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) { |
| 119 | t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) { |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame^] | 124 | ctx, _ := testHiddenAPIBootJars(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 125 | java_library { |
| 126 | name: "foo", |
| 127 | srcs: ["a.java"], |
| 128 | compile_dex: true, |
| 129 | } |
| 130 | |
| 131 | java_import { |
| 132 | name: "foo", |
| 133 | jars: ["a.jar"], |
| 134 | compile_dex: true, |
| 135 | prefer: true, |
| 136 | } |
| 137 | `, []string{":foo"}) |
| 138 | |
| 139 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 140 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 141 | prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar" |
| 142 | if !strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) { |
| 143 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command) |
| 144 | } |
| 145 | |
| 146 | fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" |
| 147 | if strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) { |
| 148 | t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command) |
| 149 | } |
| 150 | } |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame^] | 151 | |
| 152 | func TestHiddenAPISingletonSdks(t *testing.T) { |
| 153 | testCases := []struct { |
| 154 | name string |
| 155 | unbundledBuild bool |
| 156 | publicStub string |
| 157 | systemStub string |
| 158 | testStub string |
| 159 | corePlatformStub string |
| 160 | }{ |
| 161 | { |
| 162 | name: "testBundled", |
| 163 | unbundledBuild: false, |
| 164 | publicStub: "android_stubs_current", |
| 165 | systemStub: "android_system_stubs_current", |
| 166 | testStub: "android_test_stubs_current", |
| 167 | corePlatformStub: "legacy.core.platform.api.stubs", |
| 168 | }, { |
| 169 | name: "testUnbundled", |
| 170 | unbundledBuild: true, |
| 171 | publicStub: "sdk_public_current_android", |
| 172 | systemStub: "sdk_system_current_android", |
| 173 | testStub: "sdk_test_current_android", |
| 174 | corePlatformStub: "legacy.core.platform.api.stubs", |
| 175 | }, |
| 176 | } |
| 177 | for _, tc := range testCases { |
| 178 | t.Run(tc.name, func(t *testing.T) { |
| 179 | ctx, _ := testHiddenAPIUnbundled(t, tc.unbundledBuild) |
| 180 | |
| 181 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 182 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 183 | wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild) |
| 184 | if !strings.Contains(hiddenapiRule.RuleParams.Command, wantPublicStubs) { |
| 185 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantPublicStubs, hiddenapiRule.RuleParams.Command) |
| 186 | } |
| 187 | |
| 188 | wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild) |
| 189 | if !strings.Contains(hiddenapiRule.RuleParams.Command, wantSystemStubs) { |
| 190 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantSystemStubs, hiddenapiRule.RuleParams.Command) |
| 191 | } |
| 192 | |
| 193 | wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild) |
| 194 | if !strings.Contains(hiddenapiRule.RuleParams.Command, wantTestStubs) { |
| 195 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantTestStubs, hiddenapiRule.RuleParams.Command) |
| 196 | } |
| 197 | |
| 198 | wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(tc.corePlatformStub) |
| 199 | if !strings.Contains(hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) { |
| 200 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantCorePlatformStubs, hiddenapiRule.RuleParams.Command) |
| 201 | } |
| 202 | }) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | func generateDexedPath(subDir, dex, module string) string { |
| 207 | return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module) |
| 208 | } |
| 209 | |
| 210 | func generateDexPath(module string) string { |
| 211 | return generateDexedPath(module, "dex", module) |
| 212 | } |
| 213 | |
| 214 | func generateSdkDexPath(module string, unbundled bool) string { |
| 215 | if unbundled { |
| 216 | return generateDexedPath("prebuilts/sdk/"+module, "dex", module) |
| 217 | } |
| 218 | return generateDexPath(module) |
| 219 | } |