blob: ff779e169f5b23600072be198c1595ad8b75efe8 [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 "reflect"
19 "strings"
Paul Duffin3451e162021-01-20 15:16:56 +000020 "testing"
21
22 "android/soong/android"
23 "android/soong/dexpreopt"
24 "android/soong/java"
25)
26
27// Contains tests for boot_image logic from java/boot_image.go as the ART boot image requires
28// modules from the ART apex.
29
30func TestBootImages(t *testing.T) {
31 ctx, _ := testApex(t, `
32 java_sdk_library {
33 name: "foo",
34 srcs: ["b.java"],
35 unsafe_ignore_missing_latest_api: true,
36 }
37
38 java_library {
39 name: "bar",
40 srcs: ["b.java"],
41 installable: true,
42 }
43
44 apex {
45 name: "com.android.art",
46 key: "com.android.art.key",
47 java_libs: [
48 "baz",
49 "quuz",
50 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +000051 updatable: false,
Paul Duffin3451e162021-01-20 15:16:56 +000052 }
53
54 apex_key {
55 name: "com.android.art.key",
56 public_key: "com.android.art.avbpubkey",
57 private_key: "com.android.art.pem",
58 }
59
60 java_library {
61 name: "baz",
62 apex_available: [
63 "com.android.art",
64 ],
65 srcs: ["b.java"],
66 }
67
68 java_library {
69 name: "quuz",
70 apex_available: [
71 "com.android.art",
72 ],
73 srcs: ["b.java"],
74 }
Paul Duffin5bbfef82021-01-30 12:57:26 +000075
76 boot_image {
77 name: "art-boot-image",
78 image_name: "art",
79 }
80
81 boot_image {
82 name: "framework-boot-image",
83 image_name: "boot",
84 }
Paul Duffin3451e162021-01-20 15:16:56 +000085`,
86 // Configure some libraries in the art and framework boot images.
87 withArtBootImageJars("com.android.art:baz", "com.android.art:quuz"),
88 withFrameworkBootImageJars("platform:foo", "platform:bar"),
89 withFiles(filesForSdkLibrary),
90 // Some additional files needed for the art apex.
91 withFiles(map[string][]byte{
92 "com.android.art.avbpubkey": nil,
93 "com.android.art.pem": nil,
94 "system/sepolicy/apex/com.android.art-file_contexts": nil,
95 }),
96 )
97
98 // Make sure that the framework-boot-image is using the correct configuration.
Paul Duffina1d60252021-01-21 18:13:43 +000099 checkBootImage(t, ctx, "framework-boot-image", "platform:foo,platform:bar", `
100test_device/dex_bootjars/android/system/framework/arm/boot-foo.art
101test_device/dex_bootjars/android/system/framework/arm/boot-foo.oat
102test_device/dex_bootjars/android/system/framework/arm/boot-foo.vdex
103test_device/dex_bootjars/android/system/framework/arm/boot-bar.art
104test_device/dex_bootjars/android/system/framework/arm/boot-bar.oat
105test_device/dex_bootjars/android/system/framework/arm/boot-bar.vdex
106test_device/dex_bootjars/android/system/framework/arm64/boot-foo.art
107test_device/dex_bootjars/android/system/framework/arm64/boot-foo.oat
108test_device/dex_bootjars/android/system/framework/arm64/boot-foo.vdex
109test_device/dex_bootjars/android/system/framework/arm64/boot-bar.art
110test_device/dex_bootjars/android/system/framework/arm64/boot-bar.oat
111test_device/dex_bootjars/android/system/framework/arm64/boot-bar.vdex
112`)
Paul Duffin3451e162021-01-20 15:16:56 +0000113
114 // Make sure that the art-boot-image is using the correct configuration.
Paul Duffina1d60252021-01-21 18:13:43 +0000115 checkBootImage(t, ctx, "art-boot-image", "com.android.art:baz,com.android.art:quuz", `
116test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.art
117test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.oat
118test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.vdex
119test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.art
120test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.oat
121test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.vdex
122test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.art
123test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.oat
124test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.vdex
125test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.art
126test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.oat
127test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.vdex
128`)
Paul Duffin3451e162021-01-20 15:16:56 +0000129}
130
Paul Duffina1d60252021-01-21 18:13:43 +0000131func checkBootImage(t *testing.T, ctx *android.TestContext, moduleName string, expectedConfiguredModules string, expectedBootImageFiles string) {
Paul Duffin3451e162021-01-20 15:16:56 +0000132 t.Helper()
133
134 bootImage := ctx.ModuleForTests(moduleName, "android_common").Module().(*java.BootImageModule)
135
136 bootImageInfo := ctx.ModuleProvider(bootImage, java.BootImageInfoProvider).(java.BootImageInfo)
137 modules := bootImageInfo.Modules()
138 if actual := modules.String(); actual != expectedConfiguredModules {
139 t.Errorf("invalid modules for %s: expected %q, actual %q", moduleName, expectedConfiguredModules, actual)
140 }
Paul Duffina1d60252021-01-21 18:13:43 +0000141
142 // Get a list of all the paths in the boot image sorted by arch type.
143 allPaths := []string{}
144 bootImageFilesByArchType := bootImageInfo.AndroidBootImageFilesByArchType()
145 for _, archType := range android.ArchTypeList() {
146 if paths, ok := bootImageFilesByArchType[archType]; ok {
147 for _, path := range paths {
148 allPaths = append(allPaths, android.NormalizePathForTesting(path))
149 }
150 }
151 }
152 if expected, actual := strings.TrimSpace(expectedBootImageFiles), strings.TrimSpace(strings.Join(allPaths, "\n")); !reflect.DeepEqual(expected, actual) {
153 t.Errorf("invalid paths for %s: expected \n%s, actual \n%s", moduleName, expected, actual)
154 }
Paul Duffin3451e162021-01-20 15:16:56 +0000155}
156
157func modifyDexpreoptConfig(configModifier func(dexpreoptConfig *dexpreopt.GlobalConfig)) func(fs map[string][]byte, config android.Config) {
158 return func(fs map[string][]byte, config android.Config) {
159 // Initialize the dexpreopt GlobalConfig to an empty structure. This has no effect if it has
160 // already been set.
161 pathCtx := android.PathContextForTesting(config)
162 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
163 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
164
165 // Retrieve the existing configuration and modify it.
166 dexpreoptConfig = dexpreopt.GetGlobalConfig(pathCtx)
167 configModifier(dexpreoptConfig)
168 }
169}
170
171func withArtBootImageJars(bootJars ...string) func(fs map[string][]byte, config android.Config) {
172 return modifyDexpreoptConfig(func(dexpreoptConfig *dexpreopt.GlobalConfig) {
173 dexpreoptConfig.ArtApexJars = android.CreateTestConfiguredJarList(bootJars)
174 })
175}
176
177func withFrameworkBootImageJars(bootJars ...string) func(fs map[string][]byte, config android.Config) {
178 return modifyDexpreoptConfig(func(dexpreoptConfig *dexpreopt.GlobalConfig) {
179 dexpreoptConfig.BootJars = android.CreateTestConfiguredJarList(bootJars)
180 })
181}
Paul Duffina1d60252021-01-21 18:13:43 +0000182
183func TestBootImageInApex(t *testing.T) {
184 ctx, _ := testApex(t, `
185 apex {
186 name: "myapex",
187 key: "myapex.key",
188 boot_images: [
189 "mybootimage",
190 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000191 updatable: false,
Paul Duffina1d60252021-01-21 18:13:43 +0000192 }
193
194 apex_key {
195 name: "myapex.key",
196 public_key: "testkey.avbpubkey",
197 private_key: "testkey.pem",
198 }
199
200 java_library {
201 name: "foo",
202 srcs: ["b.java"],
203 installable: true,
204 }
205
206 java_library {
207 name: "bar",
208 srcs: ["b.java"],
209 installable: true,
210 }
211
212 boot_image {
213 name: "mybootimage",
214 image_name: "boot",
215 apex_available: [
216 "myapex",
217 ],
218 }
219`,
220 // Configure some libraries in the framework boot image.
221 withFrameworkBootImageJars("platform:foo", "platform:bar"),
222 )
223
224 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
225 "javalib/arm/boot-bar.art",
226 "javalib/arm/boot-bar.oat",
227 "javalib/arm/boot-bar.vdex",
228 "javalib/arm/boot-foo.art",
229 "javalib/arm/boot-foo.oat",
230 "javalib/arm/boot-foo.vdex",
231 "javalib/arm64/boot-bar.art",
232 "javalib/arm64/boot-bar.oat",
233 "javalib/arm64/boot-bar.vdex",
234 "javalib/arm64/boot-foo.art",
235 "javalib/arm64/boot-foo.oat",
236 "javalib/arm64/boot-foo.vdex",
237 })
238}
239
240// TODO(b/177892522) - add test for host apex.