Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 1 | // Copyright 2022 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package bp2build |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | "android/soong/cc" |
| 23 | "android/soong/genrule" |
| 24 | ) |
| 25 | |
| 26 | type ccTestBp2buildTestCase struct { |
| 27 | description string |
| 28 | blueprint string |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 29 | filesystem map[string]string |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 30 | targets []testBazelTarget |
| 31 | } |
| 32 | |
| 33 | func registerCcTestModuleTypes(ctx android.RegistrationContext) { |
| 34 | cc.RegisterCCBuildComponents(ctx) |
| 35 | ctx.RegisterModuleType("cc_binary", cc.BinaryFactory) |
| 36 | ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory) |
| 37 | ctx.RegisterModuleType("cc_library", cc.LibraryFactory) |
| 38 | ctx.RegisterModuleType("cc_test_library", cc.TestLibraryFactory) |
| 39 | ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) |
| 40 | } |
| 41 | |
| 42 | func runCcTestTestCase(t *testing.T, testCase ccTestBp2buildTestCase) { |
| 43 | t.Helper() |
| 44 | moduleTypeUnderTest := "cc_test" |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 45 | description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description) |
| 46 | t.Run(description, func(t *testing.T) { |
| 47 | t.Helper() |
| 48 | RunBp2BuildTestCase(t, registerCcTestModuleTypes, Bp2buildTestCase{ |
| 49 | ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.HostAndDeviceSupported), |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 50 | Filesystem: testCase.filesystem, |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 51 | ModuleTypeUnderTest: moduleTypeUnderTest, |
| 52 | ModuleTypeUnderTestFactory: cc.TestFactory, |
| 53 | Description: description, |
| 54 | Blueprint: testCase.blueprint, |
| 55 | }) |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | func TestBasicCcTest(t *testing.T) { |
| 60 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 61 | description: "basic cc_test with commonly used attributes", |
| 62 | blueprint: ` |
| 63 | cc_test { |
| 64 | name: "mytest", |
| 65 | host_supported: true, |
| 66 | srcs: ["test.cpp"], |
| 67 | target: { |
| 68 | android: { |
| 69 | srcs: ["android.cpp"], |
| 70 | shared_libs: ["foolib"], |
| 71 | }, |
| 72 | linux: { |
| 73 | srcs: ["linux.cpp"], |
| 74 | }, |
| 75 | host: { |
| 76 | static_libs: ["hostlib"], |
| 77 | }, |
| 78 | }, |
Liz Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 79 | static_libs: ["cc_test_lib1"], |
| 80 | shared_libs: ["cc_test_lib2"], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 81 | data: [":data_mod", "file.txt"], |
| 82 | data_bins: [":cc_bin"], |
| 83 | data_libs: [":cc_lib"], |
| 84 | cflags: ["-Wall"], |
| 85 | } |
Liz Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 86 | |
| 87 | cc_test_library { |
| 88 | name: "cc_test_lib1", |
| 89 | host_supported: true, |
| 90 | include_build_directory: false, |
| 91 | } |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 92 | ` + simpleModuleDoNotConvertBp2build("cc_library", "foolib") + |
| 93 | simpleModuleDoNotConvertBp2build("cc_library_static", "hostlib") + |
| 94 | simpleModuleDoNotConvertBp2build("genrule", "data_mod") + |
| 95 | simpleModuleDoNotConvertBp2build("cc_binary", "cc_bin") + |
Liz Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 96 | simpleModuleDoNotConvertBp2build("cc_library", "cc_lib") + |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 97 | simpleModuleDoNotConvertBp2build("cc_test_library", "cc_test_lib2") + |
| 98 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 99 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 100 | targets: []testBazelTarget{ |
Liz Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 101 | {"cc_library_shared", "cc_test_lib1", AttrNameToString{}}, |
| 102 | {"cc_library_static", "cc_test_lib1_bp2build_cc_library_static", AttrNameToString{}}, |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 103 | {"cc_test", "mytest", AttrNameToString{ |
| 104 | "copts": `["-Wall"]`, |
| 105 | "data": `[ |
| 106 | ":data_mod", |
| 107 | "file.txt", |
| 108 | ":cc_bin", |
| 109 | ":cc_lib", |
| 110 | ]`, |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 111 | "deps": `[ |
| 112 | ":cc_test_lib1_bp2build_cc_library_static", |
| 113 | ":libgtest_main", |
| 114 | ":libgtest", |
| 115 | ] + select({ |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 116 | "//build/bazel/platforms/os:darwin": [":hostlib"], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 117 | "//build/bazel/platforms/os:linux_bionic": [":hostlib"], |
Colin Cross | 133782e | 2022-12-20 15:29:31 -0800 | [diff] [blame] | 118 | "//build/bazel/platforms/os:linux_glibc": [":hostlib"], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 119 | "//build/bazel/platforms/os:linux_musl": [":hostlib"], |
| 120 | "//build/bazel/platforms/os:windows": [":hostlib"], |
| 121 | "//conditions:default": [], |
| 122 | })`, |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 123 | "local_includes": `["."]`, |
Liz Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 124 | "dynamic_deps": `[":cc_test_lib2"] + select({ |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 125 | "//build/bazel/platforms/os:android": [":foolib"], |
| 126 | "//conditions:default": [], |
| 127 | })`, |
| 128 | "srcs": `["test.cpp"] + select({ |
| 129 | "//build/bazel/platforms/os:android": [ |
| 130 | "linux.cpp", |
| 131 | "android.cpp", |
| 132 | ], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 133 | "//build/bazel/platforms/os:linux_bionic": ["linux.cpp"], |
Colin Cross | 133782e | 2022-12-20 15:29:31 -0800 | [diff] [blame] | 134 | "//build/bazel/platforms/os:linux_glibc": ["linux.cpp"], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 135 | "//build/bazel/platforms/os:linux_musl": ["linux.cpp"], |
| 136 | "//conditions:default": [], |
| 137 | })`, |
yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 138 | "runs_on": `[ |
| 139 | "host_without_device", |
| 140 | "device", |
| 141 | ]`, |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame^] | 142 | "features": `select({ |
| 143 | "//build/bazel/platforms/os_arch:android_arm64": [ |
| 144 | "memtag_heap", |
| 145 | "diag_memtag_heap", |
| 146 | ], |
| 147 | "//conditions:default": [], |
| 148 | })`, |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 149 | }, |
| 150 | }, |
| 151 | }, |
| 152 | }) |
| 153 | } |
| 154 | |
| 155 | func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) { |
| 156 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 157 | description: "cc test with disabled gtest and isolated props", |
| 158 | blueprint: ` |
| 159 | cc_test { |
| 160 | name: "mytest", |
| 161 | host_supported: true, |
| 162 | srcs: ["test.cpp"], |
| 163 | gtest: false, |
| 164 | isolated: false, |
| 165 | } |
| 166 | `, |
| 167 | targets: []testBazelTarget{ |
| 168 | {"cc_test", "mytest", AttrNameToString{ |
| 169 | "gtest": "False", |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 170 | "local_includes": `["."]`, |
| 171 | "srcs": `["test.cpp"]`, |
yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 172 | "runs_on": `[ |
| 173 | "host_without_device", |
| 174 | "device", |
| 175 | ]`, |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame^] | 176 | "features": `select({ |
| 177 | "//build/bazel/platforms/os_arch:android_arm64": [ |
| 178 | "memtag_heap", |
| 179 | "diag_memtag_heap", |
| 180 | ], |
| 181 | "//conditions:default": [], |
| 182 | })`, |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 183 | }, |
| 184 | }, |
| 185 | }, |
| 186 | }) |
| 187 | } |
Jingwen Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 188 | |
| 189 | func TestCcTest_TestOptions_Tags(t *testing.T) { |
| 190 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 191 | description: "cc test with test_options.tags converted to tags", |
| 192 | blueprint: ` |
| 193 | cc_test { |
| 194 | name: "mytest", |
| 195 | host_supported: true, |
| 196 | srcs: ["test.cpp"], |
| 197 | test_options: { tags: ["no-remote"] }, |
| 198 | } |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 199 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 200 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
Jingwen Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 201 | targets: []testBazelTarget{ |
| 202 | {"cc_test", "mytest", AttrNameToString{ |
| 203 | "tags": `["no-remote"]`, |
| 204 | "local_includes": `["."]`, |
| 205 | "srcs": `["test.cpp"]`, |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 206 | "deps": `[ |
| 207 | ":libgtest_main", |
| 208 | ":libgtest", |
| 209 | ]`, |
yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 210 | "runs_on": `[ |
| 211 | "host_without_device", |
| 212 | "device", |
| 213 | ]`, |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame^] | 214 | "features": `select({ |
| 215 | "//build/bazel/platforms/os_arch:android_arm64": [ |
| 216 | "memtag_heap", |
| 217 | "diag_memtag_heap", |
| 218 | ], |
| 219 | "//conditions:default": [], |
| 220 | })`, |
Jingwen Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 221 | }, |
| 222 | }, |
| 223 | }, |
| 224 | }) |
| 225 | } |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 226 | |
| 227 | func TestCcTest_TestConfig(t *testing.T) { |
| 228 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 229 | description: "cc test that sets a test_config", |
| 230 | filesystem: map[string]string{ |
| 231 | "test_config.xml": "", |
| 232 | }, |
| 233 | blueprint: ` |
| 234 | cc_test { |
| 235 | name: "mytest", |
| 236 | srcs: ["test.cpp"], |
| 237 | test_config: "test_config.xml", |
| 238 | } |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 239 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 240 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 241 | targets: []testBazelTarget{ |
| 242 | {"cc_test", "mytest", AttrNameToString{ |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 243 | "local_includes": `["."]`, |
| 244 | "srcs": `["test.cpp"]`, |
| 245 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 246 | "test_config": `"test_config.xml"`, |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 247 | "deps": `[ |
| 248 | ":libgtest_main", |
| 249 | ":libgtest", |
| 250 | ]`, |
yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 251 | "runs_on": `["device"]`, |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame^] | 252 | "features": `select({ |
| 253 | "//build/bazel/platforms/os_arch:android_arm64": [ |
| 254 | "memtag_heap", |
| 255 | "diag_memtag_heap", |
| 256 | ], |
| 257 | "//conditions:default": [], |
| 258 | })`, |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 259 | }, |
| 260 | }, |
| 261 | }, |
| 262 | }) |
| 263 | } |
| 264 | |
| 265 | func TestCcTest_TestConfigAndroidTestXML(t *testing.T) { |
| 266 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 267 | description: "cc test that defaults to test config AndroidTest.xml", |
| 268 | filesystem: map[string]string{ |
Yu Liu | d136a6a | 2023-08-21 21:23:04 +0000 | [diff] [blame] | 269 | "AndroidTest.xml": "", |
| 270 | "DynamicConfig.xml": "", |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 271 | }, |
| 272 | blueprint: ` |
| 273 | cc_test { |
| 274 | name: "mytest", |
| 275 | srcs: ["test.cpp"], |
| 276 | } |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 277 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 278 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 279 | targets: []testBazelTarget{ |
| 280 | {"cc_test", "mytest", AttrNameToString{ |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 281 | "local_includes": `["."]`, |
| 282 | "srcs": `["test.cpp"]`, |
| 283 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 284 | "test_config": `"AndroidTest.xml"`, |
Yu Liu | d136a6a | 2023-08-21 21:23:04 +0000 | [diff] [blame] | 285 | "dynamic_config": `"DynamicConfig.xml"`, |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 286 | "deps": `[ |
| 287 | ":libgtest_main", |
| 288 | ":libgtest", |
| 289 | ]`, |
yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 290 | "runs_on": `["device"]`, |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame^] | 291 | "features": `select({ |
| 292 | "//build/bazel/platforms/os_arch:android_arm64": [ |
| 293 | "memtag_heap", |
| 294 | "diag_memtag_heap", |
| 295 | ], |
| 296 | "//conditions:default": [], |
| 297 | })`, |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 298 | }, |
| 299 | }, |
| 300 | }, |
| 301 | }) |
| 302 | } |
| 303 | |
| 304 | func TestCcTest_TestConfigTemplateOptions(t *testing.T) { |
| 305 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 306 | description: "cc test that sets test config template attributes", |
| 307 | filesystem: map[string]string{ |
| 308 | "test_config_template.xml": "", |
| 309 | }, |
| 310 | blueprint: ` |
| 311 | cc_test { |
| 312 | name: "mytest", |
| 313 | srcs: ["test.cpp"], |
| 314 | test_config_template: "test_config_template.xml", |
| 315 | auto_gen_config: true, |
Spandan Das | f5a8655 | 2023-07-22 03:16:53 +0000 | [diff] [blame] | 316 | isolated: true, |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 317 | } |
Spandan Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame] | 318 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") + |
| 319 | simpleModuleDoNotConvertBp2build("cc_library", "liblog"), |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 320 | targets: []testBazelTarget{ |
| 321 | {"cc_test", "mytest", AttrNameToString{ |
| 322 | "auto_generate_test_config": "True", |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 323 | "local_includes": `["."]`, |
| 324 | "srcs": `["test.cpp"]`, |
| 325 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 326 | "template_configs": `[ |
| 327 | "'<target_preparer class=\"com.android.tradefed.targetprep.RootTargetPreparer\">\\n <option name=\"force-root\" value=\"false\" />\\n </target_preparer>'", |
| 328 | "'<option name=\"not-shardable\" value=\"true\" />'", |
| 329 | ]`, |
| 330 | "template_install_base": `"/data/local/tmp"`, |
| 331 | "template_test_config": `"test_config_template.xml"`, |
Spandan Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame] | 332 | "deps": `[":libgtest_isolated_main"]`, |
| 333 | "dynamic_deps": `[":liblog"]`, |
yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 334 | "runs_on": `["device"]`, |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame^] | 335 | "features": `select({ |
| 336 | "//build/bazel/platforms/os_arch:android_arm64": [ |
| 337 | "memtag_heap", |
| 338 | "diag_memtag_heap", |
| 339 | ], |
| 340 | "//conditions:default": [], |
| 341 | })`, |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 342 | }, |
| 343 | }, |
| 344 | }, |
| 345 | }) |
| 346 | } |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 347 | |
| 348 | func TestCcTest_WithExplicitGTestDepInAndroidBp(t *testing.T) { |
| 349 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 350 | description: "cc test that lists libgtest in Android.bp should not have dups of libgtest in BUILD file", |
| 351 | blueprint: ` |
| 352 | cc_test { |
| 353 | name: "mytest", |
| 354 | srcs: ["test.cpp"], |
| 355 | static_libs: ["libgtest"], |
| 356 | } |
| 357 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 358 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
| 359 | targets: []testBazelTarget{ |
| 360 | {"cc_test", "mytest", AttrNameToString{ |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 361 | "local_includes": `["."]`, |
| 362 | "srcs": `["test.cpp"]`, |
| 363 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 364 | "deps": `[ |
| 365 | ":libgtest", |
| 366 | ":libgtest_main", |
| 367 | ]`, |
yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 368 | "runs_on": `["device"]`, |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame^] | 369 | "features": `select({ |
| 370 | "//build/bazel/platforms/os_arch:android_arm64": [ |
| 371 | "memtag_heap", |
| 372 | "diag_memtag_heap", |
| 373 | ], |
| 374 | "//conditions:default": [], |
| 375 | })`, |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 376 | }, |
| 377 | }, |
| 378 | }, |
| 379 | }) |
| 380 | |
| 381 | } |
Spandan Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame] | 382 | |
| 383 | func TestCcTest_WithIsolatedTurnedOn(t *testing.T) { |
| 384 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 385 | description: "cc test that sets `isolated: true` should run with ligtest_isolated_main instead of libgtest_main", |
| 386 | blueprint: ` |
| 387 | cc_test { |
| 388 | name: "mytest", |
| 389 | srcs: ["test.cpp"], |
| 390 | isolated: true, |
| 391 | } |
| 392 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") + |
| 393 | simpleModuleDoNotConvertBp2build("cc_library", "liblog"), |
| 394 | targets: []testBazelTarget{ |
| 395 | {"cc_test", "mytest", AttrNameToString{ |
Spandan Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame] | 396 | "local_includes": `["."]`, |
| 397 | "srcs": `["test.cpp"]`, |
| 398 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 399 | "deps": `[":libgtest_isolated_main"]`, |
| 400 | "dynamic_deps": `[":liblog"]`, |
yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 401 | "runs_on": `["device"]`, |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame^] | 402 | "features": `select({ |
| 403 | "//build/bazel/platforms/os_arch:android_arm64": [ |
| 404 | "memtag_heap", |
| 405 | "diag_memtag_heap", |
| 406 | ], |
| 407 | "//conditions:default": [], |
| 408 | })`, |
Spandan Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame] | 409 | }, |
| 410 | }, |
| 411 | }, |
| 412 | }) |
| 413 | |
| 414 | } |
Spandan Das | e50fd11 | 2023-07-26 17:35:20 +0000 | [diff] [blame] | 415 | |
| 416 | func TestCcTest_GtestExplicitlySpecifiedInAndroidBp(t *testing.T) { |
| 417 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 418 | description: "If `gtest` is explicit in Android.bp, it should be explicit in BUILD files as well", |
| 419 | blueprint: ` |
| 420 | cc_test { |
| 421 | name: "mytest_with_gtest", |
| 422 | gtest: true, |
| 423 | } |
| 424 | cc_test { |
| 425 | name: "mytest_with_no_gtest", |
| 426 | gtest: false, |
| 427 | } |
| 428 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 429 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
| 430 | targets: []testBazelTarget{ |
| 431 | {"cc_test", "mytest_with_gtest", AttrNameToString{ |
| 432 | "local_includes": `["."]`, |
| 433 | "deps": `[ |
| 434 | ":libgtest_main", |
| 435 | ":libgtest", |
| 436 | ]`, |
| 437 | "gtest": "True", |
| 438 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 439 | "runs_on": `["device"]`, |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame^] | 440 | "features": `select({ |
| 441 | "//build/bazel/platforms/os_arch:android_arm64": [ |
| 442 | "memtag_heap", |
| 443 | "diag_memtag_heap", |
| 444 | ], |
| 445 | "//conditions:default": [], |
| 446 | })`, |
Spandan Das | e50fd11 | 2023-07-26 17:35:20 +0000 | [diff] [blame] | 447 | }, |
| 448 | }, |
| 449 | {"cc_test", "mytest_with_no_gtest", AttrNameToString{ |
| 450 | "local_includes": `["."]`, |
| 451 | "gtest": "False", |
| 452 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 453 | "runs_on": `["device"]`, |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame^] | 454 | "features": `select({ |
| 455 | "//build/bazel/platforms/os_arch:android_arm64": [ |
| 456 | "memtag_heap", |
| 457 | "diag_memtag_heap", |
| 458 | ], |
| 459 | "//conditions:default": [], |
| 460 | })`, |
| 461 | }, |
| 462 | }, |
| 463 | }, |
| 464 | }) |
| 465 | } |
| 466 | |
| 467 | func TestCcTest_DisableMemtagHeap(t *testing.T) { |
| 468 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 469 | description: "cc test that disable memtag_heap", |
| 470 | blueprint: ` |
| 471 | cc_test { |
| 472 | name: "mytest", |
| 473 | srcs: ["test.cpp"], |
| 474 | isolated: true, |
| 475 | sanitize: { |
| 476 | cfi: true, |
| 477 | memtag_heap: false, |
| 478 | }, |
| 479 | } |
| 480 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") + |
| 481 | simpleModuleDoNotConvertBp2build("cc_library", "liblog"), |
| 482 | targets: []testBazelTarget{ |
| 483 | {"cc_test", "mytest", AttrNameToString{ |
| 484 | "local_includes": `["."]`, |
| 485 | "srcs": `["test.cpp"]`, |
| 486 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 487 | "deps": `[":libgtest_isolated_main"]`, |
| 488 | "dynamic_deps": `[":liblog"]`, |
| 489 | "runs_on": `["device"]`, |
| 490 | "features": `["android_cfi"] + select({ |
| 491 | "//build/bazel/platforms/os_arch:android_arm64": ["-memtag_heap"], |
| 492 | "//conditions:default": [], |
| 493 | })`, |
| 494 | }, |
| 495 | }, |
| 496 | }, |
| 497 | }) |
| 498 | } |
| 499 | |
| 500 | func TestCcTest_RespectArm64MemtagHeap(t *testing.T) { |
| 501 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 502 | description: "cc test that disable memtag_heap", |
| 503 | blueprint: ` |
| 504 | cc_test { |
| 505 | name: "mytest", |
| 506 | srcs: ["test.cpp"], |
| 507 | isolated: true, |
| 508 | target: { |
| 509 | android_arm64: { |
| 510 | sanitize: { |
| 511 | memtag_heap: false, |
| 512 | } |
| 513 | } |
| 514 | }, |
| 515 | } |
| 516 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") + |
| 517 | simpleModuleDoNotConvertBp2build("cc_library", "liblog"), |
| 518 | targets: []testBazelTarget{ |
| 519 | {"cc_test", "mytest", AttrNameToString{ |
| 520 | "local_includes": `["."]`, |
| 521 | "srcs": `["test.cpp"]`, |
| 522 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 523 | "deps": `[":libgtest_isolated_main"]`, |
| 524 | "dynamic_deps": `[":liblog"]`, |
| 525 | "runs_on": `["device"]`, |
| 526 | "features": `select({ |
| 527 | "//build/bazel/platforms/os_arch:android_arm64": ["-memtag_heap"], |
| 528 | "//conditions:default": [], |
| 529 | })`, |
| 530 | }, |
| 531 | }, |
| 532 | }, |
| 533 | }) |
| 534 | } |
| 535 | |
| 536 | func TestCcTest_IgnoreNoneArm64MemtagHeap(t *testing.T) { |
| 537 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 538 | description: "cc test that disable memtag_heap", |
| 539 | blueprint: ` |
| 540 | cc_test { |
| 541 | name: "mytest", |
| 542 | srcs: ["test.cpp"], |
| 543 | isolated: true, |
| 544 | arch: { |
| 545 | x86: { |
| 546 | sanitize: { |
| 547 | memtag_heap: false, |
| 548 | } |
| 549 | } |
| 550 | }, |
| 551 | } |
| 552 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") + |
| 553 | simpleModuleDoNotConvertBp2build("cc_library", "liblog"), |
| 554 | targets: []testBazelTarget{ |
| 555 | {"cc_test", "mytest", AttrNameToString{ |
| 556 | "local_includes": `["."]`, |
| 557 | "srcs": `["test.cpp"]`, |
| 558 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 559 | "deps": `[":libgtest_isolated_main"]`, |
| 560 | "dynamic_deps": `[":liblog"]`, |
| 561 | "runs_on": `["device"]`, |
| 562 | "features": `select({ |
| 563 | "//build/bazel/platforms/os_arch:android_arm64": [ |
| 564 | "memtag_heap", |
| 565 | "diag_memtag_heap", |
| 566 | ], |
| 567 | "//conditions:default": [], |
| 568 | })`, |
| 569 | }, |
| 570 | }, |
| 571 | }, |
| 572 | }) |
| 573 | } |
| 574 | |
| 575 | func TestCcTest_Arm64MemtagHeapOverrideNoConfigOne(t *testing.T) { |
| 576 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 577 | description: "cc test that disable memtag_heap", |
| 578 | blueprint: ` |
| 579 | cc_test { |
| 580 | name: "mytest", |
| 581 | srcs: ["test.cpp"], |
| 582 | isolated: true, |
| 583 | sanitize: { |
| 584 | memtag_heap: true, |
| 585 | }, |
| 586 | target: { |
| 587 | android_arm64: { |
| 588 | sanitize: { |
| 589 | memtag_heap: false, |
| 590 | diag: { |
| 591 | memtag_heap: false, |
| 592 | }, |
| 593 | } |
| 594 | } |
| 595 | }, |
| 596 | } |
| 597 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") + |
| 598 | simpleModuleDoNotConvertBp2build("cc_library", "liblog"), |
| 599 | targets: []testBazelTarget{ |
| 600 | {"cc_test", "mytest", AttrNameToString{ |
| 601 | "local_includes": `["."]`, |
| 602 | "srcs": `["test.cpp"]`, |
| 603 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 604 | "deps": `[":libgtest_isolated_main"]`, |
| 605 | "dynamic_deps": `[":liblog"]`, |
| 606 | "runs_on": `["device"]`, |
| 607 | "features": `select({ |
| 608 | "//build/bazel/platforms/os_arch:android_arm64": [ |
| 609 | "-memtag_heap", |
| 610 | "-diag_memtag_heap", |
| 611 | ], |
| 612 | "//conditions:default": [], |
| 613 | })`, |
Spandan Das | e50fd11 | 2023-07-26 17:35:20 +0000 | [diff] [blame] | 614 | }, |
| 615 | }, |
| 616 | }, |
| 617 | }) |
| 618 | } |