blob: 5c449e5067823deacd2e44bdbd63abae76415301 [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"
Paul Duffin175947f2021-03-12 21:44:02 +000019 "path/filepath"
Liz Kammer5ca3a622020-08-05 15:40:41 -070020 "testing"
Anton Hanssona2adc372020-07-03 15:31:32 +010021
Paul Duffin01289a22021-02-04 17:49:33 +000022 "android/soong/android"
Anton Hanssona2adc372020-07-03 15:31:32 +010023 "github.com/google/blueprint/proptools"
Liz Kammer5ca3a622020-08-05 15:40:41 -070024)
25
Paul Duffin175947f2021-03-12 21:44:02 +000026func fixtureSetBootJarsProductVariable(bootJars ...string) android.FixturePreparer {
27 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
28 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
29 })
Liz Kammer5ca3a622020-08-05 15:40:41 -070030}
31
Paul Duffin175947f2021-03-12 21:44:02 +000032func fixtureSetPrebuiltHiddenApiDirProductVariable(prebuiltHiddenApiDir *string) android.FixturePreparer {
33 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
34 variables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
35 })
Liz Kammer5ca3a622020-08-05 15:40:41 -070036}
37
Paul Duffin71ae5942021-03-22 15:36:52 +000038var hiddenApiFixtureFactory = android.GroupFixturePreparers(
39 prepareForJavaTest, PrepareForTestWithHiddenApiBuildComponents)
Liz Kammer5ca3a622020-08-05 15:40:41 -070040
41func TestHiddenAPISingleton(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +010042 result := android.GroupFixturePreparers(
43 hiddenApiFixtureFactory,
Paul Duffin175947f2021-03-12 21:44:02 +000044 fixtureSetBootJarsProductVariable("platform:foo"),
45 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070046 java_library {
47 name: "foo",
48 srcs: ["a.java"],
49 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +000050 }
Paul Duffin175947f2021-03-12 21:44:02 +000051 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -070052
Paul Duffin175947f2021-03-12 21:44:02 +000053 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +010054 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Paul Duffin2f9e71e2021-03-22 16:24:49 +000055 want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +000056 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -070057}
58
Paul Duffin01289a22021-02-04 17:49:33 +000059func TestHiddenAPIIndexSingleton(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +010060 result := android.GroupFixturePreparers(
61 hiddenApiFixtureFactory,
Paul Duffindc92abb2021-03-13 08:28:35 +000062 PrepareForTestWithJavaSdkLibraryFiles,
63 FixtureWithLastReleaseApis("bar"),
Paul Duffin175947f2021-03-12 21:44:02 +000064 fixtureSetBootJarsProductVariable("platform:foo", "platform:bar"),
65 ).RunTestWithBp(t, `
Paul Duffin01289a22021-02-04 17:49:33 +000066 java_library {
67 name: "foo",
68 srcs: ["a.java"],
69 compile_dex: true,
Paul Duffin031d8692021-02-12 11:46:42 +000070
71 hiddenapi_additional_annotations: [
72 "foo-hiddenapi-annotations",
73 ],
Paul Duffin01289a22021-02-04 17:49:33 +000074 }
75
Paul Duffine7975ff2021-02-11 15:33:37 +000076 java_library {
Paul Duffin031d8692021-02-12 11:46:42 +000077 name: "foo-hiddenapi-annotations",
78 srcs: ["a.java"],
79 compile_dex: true,
80 }
81
Paul Duffin01289a22021-02-04 17:49:33 +000082 java_import {
83 name: "foo",
84 jars: ["a.jar"],
85 compile_dex: true,
86 prefer: false,
87 }
88
89 java_sdk_library {
90 name: "bar",
91 srcs: ["a.java"],
92 compile_dex: true,
93 }
Paul Duffin175947f2021-03-12 21:44:02 +000094 `)
Paul Duffin01289a22021-02-04 17:49:33 +000095
Paul Duffin175947f2021-03-12 21:44:02 +000096 hiddenAPIIndex := result.SingletonForTests("hiddenapi_index")
Paul Duffin01289a22021-02-04 17:49:33 +000097 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
Paul Duffin4fd997b2021-02-03 20:06:33 +000098 CheckHiddenAPIRuleInputs(t, `
Paul Duffin01289a22021-02-04 17:49:33 +000099.intermediates/bar/android_common/hiddenapi/index.csv
100.intermediates/foo/android_common/hiddenapi/index.csv
Paul Duffin01289a22021-02-04 17:49:33 +0000101`,
102 indexRule)
Paul Duffin031d8692021-02-12 11:46:42 +0000103
104 // Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that
105 // creates the index.csv file.
Paul Duffin175947f2021-03-12 21:44:02 +0000106 foo := result.ModuleForTests("foo", "android_common")
Paul Duffin031d8692021-02-12 11:46:42 +0000107 indexParams := foo.Output("hiddenapi/index.csv")
108 CheckHiddenAPIRuleInputs(t, `
109.intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar
110.intermediates/foo/android_common/javac/foo.jar
111`, indexParams)
Paul Duffin01289a22021-02-04 17:49:33 +0000112}
113
Paul Duffinec0fe172021-02-25 15:34:13 +0000114func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +0000115 expectedErrorMessage :=
116 "hiddenapi has determined that the source module \"foo\" should be ignored as it has been" +
117 " replaced by the prebuilt module \"prebuilt_foo\" but unfortunately it does not provide a" +
118 " suitable boot dex jar"
119
Paul Duffin79abe572021-03-29 02:16:14 +0100120 android.GroupFixturePreparers(
121 hiddenApiFixtureFactory,
Paul Duffin175947f2021-03-12 21:44:02 +0000122 fixtureSetBootJarsProductVariable("platform:foo"),
123 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)).
124 RunTestWithBp(t, `
Paul Duffinec0fe172021-02-25 15:34:13 +0000125 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 prefer: true,
135 }
Paul Duffin175947f2021-03-12 21:44:02 +0000136 `)
Paul Duffinec0fe172021-02-25 15:34:13 +0000137}
138
Liz Kammer5ca3a622020-08-05 15:40:41 -0700139func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100140 result := android.GroupFixturePreparers(
141 hiddenApiFixtureFactory,
Paul Duffin175947f2021-03-12 21:44:02 +0000142 fixtureSetBootJarsProductVariable("platform:foo"),
143 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700144 java_import {
145 name: "foo",
146 jars: ["a.jar"],
147 compile_dex: true,
148 }
Paul Duffin175947f2021-03-12 21:44:02 +0000149 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700150
Paul Duffin175947f2021-03-12 21:44:02 +0000151 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +0100152 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000153 want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000154 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700155}
156
157func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100158 result := android.GroupFixturePreparers(
159 hiddenApiFixtureFactory,
Paul Duffin175947f2021-03-12 21:44:02 +0000160 fixtureSetBootJarsProductVariable("platform:foo"),
161 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700162 java_library {
163 name: "foo",
164 srcs: ["a.java"],
165 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000166 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700167
168 java_import {
169 name: "foo",
170 jars: ["a.jar"],
171 compile_dex: true,
172 prefer: false,
Paul Duffin01289a22021-02-04 17:49:33 +0000173 }
Paul Duffin175947f2021-03-12 21:44:02 +0000174 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700175
Paul Duffin175947f2021-03-12 21:44:02 +0000176 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +0100177 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000178 fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000179 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700180
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000181 prebuiltJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000182 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700183}
184
185func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100186 result := android.GroupFixturePreparers(
187 hiddenApiFixtureFactory,
Paul Duffin175947f2021-03-12 21:44:02 +0000188 fixtureSetBootJarsProductVariable("platform:foo"),
189 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700190 java_library {
191 name: "foo",
192 srcs: ["a.java"],
193 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000194 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700195
196 java_import {
197 name: "foo",
198 jars: ["a.jar"],
199 compile_dex: true,
200 prefer: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000201 }
Paul Duffin175947f2021-03-12 21:44:02 +0000202 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700203
Paul Duffin175947f2021-03-12 21:44:02 +0000204 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +0100205 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000206 prebuiltJarArg := "--boot-dex=out/soong/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000207 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700208
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000209 fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000210 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700211}
Anton Hanssona2adc372020-07-03 15:31:32 +0100212
213func TestHiddenAPISingletonSdks(t *testing.T) {
214 testCases := []struct {
215 name string
216 unbundledBuild bool
217 publicStub string
218 systemStub string
219 testStub string
220 corePlatformStub string
Paul Duffindc92abb2021-03-13 08:28:35 +0000221
222 // Additional test preparer
223 preparer android.FixturePreparer
Anton Hanssona2adc372020-07-03 15:31:32 +0100224 }{
225 {
226 name: "testBundled",
227 unbundledBuild: false,
228 publicStub: "android_stubs_current",
229 systemStub: "android_system_stubs_current",
230 testStub: "android_test_stubs_current",
231 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000232 preparer: android.GroupFixturePreparers(),
Anton Hanssona2adc372020-07-03 15:31:32 +0100233 }, {
234 name: "testUnbundled",
235 unbundledBuild: true,
236 publicStub: "sdk_public_current_android",
237 systemStub: "sdk_system_current_android",
238 testStub: "sdk_test_current_android",
239 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000240 preparer: PrepareForTestWithPrebuiltsOfCurrentApi,
Anton Hanssona2adc372020-07-03 15:31:32 +0100241 },
242 }
243 for _, tc := range testCases {
244 t.Run(tc.name, func(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100245 result := android.GroupFixturePreparers(
246 hiddenApiFixtureFactory,
Paul Duffindc92abb2021-03-13 08:28:35 +0000247 tc.preparer,
Paul Duffin175947f2021-03-12 21:44:02 +0000248 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
249 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild)
250 }),
251 ).RunTest(t)
Anton Hanssona2adc372020-07-03 15:31:32 +0100252
Paul Duffin175947f2021-03-12 21:44:02 +0000253 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +0100254 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Anton Hanssona2adc372020-07-03 15:31:32 +0100255 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000256 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100257
258 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000259 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100260
261 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000262 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100263
Paul Duffin175947f2021-03-12 21:44:02 +0000264 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub)
265 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100266 })
267 }
268}
269
270func generateDexedPath(subDir, dex, module string) string {
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000271 return fmt.Sprintf("out/soong/.intermediates/%s/android_common/%s/%s.jar", subDir, dex, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100272}
273
Paul Duffin175947f2021-03-12 21:44:02 +0000274func generateDexPath(moduleDir string, module string) string {
275 return generateDexedPath(filepath.Join(moduleDir, module), "dex", module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100276}
277
278func generateSdkDexPath(module string, unbundled bool) string {
279 if unbundled {
280 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
281 }
Paul Duffin175947f2021-03-12 21:44:02 +0000282 return generateDexPath(defaultJavaDir, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100283}
Bill Peckhambae47492021-01-08 09:34:44 -0800284
285func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
286
287 // The idea behind this test is to ensure that when the build is
288 // confugured with a PrebuiltHiddenApiDir that the rules for the
289 // hiddenapi singleton copy the prebuilts to the typical output
290 // location, and then use that output location for the hiddenapi encode
291 // dex step.
292
293 // Where to find the prebuilt hiddenapi files:
294 prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
295
Paul Duffin79abe572021-03-29 02:16:14 +0100296 result := android.GroupFixturePreparers(
297 hiddenApiFixtureFactory,
Paul Duffin175947f2021-03-12 21:44:02 +0000298 fixtureSetBootJarsProductVariable("platform:foo"),
299 fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir),
300 ).RunTestWithBp(t, `
Bill Peckhambae47492021-01-08 09:34:44 -0800301 java_import {
302 name: "foo",
303 jars: ["a.jar"],
304 compile_dex: true,
305 }
Paul Duffin175947f2021-03-12 21:44:02 +0000306 `)
Bill Peckhambae47492021-01-08 09:34:44 -0800307
308 expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000309 expectedCpOutput := "out/soong/hiddenapi/hiddenapi-flags.csv"
310 expectedFlagsCsv := "out/soong/hiddenapi/hiddenapi-flags.csv"
Bill Peckhambae47492021-01-08 09:34:44 -0800311
Paul Duffin175947f2021-03-12 21:44:02 +0000312 foo := result.ModuleForTests("foo", "android_common")
Bill Peckhambae47492021-01-08 09:34:44 -0800313
Paul Duffin175947f2021-03-12 21:44:02 +0000314 hiddenAPI := result.SingletonForTests("hiddenapi")
Bill Peckhambae47492021-01-08 09:34:44 -0800315 cpRule := hiddenAPI.Rule("Cp")
316 actualCpInput := cpRule.BuildParams.Input
317 actualCpOutput := cpRule.BuildParams.Output
Paul Duffina71a67a2021-03-29 00:42:57 +0100318 encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
Bill Peckhambae47492021-01-08 09:34:44 -0800319 actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
320
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000321 android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput)
Bill Peckhambae47492021-01-08 09:34:44 -0800322
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000323 android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput)
Bill Peckhambae47492021-01-08 09:34:44 -0800324
Paul Duffin175947f2021-03-12 21:44:02 +0000325 android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv)
Bill Peckhambae47492021-01-08 09:34:44 -0800326}