Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 1 | // Copyright 2022 Google Inc. All rights reserved. |
| 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 | package apex |
| 15 | |
| 16 | import ( |
| 17 | "android/soong/android" |
| 18 | "android/soong/bazel/cquery" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func TestApexImageInMixedBuilds(t *testing.T) { |
| 24 | bp := ` |
| 25 | apex_key{ |
| 26 | name: "foo_key", |
| 27 | } |
| 28 | apex { |
| 29 | name: "foo", |
| 30 | key: "foo_key", |
| 31 | updatable: true, |
| 32 | min_sdk_version: "31", |
| 33 | file_contexts: ":myapex-file_contexts", |
| 34 | bazel_module: { label: "//:foo" }, |
| 35 | }` |
| 36 | |
| 37 | outputBaseDir := "out/bazel" |
| 38 | result := android.GroupFixturePreparers( |
| 39 | prepareForApexTest, |
| 40 | android.FixtureModifyConfig(func(config android.Config) { |
| 41 | config.BazelContext = android.MockBazelContext{ |
| 42 | OutputBaseDir: outputBaseDir, |
| 43 | LabelToApexInfo: map[string]cquery.ApexInfo{ |
| 44 | "//:foo": cquery.ApexInfo{ |
| 45 | SignedOutput: "signed_out.apex", |
| 46 | UnsignedOutput: "unsigned_out.apex", |
| 47 | BundleKeyInfo: []string{"public_key", "private_key"}, |
| 48 | ContainerKeyInfo: []string{"container_cert", "container_private"}, |
| 49 | SymbolsUsedByApex: "foo_using.txt", |
| 50 | JavaSymbolsUsedByApex: "foo_using.xml", |
| 51 | BundleFile: "apex_bundle.zip", |
| 52 | InstalledFiles: "installed-files.txt", |
| 53 | RequiresLibs: []string{"//path/c:c", "//path/d:d"}, |
| 54 | |
| 55 | // unused |
| 56 | PackageName: "pkg_name", |
| 57 | ProvidesLibs: []string{"a", "b"}, |
| 58 | }, |
| 59 | }, |
| 60 | } |
| 61 | }), |
| 62 | ).RunTestWithBp(t, bp) |
| 63 | |
| 64 | m := result.ModuleForTests("foo", "android_common_foo_image").Module() |
| 65 | ab, ok := m.(*apexBundle) |
| 66 | if !ok { |
| 67 | t.Fatalf("Expected module to be an apexBundle, was not") |
| 68 | } |
| 69 | |
| 70 | if w, g := "out/bazel/execroot/__main__/public_key", ab.publicKeyFile.String(); w != g { |
| 71 | t.Errorf("Expected public key %q, got %q", w, g) |
| 72 | } |
| 73 | |
| 74 | if w, g := "out/bazel/execroot/__main__/private_key", ab.privateKeyFile.String(); w != g { |
| 75 | t.Errorf("Expected private key %q, got %q", w, g) |
| 76 | } |
| 77 | |
| 78 | if w, g := "out/bazel/execroot/__main__/container_cert", ab.containerCertificateFile.String(); w != g { |
| 79 | t.Errorf("Expected public container key %q, got %q", w, g) |
| 80 | } |
| 81 | |
| 82 | if w, g := "out/bazel/execroot/__main__/container_private", ab.containerPrivateKeyFile.String(); w != g { |
| 83 | t.Errorf("Expected private container key %q, got %q", w, g) |
| 84 | } |
| 85 | |
| 86 | if w, g := "out/bazel/execroot/__main__/signed_out.apex", ab.outputFile.String(); w != g { |
| 87 | t.Errorf("Expected output file %q, got %q", w, g) |
| 88 | } |
| 89 | |
| 90 | if w, g := "out/bazel/execroot/__main__/foo_using.txt", ab.nativeApisUsedByModuleFile.String(); w != g { |
| 91 | t.Errorf("Expected output file %q, got %q", w, g) |
| 92 | } |
| 93 | |
| 94 | if w, g := "out/bazel/execroot/__main__/foo_using.xml", ab.javaApisUsedByModuleFile.String(); w != g { |
| 95 | t.Errorf("Expected output file %q, got %q", w, g) |
| 96 | } |
| 97 | |
| 98 | if w, g := "out/bazel/execroot/__main__/installed-files.txt", ab.installedFilesFile.String(); w != g { |
| 99 | t.Errorf("Expected installed-files.txt %q, got %q", w, g) |
| 100 | } |
| 101 | |
| 102 | mkData := android.AndroidMkDataForTest(t, result.TestContext, m) |
| 103 | var builder strings.Builder |
| 104 | mkData.Custom(&builder, "foo", "BAZEL_TARGET_", "", mkData) |
| 105 | |
| 106 | data := builder.String() |
| 107 | if w := "ALL_MODULES.$(my_register_name).BUNDLE := out/bazel/execroot/__main__/apex_bundle.zip"; !strings.Contains(data, w) { |
| 108 | t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) |
| 109 | } |
| 110 | if w := "$(call dist-for-goals,checkbuild,out/bazel/execroot/__main__/installed-files.txt:foo-installed-files.txt)"; !strings.Contains(data, w) { |
| 111 | t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) |
| 112 | } |
| 113 | if w := "LOCAL_REQUIRED_MODULES := c d"; !strings.Contains(data, w) { |
| 114 | t.Errorf("Expected %q in androidmk data, but did not find it in %q", w, data) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestOverrideApexImageInMixedBuilds(t *testing.T) { |
| 119 | bp := ` |
| 120 | apex_key{ |
| 121 | name: "foo_key", |
| 122 | } |
| 123 | apex_key{ |
| 124 | name: "override_foo_key", |
| 125 | } |
| 126 | apex { |
| 127 | name: "foo", |
| 128 | key: "foo_key", |
| 129 | updatable: true, |
| 130 | min_sdk_version: "31", |
| 131 | package_name: "pkg_name", |
| 132 | file_contexts: ":myapex-file_contexts", |
| 133 | bazel_module: { label: "//:foo" }, |
| 134 | } |
| 135 | override_apex { |
| 136 | name: "override_foo", |
| 137 | key: "override_foo_key", |
| 138 | package_name: "override_pkg_name", |
| 139 | base: "foo", |
| 140 | bazel_module: { label: "//:override_foo" }, |
| 141 | } |
| 142 | ` |
| 143 | |
| 144 | outputBaseDir := "out/bazel" |
| 145 | result := android.GroupFixturePreparers( |
| 146 | prepareForApexTest, |
| 147 | android.FixtureModifyConfig(func(config android.Config) { |
| 148 | config.BazelContext = android.MockBazelContext{ |
| 149 | OutputBaseDir: outputBaseDir, |
| 150 | LabelToApexInfo: map[string]cquery.ApexInfo{ |
| 151 | "//:foo": cquery.ApexInfo{ |
| 152 | SignedOutput: "signed_out.apex", |
| 153 | UnsignedOutput: "unsigned_out.apex", |
| 154 | BundleKeyInfo: []string{"public_key", "private_key"}, |
| 155 | ContainerKeyInfo: []string{"container_cert", "container_private"}, |
| 156 | SymbolsUsedByApex: "foo_using.txt", |
| 157 | JavaSymbolsUsedByApex: "foo_using.xml", |
| 158 | BundleFile: "apex_bundle.zip", |
| 159 | InstalledFiles: "installed-files.txt", |
| 160 | RequiresLibs: []string{"//path/c:c", "//path/d:d"}, |
| 161 | |
| 162 | // unused |
| 163 | PackageName: "pkg_name", |
| 164 | ProvidesLibs: []string{"a", "b"}, |
| 165 | }, |
| 166 | "//:override_foo": cquery.ApexInfo{ |
| 167 | SignedOutput: "override_signed_out.apex", |
| 168 | UnsignedOutput: "override_unsigned_out.apex", |
| 169 | BundleKeyInfo: []string{"override_public_key", "override_private_key"}, |
| 170 | ContainerKeyInfo: []string{"override_container_cert", "override_container_private"}, |
| 171 | SymbolsUsedByApex: "override_foo_using.txt", |
| 172 | JavaSymbolsUsedByApex: "override_foo_using.xml", |
| 173 | BundleFile: "override_apex_bundle.zip", |
| 174 | InstalledFiles: "override_installed-files.txt", |
| 175 | RequiresLibs: []string{"//path/c:c", "//path/d:d"}, |
| 176 | |
| 177 | // unused |
| 178 | PackageName: "override_pkg_name", |
| 179 | ProvidesLibs: []string{"a", "b"}, |
| 180 | }, |
| 181 | }, |
| 182 | } |
| 183 | }), |
| 184 | ).RunTestWithBp(t, bp) |
| 185 | |
| 186 | m := result.ModuleForTests("foo", "android_common_override_foo_foo_image").Module() |
| 187 | ab, ok := m.(*apexBundle) |
| 188 | if !ok { |
| 189 | t.Fatalf("Expected module to be an apexBundle, was not") |
| 190 | } |
| 191 | |
| 192 | if w, g := "out/bazel/execroot/__main__/override_public_key", ab.publicKeyFile.String(); w != g { |
| 193 | t.Errorf("Expected public key %q, got %q", w, g) |
| 194 | } |
| 195 | |
| 196 | if w, g := "out/bazel/execroot/__main__/override_private_key", ab.privateKeyFile.String(); w != g { |
| 197 | t.Errorf("Expected private key %q, got %q", w, g) |
| 198 | } |
| 199 | |
| 200 | if w, g := "out/bazel/execroot/__main__/override_container_cert", ab.containerCertificateFile.String(); w != g { |
| 201 | t.Errorf("Expected public container key %q, got %q", w, g) |
| 202 | } |
| 203 | |
| 204 | if w, g := "out/bazel/execroot/__main__/override_container_private", ab.containerPrivateKeyFile.String(); w != g { |
| 205 | t.Errorf("Expected private container key %q, got %q", w, g) |
| 206 | } |
| 207 | |
| 208 | if w, g := "out/bazel/execroot/__main__/override_signed_out.apex", ab.outputFile.String(); w != g { |
| 209 | t.Errorf("Expected output file %q, got %q", w, g) |
| 210 | } |
| 211 | |
| 212 | if w, g := "out/bazel/execroot/__main__/override_foo_using.txt", ab.nativeApisUsedByModuleFile.String(); w != g { |
| 213 | t.Errorf("Expected output file %q, got %q", w, g) |
| 214 | } |
| 215 | |
| 216 | if w, g := "out/bazel/execroot/__main__/override_foo_using.xml", ab.javaApisUsedByModuleFile.String(); w != g { |
| 217 | t.Errorf("Expected output file %q, got %q", w, g) |
| 218 | } |
| 219 | |
| 220 | if w, g := "out/bazel/execroot/__main__/override_installed-files.txt", ab.installedFilesFile.String(); w != g { |
| 221 | t.Errorf("Expected installed-files.txt %q, got %q", w, g) |
| 222 | } |
| 223 | |
| 224 | mkData := android.AndroidMkDataForTest(t, result.TestContext, m) |
| 225 | var builder strings.Builder |
| 226 | mkData.Custom(&builder, "override_foo", "BAZEL_TARGET_", "", mkData) |
| 227 | |
| 228 | data := builder.String() |
| 229 | if w := "ALL_MODULES.$(my_register_name).BUNDLE := out/bazel/execroot/__main__/override_apex_bundle.zip"; !strings.Contains(data, w) { |
| 230 | t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) |
| 231 | } |
| 232 | if w := "$(call dist-for-goals,checkbuild,out/bazel/execroot/__main__/override_installed-files.txt:override_foo-installed-files.txt)"; !strings.Contains(data, w) { |
| 233 | t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) |
| 234 | } |
| 235 | if w := "LOCAL_REQUIRED_MODULES := c d"; !strings.Contains(data, w) { |
| 236 | t.Errorf("Expected %q in androidmk data, but did not find it in %q", w, data) |
| 237 | } |
| 238 | } |