Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Alan Zhao | 99479f6b | 2022-08-26 23:03:00 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <vector> |
| 6 | |
Peter Boström | 1273688 | 2024-03-15 20:06:41 | [diff] [blame] | 7 | #include "base/check.h" |
Alan Zhao | 99479f6b | 2022-08-26 23:03:00 | [diff] [blame] | 8 | #include "build/build_config.h" |
| 9 | #include "testing/gmock/include/gmock/gmock.h" |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace { |
| 13 | |
Nico Weber | 0335506 | 2023-09-14 05:04:36 | [diff] [blame] | 14 | // TODO(thakis): Remove _LIBCPP_ENABLE_ASSERTIONS here once |
| 15 | // pnacl-saigo's libc++ is new enough. |
Jose Dapena Paz | ad472c5 | 2024-02-29 08:29:17 | [diff] [blame] | 16 | #if defined(__GLIBCXX__) |
| 17 | #if !defined(_GLIBCXX_ASSERTIONS) |
| 18 | #error "libstdc++ assertions should be enabled" |
| 19 | #endif |
| 20 | #elif !_LIBCPP_ENABLE_ASSERTIONS && \ |
Hans Wennborg | 06de54a | 2023-11-09 20:31:31 | [diff] [blame] | 21 | _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_EXTENSIVE |
| 22 | #error "_LIBCPP_HARDENING_MODE not defined" |
Alan Zhao | 99479f6b | 2022-08-26 23:03:00 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | using ::testing::ContainsRegex; |
| 26 | using ::testing::Not; |
| 27 | |
| 28 | // This test checks for two things: |
| 29 | // |
| 30 | // 0. Assertions are enabled for libc++ and cause the process to crash when |
| 31 | // invoked (in this test's case, when an out of bounds access is made in |
| 32 | // std::vector. |
| 33 | // 1. The correct assertion handler is linked in depending on whether or not |
| 34 | // this test is built in debug mode. libc++ passes the string |
| 35 | // {file}:{line} assertion {expression} failed: {message}. The default |
| 36 | // libc++ handler, which we use in debug mode, prints this string to stderr, |
| 37 | // while the nondebug assertion handler just crashes immediately. Therefore, |
| 38 | // to check that we linked in the correct assertion handler, we check for the |
| 39 | // presence or absence of the above string. |
| 40 | TEST(LibcppHardeningTest, Assertions) { |
| 41 | std::vector<int> vec = {0, 1, 2}; |
Peter Boström | 1273688 | 2024-03-15 20:06:41 | [diff] [blame] | 42 | #if CHECK_WILL_STREAM() |
| 43 | EXPECT_DEATH_IF_SUPPORTED(vec[3], ".*assertion.*failed:"); |
| 44 | #else |
Alan Zhao | 99479f6b | 2022-08-26 23:03:00 | [diff] [blame] | 45 | // We have to explicitly check for the GTEST_HAS_DEATH_TEST macro instead of |
| 46 | // using EXPECT_DEATH_IF_SUPPORTED(...) for the following reasons: |
| 47 | // |
| 48 | // 0. EXPECT_DEATH(...) does not support (non-escaped) parentheses in the regex, |
| 49 | // so we can't use negative look arounds (https://stackoverflow.com/a/406408) |
| 50 | // to check that the error message doesn't exist. |
| 51 | // 1. EXPECT_DEATH_IF_SUPPORTED(...) does not support having gmock matchers as |
| 52 | // the second argument if GTEST_HAS_DEATH_TEST is false. |
| 53 | // |
| 54 | // We also have to prevent this test from running on Android because even though |
| 55 | // death tests are supported on Android, GTest death tests don't work with |
Peter Boström | 25c6ec7 | 2022-11-02 23:25:19 | [diff] [blame] | 56 | // base::ImmediateCrash() (https://crbug.com/1353549#c2). |
Alan Zhao | 99479f6b | 2022-08-26 23:03:00 | [diff] [blame] | 57 | #if GTEST_HAS_DEATH_TEST && !GTEST_OS_LINUX_ANDROID |
| 58 | EXPECT_DEATH(vec[3], Not(ContainsRegex(".*assertion.*failed:"))); |
| 59 | #else |
| 60 | GTEST_UNSUPPORTED_DEATH_TEST(vec[3], "", ); |
Alan Zhao | 218f8ad2 | 2022-09-14 21:09:33 | [diff] [blame] | 61 | #endif // GTEST_HAS_DEATH_TEST && !GTEST_OS_LINUX_ANDROID |
Peter Boström | 1273688 | 2024-03-15 20:06:41 | [diff] [blame] | 62 | #endif // CHECK_WILL_STREAM() |
Alan Zhao | 99479f6b | 2022-08-26 23:03:00 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | } // namespace |