Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package apex |
| 16 | |
| 17 | import ( |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 18 | "reflect" |
| 19 | "strings" |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 20 | "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 | |
| 30 | func 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 Inwood | f8dcf5e | 2021-02-16 11:40:16 +0000 | [diff] [blame^] | 51 | updatable: false, |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 52 | } |
| 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 Duffin | 5bbfef8 | 2021-01-30 12:57:26 +0000 | [diff] [blame] | 75 | |
| 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 Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 85 | `, |
| 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 Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 99 | checkBootImage(t, ctx, "framework-boot-image", "platform:foo,platform:bar", ` |
| 100 | test_device/dex_bootjars/android/system/framework/arm/boot-foo.art |
| 101 | test_device/dex_bootjars/android/system/framework/arm/boot-foo.oat |
| 102 | test_device/dex_bootjars/android/system/framework/arm/boot-foo.vdex |
| 103 | test_device/dex_bootjars/android/system/framework/arm/boot-bar.art |
| 104 | test_device/dex_bootjars/android/system/framework/arm/boot-bar.oat |
| 105 | test_device/dex_bootjars/android/system/framework/arm/boot-bar.vdex |
| 106 | test_device/dex_bootjars/android/system/framework/arm64/boot-foo.art |
| 107 | test_device/dex_bootjars/android/system/framework/arm64/boot-foo.oat |
| 108 | test_device/dex_bootjars/android/system/framework/arm64/boot-foo.vdex |
| 109 | test_device/dex_bootjars/android/system/framework/arm64/boot-bar.art |
| 110 | test_device/dex_bootjars/android/system/framework/arm64/boot-bar.oat |
| 111 | test_device/dex_bootjars/android/system/framework/arm64/boot-bar.vdex |
| 112 | `) |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 113 | |
| 114 | // Make sure that the art-boot-image is using the correct configuration. |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 115 | checkBootImage(t, ctx, "art-boot-image", "com.android.art:baz,com.android.art:quuz", ` |
| 116 | test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.art |
| 117 | test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.oat |
| 118 | test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.vdex |
| 119 | test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.art |
| 120 | test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.oat |
| 121 | test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.vdex |
| 122 | test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.art |
| 123 | test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.oat |
| 124 | test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.vdex |
| 125 | test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.art |
| 126 | test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.oat |
| 127 | test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.vdex |
| 128 | `) |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 131 | func checkBootImage(t *testing.T, ctx *android.TestContext, moduleName string, expectedConfiguredModules string, expectedBootImageFiles string) { |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 132 | 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 Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 141 | |
| 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 Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | func 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 | |
| 171 | func 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 | |
| 177 | func 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 Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 182 | |
| 183 | func TestBootImageInApex(t *testing.T) { |
| 184 | ctx, _ := testApex(t, ` |
| 185 | apex { |
| 186 | name: "myapex", |
| 187 | key: "myapex.key", |
| 188 | boot_images: [ |
| 189 | "mybootimage", |
| 190 | ], |
Mathew Inwood | f8dcf5e | 2021-02-16 11:40:16 +0000 | [diff] [blame^] | 191 | updatable: false, |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 192 | } |
| 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. |