blob: dcd363c2ca674345412e660d91c81849084e76f9 [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 Duffin74431d52021-04-21 14:10:42 +010026// TODO(b/177892522): Move these tests into a more appropriate place.
27
Paul Duffin175947f2021-03-12 21:44:02 +000028func fixtureSetPrebuiltHiddenApiDirProductVariable(prebuiltHiddenApiDir *string) android.FixturePreparer {
29 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
30 variables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
31 })
Liz Kammer5ca3a622020-08-05 15:40:41 -070032}
33
Paul Duffin74431d52021-04-21 14:10:42 +010034var prepareForTestWithDefaultPlatformBootclasspath = android.FixtureAddTextFile("frameworks/base/boot/Android.bp", `
35 platform_bootclasspath {
36 name: "platform-bootclasspath",
37 }
38`)
39
Paul Duffin71ae5942021-03-22 15:36:52 +000040var hiddenApiFixtureFactory = android.GroupFixturePreparers(
41 prepareForJavaTest, PrepareForTestWithHiddenApiBuildComponents)
Liz Kammer5ca3a622020-08-05 15:40:41 -070042
43func TestHiddenAPISingleton(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +010044 result := android.GroupFixturePreparers(
45 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010046 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +010047 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +000048 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070049 java_library {
50 name: "foo",
51 srcs: ["a.java"],
52 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +000053 }
Paul Duffin175947f2021-03-12 21:44:02 +000054 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -070055
Paul Duffin74431d52021-04-21 14:10:42 +010056 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
57 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Paul Duffin2f9e71e2021-03-22 16:24:49 +000058 want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +000059 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -070060}
61
Paul Duffinec0fe172021-02-25 15:34:13 +000062func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +010063 expectedErrorMessage := "module prebuilt_foo{os:android,arch:common} does not provide a dex jar"
Paul Duffin175947f2021-03-12 21:44:02 +000064
Paul Duffin79abe572021-03-29 02:16:14 +010065 android.GroupFixturePreparers(
66 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010067 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +010068 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +000069 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)).
70 RunTestWithBp(t, `
Paul Duffinec0fe172021-02-25 15:34:13 +000071 java_library {
72 name: "foo",
73 srcs: ["a.java"],
74 compile_dex: true,
75 }
76
77 java_import {
78 name: "foo",
79 jars: ["a.jar"],
80 prefer: true,
81 }
Paul Duffin175947f2021-03-12 21:44:02 +000082 `)
Paul Duffinec0fe172021-02-25 15:34:13 +000083}
84
Liz Kammer5ca3a622020-08-05 15:40:41 -070085func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +010086 result := android.GroupFixturePreparers(
87 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010088 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +010089 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +000090 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070091 java_import {
92 name: "foo",
93 jars: ["a.jar"],
94 compile_dex: true,
95 }
Paul Duffin175947f2021-03-12 21:44:02 +000096 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -070097
Paul Duffin74431d52021-04-21 14:10:42 +010098 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
99 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000100 want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000101 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700102}
103
104func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100105 result := android.GroupFixturePreparers(
106 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100107 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +0100108 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +0000109 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700110 java_library {
111 name: "foo",
112 srcs: ["a.java"],
113 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000114 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700115
116 java_import {
117 name: "foo",
118 jars: ["a.jar"],
119 compile_dex: true,
120 prefer: false,
Paul Duffin01289a22021-02-04 17:49:33 +0000121 }
Paul Duffin175947f2021-03-12 21:44:02 +0000122 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700123
Paul Duffin74431d52021-04-21 14:10:42 +0100124 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
125 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000126 fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000127 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700128
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000129 prebuiltJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000130 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700131}
132
133func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100134 result := android.GroupFixturePreparers(
135 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100136 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +0100137 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +0000138 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700139 java_library {
140 name: "foo",
141 srcs: ["a.java"],
142 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000143 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700144
145 java_import {
146 name: "foo",
147 jars: ["a.jar"],
148 compile_dex: true,
149 prefer: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000150 }
Paul Duffin175947f2021-03-12 21:44:02 +0000151 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700152
Paul Duffin74431d52021-04-21 14:10:42 +0100153 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
154 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000155 prebuiltJarArg := "--boot-dex=out/soong/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000156 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700157
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000158 fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000159 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700160}
Anton Hanssona2adc372020-07-03 15:31:32 +0100161
162func TestHiddenAPISingletonSdks(t *testing.T) {
163 testCases := []struct {
164 name string
165 unbundledBuild bool
166 publicStub string
167 systemStub string
168 testStub string
169 corePlatformStub string
Paul Duffindc92abb2021-03-13 08:28:35 +0000170
171 // Additional test preparer
172 preparer android.FixturePreparer
Anton Hanssona2adc372020-07-03 15:31:32 +0100173 }{
174 {
175 name: "testBundled",
176 unbundledBuild: false,
177 publicStub: "android_stubs_current",
178 systemStub: "android_system_stubs_current",
179 testStub: "android_test_stubs_current",
180 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000181 preparer: android.GroupFixturePreparers(),
Anton Hanssona2adc372020-07-03 15:31:32 +0100182 }, {
183 name: "testUnbundled",
184 unbundledBuild: true,
185 publicStub: "sdk_public_current_android",
186 systemStub: "sdk_system_current_android",
187 testStub: "sdk_test_current_android",
188 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000189 preparer: PrepareForTestWithPrebuiltsOfCurrentApi,
Anton Hanssona2adc372020-07-03 15:31:32 +0100190 },
191 }
192 for _, tc := range testCases {
193 t.Run(tc.name, func(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100194 result := android.GroupFixturePreparers(
195 hiddenApiFixtureFactory,
Paul Duffindc92abb2021-03-13 08:28:35 +0000196 tc.preparer,
Paul Duffin74431d52021-04-21 14:10:42 +0100197 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +0000198 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
199 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild)
200 }),
201 ).RunTest(t)
Anton Hanssona2adc372020-07-03 15:31:32 +0100202
Paul Duffin74431d52021-04-21 14:10:42 +0100203 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
204 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Anton Hanssona2adc372020-07-03 15:31:32 +0100205 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000206 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100207
208 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000209 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100210
211 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000212 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100213
Paul Duffin175947f2021-03-12 21:44:02 +0000214 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub)
215 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100216 })
217 }
218}
219
220func generateDexedPath(subDir, dex, module string) string {
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000221 return fmt.Sprintf("out/soong/.intermediates/%s/android_common/%s/%s.jar", subDir, dex, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100222}
223
Paul Duffin175947f2021-03-12 21:44:02 +0000224func generateDexPath(moduleDir string, module string) string {
225 return generateDexedPath(filepath.Join(moduleDir, module), "dex", module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100226}
227
228func generateSdkDexPath(module string, unbundled bool) string {
229 if unbundled {
230 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
231 }
Paul Duffin175947f2021-03-12 21:44:02 +0000232 return generateDexPath(defaultJavaDir, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100233}
Bill Peckhambae47492021-01-08 09:34:44 -0800234
235func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
236
237 // The idea behind this test is to ensure that when the build is
238 // confugured with a PrebuiltHiddenApiDir that the rules for the
239 // hiddenapi singleton copy the prebuilts to the typical output
240 // location, and then use that output location for the hiddenapi encode
241 // dex step.
242
243 // Where to find the prebuilt hiddenapi files:
244 prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
245
Paul Duffin79abe572021-03-29 02:16:14 +0100246 result := android.GroupFixturePreparers(
247 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100248 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +0000249 fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir),
250 ).RunTestWithBp(t, `
Bill Peckhambae47492021-01-08 09:34:44 -0800251 java_import {
252 name: "foo",
253 jars: ["a.jar"],
254 compile_dex: true,
255 }
Paul Duffin175947f2021-03-12 21:44:02 +0000256 `)
Bill Peckhambae47492021-01-08 09:34:44 -0800257
258 expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000259 expectedCpOutput := "out/soong/hiddenapi/hiddenapi-flags.csv"
260 expectedFlagsCsv := "out/soong/hiddenapi/hiddenapi-flags.csv"
Bill Peckhambae47492021-01-08 09:34:44 -0800261
Paul Duffin175947f2021-03-12 21:44:02 +0000262 foo := result.ModuleForTests("foo", "android_common")
Bill Peckhambae47492021-01-08 09:34:44 -0800263
Paul Duffin175947f2021-03-12 21:44:02 +0000264 hiddenAPI := result.SingletonForTests("hiddenapi")
Bill Peckhambae47492021-01-08 09:34:44 -0800265 cpRule := hiddenAPI.Rule("Cp")
266 actualCpInput := cpRule.BuildParams.Input
267 actualCpOutput := cpRule.BuildParams.Output
Paul Duffina71a67a2021-03-29 00:42:57 +0100268 encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
Bill Peckhambae47492021-01-08 09:34:44 -0800269 actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
270
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000271 android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput)
Bill Peckhambae47492021-01-08 09:34:44 -0800272
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000273 android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput)
Bill Peckhambae47492021-01-08 09:34:44 -0800274
Paul Duffin175947f2021-03-12 21:44:02 +0000275 android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv)
Bill Peckhambae47492021-01-08 09:34:44 -0800276}
Paul Duffin0d586582021-05-14 15:03:30 +0100277
278func TestHiddenAPIEncoding_JavaSdkLibrary(t *testing.T) {
279
280 result := android.GroupFixturePreparers(
281 hiddenApiFixtureFactory,
282 FixtureConfigureBootJars("platform:foo"),
283 PrepareForTestWithJavaSdkLibraryFiles,
284 FixtureWithLastReleaseApis("foo"),
285
286 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
287 // is disabled.
288 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
289 ).RunTestWithBp(t, `
290 java_sdk_library {
291 name: "foo",
292 srcs: ["a.java"],
293 shared_library: false,
294 compile_dex: true,
295 public: {enabled: true},
296 }
297 `)
298
299 checkDexEncoded := func(t *testing.T, name, unencodedDexJar, encodedDexJar string) {
300 moduleForTests := result.ModuleForTests(name, "android_common")
301
302 encodeDexRule := moduleForTests.Rule("hiddenAPIEncodeDex")
303 actualUnencodedDexJar := encodeDexRule.Input
304
305 // Make sure that the module has its dex jar encoded.
306 android.AssertStringEquals(t, "encode embedded java_library", unencodedDexJar, actualUnencodedDexJar.String())
307
308 // Make sure that the encoded dex jar is the exported one.
309 exportedDexJar := moduleForTests.Module().(UsesLibraryDependency).DexJarBuildPath()
310 android.AssertPathRelativeToTopEquals(t, "encode embedded java_library", encodedDexJar, exportedDexJar)
311 }
312
313 // The java_library embedded with the java_sdk_library must be dex encoded.
314 t.Run("foo", func(t *testing.T) {
315 expectedUnencodedDexJar := "out/soong/.intermediates/foo/android_common/aligned/foo.jar"
316 expectedEncodedDexJar := "out/soong/.intermediates/foo/android_common/hiddenapi/foo.jar"
317 checkDexEncoded(t, "foo", expectedUnencodedDexJar, expectedEncodedDexJar)
318 })
319
320 // The dex jar of the child implementation java_library of the java_sdk_library is not currently
321 // dex encoded.
322 t.Run("foo.impl", func(t *testing.T) {
323 fooImpl := result.ModuleForTests("foo.impl", "android_common")
324 encodeDexRule := fooImpl.MaybeRule("hiddenAPIEncodeDex")
325 if encodeDexRule.Rule != nil {
326 t.Errorf("foo.impl is not expected to be encoded")
327 }
328 })
329}