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