| 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" | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 18 | "android/soong/android/allowlists" | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 19 | "android/soong/bazel/cquery" | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 20 | "fmt" | 
|  | 21 | "path/filepath" | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 22 | "strings" | 
|  | 23 | "testing" | 
|  | 24 | ) | 
|  | 25 |  | 
|  | 26 | func TestApexImageInMixedBuilds(t *testing.T) { | 
|  | 27 | bp := ` | 
|  | 28 | apex_key{ | 
|  | 29 | name: "foo_key", | 
|  | 30 | } | 
| Jingwen Chen | 2d37b64 | 2023-03-14 16:11:38 +0000 | [diff] [blame] | 31 |  | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 32 | apex { | 
|  | 33 | name: "foo", | 
|  | 34 | key: "foo_key", | 
|  | 35 | updatable: true, | 
|  | 36 | min_sdk_version: "31", | 
|  | 37 | file_contexts: ":myapex-file_contexts", | 
|  | 38 | bazel_module: { label: "//:foo" }, | 
|  | 39 | }` | 
|  | 40 |  | 
|  | 41 | outputBaseDir := "out/bazel" | 
|  | 42 | result := android.GroupFixturePreparers( | 
|  | 43 | prepareForApexTest, | 
|  | 44 | android.FixtureModifyConfig(func(config android.Config) { | 
|  | 45 | config.BazelContext = android.MockBazelContext{ | 
|  | 46 | OutputBaseDir: outputBaseDir, | 
|  | 47 | LabelToApexInfo: map[string]cquery.ApexInfo{ | 
|  | 48 | "//:foo": cquery.ApexInfo{ | 
| Jingwen Chen | 29743c8 | 2023-01-25 17:49:46 +0000 | [diff] [blame] | 49 | // ApexInfo Starlark provider. | 
| Jingwen Chen | 94098e8 | 2023-01-10 14:50:42 +0000 | [diff] [blame] | 50 | SignedOutput:           "signed_out.apex", | 
|  | 51 | SignedCompressedOutput: "signed_out.capex", | 
|  | 52 | UnsignedOutput:         "unsigned_out.apex", | 
|  | 53 | BundleKeyInfo:          []string{"public_key", "private_key"}, | 
|  | 54 | ContainerKeyInfo:       []string{"container_cert", "container_private"}, | 
|  | 55 | SymbolsUsedByApex:      "foo_using.txt", | 
|  | 56 | JavaSymbolsUsedByApex:  "foo_using.xml", | 
|  | 57 | BundleFile:             "apex_bundle.zip", | 
|  | 58 | InstalledFiles:         "installed-files.txt", | 
|  | 59 | RequiresLibs:           []string{"//path/c:c", "//path/d:d"}, | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 60 |  | 
|  | 61 | // unused | 
|  | 62 | PackageName:  "pkg_name", | 
|  | 63 | ProvidesLibs: []string{"a", "b"}, | 
| Jingwen Chen | 29743c8 | 2023-01-25 17:49:46 +0000 | [diff] [blame] | 64 |  | 
|  | 65 | // ApexMkInfo Starlark provider | 
| Jingwen Chen | 2d37b64 | 2023-03-14 16:11:38 +0000 | [diff] [blame] | 66 | PayloadFilesInfo: []map[string]string{ | 
|  | 67 | { | 
|  | 68 | "built_file":       "bazel-out/adbd", | 
|  | 69 | "install_dir":      "bin", | 
|  | 70 | "class":            "nativeExecutable", | 
|  | 71 | "make_module_name": "adbd", | 
|  | 72 | "basename":         "adbd", | 
|  | 73 | "package":          "foo", | 
|  | 74 | }, | 
|  | 75 | }, | 
| Jingwen Chen | 29743c8 | 2023-01-25 17:49:46 +0000 | [diff] [blame] | 76 | MakeModulesToInstall: []string{"c"}, // d deliberately omitted | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 77 | }, | 
|  | 78 | }, | 
|  | 79 | } | 
|  | 80 | }), | 
|  | 81 | ).RunTestWithBp(t, bp) | 
|  | 82 |  | 
| Jooyung Han | a0503a5 | 2023-08-23 13:12:50 +0900 | [diff] [blame] | 83 | m := result.ModuleForTests("foo", "android_common_foo").Module() | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 84 | ab, ok := m.(*apexBundle) | 
| Jingwen Chen | 2d37b64 | 2023-03-14 16:11:38 +0000 | [diff] [blame] | 85 |  | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 86 | if !ok { | 
|  | 87 | t.Fatalf("Expected module to be an apexBundle, was not") | 
|  | 88 | } | 
|  | 89 |  | 
| Jingwen Chen | 2d37b64 | 2023-03-14 16:11:38 +0000 | [diff] [blame] | 90 | // TODO: refactor to android.AssertStringEquals | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 91 | if w, g := "out/bazel/execroot/__main__/public_key", ab.publicKeyFile.String(); w != g { | 
|  | 92 | t.Errorf("Expected public key %q, got %q", w, g) | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | if w, g := "out/bazel/execroot/__main__/private_key", ab.privateKeyFile.String(); w != g { | 
|  | 96 | t.Errorf("Expected private key %q, got %q", w, g) | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | if w, g := "out/bazel/execroot/__main__/container_cert", ab.containerCertificateFile.String(); w != g { | 
|  | 100 | t.Errorf("Expected public container key %q, got %q", w, g) | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | if w, g := "out/bazel/execroot/__main__/container_private", ab.containerPrivateKeyFile.String(); w != g { | 
|  | 104 | t.Errorf("Expected private container key %q, got %q", w, g) | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | if w, g := "out/bazel/execroot/__main__/signed_out.apex", ab.outputFile.String(); w != g { | 
|  | 108 | t.Errorf("Expected output file %q, got %q", w, g) | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | if w, g := "out/bazel/execroot/__main__/foo_using.txt", ab.nativeApisUsedByModuleFile.String(); w != g { | 
|  | 112 | t.Errorf("Expected output file %q, got %q", w, g) | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | if w, g := "out/bazel/execroot/__main__/foo_using.xml", ab.javaApisUsedByModuleFile.String(); w != g { | 
|  | 116 | t.Errorf("Expected output file %q, got %q", w, g) | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | if w, g := "out/bazel/execroot/__main__/installed-files.txt", ab.installedFilesFile.String(); w != g { | 
|  | 120 | t.Errorf("Expected installed-files.txt %q, got %q", w, g) | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | mkData := android.AndroidMkDataForTest(t, result.TestContext, m) | 
|  | 124 | var builder strings.Builder | 
|  | 125 | mkData.Custom(&builder, "foo", "BAZEL_TARGET_", "", mkData) | 
|  | 126 |  | 
|  | 127 | data := builder.String() | 
|  | 128 | if w := "ALL_MODULES.$(my_register_name).BUNDLE := out/bazel/execroot/__main__/apex_bundle.zip"; !strings.Contains(data, w) { | 
|  | 129 | t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) | 
|  | 130 | } | 
|  | 131 | if w := "$(call dist-for-goals,checkbuild,out/bazel/execroot/__main__/installed-files.txt:foo-installed-files.txt)"; !strings.Contains(data, w) { | 
|  | 132 | t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) | 
|  | 133 | } | 
| Jingwen Chen | 29743c8 | 2023-01-25 17:49:46 +0000 | [diff] [blame] | 134 |  | 
|  | 135 | // make modules to be installed to system | 
|  | 136 | if len(ab.makeModulesToInstall) != 1 && ab.makeModulesToInstall[0] != "c" { | 
|  | 137 | t.Errorf("Expected makeModulesToInstall slice to only contain 'c', got %q", ab.makeModulesToInstall) | 
|  | 138 | } | 
| Jingwen Chen | 2d37b64 | 2023-03-14 16:11:38 +0000 | [diff] [blame] | 139 | if w := "LOCAL_REQUIRED_MODULES := adbd.foo c"; !strings.Contains(data, w) { | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 140 | t.Errorf("Expected %q in androidmk data, but did not find it in %q", w, data) | 
|  | 141 | } | 
|  | 142 | } | 
|  | 143 |  | 
| Jingwen Chen | 2d37b64 | 2023-03-14 16:11:38 +0000 | [diff] [blame] | 144 | func TestApexImageCreatesFilesInfoForMake(t *testing.T) { | 
|  | 145 | bp := ` | 
|  | 146 | apex_key{ | 
|  | 147 | name: "foo_key", | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | apex { | 
|  | 151 | name: "foo", | 
|  | 152 | key: "foo_key", | 
|  | 153 | updatable: true, | 
|  | 154 | min_sdk_version: "31", | 
|  | 155 | file_contexts: ":myapex-file_contexts", | 
|  | 156 | bazel_module: { label: "//:foo" }, | 
|  | 157 | }` | 
|  | 158 |  | 
|  | 159 | outputBaseDir := "out/bazel" | 
|  | 160 | result := android.GroupFixturePreparers( | 
|  | 161 | prepareForApexTest, | 
|  | 162 | android.FixtureModifyConfig(func(config android.Config) { | 
|  | 163 | config.BazelContext = android.MockBazelContext{ | 
|  | 164 | OutputBaseDir: outputBaseDir, | 
|  | 165 | LabelToApexInfo: map[string]cquery.ApexInfo{ | 
|  | 166 | "//:foo": { | 
|  | 167 | // ApexInfo Starlark provider. Necessary for the test. | 
|  | 168 | SignedOutput:     "signed_out.apex", | 
|  | 169 | BundleKeyInfo:    []string{"public_key", "private_key"}, | 
|  | 170 | ContainerKeyInfo: []string{"container_cert", "container_private"}, | 
|  | 171 |  | 
|  | 172 | // ApexMkInfo Starlark provider | 
|  | 173 | PayloadFilesInfo: []map[string]string{ | 
|  | 174 | { | 
|  | 175 | "arch":                  "arm64", | 
|  | 176 | "basename":              "libcrypto.so", | 
|  | 177 | "built_file":            "bazel-out/64/libcrypto.so", | 
|  | 178 | "class":                 "nativeSharedLib", | 
|  | 179 | "install_dir":           "lib64", | 
|  | 180 | "make_module_name":      "libcrypto", | 
|  | 181 | "package":               "foo/bar", | 
|  | 182 | "unstripped_built_file": "bazel-out/64/unstripped_libcrypto.so", | 
|  | 183 | }, | 
|  | 184 | { | 
|  | 185 | "arch":             "arm", | 
|  | 186 | "basename":         "libcrypto.so", | 
|  | 187 | "built_file":       "bazel-out/32/libcrypto.so", | 
|  | 188 | "class":            "nativeSharedLib", | 
|  | 189 | "install_dir":      "lib", | 
|  | 190 | "make_module_name": "libcrypto", | 
|  | 191 | "package":          "foo/bar", | 
|  | 192 | }, | 
|  | 193 | { | 
|  | 194 | "arch":             "arm64", | 
|  | 195 | "basename":         "adbd", | 
|  | 196 | "built_file":       "bazel-out/adbd", | 
|  | 197 | "class":            "nativeExecutable", | 
|  | 198 | "install_dir":      "bin", | 
|  | 199 | "make_module_name": "adbd", | 
|  | 200 | "package":          "foo", | 
|  | 201 | }, | 
|  | 202 | }, | 
|  | 203 | }, | 
|  | 204 | }, | 
|  | 205 | } | 
|  | 206 | }), | 
|  | 207 | ).RunTestWithBp(t, bp) | 
|  | 208 |  | 
| Jooyung Han | a0503a5 | 2023-08-23 13:12:50 +0900 | [diff] [blame] | 209 | m := result.ModuleForTests("foo", "android_common_foo").Module() | 
| Jingwen Chen | 2d37b64 | 2023-03-14 16:11:38 +0000 | [diff] [blame] | 210 | ab, ok := m.(*apexBundle) | 
|  | 211 |  | 
|  | 212 | if !ok { | 
|  | 213 | t.Fatalf("Expected module to be an apexBundle, was not") | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | expectedFilesInfo := []apexFile{ | 
|  | 217 | { | 
|  | 218 | androidMkModuleName: "libcrypto", | 
|  | 219 | builtFile:           android.PathForTesting("out/bazel/execroot/__main__/bazel-out/64/libcrypto.so"), | 
|  | 220 | class:               nativeSharedLib, | 
|  | 221 | customStem:          "libcrypto.so", | 
|  | 222 | installDir:          "lib64", | 
|  | 223 | moduleDir:           "foo/bar", | 
|  | 224 | arch:                "arm64", | 
|  | 225 | unstrippedBuiltFile: android.PathForTesting("out/bazel/execroot/__main__/bazel-out/64/unstripped_libcrypto.so"), | 
|  | 226 | }, | 
|  | 227 | { | 
|  | 228 | androidMkModuleName: "libcrypto", | 
|  | 229 | builtFile:           android.PathForTesting("out/bazel/execroot/__main__/bazel-out/32/libcrypto.so"), | 
|  | 230 | class:               nativeSharedLib, | 
|  | 231 | customStem:          "libcrypto.so", | 
|  | 232 | installDir:          "lib", | 
|  | 233 | moduleDir:           "foo/bar", | 
|  | 234 | arch:                "arm", | 
|  | 235 | }, | 
|  | 236 | { | 
|  | 237 | androidMkModuleName: "adbd", | 
|  | 238 | builtFile:           android.PathForTesting("out/bazel/execroot/__main__/bazel-out/adbd"), | 
|  | 239 | class:               nativeExecutable, | 
|  | 240 | customStem:          "adbd", | 
|  | 241 | installDir:          "bin", | 
|  | 242 | moduleDir:           "foo", | 
|  | 243 | arch:                "arm64", | 
|  | 244 | }, | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | if len(ab.filesInfo) != len(expectedFilesInfo) { | 
|  | 248 | t.Errorf("Expected %d entries in ab.filesInfo, but got %d", len(ab.filesInfo), len(expectedFilesInfo)) | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | for idx, f := range ab.filesInfo { | 
|  | 252 | expected := expectedFilesInfo[idx] | 
|  | 253 | android.AssertSame(t, "different class", expected.class, f.class) | 
|  | 254 | android.AssertStringEquals(t, "different built file", expected.builtFile.String(), f.builtFile.String()) | 
|  | 255 | android.AssertStringEquals(t, "different custom stem", expected.customStem, f.customStem) | 
|  | 256 | android.AssertStringEquals(t, "different install dir", expected.installDir, f.installDir) | 
|  | 257 | android.AssertStringEquals(t, "different make module name", expected.androidMkModuleName, f.androidMkModuleName) | 
|  | 258 | android.AssertStringEquals(t, "different moduleDir", expected.moduleDir, f.moduleDir) | 
|  | 259 | android.AssertStringEquals(t, "different arch", expected.arch, f.arch) | 
|  | 260 | if expected.unstrippedBuiltFile != nil { | 
|  | 261 | if f.unstrippedBuiltFile == nil { | 
|  | 262 | t.Errorf("expected an unstripped built file path.") | 
|  | 263 | } | 
|  | 264 | android.AssertStringEquals(t, "different unstripped built file", expected.unstrippedBuiltFile.String(), f.unstrippedBuiltFile.String()) | 
|  | 265 | } | 
|  | 266 | } | 
|  | 267 | } | 
|  | 268 |  | 
| Jingwen Chen | 94098e8 | 2023-01-10 14:50:42 +0000 | [diff] [blame] | 269 | func TestCompressedApexImageInMixedBuilds(t *testing.T) { | 
|  | 270 | bp := ` | 
|  | 271 | apex_key{ | 
|  | 272 | name: "foo_key", | 
|  | 273 | } | 
|  | 274 | apex { | 
|  | 275 | name: "foo", | 
|  | 276 | key: "foo_key", | 
|  | 277 | updatable: true, | 
|  | 278 | min_sdk_version: "31", | 
|  | 279 | file_contexts: ":myapex-file_contexts", | 
|  | 280 | bazel_module: { label: "//:foo" }, | 
|  | 281 | test_only_force_compression: true, // force compression | 
|  | 282 | }` | 
|  | 283 |  | 
|  | 284 | outputBaseDir := "out/bazel" | 
|  | 285 | result := android.GroupFixturePreparers( | 
|  | 286 | prepareForApexTest, | 
|  | 287 | android.FixtureModifyConfig(func(config android.Config) { | 
|  | 288 | config.BazelContext = android.MockBazelContext{ | 
|  | 289 | OutputBaseDir: outputBaseDir, | 
|  | 290 | LabelToApexInfo: map[string]cquery.ApexInfo{ | 
|  | 291 | "//:foo": cquery.ApexInfo{ | 
|  | 292 | SignedOutput:           "signed_out.apex", | 
|  | 293 | SignedCompressedOutput: "signed_out.capex", | 
|  | 294 | BundleKeyInfo:          []string{"public_key", "private_key"}, | 
|  | 295 | ContainerKeyInfo:       []string{"container_cert", "container_private"}, | 
|  | 296 | }, | 
|  | 297 | }, | 
|  | 298 | } | 
|  | 299 | }), | 
|  | 300 | ).RunTestWithBp(t, bp) | 
|  | 301 |  | 
| Jooyung Han | a0503a5 | 2023-08-23 13:12:50 +0900 | [diff] [blame] | 302 | m := result.ModuleForTests("foo", "android_common_foo").Module() | 
| Jingwen Chen | 94098e8 | 2023-01-10 14:50:42 +0000 | [diff] [blame] | 303 | ab, ok := m.(*apexBundle) | 
|  | 304 | if !ok { | 
|  | 305 | t.Fatalf("Expected module to be an apexBundle, was not") | 
|  | 306 | } | 
|  | 307 |  | 
|  | 308 | if w, g := "out/bazel/execroot/__main__/signed_out.capex", ab.outputFile.String(); w != g { | 
|  | 309 | t.Errorf("Expected output file to be compressed apex %q, got %q", w, g) | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | mkData := android.AndroidMkDataForTest(t, result.TestContext, m) | 
|  | 313 | var builder strings.Builder | 
|  | 314 | mkData.Custom(&builder, "foo", "BAZEL_TARGET_", "", mkData) | 
|  | 315 |  | 
|  | 316 | data := builder.String() | 
|  | 317 |  | 
|  | 318 | expectedAndroidMk := []string{ | 
|  | 319 | "LOCAL_PREBUILT_MODULE_FILE := out/bazel/execroot/__main__/signed_out.capex", | 
|  | 320 |  | 
|  | 321 | // Check that the source install file is the capex. The dest is not important. | 
|  | 322 | "LOCAL_SOONG_INSTALL_PAIRS := out/bazel/execroot/__main__/signed_out.capex:", | 
|  | 323 | } | 
|  | 324 | for _, androidMk := range expectedAndroidMk { | 
|  | 325 | if !strings.Contains(data, androidMk) { | 
|  | 326 | t.Errorf("Expected %q in androidmk data, but did not find %q", androidMk, data) | 
|  | 327 | } | 
|  | 328 | } | 
|  | 329 | } | 
|  | 330 |  | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 331 | func TestOverrideApexImageInMixedBuilds(t *testing.T) { | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 332 | originalBp := ` | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 333 | apex_key{ | 
|  | 334 | name: "foo_key", | 
|  | 335 | } | 
|  | 336 | apex_key{ | 
|  | 337 | name: "override_foo_key", | 
|  | 338 | } | 
|  | 339 | apex { | 
|  | 340 | name: "foo", | 
|  | 341 | key: "foo_key", | 
|  | 342 | updatable: true, | 
|  | 343 | min_sdk_version: "31", | 
|  | 344 | package_name: "pkg_name", | 
|  | 345 | file_contexts: ":myapex-file_contexts", | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 346 | %s | 
|  | 347 | }` | 
|  | 348 | overrideBp := ` | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 349 | override_apex { | 
|  | 350 | name: "override_foo", | 
|  | 351 | key: "override_foo_key", | 
|  | 352 | package_name: "override_pkg_name", | 
|  | 353 | base: "foo", | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 354 | %s | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 355 | } | 
|  | 356 | ` | 
|  | 357 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 358 | originalApexBpDir := "original" | 
|  | 359 | originalApexName := "foo" | 
|  | 360 | overrideApexBpDir := "override" | 
|  | 361 | overrideApexName := "override_foo" | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 362 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 363 | defaultApexLabel := fmt.Sprintf("//%s:%s", originalApexBpDir, originalApexName) | 
|  | 364 | defaultOverrideApexLabel := fmt.Sprintf("//%s:%s", overrideApexBpDir, overrideApexName) | 
| Jingwen Chen | 29743c8 | 2023-01-25 17:49:46 +0000 | [diff] [blame] | 365 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 366 | testCases := []struct { | 
|  | 367 | desc                    string | 
|  | 368 | bazelModuleProp         string | 
|  | 369 | apexLabel               string | 
|  | 370 | overrideBazelModuleProp string | 
|  | 371 | overrideApexLabel       string | 
|  | 372 | bp2buildConfiguration   android.Bp2BuildConversionAllowlist | 
|  | 373 | }{ | 
|  | 374 | { | 
|  | 375 | desc:                    "both explicit labels", | 
|  | 376 | bazelModuleProp:         `bazel_module: { label: "//:foo" },`, | 
|  | 377 | apexLabel:               "//:foo", | 
|  | 378 | overrideBazelModuleProp: `bazel_module: { label: "//:override_foo" },`, | 
|  | 379 | overrideApexLabel:       "//:override_foo", | 
|  | 380 | bp2buildConfiguration:   android.NewBp2BuildAllowlist(), | 
|  | 381 | }, | 
|  | 382 | { | 
|  | 383 | desc:                    "both explicitly allowed", | 
|  | 384 | bazelModuleProp:         `bazel_module: { bp2build_available: true },`, | 
|  | 385 | apexLabel:               defaultApexLabel, | 
|  | 386 | overrideBazelModuleProp: `bazel_module: { bp2build_available: true },`, | 
|  | 387 | overrideApexLabel:       defaultOverrideApexLabel, | 
|  | 388 | bp2buildConfiguration:   android.NewBp2BuildAllowlist(), | 
|  | 389 | }, | 
|  | 390 | { | 
|  | 391 | desc:              "original allowed by dir, override allowed by name", | 
|  | 392 | apexLabel:         defaultApexLabel, | 
|  | 393 | overrideApexLabel: defaultOverrideApexLabel, | 
|  | 394 | bp2buildConfiguration: android.NewBp2BuildAllowlist().SetDefaultConfig( | 
|  | 395 | map[string]allowlists.BazelConversionConfigEntry{ | 
|  | 396 | originalApexBpDir: allowlists.Bp2BuildDefaultTrue, | 
|  | 397 | }).SetModuleAlwaysConvertList([]string{ | 
|  | 398 | overrideApexName, | 
|  | 399 | }), | 
|  | 400 | }, | 
|  | 401 | { | 
|  | 402 | desc:              "both allowed by name", | 
|  | 403 | apexLabel:         defaultApexLabel, | 
|  | 404 | overrideApexLabel: defaultOverrideApexLabel, | 
|  | 405 | bp2buildConfiguration: android.NewBp2BuildAllowlist().SetModuleAlwaysConvertList([]string{ | 
|  | 406 | originalApexName, | 
|  | 407 | overrideApexName, | 
|  | 408 | }), | 
|  | 409 | }, | 
|  | 410 | { | 
|  | 411 | desc:              "override allowed by name", | 
|  | 412 | apexLabel:         defaultApexLabel, | 
|  | 413 | overrideApexLabel: defaultOverrideApexLabel, | 
|  | 414 | bp2buildConfiguration: android.NewBp2BuildAllowlist().SetModuleAlwaysConvertList([]string{ | 
|  | 415 | overrideApexName, | 
|  | 416 | }), | 
|  | 417 | }, | 
|  | 418 | { | 
|  | 419 | desc:              "override allowed by dir", | 
|  | 420 | apexLabel:         defaultApexLabel, | 
|  | 421 | overrideApexLabel: defaultOverrideApexLabel, | 
|  | 422 | bp2buildConfiguration: android.NewBp2BuildAllowlist().SetDefaultConfig( | 
|  | 423 | map[string]allowlists.BazelConversionConfigEntry{ | 
|  | 424 | overrideApexBpDir: allowlists.Bp2BuildDefaultTrue, | 
|  | 425 | }).SetModuleAlwaysConvertList([]string{}), | 
|  | 426 | }, | 
|  | 427 | } | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 428 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 429 | for _, tc := range testCases { | 
|  | 430 | t.Run(tc.desc, func(t *testing.T) { | 
|  | 431 | outputBaseDir := "out/bazel" | 
|  | 432 | result := android.GroupFixturePreparers( | 
|  | 433 | prepareForApexTest, | 
|  | 434 | android.FixtureAddTextFile(filepath.Join(originalApexBpDir, "Android.bp"), fmt.Sprintf(originalBp, tc.bazelModuleProp)), | 
|  | 435 | android.FixtureAddTextFile(filepath.Join(overrideApexBpDir, "Android.bp"), fmt.Sprintf(overrideBp, tc.overrideBazelModuleProp)), | 
|  | 436 | android.FixtureModifyContext(func(ctx *android.TestContext) { | 
|  | 437 | ctx.RegisterBp2BuildConfig(tc.bp2buildConfiguration) | 
|  | 438 | }), | 
|  | 439 | android.FixtureModifyConfig(func(config android.Config) { | 
|  | 440 | config.BazelContext = android.MockBazelContext{ | 
|  | 441 | OutputBaseDir: outputBaseDir, | 
|  | 442 | LabelToApexInfo: map[string]cquery.ApexInfo{ | 
|  | 443 | tc.apexLabel: cquery.ApexInfo{ | 
|  | 444 | // ApexInfo Starlark provider | 
|  | 445 | SignedOutput:          "signed_out.apex", | 
|  | 446 | UnsignedOutput:        "unsigned_out.apex", | 
|  | 447 | BundleKeyInfo:         []string{"public_key", "private_key"}, | 
|  | 448 | ContainerKeyInfo:      []string{"container_cert", "container_private"}, | 
|  | 449 | SymbolsUsedByApex:     "foo_using.txt", | 
|  | 450 | JavaSymbolsUsedByApex: "foo_using.xml", | 
|  | 451 | BundleFile:            "apex_bundle.zip", | 
|  | 452 | InstalledFiles:        "installed-files.txt", | 
|  | 453 | RequiresLibs:          []string{"//path/c:c", "//path/d:d"}, | 
| Jingwen Chen | 29743c8 | 2023-01-25 17:49:46 +0000 | [diff] [blame] | 454 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 455 | // unused | 
|  | 456 | PackageName:  "pkg_name", | 
|  | 457 | ProvidesLibs: []string{"a", "b"}, | 
|  | 458 |  | 
|  | 459 | // ApexMkInfo Starlark provider | 
|  | 460 | MakeModulesToInstall: []string{"c"}, // d deliberately omitted | 
|  | 461 | }, | 
|  | 462 | tc.overrideApexLabel: cquery.ApexInfo{ | 
|  | 463 | // ApexInfo Starlark provider | 
|  | 464 | SignedOutput:          "override_signed_out.apex", | 
|  | 465 | UnsignedOutput:        "override_unsigned_out.apex", | 
|  | 466 | BundleKeyInfo:         []string{"override_public_key", "override_private_key"}, | 
|  | 467 | ContainerKeyInfo:      []string{"override_container_cert", "override_container_private"}, | 
|  | 468 | SymbolsUsedByApex:     "override_foo_using.txt", | 
|  | 469 | JavaSymbolsUsedByApex: "override_foo_using.xml", | 
|  | 470 | BundleFile:            "override_apex_bundle.zip", | 
|  | 471 | InstalledFiles:        "override_installed-files.txt", | 
|  | 472 | RequiresLibs:          []string{"//path/c:c", "//path/d:d"}, | 
|  | 473 |  | 
|  | 474 | // unused | 
|  | 475 | PackageName:  "override_pkg_name", | 
|  | 476 | ProvidesLibs: []string{"a", "b"}, | 
|  | 477 |  | 
|  | 478 | // ApexMkInfo Starlark provider | 
|  | 479 | MakeModulesToInstall: []string{"c"}, // d deliberately omitted | 
|  | 480 | }, | 
|  | 481 | }, | 
|  | 482 | } | 
|  | 483 | }), | 
|  | 484 | ).RunTest(t) | 
|  | 485 |  | 
| Jooyung Han | a0503a5 | 2023-08-23 13:12:50 +0900 | [diff] [blame] | 486 | m := result.ModuleForTests("foo", "android_common_override_foo_foo").Module() | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 487 | ab, ok := m.(*apexBundle) | 
|  | 488 | if !ok { | 
|  | 489 | t.Fatalf("Expected module to be an apexBundle, was not") | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 490 | } | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 491 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 492 | if w, g := "out/bazel/execroot/__main__/override_public_key", ab.publicKeyFile.String(); w != g { | 
|  | 493 | t.Errorf("Expected public key %q, got %q", w, g) | 
|  | 494 | } | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 495 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 496 | if w, g := "out/bazel/execroot/__main__/override_private_key", ab.privateKeyFile.String(); w != g { | 
|  | 497 | t.Errorf("Expected private key %q, got %q", w, g) | 
|  | 498 | } | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 499 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 500 | if w, g := "out/bazel/execroot/__main__/override_container_cert", ab.containerCertificateFile; g != nil && w != g.String() { | 
|  | 501 | t.Errorf("Expected public container key %q, got %q", w, g) | 
|  | 502 | } | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 503 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 504 | if w, g := "out/bazel/execroot/__main__/override_container_private", ab.containerPrivateKeyFile; g != nil && w != g.String() { | 
|  | 505 | t.Errorf("Expected private container key %q, got %q", w, g) | 
|  | 506 | } | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 507 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 508 | if w, g := "out/bazel/execroot/__main__/override_signed_out.apex", ab.outputFile.String(); w != g { | 
|  | 509 | t.Errorf("Expected output file %q, got %q", w, g) | 
|  | 510 | } | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 511 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 512 | if w, g := "out/bazel/execroot/__main__/override_foo_using.txt", ab.nativeApisUsedByModuleFile.String(); w != g { | 
|  | 513 | t.Errorf("Expected output file %q, got %q", w, g) | 
|  | 514 | } | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 515 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 516 | if w, g := "out/bazel/execroot/__main__/override_foo_using.xml", ab.javaApisUsedByModuleFile.String(); w != g { | 
|  | 517 | t.Errorf("Expected output file %q, got %q", w, g) | 
|  | 518 | } | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 519 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 520 | if w, g := "out/bazel/execroot/__main__/override_installed-files.txt", ab.installedFilesFile.String(); w != g { | 
|  | 521 | t.Errorf("Expected installed-files.txt %q, got %q", w, g) | 
|  | 522 | } | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 523 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 524 | mkData := android.AndroidMkDataForTest(t, result.TestContext, m) | 
|  | 525 | var builder strings.Builder | 
|  | 526 | mkData.Custom(&builder, "override_foo", "BAZEL_TARGET_", "", mkData) | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 527 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 528 | data := builder.String() | 
|  | 529 | if w := "ALL_MODULES.$(my_register_name).BUNDLE := out/bazel/execroot/__main__/override_apex_bundle.zip"; !strings.Contains(data, w) { | 
|  | 530 | t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) | 
|  | 531 | } | 
|  | 532 | if w := "$(call dist-for-goals,checkbuild,out/bazel/execroot/__main__/override_installed-files.txt:override_foo-installed-files.txt)"; !strings.Contains(data, w) { | 
|  | 533 | t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) | 
|  | 534 | } | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 535 |  | 
| Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 536 | // make modules to be installed to system | 
|  | 537 | if len(ab.makeModulesToInstall) != 1 || ab.makeModulesToInstall[0] != "c" { | 
|  | 538 | t.Errorf("Expected makeModulestoInstall slice to only contain 'c', got %q", ab.makeModulesToInstall) | 
|  | 539 | } | 
|  | 540 | if w := "LOCAL_REQUIRED_MODULES := c"; !strings.Contains(data, w) { | 
|  | 541 | t.Errorf("Expected %q in androidmk data, but did not find it in %q", w, data) | 
|  | 542 | } | 
|  | 543 | }) | 
| Jingwen Chen | 889f2f2 | 2022-12-16 08:16:01 +0000 | [diff] [blame] | 544 | } | 
|  | 545 | } |