blob: 37afe297c8019a6a431e3bd8cf8f2952b45b4860 [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(
Jiakai Zhangb95998b2023-05-11 16:39:27 +010042 PrepareForTestWithJavaDefaultModules,
43 PrepareForTestWithHiddenApiBuildComponents,
44)
Liz Kammer5ca3a622020-08-05 15:40:41 -070045
46func TestHiddenAPISingleton(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +010047 result := android.GroupFixturePreparers(
48 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010049 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +010050 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +000051 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070052 java_library {
53 name: "foo",
54 srcs: ["a.java"],
55 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +000056 }
Paul Duffin175947f2021-03-12 21:44:02 +000057 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -070058
Paul Duffin74431d52021-04-21 14:10:42 +010059 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
60 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Paul Duffin2f9e71e2021-03-22 16:24:49 +000061 want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +000062 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -070063}
64
Paul Duffinec0fe172021-02-25 15:34:13 +000065func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +010066 expectedErrorMessage := "module prebuilt_foo{os:android,arch:common} does not provide a dex jar"
Paul Duffin175947f2021-03-12 21:44:02 +000067
Paul Duffin79abe572021-03-29 02:16:14 +010068 android.GroupFixturePreparers(
69 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010070 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +010071 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +000072 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)).
73 RunTestWithBp(t, `
Paul Duffinec0fe172021-02-25 15:34:13 +000074 java_library {
75 name: "foo",
76 srcs: ["a.java"],
77 compile_dex: true,
78 }
79
80 java_import {
81 name: "foo",
82 jars: ["a.jar"],
83 prefer: true,
84 }
Paul Duffin175947f2021-03-12 21:44:02 +000085 `)
Paul Duffinec0fe172021-02-25 15:34:13 +000086}
87
Liz Kammer5ca3a622020-08-05 15:40:41 -070088func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +010089 result := android.GroupFixturePreparers(
90 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010091 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +010092 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +000093 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070094 java_import {
95 name: "foo",
96 jars: ["a.jar"],
97 compile_dex: true,
98 }
Paul Duffin175947f2021-03-12 21:44:02 +000099 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700100
Paul Duffin74431d52021-04-21 14:10:42 +0100101 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
102 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000103 want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000104 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700105}
106
107func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100108 result := android.GroupFixturePreparers(
109 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100110 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +0100111 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +0000112 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700113 java_library {
114 name: "foo",
115 srcs: ["a.java"],
116 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000117 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700118
119 java_import {
120 name: "foo",
121 jars: ["a.jar"],
122 compile_dex: true,
123 prefer: false,
Paul Duffin01289a22021-02-04 17:49:33 +0000124 }
Paul Duffin175947f2021-03-12 21:44:02 +0000125 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700126
Paul Duffin74431d52021-04-21 14:10:42 +0100127 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
128 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000129 fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000130 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700131
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000132 prebuiltJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000133 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700134}
135
136func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100137 result := android.GroupFixturePreparers(
138 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100139 FixtureConfigureBootJars("platform:foo"),
Paul Duffin74431d52021-04-21 14:10:42 +0100140 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +0000141 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700142 java_library {
143 name: "foo",
144 srcs: ["a.java"],
145 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000146 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700147
148 java_import {
149 name: "foo",
150 jars: ["a.jar"],
151 compile_dex: true,
152 prefer: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000153 }
Paul Duffin175947f2021-03-12 21:44:02 +0000154 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700155
Paul Duffin74431d52021-04-21 14:10:42 +0100156 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
157 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000158 prebuiltJarArg := "--boot-dex=out/soong/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000159 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700160
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000161 fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000162 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700163}
Anton Hanssona2adc372020-07-03 15:31:32 +0100164
165func TestHiddenAPISingletonSdks(t *testing.T) {
166 testCases := []struct {
167 name string
168 unbundledBuild bool
169 publicStub string
170 systemStub string
171 testStub string
172 corePlatformStub string
Paul Duffindc92abb2021-03-13 08:28:35 +0000173
174 // Additional test preparer
175 preparer android.FixturePreparer
Anton Hanssona2adc372020-07-03 15:31:32 +0100176 }{
177 {
178 name: "testBundled",
179 unbundledBuild: false,
180 publicStub: "android_stubs_current",
181 systemStub: "android_system_stubs_current",
182 testStub: "android_test_stubs_current",
183 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000184 preparer: android.GroupFixturePreparers(),
Anton Hanssona2adc372020-07-03 15:31:32 +0100185 }, {
186 name: "testUnbundled",
187 unbundledBuild: true,
188 publicStub: "sdk_public_current_android",
189 systemStub: "sdk_system_current_android",
190 testStub: "sdk_test_current_android",
191 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000192 preparer: PrepareForTestWithPrebuiltsOfCurrentApi,
Anton Hanssona2adc372020-07-03 15:31:32 +0100193 },
194 }
195 for _, tc := range testCases {
196 t.Run(tc.name, func(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100197 result := android.GroupFixturePreparers(
198 hiddenApiFixtureFactory,
Paul Duffindc92abb2021-03-13 08:28:35 +0000199 tc.preparer,
Paul Duffin74431d52021-04-21 14:10:42 +0100200 prepareForTestWithDefaultPlatformBootclasspath,
Paul Duffin175947f2021-03-12 21:44:02 +0000201 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
202 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild)
203 }),
204 ).RunTest(t)
Anton Hanssona2adc372020-07-03 15:31:32 +0100205
Paul Duffin74431d52021-04-21 14:10:42 +0100206 hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
207 hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
Anton Hanssona2adc372020-07-03 15:31:32 +0100208 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000209 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100210
211 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000212 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100213
214 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000215 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100216
Paul Duffin175947f2021-03-12 21:44:02 +0000217 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub)
218 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100219 })
220 }
221}
222
223func generateDexedPath(subDir, dex, module string) string {
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000224 return fmt.Sprintf("out/soong/.intermediates/%s/android_common/%s/%s.jar", subDir, dex, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100225}
226
Paul Duffin175947f2021-03-12 21:44:02 +0000227func generateDexPath(moduleDir string, module string) string {
228 return generateDexedPath(filepath.Join(moduleDir, module), "dex", module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100229}
230
231func generateSdkDexPath(module string, unbundled bool) string {
232 if unbundled {
233 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
234 }
Paul Duffin175947f2021-03-12 21:44:02 +0000235 return generateDexPath(defaultJavaDir, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100236}
Bill Peckhambae47492021-01-08 09:34:44 -0800237
238func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
239
240 // The idea behind this test is to ensure that when the build is
241 // confugured with a PrebuiltHiddenApiDir that the rules for the
242 // hiddenapi singleton copy the prebuilts to the typical output
243 // location, and then use that output location for the hiddenapi encode
244 // dex step.
245
246 // Where to find the prebuilt hiddenapi files:
247 prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
248
Paul Duffin79abe572021-03-29 02:16:14 +0100249 result := android.GroupFixturePreparers(
250 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100251 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +0000252 fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir),
253 ).RunTestWithBp(t, `
Bill Peckhambae47492021-01-08 09:34:44 -0800254 java_import {
255 name: "foo",
256 jars: ["a.jar"],
257 compile_dex: true,
258 }
Paul Duffin175947f2021-03-12 21:44:02 +0000259 `)
Bill Peckhambae47492021-01-08 09:34:44 -0800260
261 expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000262 expectedCpOutput := "out/soong/hiddenapi/hiddenapi-flags.csv"
263 expectedFlagsCsv := "out/soong/hiddenapi/hiddenapi-flags.csv"
Bill Peckhambae47492021-01-08 09:34:44 -0800264
Paul Duffin175947f2021-03-12 21:44:02 +0000265 foo := result.ModuleForTests("foo", "android_common")
Bill Peckhambae47492021-01-08 09:34:44 -0800266
Paul Duffin175947f2021-03-12 21:44:02 +0000267 hiddenAPI := result.SingletonForTests("hiddenapi")
Bill Peckhambae47492021-01-08 09:34:44 -0800268 cpRule := hiddenAPI.Rule("Cp")
269 actualCpInput := cpRule.BuildParams.Input
270 actualCpOutput := cpRule.BuildParams.Output
Paul Duffina71a67a2021-03-29 00:42:57 +0100271 encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
Bill Peckhambae47492021-01-08 09:34:44 -0800272 actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
273
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000274 android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput)
Bill Peckhambae47492021-01-08 09:34:44 -0800275
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000276 android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput)
Bill Peckhambae47492021-01-08 09:34:44 -0800277
Paul Duffin175947f2021-03-12 21:44:02 +0000278 android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv)
Bill Peckhambae47492021-01-08 09:34:44 -0800279}
Paul Duffin0d586582021-05-14 15:03:30 +0100280
281func TestHiddenAPIEncoding_JavaSdkLibrary(t *testing.T) {
282
283 result := android.GroupFixturePreparers(
284 hiddenApiFixtureFactory,
285 FixtureConfigureBootJars("platform:foo"),
286 PrepareForTestWithJavaSdkLibraryFiles,
287 FixtureWithLastReleaseApis("foo"),
288
289 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
290 // is disabled.
291 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
292 ).RunTestWithBp(t, `
293 java_sdk_library {
294 name: "foo",
295 srcs: ["a.java"],
296 shared_library: false,
297 compile_dex: true,
298 public: {enabled: true},
299 }
300 `)
301
302 checkDexEncoded := func(t *testing.T, name, unencodedDexJar, encodedDexJar string) {
303 moduleForTests := result.ModuleForTests(name, "android_common")
304
305 encodeDexRule := moduleForTests.Rule("hiddenAPIEncodeDex")
306 actualUnencodedDexJar := encodeDexRule.Input
307
308 // Make sure that the module has its dex jar encoded.
309 android.AssertStringEquals(t, "encode embedded java_library", unencodedDexJar, actualUnencodedDexJar.String())
310
311 // Make sure that the encoded dex jar is the exported one.
Spandan Das59a4a2b2024-01-09 21:35:56 +0000312 errCtx := moduleErrorfTestCtx{}
313 exportedDexJar := moduleForTests.Module().(UsesLibraryDependency).DexJarBuildPath(errCtx).Path()
Paul Duffin0d586582021-05-14 15:03:30 +0100314 android.AssertPathRelativeToTopEquals(t, "encode embedded java_library", encodedDexJar, exportedDexJar)
315 }
316
317 // The java_library embedded with the java_sdk_library must be dex encoded.
318 t.Run("foo", func(t *testing.T) {
319 expectedUnencodedDexJar := "out/soong/.intermediates/foo/android_common/aligned/foo.jar"
320 expectedEncodedDexJar := "out/soong/.intermediates/foo/android_common/hiddenapi/foo.jar"
321 checkDexEncoded(t, "foo", expectedUnencodedDexJar, expectedEncodedDexJar)
322 })
323
324 // The dex jar of the child implementation java_library of the java_sdk_library is not currently
325 // dex encoded.
326 t.Run("foo.impl", func(t *testing.T) {
327 fooImpl := result.ModuleForTests("foo.impl", "android_common")
328 encodeDexRule := fooImpl.MaybeRule("hiddenAPIEncodeDex")
329 if encodeDexRule.Rule != nil {
330 t.Errorf("foo.impl is not expected to be encoded")
331 }
332 })
333}