blob: 3d39d341c18fbf6e8c42a301eb00422764548bb4 [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 Duffina1d60252021-01-21 18:13:43 +000019 "strings"
Paul Duffin3451e162021-01-20 15:16:56 +000020 "testing"
21
22 "android/soong/android"
Paul Duffin3451e162021-01-20 15:16:56 +000023 "android/soong/java"
24)
25
Paul Duffin7771eba2021-04-23 14:25:28 +010026// Contains tests for bootclasspath_fragment logic from java/bootclasspath_fragment.go as the ART
27// bootclasspath_fragment requires modules from the ART apex.
Paul Duffin3451e162021-01-20 15:16:56 +000028
Paul Duffin94f19632021-04-20 12:40:07 +010029var prepareForTestWithBootclasspathFragment = android.GroupFixturePreparers(
Paul Duffin52bfaa42021-03-23 23:40:12 +000030 java.PrepareForTestWithDexpreopt,
31 PrepareForTestWithApexBuildComponents,
32)
33
34// Some additional files needed for the art apex.
35var prepareForTestWithArtApex = android.FixtureMergeMockFs(android.MockFS{
36 "com.android.art.avbpubkey": nil,
37 "com.android.art.pem": nil,
38 "system/sepolicy/apex/com.android.art-file_contexts": nil,
39})
40
Paul Duffin94f19632021-04-20 12:40:07 +010041func TestBootclasspathFragments(t *testing.T) {
Paul Duffin52bfaa42021-03-23 23:40:12 +000042 result := android.GroupFixturePreparers(
Paul Duffin94f19632021-04-20 12:40:07 +010043 prepareForTestWithBootclasspathFragment,
Paul Duffin7771eba2021-04-23 14:25:28 +010044 // Configure some libraries in the art bootclasspath_fragment and platform_bootclasspath.
Paul Duffin60264a02021-04-12 20:02:36 +010045 java.FixtureConfigureBootJars("com.android.art:baz", "com.android.art:quuz", "platform:foo", "platform:bar"),
Paul Duffin52bfaa42021-03-23 23:40:12 +000046 prepareForTestWithArtApex,
47
48 java.PrepareForTestWithJavaSdkLibraryFiles,
49 java.FixtureWithLastReleaseApis("foo"),
Paul Duffin34d433a2021-03-09 14:13:25 +000050 ).RunTestWithBp(t, `
Paul Duffin3451e162021-01-20 15:16:56 +000051 java_sdk_library {
52 name: "foo",
53 srcs: ["b.java"],
Paul Duffin3451e162021-01-20 15:16:56 +000054 }
55
56 java_library {
57 name: "bar",
58 srcs: ["b.java"],
59 installable: true,
60 }
61
62 apex {
63 name: "com.android.art",
64 key: "com.android.art.key",
65 java_libs: [
66 "baz",
67 "quuz",
68 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +000069 updatable: false,
Paul Duffin3451e162021-01-20 15:16:56 +000070 }
71
72 apex_key {
73 name: "com.android.art.key",
74 public_key: "com.android.art.avbpubkey",
75 private_key: "com.android.art.pem",
76 }
77
78 java_library {
79 name: "baz",
80 apex_available: [
81 "com.android.art",
82 ],
83 srcs: ["b.java"],
84 }
85
86 java_library {
87 name: "quuz",
88 apex_available: [
89 "com.android.art",
90 ],
91 srcs: ["b.java"],
92 }
Paul Duffin5bbfef82021-01-30 12:57:26 +000093
Paul Duffin94f19632021-04-20 12:40:07 +010094 bootclasspath_fragment {
95 name: "art-bootclasspath-fragment",
Paul Duffin5bbfef82021-01-30 12:57:26 +000096 image_name: "art",
Paul Duffinc7ef9892021-03-23 23:21:59 +000097 apex_available: [
98 "com.android.art",
99 ],
Paul Duffin5bbfef82021-01-30 12:57:26 +0000100 }
101
Paul Duffin94f19632021-04-20 12:40:07 +0100102 bootclasspath_fragment {
103 name: "framework-bootclasspath-fragment",
Paul Duffin5bbfef82021-01-30 12:57:26 +0000104 image_name: "boot",
105 }
Paul Duffin3451e162021-01-20 15:16:56 +0000106`,
Paul Duffin3451e162021-01-20 15:16:56 +0000107 )
108
Paul Duffin94f19632021-04-20 12:40:07 +0100109 // Make sure that the framework-bootclasspath-fragment is using the correct configuration.
110 checkBootclasspathFragment(t, result, "framework-bootclasspath-fragment", "platform:foo,platform:bar", `
Paul Duffina1d60252021-01-21 18:13:43 +0000111test_device/dex_bootjars/android/system/framework/arm/boot-foo.art
112test_device/dex_bootjars/android/system/framework/arm/boot-foo.oat
113test_device/dex_bootjars/android/system/framework/arm/boot-foo.vdex
114test_device/dex_bootjars/android/system/framework/arm/boot-bar.art
115test_device/dex_bootjars/android/system/framework/arm/boot-bar.oat
116test_device/dex_bootjars/android/system/framework/arm/boot-bar.vdex
117test_device/dex_bootjars/android/system/framework/arm64/boot-foo.art
118test_device/dex_bootjars/android/system/framework/arm64/boot-foo.oat
119test_device/dex_bootjars/android/system/framework/arm64/boot-foo.vdex
120test_device/dex_bootjars/android/system/framework/arm64/boot-bar.art
121test_device/dex_bootjars/android/system/framework/arm64/boot-bar.oat
122test_device/dex_bootjars/android/system/framework/arm64/boot-bar.vdex
123`)
Paul Duffin3451e162021-01-20 15:16:56 +0000124
Paul Duffin94f19632021-04-20 12:40:07 +0100125 // Make sure that the art-bootclasspath-fragment is using the correct configuration.
126 checkBootclasspathFragment(t, result, "art-bootclasspath-fragment", "com.android.art:baz,com.android.art:quuz", `
Paul Duffina1d60252021-01-21 18:13:43 +0000127test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.art
128test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.oat
129test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.vdex
130test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.art
131test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.oat
132test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.vdex
133test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.art
134test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.oat
135test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.vdex
136test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.art
137test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.oat
138test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.vdex
139`)
Paul Duffin3451e162021-01-20 15:16:56 +0000140}
141
Paul Duffin94f19632021-04-20 12:40:07 +0100142func checkBootclasspathFragment(t *testing.T, result *android.TestResult, moduleName string, expectedConfiguredModules string, expectedBootclasspathFragmentFiles string) {
Paul Duffin3451e162021-01-20 15:16:56 +0000143 t.Helper()
144
Paul Duffin7771eba2021-04-23 14:25:28 +0100145 bootclasspathFragment := result.ModuleForTests(moduleName, "android_common").Module().(*java.BootclasspathFragmentModule)
Paul Duffin3451e162021-01-20 15:16:56 +0000146
Paul Duffine946b322021-04-25 23:04:00 +0100147 bootclasspathFragmentInfo := result.ModuleProvider(bootclasspathFragment, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
148 modules := bootclasspathFragmentInfo.Modules()
Paul Duffin34d433a2021-03-09 14:13:25 +0000149 android.AssertStringEquals(t, "invalid modules for "+moduleName, expectedConfiguredModules, modules.String())
Paul Duffina1d60252021-01-21 18:13:43 +0000150
151 // Get a list of all the paths in the boot image sorted by arch type.
152 allPaths := []string{}
Paul Duffine946b322021-04-25 23:04:00 +0100153 bootImageFilesByArchType := bootclasspathFragmentInfo.AndroidBootImageFilesByArchType()
Paul Duffina1d60252021-01-21 18:13:43 +0000154 for _, archType := range android.ArchTypeList() {
155 if paths, ok := bootImageFilesByArchType[archType]; ok {
156 for _, path := range paths {
157 allPaths = append(allPaths, android.NormalizePathForTesting(path))
158 }
159 }
160 }
Paul Duffin3451e162021-01-20 15:16:56 +0000161
Paul Duffin94f19632021-04-20 12:40:07 +0100162 android.AssertTrimmedStringEquals(t, "invalid paths for "+moduleName, expectedBootclasspathFragmentFiles, strings.Join(allPaths, "\n"))
Paul Duffin3451e162021-01-20 15:16:56 +0000163}
Paul Duffina1d60252021-01-21 18:13:43 +0000164
Paul Duffin94f19632021-04-20 12:40:07 +0100165func TestBootclasspathFragmentInArtApex(t *testing.T) {
Paul Duffinba6afd02019-11-19 19:44:10 +0000166 commonPreparer := android.GroupFixturePreparers(
Paul Duffin94f19632021-04-20 12:40:07 +0100167 prepareForTestWithBootclasspathFragment,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000168 prepareForTestWithArtApex,
169
Paul Duffinba6afd02019-11-19 19:44:10 +0000170 android.FixtureWithRootAndroidBp(`
Paul Duffina1d60252021-01-21 18:13:43 +0000171 apex {
Paul Duffin9ea71c02021-03-23 22:53:07 +0000172 name: "com.android.art",
173 key: "com.android.art.key",
Paul Duffin94f19632021-04-20 12:40:07 +0100174 bootclasspath_fragments: [
175 "mybootclasspathfragment",
Paul Duffina1d60252021-01-21 18:13:43 +0000176 ],
Paul Duffin4d101b62021-03-24 15:42:20 +0000177 // bar (like foo) should be transitively included in this apex because it is part of the
Paul Duffin7771eba2021-04-23 14:25:28 +0100178 // mybootclasspathfragment bootclasspath_fragment. However, it is kept here to ensure that the
179 // apex dedups the files correctly.
Paul Duffin9ea71c02021-03-23 22:53:07 +0000180 java_libs: [
Paul Duffin9ea71c02021-03-23 22:53:07 +0000181 "bar",
182 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000183 updatable: false,
Paul Duffina1d60252021-01-21 18:13:43 +0000184 }
185
186 apex_key {
Paul Duffin9ea71c02021-03-23 22:53:07 +0000187 name: "com.android.art.key",
Paul Duffina1d60252021-01-21 18:13:43 +0000188 public_key: "testkey.avbpubkey",
189 private_key: "testkey.pem",
190 }
191
192 java_library {
193 name: "foo",
194 srcs: ["b.java"],
195 installable: true,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000196 apex_available: [
197 "com.android.art",
198 ],
Paul Duffina1d60252021-01-21 18:13:43 +0000199 }
200
201 java_library {
202 name: "bar",
203 srcs: ["b.java"],
204 installable: true,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000205 apex_available: [
206 "com.android.art",
207 ],
Paul Duffina1d60252021-01-21 18:13:43 +0000208 }
209
Paul Duffin65898052021-04-20 22:47:03 +0100210 java_import {
211 name: "foo",
212 jars: ["foo.jar"],
213 apex_available: [
214 "com.android.art",
215 ],
216 }
217
218 java_import {
219 name: "bar",
220 jars: ["bar.jar"],
221 apex_available: [
222 "com.android.art",
223 ],
224 }
Paul Duffinba6afd02019-11-19 19:44:10 +0000225 `),
226 )
Paul Duffin65898052021-04-20 22:47:03 +0100227
Paul Duffinba6afd02019-11-19 19:44:10 +0000228 contentsInsert := func(contents []string) string {
229 insert := ""
230 if contents != nil {
231 insert = fmt.Sprintf(`contents: ["%s"],`, strings.Join(contents, `", "`))
Paul Duffin396229f2021-03-18 18:30:31 +0000232 }
Paul Duffinba6afd02019-11-19 19:44:10 +0000233 return insert
234 }
Paul Duffina1d60252021-01-21 18:13:43 +0000235
Paul Duffinba6afd02019-11-19 19:44:10 +0000236 addSource := func(contents ...string) android.FixturePreparer {
237 text := fmt.Sprintf(`
238 bootclasspath_fragment {
239 name: "mybootclasspathfragment",
240 image_name: "art",
241 %s
242 apex_available: [
243 "com.android.art",
244 ],
245 }
246 `, contentsInsert(contents))
247
248 return android.FixtureAddTextFile("art/build/boot/Android.bp", text)
249 }
250
251 addPrebuilt := func(prefer bool, contents ...string) android.FixturePreparer {
252 text := fmt.Sprintf(`
253 prebuilt_bootclasspath_fragment {
254 name: "mybootclasspathfragment",
255 image_name: "art",
256 %s
257 prefer: %t,
258 apex_available: [
259 "com.android.art",
260 ],
261 }
262 `, contentsInsert(contents), prefer)
263 return android.FixtureAddTextFile("prebuilts/module_sdk/art/Android.bp", text)
264 }
265
266 t.Run("boot image files", func(t *testing.T) {
267 result := android.GroupFixturePreparers(
268 commonPreparer,
269
270 // Configure some libraries in the art bootclasspath_fragment that match the source
271 // bootclasspath_fragment's contents property.
272 java.FixtureConfigureBootJars("com.android.art:foo", "com.android.art:bar"),
273 addSource("foo", "bar"),
274
275 // Make sure that a preferred prebuilt with consistent contents doesn't affect the apex.
276 addPrebuilt(true, "foo", "bar"),
277 ).RunTest(t)
278
279 ensureExactContents(t, result.TestContext, "com.android.art", "android_common_com.android.art_image", []string{
280 "javalib/arm/boot.art",
281 "javalib/arm/boot.oat",
282 "javalib/arm/boot.vdex",
283 "javalib/arm/boot-bar.art",
284 "javalib/arm/boot-bar.oat",
285 "javalib/arm/boot-bar.vdex",
286 "javalib/arm64/boot.art",
287 "javalib/arm64/boot.oat",
288 "javalib/arm64/boot.vdex",
289 "javalib/arm64/boot-bar.art",
290 "javalib/arm64/boot-bar.oat",
291 "javalib/arm64/boot-bar.vdex",
292 "javalib/bar.jar",
293 "javalib/foo.jar",
294 })
295
296 java.CheckModuleDependencies(t, result.TestContext, "com.android.art", "android_common_com.android.art_image", []string{
297 `bar`,
298 `com.android.art.key`,
299 `mybootclasspathfragment`,
300 })
Paul Duffina1d60252021-01-21 18:13:43 +0000301 })
Paul Duffin396229f2021-03-18 18:30:31 +0000302
Paul Duffinba6afd02019-11-19 19:44:10 +0000303 t.Run("source with inconsistency between config and contents", func(t *testing.T) {
304 android.GroupFixturePreparers(
305 commonPreparer,
306
307 // Create an inconsistency between the ArtApexJars configuration and the art source
308 // bootclasspath_fragment module's contents property.
309 java.FixtureConfigureBootJars("com.android.art:foo"),
310 addSource("foo", "bar"),
311 ).
312 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`\QArtApexJars configuration specifies []string{"foo"}, contents property specifies []string{"foo", "bar"}\E`)).
313 RunTest(t)
314 })
315
316 t.Run("prebuilt with inconsistency between config and contents", func(t *testing.T) {
317 android.GroupFixturePreparers(
318 commonPreparer,
319
320 // Create an inconsistency between the ArtApexJars configuration and the art
321 // prebuilt_bootclasspath_fragment module's contents property.
322 java.FixtureConfigureBootJars("com.android.art:foo"),
323 addPrebuilt(false, "foo", "bar"),
324 ).
325 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`\QArtApexJars configuration specifies []string{"foo"}, contents property specifies []string{"foo", "bar"}\E`)).
326 RunTest(t)
327 })
328
329 t.Run("preferred prebuilt with inconsistency between config and contents", func(t *testing.T) {
330 android.GroupFixturePreparers(
331 commonPreparer,
332
333 // Create an inconsistency between the ArtApexJars configuration and the art
334 // prebuilt_bootclasspath_fragment module's contents property.
335 java.FixtureConfigureBootJars("com.android.art:foo"),
336 addPrebuilt(true, "foo", "bar"),
337
338 // Source contents property is consistent with the config.
339 addSource("foo"),
340 ).
341 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`\QArtApexJars configuration specifies []string{"foo"}, contents property specifies []string{"foo", "bar"}\E`)).
342 RunTest(t)
343 })
344
345 t.Run("source preferred and prebuilt with inconsistency between config and contents", func(t *testing.T) {
346 android.GroupFixturePreparers(
347 commonPreparer,
348
349 // Create an inconsistency between the ArtApexJars configuration and the art
350 // prebuilt_bootclasspath_fragment module's contents property.
351 java.FixtureConfigureBootJars("com.android.art:foo"),
352 addPrebuilt(false, "foo", "bar"),
353
354 // Source contents property is consistent with the config.
355 addSource("foo"),
356
357 // This should pass because while the prebuilt is inconsistent with the configuration it is
358 // not actually used.
359 ).RunTest(t)
Paul Duffin396229f2021-03-18 18:30:31 +0000360 })
Paul Duffina1d60252021-01-21 18:13:43 +0000361}
362
Paul Duffin94f19632021-04-20 12:40:07 +0100363func TestBootclasspathFragmentInPrebuiltArtApex(t *testing.T) {
Paul Duffin9ea71c02021-03-23 22:53:07 +0000364 result := android.GroupFixturePreparers(
Paul Duffin94f19632021-04-20 12:40:07 +0100365 prepareForTestWithBootclasspathFragment,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000366 prepareForTestWithArtApex,
367
368 android.FixtureMergeMockFs(android.MockFS{
369 "com.android.art-arm64.apex": nil,
370 "com.android.art-arm.apex": nil,
371 }),
372
Paul Duffin7771eba2021-04-23 14:25:28 +0100373 // Configure some libraries in the art bootclasspath_fragment.
Paul Duffin60264a02021-04-12 20:02:36 +0100374 java.FixtureConfigureBootJars("com.android.art:foo", "com.android.art:bar"),
Paul Duffin9ea71c02021-03-23 22:53:07 +0000375 ).RunTestWithBp(t, `
376 prebuilt_apex {
377 name: "com.android.art",
378 arch: {
379 arm64: {
380 src: "com.android.art-arm64.apex",
381 },
382 arm: {
383 src: "com.android.art-arm.apex",
384 },
385 },
386 exported_java_libs: ["foo", "bar"],
387 }
388
389 java_import {
390 name: "foo",
391 jars: ["foo.jar"],
392 apex_available: [
393 "com.android.art",
394 ],
395 }
396
397 java_import {
398 name: "bar",
399 jars: ["bar.jar"],
400 apex_available: [
401 "com.android.art",
402 ],
403 }
404
Paul Duffin7771eba2021-04-23 14:25:28 +0100405 prebuilt_bootclasspath_fragment {
Paul Duffin94f19632021-04-20 12:40:07 +0100406 name: "mybootclasspathfragment",
Paul Duffin9ea71c02021-03-23 22:53:07 +0000407 image_name: "art",
408 apex_available: [
409 "com.android.art",
410 ],
411 }
412 `)
413
414 java.CheckModuleDependencies(t, result.TestContext, "com.android.art", "android_common", []string{
Paul Duffin11216db2021-03-01 14:14:52 +0000415 `com.android.art.apex.selector`,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000416 `prebuilt_bar`,
417 `prebuilt_foo`,
418 })
419
Paul Duffin94f19632021-04-20 12:40:07 +0100420 java.CheckModuleDependencies(t, result.TestContext, "mybootclasspathfragment", "android_common", []string{
Paul Duffin9ea71c02021-03-23 22:53:07 +0000421 `dex2oatd`,
Paul Duffinc7ef9892021-03-23 23:21:59 +0000422 `prebuilt_bar`,
423 `prebuilt_foo`,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000424 })
425}
426
Paul Duffin94f19632021-04-20 12:40:07 +0100427func TestBootclasspathFragmentContentsNoName(t *testing.T) {
Paul Duffin82886d62021-03-24 01:34:57 +0000428 result := android.GroupFixturePreparers(
Paul Duffin94f19632021-04-20 12:40:07 +0100429 prepareForTestWithBootclasspathFragment,
Paul Duffin82886d62021-03-24 01:34:57 +0000430 prepareForTestWithMyapex,
431 ).RunTestWithBp(t, `
432 apex {
433 name: "myapex",
434 key: "myapex.key",
Paul Duffin94f19632021-04-20 12:40:07 +0100435 bootclasspath_fragments: [
436 "mybootclasspathfragment",
Paul Duffin82886d62021-03-24 01:34:57 +0000437 ],
438 updatable: false,
439 }
440
441 apex_key {
442 name: "myapex.key",
443 public_key: "testkey.avbpubkey",
444 private_key: "testkey.pem",
445 }
446
447 java_library {
448 name: "foo",
449 srcs: ["b.java"],
450 installable: true,
451 apex_available: [
452 "myapex",
453 ],
454 }
455
456 java_library {
457 name: "bar",
458 srcs: ["b.java"],
459 installable: true,
460 apex_available: [
461 "myapex",
462 ],
463 }
464
Paul Duffin7771eba2021-04-23 14:25:28 +0100465 bootclasspath_fragment {
Paul Duffin94f19632021-04-20 12:40:07 +0100466 name: "mybootclasspathfragment",
Paul Duffin82886d62021-03-24 01:34:57 +0000467 contents: [
468 "foo",
469 "bar",
470 ],
471 apex_available: [
472 "myapex",
473 ],
474 }
475 `)
476
Paul Duffin4d101b62021-03-24 15:42:20 +0000477 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
478 // This does not include art, oat or vdex files as they are only included for the art boot
479 // image.
480 "javalib/bar.jar",
481 "javalib/foo.jar",
482 })
Paul Duffin82886d62021-03-24 01:34:57 +0000483
484 java.CheckModuleDependencies(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
485 `myapex.key`,
Paul Duffin94f19632021-04-20 12:40:07 +0100486 `mybootclasspathfragment`,
Paul Duffin82886d62021-03-24 01:34:57 +0000487 })
488}
489
Paul Duffina1d60252021-01-21 18:13:43 +0000490// TODO(b/177892522) - add test for host apex.