Paul Duffin | b432df9 | 2021-03-22 22:09:42 +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 ( |
| 18 | "testing" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | "android/soong/dexpreopt" |
| 22 | "android/soong/java" |
| 23 | "github.com/google/blueprint" |
| 24 | ) |
| 25 | |
| 26 | // Contains tests for platform_bootclasspath logic from java/platform_bootclasspath.go that requires |
| 27 | // apexes. |
| 28 | |
| 29 | var prepareForTestWithPlatformBootclasspath = android.GroupFixturePreparers( |
| 30 | java.PrepareForTestWithDexpreopt, |
| 31 | PrepareForTestWithApexBuildComponents, |
| 32 | ) |
| 33 | |
| 34 | func TestPlatformBootclasspathDependencies(t *testing.T) { |
| 35 | result := android.GroupFixturePreparers( |
| 36 | prepareForTestWithPlatformBootclasspath, |
| 37 | prepareForTestWithArtApex, |
| 38 | prepareForTestWithMyapex, |
| 39 | // Configure some libraries in the art and framework boot images. |
| 40 | dexpreopt.FixtureSetArtBootJars("com.android.art:baz", "com.android.art:quuz"), |
| 41 | dexpreopt.FixtureSetBootJars("platform:foo"), |
| 42 | dexpreopt.FixtureSetUpdatableBootJars("myapex:bar"), |
| 43 | java.PrepareForTestWithJavaSdkLibraryFiles, |
| 44 | java.FixtureWithLastReleaseApis("foo"), |
| 45 | ).RunTestWithBp(t, ` |
| 46 | apex { |
| 47 | name: "com.android.art", |
| 48 | key: "com.android.art.key", |
| 49 | bootclasspath_fragments: [ |
| 50 | "art-bootclasspath-fragment", |
| 51 | ], |
| 52 | updatable: false, |
| 53 | } |
| 54 | |
| 55 | apex_key { |
| 56 | name: "com.android.art.key", |
| 57 | public_key: "com.android.art.avbpubkey", |
| 58 | private_key: "com.android.art.pem", |
| 59 | } |
| 60 | |
| 61 | bootclasspath_fragment { |
| 62 | name: "art-bootclasspath-fragment", |
| 63 | apex_available: [ |
| 64 | "com.android.art", |
| 65 | ], |
| 66 | contents: [ |
| 67 | "baz", |
| 68 | "quuz", |
| 69 | ], |
| 70 | } |
| 71 | |
| 72 | java_library { |
| 73 | name: "baz", |
| 74 | apex_available: [ |
| 75 | "com.android.art", |
| 76 | ], |
| 77 | srcs: ["b.java"], |
| 78 | installable: true, |
| 79 | } |
| 80 | |
| 81 | // Add a java_import that is not preferred and so won't have an appropriate apex variant created |
| 82 | // for it to make sure that the platform_bootclasspath doesn't try and add a dependency onto it. |
| 83 | java_import { |
| 84 | name: "baz", |
| 85 | apex_available: [ |
| 86 | "com.android.art", |
| 87 | ], |
| 88 | jars: ["b.jar"], |
| 89 | } |
| 90 | |
| 91 | java_library { |
| 92 | name: "quuz", |
| 93 | apex_available: [ |
| 94 | "com.android.art", |
| 95 | ], |
| 96 | srcs: ["b.java"], |
| 97 | installable: true, |
| 98 | } |
| 99 | |
| 100 | apex { |
| 101 | name: "myapex", |
| 102 | key: "myapex.key", |
| 103 | java_libs: [ |
| 104 | "bar", |
| 105 | ], |
| 106 | updatable: false, |
| 107 | } |
| 108 | |
| 109 | apex_key { |
| 110 | name: "myapex.key", |
| 111 | public_key: "testkey.avbpubkey", |
| 112 | private_key: "testkey.pem", |
| 113 | } |
| 114 | |
| 115 | java_sdk_library { |
| 116 | name: "foo", |
| 117 | srcs: ["b.java"], |
| 118 | } |
| 119 | |
| 120 | java_library { |
| 121 | name: "bar", |
| 122 | srcs: ["b.java"], |
| 123 | installable: true, |
| 124 | apex_available: ["myapex"], |
| 125 | permitted_packages: ["bar"], |
| 126 | } |
| 127 | |
| 128 | platform_bootclasspath { |
| 129 | name: "myplatform-bootclasspath", |
| 130 | } |
| 131 | `, |
| 132 | ) |
| 133 | |
| 134 | // Make sure that the myplatform-bootclasspath has the correct dependencies. |
| 135 | CheckModuleDependencies(t, result.TestContext, "myplatform-bootclasspath", "android_common", []string{ |
| 136 | `platform:dex2oatd`, |
| 137 | `com.android.art:baz`, |
| 138 | `com.android.art:quuz`, |
| 139 | `platform:foo`, |
| 140 | `myapex:bar`, |
| 141 | }) |
| 142 | } |
| 143 | |
| 144 | // CheckModuleDependencies checks the dependencies of the selected module against the expected list. |
| 145 | // |
| 146 | // The expected list must be a list of strings of the form "<apex>:<module>", where <apex> is the |
| 147 | // name of the apex, or platform is it is not part of an apex and <module> is the module name. |
| 148 | func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) { |
| 149 | t.Helper() |
| 150 | module := ctx.ModuleForTests(name, variant).Module() |
| 151 | modules := []android.Module{} |
| 152 | ctx.VisitDirectDeps(module, func(m blueprint.Module) { |
| 153 | modules = append(modules, m.(android.Module)) |
| 154 | }) |
| 155 | |
| 156 | pairs := java.ApexNamePairsFromModules(ctx, modules) |
| 157 | android.AssertDeepEquals(t, "module dependencies", expected, pairs) |
| 158 | } |