blob: 4670d03112f8d2581423af77f82126550973dc3a [file] [log] [blame]
Liz Kammer5ca3a622020-08-05 15:40:41 -07001// 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
15package java
16
17import (
Anton Hanssona2adc372020-07-03 15:31:32 +010018 "fmt"
Liz Kammer5ca3a622020-08-05 15:40:41 -070019 "strings"
20 "testing"
Anton Hanssona2adc372020-07-03 15:31:32 +010021
Paul Duffin01289a22021-02-04 17:49:33 +000022 "android/soong/android"
23
Anton Hanssona2adc372020-07-03 15:31:32 +010024 "github.com/google/blueprint/proptools"
Liz Kammer5ca3a622020-08-05 15:40:41 -070025)
26
Bill Peckhambae47492021-01-08 09:34:44 -080027func testConfigWithBootJars(bp string, bootJars []string, prebuiltHiddenApiDir *string) android.Config {
Liz Kammer5ca3a622020-08-05 15:40:41 -070028 config := testConfig(nil, bp, nil)
Paul Duffine10dfa42020-10-23 21:23:44 +010029 config.TestProductVariables.BootJars = android.CreateTestConfiguredJarList(bootJars)
Bill Peckhambae47492021-01-08 09:34:44 -080030 config.TestProductVariables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
Liz Kammer5ca3a622020-08-05 15:40:41 -070031 return config
32}
33
Colin Crossae8600b2020-10-29 17:09:13 -070034func testContextWithHiddenAPI(config android.Config) *android.TestContext {
35 ctx := testContext(config)
Paul Duffin01289a22021-02-04 17:49:33 +000036 RegisterHiddenApiSingletonComponents(ctx)
Liz Kammer5ca3a622020-08-05 15:40:41 -070037 return ctx
38}
39
Anton Hanssona2adc372020-07-03 15:31:32 +010040func testHiddenAPIWithConfig(t *testing.T, config android.Config) *android.TestContext {
Liz Kammer5ca3a622020-08-05 15:40:41 -070041 t.Helper()
42
Colin Crossae8600b2020-10-29 17:09:13 -070043 ctx := testContextWithHiddenAPI(config)
Liz Kammer5ca3a622020-08-05 15:40:41 -070044
45 run(t, ctx, config)
Anton Hanssona2adc372020-07-03 15:31:32 +010046 return ctx
47}
Liz Kammer5ca3a622020-08-05 15:40:41 -070048
Bill Peckhambae47492021-01-08 09:34:44 -080049func testHiddenAPIBootJars(t *testing.T, bp string, bootJars []string, prebuiltHiddenApiDir *string) (*android.TestContext, android.Config) {
50 config := testConfigWithBootJars(bp, bootJars, prebuiltHiddenApiDir)
Anton Hanssona2adc372020-07-03 15:31:32 +010051
52 return testHiddenAPIWithConfig(t, config), config
53}
54
55func 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 Kammer5ca3a622020-08-05 15:40:41 -070060}
61
62func TestHiddenAPISingleton(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +010063 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070064 java_library {
65 name: "foo",
66 srcs: ["a.java"],
67 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +000068 }
Paul Duffin9c3ac962021-02-03 14:11:27 +000069 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -070070
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 Duffin01289a22021-02-04 17:49:33 +000079func TestHiddenAPIIndexSingleton(t *testing.T) {
80 ctx, _ := testHiddenAPIBootJars(t, `
81 java_library {
82 name: "foo",
83 srcs: ["a.java"],
84 compile_dex: true,
Paul Duffin031d8692021-02-12 11:46:42 +000085
86 hiddenapi_additional_annotations: [
87 "foo-hiddenapi-annotations",
88 ],
Paul Duffin01289a22021-02-04 17:49:33 +000089 }
90
Paul Duffine7975ff2021-02-11 15:33:37 +000091 java_library {
92 name: "foo-hiddenapi",
93 srcs: ["a.java"],
94 compile_dex: true,
95 }
96
Paul Duffin031d8692021-02-12 11:46:42 +000097 java_library {
98 name: "foo-hiddenapi-annotations",
99 srcs: ["a.java"],
100 compile_dex: true,
101 }
102
Paul Duffin01289a22021-02-04 17:49:33 +0000103 java_import {
104 name: "foo",
105 jars: ["a.jar"],
106 compile_dex: true,
107 prefer: false,
108 }
109
110 java_sdk_library {
111 name: "bar",
112 srcs: ["a.java"],
113 compile_dex: true,
114 }
115 `, []string{"platform:foo", "platform:bar"}, nil)
116
117 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
118 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
Paul Duffin4fd997b2021-02-03 20:06:33 +0000119 CheckHiddenAPIRuleInputs(t, `
Paul Duffin01289a22021-02-04 17:49:33 +0000120.intermediates/bar/android_common/hiddenapi/index.csv
Paul Duffine7975ff2021-02-11 15:33:37 +0000121.intermediates/foo-hiddenapi/android_common/hiddenapi/index.csv
Paul Duffin01289a22021-02-04 17:49:33 +0000122.intermediates/foo/android_common/hiddenapi/index.csv
Paul Duffin01289a22021-02-04 17:49:33 +0000123`,
124 indexRule)
Paul Duffin031d8692021-02-12 11:46:42 +0000125
126 // Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that
127 // creates the index.csv file.
128 foo := ctx.ModuleForTests("foo", "android_common")
129 indexParams := foo.Output("hiddenapi/index.csv")
130 CheckHiddenAPIRuleInputs(t, `
131.intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar
132.intermediates/foo/android_common/javac/foo.jar
133`, indexParams)
Paul Duffin01289a22021-02-04 17:49:33 +0000134}
135
Liz Kammer5ca3a622020-08-05 15:40:41 -0700136func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100137 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700138 java_import {
139 name: "foo",
140 jars: ["a.jar"],
141 compile_dex: true,
142 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000143 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700144
145 hiddenAPI := ctx.SingletonForTests("hiddenapi")
146 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Bill Peckhamff89ffa2020-12-23 16:13:04 -0800147 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
Liz Kammer5ca3a622020-08-05 15:40:41 -0700148 if !strings.Contains(hiddenapiRule.RuleParams.Command, want) {
149 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
150 }
151}
152
153func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100154 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700155 java_library {
156 name: "foo",
157 srcs: ["a.java"],
158 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000159 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700160
161 java_import {
162 name: "foo",
163 jars: ["a.jar"],
164 compile_dex: true,
165 prefer: false,
Paul Duffin01289a22021-02-04 17:49:33 +0000166 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000167 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700168
169 hiddenAPI := ctx.SingletonForTests("hiddenapi")
170 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
171 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
172 if !strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
173 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
174 }
175
176 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar"
177 if strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
178 t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
179 }
180}
181
182func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100183 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700184 java_library {
185 name: "foo",
186 srcs: ["a.java"],
187 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000188 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700189
190 java_import {
191 name: "foo",
192 jars: ["a.jar"],
193 compile_dex: true,
194 prefer: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000195 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000196 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700197
198 hiddenAPI := ctx.SingletonForTests("hiddenapi")
199 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
200 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
201 if !strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
202 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
203 }
204
205 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
206 if strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
207 t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
208 }
209}
Anton Hanssona2adc372020-07-03 15:31:32 +0100210
211func TestHiddenAPISingletonSdks(t *testing.T) {
212 testCases := []struct {
213 name string
214 unbundledBuild bool
215 publicStub string
216 systemStub string
217 testStub string
218 corePlatformStub string
219 }{
220 {
221 name: "testBundled",
222 unbundledBuild: false,
223 publicStub: "android_stubs_current",
224 systemStub: "android_system_stubs_current",
225 testStub: "android_test_stubs_current",
226 corePlatformStub: "legacy.core.platform.api.stubs",
227 }, {
228 name: "testUnbundled",
229 unbundledBuild: true,
230 publicStub: "sdk_public_current_android",
231 systemStub: "sdk_system_current_android",
232 testStub: "sdk_test_current_android",
233 corePlatformStub: "legacy.core.platform.api.stubs",
234 },
235 }
236 for _, tc := range testCases {
237 t.Run(tc.name, func(t *testing.T) {
238 ctx, _ := testHiddenAPIUnbundled(t, tc.unbundledBuild)
239
240 hiddenAPI := ctx.SingletonForTests("hiddenapi")
241 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
242 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
243 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantPublicStubs) {
244 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantPublicStubs, hiddenapiRule.RuleParams.Command)
245 }
246
247 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
248 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantSystemStubs) {
249 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantSystemStubs, hiddenapiRule.RuleParams.Command)
250 }
251
252 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
253 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantTestStubs) {
254 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantTestStubs, hiddenapiRule.RuleParams.Command)
255 }
256
257 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(tc.corePlatformStub)
258 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) {
259 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantCorePlatformStubs, hiddenapiRule.RuleParams.Command)
260 }
261 })
262 }
263}
264
265func generateDexedPath(subDir, dex, module string) string {
266 return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module)
267}
268
269func generateDexPath(module string) string {
270 return generateDexedPath(module, "dex", module)
271}
272
273func generateSdkDexPath(module string, unbundled bool) string {
274 if unbundled {
275 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
276 }
277 return generateDexPath(module)
278}
Bill Peckhambae47492021-01-08 09:34:44 -0800279
280func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
281
282 // The idea behind this test is to ensure that when the build is
283 // confugured with a PrebuiltHiddenApiDir that the rules for the
284 // hiddenapi singleton copy the prebuilts to the typical output
285 // location, and then use that output location for the hiddenapi encode
286 // dex step.
287
288 // Where to find the prebuilt hiddenapi files:
289 prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
290
291 ctx, _ := testHiddenAPIBootJars(t, `
292 java_import {
293 name: "foo",
294 jars: ["a.jar"],
295 compile_dex: true,
296 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000297 `, []string{"platform:foo"}, &prebuiltHiddenApiDir)
Bill Peckhambae47492021-01-08 09:34:44 -0800298
299 expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
300 expectedCpOutput := buildDir + "/hiddenapi/hiddenapi-flags.csv"
301 expectedFlagsCsv := buildDir + "/hiddenapi/hiddenapi-flags.csv"
302
303 foo := ctx.ModuleForTests("foo", "android_common")
304
305 hiddenAPI := ctx.SingletonForTests("hiddenapi")
306 cpRule := hiddenAPI.Rule("Cp")
307 actualCpInput := cpRule.BuildParams.Input
308 actualCpOutput := cpRule.BuildParams.Output
309 encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
310 actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
311
312 if actualCpInput.String() != expectedCpInput {
313 t.Errorf("Prebuilt hiddenapi cp rule input mismatch, actual: %s, expected: %s", actualCpInput, expectedCpInput)
314 }
315
316 if actualCpOutput.String() != expectedCpOutput {
317 t.Errorf("Prebuilt hiddenapi cp rule output mismatch, actual: %s, expected: %s", actualCpOutput, expectedCpOutput)
318 }
319
320 if actualFlagsCsv != expectedFlagsCsv {
321 t.Errorf("Prebuilt hiddenapi encode dex rule flags csv mismatch, actual: %s, expected: %s", actualFlagsCsv, expectedFlagsCsv)
322 }
323}