blob: 4c9588b725221ecfacbeeba1ecfc6a150e97b8ec [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 apex
16
17import (
Paul Duffinba6afd02019-11-19 19:44:10 +000018 "fmt"
Paul Duffince918b02021-06-07 14:33:47 +010019 "sort"
Paul Duffina1d60252021-01-21 18:13:43 +000020 "strings"
Paul Duffin3451e162021-01-20 15:16:56 +000021 "testing"
22
23 "android/soong/android"
Paul Duffin3451e162021-01-20 15:16:56 +000024 "android/soong/java"
25)
26
Paul Duffin7771eba2021-04-23 14:25:28 +010027// Contains tests for bootclasspath_fragment logic from java/bootclasspath_fragment.go as the ART
28// bootclasspath_fragment requires modules from the ART apex.
Paul Duffin3451e162021-01-20 15:16:56 +000029
Paul Duffin94f19632021-04-20 12:40:07 +010030var prepareForTestWithBootclasspathFragment = android.GroupFixturePreparers(
Paul Duffin52bfaa42021-03-23 23:40:12 +000031 java.PrepareForTestWithDexpreopt,
32 PrepareForTestWithApexBuildComponents,
33)
34
35// Some additional files needed for the art apex.
36var prepareForTestWithArtApex = android.FixtureMergeMockFs(android.MockFS{
37 "com.android.art.avbpubkey": nil,
38 "com.android.art.pem": nil,
39 "system/sepolicy/apex/com.android.art-file_contexts": nil,
40})
41
Paul Duffin94f19632021-04-20 12:40:07 +010042func TestBootclasspathFragments(t *testing.T) {
Paul Duffin52bfaa42021-03-23 23:40:12 +000043 result := android.GroupFixturePreparers(
Paul Duffin94f19632021-04-20 12:40:07 +010044 prepareForTestWithBootclasspathFragment,
Paul Duffin7771eba2021-04-23 14:25:28 +010045 // Configure some libraries in the art bootclasspath_fragment and platform_bootclasspath.
Paul Duffin60264a02021-04-12 20:02:36 +010046 java.FixtureConfigureBootJars("com.android.art:baz", "com.android.art:quuz", "platform:foo", "platform:bar"),
Paul Duffin52bfaa42021-03-23 23:40:12 +000047 prepareForTestWithArtApex,
48
49 java.PrepareForTestWithJavaSdkLibraryFiles,
50 java.FixtureWithLastReleaseApis("foo"),
Paul Duffin34d433a2021-03-09 14:13:25 +000051 ).RunTestWithBp(t, `
Paul Duffin3451e162021-01-20 15:16:56 +000052 java_sdk_library {
53 name: "foo",
54 srcs: ["b.java"],
Paul Duffin3451e162021-01-20 15:16:56 +000055 }
56
57 java_library {
58 name: "bar",
59 srcs: ["b.java"],
60 installable: true,
61 }
62
63 apex {
64 name: "com.android.art",
65 key: "com.android.art.key",
Paul Duffin58e0e762021-05-21 19:27:58 +010066 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Paul Duffin3451e162021-01-20 15:16:56 +000067 java_libs: [
68 "baz",
69 "quuz",
70 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +000071 updatable: false,
Paul Duffin3451e162021-01-20 15:16:56 +000072 }
73
74 apex_key {
75 name: "com.android.art.key",
76 public_key: "com.android.art.avbpubkey",
77 private_key: "com.android.art.pem",
78 }
79
80 java_library {
81 name: "baz",
82 apex_available: [
83 "com.android.art",
84 ],
85 srcs: ["b.java"],
Paul Duffine5218812021-06-07 13:28:19 +010086 compile_dex: true,
Paul Duffin3451e162021-01-20 15:16:56 +000087 }
88
89 java_library {
90 name: "quuz",
91 apex_available: [
92 "com.android.art",
93 ],
94 srcs: ["b.java"],
Paul Duffine5218812021-06-07 13:28:19 +010095 compile_dex: true,
Paul Duffin3451e162021-01-20 15:16:56 +000096 }
Paul Duffin5bbfef82021-01-30 12:57:26 +000097
Paul Duffin94f19632021-04-20 12:40:07 +010098 bootclasspath_fragment {
99 name: "art-bootclasspath-fragment",
Paul Duffin5bbfef82021-01-30 12:57:26 +0000100 image_name: "art",
Paul Duffinf23bc472021-04-27 12:42:20 +0100101 // Must match the "com.android.art:" entries passed to FixtureConfigureBootJars above.
102 contents: ["baz", "quuz"],
Paul Duffinc7ef9892021-03-23 23:21:59 +0000103 apex_available: [
104 "com.android.art",
105 ],
Paul Duffin5bbfef82021-01-30 12:57:26 +0000106 }
Paul Duffin3451e162021-01-20 15:16:56 +0000107`,
Paul Duffin3451e162021-01-20 15:16:56 +0000108 )
109
Paul Duffin94f19632021-04-20 12:40:07 +0100110 // Make sure that the art-bootclasspath-fragment is using the correct configuration.
Paul Duffin58e0e762021-05-21 19:27:58 +0100111 checkBootclasspathFragment(t, result, "art-bootclasspath-fragment", "android_common_apex10000",
112 "com.android.art:baz,com.android.art:quuz", `
Paul Duffina1d60252021-01-21 18:13:43 +0000113test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.art
114test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.oat
115test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.vdex
116test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.art
117test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.oat
118test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.vdex
119test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.art
120test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.oat
121test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.vdex
122test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.art
123test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.oat
124test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.vdex
125`)
Paul Duffin3451e162021-01-20 15:16:56 +0000126}
127
Paul Duffinf1b358c2021-05-17 07:38:47 +0100128func TestBootclasspathFragments_FragmentDependency(t *testing.T) {
129 result := android.GroupFixturePreparers(
130 prepareForTestWithBootclasspathFragment,
131 // Configure some libraries in the art bootclasspath_fragment and platform_bootclasspath.
132 java.FixtureConfigureBootJars("com.android.art:baz", "com.android.art:quuz", "platform:foo", "platform:bar"),
133 prepareForTestWithArtApex,
134
135 java.PrepareForTestWithJavaSdkLibraryFiles,
136 java.FixtureWithLastReleaseApis("foo", "baz"),
137 ).RunTestWithBp(t, `
138 java_sdk_library {
139 name: "foo",
140 srcs: ["b.java"],
141 shared_library: false,
142 public: {
143 enabled: true,
144 },
145 system: {
146 enabled: true,
147 },
148 }
149
150 java_library {
151 name: "bar",
152 srcs: ["b.java"],
153 installable: true,
154 }
155
156 apex {
157 name: "com.android.art",
158 key: "com.android.art.key",
159 bootclasspath_fragments: ["art-bootclasspath-fragment"],
160 updatable: false,
161 }
162
163 apex_key {
164 name: "com.android.art.key",
165 public_key: "com.android.art.avbpubkey",
166 private_key: "com.android.art.pem",
167 }
168
169 java_sdk_library {
170 name: "baz",
171 apex_available: [
172 "com.android.art",
173 ],
174 srcs: ["b.java"],
175 shared_library: false,
176 public: {
177 enabled: true,
178 },
179 system: {
180 enabled: true,
181 },
182 test: {
183 enabled: true,
184 },
185 }
186
187 java_library {
188 name: "quuz",
189 apex_available: [
190 "com.android.art",
191 ],
192 srcs: ["b.java"],
193 compile_dex: true,
194 }
195
196 bootclasspath_fragment {
197 name: "art-bootclasspath-fragment",
198 image_name: "art",
199 // Must match the "com.android.art:" entries passed to FixtureConfigureBootJars above.
200 contents: ["baz", "quuz"],
201 apex_available: [
202 "com.android.art",
203 ],
204 }
205
206 bootclasspath_fragment {
207 name: "other-bootclasspath-fragment",
208 contents: ["foo", "bar"],
209 fragments: [
210 {
211 apex: "com.android.art",
212 module: "art-bootclasspath-fragment",
213 },
214 ],
215 }
216`,
217 )
218
Paul Duffin31fad802021-06-18 18:14:25 +0100219 checkAPIScopeStubs := func(message string, info java.HiddenAPIInfo, apiScope *java.HiddenAPIScope, expectedPaths ...string) {
Paul Duffinf1b358c2021-05-17 07:38:47 +0100220 t.Helper()
Paul Duffin31fad802021-06-18 18:14:25 +0100221 android.AssertPathsRelativeToTopEquals(t, fmt.Sprintf("%s %s", message, apiScope), expectedPaths, info.TransitiveStubDexJarsByScope[apiScope])
Paul Duffinf1b358c2021-05-17 07:38:47 +0100222 }
223
224 // Check stub dex paths exported by art.
225 artFragment := result.Module("art-bootclasspath-fragment", "android_common")
226 artInfo := result.ModuleProvider(artFragment, java.HiddenAPIInfoProvider).(java.HiddenAPIInfo)
227
228 bazPublicStubs := "out/soong/.intermediates/baz.stubs/android_common/dex/baz.stubs.jar"
229 bazSystemStubs := "out/soong/.intermediates/baz.stubs.system/android_common/dex/baz.stubs.system.jar"
230 bazTestStubs := "out/soong/.intermediates/baz.stubs.test/android_common/dex/baz.stubs.test.jar"
231
Paul Duffin31fad802021-06-18 18:14:25 +0100232 checkAPIScopeStubs("art", artInfo, java.PublicHiddenAPIScope, bazPublicStubs)
233 checkAPIScopeStubs("art", artInfo, java.SystemHiddenAPIScope, bazSystemStubs)
234 checkAPIScopeStubs("art", artInfo, java.TestHiddenAPIScope, bazTestStubs)
235 checkAPIScopeStubs("art", artInfo, java.CorePlatformHiddenAPIScope)
Paul Duffinf1b358c2021-05-17 07:38:47 +0100236
237 // Check stub dex paths exported by other.
238 otherFragment := result.Module("other-bootclasspath-fragment", "android_common")
239 otherInfo := result.ModuleProvider(otherFragment, java.HiddenAPIInfoProvider).(java.HiddenAPIInfo)
240
241 fooPublicStubs := "out/soong/.intermediates/foo.stubs/android_common/dex/foo.stubs.jar"
242 fooSystemStubs := "out/soong/.intermediates/foo.stubs.system/android_common/dex/foo.stubs.system.jar"
243
Paul Duffin31fad802021-06-18 18:14:25 +0100244 checkAPIScopeStubs("other", otherInfo, java.PublicHiddenAPIScope, bazPublicStubs, fooPublicStubs)
245 checkAPIScopeStubs("other", otherInfo, java.SystemHiddenAPIScope, bazSystemStubs, fooSystemStubs)
246 checkAPIScopeStubs("other", otherInfo, java.TestHiddenAPIScope, bazTestStubs, fooSystemStubs)
247 checkAPIScopeStubs("other", otherInfo, java.CorePlatformHiddenAPIScope)
Paul Duffinf1b358c2021-05-17 07:38:47 +0100248}
249
Paul Duffin58e0e762021-05-21 19:27:58 +0100250func checkBootclasspathFragment(t *testing.T, result *android.TestResult, moduleName, variantName string, expectedConfiguredModules string, expectedBootclasspathFragmentFiles string) {
Paul Duffin3451e162021-01-20 15:16:56 +0000251 t.Helper()
252
Paul Duffin58e0e762021-05-21 19:27:58 +0100253 bootclasspathFragment := result.ModuleForTests(moduleName, variantName).Module().(*java.BootclasspathFragmentModule)
Paul Duffin3451e162021-01-20 15:16:56 +0000254
Paul Duffine946b322021-04-25 23:04:00 +0100255 bootclasspathFragmentInfo := result.ModuleProvider(bootclasspathFragment, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
256 modules := bootclasspathFragmentInfo.Modules()
Paul Duffin34d433a2021-03-09 14:13:25 +0000257 android.AssertStringEquals(t, "invalid modules for "+moduleName, expectedConfiguredModules, modules.String())
Paul Duffina1d60252021-01-21 18:13:43 +0000258
259 // Get a list of all the paths in the boot image sorted by arch type.
260 allPaths := []string{}
Paul Duffine946b322021-04-25 23:04:00 +0100261 bootImageFilesByArchType := bootclasspathFragmentInfo.AndroidBootImageFilesByArchType()
Paul Duffina1d60252021-01-21 18:13:43 +0000262 for _, archType := range android.ArchTypeList() {
263 if paths, ok := bootImageFilesByArchType[archType]; ok {
264 for _, path := range paths {
265 allPaths = append(allPaths, android.NormalizePathForTesting(path))
266 }
267 }
268 }
Paul Duffin3451e162021-01-20 15:16:56 +0000269
Paul Duffin94f19632021-04-20 12:40:07 +0100270 android.AssertTrimmedStringEquals(t, "invalid paths for "+moduleName, expectedBootclasspathFragmentFiles, strings.Join(allPaths, "\n"))
Paul Duffin3451e162021-01-20 15:16:56 +0000271}
Paul Duffina1d60252021-01-21 18:13:43 +0000272
Paul Duffin94f19632021-04-20 12:40:07 +0100273func TestBootclasspathFragmentInArtApex(t *testing.T) {
Paul Duffinba6afd02019-11-19 19:44:10 +0000274 commonPreparer := android.GroupFixturePreparers(
Paul Duffin94f19632021-04-20 12:40:07 +0100275 prepareForTestWithBootclasspathFragment,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000276 prepareForTestWithArtApex,
277
Paul Duffinba6afd02019-11-19 19:44:10 +0000278 android.FixtureWithRootAndroidBp(`
Paul Duffina1d60252021-01-21 18:13:43 +0000279 apex {
Paul Duffin9ea71c02021-03-23 22:53:07 +0000280 name: "com.android.art",
281 key: "com.android.art.key",
Paul Duffin94f19632021-04-20 12:40:07 +0100282 bootclasspath_fragments: [
283 "mybootclasspathfragment",
Paul Duffina1d60252021-01-21 18:13:43 +0000284 ],
Paul Duffin4d101b62021-03-24 15:42:20 +0000285 // bar (like foo) should be transitively included in this apex because it is part of the
Paul Duffin7771eba2021-04-23 14:25:28 +0100286 // mybootclasspathfragment bootclasspath_fragment. However, it is kept here to ensure that the
287 // apex dedups the files correctly.
Paul Duffin9ea71c02021-03-23 22:53:07 +0000288 java_libs: [
Paul Duffin9ea71c02021-03-23 22:53:07 +0000289 "bar",
290 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000291 updatable: false,
Paul Duffina1d60252021-01-21 18:13:43 +0000292 }
293
294 apex_key {
Paul Duffin9ea71c02021-03-23 22:53:07 +0000295 name: "com.android.art.key",
Paul Duffina1d60252021-01-21 18:13:43 +0000296 public_key: "testkey.avbpubkey",
297 private_key: "testkey.pem",
298 }
299
300 java_library {
301 name: "foo",
302 srcs: ["b.java"],
303 installable: true,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000304 apex_available: [
305 "com.android.art",
306 ],
Paul Duffina1d60252021-01-21 18:13:43 +0000307 }
308
309 java_library {
310 name: "bar",
311 srcs: ["b.java"],
312 installable: true,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000313 apex_available: [
314 "com.android.art",
315 ],
Paul Duffina1d60252021-01-21 18:13:43 +0000316 }
317
Paul Duffin65898052021-04-20 22:47:03 +0100318 java_import {
319 name: "foo",
320 jars: ["foo.jar"],
321 apex_available: [
322 "com.android.art",
323 ],
Paul Duffine5218812021-06-07 13:28:19 +0100324 compile_dex: true,
Paul Duffin65898052021-04-20 22:47:03 +0100325 }
326
327 java_import {
328 name: "bar",
329 jars: ["bar.jar"],
330 apex_available: [
331 "com.android.art",
332 ],
Paul Duffine5218812021-06-07 13:28:19 +0100333 compile_dex: true,
Paul Duffin65898052021-04-20 22:47:03 +0100334 }
Paul Duffinba6afd02019-11-19 19:44:10 +0000335 `),
336 )
Paul Duffin65898052021-04-20 22:47:03 +0100337
Paul Duffinba6afd02019-11-19 19:44:10 +0000338 contentsInsert := func(contents []string) string {
339 insert := ""
340 if contents != nil {
341 insert = fmt.Sprintf(`contents: ["%s"],`, strings.Join(contents, `", "`))
Paul Duffin396229f2021-03-18 18:30:31 +0000342 }
Paul Duffinba6afd02019-11-19 19:44:10 +0000343 return insert
344 }
Paul Duffina1d60252021-01-21 18:13:43 +0000345
Paul Duffinba6afd02019-11-19 19:44:10 +0000346 addSource := func(contents ...string) android.FixturePreparer {
347 text := fmt.Sprintf(`
348 bootclasspath_fragment {
349 name: "mybootclasspathfragment",
350 image_name: "art",
351 %s
352 apex_available: [
353 "com.android.art",
354 ],
355 }
356 `, contentsInsert(contents))
357
358 return android.FixtureAddTextFile("art/build/boot/Android.bp", text)
359 }
360
361 addPrebuilt := func(prefer bool, contents ...string) android.FixturePreparer {
362 text := fmt.Sprintf(`
Paul Duffince918b02021-06-07 14:33:47 +0100363 prebuilt_apex {
364 name: "com.android.art",
365 arch: {
366 arm64: {
367 src: "com.android.art-arm64.apex",
368 },
369 arm: {
370 src: "com.android.art-arm.apex",
371 },
372 },
373 exported_bootclasspath_fragments: ["mybootclasspathfragment"],
374 }
375
Paul Duffinba6afd02019-11-19 19:44:10 +0000376 prebuilt_bootclasspath_fragment {
377 name: "mybootclasspathfragment",
378 image_name: "art",
379 %s
380 prefer: %t,
381 apex_available: [
382 "com.android.art",
383 ],
384 }
385 `, contentsInsert(contents), prefer)
386 return android.FixtureAddTextFile("prebuilts/module_sdk/art/Android.bp", text)
387 }
388
Paul Duffince918b02021-06-07 14:33:47 +0100389 t.Run("boot image files from source", func(t *testing.T) {
390 result := android.GroupFixturePreparers(
391 commonPreparer,
392
393 // Configure some libraries in the art bootclasspath_fragment that match the source
394 // bootclasspath_fragment's contents property.
395 java.FixtureConfigureBootJars("com.android.art:foo", "com.android.art:bar"),
396 addSource("foo", "bar"),
397 ).RunTest(t)
398
399 ensureExactContents(t, result.TestContext, "com.android.art", "android_common_com.android.art_image", []string{
400 "etc/classpaths/bootclasspath.pb",
401 "javalib/arm/boot.art",
402 "javalib/arm/boot.oat",
403 "javalib/arm/boot.vdex",
404 "javalib/arm/boot-bar.art",
405 "javalib/arm/boot-bar.oat",
406 "javalib/arm/boot-bar.vdex",
407 "javalib/arm64/boot.art",
408 "javalib/arm64/boot.oat",
409 "javalib/arm64/boot.vdex",
410 "javalib/arm64/boot-bar.art",
411 "javalib/arm64/boot-bar.oat",
412 "javalib/arm64/boot-bar.vdex",
413 "javalib/bar.jar",
414 "javalib/foo.jar",
415 })
416
417 java.CheckModuleDependencies(t, result.TestContext, "com.android.art", "android_common_com.android.art_image", []string{
418 `bar`,
419 `com.android.art.key`,
420 `mybootclasspathfragment`,
421 })
422
423 // Make sure that the source bootclasspath_fragment copies its dex files to the predefined
424 // locations for the art image.
425 module := result.ModuleForTests("mybootclasspathfragment", "android_common_apex10000")
426 checkCopiesToPredefinedLocationForArt(t, result.Config, module, "bar", "foo")
427 })
428
429 t.Run("boot image files with preferred prebuilt", func(t *testing.T) {
Paul Duffinba6afd02019-11-19 19:44:10 +0000430 result := android.GroupFixturePreparers(
431 commonPreparer,
432
433 // Configure some libraries in the art bootclasspath_fragment that match the source
434 // bootclasspath_fragment's contents property.
435 java.FixtureConfigureBootJars("com.android.art:foo", "com.android.art:bar"),
436 addSource("foo", "bar"),
437
438 // Make sure that a preferred prebuilt with consistent contents doesn't affect the apex.
439 addPrebuilt(true, "foo", "bar"),
440 ).RunTest(t)
441
442 ensureExactContents(t, result.TestContext, "com.android.art", "android_common_com.android.art_image", []string{
satayev227e7452021-05-20 21:35:06 +0100443 "etc/classpaths/bootclasspath.pb",
Paul Duffinba6afd02019-11-19 19:44:10 +0000444 "javalib/arm/boot.art",
445 "javalib/arm/boot.oat",
446 "javalib/arm/boot.vdex",
447 "javalib/arm/boot-bar.art",
448 "javalib/arm/boot-bar.oat",
449 "javalib/arm/boot-bar.vdex",
450 "javalib/arm64/boot.art",
451 "javalib/arm64/boot.oat",
452 "javalib/arm64/boot.vdex",
453 "javalib/arm64/boot-bar.art",
454 "javalib/arm64/boot-bar.oat",
455 "javalib/arm64/boot-bar.vdex",
456 "javalib/bar.jar",
457 "javalib/foo.jar",
458 })
459
460 java.CheckModuleDependencies(t, result.TestContext, "com.android.art", "android_common_com.android.art_image", []string{
461 `bar`,
462 `com.android.art.key`,
463 `mybootclasspathfragment`,
Paul Duffince918b02021-06-07 14:33:47 +0100464 `prebuilt_com.android.art`,
Paul Duffinba6afd02019-11-19 19:44:10 +0000465 })
Paul Duffince918b02021-06-07 14:33:47 +0100466
467 // Make sure that the prebuilt bootclasspath_fragment copies its dex files to the predefined
468 // locations for the art image.
469 module := result.ModuleForTests("prebuilt_mybootclasspathfragment", "android_common_com.android.art")
470 checkCopiesToPredefinedLocationForArt(t, result.Config, module, "bar", "foo")
Paul Duffina1d60252021-01-21 18:13:43 +0000471 })
Paul Duffin396229f2021-03-18 18:30:31 +0000472
Paul Duffinba6afd02019-11-19 19:44:10 +0000473 t.Run("source with inconsistency between config and contents", func(t *testing.T) {
474 android.GroupFixturePreparers(
475 commonPreparer,
476
477 // Create an inconsistency between the ArtApexJars configuration and the art source
478 // bootclasspath_fragment module's contents property.
479 java.FixtureConfigureBootJars("com.android.art:foo"),
480 addSource("foo", "bar"),
481 ).
482 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`\QArtApexJars configuration specifies []string{"foo"}, contents property specifies []string{"foo", "bar"}\E`)).
483 RunTest(t)
484 })
485
486 t.Run("prebuilt with inconsistency between config and contents", func(t *testing.T) {
487 android.GroupFixturePreparers(
488 commonPreparer,
489
490 // Create an inconsistency between the ArtApexJars configuration and the art
491 // prebuilt_bootclasspath_fragment module's contents property.
492 java.FixtureConfigureBootJars("com.android.art:foo"),
493 addPrebuilt(false, "foo", "bar"),
494 ).
495 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`\QArtApexJars configuration specifies []string{"foo"}, contents property specifies []string{"foo", "bar"}\E`)).
496 RunTest(t)
497 })
498
499 t.Run("preferred prebuilt with inconsistency between config and contents", func(t *testing.T) {
500 android.GroupFixturePreparers(
501 commonPreparer,
502
503 // Create an inconsistency between the ArtApexJars configuration and the art
504 // prebuilt_bootclasspath_fragment module's contents property.
505 java.FixtureConfigureBootJars("com.android.art:foo"),
506 addPrebuilt(true, "foo", "bar"),
507
508 // Source contents property is consistent with the config.
509 addSource("foo"),
510 ).
511 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`\QArtApexJars configuration specifies []string{"foo"}, contents property specifies []string{"foo", "bar"}\E`)).
512 RunTest(t)
513 })
514
515 t.Run("source preferred and prebuilt with inconsistency between config and contents", func(t *testing.T) {
516 android.GroupFixturePreparers(
517 commonPreparer,
518
519 // Create an inconsistency between the ArtApexJars configuration and the art
520 // prebuilt_bootclasspath_fragment module's contents property.
521 java.FixtureConfigureBootJars("com.android.art:foo"),
522 addPrebuilt(false, "foo", "bar"),
523
524 // Source contents property is consistent with the config.
525 addSource("foo"),
526
527 // This should pass because while the prebuilt is inconsistent with the configuration it is
528 // not actually used.
529 ).RunTest(t)
Paul Duffin396229f2021-03-18 18:30:31 +0000530 })
Paul Duffina1d60252021-01-21 18:13:43 +0000531}
532
Paul Duffin94f19632021-04-20 12:40:07 +0100533func TestBootclasspathFragmentInPrebuiltArtApex(t *testing.T) {
Paul Duffin9ea71c02021-03-23 22:53:07 +0000534 result := android.GroupFixturePreparers(
Paul Duffin94f19632021-04-20 12:40:07 +0100535 prepareForTestWithBootclasspathFragment,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000536 prepareForTestWithArtApex,
537
538 android.FixtureMergeMockFs(android.MockFS{
539 "com.android.art-arm64.apex": nil,
540 "com.android.art-arm.apex": nil,
541 }),
542
Paul Duffin7771eba2021-04-23 14:25:28 +0100543 // Configure some libraries in the art bootclasspath_fragment.
Paul Duffin60264a02021-04-12 20:02:36 +0100544 java.FixtureConfigureBootJars("com.android.art:foo", "com.android.art:bar"),
Paul Duffin9ea71c02021-03-23 22:53:07 +0000545 ).RunTestWithBp(t, `
546 prebuilt_apex {
547 name: "com.android.art",
548 arch: {
549 arm64: {
550 src: "com.android.art-arm64.apex",
551 },
552 arm: {
553 src: "com.android.art-arm.apex",
554 },
555 },
Paul Duffine5218812021-06-07 13:28:19 +0100556 exported_bootclasspath_fragments: ["mybootclasspathfragment"],
Paul Duffin9ea71c02021-03-23 22:53:07 +0000557 }
558
559 java_import {
560 name: "foo",
561 jars: ["foo.jar"],
562 apex_available: [
563 "com.android.art",
564 ],
565 }
566
567 java_import {
568 name: "bar",
569 jars: ["bar.jar"],
570 apex_available: [
571 "com.android.art",
572 ],
573 }
574
Paul Duffin7771eba2021-04-23 14:25:28 +0100575 prebuilt_bootclasspath_fragment {
Paul Duffin94f19632021-04-20 12:40:07 +0100576 name: "mybootclasspathfragment",
Paul Duffin9ea71c02021-03-23 22:53:07 +0000577 image_name: "art",
Paul Duffinf23bc472021-04-27 12:42:20 +0100578 // Must match the "com.android.art:" entries passed to FixtureConfigureBootJars above.
579 contents: ["foo", "bar"],
Paul Duffin9ea71c02021-03-23 22:53:07 +0000580 apex_available: [
581 "com.android.art",
582 ],
583 }
584 `)
585
Paul Duffin6717d882021-06-15 19:09:41 +0100586 java.CheckModuleDependencies(t, result.TestContext, "com.android.art", "android_common_com.android.art", []string{
Paul Duffin11216db2021-03-01 14:14:52 +0000587 `com.android.art.apex.selector`,
Paul Duffine5218812021-06-07 13:28:19 +0100588 `prebuilt_mybootclasspathfragment`,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000589 })
590
Paul Duffince918b02021-06-07 14:33:47 +0100591 java.CheckModuleDependencies(t, result.TestContext, "mybootclasspathfragment", "android_common_com.android.art", []string{
Paul Duffin5466a362021-06-07 10:25:31 +0100592 `com.android.art.deapexer`,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000593 `dex2oatd`,
Paul Duffinc7ef9892021-03-23 23:21:59 +0000594 `prebuilt_bar`,
595 `prebuilt_foo`,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000596 })
Paul Duffince918b02021-06-07 14:33:47 +0100597
598 module := result.ModuleForTests("mybootclasspathfragment", "android_common_com.android.art")
599 checkCopiesToPredefinedLocationForArt(t, result.Config, module, "bar", "foo")
600}
601
602// checkCopiesToPredefinedLocationForArt checks that the supplied modules are copied to the
603// predefined locations of boot dex jars used as inputs for the ART boot image.
604func checkCopiesToPredefinedLocationForArt(t *testing.T, config android.Config, module android.TestingModule, modules ...string) {
605 t.Helper()
606 bootJarLocations := []string{}
607 for _, output := range module.AllOutputs() {
608 output = android.StringRelativeToTop(config, output)
609 if strings.HasPrefix(output, "out/soong/test_device/dex_artjars_input/") {
610 bootJarLocations = append(bootJarLocations, output)
611 }
612 }
613
614 sort.Strings(bootJarLocations)
615 expected := []string{}
616 for _, m := range modules {
617 expected = append(expected, fmt.Sprintf("out/soong/test_device/dex_artjars_input/%s.jar", m))
618 }
619 sort.Strings(expected)
620
621 android.AssertArrayString(t, "copies to predefined locations for art", expected, bootJarLocations)
Paul Duffin9ea71c02021-03-23 22:53:07 +0000622}
623
Paul Duffin94f19632021-04-20 12:40:07 +0100624func TestBootclasspathFragmentContentsNoName(t *testing.T) {
Paul Duffin82886d62021-03-24 01:34:57 +0000625 result := android.GroupFixturePreparers(
Paul Duffin94f19632021-04-20 12:40:07 +0100626 prepareForTestWithBootclasspathFragment,
Paul Duffin82886d62021-03-24 01:34:57 +0000627 prepareForTestWithMyapex,
Paul Duffinf2fa0b52021-05-14 18:21:45 +0100628 // Configure bootclasspath jars to ensure that hidden API encoding is performed on them.
629 java.FixtureConfigureBootJars("myapex:foo", "myapex:bar"),
630 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
631 // is disabled.
632 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
633
634 java.PrepareForTestWithJavaSdkLibraryFiles,
635 java.FixtureWithLastReleaseApis("foo"),
Paul Duffin82886d62021-03-24 01:34:57 +0000636 ).RunTestWithBp(t, `
637 apex {
638 name: "myapex",
639 key: "myapex.key",
Paul Duffin94f19632021-04-20 12:40:07 +0100640 bootclasspath_fragments: [
641 "mybootclasspathfragment",
Paul Duffin82886d62021-03-24 01:34:57 +0000642 ],
643 updatable: false,
644 }
645
646 apex_key {
647 name: "myapex.key",
648 public_key: "testkey.avbpubkey",
649 private_key: "testkey.pem",
650 }
651
Paul Duffinf2fa0b52021-05-14 18:21:45 +0100652 java_sdk_library {
Paul Duffin82886d62021-03-24 01:34:57 +0000653 name: "foo",
654 srcs: ["b.java"],
Paul Duffinf2fa0b52021-05-14 18:21:45 +0100655 shared_library: false,
656 public: {enabled: true},
Paul Duffin82886d62021-03-24 01:34:57 +0000657 apex_available: [
658 "myapex",
659 ],
660 }
661
662 java_library {
663 name: "bar",
664 srcs: ["b.java"],
665 installable: true,
666 apex_available: [
667 "myapex",
668 ],
669 }
670
Paul Duffin7771eba2021-04-23 14:25:28 +0100671 bootclasspath_fragment {
Paul Duffin94f19632021-04-20 12:40:07 +0100672 name: "mybootclasspathfragment",
Paul Duffin82886d62021-03-24 01:34:57 +0000673 contents: [
674 "foo",
675 "bar",
676 ],
677 apex_available: [
678 "myapex",
679 ],
680 }
681 `)
682
Paul Duffin4d101b62021-03-24 15:42:20 +0000683 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
684 // This does not include art, oat or vdex files as they are only included for the art boot
685 // image.
satayev227e7452021-05-20 21:35:06 +0100686 "etc/classpaths/bootclasspath.pb",
Paul Duffin4d101b62021-03-24 15:42:20 +0000687 "javalib/bar.jar",
688 "javalib/foo.jar",
689 })
Paul Duffin82886d62021-03-24 01:34:57 +0000690
691 java.CheckModuleDependencies(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
692 `myapex.key`,
Paul Duffin94f19632021-04-20 12:40:07 +0100693 `mybootclasspathfragment`,
Paul Duffin82886d62021-03-24 01:34:57 +0000694 })
Paul Duffinf2fa0b52021-05-14 18:21:45 +0100695
696 apex := result.ModuleForTests("myapex", "android_common_myapex_image")
697 apexRule := apex.Rule("apexRule")
698 copyCommands := apexRule.Args["copy_commands"]
699
700 // Make sure that the fragment provides the hidden API encoded dex jars to the APEX.
701 fragment := result.Module("mybootclasspathfragment", "android_common_apex10000")
702
703 info := result.ModuleProvider(fragment, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
704
705 checkFragmentExportedDexJar := func(name string, expectedDexJar string) {
706 module := result.Module(name, "android_common_apex10000")
Paul Duffin1a8010a2021-05-15 12:39:23 +0100707 dexJar, err := info.DexBootJarPathForContentModule(module)
708 if err != nil {
709 t.Error(err)
710 }
Paul Duffinf2fa0b52021-05-14 18:21:45 +0100711 android.AssertPathRelativeToTopEquals(t, name+" dex", expectedDexJar, dexJar)
712
713 expectedCopyCommand := fmt.Sprintf("&& cp -f %s out/soong/.intermediates/myapex/android_common_myapex_image/image.apex/javalib/%s.jar", expectedDexJar, name)
714 android.AssertStringDoesContain(t, name+" apex copy command", copyCommands, expectedCopyCommand)
715 }
716
Paul Duffin54c98f52021-05-15 08:54:30 +0100717 checkFragmentExportedDexJar("foo", "out/soong/.intermediates/mybootclasspathfragment/android_common_apex10000/hiddenapi-modular/encoded/foo.jar")
718 checkFragmentExportedDexJar("bar", "out/soong/.intermediates/mybootclasspathfragment/android_common_apex10000/hiddenapi-modular/encoded/bar.jar")
Paul Duffin82886d62021-03-24 01:34:57 +0000719}
720
Paul Duffin48b67412021-06-23 16:13:50 +0100721func getDexJarPath(result *android.TestResult, name string) string {
722 module := result.Module(name, "android_common")
723 return module.(java.UsesLibraryDependency).DexJarBuildPath().RelativeToTop().String()
724}
725
726// TestBootclasspathFragment_HiddenAPIList checks to make sure that the correct parameters are
727// passed to the hiddenapi list tool.
728func TestBootclasspathFragment_HiddenAPIList(t *testing.T) {
729 result := android.GroupFixturePreparers(
730 prepareForTestWithBootclasspathFragment,
731 prepareForTestWithArtApex,
732 prepareForTestWithMyapex,
733 // Configure bootclasspath jars to ensure that hidden API encoding is performed on them.
734 java.FixtureConfigureBootJars("com.android.art:baz", "com.android.art:quuz"),
735 java.FixtureConfigureUpdatableBootJars("myapex:foo", "myapex:bar"),
736 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
737 // is disabled.
738 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
739
740 java.PrepareForTestWithJavaSdkLibraryFiles,
741 java.FixtureWithLastReleaseApis("foo", "quuz"),
742 ).RunTestWithBp(t, `
743 apex {
744 name: "com.android.art",
745 key: "com.android.art.key",
746 bootclasspath_fragments: ["art-bootclasspath-fragment"],
747 updatable: false,
748 }
749
750 apex_key {
751 name: "com.android.art.key",
752 public_key: "com.android.art.avbpubkey",
753 private_key: "com.android.art.pem",
754 }
755
756 java_library {
757 name: "baz",
758 apex_available: [
759 "com.android.art",
760 ],
761 srcs: ["b.java"],
762 compile_dex: true,
763 }
764
765 java_sdk_library {
766 name: "quuz",
767 apex_available: [
768 "com.android.art",
769 ],
770 srcs: ["b.java"],
771 compile_dex: true,
772 public: {enabled: true},
773 system: {enabled: true},
774 test: {enabled: true},
775 module_lib: {enabled: true},
776 }
777
778 bootclasspath_fragment {
779 name: "art-bootclasspath-fragment",
780 image_name: "art",
781 // Must match the "com.android.art:" entries passed to FixtureConfigureBootJars above.
782 contents: ["baz", "quuz"],
783 apex_available: [
784 "com.android.art",
785 ],
786 }
787
788 apex {
789 name: "myapex",
790 key: "myapex.key",
791 bootclasspath_fragments: [
792 "mybootclasspathfragment",
793 ],
794 updatable: false,
795 }
796
797 apex_key {
798 name: "myapex.key",
799 public_key: "testkey.avbpubkey",
800 private_key: "testkey.pem",
801 }
802
803 java_sdk_library {
804 name: "foo",
805 srcs: ["b.java"],
806 shared_library: false,
807 public: {enabled: true},
808 apex_available: [
809 "myapex",
810 ],
811 }
812
813 java_library {
814 name: "bar",
815 srcs: ["b.java"],
816 installable: true,
817 apex_available: [
818 "myapex",
819 ],
820 }
821
822 bootclasspath_fragment {
823 name: "mybootclasspathfragment",
824 contents: [
825 "foo",
826 "bar",
827 ],
828 apex_available: [
829 "myapex",
830 ],
831 fragments: [
832 {
833 apex: "com.android.art",
834 module: "art-bootclasspath-fragment",
835 },
836 ],
837 }
838 `)
839
840 java.CheckModuleDependencies(t, result.TestContext, "mybootclasspathfragment", "android_common_apex10000", []string{
841 "art-bootclasspath-fragment",
842 "bar",
843 "dex2oatd",
844 "foo",
845 })
846
847 fooStubs := getDexJarPath(result, "foo.stubs")
848 quuzPublicStubs := getDexJarPath(result, "quuz.stubs")
849 quuzSystemStubs := getDexJarPath(result, "quuz.stubs.system")
850 quuzTestStubs := getDexJarPath(result, "quuz.stubs.test")
851
852 // Make sure that the fragment uses the quuz stub dex jars when generating the hidden API flags.
853 fragment := result.ModuleForTests("mybootclasspathfragment", "android_common_apex10000")
854
855 rule := fragment.Rule("modularHiddenAPIStubFlagsFile")
856 command := rule.RuleParams.Command
857 android.AssertStringDoesContain(t, "check correct rule", command, "hiddenapi list")
858
859 // Make sure that the quuz stubs are available for resolving references from the implementation
860 // boot dex jars provided by this module.
861 android.AssertStringDoesContain(t, "quuz widest", command, "--dependency-stub-dex="+quuzTestStubs)
862
863 // Make sure that the quuz stubs are available for resolving references from the different API
864 // stubs provided by this module.
865 android.AssertStringDoesContain(t, "public", command, "--public-stub-classpath="+quuzPublicStubs+":"+fooStubs)
866 android.AssertStringDoesContain(t, "system", command, "--system-stub-classpath="+quuzSystemStubs+":"+fooStubs)
867 android.AssertStringDoesContain(t, "test", command, "--test-stub-classpath="+quuzTestStubs+":"+fooStubs)
868}
869
Paul Duffina1d60252021-01-21 18:13:43 +0000870// TODO(b/177892522) - add test for host apex.