blob: df825bb3a5e3fe4953b3c5bf602d5ca5c2171072 [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,
85 }
86
Paul Duffine7975ff2021-02-11 15:33:37 +000087 java_library {
88 name: "foo-hiddenapi",
89 srcs: ["a.java"],
90 compile_dex: true,
91 }
92
Paul Duffin01289a22021-02-04 17:49:33 +000093 java_import {
94 name: "foo",
95 jars: ["a.jar"],
96 compile_dex: true,
97 prefer: false,
98 }
99
100 java_sdk_library {
101 name: "bar",
102 srcs: ["a.java"],
103 compile_dex: true,
104 }
105 `, []string{"platform:foo", "platform:bar"}, nil)
106
107 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
108 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
Paul Duffin4fd997b2021-02-03 20:06:33 +0000109 CheckHiddenAPIRuleInputs(t, `
Paul Duffin01289a22021-02-04 17:49:33 +0000110.intermediates/bar/android_common/hiddenapi/index.csv
Paul Duffine7975ff2021-02-11 15:33:37 +0000111.intermediates/foo-hiddenapi/android_common/hiddenapi/index.csv
Paul Duffin01289a22021-02-04 17:49:33 +0000112.intermediates/foo/android_common/hiddenapi/index.csv
Paul Duffin01289a22021-02-04 17:49:33 +0000113`,
114 indexRule)
115}
116
Liz Kammer5ca3a622020-08-05 15:40:41 -0700117func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100118 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700119 java_import {
120 name: "foo",
121 jars: ["a.jar"],
122 compile_dex: true,
123 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000124 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700125
126 hiddenAPI := ctx.SingletonForTests("hiddenapi")
127 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Bill Peckhamff89ffa2020-12-23 16:13:04 -0800128 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
Liz Kammer5ca3a622020-08-05 15:40:41 -0700129 if !strings.Contains(hiddenapiRule.RuleParams.Command, want) {
130 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
131 }
132}
133
134func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100135 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700136 java_library {
137 name: "foo",
138 srcs: ["a.java"],
139 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000140 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700141
142 java_import {
143 name: "foo",
144 jars: ["a.jar"],
145 compile_dex: true,
146 prefer: false,
Paul Duffin01289a22021-02-04 17:49:33 +0000147 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000148 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700149
150 hiddenAPI := ctx.SingletonForTests("hiddenapi")
151 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
152 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
153 if !strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
154 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
155 }
156
157 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar"
158 if strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
159 t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
160 }
161}
162
163func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100164 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700165 java_library {
166 name: "foo",
167 srcs: ["a.java"],
168 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000169 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700170
171 java_import {
172 name: "foo",
173 jars: ["a.jar"],
174 compile_dex: true,
175 prefer: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000176 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000177 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700178
179 hiddenAPI := ctx.SingletonForTests("hiddenapi")
180 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
181 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
182 if !strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
183 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
184 }
185
186 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
187 if strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
188 t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
189 }
190}
Anton Hanssona2adc372020-07-03 15:31:32 +0100191
192func TestHiddenAPISingletonSdks(t *testing.T) {
193 testCases := []struct {
194 name string
195 unbundledBuild bool
196 publicStub string
197 systemStub string
198 testStub string
199 corePlatformStub string
200 }{
201 {
202 name: "testBundled",
203 unbundledBuild: false,
204 publicStub: "android_stubs_current",
205 systemStub: "android_system_stubs_current",
206 testStub: "android_test_stubs_current",
207 corePlatformStub: "legacy.core.platform.api.stubs",
208 }, {
209 name: "testUnbundled",
210 unbundledBuild: true,
211 publicStub: "sdk_public_current_android",
212 systemStub: "sdk_system_current_android",
213 testStub: "sdk_test_current_android",
214 corePlatformStub: "legacy.core.platform.api.stubs",
215 },
216 }
217 for _, tc := range testCases {
218 t.Run(tc.name, func(t *testing.T) {
219 ctx, _ := testHiddenAPIUnbundled(t, tc.unbundledBuild)
220
221 hiddenAPI := ctx.SingletonForTests("hiddenapi")
222 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
223 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
224 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantPublicStubs) {
225 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantPublicStubs, hiddenapiRule.RuleParams.Command)
226 }
227
228 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
229 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantSystemStubs) {
230 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantSystemStubs, hiddenapiRule.RuleParams.Command)
231 }
232
233 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
234 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantTestStubs) {
235 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantTestStubs, hiddenapiRule.RuleParams.Command)
236 }
237
238 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(tc.corePlatformStub)
239 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) {
240 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantCorePlatformStubs, hiddenapiRule.RuleParams.Command)
241 }
242 })
243 }
244}
245
246func generateDexedPath(subDir, dex, module string) string {
247 return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module)
248}
249
250func generateDexPath(module string) string {
251 return generateDexedPath(module, "dex", module)
252}
253
254func generateSdkDexPath(module string, unbundled bool) string {
255 if unbundled {
256 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
257 }
258 return generateDexPath(module)
259}
Bill Peckhambae47492021-01-08 09:34:44 -0800260
261func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
262
263 // The idea behind this test is to ensure that when the build is
264 // confugured with a PrebuiltHiddenApiDir that the rules for the
265 // hiddenapi singleton copy the prebuilts to the typical output
266 // location, and then use that output location for the hiddenapi encode
267 // dex step.
268
269 // Where to find the prebuilt hiddenapi files:
270 prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
271
272 ctx, _ := testHiddenAPIBootJars(t, `
273 java_import {
274 name: "foo",
275 jars: ["a.jar"],
276 compile_dex: true,
277 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000278 `, []string{"platform:foo"}, &prebuiltHiddenApiDir)
Bill Peckhambae47492021-01-08 09:34:44 -0800279
280 expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
281 expectedCpOutput := buildDir + "/hiddenapi/hiddenapi-flags.csv"
282 expectedFlagsCsv := buildDir + "/hiddenapi/hiddenapi-flags.csv"
283
284 foo := ctx.ModuleForTests("foo", "android_common")
285
286 hiddenAPI := ctx.SingletonForTests("hiddenapi")
287 cpRule := hiddenAPI.Rule("Cp")
288 actualCpInput := cpRule.BuildParams.Input
289 actualCpOutput := cpRule.BuildParams.Output
290 encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
291 actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
292
293 if actualCpInput.String() != expectedCpInput {
294 t.Errorf("Prebuilt hiddenapi cp rule input mismatch, actual: %s, expected: %s", actualCpInput, expectedCpInput)
295 }
296
297 if actualCpOutput.String() != expectedCpOutput {
298 t.Errorf("Prebuilt hiddenapi cp rule output mismatch, actual: %s, expected: %s", actualCpOutput, expectedCpOutput)
299 }
300
301 if actualFlagsCsv != expectedFlagsCsv {
302 t.Errorf("Prebuilt hiddenapi encode dex rule flags csv mismatch, actual: %s, expected: %s", actualFlagsCsv, expectedFlagsCsv)
303 }
304}