blob: 5b7242b637bfbbb4588a7f3732bd6d9f07bfaf69 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2022 The Chromium Authors
Alan Zhao99479f6b2022-08-26 23:03:002// 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öm12736882024-03-15 20:06:417#include "base/check.h"
Alan Zhao99479f6b2022-08-26 23:03:008#include "build/build_config.h"
9#include "testing/gmock/include/gmock/gmock.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace {
13
Nico Weber03355062023-09-14 05:04:3614// TODO(thakis): Remove _LIBCPP_ENABLE_ASSERTIONS here once
15// pnacl-saigo's libc++ is new enough.
Jose Dapena Pazad472c52024-02-29 08:29:1716#if defined(__GLIBCXX__)
17#if !defined(_GLIBCXX_ASSERTIONS)
18#error "libstdc++ assertions should be enabled"
19#endif
20#elif !_LIBCPP_ENABLE_ASSERTIONS && \
Hans Wennborg06de54a2023-11-09 20:31:3121 _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_EXTENSIVE
22#error "_LIBCPP_HARDENING_MODE not defined"
Alan Zhao99479f6b2022-08-26 23:03:0023#endif
24
25using ::testing::ContainsRegex;
26using ::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.
40TEST(LibcppHardeningTest, Assertions) {
41 std::vector<int> vec = {0, 1, 2};
Peter Boström12736882024-03-15 20:06:4142#if CHECK_WILL_STREAM()
43 EXPECT_DEATH_IF_SUPPORTED(vec[3], ".*assertion.*failed:");
44#else
Alan Zhao99479f6b2022-08-26 23:03:0045// 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öm25c6ec72022-11-02 23:25:1956// base::ImmediateCrash() (https://crbug.com/1353549#c2).
Alan Zhao99479f6b2022-08-26 23:03:0057#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 Zhao218f8ad22022-09-14 21:09:3361#endif // GTEST_HAS_DEATH_TEST && !GTEST_OS_LINUX_ANDROID
Peter Boström12736882024-03-15 20:06:4162#endif // CHECK_WILL_STREAM()
Alan Zhao99479f6b2022-08-26 23:03:0063}
64
65} // namespace