blob: 75b7bb7c848bde573f74893e19f567fb018910d6 [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"
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +010023
Anton Hanssona2adc372020-07-03 15:31:32 +010024 "github.com/google/blueprint/proptools"
Liz Kammer5ca3a622020-08-05 15:40:41 -070025)
26
Paul Duffin74431d52021-04-21 14:10:42 +010027// TODO(b/177892522): Move these tests into a more appropriate place.
28
Paul Duffin175947f2021-03-12 21:44:02 +000029func fixtureSetPrebuiltHiddenApiDirProductVariable(prebuiltHiddenApiDir *string) android.FixturePreparer {
30 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
31 variables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
32 })
Liz Kammer5ca3a622020-08-05 15:40:41 -070033}
34
Paul Duffin74431d52021-04-21 14:10:42 +010035var prepareForTestWithDefaultPlatformBootclasspath = android.FixtureAddTextFile("frameworks/base/boot/Android.bp", `
36 platform_bootclasspath {
37 name: "platform-bootclasspath",
38 }
39`)
40
Paul Duffin71ae5942021-03-22 15:36:52 +000041var hiddenApiFixtureFactory = android.GroupFixturePreparers(
42 prepareForJavaTest, PrepareForTestWithHiddenApiBuildComponents)
Liz Kammer5ca3a622020-08-05 15:40:41 -070043
44func TestHiddenAPISingleton(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +010045 result := android.GroupFixturePreparers(
46 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010047 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +010048 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +000049 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070050 java_library {
51 name: "foo",
52 srcs: ["a.java"],
53 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +000054 }
Paul Duffin175947f2021-03-12 21:44:02 +000055 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -070056
Paul Duffin74431d52021-04-21 14:10:42 +010057 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
58 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Paul Duffin2f9e71e2021-03-22 16:24:49 +000059 want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +000060 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -070061}
62
Paul Duffinec0fe172021-02-25 15:34:13 +000063func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +010064 expectedErrorMessage := "module prebuilt_foo{os:android,arch:common} does not provide a dex jar"
Paul Duffin175947f2021-03-12 21:44:02 +000065
Paul Duffin79abe572021-03-29 02:16:14 +010066 android.GroupFixturePreparers(
67 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010068 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +010069 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +000070 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)).
71 RunTestWithBp(t, `
Paul Duffinec0fe172021-02-25 15:34:13 +000072 java_library {
73 name: "foo",
74 srcs: ["a.java"],
75 compile_dex: true,
76 }
77
78 java_import {
79 name: "foo",
80 jars: ["a.jar"],
81 prefer: true,
82 }
Paul Duffin175947f2021-03-12 21:44:02 +000083 `)
Paul Duffinec0fe172021-02-25 15:34:13 +000084}
85
Liz Kammer5ca3a622020-08-05 15:40:41 -070086func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +010087 result := android.GroupFixturePreparers(
88 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010089 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +010090 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +000091 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070092 java_import {
93 name: "foo",
94 jars: ["a.jar"],
95 compile_dex: true,
96 }
Paul Duffin175947f2021-03-12 21:44:02 +000097 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -070098
Paul Duffin74431d52021-04-21 14:10:42 +010099 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
100 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000101 want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000102 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700103}
104
105func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100106 result := android.GroupFixturePreparers(
107 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100108 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +0100109 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +0000110 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700111 java_library {
112 name: "foo",
113 srcs: ["a.java"],
114 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000115 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700116
117 java_import {
118 name: "foo",
119 jars: ["a.jar"],
120 compile_dex: true,
121 prefer: false,
Paul Duffin01289a22021-02-04 17:49:33 +0000122 }
Paul Duffin175947f2021-03-12 21:44:02 +0000123 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700124
Paul Duffin74431d52021-04-21 14:10:42 +0100125 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
126 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000127 fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000128 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700129
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000130 prebuiltJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000131 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700132}
133
134func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100135 result := android.GroupFixturePreparers(
136 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100137 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +0100138 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +0000139 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700140 java_library {
141 name: "foo",
142 srcs: ["a.java"],
143 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000144 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700145
146 java_import {
147 name: "foo",
148 jars: ["a.jar"],
149 compile_dex: true,
150 prefer: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000151 }
Paul Duffin175947f2021-03-12 21:44:02 +0000152 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700153
Paul Duffin74431d52021-04-21 14:10:42 +0100154 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
155 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000156 prebuiltJarArg := "--boot-dex=out/soong/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000157 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700158
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000159 fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000160 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700161}
Anton Hanssona2adc372020-07-03 15:31:32 +0100162
163func TestHiddenAPISingletonSdks(t *testing.T) {
164 testCases := []struct {
165 name string
166 unbundledBuild bool
167 publicStub string
168 systemStub string
169 testStub string
170 corePlatformStub string
Paul Duffindc92abb2021-03-13 08:28:35 +0000171
172 // Additional test preparer
173 preparer android.FixturePreparer
Anton Hanssona2adc372020-07-03 15:31:32 +0100174 }{
175 {
176 name: "testBundled",
177 unbundledBuild: false,
178 publicStub: "android_stubs_current",
179 systemStub: "android_system_stubs_current",
180 testStub: "android_test_stubs_current",
181 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000182 preparer: android.GroupFixturePreparers(),
Anton Hanssona2adc372020-07-03 15:31:32 +0100183 }, {
184 name: "testUnbundled",
185 unbundledBuild: true,
186 publicStub: "sdk_public_current_android",
187 systemStub: "sdk_system_current_android",
188 testStub: "sdk_test_current_android",
189 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000190 preparer: PrepareForTestWithPrebuiltsOfCurrentApi,
Anton Hanssona2adc372020-07-03 15:31:32 +0100191 },
192 }
193 for _, tc := range testCases {
194 t.Run(tc.name, func(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100195 result := android.GroupFixturePreparers(
196 hiddenApiFixtureFactory,
Paul Duffindc92abb2021-03-13 08:28:35 +0000197 tc.preparer,
Paul Duffin74431d52021-04-21 14:10:42 +0100198 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +0000199 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
200 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild)
201 }),
202 ).RunTest(t)
Anton Hanssona2adc372020-07-03 15:31:32 +0100203
Paul Duffin74431d52021-04-21 14:10:42 +0100204 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
205 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Anton Hanssona2adc372020-07-03 15:31:32 +0100206 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000207 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100208
209 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000210 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100211
212 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000213 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100214
Paul Duffin175947f2021-03-12 21:44:02 +0000215 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub)
216 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100217 })
218 }
219}
220
221func generateDexedPath(subDir, dex, module string) string {
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000222 return fmt.Sprintf("out/soong/.intermediates/%s/android_common/%s/%s.jar", subDir, dex, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100223}
224
Paul Duffin175947f2021-03-12 21:44:02 +0000225func generateDexPath(moduleDir string, module string) string {
226 return generateDexedPath(filepath.Join(moduleDir, module), "dex", module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100227}
228
229func generateSdkDexPath(module string, unbundled bool) string {
230 if unbundled {
231 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
232 }
Paul Duffin175947f2021-03-12 21:44:02 +0000233 return generateDexPath(defaultJavaDir, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100234}
Bill Peckhambae47492021-01-08 09:34:44 -0800235
236func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
237
238 // The idea behind this test is to ensure that when the build is
239 // confugured with a PrebuiltHiddenApiDir that the rules for the
240 // hiddenapi singleton copy the prebuilts to the typical output
241 // location, and then use that output location for the hiddenapi encode
242 // dex step.
243
244 // Where to find the prebuilt hiddenapi files:
245 prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
246
Paul Duffin79abe572021-03-29 02:16:14 +0100247 result := android.GroupFixturePreparers(
248 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100249 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +0000250 fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir),
251 ).RunTestWithBp(t, `
Bill Peckhambae47492021-01-08 09:34:44 -0800252 java_import {
253 name: "foo",
254 jars: ["a.jar"],
255 compile_dex: true,
256 }
Paul Duffin175947f2021-03-12 21:44:02 +0000257 `)
Bill Peckhambae47492021-01-08 09:34:44 -0800258
259 expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000260 expectedCpOutput := "out/soong/hiddenapi/hiddenapi-flags.csv"
261 expectedFlagsCsv := "out/soong/hiddenapi/hiddenapi-flags.csv"
Bill Peckhambae47492021-01-08 09:34:44 -0800262
Paul Duffin175947f2021-03-12 21:44:02 +0000263 foo := result.ModuleForTests("foo", "android_common")
Bill Peckhambae47492021-01-08 09:34:44 -0800264
Paul Duffin175947f2021-03-12 21:44:02 +0000265 hiddenAPI := result.SingletonForTests("hiddenapi")
Bill Peckhambae47492021-01-08 09:34:44 -0800266 cpRule := hiddenAPI.Rule("Cp")
267 actualCpInput := cpRule.BuildParams.Input
268 actualCpOutput := cpRule.BuildParams.Output
Paul Duffina71a67a2021-03-29 00:42:57 +0100269 encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
Bill Peckhambae47492021-01-08 09:34:44 -0800270 actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
271
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000272 android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput)
Bill Peckhambae47492021-01-08 09:34:44 -0800273
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000274 android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput)
Bill Peckhambae47492021-01-08 09:34:44 -0800275
Paul Duffin175947f2021-03-12 21:44:02 +0000276 android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv)
Bill Peckhambae47492021-01-08 09:34:44 -0800277}
Paul Duffin0d586582021-05-14 15:03:30 +0100278
279func TestHiddenAPIEncoding_JavaSdkLibrary(t *testing.T) {
280
281 result := android.GroupFixturePreparers(
282 hiddenApiFixtureFactory,
283 FixtureConfigureBootJars("platform:foo"),
284 PrepareForTestWithJavaSdkLibraryFiles,
285 FixtureWithLastReleaseApis("foo"),
286
287 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
288 // is disabled.
289 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
290 ).RunTestWithBp(t, `
291 java_sdk_library {
292 name: "foo",
293 srcs: ["a.java"],
294 shared_library: false,
295 compile_dex: true,
296 public: {enabled: true},
297 }
298 `)
299
300 checkDexEncoded := func(t *testing.T, name, unencodedDexJar, encodedDexJar string) {
301 moduleForTests := result.ModuleForTests(name, "android_common")
302
303 encodeDexRule := moduleForTests.Rule("hiddenAPIEncodeDex")
304 actualUnencodedDexJar := encodeDexRule.Input
305
306 // Make sure that the module has its dex jar encoded.
307 android.AssertStringEquals(t, "encode embedded java_library", unencodedDexJar, actualUnencodedDexJar.String())
308
309 // Make sure that the encoded dex jar is the exported one.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100310 exportedDexJar := moduleForTests.Module().(UsesLibraryDependency).DexJarBuildPath().Path()
Paul Duffin0d586582021-05-14 15:03:30 +0100311 android.AssertPathRelativeToTopEquals(t, "encode embedded java_library", encodedDexJar, exportedDexJar)
312 }
313
314 // The java_library embedded with the java_sdk_library must be dex encoded.
315 t.Run("foo", func(t *testing.T) {
316 expectedUnencodedDexJar := "out/soong/.intermediates/foo/android_common/aligned/foo.jar"
317 expectedEncodedDexJar := "out/soong/.intermediates/foo/android_common/hiddenapi/foo.jar"
318 checkDexEncoded(t, "foo", expectedUnencodedDexJar, expectedEncodedDexJar)
319 })
320
321 // The dex jar of the child implementation java_library of the java_sdk_library is not currently
322 // dex encoded.
323 t.Run("foo.impl", func(t *testing.T) {
324 fooImpl := result.ModuleForTests("foo.impl", "android_common")
325 encodeDexRule := fooImpl.MaybeRule("hiddenAPIEncodeDex")
326 if encodeDexRule.Rule != nil {
327 t.Errorf("foo.impl is not expected to be encoded")
328 }
329 })
330}