Fix FullScreen crash in Webapp
[platform/framework/web/chromium-efl.git] / base / libcpp_hardening_test.cc
1 // Copyright 2022 The Chromium Authors
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
7 #include "build/build_config.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 // TODO(thakis): Remove _LIBCPP_ENABLE_ASSERTIONS here once
14 // pnacl-saigo's libc++ is new enough.
15 #if !_LIBCPP_ENABLE_ASSERTIONS && !_LIBCPP_ENABLE_SAFE_MODE
16 #error \
17     "Define _LIBCPP_ENABLE_SAFE_MODE to 1 in \
18 buildtools/third_party/libc++/__config_site"
19
20 #endif
21
22 using ::testing::ContainsRegex;
23 using ::testing::Not;
24
25 // This test checks for two things:
26 //
27 // 0. Assertions are enabled for libc++ and cause the process to crash when
28 //    invoked (in this test's case, when an out of bounds access is made in
29 //    std::vector.
30 // 1. The correct assertion handler is linked in depending on whether or not
31 //    this test is built in debug mode. libc++ passes the string
32 //    {file}:{line} assertion {expression} failed: {message}. The default
33 //    libc++ handler, which we use in debug mode, prints this string to stderr,
34 //    while the nondebug assertion handler just crashes immediately. Therefore,
35 //    to check that we linked in the correct assertion handler, we check for the
36 //    presence or absence of the above string.
37 TEST(LibcppHardeningTest, Assertions) {
38   std::vector<int> vec = {0, 1, 2};
39 #ifdef NDEBUG
40 // We have to explicitly check for the GTEST_HAS_DEATH_TEST macro instead of
41 // using EXPECT_DEATH_IF_SUPPORTED(...) for the following reasons:
42 //
43 // 0. EXPECT_DEATH(...) does not support (non-escaped) parentheses in the regex,
44 //    so we can't use negative look arounds (https://stackoverflow.com/a/406408)
45 //    to check that the error message doesn't exist.
46 // 1. EXPECT_DEATH_IF_SUPPORTED(...) does not support having gmock matchers as
47 //    the second argument if GTEST_HAS_DEATH_TEST is false.
48 //
49 // We also have to prevent this test from running on Android because even though
50 // death tests are supported on Android, GTest death tests don't work with
51 // base::ImmediateCrash() (https://crbug.com/1353549#c2).
52 #if GTEST_HAS_DEATH_TEST && !GTEST_OS_LINUX_ANDROID
53   EXPECT_DEATH(vec[3], Not(ContainsRegex(".*assertion.*failed:")));
54 #else
55   GTEST_UNSUPPORTED_DEATH_TEST(vec[3], "", );
56 #endif  // GTEST_HAS_DEATH_TEST && !GTEST_OS_LINUX_ANDROID
57 #else
58   EXPECT_DEATH_IF_SUPPORTED(vec[3], ".*assertion.*failed:");
59 #endif  // ifdef NDEBUG
60 }
61
62 }  // namespace