blob: f95c83fe75a99af2dff86703722205d9aa1148c3 [file] [log] [blame]
Paul Duffin3451e162021-01-20 15:16:56 +00001// Copyright (C) 2021 The Android Open Source Project
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 (
Paul Duffin3f1ae0b2022-07-27 16:27:42 +000018 "strings"
Paul Duffin3451e162021-01-20 15:16:56 +000019 "testing"
Paul Duffin837486d2021-03-23 23:13:53 +000020
21 "android/soong/android"
22 "android/soong/dexpreopt"
Paul Duffin3451e162021-01-20 15:16:56 +000023)
24
Paul Duffin7771eba2021-04-23 14:25:28 +010025// Contains some simple tests for bootclasspath_fragment logic, additional tests can be found in
26// apex/bootclasspath_fragment_test.go as the ART boot image requires modules from the ART apex.
Paul Duffin3451e162021-01-20 15:16:56 +000027
Paul Duffin7771eba2021-04-23 14:25:28 +010028var prepareForTestWithBootclasspathFragment = android.GroupFixturePreparers(
Paul Duffin837486d2021-03-23 23:13:53 +000029 PrepareForTestWithJavaDefaultModules,
30 dexpreopt.PrepareForTestByEnablingDexpreopt,
31)
32
Paul Duffin8018e502021-05-21 19:28:09 +010033func TestBootclasspathFragment_UnknownImageName(t *testing.T) {
Paul Duffin7771eba2021-04-23 14:25:28 +010034 prepareForTestWithBootclasspathFragment.
Paul Duffin837486d2021-03-23 23:13:53 +000035 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
Paul Duffin8018e502021-05-21 19:28:09 +010036 `\Qimage_name: unknown image name "unknown", expected "art"\E`)).
Paul Duffin837486d2021-03-23 23:13:53 +000037 RunTestWithBp(t, `
Paul Duffin7771eba2021-04-23 14:25:28 +010038 bootclasspath_fragment {
39 name: "unknown-bootclasspath-fragment",
Paul Duffin837486d2021-03-23 23:13:53 +000040 image_name: "unknown",
Paul Duffin8018e502021-05-21 19:28:09 +010041 contents: ["foo"],
Paul Duffin837486d2021-03-23 23:13:53 +000042 }
43 `)
Paul Duffin3451e162021-01-20 15:16:56 +000044}
Paul Duffinf7f65da2021-03-10 15:00:46 +000045
Paul Duffin8018e502021-05-21 19:28:09 +010046func TestPrebuiltBootclasspathFragment_UnknownImageName(t *testing.T) {
Paul Duffin7771eba2021-04-23 14:25:28 +010047 prepareForTestWithBootclasspathFragment.
Paul Duffin4b64ba02021-03-29 11:02:53 +010048 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
Paul Duffin8018e502021-05-21 19:28:09 +010049 `\Qimage_name: unknown image name "unknown", expected "art"\E`)).
Paul Duffin837486d2021-03-23 23:13:53 +000050 RunTestWithBp(t, `
Paul Duffin7771eba2021-04-23 14:25:28 +010051 prebuilt_bootclasspath_fragment {
52 name: "unknown-bootclasspath-fragment",
Paul Duffin837486d2021-03-23 23:13:53 +000053 image_name: "unknown",
Paul Duffin8018e502021-05-21 19:28:09 +010054 contents: ["foo"],
Paul Duffin837486d2021-03-23 23:13:53 +000055 }
56 `)
Paul Duffinf7f65da2021-03-10 15:00:46 +000057}
Paul Duffinc7ef9892021-03-23 23:21:59 +000058
Paul Duffin7771eba2021-04-23 14:25:28 +010059func TestBootclasspathFragmentInconsistentArtConfiguration_Platform(t *testing.T) {
Paul Duffinc7ef9892021-03-23 23:21:59 +000060 android.GroupFixturePreparers(
Paul Duffin7771eba2021-04-23 14:25:28 +010061 prepareForTestWithBootclasspathFragment,
Paul Duffinc7ef9892021-03-23 23:21:59 +000062 dexpreopt.FixtureSetArtBootJars("platform:foo", "apex:bar"),
63 ).
64 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
65 `\QArtApexJars is invalid as it requests a platform variant of "foo"\E`)).
66 RunTestWithBp(t, `
Paul Duffin7771eba2021-04-23 14:25:28 +010067 bootclasspath_fragment {
68 name: "bootclasspath-fragment",
Paul Duffinc7ef9892021-03-23 23:21:59 +000069 image_name: "art",
Paul Duffin8018e502021-05-21 19:28:09 +010070 contents: ["foo", "bar"],
Paul Duffinc7ef9892021-03-23 23:21:59 +000071 apex_available: [
72 "apex",
73 ],
74 }
75 `)
76}
77
Paul Duffin7771eba2021-04-23 14:25:28 +010078func TestBootclasspathFragmentInconsistentArtConfiguration_ApexMixture(t *testing.T) {
Paul Duffinc7ef9892021-03-23 23:21:59 +000079 android.GroupFixturePreparers(
Paul Duffin7771eba2021-04-23 14:25:28 +010080 prepareForTestWithBootclasspathFragment,
Paul Duffinc7ef9892021-03-23 23:21:59 +000081 dexpreopt.FixtureSetArtBootJars("apex1:foo", "apex2:bar"),
82 ).
83 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
84 `\QArtApexJars configuration is inconsistent, expected all jars to be in the same apex but it specifies apex "apex1" and "apex2"\E`)).
85 RunTestWithBp(t, `
Paul Duffin7771eba2021-04-23 14:25:28 +010086 bootclasspath_fragment {
87 name: "bootclasspath-fragment",
Paul Duffinc7ef9892021-03-23 23:21:59 +000088 image_name: "art",
Paul Duffin8018e502021-05-21 19:28:09 +010089 contents: ["foo", "bar"],
Paul Duffinc7ef9892021-03-23 23:21:59 +000090 apex_available: [
91 "apex1",
92 "apex2",
93 ],
94 }
95 `)
96}
Paul Duffin82886d62021-03-24 01:34:57 +000097
Paul Duffinc7d16442021-04-23 13:55:49 +010098func TestBootclasspathFragment_Coverage(t *testing.T) {
99 prepareForTestWithFrameworkCoverage := android.FixtureMergeEnv(map[string]string{
100 "EMMA_INSTRUMENT": "true",
101 "EMMA_INSTRUMENT_FRAMEWORK": "true",
102 })
103
104 prepareWithBp := android.FixtureWithRootAndroidBp(`
105 bootclasspath_fragment {
106 name: "myfragment",
107 contents: [
108 "mybootlib",
109 ],
Paul Duffin895c7142021-04-25 13:40:15 +0100110 api: {
111 stub_libs: [
112 "mysdklibrary",
113 ],
114 },
Paul Duffinc7d16442021-04-23 13:55:49 +0100115 coverage: {
116 contents: [
117 "coveragelib",
118 ],
Paul Duffin895c7142021-04-25 13:40:15 +0100119 api: {
120 stub_libs: [
121 "mycoveragestubs",
122 ],
123 },
Paul Duffinc7d16442021-04-23 13:55:49 +0100124 },
Paul Duffin9fd56472022-03-31 15:42:30 +0100125 hidden_api: {
126 split_packages: ["*"],
127 },
Paul Duffinc7d16442021-04-23 13:55:49 +0100128 }
129
130 java_library {
131 name: "mybootlib",
132 srcs: ["Test.java"],
133 system_modules: "none",
134 sdk_version: "none",
135 compile_dex: true,
136 }
137
138 java_library {
139 name: "coveragelib",
140 srcs: ["Test.java"],
141 system_modules: "none",
142 sdk_version: "none",
143 compile_dex: true,
144 }
Paul Duffin895c7142021-04-25 13:40:15 +0100145
146 java_sdk_library {
147 name: "mysdklibrary",
148 srcs: ["Test.java"],
149 compile_dex: true,
150 public: {enabled: true},
151 system: {enabled: true},
152 }
153
154 java_sdk_library {
155 name: "mycoveragestubs",
156 srcs: ["Test.java"],
157 compile_dex: true,
158 public: {enabled: true},
159 }
Paul Duffinc7d16442021-04-23 13:55:49 +0100160 `)
161
162 checkContents := func(t *testing.T, result *android.TestResult, expected ...string) {
163 module := result.Module("myfragment", "android_common").(*BootclasspathFragmentModule)
164 android.AssertArrayString(t, "contents property", expected, module.properties.Contents)
165 }
166
Paul Duffin895c7142021-04-25 13:40:15 +0100167 preparer := android.GroupFixturePreparers(
168 prepareForTestWithBootclasspathFragment,
169 PrepareForTestWithJavaSdkLibraryFiles,
170 FixtureWithLastReleaseApis("mysdklibrary", "mycoveragestubs"),
satayevabcd5972021-08-06 17:49:46 +0100171 FixtureConfigureApexBootJars("someapex:mybootlib"),
Paul Duffin895c7142021-04-25 13:40:15 +0100172 prepareWithBp,
173 )
174
Paul Duffinc7d16442021-04-23 13:55:49 +0100175 t.Run("without coverage", func(t *testing.T) {
Paul Duffin895c7142021-04-25 13:40:15 +0100176 result := preparer.RunTest(t)
Paul Duffinc7d16442021-04-23 13:55:49 +0100177 checkContents(t, result, "mybootlib")
178 })
179
180 t.Run("with coverage", func(t *testing.T) {
181 result := android.GroupFixturePreparers(
Paul Duffinc7d16442021-04-23 13:55:49 +0100182 prepareForTestWithFrameworkCoverage,
Paul Duffin895c7142021-04-25 13:40:15 +0100183 preparer,
Paul Duffinc7d16442021-04-23 13:55:49 +0100184 ).RunTest(t)
185 checkContents(t, result, "mybootlib", "coveragelib")
186 })
187}
Paul Duffin10931582021-04-25 10:13:54 +0100188
189func TestBootclasspathFragment_StubLibs(t *testing.T) {
190 result := android.GroupFixturePreparers(
191 prepareForTestWithBootclasspathFragment,
192 PrepareForTestWithJavaSdkLibraryFiles,
Paul Duffin34827d42021-05-13 21:25:05 +0100193 FixtureWithLastReleaseApis("mysdklibrary", "myothersdklibrary", "mycoreplatform"),
satayevabcd5972021-08-06 17:49:46 +0100194 FixtureConfigureApexBootJars("someapex:mysdklibrary"),
Paul Duffin10931582021-04-25 10:13:54 +0100195 ).RunTestWithBp(t, `
196 bootclasspath_fragment {
197 name: "myfragment",
198 contents: ["mysdklibrary"],
199 api: {
200 stub_libs: [
201 "mystublib",
Paul Duffin34827d42021-05-13 21:25:05 +0100202 "myothersdklibrary",
Paul Duffin10931582021-04-25 10:13:54 +0100203 ],
204 },
205 core_platform_api: {
Paul Duffin3f0290e2021-06-30 18:25:36 +0100206 stub_libs: ["mycoreplatform.stubs"],
Paul Duffin10931582021-04-25 10:13:54 +0100207 },
Paul Duffin9fd56472022-03-31 15:42:30 +0100208 hidden_api: {
209 split_packages: ["*"],
210 },
Paul Duffin10931582021-04-25 10:13:54 +0100211 }
212
213 java_library {
214 name: "mystublib",
215 srcs: ["Test.java"],
216 system_modules: "none",
217 sdk_version: "none",
218 compile_dex: true,
219 }
220
221 java_sdk_library {
222 name: "mysdklibrary",
223 srcs: ["a.java"],
Paul Duffinf4600f62021-05-13 22:34:45 +0100224 shared_library: false,
Paul Duffin10931582021-04-25 10:13:54 +0100225 public: {enabled: true},
226 system: {enabled: true},
227 }
228
229 java_sdk_library {
Paul Duffin34827d42021-05-13 21:25:05 +0100230 name: "myothersdklibrary",
231 srcs: ["a.java"],
232 shared_library: false,
233 public: {enabled: true},
234 }
235
236 java_sdk_library {
Paul Duffin10931582021-04-25 10:13:54 +0100237 name: "mycoreplatform",
238 srcs: ["a.java"],
Paul Duffinf4600f62021-05-13 22:34:45 +0100239 shared_library: false,
Paul Duffin10931582021-04-25 10:13:54 +0100240 public: {enabled: true},
241 }
242 `)
243
244 fragment := result.Module("myfragment", "android_common")
Paul Duffin18cf1972021-05-21 22:46:59 +0100245 info := result.ModuleProvider(fragment, HiddenAPIInfoProvider).(HiddenAPIInfo)
Paul Duffin10931582021-04-25 10:13:54 +0100246
247 stubsJar := "out/soong/.intermediates/mystublib/android_common/dex/mystublib.jar"
248
Paul Duffin34827d42021-05-13 21:25:05 +0100249 // Stubs jars for mysdklibrary
Paul Duffin10931582021-04-25 10:13:54 +0100250 publicStubsJar := "out/soong/.intermediates/mysdklibrary.stubs/android_common/dex/mysdklibrary.stubs.jar"
Paul Duffin10931582021-04-25 10:13:54 +0100251 systemStubsJar := "out/soong/.intermediates/mysdklibrary.stubs.system/android_common/dex/mysdklibrary.stubs.system.jar"
Paul Duffin10931582021-04-25 10:13:54 +0100252
Paul Duffin34827d42021-05-13 21:25:05 +0100253 // Stubs jars for myothersdklibrary
254 otherPublicStubsJar := "out/soong/.intermediates/myothersdklibrary.stubs/android_common/dex/myothersdklibrary.stubs.jar"
255
256 // Check that SdkPublic uses public stubs for all sdk libraries.
Paul Duffin280a31a2021-06-27 20:28:29 +0100257 android.AssertPathsRelativeToTopEquals(t, "public dex stubs jar", []string{otherPublicStubsJar, publicStubsJar, stubsJar}, info.TransitiveStubDexJarsByScope.StubDexJarsForScope(PublicHiddenAPIScope))
Paul Duffin34827d42021-05-13 21:25:05 +0100258
259 // Check that SdkSystem uses system stubs for mysdklibrary and public stubs for myothersdklibrary
260 // as it does not provide system stubs.
Paul Duffin280a31a2021-06-27 20:28:29 +0100261 android.AssertPathsRelativeToTopEquals(t, "system dex stubs jar", []string{otherPublicStubsJar, systemStubsJar, stubsJar}, info.TransitiveStubDexJarsByScope.StubDexJarsForScope(SystemHiddenAPIScope))
Paul Duffin34827d42021-05-13 21:25:05 +0100262
263 // Check that SdkTest also uses system stubs for mysdklibrary as it does not provide test stubs
264 // and public stubs for myothersdklibrary as it does not provide test stubs either.
Paul Duffin280a31a2021-06-27 20:28:29 +0100265 android.AssertPathsRelativeToTopEquals(t, "test dex stubs jar", []string{otherPublicStubsJar, systemStubsJar, stubsJar}, info.TransitiveStubDexJarsByScope.StubDexJarsForScope(TestHiddenAPIScope))
Paul Duffin10931582021-04-25 10:13:54 +0100266
267 // Check that SdkCorePlatform uses public stubs from the mycoreplatform library.
268 corePlatformStubsJar := "out/soong/.intermediates/mycoreplatform.stubs/android_common/dex/mycoreplatform.stubs.jar"
Paul Duffin280a31a2021-06-27 20:28:29 +0100269 android.AssertPathsRelativeToTopEquals(t, "core platform dex stubs jar", []string{corePlatformStubsJar}, info.TransitiveStubDexJarsByScope.StubDexJarsForScope(CorePlatformHiddenAPIScope))
Paul Duffind2b1e0c2021-06-27 20:53:39 +0100270
Paul Duffin280a31a2021-06-27 20:28:29 +0100271 // Check the widest stubs.. The list contains the widest stub dex jar provided by each module.
272 expectedWidestPaths := []string{
273 // mycoreplatform's widest API is core platform.
274 corePlatformStubsJar,
275
276 // myothersdklibrary's widest API is public.
277 otherPublicStubsJar,
278
279 // sdklibrary's widest API is system.
280 systemStubsJar,
281
282 // mystublib's only provides one API and so it must be the widest.
283 stubsJar,
284 }
285
286 android.AssertPathsRelativeToTopEquals(t, "widest dex stubs jar", expectedWidestPaths, info.TransitiveStubDexJarsByScope.StubDexJarsForWidestAPIScope())
Paul Duffin10931582021-04-25 10:13:54 +0100287}
Paul Duffinc15b9e92022-03-31 15:42:30 +0100288
Paul Duffin3f1ae0b2022-07-27 16:27:42 +0000289func TestSnapshotWithBootclasspathFragment_HiddenAPI(t *testing.T) {
290 result := android.GroupFixturePreparers(
291 prepareForTestWithBootclasspathFragment,
292 PrepareForTestWithJavaSdkLibraryFiles,
293 FixtureWithLastReleaseApis("mysdklibrary", "mynewlibrary"),
294 FixtureConfigureApexBootJars("myapex:mybootlib", "myapex:mynewlibrary"),
295 android.MockFS{
296 "my-blocked.txt": nil,
297 "my-max-target-o-low-priority.txt": nil,
298 "my-max-target-p.txt": nil,
299 "my-max-target-q.txt": nil,
300 "my-max-target-r-low-priority.txt": nil,
301 "my-removed.txt": nil,
302 "my-unsupported-packages.txt": nil,
303 "my-unsupported.txt": nil,
304 "my-new-max-target-q.txt": nil,
305 }.AddToFixture(),
306 android.FixtureWithRootAndroidBp(`
307 bootclasspath_fragment {
308 name: "mybootclasspathfragment",
309 apex_available: ["myapex"],
310 contents: ["mybootlib", "mynewlibrary"],
311 hidden_api: {
312 unsupported: [
313 "my-unsupported.txt",
314 ],
315 removed: [
316 "my-removed.txt",
317 ],
318 max_target_r_low_priority: [
319 "my-max-target-r-low-priority.txt",
320 ],
321 max_target_q: [
322 "my-max-target-q.txt",
323 ],
324 max_target_p: [
325 "my-max-target-p.txt",
326 ],
327 max_target_o_low_priority: [
328 "my-max-target-o-low-priority.txt",
329 ],
330 blocked: [
331 "my-blocked.txt",
332 ],
333 unsupported_packages: [
334 "my-unsupported-packages.txt",
335 ],
336 split_packages: ["sdklibrary"],
337 package_prefixes: ["sdklibrary.all.mine"],
338 single_packages: ["sdklibrary.mine"],
339 },
340 }
341
342 java_library {
343 name: "mybootlib",
344 apex_available: ["myapex"],
345 srcs: ["Test.java"],
346 system_modules: "none",
347 sdk_version: "none",
348 min_sdk_version: "1",
349 compile_dex: true,
350 permitted_packages: ["mybootlib"],
351 }
352
353 java_sdk_library {
354 name: "mynewlibrary",
355 apex_available: ["myapex"],
356 srcs: ["Test.java"],
357 min_sdk_version: "10",
358 compile_dex: true,
359 public: {enabled: true},
360 permitted_packages: ["mysdklibrary"],
361 hidden_api: {
362 max_target_q: [
363 "my-new-max-target-q.txt",
364 ],
365 split_packages: ["sdklibrary", "newlibrary"],
366 package_prefixes: ["newlibrary.all.mine"],
367 single_packages: ["newlibrary.mine"],
368 },
369 }
370 `),
371 ).RunTest(t)
372
373 // Make sure that the library exports hidden API properties for use by the bootclasspath_fragment.
374 library := result.Module("mynewlibrary", "android_common")
375 info := result.ModuleProvider(library, hiddenAPIPropertyInfoProvider).(HiddenAPIPropertyInfo)
376 android.AssertArrayString(t, "split packages", []string{"sdklibrary", "newlibrary"}, info.SplitPackages)
377 android.AssertArrayString(t, "package prefixes", []string{"newlibrary.all.mine"}, info.PackagePrefixes)
378 android.AssertArrayString(t, "single packages", []string{"newlibrary.mine"}, info.SinglePackages)
379 for _, c := range HiddenAPIFlagFileCategories {
380 expectedMaxTargetQPaths := []string(nil)
381 if c.PropertyName == "max_target_q" {
382 expectedMaxTargetQPaths = []string{"my-new-max-target-q.txt"}
383 }
384 android.AssertPathsRelativeToTopEquals(t, c.PropertyName, expectedMaxTargetQPaths, info.FlagFilesByCategory[c])
385 }
386
387 // Make sure that the signature-patterns.csv is passed all the appropriate package properties
388 // from the bootclasspath_fragment and its contents.
389 fragment := result.ModuleForTests("mybootclasspathfragment", "android_common")
390 rule := fragment.Output("modular-hiddenapi/signature-patterns.csv")
391 expectedCommand := strings.Join([]string{
392 "--split-package newlibrary",
393 "--split-package sdklibrary",
394 "--package-prefix newlibrary.all.mine",
395 "--package-prefix sdklibrary.all.mine",
396 "--single-package newlibrary.mine",
397 "--single-package sdklibrary",
398 }, " ")
399 android.AssertStringDoesContain(t, "signature patterns command", rule.RuleParams.Command, expectedCommand)
400}
401
Paul Duffinc15b9e92022-03-31 15:42:30 +0100402func TestBootclasspathFragment_Test(t *testing.T) {
403 result := android.GroupFixturePreparers(
404 prepareForTestWithBootclasspathFragment,
405 PrepareForTestWithJavaSdkLibraryFiles,
406 FixtureWithLastReleaseApis("mysdklibrary"),
407 ).RunTestWithBp(t, `
408 bootclasspath_fragment {
409 name: "myfragment",
410 contents: ["mysdklibrary"],
411 hidden_api: {
412 split_packages: [],
413 },
414 }
415
Paul Duffinff9b6fa2022-04-12 18:20:14 +0100416 bootclasspath_fragment {
417 name: "test_fragment",
418 contents: ["mysdklibrary"],
419 hidden_api: {
420 split_packages: [],
421 },
422 }
423
424 bootclasspath_fragment {
425 name: "apex.apexd_test_bootclasspath-fragment",
426 contents: ["mysdklibrary"],
427 hidden_api: {
428 split_packages: [],
429 },
430 }
431
Paul Duffinc15b9e92022-03-31 15:42:30 +0100432 bootclasspath_fragment_test {
433 name: "a_test_fragment",
434 contents: ["mysdklibrary"],
435 hidden_api: {
436 split_packages: [],
437 },
438 }
439
440
441 java_sdk_library {
442 name: "mysdklibrary",
443 srcs: ["a.java"],
444 shared_library: false,
445 public: {enabled: true},
446 system: {enabled: true},
447 }
448 `)
449
450 fragment := result.Module("myfragment", "android_common").(*BootclasspathFragmentModule)
451 android.AssertBoolEquals(t, "not a test fragment", false, fragment.isTestFragment())
452
Paul Duffinff9b6fa2022-04-12 18:20:14 +0100453 fragment = result.Module("test_fragment", "android_common").(*BootclasspathFragmentModule)
454 android.AssertBoolEquals(t, "is a test fragment by prefix", true, fragment.isTestFragment())
455
Paul Duffinc15b9e92022-03-31 15:42:30 +0100456 fragment = result.Module("a_test_fragment", "android_common").(*BootclasspathFragmentModule)
457 android.AssertBoolEquals(t, "is a test fragment by type", true, fragment.isTestFragment())
Paul Duffinff9b6fa2022-04-12 18:20:14 +0100458
459 fragment = result.Module("apex.apexd_test_bootclasspath-fragment", "android_common").(*BootclasspathFragmentModule)
460 android.AssertBoolEquals(t, "is a test fragment by name", true, fragment.isTestFragment())
Paul Duffinc15b9e92022-03-31 15:42:30 +0100461}