blob: 05045eec0f5759f9540aa1ad9b477cec04bfc466 [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",
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400140}
141`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500142 expectedBazelTargets: []string{
143 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
144 "android_manifest": `"ApogeeAndroidManifest.xml"`,
145 "binaries": `[
Jingwen Chenb07c9012021-12-08 10:05:45 +0000146 ":cc_binary_1",
147 ":sh_binary_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500148 ]`,
149 "certificate": `":com.android.apogee.certificate"`,
150 "file_contexts": `":com.android.apogee-file_contexts"`,
151 "installable": "False",
152 "key": `":com.android.apogee.key"`,
153 "manifest": `"apogee_manifest.json"`,
154 "min_sdk_version": `"29"`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800155 "native_shared_libs_32": `[
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400156 ":native_shared_lib_1",
157 ":native_shared_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500158 ]`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800159 "native_shared_libs_64": `select({
160 "//build/bazel/platforms/arch:arm64": [
161 ":native_shared_lib_1",
162 ":native_shared_lib_2",
163 ],
164 "//build/bazel/platforms/arch:x86_64": [
165 ":native_shared_lib_1",
166 ":native_shared_lib_2",
167 ],
168 "//conditions:default": [],
169 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500170 "prebuilts": `[
Jingwen Chen81c67d32022-06-08 16:08:25 +0000171 ":prebuilt_1",
172 ":prebuilt_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500173 ]`,
Wei Lif034cb42022-01-19 15:54:31 -0800174 "updatable": "False",
175 "compressible": "False",
Jingwen Chen9b7ebca2022-06-03 09:11:20 +0000176 "package_name": `"com.android.apogee.test.package"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500177 }),
178 }})
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400179}
180
Wei Li1c66fc72022-05-09 23:59:14 -0700181func TestApexBundleSimple_fileContextsInAnotherAndroidBp(t *testing.T) {
182 runApexTestCase(t, bp2buildTestCase{
183 description: "apex - file contexts is a module in another Android.bp",
184 moduleTypeUnderTest: "apex",
185 moduleTypeUnderTestFactory: apex.BundleFactory,
186 filesystem: map[string]string{
187 "a/b/Android.bp": `
188filegroup {
189 name: "com.android.apogee-file_contexts",
190 srcs: [
191 "com.android.apogee-file_contexts",
192 ],
193 bazel_module: { bp2build_available: false },
194}
195`,
196 },
197 blueprint: `
198apex {
199 name: "com.android.apogee",
200 file_contexts: ":com.android.apogee-file_contexts",
201}
202`,
203 expectedBazelTargets: []string{
204 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
205 "file_contexts": `"//a/b:com.android.apogee-file_contexts"`,
Wei Li40f98732022-05-20 22:08:11 -0700206 "manifest": `"apex_manifest.json"`,
Wei Li1c66fc72022-05-09 23:59:14 -0700207 }),
208 }})
209}
210
211func TestApexBundleSimple_fileContextsIsFile(t *testing.T) {
212 runApexTestCase(t, bp2buildTestCase{
213 description: "apex - file contexts is a file",
214 moduleTypeUnderTest: "apex",
215 moduleTypeUnderTestFactory: apex.BundleFactory,
216 filesystem: map[string]string{},
217 blueprint: `
218apex {
219 name: "com.android.apogee",
220 file_contexts: "file_contexts_file",
221}
222`,
223 expectedBazelTargets: []string{
224 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
225 "file_contexts": `"file_contexts_file"`,
Wei Li40f98732022-05-20 22:08:11 -0700226 "manifest": `"apex_manifest.json"`,
Wei Li1c66fc72022-05-09 23:59:14 -0700227 }),
228 }})
229}
230
231func TestApexBundleSimple_fileContextsIsNotSpecified(t *testing.T) {
232 runApexTestCase(t, bp2buildTestCase{
233 description: "apex - file contexts is not specified",
234 moduleTypeUnderTest: "apex",
235 moduleTypeUnderTestFactory: apex.BundleFactory,
236 filesystem: map[string]string{
237 "system/sepolicy/apex/Android.bp": `
238filegroup {
239 name: "com.android.apogee-file_contexts",
240 srcs: [
241 "com.android.apogee-file_contexts",
242 ],
243 bazel_module: { bp2build_available: false },
244}
245`,
246 },
247 blueprint: `
248apex {
249 name: "com.android.apogee",
250}
251`,
252 expectedBazelTargets: []string{
253 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
254 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
Wei Li40f98732022-05-20 22:08:11 -0700255 "manifest": `"apex_manifest.json"`,
Wei Li1c66fc72022-05-09 23:59:14 -0700256 }),
257 }})
258}
259
Yu Liu4ae55d12022-01-05 17:17:23 -0800260func TestApexBundleCompileMultilibBoth(t *testing.T) {
261 runApexTestCase(t, bp2buildTestCase{
262 description: "apex - example with compile_multilib=both",
263 moduleTypeUnderTest: "apex",
264 moduleTypeUnderTestFactory: apex.BundleFactory,
Wei Li1c66fc72022-05-09 23:59:14 -0700265 filesystem: map[string]string{
266 "system/sepolicy/apex/Android.bp": `
267filegroup {
268 name: "com.android.apogee-file_contexts",
269 srcs: [ "apogee-file_contexts", ],
270 bazel_module: { bp2build_available: false },
271}
272`,
273 },
274 blueprint: createMultilibBlueprint("both"),
Yu Liu4ae55d12022-01-05 17:17:23 -0800275 expectedBazelTargets: []string{
276 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
277 "native_shared_libs_32": `[
278 ":native_shared_lib_1",
279 ":native_shared_lib_3",
280 ] + select({
281 "//build/bazel/platforms/arch:arm": [":native_shared_lib_2"],
282 "//build/bazel/platforms/arch:x86": [":native_shared_lib_2"],
283 "//conditions:default": [],
284 })`,
285 "native_shared_libs_64": `select({
286 "//build/bazel/platforms/arch:arm64": [
287 ":native_shared_lib_1",
288 ":native_shared_lib_4",
289 ":native_shared_lib_2",
290 ],
291 "//build/bazel/platforms/arch:x86_64": [
292 ":native_shared_lib_1",
293 ":native_shared_lib_4",
294 ":native_shared_lib_2",
295 ],
296 "//conditions:default": [],
297 })`,
Wei Li1c66fc72022-05-09 23:59:14 -0700298 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
Wei Li40f98732022-05-20 22:08:11 -0700299 "manifest": `"apex_manifest.json"`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800300 }),
301 }})
302}
303
304func TestApexBundleCompileMultilibFirst(t *testing.T) {
305 runApexTestCase(t, bp2buildTestCase{
306 description: "apex - example with compile_multilib=first",
307 moduleTypeUnderTest: "apex",
308 moduleTypeUnderTestFactory: apex.BundleFactory,
Wei Li1c66fc72022-05-09 23:59:14 -0700309 filesystem: map[string]string{
310 "system/sepolicy/apex/Android.bp": `
311filegroup {
312 name: "com.android.apogee-file_contexts",
313 srcs: [ "apogee-file_contexts", ],
314 bazel_module: { bp2build_available: false },
315}
316`,
317 },
318 blueprint: createMultilibBlueprint("first"),
Yu Liu4ae55d12022-01-05 17:17:23 -0800319 expectedBazelTargets: []string{
320 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
321 "native_shared_libs_32": `select({
322 "//build/bazel/platforms/arch:arm": [
323 ":native_shared_lib_1",
324 ":native_shared_lib_3",
325 ":native_shared_lib_2",
326 ],
327 "//build/bazel/platforms/arch:x86": [
328 ":native_shared_lib_1",
329 ":native_shared_lib_3",
330 ":native_shared_lib_2",
331 ],
332 "//conditions:default": [],
333 })`,
334 "native_shared_libs_64": `select({
335 "//build/bazel/platforms/arch:arm64": [
336 ":native_shared_lib_1",
337 ":native_shared_lib_4",
338 ":native_shared_lib_2",
339 ],
340 "//build/bazel/platforms/arch:x86_64": [
341 ":native_shared_lib_1",
342 ":native_shared_lib_4",
343 ":native_shared_lib_2",
344 ],
345 "//conditions:default": [],
346 })`,
Wei Li1c66fc72022-05-09 23:59:14 -0700347 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
Wei Li40f98732022-05-20 22:08:11 -0700348 "manifest": `"apex_manifest.json"`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800349 }),
350 }})
351}
352
353func TestApexBundleCompileMultilib32(t *testing.T) {
354 runApexTestCase(t, bp2buildTestCase{
355 description: "apex - example with compile_multilib=32",
356 moduleTypeUnderTest: "apex",
357 moduleTypeUnderTestFactory: apex.BundleFactory,
Wei Li1c66fc72022-05-09 23:59:14 -0700358 filesystem: map[string]string{
359 "system/sepolicy/apex/Android.bp": `
360filegroup {
361 name: "com.android.apogee-file_contexts",
362 srcs: [ "apogee-file_contexts", ],
363 bazel_module: { bp2build_available: false },
364}
365`,
366 },
367 blueprint: createMultilibBlueprint("32"),
Yu Liu4ae55d12022-01-05 17:17:23 -0800368 expectedBazelTargets: []string{
369 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
370 "native_shared_libs_32": `[
371 ":native_shared_lib_1",
372 ":native_shared_lib_3",
373 ] + select({
374 "//build/bazel/platforms/arch:arm": [":native_shared_lib_2"],
375 "//build/bazel/platforms/arch:x86": [":native_shared_lib_2"],
376 "//conditions:default": [],
377 })`,
Wei Li1c66fc72022-05-09 23:59:14 -0700378 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
Wei Li40f98732022-05-20 22:08:11 -0700379 "manifest": `"apex_manifest.json"`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800380 }),
381 }})
382}
383
384func TestApexBundleCompileMultilib64(t *testing.T) {
385 runApexTestCase(t, bp2buildTestCase{
386 description: "apex - example with compile_multilib=64",
387 moduleTypeUnderTest: "apex",
388 moduleTypeUnderTestFactory: apex.BundleFactory,
Wei Li1c66fc72022-05-09 23:59:14 -0700389 filesystem: map[string]string{
390 "system/sepolicy/apex/Android.bp": `
391filegroup {
392 name: "com.android.apogee-file_contexts",
393 srcs: [ "apogee-file_contexts", ],
394 bazel_module: { bp2build_available: false },
395}
396`,
397 },
398 blueprint: createMultilibBlueprint("64"),
Yu Liu4ae55d12022-01-05 17:17:23 -0800399 expectedBazelTargets: []string{
400 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
401 "native_shared_libs_64": `select({
402 "//build/bazel/platforms/arch:arm64": [
403 ":native_shared_lib_1",
404 ":native_shared_lib_4",
405 ":native_shared_lib_2",
406 ],
407 "//build/bazel/platforms/arch:x86_64": [
408 ":native_shared_lib_1",
409 ":native_shared_lib_4",
410 ":native_shared_lib_2",
411 ],
412 "//conditions:default": [],
413 })`,
Wei Li1c66fc72022-05-09 23:59:14 -0700414 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
Wei Li40f98732022-05-20 22:08:11 -0700415 "manifest": `"apex_manifest.json"`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800416 }),
417 }})
418}
419
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400420func TestApexBundleDefaultPropertyValues(t *testing.T) {
421 runApexTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400422 description: "apex - default property values",
423 moduleTypeUnderTest: "apex",
424 moduleTypeUnderTestFactory: apex.BundleFactory,
Wei Li1c66fc72022-05-09 23:59:14 -0700425 filesystem: map[string]string{
426 "system/sepolicy/apex/Android.bp": `
427filegroup {
428 name: "com.android.apogee-file_contexts",
429 srcs: [ "apogee-file_contexts", ],
430 bazel_module: { bp2build_available: false },
431}
432`,
433 },
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400434 blueprint: `
435apex {
436 name: "com.android.apogee",
437 manifest: "apogee_manifest.json",
438}
439`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500440 expectedBazelTargets: []string{makeBazelTarget("apex", "com.android.apogee", attrNameToString{
Wei Li1c66fc72022-05-09 23:59:14 -0700441 "manifest": `"apogee_manifest.json"`,
442 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500443 }),
444 }})
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400445}
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000446
447func TestApexBundleHasBazelModuleProps(t *testing.T) {
448 runApexTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400449 description: "apex - has bazel module props",
450 moduleTypeUnderTest: "apex",
451 moduleTypeUnderTestFactory: apex.BundleFactory,
Wei Li1c66fc72022-05-09 23:59:14 -0700452 filesystem: map[string]string{
453 "system/sepolicy/apex/Android.bp": `
454filegroup {
455 name: "apogee-file_contexts",
456 srcs: [ "apogee-file_contexts", ],
457 bazel_module: { bp2build_available: false },
458}
459`,
460 },
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000461 blueprint: `
462apex {
463 name: "apogee",
464 manifest: "manifest.json",
465 bazel_module: { bp2build_available: true },
466}
467`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500468 expectedBazelTargets: []string{makeBazelTarget("apex", "apogee", attrNameToString{
Wei Li1c66fc72022-05-09 23:59:14 -0700469 "manifest": `"manifest.json"`,
470 "file_contexts": `"//system/sepolicy/apex:apogee-file_contexts"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500471 }),
472 }})
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000473}
Yu Liu4ae55d12022-01-05 17:17:23 -0800474
475func createMultilibBlueprint(compile_multilib string) string {
476 return `
477cc_library {
478 name: "native_shared_lib_1",
479 bazel_module: { bp2build_available: false },
480}
481
482cc_library {
483 name: "native_shared_lib_2",
484 bazel_module: { bp2build_available: false },
485}
486
487cc_library {
488 name: "native_shared_lib_3",
489 bazel_module: { bp2build_available: false },
490}
491
492cc_library {
493 name: "native_shared_lib_4",
494 bazel_module: { bp2build_available: false },
495}
496
497apex {
498 name: "com.android.apogee",
499 compile_multilib: "` + compile_multilib + `",
500 multilib: {
501 both: {
502 native_shared_libs: [
503 "native_shared_lib_1",
504 ],
505 },
506 first: {
507 native_shared_libs: [
508 "native_shared_lib_2",
509 ],
510 },
511 lib32: {
512 native_shared_libs: [
513 "native_shared_lib_3",
514 ],
515 },
516 lib64: {
517 native_shared_libs: [
518 "native_shared_lib_4",
519 ],
520 },
521 },
522}`
523}
Wei Li1c66fc72022-05-09 23:59:14 -0700524
525func TestBp2BuildOverrideApex(t *testing.T) {
526 runOverrideApexTestCase(t, bp2buildTestCase{
527 description: "override_apex",
528 moduleTypeUnderTest: "override_apex",
529 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
530 filesystem: map[string]string{},
531 blueprint: `
532apex_key {
533 name: "com.android.apogee.key",
534 public_key: "com.android.apogee.avbpubkey",
535 private_key: "com.android.apogee.pem",
536 bazel_module: { bp2build_available: false },
537}
538
539android_app_certificate {
540 name: "com.android.apogee.certificate",
541 certificate: "com.android.apogee",
542 bazel_module: { bp2build_available: false },
543}
544
545cc_library {
546 name: "native_shared_lib_1",
547 bazel_module: { bp2build_available: false },
548}
549
550cc_library {
551 name: "native_shared_lib_2",
552 bazel_module: { bp2build_available: false },
553}
554
Jingwen Chen81c67d32022-06-08 16:08:25 +0000555prebuilt_etc {
556 name: "prebuilt_1",
Wei Li1c66fc72022-05-09 23:59:14 -0700557 bazel_module: { bp2build_available: false },
558}
559
Jingwen Chen81c67d32022-06-08 16:08:25 +0000560prebuilt_etc {
561 name: "prebuilt_2",
Wei Li1c66fc72022-05-09 23:59:14 -0700562 bazel_module: { bp2build_available: false },
563}
564
565filegroup {
566 name: "com.android.apogee-file_contexts",
567 srcs: [
568 "com.android.apogee-file_contexts",
569 ],
570 bazel_module: { bp2build_available: false },
571}
572
573cc_binary { name: "cc_binary_1", bazel_module: { bp2build_available: false } }
574sh_binary { name: "sh_binary_2", bazel_module: { bp2build_available: false } }
575
576apex {
577 name: "com.android.apogee",
578 manifest: "apogee_manifest.json",
579 androidManifest: "ApogeeAndroidManifest.xml",
580 file_contexts: ":com.android.apogee-file_contexts",
581 min_sdk_version: "29",
582 key: "com.android.apogee.key",
583 certificate: "com.android.apogee.certificate",
584 updatable: false,
585 installable: false,
586 compressible: false,
587 native_shared_libs: [
588 "native_shared_lib_1",
589 "native_shared_lib_2",
590 ],
591 binaries: [
592 "cc_binary_1",
593 "sh_binary_2",
594 ],
595 prebuilts: [
Jingwen Chen81c67d32022-06-08 16:08:25 +0000596 "prebuilt_1",
597 "prebuilt_2",
Wei Li1c66fc72022-05-09 23:59:14 -0700598 ],
599 bazel_module: { bp2build_available: false },
600}
601
602apex_key {
603 name: "com.google.android.apogee.key",
604 public_key: "com.google.android.apogee.avbpubkey",
605 private_key: "com.google.android.apogee.pem",
606 bazel_module: { bp2build_available: false },
607}
608
609android_app_certificate {
610 name: "com.google.android.apogee.certificate",
611 certificate: "com.google.android.apogee",
612 bazel_module: { bp2build_available: false },
613}
614
615override_apex {
616 name: "com.google.android.apogee",
617 base: ":com.android.apogee",
618 key: "com.google.android.apogee.key",
619 certificate: "com.google.android.apogee.certificate",
620 prebuilts: [],
621 compressible: true,
622}
623`,
624 expectedBazelTargets: []string{
625 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
626 "android_manifest": `"ApogeeAndroidManifest.xml"`,
627 "binaries": `[
628 ":cc_binary_1",
629 ":sh_binary_2",
630 ]`,
631 "certificate": `":com.google.android.apogee.certificate"`,
632 "file_contexts": `":com.android.apogee-file_contexts"`,
633 "installable": "False",
634 "key": `":com.google.android.apogee.key"`,
635 "manifest": `"apogee_manifest.json"`,
636 "min_sdk_version": `"29"`,
637 "native_shared_libs_32": `[
638 ":native_shared_lib_1",
639 ":native_shared_lib_2",
640 ]`,
641 "native_shared_libs_64": `select({
642 "//build/bazel/platforms/arch:arm64": [
643 ":native_shared_lib_1",
644 ":native_shared_lib_2",
645 ],
646 "//build/bazel/platforms/arch:x86_64": [
647 ":native_shared_lib_1",
648 ":native_shared_lib_2",
649 ],
650 "//conditions:default": [],
651 })`,
652 "prebuilts": `[]`,
653 "updatable": "False",
654 "compressible": "True",
655 }),
656 }})
657}
Wei Li40f98732022-05-20 22:08:11 -0700658
659func TestApexBundleSimple_manifestIsEmpty_baseApexOverrideApexInDifferentAndroidBp(t *testing.T) {
660 runOverrideApexTestCase(t, bp2buildTestCase{
661 description: "override_apex - manifest of base apex is empty, base apex and override_apex is in different Android.bp",
662 moduleTypeUnderTest: "override_apex",
663 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
664 filesystem: map[string]string{
665 "system/sepolicy/apex/Android.bp": `
666filegroup {
667 name: "com.android.apogee-file_contexts",
668 srcs: [ "apogee-file_contexts", ],
669 bazel_module: { bp2build_available: false },
670}`,
671 "a/b/Android.bp": `
672apex {
673 name: "com.android.apogee",
674 bazel_module: { bp2build_available: false },
675}
676`,
677 },
678 blueprint: `
679override_apex {
680 name: "com.google.android.apogee",
681 base: ":com.android.apogee",
682}
683`,
684 expectedBazelTargets: []string{
685 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
686 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
687 "manifest": `"//a/b:apex_manifest.json"`,
688 }),
689 }})
690}
691
692func TestApexBundleSimple_manifestIsSet_baseApexOverrideApexInDifferentAndroidBp(t *testing.T) {
693 runOverrideApexTestCase(t, bp2buildTestCase{
694 description: "override_apex - manifest of base apex is set, base apex and override_apex is in different Android.bp",
695 moduleTypeUnderTest: "override_apex",
696 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
697 filesystem: map[string]string{
698 "system/sepolicy/apex/Android.bp": `
699filegroup {
700 name: "com.android.apogee-file_contexts",
701 srcs: [ "apogee-file_contexts", ],
702 bazel_module: { bp2build_available: false },
703}`,
704 "a/b/Android.bp": `
705apex {
706 name: "com.android.apogee",
707 manifest: "apogee_manifest.json",
708 bazel_module: { bp2build_available: false },
709}
710`,
711 },
712 blueprint: `
713override_apex {
714 name: "com.google.android.apogee",
715 base: ":com.android.apogee",
716}
717`,
718 expectedBazelTargets: []string{
719 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
720 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
721 "manifest": `"//a/b:apogee_manifest.json"`,
722 }),
723 }})
724}
725
726func TestApexBundleSimple_manifestIsEmpty_baseApexOverrideApexInSameAndroidBp(t *testing.T) {
727 runOverrideApexTestCase(t, bp2buildTestCase{
728 description: "override_apex - manifest of base apex is empty, base apex and override_apex is in same Android.bp",
729 moduleTypeUnderTest: "override_apex",
730 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
731 filesystem: map[string]string{
732 "system/sepolicy/apex/Android.bp": `
733filegroup {
734 name: "com.android.apogee-file_contexts",
735 srcs: [ "apogee-file_contexts", ],
736 bazel_module: { bp2build_available: false },
737}`,
738 },
739 blueprint: `
740apex {
741 name: "com.android.apogee",
742 bazel_module: { bp2build_available: false },
743}
744
745override_apex {
746 name: "com.google.android.apogee",
747 base: ":com.android.apogee",
748}
749`,
750 expectedBazelTargets: []string{
751 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
752 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
753 "manifest": `"apex_manifest.json"`,
754 }),
755 }})
756}
757
758func TestApexBundleSimple_manifestIsSet_baseApexOverrideApexInSameAndroidBp(t *testing.T) {
759 runOverrideApexTestCase(t, bp2buildTestCase{
760 description: "override_apex - manifest of base apex is set, base apex and override_apex is in same Android.bp",
761 moduleTypeUnderTest: "override_apex",
762 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
763 filesystem: map[string]string{
764 "system/sepolicy/apex/Android.bp": `
765filegroup {
766 name: "com.android.apogee-file_contexts",
767 srcs: [ "apogee-file_contexts", ],
768 bazel_module: { bp2build_available: false },
769}`,
770 },
771 blueprint: `
772apex {
773 name: "com.android.apogee",
774 manifest: "apogee_manifest.json",
775 bazel_module: { bp2build_available: false },
776}
777
778override_apex {
779 name: "com.google.android.apogee",
780 base: ":com.android.apogee",
781}
782`,
783 expectedBazelTargets: []string{
784 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
785 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
786 "manifest": `"apogee_manifest.json"`,
787 }),
788 }})
789}
Jingwen Chen9b7ebca2022-06-03 09:11:20 +0000790
791func TestApexBundleSimple_packageNameOverride(t *testing.T) {
792 runOverrideApexTestCase(t, bp2buildTestCase{
793 description: "override_apex - override package name",
794 moduleTypeUnderTest: "override_apex",
795 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
796 filesystem: map[string]string{
797 "system/sepolicy/apex/Android.bp": `
798filegroup {
799 name: "com.android.apogee-file_contexts",
800 srcs: [ "apogee-file_contexts", ],
801 bazel_module: { bp2build_available: false },
802}`,
803 },
804 blueprint: `
805apex {
806 name: "com.android.apogee",
807 bazel_module: { bp2build_available: false },
808}
809
810override_apex {
811 name: "com.google.android.apogee",
812 base: ":com.android.apogee",
813 package_name: "com.google.android.apogee",
814}
815`,
816 expectedBazelTargets: []string{
817 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
818 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
819 "manifest": `"apex_manifest.json"`,
820 "package_name": `"com.google.android.apogee"`,
821 }),
822 }})
823}
Jingwen Chendf165c92022-06-08 16:00:39 +0000824
825func TestApexBundleSimple_NoPrebuiltsOverride(t *testing.T) {
826 runOverrideApexTestCase(t, bp2buildTestCase{
827 description: "override_apex - no override",
828 moduleTypeUnderTest: "override_apex",
829 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
830 filesystem: map[string]string{
831 "system/sepolicy/apex/Android.bp": `
832filegroup {
833 name: "com.android.apogee-file_contexts",
834 srcs: [ "apogee-file_contexts", ],
835 bazel_module: { bp2build_available: false },
836}`,
837 },
838 blueprint: `
839prebuilt_etc {
840 name: "prebuilt_file",
841 bazel_module: { bp2build_available: false },
842}
843
844apex {
845 name: "com.android.apogee",
846 bazel_module: { bp2build_available: false },
847 prebuilts: ["prebuilt_file"]
848}
849
850override_apex {
851 name: "com.google.android.apogee",
852 base: ":com.android.apogee",
853}
854`,
855 expectedBazelTargets: []string{
856 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
857 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
858 "manifest": `"apex_manifest.json"`,
859 "prebuilts": `[":prebuilt_file"]`,
860 }),
861 }})
862}
863
864func TestApexBundleSimple_PrebuiltsOverride(t *testing.T) {
865 runOverrideApexTestCase(t, bp2buildTestCase{
866 description: "override_apex - ooverride",
867 moduleTypeUnderTest: "override_apex",
868 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
869 filesystem: map[string]string{
870 "system/sepolicy/apex/Android.bp": `
871filegroup {
872 name: "com.android.apogee-file_contexts",
873 srcs: [ "apogee-file_contexts", ],
874 bazel_module: { bp2build_available: false },
875}`,
876 },
877 blueprint: `
878prebuilt_etc {
879 name: "prebuilt_file",
880 bazel_module: { bp2build_available: false },
881}
882
883prebuilt_etc {
884 name: "prebuilt_file2",
885 bazel_module: { bp2build_available: false },
886}
887
888apex {
889 name: "com.android.apogee",
890 bazel_module: { bp2build_available: false },
891 prebuilts: ["prebuilt_file"]
892}
893
894override_apex {
895 name: "com.google.android.apogee",
896 base: ":com.android.apogee",
897 prebuilts: ["prebuilt_file2"]
898}
899`,
900 expectedBazelTargets: []string{
901 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
902 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
903 "manifest": `"apex_manifest.json"`,
904 "prebuilts": `[":prebuilt_file2"]`,
905 }),
906 }})
907}
908
909func TestApexBundleSimple_PrebuiltsOverrideEmptyList(t *testing.T) {
910 runOverrideApexTestCase(t, bp2buildTestCase{
911 description: "override_apex - override with empty list",
912 moduleTypeUnderTest: "override_apex",
913 moduleTypeUnderTestFactory: apex.OverrideApexFactory,
914 filesystem: map[string]string{
915 "system/sepolicy/apex/Android.bp": `
916filegroup {
917 name: "com.android.apogee-file_contexts",
918 srcs: [ "apogee-file_contexts", ],
919 bazel_module: { bp2build_available: false },
920}`,
921 },
922 blueprint: `
923prebuilt_etc {
924 name: "prebuilt_file",
925 bazel_module: { bp2build_available: false },
926}
927
928apex {
929 name: "com.android.apogee",
930 bazel_module: { bp2build_available: false },
931 prebuilts: ["prebuilt_file"]
932}
933
934override_apex {
935 name: "com.google.android.apogee",
936 base: ":com.android.apogee",
937 prebuilts: [],
938}
939`,
940 expectedBazelTargets: []string{
941 makeBazelTarget("apex", "com.google.android.apogee", attrNameToString{
942 "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`,
943 "manifest": `"apex_manifest.json"`,
944 "prebuilts": `[]`,
945 }),
946 }})
947}