blob: 7bc379facb1f4d14451155662e9614a67a4b4dca [file] [log] [blame]
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04001// Copyright 2021 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
15package bp2build
16
17import (
18 "android/soong/android"
19 "android/soong/apex"
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040020 "android/soong/cc"
Jingwen Chendf165c92022-06-08 16:00:39 +000021 "android/soong/etc"
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040022 "android/soong/java"
Jingwen Chenb07c9012021-12-08 10:05:45 +000023 "android/soong/sh"
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040024
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040025 "testing"
26)
27
28func runApexTestCase(t *testing.T, tc bp2buildTestCase) {
29 t.Helper()
30 runBp2BuildTestCase(t, registerApexModuleTypes, tc)
31}
32
33func registerApexModuleTypes(ctx android.RegistrationContext) {
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040034 // CC module types needed as they can be APEX dependencies
35 cc.RegisterCCBuildComponents(ctx)
36
Jingwen Chenb07c9012021-12-08 10:05:45 +000037 ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory)
38 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040039 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
40 ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory)
41 ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory)
42 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Jingwen Chen81c67d32022-06-08 16:08:25 +000043 ctx.RegisterModuleType("prebuilt_etc", etc.PrebuiltEtcFactory)
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040044}
45
Wei Li1c66fc72022-05-09 23:59:14 -070046func runOverrideApexTestCase(t *testing.T, tc bp2buildTestCase) {
47 t.Helper()
48 runBp2BuildTestCase(t, registerOverrideApexModuleTypes, tc)
49}
50
51func registerOverrideApexModuleTypes(ctx android.RegistrationContext) {
52 // CC module types needed as they can be APEX dependencies
53 cc.RegisterCCBuildComponents(ctx)
54
55 ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory)
56 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
57 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
58 ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory)
59 ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory)
60 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
61 ctx.RegisterModuleType("apex", apex.BundleFactory)
Jingwen Chendf165c92022-06-08 16:00:39 +000062 ctx.RegisterModuleType("prebuilt_etc", etc.PrebuiltEtcFactory)
Wei Li1c66fc72022-05-09 23:59:14 -070063}
64
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040065func TestApexBundleSimple(t *testing.T) {
66 runApexTestCase(t, bp2buildTestCase{
Wei Li1c66fc72022-05-09 23:59:14 -070067 description: "apex - example with all props, file_context is a module in same Android.bp",
Liz Kammerbe46fcc2021-11-01 15:32:43 -040068 moduleTypeUnderTest: "apex",
69 moduleTypeUnderTestFactory: apex.BundleFactory,
70 filesystem: map[string]string{},
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040071 blueprint: `
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040072apex_key {
Jingwen Chenb07c9012021-12-08 10:05:45 +000073 name: "com.android.apogee.key",
74 public_key: "com.android.apogee.avbpubkey",
75 private_key: "com.android.apogee.pem",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040076 bazel_module: { bp2build_available: false },
77}
78
79android_app_certificate {
Jingwen Chenb07c9012021-12-08 10:05:45 +000080 name: "com.android.apogee.certificate",
81 certificate: "com.android.apogee",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040082 bazel_module: { bp2build_available: false },
83}
84
85cc_library {
Jingwen Chenb07c9012021-12-08 10:05:45 +000086 name: "native_shared_lib_1",
87 bazel_module: { bp2build_available: false },
88}
89
90cc_library {
91 name: "native_shared_lib_2",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040092 bazel_module: { bp2build_available: false },
93}
94
Jingwen Chen81c67d32022-06-08 16:08:25 +000095prebuilt_etc {
96 name: "prebuilt_1",
Jingwen Chenb07c9012021-12-08 10:05:45 +000097 bazel_module: { bp2build_available: false },
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -040098}
99
Jingwen Chen81c67d32022-06-08 16:08:25 +0000100prebuilt_etc {
101 name: "prebuilt_2",
Jingwen Chenb07c9012021-12-08 10:05:45 +0000102 bazel_module: { bp2build_available: false },
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -0400103}
104
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400105filegroup {
106 name: "com.android.apogee-file_contexts",
Jingwen Chenb07c9012021-12-08 10:05:45 +0000107 srcs: [
Wei Li1c66fc72022-05-09 23:59:14 -0700108 "com.android.apogee-file_contexts",
Jingwen Chenb07c9012021-12-08 10:05:45 +0000109 ],
110 bazel_module: { bp2build_available: false },
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400111}
112
Jingwen Chenb07c9012021-12-08 10:05:45 +0000113cc_binary { name: "cc_binary_1", bazel_module: { bp2build_available: false } }
114sh_binary { name: "sh_binary_2", bazel_module: { bp2build_available: false } }
115
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400116apex {
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400117 name: "com.android.apogee",
118 manifest: "apogee_manifest.json",
119 androidManifest: "ApogeeAndroidManifest.xml",
Wei Li1c66fc72022-05-09 23:59:14 -0700120 file_contexts: ":com.android.apogee-file_contexts",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400121 min_sdk_version: "29",
122 key: "com.android.apogee.key",
123 certificate: "com.android.apogee.certificate",
124 updatable: false,
125 installable: false,
Wei Lif034cb42022-01-19 15:54:31 -0800126 compressible: false,
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400127 native_shared_libs: [
128 "native_shared_lib_1",
129 "native_shared_lib_2",
130 ],
131 binaries: [
Jingwen Chenb07c9012021-12-08 10:05:45 +0000132 "cc_binary_1",
133 "sh_binary_2",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400134 ],
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -0400135 prebuilts: [
Jingwen Chen81c67d32022-06-08 16:08:25 +0000136 "prebuilt_1",
137 "prebuilt_2",
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -0400138 ],
Jingwen Chen9b7ebca2022-06-03 09:11:20 +0000139 package_name: "com.android.apogee.test.package",
Jingwen Chenb732d7c2022-06-10 08:14:19 +0000140 logging_parent: "logging.parent",
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400141}
142`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500143 expectedBazelTargets: []string{
144 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
145 "android_manifest": `"ApogeeAndroidManifest.xml"`,
146 "binaries": `[
Jingwen Chenb07c9012021-12-08 10:05:45 +0000147 ":cc_binary_1",
148 ":sh_binary_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500149 ]`,
150 "certificate": `":com.android.apogee.certificate"`,
151 "file_contexts": `":com.android.apogee-file_contexts"`,
152 "installable": "False",
153 "key": `":com.android.apogee.key"`,
154 "manifest": `"apogee_manifest.json"`,
155 "min_sdk_version": `"29"`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800156 "native_shared_libs_32": `[
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400157 ":native_shared_lib_1",
158 ":native_shared_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500159 ]`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800160 "native_shared_libs_64": `select({
161 "//build/bazel/platforms/arch:arm64": [
162 ":native_shared_lib_1",
163 ":native_shared_lib_2",
164 ],
165 "//build/bazel/platforms/arch:x86_64": [
166 ":native_shared_lib_1",
167 ":native_shared_lib_2",
168 ],
169 "//conditions:default": [],
170 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500171 "prebuilts": `[
Jingwen Chen81c67d32022-06-08 16:08:25 +0000172 ":prebuilt_1",
173 ":prebuilt_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500174 ]`,
Jingwen Chenb732d7c2022-06-10 08:14:19 +0000175 "updatable": "False",
176 "compressible": "False",
177 "package_name": `"com.android.apogee.test.package"`,
178 "logging_parent": `"logging.parent"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500179 }),
180 }})
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400181}
182
Wei Li1c66fc72022-05-09 23:59:14 -0700183func TestApexBundleSimple_fileContextsInAnotherAndroidBp(t *testing.T) {
184 runApexTestCase(t, bp2buildTestCase{
185 description: "apex - file contexts is a module in another Android.bp",
186 moduleTypeUnderTest: "apex",
187 moduleTypeUnderTestFactory: apex.BundleFactory,
188 filesystem: map[string]string{
189 "a/b/Android.bp": `
190filegroup {
191 name: "com.android.apogee-file_contexts",
192 srcs: [
193 "com.android.apogee-file_contexts",
194 ],
195 bazel_module: { bp2build_available: false },
196}
197`,
198 },
199 blueprint: `
200apex {
201 name: "com.android.apogee",
202 file_contexts: ":com.android.apogee-file_contexts",
203}
204`,
205 expectedBazelTargets: []string{
206 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
207 "file_contexts": `"//a/b:com.android.apogee-file_contexts"`,
Wei Li40f98732022-05-20 22:08:11 -0700208 "manifest": `"apex_manifest.json"`,
Wei Li1c66fc72022-05-09 23:59:14 -0700209 }),
210 }})
211}
212
213func TestApexBundleSimple_fileContextsIsFile(t *testing.T) {
214 runApexTestCase(t, bp2buildTestCase{
215 description: "apex - file contexts is a file",
216 moduleTypeUnderTest: "apex",
217 moduleTypeUnderTestFactory: apex.BundleFactory,
218 filesystem: map[string]string{},
219 blueprint: `
220apex {
221 name: "com.android.apogee",
222 file_contexts: "file_contexts_file",
223}
224`,
225 expectedBazelTargets: []string{
226 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
227 "file_contexts": `"file_contexts_file"`,
Wei Li40f98732022-05-20 22:08:11 -0700228 "manifest": `"apex_manifest.json"`,
Wei Li1c66fc72022-05-09 23:59:14 -0700229 }),
230 }})
231}
232
233func TestApexBundleSimple_fileContextsIsNotSpecified(t *testing.T) {
234 runApexTestCase(t, bp2buildTestCase{
235 description: "apex - file contexts is not specified",
236 moduleTypeUnderTest: "apex",
237 moduleTypeUnderTestFactory: apex.BundleFactory,
238 filesystem: map[string]string{
239 "system/sepolicy/apex/Android.bp": `
240filegroup {
241 name: "com.android.apogee-file_contexts",
242 srcs: [
243 "com.android.apogee-file_contexts",
244 ],
245 bazel_module: { bp2build_available: false },
246}
247`,
248 },
249 blueprint: `
250apex {
251 name: "com.android.apogee",
252}
253`,
254 expectedBazelTargets: []string{
255 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
256 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
Wei Li40f98732022-05-20 22:08:11 -0700257 "manifest": `"apex_manifest.json"`,
Wei Li1c66fc72022-05-09 23:59:14 -0700258 }),
259 }})
260}
261
Yu Liu4ae55d12022-01-05 17:17:23 -0800262func TestApexBundleCompileMultilibBoth(t *testing.T) {
263 runApexTestCase(t, bp2buildTestCase{
264 description: "apex - example with compile_multilib=both",
265 moduleTypeUnderTest: "apex",
266 moduleTypeUnderTestFactory: apex.BundleFactory,
Wei Li1c66fc72022-05-09 23:59:14 -0700267 filesystem: map[string]string{
268 "system/sepolicy/apex/Android.bp": `
269filegroup {
270 name: "com.android.apogee-file_contexts",
271 srcs: [ "apogee-file_contexts", ],
272 bazel_module: { bp2build_available: false },
273}
274`,
275 },
276 blueprint: createMultilibBlueprint("both"),
Yu Liu4ae55d12022-01-05 17:17:23 -0800277 expectedBazelTargets: []string{
278 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
279 "native_shared_libs_32": `[
280 ":native_shared_lib_1",
281 ":native_shared_lib_3",
282 ] + select({
283 "//build/bazel/platforms/arch:arm": [":native_shared_lib_2"],
284 "//build/bazel/platforms/arch:x86": [":native_shared_lib_2"],
285 "//conditions:default": [],
286 })`,
287 "native_shared_libs_64": `select({
288 "//build/bazel/platforms/arch:arm64": [
289 ":native_shared_lib_1",
290 ":native_shared_lib_4",
291 ":native_shared_lib_2",
292 ],
293 "//build/bazel/platforms/arch:x86_64": [
294 ":native_shared_lib_1",
295 ":native_shared_lib_4",
296 ":native_shared_lib_2",
297 ],
298 "//conditions:default": [],
299 })`,
Wei Li1c66fc72022-05-09 23:59:14 -0700300 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
Wei Li40f98732022-05-20 22:08:11 -0700301 "manifest": `"apex_manifest.json"`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800302 }),
303 }})
304}
305
306func TestApexBundleCompileMultilibFirst(t *testing.T) {
307 runApexTestCase(t, bp2buildTestCase{
308 description: "apex - example with compile_multilib=first",
309 moduleTypeUnderTest: "apex",
310 moduleTypeUnderTestFactory: apex.BundleFactory,
Wei Li1c66fc72022-05-09 23:59:14 -0700311 filesystem: map[string]string{
312 "system/sepolicy/apex/Android.bp": `
313filegroup {
314 name: "com.android.apogee-file_contexts",
315 srcs: [ "apogee-file_contexts", ],
316 bazel_module: { bp2build_available: false },
317}
318`,
319 },
320 blueprint: createMultilibBlueprint("first"),
Yu Liu4ae55d12022-01-05 17:17:23 -0800321 expectedBazelTargets: []string{
322 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
323 "native_shared_libs_32": `select({
324 "//build/bazel/platforms/arch:arm": [
325 ":native_shared_lib_1",
326 ":native_shared_lib_3",
327 ":native_shared_lib_2",
328 ],
329 "//build/bazel/platforms/arch:x86": [
330 ":native_shared_lib_1",
331 ":native_shared_lib_3",
332 ":native_shared_lib_2",
333 ],
334 "//conditions:default": [],
335 })`,
336 "native_shared_libs_64": `select({
337 "//build/bazel/platforms/arch:arm64": [
338 ":native_shared_lib_1",
339 ":native_shared_lib_4",
340 ":native_shared_lib_2",
341 ],
342 "//build/bazel/platforms/arch:x86_64": [
343 ":native_shared_lib_1",
344 ":native_shared_lib_4",
345 ":native_shared_lib_2",
346 ],
347 "//conditions:default": [],
348 })`,
Wei Li1c66fc72022-05-09 23:59:14 -0700349 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
Wei Li40f98732022-05-20 22:08:11 -0700350 "manifest": `"apex_manifest.json"`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800351 }),
352 }})
353}
354
355func TestApexBundleCompileMultilib32(t *testing.T) {
356 runApexTestCase(t, bp2buildTestCase{
357 description: "apex - example with compile_multilib=32",
358 moduleTypeUnderTest: "apex",
359 moduleTypeUnderTestFactory: apex.BundleFactory,
Wei Li1c66fc72022-05-09 23:59:14 -0700360 filesystem: map[string]string{
361 "system/sepolicy/apex/Android.bp": `
362filegroup {
363 name: "com.android.apogee-file_contexts",
364 srcs: [ "apogee-file_contexts", ],
365 bazel_module: { bp2build_available: false },
366}
367`,
368 },
369 blueprint: createMultilibBlueprint("32"),
Yu Liu4ae55d12022-01-05 17:17:23 -0800370 expectedBazelTargets: []string{
371 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
372 "native_shared_libs_32": `[
373 ":native_shared_lib_1",
374 ":native_shared_lib_3",
375 ] + select({
376 "//build/bazel/platforms/arch:arm": [":native_shared_lib_2"],
377 "//build/bazel/platforms/arch:x86": [":native_shared_lib_2"],
378 "//conditions:default": [],
379 })`,
Wei Li1c66fc72022-05-09 23:59:14 -0700380 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
Wei Li40f98732022-05-20 22:08:11 -0700381 "manifest": `"apex_manifest.json"`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800382 }),
383 }})
384}
385
386func TestApexBundleCompileMultilib64(t *testing.T) {
387 runApexTestCase(t, bp2buildTestCase{
388 description: "apex - example with compile_multilib=64",
389 moduleTypeUnderTest: "apex",
390 moduleTypeUnderTestFactory: apex.BundleFactory,
Wei Li1c66fc72022-05-09 23:59:14 -0700391 filesystem: map[string]string{
392 "system/sepolicy/apex/Android.bp": `
393filegroup {
394 name: "com.android.apogee-file_contexts",
395 srcs: [ "apogee-file_contexts", ],
396 bazel_module: { bp2build_available: false },
397}
398`,
399 },
400 blueprint: createMultilibBlueprint("64"),
Yu Liu4ae55d12022-01-05 17:17:23 -0800401 expectedBazelTargets: []string{
402 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
403 "native_shared_libs_64": `select({
404 "//build/bazel/platforms/arch:arm64": [
405 ":native_shared_lib_1",
406 ":native_shared_lib_4",
407 ":native_shared_lib_2",
408 ],
409 "//build/bazel/platforms/arch:x86_64": [
410 ":native_shared_lib_1",
411 ":native_shared_lib_4",
412 ":native_shared_lib_2",
413 ],
414 "//conditions:default": [],
415 })`,
Wei Li1c66fc72022-05-09 23:59:14 -0700416 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
Wei Li40f98732022-05-20 22:08:11 -0700417 "manifest": `"apex_manifest.json"`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800418 }),
419 }})
420}
421
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400422func TestApexBundleDefaultPropertyValues(t *testing.T) {
423 runApexTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400424 description: "apex - default property values",
425 moduleTypeUnderTest: "apex",
426 moduleTypeUnderTestFactory: apex.BundleFactory,
Wei Li1c66fc72022-05-09 23:59:14 -0700427 filesystem: map[string]string{
428 "system/sepolicy/apex/Android.bp": `
429filegroup {
430 name: "com.android.apogee-file_contexts",
431 srcs: [ "apogee-file_contexts", ],
432 bazel_module: { bp2build_available: false },
433}
434`,
435 },
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400436 blueprint: `
437apex {
438 name: "com.android.apogee",
439 manifest: "apogee_manifest.json",
440}
441`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500442 expectedBazelTargets: []string{makeBazelTarget("apex", "com.android.apogee", attrNameToString{
Wei Li1c66fc72022-05-09 23:59:14 -0700443 "manifest": `"apogee_manifest.json"`,
444 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500445 }),
446 }})
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400447}
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000448
449func TestApexBundleHasBazelModuleProps(t *testing.T) {
450 runApexTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400451 description: "apex - has bazel module props",
452 moduleTypeUnderTest: "apex",
453 moduleTypeUnderTestFactory: apex.BundleFactory,
Wei Li1c66fc72022-05-09 23:59:14 -0700454 filesystem: map[string]string{
455 "system/sepolicy/apex/Android.bp": `
456filegroup {
457 name: "apogee-file_contexts",
458 srcs: [ "apogee-file_contexts", ],
459 bazel_module: { bp2build_available: false },
460}
461`,
462 },
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000463 blueprint: `
464apex {
465 name: "apogee",
466 manifest: "manifest.json",
467 bazel_module: { bp2build_available: true },
468}
469`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500470 expectedBazelTargets: []string{makeBazelTarget("apex", "apogee", attrNameToString{
Wei Li1c66fc72022-05-09 23:59:14 -0700471 "manifest": `"manifest.json"`,
472 "file_contexts": `"//system/sepolicy/apex:apogee-file_contexts"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500473 }),
474 }})
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000475}
Yu Liu4ae55d12022-01-05 17:17:23 -0800476
477func createMultilibBlueprint(compile_multilib string) string {
478 return `
479cc_library {
480 name: "native_shared_lib_1",
481 bazel_module: { bp2build_available: false },
482}
483
484cc_library {
485 name: "native_shared_lib_2",
486 bazel_module: { bp2build_available: false },
487}
488
489cc_library {
490 name: "native_shared_lib_3",
491 bazel_module: { bp2build_available: false },
492}
493
494cc_library {
495 name: "native_shared_lib_4",
496 bazel_module: { bp2build_available: false },
497}
498
499apex {
500 name: "com.android.apogee",
501 compile_multilib: "` + compile_multilib + `",
502 multilib: {
503 both: {
504 native_shared_libs: [
505 "native_shared_lib_1",
506 ],
507 },
508 first: {
509 native_shared_libs: [
510 "native_shared_lib_2",
511 ],
512 },
513 lib32: {
514 native_shared_libs: [
515 "native_shared_lib_3",
516 ],
517 },
518 lib64: {
519 native_shared_libs: [
520 "native_shared_lib_4",
521 ],
522 },
523 },
524}`
525}
Wei Li1c66fc72022-05-09 23:59:14 -0700526
527func TestBp2BuildOverrideApex(t *testing.T) {
528 runOverrideApexTestCase(t, bp2buildTestCase{
529 description: "override_apex",
530 moduleTypeUnderTest: "override_apex",
531 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
532 filesystem: map[string]string{},
533 blueprint: `
534apex_key {
535 name: "com.android.apogee.key",
536 public_key: "com.android.apogee.avbpubkey",
537 private_key: "com.android.apogee.pem",
538 bazel_module: { bp2build_available: false },
539}
540
541android_app_certificate {
542 name: "com.android.apogee.certificate",
543 certificate: "com.android.apogee",
544 bazel_module: { bp2build_available: false },
545}
546
547cc_library {
548 name: "native_shared_lib_1",
549 bazel_module: { bp2build_available: false },
550}
551
552cc_library {
553 name: "native_shared_lib_2",
554 bazel_module: { bp2build_available: false },
555}
556
Jingwen Chen81c67d32022-06-08 16:08:25 +0000557prebuilt_etc {
558 name: "prebuilt_1",
Wei Li1c66fc72022-05-09 23:59:14 -0700559 bazel_module: { bp2build_available: false },
560}
561
Jingwen Chen81c67d32022-06-08 16:08:25 +0000562prebuilt_etc {
563 name: "prebuilt_2",
Wei Li1c66fc72022-05-09 23:59:14 -0700564 bazel_module: { bp2build_available: false },
565}
566
567filegroup {
568 name: "com.android.apogee-file_contexts",
569 srcs: [
570 "com.android.apogee-file_contexts",
571 ],
572 bazel_module: { bp2build_available: false },
573}
574
575cc_binary { name: "cc_binary_1", bazel_module: { bp2build_available: false } }
576sh_binary { name: "sh_binary_2", bazel_module: { bp2build_available: false } }
577
578apex {
579 name: "com.android.apogee",
580 manifest: "apogee_manifest.json",
581 androidManifest: "ApogeeAndroidManifest.xml",
582 file_contexts: ":com.android.apogee-file_contexts",
583 min_sdk_version: "29",
584 key: "com.android.apogee.key",
585 certificate: "com.android.apogee.certificate",
586 updatable: false,
587 installable: false,
588 compressible: false,
589 native_shared_libs: [
590 "native_shared_lib_1",
591 "native_shared_lib_2",
592 ],
593 binaries: [
594 "cc_binary_1",
595 "sh_binary_2",
596 ],
597 prebuilts: [
Jingwen Chen81c67d32022-06-08 16:08:25 +0000598 "prebuilt_1",
599 "prebuilt_2",
Wei Li1c66fc72022-05-09 23:59:14 -0700600 ],
601 bazel_module: { bp2build_available: false },
602}
603
604apex_key {
605 name: "com.google.android.apogee.key",
606 public_key: "com.google.android.apogee.avbpubkey",
607 private_key: "com.google.android.apogee.pem",
608 bazel_module: { bp2build_available: false },
609}
610
611android_app_certificate {
612 name: "com.google.android.apogee.certificate",
613 certificate: "com.google.android.apogee",
614 bazel_module: { bp2build_available: false },
615}
616
617override_apex {
618 name: "com.google.android.apogee",
619 base: ":com.android.apogee",
620 key: "com.google.android.apogee.key",
621 certificate: "com.google.android.apogee.certificate",
622 prebuilts: [],
623 compressible: true,
624}
625`,
626 expectedBazelTargets: []string{
627 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
628 "android_manifest": `"ApogeeAndroidManifest.xml"`,
629 "binaries": `[
630 ":cc_binary_1",
631 ":sh_binary_2",
632 ]`,
633 "certificate": `":com.google.android.apogee.certificate"`,
634 "file_contexts": `":com.android.apogee-file_contexts"`,
635 "installable": "False",
636 "key": `":com.google.android.apogee.key"`,
637 "manifest": `"apogee_manifest.json"`,
638 "min_sdk_version": `"29"`,
639 "native_shared_libs_32": `[
640 ":native_shared_lib_1",
641 ":native_shared_lib_2",
642 ]`,
643 "native_shared_libs_64": `select({
644 "//build/bazel/platforms/arch:arm64": [
645 ":native_shared_lib_1",
646 ":native_shared_lib_2",
647 ],
648 "//build/bazel/platforms/arch:x86_64": [
649 ":native_shared_lib_1",
650 ":native_shared_lib_2",
651 ],
652 "//conditions:default": [],
653 })`,
654 "prebuilts": `[]`,
655 "updatable": "False",
656 "compressible": "True",
657 }),
658 }})
659}
Wei Li40f98732022-05-20 22:08:11 -0700660
661func TestApexBundleSimple_manifestIsEmpty_baseApexOverrideApexInDifferentAndroidBp(t *testing.T) {
662 runOverrideApexTestCase(t, bp2buildTestCase{
663 description: "override_apex - manifest of base apex is empty, base apex and override_apex is in different Android.bp",
664 moduleTypeUnderTest: "override_apex",
665 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
666 filesystem: map[string]string{
667 "system/sepolicy/apex/Android.bp": `
668filegroup {
669 name: "com.android.apogee-file_contexts",
670 srcs: [ "apogee-file_contexts", ],
671 bazel_module: { bp2build_available: false },
672}`,
673 "a/b/Android.bp": `
674apex {
675 name: "com.android.apogee",
676 bazel_module: { bp2build_available: false },
677}
678`,
679 },
680 blueprint: `
681override_apex {
682 name: "com.google.android.apogee",
683 base: ":com.android.apogee",
684}
685`,
686 expectedBazelTargets: []string{
687 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
688 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
689 "manifest": `"//a/b:apex_manifest.json"`,
690 }),
691 }})
692}
693
694func TestApexBundleSimple_manifestIsSet_baseApexOverrideApexInDifferentAndroidBp(t *testing.T) {
695 runOverrideApexTestCase(t, bp2buildTestCase{
696 description: "override_apex - manifest of base apex is set, base apex and override_apex is in different Android.bp",
697 moduleTypeUnderTest: "override_apex",
698 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
699 filesystem: map[string]string{
700 "system/sepolicy/apex/Android.bp": `
701filegroup {
702 name: "com.android.apogee-file_contexts",
703 srcs: [ "apogee-file_contexts", ],
704 bazel_module: { bp2build_available: false },
705}`,
706 "a/b/Android.bp": `
707apex {
708 name: "com.android.apogee",
709 manifest: "apogee_manifest.json",
710 bazel_module: { bp2build_available: false },
711}
712`,
713 },
714 blueprint: `
715override_apex {
716 name: "com.google.android.apogee",
717 base: ":com.android.apogee",
718}
719`,
720 expectedBazelTargets: []string{
721 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
722 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
723 "manifest": `"//a/b:apogee_manifest.json"`,
724 }),
725 }})
726}
727
728func TestApexBundleSimple_manifestIsEmpty_baseApexOverrideApexInSameAndroidBp(t *testing.T) {
729 runOverrideApexTestCase(t, bp2buildTestCase{
730 description: "override_apex - manifest of base apex is empty, base apex and override_apex is in same Android.bp",
731 moduleTypeUnderTest: "override_apex",
732 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
733 filesystem: map[string]string{
734 "system/sepolicy/apex/Android.bp": `
735filegroup {
736 name: "com.android.apogee-file_contexts",
737 srcs: [ "apogee-file_contexts", ],
738 bazel_module: { bp2build_available: false },
739}`,
740 },
741 blueprint: `
742apex {
743 name: "com.android.apogee",
744 bazel_module: { bp2build_available: false },
745}
746
747override_apex {
748 name: "com.google.android.apogee",
749 base: ":com.android.apogee",
750}
751`,
752 expectedBazelTargets: []string{
753 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
754 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
755 "manifest": `"apex_manifest.json"`,
756 }),
757 }})
758}
759
760func TestApexBundleSimple_manifestIsSet_baseApexOverrideApexInSameAndroidBp(t *testing.T) {
761 runOverrideApexTestCase(t, bp2buildTestCase{
762 description: "override_apex - manifest of base apex is set, base apex and override_apex is in same Android.bp",
763 moduleTypeUnderTest: "override_apex",
764 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
765 filesystem: map[string]string{
766 "system/sepolicy/apex/Android.bp": `
767filegroup {
768 name: "com.android.apogee-file_contexts",
769 srcs: [ "apogee-file_contexts", ],
770 bazel_module: { bp2build_available: false },
771}`,
772 },
773 blueprint: `
774apex {
775 name: "com.android.apogee",
776 manifest: "apogee_manifest.json",
777 bazel_module: { bp2build_available: false },
778}
779
780override_apex {
781 name: "com.google.android.apogee",
782 base: ":com.android.apogee",
783}
784`,
785 expectedBazelTargets: []string{
786 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
787 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
788 "manifest": `"apogee_manifest.json"`,
789 }),
790 }})
791}
Jingwen Chen9b7ebca2022-06-03 09:11:20 +0000792
793func TestApexBundleSimple_packageNameOverride(t *testing.T) {
794 runOverrideApexTestCase(t, bp2buildTestCase{
795 description: "override_apex - override package name",
796 moduleTypeUnderTest: "override_apex",
797 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
798 filesystem: map[string]string{
799 "system/sepolicy/apex/Android.bp": `
800filegroup {
801 name: "com.android.apogee-file_contexts",
802 srcs: [ "apogee-file_contexts", ],
803 bazel_module: { bp2build_available: false },
804}`,
805 },
806 blueprint: `
807apex {
808 name: "com.android.apogee",
809 bazel_module: { bp2build_available: false },
810}
811
812override_apex {
813 name: "com.google.android.apogee",
814 base: ":com.android.apogee",
815 package_name: "com.google.android.apogee",
816}
817`,
818 expectedBazelTargets: []string{
819 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
820 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
821 "manifest": `"apex_manifest.json"`,
822 "package_name": `"com.google.android.apogee"`,
823 }),
824 }})
825}
Jingwen Chendf165c92022-06-08 16:00:39 +0000826
827func TestApexBundleSimple_NoPrebuiltsOverride(t *testing.T) {
828 runOverrideApexTestCase(t, bp2buildTestCase{
829 description: "override_apex - no override",
830 moduleTypeUnderTest: "override_apex",
831 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
832 filesystem: map[string]string{
833 "system/sepolicy/apex/Android.bp": `
834filegroup {
835 name: "com.android.apogee-file_contexts",
836 srcs: [ "apogee-file_contexts", ],
837 bazel_module: { bp2build_available: false },
838}`,
839 },
840 blueprint: `
841prebuilt_etc {
842 name: "prebuilt_file",
843 bazel_module: { bp2build_available: false },
844}
845
846apex {
847 name: "com.android.apogee",
848 bazel_module: { bp2build_available: false },
849 prebuilts: ["prebuilt_file"]
850}
851
852override_apex {
853 name: "com.google.android.apogee",
854 base: ":com.android.apogee",
855}
856`,
857 expectedBazelTargets: []string{
858 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
859 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
860 "manifest": `"apex_manifest.json"`,
861 "prebuilts": `[":prebuilt_file"]`,
862 }),
863 }})
864}
865
866func TestApexBundleSimple_PrebuiltsOverride(t *testing.T) {
867 runOverrideApexTestCase(t, bp2buildTestCase{
868 description: "override_apex - ooverride",
869 moduleTypeUnderTest: "override_apex",
870 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
871 filesystem: map[string]string{
872 "system/sepolicy/apex/Android.bp": `
873filegroup {
874 name: "com.android.apogee-file_contexts",
875 srcs: [ "apogee-file_contexts", ],
876 bazel_module: { bp2build_available: false },
877}`,
878 },
879 blueprint: `
880prebuilt_etc {
881 name: "prebuilt_file",
882 bazel_module: { bp2build_available: false },
883}
884
885prebuilt_etc {
886 name: "prebuilt_file2",
887 bazel_module: { bp2build_available: false },
888}
889
890apex {
891 name: "com.android.apogee",
892 bazel_module: { bp2build_available: false },
893 prebuilts: ["prebuilt_file"]
894}
895
896override_apex {
897 name: "com.google.android.apogee",
898 base: ":com.android.apogee",
899 prebuilts: ["prebuilt_file2"]
900}
901`,
902 expectedBazelTargets: []string{
903 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
904 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
905 "manifest": `"apex_manifest.json"`,
906 "prebuilts": `[":prebuilt_file2"]`,
907 }),
908 }})
909}
910
911func TestApexBundleSimple_PrebuiltsOverrideEmptyList(t *testing.T) {
912 runOverrideApexTestCase(t, bp2buildTestCase{
913 description: "override_apex - override with empty list",
914 moduleTypeUnderTest: "override_apex",
915 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
916 filesystem: map[string]string{
917 "system/sepolicy/apex/Android.bp": `
918filegroup {
919 name: "com.android.apogee-file_contexts",
920 srcs: [ "apogee-file_contexts", ],
921 bazel_module: { bp2build_available: false },
922}`,
923 },
924 blueprint: `
925prebuilt_etc {
926 name: "prebuilt_file",
927 bazel_module: { bp2build_available: false },
928}
929
930apex {
931 name: "com.android.apogee",
932 bazel_module: { bp2build_available: false },
933 prebuilts: ["prebuilt_file"]
934}
935
936override_apex {
937 name: "com.google.android.apogee",
938 base: ":com.android.apogee",
939 prebuilts: [],
940}
941`,
942 expectedBazelTargets: []string{
943 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
944 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
945 "manifest": `"apex_manifest.json"`,
946 "prebuilts": `[]`,
947 }),
948 }})
949}
Jingwen Chenb732d7c2022-06-10 08:14:19 +0000950
951func TestApexBundleSimple_NoLoggingParentOverride(t *testing.T) {
952 runOverrideApexTestCase(t, bp2buildTestCase{
953 description: "override_apex - logging_parent - no override",
954 moduleTypeUnderTest: "override_apex",
955 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
956 filesystem: map[string]string{
957 "system/sepolicy/apex/Android.bp": `
958filegroup {
959 name: "com.android.apogee-file_contexts",
960 srcs: [ "apogee-file_contexts", ],
961 bazel_module: { bp2build_available: false },
962}`,
963 },
964 blueprint: `
965apex {
966 name: "com.android.apogee",
967 bazel_module: { bp2build_available: false },
968 logging_parent: "foo.bar.baz",
969}
970
971override_apex {
972 name: "com.google.android.apogee",
973 base: ":com.android.apogee",
974}
975`,
976 expectedBazelTargets: []string{
977 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
978 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
979 "manifest": `"apex_manifest.json"`,
980 "logging_parent": `"foo.bar.baz"`,
981 }),
982 }})
983}
984
985func TestApexBundleSimple_LoggingParentOverride(t *testing.T) {
986 runOverrideApexTestCase(t, bp2buildTestCase{
987 description: "override_apex - logging_parent - override",
988 moduleTypeUnderTest: "override_apex",
989 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
990 filesystem: map[string]string{
991 "system/sepolicy/apex/Android.bp": `
992filegroup {
993 name: "com.android.apogee-file_contexts",
994 srcs: [ "apogee-file_contexts", ],
995 bazel_module: { bp2build_available: false },
996}`,
997 },
998 blueprint: `
999apex {
1000 name: "com.android.apogee",
1001 bazel_module: { bp2build_available: false },
1002 logging_parent: "foo.bar.baz",
1003}
1004
1005override_apex {
1006 name: "com.google.android.apogee",
1007 base: ":com.android.apogee",
1008 logging_parent: "foo.bar.baz.override",
1009}
1010`,
1011 expectedBazelTargets: []string{
1012 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
1013 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
1014 "manifest": `"apex_manifest.json"`,
1015 "logging_parent": `"foo.bar.baz.override"`,
1016 }),
1017 }})
1018}