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{ |
Jingwen Chen | 94098e8 | 2023-01-10 14:50:42 +0000 | [diff] [blame] | 45 | SignedOutput: "signed_out.apex", |
| 46 | SignedCompressedOutput: "signed_out.capex", |
| 47 | UnsignedOutput: "unsigned_out.apex", |
| 48 | BundleKeyInfo: []string{"public_key", "private_key"}, |
| 49 | ContainerKeyInfo: []string{"container_cert", "container_private"}, |
| 50 | SymbolsUsedByApex: "foo_using.txt", |
| 51 | JavaSymbolsUsedByApex: "foo_using.xml", |
| 52 | BundleFile: "apex_bundle.zip", |
| 53 | InstalledFiles: "installed-files.txt", |
| 54 | RequiresLibs: []string{"//path/c:c", "//path/d:d"}, |
Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 55 | |
| 56 | // unused |
| 57 | PackageName: "pkg_name", |
| 58 | ProvidesLibs: []string{"a", "b"}, |
| 59 | }, |
| 60 | }, |
| 61 | } |
| 62 | }), |
| 63 | ).RunTestWithBp(t, bp) |
| 64 | |
| 65 | m := result.ModuleForTests("foo", "android_common_foo_image").Module() |
| 66 | ab, ok := m.(*apexBundle) |
| 67 | if !ok { |
| 68 | t.Fatalf("Expected module to be an apexBundle, was not") |
| 69 | } |
| 70 | |
| 71 | if w, g := "out/bazel/execroot/__main__/public_key", ab.publicKeyFile.String(); w != g { |
| 72 | t.Errorf("Expected public key %q, got %q", w, g) |
| 73 | } |
| 74 | |
| 75 | if w, g := "out/bazel/execroot/__main__/private_key", ab.privateKeyFile.String(); w != g { |
| 76 | t.Errorf("Expected private key %q, got %q", w, g) |
| 77 | } |
| 78 | |
| 79 | if w, g := "out/bazel/execroot/__main__/container_cert", ab.containerCertificateFile.String(); w != g { |
| 80 | t.Errorf("Expected public container key %q, got %q", w, g) |
| 81 | } |
| 82 | |
| 83 | if w, g := "out/bazel/execroot/__main__/container_private", ab.containerPrivateKeyFile.String(); w != g { |
| 84 | t.Errorf("Expected private container key %q, got %q", w, g) |
| 85 | } |
| 86 | |
| 87 | if w, g := "out/bazel/execroot/__main__/signed_out.apex", ab.outputFile.String(); w != g { |
| 88 | t.Errorf("Expected output file %q, got %q", w, g) |
| 89 | } |
| 90 | |
| 91 | if w, g := "out/bazel/execroot/__main__/foo_using.txt", ab.nativeApisUsedByModuleFile.String(); w != g { |
| 92 | t.Errorf("Expected output file %q, got %q", w, g) |
| 93 | } |
| 94 | |
| 95 | if w, g := "out/bazel/execroot/__main__/foo_using.xml", ab.javaApisUsedByModuleFile.String(); w != g { |
| 96 | t.Errorf("Expected output file %q, got %q", w, g) |
| 97 | } |
| 98 | |
| 99 | if w, g := "out/bazel/execroot/__main__/installed-files.txt", ab.installedFilesFile.String(); w != g { |
| 100 | t.Errorf("Expected installed-files.txt %q, got %q", w, g) |
| 101 | } |
| 102 | |
| 103 | mkData := android.AndroidMkDataForTest(t, result.TestContext, m) |
| 104 | var builder strings.Builder |
| 105 | mkData.Custom(&builder, "foo", "BAZEL_TARGET_", "", mkData) |
| 106 | |
| 107 | data := builder.String() |
| 108 | if w := "ALL_MODULES.$(my_register_name).BUNDLE := out/bazel/execroot/__main__/apex_bundle.zip"; !strings.Contains(data, w) { |
| 109 | t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) |
| 110 | } |
| 111 | if w := "$(call dist-for-goals,checkbuild,out/bazel/execroot/__main__/installed-files.txt:foo-installed-files.txt)"; !strings.Contains(data, w) { |
| 112 | t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) |
| 113 | } |
| 114 | if w := "LOCAL_REQUIRED_MODULES := c d"; !strings.Contains(data, w) { |
| 115 | t.Errorf("Expected %q in androidmk data, but did not find it in %q", w, data) |
| 116 | } |
| 117 | } |
| 118 | |
Jingwen Chen | 94098e8 | 2023-01-10 14:50:42 +0000 | [diff] [blame] | 119 | func TestCompressedApexImageInMixedBuilds(t *testing.T) { |
| 120 | bp := ` |
| 121 | apex_key{ |
| 122 | name: "foo_key", |
| 123 | } |
| 124 | apex { |
| 125 | name: "foo", |
| 126 | key: "foo_key", |
| 127 | updatable: true, |
| 128 | min_sdk_version: "31", |
| 129 | file_contexts: ":myapex-file_contexts", |
| 130 | bazel_module: { label: "//:foo" }, |
| 131 | test_only_force_compression: true, // force compression |
| 132 | }` |
| 133 | |
| 134 | outputBaseDir := "out/bazel" |
| 135 | result := android.GroupFixturePreparers( |
| 136 | prepareForApexTest, |
| 137 | android.FixtureModifyConfig(func(config android.Config) { |
| 138 | config.BazelContext = android.MockBazelContext{ |
| 139 | OutputBaseDir: outputBaseDir, |
| 140 | LabelToApexInfo: map[string]cquery.ApexInfo{ |
| 141 | "//:foo": cquery.ApexInfo{ |
| 142 | SignedOutput: "signed_out.apex", |
| 143 | SignedCompressedOutput: "signed_out.capex", |
| 144 | BundleKeyInfo: []string{"public_key", "private_key"}, |
| 145 | ContainerKeyInfo: []string{"container_cert", "container_private"}, |
| 146 | }, |
| 147 | }, |
| 148 | } |
| 149 | }), |
| 150 | ).RunTestWithBp(t, bp) |
| 151 | |
| 152 | m := result.ModuleForTests("foo", "android_common_foo_image").Module() |
| 153 | ab, ok := m.(*apexBundle) |
| 154 | if !ok { |
| 155 | t.Fatalf("Expected module to be an apexBundle, was not") |
| 156 | } |
| 157 | |
| 158 | if w, g := "out/bazel/execroot/__main__/signed_out.capex", ab.outputFile.String(); w != g { |
| 159 | t.Errorf("Expected output file to be compressed apex %q, got %q", w, g) |
| 160 | } |
| 161 | |
| 162 | mkData := android.AndroidMkDataForTest(t, result.TestContext, m) |
| 163 | var builder strings.Builder |
| 164 | mkData.Custom(&builder, "foo", "BAZEL_TARGET_", "", mkData) |
| 165 | |
| 166 | data := builder.String() |
| 167 | |
| 168 | expectedAndroidMk := []string{ |
| 169 | "LOCAL_PREBUILT_MODULE_FILE := out/bazel/execroot/__main__/signed_out.capex", |
| 170 | |
| 171 | // Check that the source install file is the capex. The dest is not important. |
| 172 | "LOCAL_SOONG_INSTALL_PAIRS := out/bazel/execroot/__main__/signed_out.capex:", |
| 173 | } |
| 174 | for _, androidMk := range expectedAndroidMk { |
| 175 | if !strings.Contains(data, androidMk) { |
| 176 | t.Errorf("Expected %q in androidmk data, but did not find %q", androidMk, data) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 181 | func TestOverrideApexImageInMixedBuilds(t *testing.T) { |
| 182 | bp := ` |
| 183 | apex_key{ |
| 184 | name: "foo_key", |
| 185 | } |
| 186 | apex_key{ |
| 187 | name: "override_foo_key", |
| 188 | } |
| 189 | apex { |
| 190 | name: "foo", |
| 191 | key: "foo_key", |
| 192 | updatable: true, |
| 193 | min_sdk_version: "31", |
| 194 | package_name: "pkg_name", |
| 195 | file_contexts: ":myapex-file_contexts", |
| 196 | bazel_module: { label: "//:foo" }, |
| 197 | } |
| 198 | override_apex { |
| 199 | name: "override_foo", |
| 200 | key: "override_foo_key", |
| 201 | package_name: "override_pkg_name", |
| 202 | base: "foo", |
| 203 | bazel_module: { label: "//:override_foo" }, |
| 204 | } |
| 205 | ` |
| 206 | |
| 207 | outputBaseDir := "out/bazel" |
| 208 | result := android.GroupFixturePreparers( |
| 209 | prepareForApexTest, |
| 210 | android.FixtureModifyConfig(func(config android.Config) { |
| 211 | config.BazelContext = android.MockBazelContext{ |
| 212 | OutputBaseDir: outputBaseDir, |
| 213 | LabelToApexInfo: map[string]cquery.ApexInfo{ |
| 214 | "//:foo": cquery.ApexInfo{ |
| 215 | SignedOutput: "signed_out.apex", |
| 216 | UnsignedOutput: "unsigned_out.apex", |
| 217 | BundleKeyInfo: []string{"public_key", "private_key"}, |
| 218 | ContainerKeyInfo: []string{"container_cert", "container_private"}, |
| 219 | SymbolsUsedByApex: "foo_using.txt", |
| 220 | JavaSymbolsUsedByApex: "foo_using.xml", |
| 221 | BundleFile: "apex_bundle.zip", |
| 222 | InstalledFiles: "installed-files.txt", |
| 223 | RequiresLibs: []string{"//path/c:c", "//path/d:d"}, |
| 224 | |
| 225 | // unused |
| 226 | PackageName: "pkg_name", |
| 227 | ProvidesLibs: []string{"a", "b"}, |
| 228 | }, |
| 229 | "//:override_foo": cquery.ApexInfo{ |
| 230 | SignedOutput: "override_signed_out.apex", |
| 231 | UnsignedOutput: "override_unsigned_out.apex", |
| 232 | BundleKeyInfo: []string{"override_public_key", "override_private_key"}, |
| 233 | ContainerKeyInfo: []string{"override_container_cert", "override_container_private"}, |
| 234 | SymbolsUsedByApex: "override_foo_using.txt", |
| 235 | JavaSymbolsUsedByApex: "override_foo_using.xml", |
| 236 | BundleFile: "override_apex_bundle.zip", |
| 237 | InstalledFiles: "override_installed-files.txt", |
| 238 | RequiresLibs: []string{"//path/c:c", "//path/d:d"}, |
| 239 | |
| 240 | // unused |
| 241 | PackageName: "override_pkg_name", |
| 242 | ProvidesLibs: []string{"a", "b"}, |
| 243 | }, |
| 244 | }, |
| 245 | } |
| 246 | }), |
| 247 | ).RunTestWithBp(t, bp) |
| 248 | |
| 249 | m := result.ModuleForTests("foo", "android_common_override_foo_foo_image").Module() |
| 250 | ab, ok := m.(*apexBundle) |
| 251 | if !ok { |
| 252 | t.Fatalf("Expected module to be an apexBundle, was not") |
| 253 | } |
| 254 | |
| 255 | if w, g := "out/bazel/execroot/__main__/override_public_key", ab.publicKeyFile.String(); w != g { |
| 256 | t.Errorf("Expected public key %q, got %q", w, g) |
| 257 | } |
| 258 | |
| 259 | if w, g := "out/bazel/execroot/__main__/override_private_key", ab.privateKeyFile.String(); w != g { |
| 260 | t.Errorf("Expected private key %q, got %q", w, g) |
| 261 | } |
| 262 | |
| 263 | if w, g := "out/bazel/execroot/__main__/override_container_cert", ab.containerCertificateFile.String(); w != g { |
| 264 | t.Errorf("Expected public container key %q, got %q", w, g) |
| 265 | } |
| 266 | |
| 267 | if w, g := "out/bazel/execroot/__main__/override_container_private", ab.containerPrivateKeyFile.String(); w != g { |
| 268 | t.Errorf("Expected private container key %q, got %q", w, g) |
| 269 | } |
| 270 | |
| 271 | if w, g := "out/bazel/execroot/__main__/override_signed_out.apex", ab.outputFile.String(); w != g { |
| 272 | t.Errorf("Expected output file %q, got %q", w, g) |
| 273 | } |
| 274 | |
| 275 | if w, g := "out/bazel/execroot/__main__/override_foo_using.txt", ab.nativeApisUsedByModuleFile.String(); w != g { |
| 276 | t.Errorf("Expected output file %q, got %q", w, g) |
| 277 | } |
| 278 | |
| 279 | if w, g := "out/bazel/execroot/__main__/override_foo_using.xml", ab.javaApisUsedByModuleFile.String(); w != g { |
| 280 | t.Errorf("Expected output file %q, got %q", w, g) |
| 281 | } |
| 282 | |
| 283 | if w, g := "out/bazel/execroot/__main__/override_installed-files.txt", ab.installedFilesFile.String(); w != g { |
| 284 | t.Errorf("Expected installed-files.txt %q, got %q", w, g) |
| 285 | } |
| 286 | |
| 287 | mkData := android.AndroidMkDataForTest(t, result.TestContext, m) |
| 288 | var builder strings.Builder |
| 289 | mkData.Custom(&builder, "override_foo", "BAZEL_TARGET_", "", mkData) |
| 290 | |
| 291 | data := builder.String() |
| 292 | if w := "ALL_MODULES.$(my_register_name).BUNDLE := out/bazel/execroot/__main__/override_apex_bundle.zip"; !strings.Contains(data, w) { |
| 293 | t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) |
| 294 | } |
| 295 | if w := "$(call dist-for-goals,checkbuild,out/bazel/execroot/__main__/override_installed-files.txt:override_foo-installed-files.txt)"; !strings.Contains(data, w) { |
| 296 | t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) |
| 297 | } |
| 298 | if w := "LOCAL_REQUIRED_MODULES := c d"; !strings.Contains(data, w) { |
| 299 | t.Errorf("Expected %q in androidmk data, but did not find it in %q", w, data) |
| 300 | } |
| 301 | } |