blob: fcaa94adca808b29793396e5a1851d0703ced27f [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 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
87func 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 Duffin01289a22021-02-04 17:49:33 +0000112.intermediates/bar/android_common/hiddenapi/index.csv
113.intermediates/foo/android_common/hiddenapi/index.csv
Paul Duffin01289a22021-02-04 17:49:33 +0000114`,
115 indexRule)
116}
117
Liz Kammer5ca3a622020-08-05 15:40:41 -0700118func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100119 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700120 java_import {
121 name: "foo",
122 jars: ["a.jar"],
123 compile_dex: true,
124 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000125 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700126
127 hiddenAPI := ctx.SingletonForTests("hiddenapi")
128 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Bill Peckhamff89ffa2020-12-23 16:13:04 -0800129 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
Liz Kammer5ca3a622020-08-05 15:40:41 -0700130 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
135func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100136 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700137 java_library {
138 name: "foo",
139 srcs: ["a.java"],
140 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000141 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700142
143 java_import {
144 name: "foo",
145 jars: ["a.jar"],
146 compile_dex: true,
147 prefer: false,
Paul Duffin01289a22021-02-04 17:49:33 +0000148 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000149 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700150
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
164func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100165 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700166 java_library {
167 name: "foo",
168 srcs: ["a.java"],
169 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000170 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700171
172 java_import {
173 name: "foo",
174 jars: ["a.jar"],
175 compile_dex: true,
176 prefer: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000177 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000178 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700179
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 Hanssona2adc372020-07-03 15:31:32 +0100192
193func 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
247func generateDexedPath(subDir, dex, module string) string {
248 return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module)
249}
250
251func generateDexPath(module string) string {
252 return generateDexedPath(module, "dex", module)
253}
254
255func generateSdkDexPath(module string, unbundled bool) string {
256 if unbundled {
257 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
258 }
259 return generateDexPath(module)
260}
Bill Peckhambae47492021-01-08 09:34:44 -0800261
262func 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 Duffin9c3ac962021-02-03 14:11:27 +0000279 `, []string{"platform:foo"}, &prebuiltHiddenApiDir)
Bill Peckhambae47492021-01-08 09:34:44 -0800280
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}