blob: 574166a536af7744eb2e0f17c09445427407cf5d [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 Duffina1d60252021-01-21 18:13:43 +000018 "strings"
Paul Duffin3451e162021-01-20 15:16:56 +000019 "testing"
20
21 "android/soong/android"
22 "android/soong/dexpreopt"
23 "android/soong/java"
24)
25
26// Contains tests for boot_image logic from java/boot_image.go as the ART boot image requires
27// modules from the ART apex.
28
Paul Duffin52bfaa42021-03-23 23:40:12 +000029var prepareForTestWithBootImage = android.GroupFixturePreparers(
30 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 Duffin3451e162021-01-20 15:16:56 +000041func TestBootImages(t *testing.T) {
Paul Duffin52bfaa42021-03-23 23:40:12 +000042 result := android.GroupFixturePreparers(
43 prepareForTestWithBootImage,
Paul Duffin34d433a2021-03-09 14:13:25 +000044 // Configure some libraries in the art and framework boot images.
45 dexpreopt.FixtureSetArtBootJars("com.android.art:baz", "com.android.art:quuz"),
46 dexpreopt.FixtureSetBootJars("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",
66 java_libs: [
67 "baz",
68 "quuz",
69 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +000070 updatable: false,
Paul Duffin3451e162021-01-20 15:16:56 +000071 }
72
73 apex_key {
74 name: "com.android.art.key",
75 public_key: "com.android.art.avbpubkey",
76 private_key: "com.android.art.pem",
77 }
78
79 java_library {
80 name: "baz",
81 apex_available: [
82 "com.android.art",
83 ],
84 srcs: ["b.java"],
85 }
86
87 java_library {
88 name: "quuz",
89 apex_available: [
90 "com.android.art",
91 ],
92 srcs: ["b.java"],
93 }
Paul Duffin5bbfef82021-01-30 12:57:26 +000094
95 boot_image {
96 name: "art-boot-image",
97 image_name: "art",
Paul Duffinc7ef9892021-03-23 23:21:59 +000098 apex_available: [
99 "com.android.art",
100 ],
Paul Duffin5bbfef82021-01-30 12:57:26 +0000101 }
102
103 boot_image {
104 name: "framework-boot-image",
105 image_name: "boot",
106 }
Paul Duffin3451e162021-01-20 15:16:56 +0000107`,
Paul Duffin3451e162021-01-20 15:16:56 +0000108 )
109
110 // Make sure that the framework-boot-image is using the correct configuration.
Paul Duffin34d433a2021-03-09 14:13:25 +0000111 checkBootImage(t, result, "framework-boot-image", "platform:foo,platform:bar", `
Paul Duffina1d60252021-01-21 18:13:43 +0000112test_device/dex_bootjars/android/system/framework/arm/boot-foo.art
113test_device/dex_bootjars/android/system/framework/arm/boot-foo.oat
114test_device/dex_bootjars/android/system/framework/arm/boot-foo.vdex
115test_device/dex_bootjars/android/system/framework/arm/boot-bar.art
116test_device/dex_bootjars/android/system/framework/arm/boot-bar.oat
117test_device/dex_bootjars/android/system/framework/arm/boot-bar.vdex
118test_device/dex_bootjars/android/system/framework/arm64/boot-foo.art
119test_device/dex_bootjars/android/system/framework/arm64/boot-foo.oat
120test_device/dex_bootjars/android/system/framework/arm64/boot-foo.vdex
121test_device/dex_bootjars/android/system/framework/arm64/boot-bar.art
122test_device/dex_bootjars/android/system/framework/arm64/boot-bar.oat
123test_device/dex_bootjars/android/system/framework/arm64/boot-bar.vdex
124`)
Paul Duffin3451e162021-01-20 15:16:56 +0000125
126 // Make sure that the art-boot-image is using the correct configuration.
Paul Duffin34d433a2021-03-09 14:13:25 +0000127 checkBootImage(t, result, "art-boot-image", "com.android.art:baz,com.android.art:quuz", `
Paul Duffina1d60252021-01-21 18:13:43 +0000128test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.art
129test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.oat
130test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.vdex
131test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.art
132test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.oat
133test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.vdex
134test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.art
135test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.oat
136test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.vdex
137test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.art
138test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.oat
139test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.vdex
140`)
Paul Duffin3451e162021-01-20 15:16:56 +0000141}
142
Paul Duffin34d433a2021-03-09 14:13:25 +0000143func checkBootImage(t *testing.T, result *android.TestResult, moduleName string, expectedConfiguredModules string, expectedBootImageFiles string) {
Paul Duffin3451e162021-01-20 15:16:56 +0000144 t.Helper()
145
Paul Duffin34d433a2021-03-09 14:13:25 +0000146 bootImage := result.ModuleForTests(moduleName, "android_common").Module().(*java.BootImageModule)
Paul Duffin3451e162021-01-20 15:16:56 +0000147
Paul Duffin34d433a2021-03-09 14:13:25 +0000148 bootImageInfo := result.ModuleProvider(bootImage, java.BootImageInfoProvider).(java.BootImageInfo)
Paul Duffin3451e162021-01-20 15:16:56 +0000149 modules := bootImageInfo.Modules()
Paul Duffin34d433a2021-03-09 14:13:25 +0000150 android.AssertStringEquals(t, "invalid modules for "+moduleName, expectedConfiguredModules, modules.String())
Paul Duffina1d60252021-01-21 18:13:43 +0000151
152 // Get a list of all the paths in the boot image sorted by arch type.
153 allPaths := []string{}
154 bootImageFilesByArchType := bootImageInfo.AndroidBootImageFilesByArchType()
155 for _, archType := range android.ArchTypeList() {
156 if paths, ok := bootImageFilesByArchType[archType]; ok {
157 for _, path := range paths {
158 allPaths = append(allPaths, android.NormalizePathForTesting(path))
159 }
160 }
161 }
Paul Duffin3451e162021-01-20 15:16:56 +0000162
Paul Duffin34d433a2021-03-09 14:13:25 +0000163 android.AssertTrimmedStringEquals(t, "invalid paths for "+moduleName, expectedBootImageFiles, strings.Join(allPaths, "\n"))
Paul Duffin3451e162021-01-20 15:16:56 +0000164}
Paul Duffina1d60252021-01-21 18:13:43 +0000165
Paul Duffin9ea71c02021-03-23 22:53:07 +0000166func TestBootImageInArtApex(t *testing.T) {
Paul Duffin52bfaa42021-03-23 23:40:12 +0000167 result := android.GroupFixturePreparers(
168 prepareForTestWithBootImage,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000169 prepareForTestWithArtApex,
170
171 // Configure some libraries in the art boot image.
172 dexpreopt.FixtureSetArtBootJars("com.android.art:foo", "com.android.art:bar"),
Paul Duffin34d433a2021-03-09 14:13:25 +0000173 ).RunTestWithBp(t, `
Paul Duffina1d60252021-01-21 18:13:43 +0000174 apex {
Paul Duffin9ea71c02021-03-23 22:53:07 +0000175 name: "com.android.art",
176 key: "com.android.art.key",
Paul Duffina1d60252021-01-21 18:13:43 +0000177 boot_images: [
178 "mybootimage",
179 ],
Paul Duffin4d101b62021-03-24 15:42:20 +0000180 // bar (like foo) should be transitively included in this apex because it is part of the
181 // mybootimage boot_image. However, it is kept here to ensure that the apex dedups the files
182 // correctly.
Paul Duffin9ea71c02021-03-23 22:53:07 +0000183 java_libs: [
Paul Duffin9ea71c02021-03-23 22:53:07 +0000184 "bar",
185 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000186 updatable: false,
Paul Duffina1d60252021-01-21 18:13:43 +0000187 }
188
189 apex_key {
Paul Duffin9ea71c02021-03-23 22:53:07 +0000190 name: "com.android.art.key",
Paul Duffina1d60252021-01-21 18:13:43 +0000191 public_key: "testkey.avbpubkey",
192 private_key: "testkey.pem",
193 }
194
195 java_library {
196 name: "foo",
197 srcs: ["b.java"],
198 installable: true,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000199 apex_available: [
200 "com.android.art",
201 ],
Paul Duffina1d60252021-01-21 18:13:43 +0000202 }
203
204 java_library {
205 name: "bar",
206 srcs: ["b.java"],
207 installable: true,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000208 apex_available: [
209 "com.android.art",
210 ],
Paul Duffina1d60252021-01-21 18:13:43 +0000211 }
212
213 boot_image {
214 name: "mybootimage",
Paul Duffin9ea71c02021-03-23 22:53:07 +0000215 image_name: "art",
Paul Duffina1d60252021-01-21 18:13:43 +0000216 apex_available: [
Paul Duffin9ea71c02021-03-23 22:53:07 +0000217 "com.android.art",
Paul Duffina1d60252021-01-21 18:13:43 +0000218 ],
219 }
Paul Duffin396229f2021-03-18 18:30:31 +0000220
221 // Make sure that a preferred prebuilt doesn't affect the apex.
222 prebuilt_boot_image {
223 name: "mybootimage",
Paul Duffin9ea71c02021-03-23 22:53:07 +0000224 image_name: "art",
Paul Duffin396229f2021-03-18 18:30:31 +0000225 prefer: true,
226 apex_available: [
Paul Duffin9ea71c02021-03-23 22:53:07 +0000227 "com.android.art",
Paul Duffin396229f2021-03-18 18:30:31 +0000228 ],
229 }
Paul Duffin34d433a2021-03-09 14:13:25 +0000230 `)
Paul Duffina1d60252021-01-21 18:13:43 +0000231
Paul Duffin9ea71c02021-03-23 22:53:07 +0000232 ensureExactContents(t, result.TestContext, "com.android.art", "android_common_com.android.art_image", []string{
233 "javalib/arm/boot.art",
234 "javalib/arm/boot.oat",
235 "javalib/arm/boot.vdex",
Paul Duffina1d60252021-01-21 18:13:43 +0000236 "javalib/arm/boot-bar.art",
237 "javalib/arm/boot-bar.oat",
238 "javalib/arm/boot-bar.vdex",
Paul Duffin9ea71c02021-03-23 22:53:07 +0000239 "javalib/arm64/boot.art",
240 "javalib/arm64/boot.oat",
241 "javalib/arm64/boot.vdex",
Paul Duffina1d60252021-01-21 18:13:43 +0000242 "javalib/arm64/boot-bar.art",
243 "javalib/arm64/boot-bar.oat",
244 "javalib/arm64/boot-bar.vdex",
Paul Duffin9ea71c02021-03-23 22:53:07 +0000245 "javalib/bar.jar",
246 "javalib/foo.jar",
Paul Duffina1d60252021-01-21 18:13:43 +0000247 })
Paul Duffin396229f2021-03-18 18:30:31 +0000248
Paul Duffin9ea71c02021-03-23 22:53:07 +0000249 java.CheckModuleDependencies(t, result.TestContext, "com.android.art", "android_common_com.android.art_image", []string{
250 `bar`,
251 `com.android.art.key`,
Paul Duffin396229f2021-03-18 18:30:31 +0000252 `mybootimage`,
253 })
Paul Duffina1d60252021-01-21 18:13:43 +0000254}
255
Paul Duffin9ea71c02021-03-23 22:53:07 +0000256func TestBootImageInPrebuiltArtApex(t *testing.T) {
257 result := android.GroupFixturePreparers(
258 prepareForTestWithBootImage,
259 prepareForTestWithArtApex,
260
261 android.FixtureMergeMockFs(android.MockFS{
262 "com.android.art-arm64.apex": nil,
263 "com.android.art-arm.apex": nil,
264 }),
265
266 // Configure some libraries in the art boot image.
267 dexpreopt.FixtureSetArtBootJars("com.android.art:foo", "com.android.art:bar"),
268 ).RunTestWithBp(t, `
269 prebuilt_apex {
270 name: "com.android.art",
271 arch: {
272 arm64: {
273 src: "com.android.art-arm64.apex",
274 },
275 arm: {
276 src: "com.android.art-arm.apex",
277 },
278 },
279 exported_java_libs: ["foo", "bar"],
280 }
281
282 java_import {
283 name: "foo",
284 jars: ["foo.jar"],
285 apex_available: [
286 "com.android.art",
287 ],
288 }
289
290 java_import {
291 name: "bar",
292 jars: ["bar.jar"],
293 apex_available: [
294 "com.android.art",
295 ],
296 }
297
298 prebuilt_boot_image {
299 name: "mybootimage",
300 image_name: "art",
301 apex_available: [
302 "com.android.art",
303 ],
304 }
305 `)
306
307 java.CheckModuleDependencies(t, result.TestContext, "com.android.art", "android_common", []string{
308 `prebuilt_bar`,
309 `prebuilt_foo`,
310 })
311
312 java.CheckModuleDependencies(t, result.TestContext, "mybootimage", "android_common", []string{
313 `dex2oatd`,
Paul Duffinc7ef9892021-03-23 23:21:59 +0000314 `prebuilt_bar`,
315 `prebuilt_foo`,
Paul Duffin9ea71c02021-03-23 22:53:07 +0000316 })
317}
318
Paul Duffin82886d62021-03-24 01:34:57 +0000319func TestBootImageContentsNoName(t *testing.T) {
320 result := android.GroupFixturePreparers(
321 prepareForTestWithBootImage,
322 prepareForTestWithMyapex,
323 ).RunTestWithBp(t, `
324 apex {
325 name: "myapex",
326 key: "myapex.key",
327 boot_images: [
328 "mybootimage",
329 ],
330 updatable: false,
331 }
332
333 apex_key {
334 name: "myapex.key",
335 public_key: "testkey.avbpubkey",
336 private_key: "testkey.pem",
337 }
338
339 java_library {
340 name: "foo",
341 srcs: ["b.java"],
342 installable: true,
343 apex_available: [
344 "myapex",
345 ],
346 }
347
348 java_library {
349 name: "bar",
350 srcs: ["b.java"],
351 installable: true,
352 apex_available: [
353 "myapex",
354 ],
355 }
356
357 boot_image {
358 name: "mybootimage",
359 contents: [
360 "foo",
361 "bar",
362 ],
363 apex_available: [
364 "myapex",
365 ],
366 }
367 `)
368
Paul Duffin4d101b62021-03-24 15:42:20 +0000369 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
370 // This does not include art, oat or vdex files as they are only included for the art boot
371 // image.
372 "javalib/bar.jar",
373 "javalib/foo.jar",
374 })
Paul Duffin82886d62021-03-24 01:34:57 +0000375
376 java.CheckModuleDependencies(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
377 `myapex.key`,
378 `mybootimage`,
379 })
380}
381
Paul Duffina1d60252021-01-21 18:13:43 +0000382// TODO(b/177892522) - add test for host apex.