blob: 07feb0358ba0692f1f182ac5cf0d9629128c05e9 [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 (
18 "testing"
19
20 "android/soong/android"
21 "android/soong/dexpreopt"
22 "android/soong/java"
23)
24
25// Contains tests for boot_image logic from java/boot_image.go as the ART boot image requires
26// modules from the ART apex.
27
28func TestBootImages(t *testing.T) {
29 ctx, _ := testApex(t, `
30 java_sdk_library {
31 name: "foo",
32 srcs: ["b.java"],
33 unsafe_ignore_missing_latest_api: true,
34 }
35
36 java_library {
37 name: "bar",
38 srcs: ["b.java"],
39 installable: true,
40 }
41
42 apex {
43 name: "com.android.art",
44 key: "com.android.art.key",
45 java_libs: [
46 "baz",
47 "quuz",
48 ],
49 }
50
51 apex_key {
52 name: "com.android.art.key",
53 public_key: "com.android.art.avbpubkey",
54 private_key: "com.android.art.pem",
55 }
56
57 java_library {
58 name: "baz",
59 apex_available: [
60 "com.android.art",
61 ],
62 srcs: ["b.java"],
63 }
64
65 java_library {
66 name: "quuz",
67 apex_available: [
68 "com.android.art",
69 ],
70 srcs: ["b.java"],
71 }
72`,
73 // Configure some libraries in the art and framework boot images.
74 withArtBootImageJars("com.android.art:baz", "com.android.art:quuz"),
75 withFrameworkBootImageJars("platform:foo", "platform:bar"),
76 withFiles(filesForSdkLibrary),
77 // Some additional files needed for the art apex.
78 withFiles(map[string][]byte{
79 "com.android.art.avbpubkey": nil,
80 "com.android.art.pem": nil,
81 "system/sepolicy/apex/com.android.art-file_contexts": nil,
82 }),
83 )
84
85 // Make sure that the framework-boot-image is using the correct configuration.
86 checkBootImage(t, ctx, "framework-boot-image", "platform:foo,platform:bar")
87
88 // Make sure that the art-boot-image is using the correct configuration.
89 checkBootImage(t, ctx, "art-boot-image", "com.android.art:baz,com.android.art:quuz")
90}
91
92func checkBootImage(t *testing.T, ctx *android.TestContext, moduleName string, expectedConfiguredModules string) {
93 t.Helper()
94
95 bootImage := ctx.ModuleForTests(moduleName, "android_common").Module().(*java.BootImageModule)
96
97 bootImageInfo := ctx.ModuleProvider(bootImage, java.BootImageInfoProvider).(java.BootImageInfo)
98 modules := bootImageInfo.Modules()
99 if actual := modules.String(); actual != expectedConfiguredModules {
100 t.Errorf("invalid modules for %s: expected %q, actual %q", moduleName, expectedConfiguredModules, actual)
101 }
102}
103
104func modifyDexpreoptConfig(configModifier func(dexpreoptConfig *dexpreopt.GlobalConfig)) func(fs map[string][]byte, config android.Config) {
105 return func(fs map[string][]byte, config android.Config) {
106 // Initialize the dexpreopt GlobalConfig to an empty structure. This has no effect if it has
107 // already been set.
108 pathCtx := android.PathContextForTesting(config)
109 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
110 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
111
112 // Retrieve the existing configuration and modify it.
113 dexpreoptConfig = dexpreopt.GetGlobalConfig(pathCtx)
114 configModifier(dexpreoptConfig)
115 }
116}
117
118func withArtBootImageJars(bootJars ...string) func(fs map[string][]byte, config android.Config) {
119 return modifyDexpreoptConfig(func(dexpreoptConfig *dexpreopt.GlobalConfig) {
120 dexpreoptConfig.ArtApexJars = android.CreateTestConfiguredJarList(bootJars)
121 })
122}
123
124func withFrameworkBootImageJars(bootJars ...string) func(fs map[string][]byte, config android.Config) {
125 return modifyDexpreoptConfig(func(dexpreoptConfig *dexpreopt.GlobalConfig) {
126 dexpreoptConfig.BootJars = android.CreateTestConfiguredJarList(bootJars)
127 })
128}