blob: c5f5f795c632726f006fdf847c4779643ad088cc [file] [log] [blame]
Jingwen Chen537242c2022-08-24 11:53:27 +00001// 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
15package bp2build
16
17import (
18 "fmt"
19 "testing"
20
21 "android/soong/android"
22 "android/soong/cc"
23 "android/soong/genrule"
24)
25
26type ccTestBp2buildTestCase struct {
Chris Parsonscd209032023-09-19 01:12:48 +000027 description string
28 blueprint string
29 filesystem map[string]string
30 targets []testBazelTarget
31 stubbedBuildDefinitions []string
Jingwen Chen537242c2022-08-24 11:53:27 +000032}
33
34func registerCcTestModuleTypes(ctx android.RegistrationContext) {
35 cc.RegisterCCBuildComponents(ctx)
36 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
37 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
38 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
39 ctx.RegisterModuleType("cc_test_library", cc.TestLibraryFactory)
40 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
41}
42
43func runCcTestTestCase(t *testing.T, testCase ccTestBp2buildTestCase) {
44 t.Helper()
45 moduleTypeUnderTest := "cc_test"
Jingwen Chen537242c2022-08-24 11:53:27 +000046 description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description)
47 t.Run(description, func(t *testing.T) {
48 t.Helper()
49 RunBp2BuildTestCase(t, registerCcTestModuleTypes, Bp2buildTestCase{
50 ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.HostAndDeviceSupported),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +000051 Filesystem: testCase.filesystem,
Jingwen Chen537242c2022-08-24 11:53:27 +000052 ModuleTypeUnderTest: moduleTypeUnderTest,
53 ModuleTypeUnderTestFactory: cc.TestFactory,
54 Description: description,
55 Blueprint: testCase.blueprint,
Chris Parsonscd209032023-09-19 01:12:48 +000056 StubbedBuildDefinitions: testCase.stubbedBuildDefinitions,
Jingwen Chen537242c2022-08-24 11:53:27 +000057 })
58 })
59}
60
61func TestBasicCcTest(t *testing.T) {
62 runCcTestTestCase(t, ccTestBp2buildTestCase{
63 description: "basic cc_test with commonly used attributes",
Chris Parsonscd209032023-09-19 01:12:48 +000064 stubbedBuildDefinitions: []string{"libbuildversion", "libprotobuf-cpp-lite", "libprotobuf-cpp-full",
65 "foolib", "hostlib", "data_mod", "cc_bin", "cc_lib", "cc_test_lib2", "libgtest_main", "libgtest"},
Jingwen Chen537242c2022-08-24 11:53:27 +000066 blueprint: `
67cc_test {
68 name: "mytest",
69 host_supported: true,
70 srcs: ["test.cpp"],
71 target: {
72 android: {
73 srcs: ["android.cpp"],
74 shared_libs: ["foolib"],
75 },
76 linux: {
77 srcs: ["linux.cpp"],
78 },
79 host: {
80 static_libs: ["hostlib"],
81 },
82 },
Liz Kammerefc51d92023-04-21 15:11:25 -040083 static_libs: ["cc_test_lib1"],
84 shared_libs: ["cc_test_lib2"],
Jingwen Chen537242c2022-08-24 11:53:27 +000085 data: [":data_mod", "file.txt"],
86 data_bins: [":cc_bin"],
87 data_libs: [":cc_lib"],
88 cflags: ["-Wall"],
89}
Liz Kammerefc51d92023-04-21 15:11:25 -040090
91cc_test_library {
92 name: "cc_test_lib1",
93 host_supported: true,
94 include_build_directory: false,
95}
Chris Parsonscd209032023-09-19 01:12:48 +000096` + simpleModule("cc_library", "foolib") +
97 simpleModule("cc_library_static", "hostlib") +
98 simpleModule("genrule", "data_mod") +
99 simpleModule("cc_binary", "cc_bin") +
100 simpleModule("cc_library", "cc_lib") +
101 simpleModule("cc_test_library", "cc_test_lib2") +
102 simpleModule("cc_library_static", "libgtest_main") +
103 simpleModule("cc_library_static", "libgtest"),
Jingwen Chen537242c2022-08-24 11:53:27 +0000104 targets: []testBazelTarget{
Liz Kammerefc51d92023-04-21 15:11:25 -0400105 {"cc_library_shared", "cc_test_lib1", AttrNameToString{}},
106 {"cc_library_static", "cc_test_lib1_bp2build_cc_library_static", AttrNameToString{}},
Jingwen Chen537242c2022-08-24 11:53:27 +0000107 {"cc_test", "mytest", AttrNameToString{
108 "copts": `["-Wall"]`,
109 "data": `[
110 ":data_mod",
111 "file.txt",
112 ":cc_bin",
113 ":cc_lib",
114 ]`,
Spandan Das651203d2023-07-21 22:55:32 +0000115 "deps": `[
116 ":cc_test_lib1_bp2build_cc_library_static",
117 ":libgtest_main",
118 ":libgtest",
119 ] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000120 "//build/bazel_common_rules/platforms/os:darwin": [":hostlib"],
121 "//build/bazel_common_rules/platforms/os:linux_bionic": [":hostlib"],
122 "//build/bazel_common_rules/platforms/os:linux_glibc": [":hostlib"],
123 "//build/bazel_common_rules/platforms/os:linux_musl": [":hostlib"],
124 "//build/bazel_common_rules/platforms/os:windows": [":hostlib"],
Jingwen Chen537242c2022-08-24 11:53:27 +0000125 "//conditions:default": [],
126 })`,
Jingwen Chen537242c2022-08-24 11:53:27 +0000127 "local_includes": `["."]`,
Liz Kammerefc51d92023-04-21 15:11:25 -0400128 "dynamic_deps": `[":cc_test_lib2"] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000129 "//build/bazel_common_rules/platforms/os:android": [":foolib"],
Jingwen Chen537242c2022-08-24 11:53:27 +0000130 "//conditions:default": [],
131 })`,
132 "srcs": `["test.cpp"] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000133 "//build/bazel_common_rules/platforms/os:android": [
Jingwen Chen537242c2022-08-24 11:53:27 +0000134 "linux.cpp",
135 "android.cpp",
136 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000137 "//build/bazel_common_rules/platforms/os:linux_bionic": ["linux.cpp"],
138 "//build/bazel_common_rules/platforms/os:linux_glibc": ["linux.cpp"],
139 "//build/bazel_common_rules/platforms/os:linux_musl": ["linux.cpp"],
Jingwen Chen537242c2022-08-24 11:53:27 +0000140 "//conditions:default": [],
141 })`,
yikefdca7fe2023-08-17 01:03:37 +0000142 "runs_on": `[
143 "host_without_device",
144 "device",
145 ]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700146 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000147 "//build/bazel_common_rules/platforms/os_arch:android_arm64": [
Yu Liu7ece4aa2023-08-25 16:56:33 -0700148 "memtag_heap",
149 "diag_memtag_heap",
150 ],
151 "//conditions:default": [],
152 })`,
Jingwen Chen537242c2022-08-24 11:53:27 +0000153 },
154 },
155 },
156 })
157}
158
159func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) {
160 runCcTestTestCase(t, ccTestBp2buildTestCase{
161 description: "cc test with disabled gtest and isolated props",
162 blueprint: `
163cc_test {
164 name: "mytest",
165 host_supported: true,
166 srcs: ["test.cpp"],
167 gtest: false,
168 isolated: false,
169}
170`,
171 targets: []testBazelTarget{
172 {"cc_test", "mytest", AttrNameToString{
173 "gtest": "False",
Jingwen Chen537242c2022-08-24 11:53:27 +0000174 "local_includes": `["."]`,
175 "srcs": `["test.cpp"]`,
yikefdca7fe2023-08-17 01:03:37 +0000176 "runs_on": `[
177 "host_without_device",
178 "device",
179 ]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700180 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000181 "//build/bazel_common_rules/platforms/os_arch:android_arm64": [
Yu Liu7ece4aa2023-08-25 16:56:33 -0700182 "memtag_heap",
183 "diag_memtag_heap",
184 ],
185 "//conditions:default": [],
186 })`,
Jingwen Chen537242c2022-08-24 11:53:27 +0000187 },
188 },
189 },
190 })
191}
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000192
193func TestCcTest_TestOptions_Tags(t *testing.T) {
194 runCcTestTestCase(t, ccTestBp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +0000195 description: "cc test with test_options.tags converted to tags",
196 stubbedBuildDefinitions: []string{"libgtest_main", "libgtest"},
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000197 blueprint: `
198cc_test {
199 name: "mytest",
200 host_supported: true,
201 srcs: ["test.cpp"],
202 test_options: { tags: ["no-remote"] },
203}
Chris Parsonscd209032023-09-19 01:12:48 +0000204` + simpleModule("cc_library_static", "libgtest_main") +
205 simpleModule("cc_library_static", "libgtest"),
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000206 targets: []testBazelTarget{
207 {"cc_test", "mytest", AttrNameToString{
208 "tags": `["no-remote"]`,
209 "local_includes": `["."]`,
210 "srcs": `["test.cpp"]`,
Spandan Das651203d2023-07-21 22:55:32 +0000211 "deps": `[
212 ":libgtest_main",
213 ":libgtest",
214 ]`,
yikefdca7fe2023-08-17 01:03:37 +0000215 "runs_on": `[
216 "host_without_device",
217 "device",
218 ]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700219 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000220 "//build/bazel_common_rules/platforms/os_arch:android_arm64": [
Yu Liu7ece4aa2023-08-25 16:56:33 -0700221 "memtag_heap",
222 "diag_memtag_heap",
223 ],
224 "//conditions:default": [],
225 })`,
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000226 },
227 },
228 },
229 })
230}
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000231
232func TestCcTest_TestConfig(t *testing.T) {
233 runCcTestTestCase(t, ccTestBp2buildTestCase{
234 description: "cc test that sets a test_config",
235 filesystem: map[string]string{
236 "test_config.xml": "",
237 },
Chris Parsonscd209032023-09-19 01:12:48 +0000238 stubbedBuildDefinitions: []string{"libgtest_main", "libgtest"},
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000239 blueprint: `
240cc_test {
241 name: "mytest",
242 srcs: ["test.cpp"],
243 test_config: "test_config.xml",
244}
Chris Parsonscd209032023-09-19 01:12:48 +0000245` + simpleModule("cc_library_static", "libgtest_main") +
246 simpleModule("cc_library_static", "libgtest"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000247 targets: []testBazelTarget{
248 {"cc_test", "mytest", AttrNameToString{
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000249 "local_includes": `["."]`,
250 "srcs": `["test.cpp"]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000251 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000252 "test_config": `"test_config.xml"`,
Spandan Das651203d2023-07-21 22:55:32 +0000253 "deps": `[
254 ":libgtest_main",
255 ":libgtest",
256 ]`,
yikefdca7fe2023-08-17 01:03:37 +0000257 "runs_on": `["device"]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700258 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000259 "//build/bazel_common_rules/platforms/os_arch:android_arm64": [
Yu Liu7ece4aa2023-08-25 16:56:33 -0700260 "memtag_heap",
261 "diag_memtag_heap",
262 ],
263 "//conditions:default": [],
264 })`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000265 },
266 },
267 },
268 })
269}
270
271func TestCcTest_TestConfigAndroidTestXML(t *testing.T) {
272 runCcTestTestCase(t, ccTestBp2buildTestCase{
273 description: "cc test that defaults to test config AndroidTest.xml",
274 filesystem: map[string]string{
Yu Liud136a6a2023-08-21 21:23:04 +0000275 "AndroidTest.xml": "",
276 "DynamicConfig.xml": "",
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000277 },
Chris Parsonscd209032023-09-19 01:12:48 +0000278 stubbedBuildDefinitions: []string{"libgtest_main", "libgtest"},
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000279 blueprint: `
280cc_test {
281 name: "mytest",
282 srcs: ["test.cpp"],
283}
Chris Parsonscd209032023-09-19 01:12:48 +0000284` + simpleModule("cc_library_static", "libgtest_main") +
285 simpleModule("cc_library_static", "libgtest"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000286 targets: []testBazelTarget{
287 {"cc_test", "mytest", AttrNameToString{
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000288 "local_includes": `["."]`,
289 "srcs": `["test.cpp"]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000290 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000291 "test_config": `"AndroidTest.xml"`,
Yu Liud136a6a2023-08-21 21:23:04 +0000292 "dynamic_config": `"DynamicConfig.xml"`,
Spandan Das651203d2023-07-21 22:55:32 +0000293 "deps": `[
294 ":libgtest_main",
295 ":libgtest",
296 ]`,
yikefdca7fe2023-08-17 01:03:37 +0000297 "runs_on": `["device"]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700298 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000299 "//build/bazel_common_rules/platforms/os_arch:android_arm64": [
Yu Liu7ece4aa2023-08-25 16:56:33 -0700300 "memtag_heap",
301 "diag_memtag_heap",
302 ],
303 "//conditions:default": [],
304 })`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000305 },
306 },
307 },
308 })
309}
310
311func TestCcTest_TestConfigTemplateOptions(t *testing.T) {
312 runCcTestTestCase(t, ccTestBp2buildTestCase{
313 description: "cc test that sets test config template attributes",
314 filesystem: map[string]string{
315 "test_config_template.xml": "",
316 },
Chris Parsonscd209032023-09-19 01:12:48 +0000317 stubbedBuildDefinitions: []string{"libgtest_isolated_main", "liblog"},
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000318 blueprint: `
319cc_test {
320 name: "mytest",
321 srcs: ["test.cpp"],
322 test_config_template: "test_config_template.xml",
323 auto_gen_config: true,
Spandan Dasf5a86552023-07-22 03:16:53 +0000324 isolated: true,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000325}
Chris Parsonscd209032023-09-19 01:12:48 +0000326` + simpleModule("cc_library_static", "libgtest_isolated_main") +
327 simpleModule("cc_library", "liblog"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000328 targets: []testBazelTarget{
329 {"cc_test", "mytest", AttrNameToString{
330 "auto_generate_test_config": "True",
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000331 "local_includes": `["."]`,
332 "srcs": `["test.cpp"]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000333 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000334 "template_configs": `[
335 "'<target_preparer class=\"com.android.tradefed.targetprep.RootTargetPreparer\">\\n <option name=\"force-root\" value=\"false\" />\\n </target_preparer>'",
336 "'<option name=\"not-shardable\" value=\"true\" />'",
337 ]`,
338 "template_install_base": `"/data/local/tmp"`,
339 "template_test_config": `"test_config_template.xml"`,
Spandan Dasd1cd3512023-07-22 02:19:42 +0000340 "deps": `[":libgtest_isolated_main"]`,
341 "dynamic_deps": `[":liblog"]`,
yikefdca7fe2023-08-17 01:03:37 +0000342 "runs_on": `["device"]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700343 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000344 "//build/bazel_common_rules/platforms/os_arch:android_arm64": [
Yu Liu7ece4aa2023-08-25 16:56:33 -0700345 "memtag_heap",
346 "diag_memtag_heap",
347 ],
348 "//conditions:default": [],
349 })`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000350 },
351 },
352 },
353 })
354}
Spandan Das651203d2023-07-21 22:55:32 +0000355
356func TestCcTest_WithExplicitGTestDepInAndroidBp(t *testing.T) {
357 runCcTestTestCase(t, ccTestBp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +0000358 description: "cc test that lists libgtest in Android.bp should not have dups of libgtest in BUILD file",
359 stubbedBuildDefinitions: []string{"libgtest_main", "libgtest"},
Spandan Das651203d2023-07-21 22:55:32 +0000360 blueprint: `
361cc_test {
362 name: "mytest",
363 srcs: ["test.cpp"],
364 static_libs: ["libgtest"],
365}
Chris Parsonscd209032023-09-19 01:12:48 +0000366` + simpleModule("cc_library_static", "libgtest_main") +
367 simpleModule("cc_library_static", "libgtest"),
Spandan Das651203d2023-07-21 22:55:32 +0000368 targets: []testBazelTarget{
369 {"cc_test", "mytest", AttrNameToString{
Spandan Das651203d2023-07-21 22:55:32 +0000370 "local_includes": `["."]`,
371 "srcs": `["test.cpp"]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000372 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Spandan Das651203d2023-07-21 22:55:32 +0000373 "deps": `[
374 ":libgtest",
375 ":libgtest_main",
376 ]`,
yikefdca7fe2023-08-17 01:03:37 +0000377 "runs_on": `["device"]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700378 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000379 "//build/bazel_common_rules/platforms/os_arch:android_arm64": [
Yu Liu7ece4aa2023-08-25 16:56:33 -0700380 "memtag_heap",
381 "diag_memtag_heap",
382 ],
383 "//conditions:default": [],
384 })`,
Spandan Das651203d2023-07-21 22:55:32 +0000385 },
386 },
387 },
388 })
389
390}
Spandan Dasd1cd3512023-07-22 02:19:42 +0000391
392func TestCcTest_WithIsolatedTurnedOn(t *testing.T) {
393 runCcTestTestCase(t, ccTestBp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +0000394 description: "cc test that sets `isolated: true` should run with ligtest_isolated_main instead of libgtest_main",
395 stubbedBuildDefinitions: []string{"libgtest_isolated_main", "liblog"},
Spandan Dasd1cd3512023-07-22 02:19:42 +0000396 blueprint: `
397cc_test {
398 name: "mytest",
399 srcs: ["test.cpp"],
400 isolated: true,
401}
Chris Parsonscd209032023-09-19 01:12:48 +0000402` + simpleModule("cc_library_static", "libgtest_isolated_main") +
403 simpleModule("cc_library", "liblog"),
Spandan Dasd1cd3512023-07-22 02:19:42 +0000404 targets: []testBazelTarget{
405 {"cc_test", "mytest", AttrNameToString{
Spandan Dasd1cd3512023-07-22 02:19:42 +0000406 "local_includes": `["."]`,
407 "srcs": `["test.cpp"]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000408 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Spandan Dasd1cd3512023-07-22 02:19:42 +0000409 "deps": `[":libgtest_isolated_main"]`,
410 "dynamic_deps": `[":liblog"]`,
yikefdca7fe2023-08-17 01:03:37 +0000411 "runs_on": `["device"]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700412 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000413 "//build/bazel_common_rules/platforms/os_arch:android_arm64": [
Yu Liu7ece4aa2023-08-25 16:56:33 -0700414 "memtag_heap",
415 "diag_memtag_heap",
416 ],
417 "//conditions:default": [],
418 })`,
Spandan Dasd1cd3512023-07-22 02:19:42 +0000419 },
420 },
421 },
422 })
423
424}
Spandan Dase50fd112023-07-26 17:35:20 +0000425
426func TestCcTest_GtestExplicitlySpecifiedInAndroidBp(t *testing.T) {
427 runCcTestTestCase(t, ccTestBp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +0000428 description: "If `gtest` is explicit in Android.bp, it should be explicit in BUILD files as well",
429 stubbedBuildDefinitions: []string{"libgtest_main", "libgtest"},
Spandan Dase50fd112023-07-26 17:35:20 +0000430 blueprint: `
431cc_test {
432 name: "mytest_with_gtest",
433 gtest: true,
434}
435cc_test {
436 name: "mytest_with_no_gtest",
437 gtest: false,
438}
Chris Parsonscd209032023-09-19 01:12:48 +0000439` + simpleModule("cc_library_static", "libgtest_main") +
440 simpleModule("cc_library_static", "libgtest"),
Spandan Dase50fd112023-07-26 17:35:20 +0000441 targets: []testBazelTarget{
442 {"cc_test", "mytest_with_gtest", AttrNameToString{
443 "local_includes": `["."]`,
444 "deps": `[
445 ":libgtest_main",
446 ":libgtest",
447 ]`,
448 "gtest": "True",
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000449 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
yikefdca7fe2023-08-17 01:03:37 +0000450 "runs_on": `["device"]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700451 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000452 "//build/bazel_common_rules/platforms/os_arch:android_arm64": [
Yu Liu7ece4aa2023-08-25 16:56:33 -0700453 "memtag_heap",
454 "diag_memtag_heap",
455 ],
456 "//conditions:default": [],
457 })`,
Spandan Dase50fd112023-07-26 17:35:20 +0000458 },
459 },
460 {"cc_test", "mytest_with_no_gtest", AttrNameToString{
461 "local_includes": `["."]`,
462 "gtest": "False",
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000463 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
yikefdca7fe2023-08-17 01:03:37 +0000464 "runs_on": `["device"]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700465 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000466 "//build/bazel_common_rules/platforms/os_arch:android_arm64": [
Yu Liu7ece4aa2023-08-25 16:56:33 -0700467 "memtag_heap",
468 "diag_memtag_heap",
469 ],
470 "//conditions:default": [],
471 })`,
472 },
473 },
474 },
475 })
476}
477
478func TestCcTest_DisableMemtagHeap(t *testing.T) {
479 runCcTestTestCase(t, ccTestBp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +0000480 description: "cc test that disable memtag_heap",
481 stubbedBuildDefinitions: []string{"libgtest_isolated_main", "liblog"},
Yu Liu7ece4aa2023-08-25 16:56:33 -0700482 blueprint: `
483cc_test {
484 name: "mytest",
485 srcs: ["test.cpp"],
486 isolated: true,
487 sanitize: {
488 cfi: true,
489 memtag_heap: false,
490 },
491}
Chris Parsonscd209032023-09-19 01:12:48 +0000492` + simpleModule("cc_library_static", "libgtest_isolated_main") +
493 simpleModule("cc_library", "liblog"),
Yu Liu7ece4aa2023-08-25 16:56:33 -0700494 targets: []testBazelTarget{
495 {"cc_test", "mytest", AttrNameToString{
496 "local_includes": `["."]`,
497 "srcs": `["test.cpp"]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000498 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700499 "deps": `[":libgtest_isolated_main"]`,
500 "dynamic_deps": `[":liblog"]`,
501 "runs_on": `["device"]`,
502 "features": `["android_cfi"] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000503 "//build/bazel_common_rules/platforms/os_arch:android_arm64": ["-memtag_heap"],
Yu Liu7ece4aa2023-08-25 16:56:33 -0700504 "//conditions:default": [],
505 })`,
506 },
507 },
508 },
509 })
510}
511
512func TestCcTest_RespectArm64MemtagHeap(t *testing.T) {
513 runCcTestTestCase(t, ccTestBp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +0000514 description: "cc test that disable memtag_heap",
515 stubbedBuildDefinitions: []string{"libgtest_isolated_main", "liblog"},
Yu Liu7ece4aa2023-08-25 16:56:33 -0700516 blueprint: `
517cc_test {
518 name: "mytest",
519 srcs: ["test.cpp"],
520 isolated: true,
521 target: {
522 android_arm64: {
523 sanitize: {
524 memtag_heap: false,
525 }
526 }
527 },
528}
Chris Parsonscd209032023-09-19 01:12:48 +0000529` + simpleModule("cc_library_static", "libgtest_isolated_main") +
530 simpleModule("cc_library", "liblog"),
Yu Liu7ece4aa2023-08-25 16:56:33 -0700531 targets: []testBazelTarget{
532 {"cc_test", "mytest", AttrNameToString{
533 "local_includes": `["."]`,
534 "srcs": `["test.cpp"]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000535 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700536 "deps": `[":libgtest_isolated_main"]`,
537 "dynamic_deps": `[":liblog"]`,
538 "runs_on": `["device"]`,
539 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000540 "//build/bazel_common_rules/platforms/os_arch:android_arm64": ["-memtag_heap"],
Yu Liu7ece4aa2023-08-25 16:56:33 -0700541 "//conditions:default": [],
542 })`,
543 },
544 },
545 },
546 })
547}
548
549func TestCcTest_IgnoreNoneArm64MemtagHeap(t *testing.T) {
550 runCcTestTestCase(t, ccTestBp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +0000551 description: "cc test that disable memtag_heap",
552 stubbedBuildDefinitions: []string{"libgtest_isolated_main", "liblog"},
Yu Liu7ece4aa2023-08-25 16:56:33 -0700553 blueprint: `
554cc_test {
555 name: "mytest",
556 srcs: ["test.cpp"],
557 isolated: true,
558 arch: {
559 x86: {
560 sanitize: {
561 memtag_heap: false,
562 }
563 }
564 },
565}
Chris Parsonscd209032023-09-19 01:12:48 +0000566` + simpleModule("cc_library_static", "libgtest_isolated_main") +
567 simpleModule("cc_library", "liblog"),
Yu Liu7ece4aa2023-08-25 16:56:33 -0700568 targets: []testBazelTarget{
569 {"cc_test", "mytest", AttrNameToString{
570 "local_includes": `["."]`,
571 "srcs": `["test.cpp"]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000572 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700573 "deps": `[":libgtest_isolated_main"]`,
574 "dynamic_deps": `[":liblog"]`,
575 "runs_on": `["device"]`,
576 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000577 "//build/bazel_common_rules/platforms/os_arch:android_arm64": [
Yu Liu7ece4aa2023-08-25 16:56:33 -0700578 "memtag_heap",
579 "diag_memtag_heap",
580 ],
581 "//conditions:default": [],
582 })`,
583 },
584 },
585 },
586 })
587}
588
589func TestCcTest_Arm64MemtagHeapOverrideNoConfigOne(t *testing.T) {
590 runCcTestTestCase(t, ccTestBp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +0000591 description: "cc test that disable memtag_heap",
592 stubbedBuildDefinitions: []string{"libgtest_isolated_main", "liblog"},
Yu Liu7ece4aa2023-08-25 16:56:33 -0700593 blueprint: `
594cc_test {
595 name: "mytest",
596 srcs: ["test.cpp"],
597 isolated: true,
598 sanitize: {
599 memtag_heap: true,
600 },
601 target: {
602 android_arm64: {
603 sanitize: {
604 memtag_heap: false,
605 diag: {
606 memtag_heap: false,
607 },
608 }
609 }
610 },
611}
Chris Parsonscd209032023-09-19 01:12:48 +0000612` + simpleModule("cc_library_static", "libgtest_isolated_main") +
613 simpleModule("cc_library", "liblog"),
Yu Liu7ece4aa2023-08-25 16:56:33 -0700614 targets: []testBazelTarget{
615 {"cc_test", "mytest", AttrNameToString{
616 "local_includes": `["."]`,
617 "srcs": `["test.cpp"]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000618 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Yu Liu7ece4aa2023-08-25 16:56:33 -0700619 "deps": `[":libgtest_isolated_main"]`,
620 "dynamic_deps": `[":liblog"]`,
621 "runs_on": `["device"]`,
622 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000623 "//build/bazel_common_rules/platforms/os_arch:android_arm64": [
Yu Liu7ece4aa2023-08-25 16:56:33 -0700624 "-memtag_heap",
625 "-diag_memtag_heap",
626 ],
627 "//conditions:default": [],
628 })`,
Spandan Dase50fd112023-07-26 17:35:20 +0000629 },
630 },
631 },
632 })
633}