tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

googletest-catch-exceptions-test_.cc (8386B)


      1 // Copyright 2010, Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 //     * Redistributions of source code must retain the above copyright
      9 // notice, this list of conditions and the following disclaimer.
     10 //     * Redistributions in binary form must reproduce the above
     11 // copyright notice, this list of conditions and the following disclaimer
     12 // in the documentation and/or other materials provided with the
     13 // distribution.
     14 //     * Neither the name of Google Inc. nor the names of its
     15 // contributors may be used to endorse or promote products derived from
     16 // this software without specific prior written permission.
     17 //
     18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 
     30 //
     31 // Tests for Google Test itself. Tests in this file throw C++ or SEH
     32 // exceptions, and the output is verified by
     33 // googletest-catch-exceptions-test.py.
     34 
     35 #include <stdio.h>  // NOLINT
     36 #include <stdlib.h>  // For exit().
     37 
     38 #include "gtest/gtest.h"
     39 
     40 #if GTEST_HAS_SEH
     41 # include <windows.h>
     42 #endif
     43 
     44 #if GTEST_HAS_EXCEPTIONS
     45 # include <exception>  // For set_terminate().
     46 # include <stdexcept>
     47 #endif
     48 
     49 using testing::Test;
     50 
     51 #if GTEST_HAS_SEH
     52 
     53 class SehExceptionInConstructorTest : public Test {
     54 public:
     55  SehExceptionInConstructorTest() { RaiseException(42, 0, 0, NULL); }
     56 };
     57 
     58 TEST_F(SehExceptionInConstructorTest, ThrowsExceptionInConstructor) {}
     59 
     60 class SehExceptionInDestructorTest : public Test {
     61 public:
     62  ~SehExceptionInDestructorTest() { RaiseException(42, 0, 0, NULL); }
     63 };
     64 
     65 TEST_F(SehExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
     66 
     67 class SehExceptionInSetUpTestSuiteTest : public Test {
     68 public:
     69  static void SetUpTestSuite() { RaiseException(42, 0, 0, NULL); }
     70 };
     71 
     72 TEST_F(SehExceptionInSetUpTestSuiteTest, ThrowsExceptionInSetUpTestSuite) {}
     73 
     74 class SehExceptionInTearDownTestSuiteTest : public Test {
     75 public:
     76  static void TearDownTestSuite() { RaiseException(42, 0, 0, NULL); }
     77 };
     78 
     79 TEST_F(SehExceptionInTearDownTestSuiteTest,
     80       ThrowsExceptionInTearDownTestSuite) {}
     81 
     82 class SehExceptionInSetUpTest : public Test {
     83 protected:
     84  virtual void SetUp() { RaiseException(42, 0, 0, NULL); }
     85 };
     86 
     87 TEST_F(SehExceptionInSetUpTest, ThrowsExceptionInSetUp) {}
     88 
     89 class SehExceptionInTearDownTest : public Test {
     90 protected:
     91  virtual void TearDown() { RaiseException(42, 0, 0, NULL); }
     92 };
     93 
     94 TEST_F(SehExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
     95 
     96 TEST(SehExceptionTest, ThrowsSehException) {
     97  RaiseException(42, 0, 0, NULL);
     98 }
     99 
    100 #endif  // GTEST_HAS_SEH
    101 
    102 #if GTEST_HAS_EXCEPTIONS
    103 
    104 class CxxExceptionInConstructorTest : public Test {
    105 public:
    106  CxxExceptionInConstructorTest() {
    107    // Without this macro VC++ complains about unreachable code at the end of
    108    // the constructor.
    109    GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
    110        throw std::runtime_error("Standard C++ exception"));
    111  }
    112 
    113  static void TearDownTestSuite() {
    114    printf("%s",
    115           "CxxExceptionInConstructorTest::TearDownTestSuite() "
    116           "called as expected.\n");
    117  }
    118 
    119 protected:
    120  ~CxxExceptionInConstructorTest() override {
    121    ADD_FAILURE() << "CxxExceptionInConstructorTest destructor "
    122                  << "called unexpectedly.";
    123  }
    124 
    125  void SetUp() override {
    126    ADD_FAILURE() << "CxxExceptionInConstructorTest::SetUp() "
    127                  << "called unexpectedly.";
    128  }
    129 
    130  void TearDown() override {
    131    ADD_FAILURE() << "CxxExceptionInConstructorTest::TearDown() "
    132                  << "called unexpectedly.";
    133  }
    134 };
    135 
    136 TEST_F(CxxExceptionInConstructorTest, ThrowsExceptionInConstructor) {
    137  ADD_FAILURE() << "CxxExceptionInConstructorTest test body "
    138                << "called unexpectedly.";
    139 }
    140 
    141 class CxxExceptionInSetUpTestSuiteTest : public Test {
    142 public:
    143  CxxExceptionInSetUpTestSuiteTest() {
    144    printf("%s",
    145           "CxxExceptionInSetUpTestSuiteTest constructor "
    146           "called as expected.\n");
    147  }
    148 
    149  static void SetUpTestSuite() {
    150    throw std::runtime_error("Standard C++ exception");
    151  }
    152 
    153  static void TearDownTestSuite() {
    154    printf("%s",
    155           "CxxExceptionInSetUpTestSuiteTest::TearDownTestSuite() "
    156           "called as expected.\n");
    157  }
    158 
    159 protected:
    160  ~CxxExceptionInSetUpTestSuiteTest() override {
    161    printf("%s",
    162           "CxxExceptionInSetUpTestSuiteTest destructor "
    163           "called as expected.\n");
    164  }
    165 
    166  void SetUp() override {
    167    printf("%s",
    168           "CxxExceptionInSetUpTestSuiteTest::SetUp() "
    169           "called as expected.\n");
    170  }
    171 
    172  void TearDown() override {
    173    printf("%s",
    174           "CxxExceptionInSetUpTestSuiteTest::TearDown() "
    175           "called as expected.\n");
    176  }
    177 };
    178 
    179 TEST_F(CxxExceptionInSetUpTestSuiteTest, ThrowsExceptionInSetUpTestSuite) {
    180  printf("%s",
    181         "CxxExceptionInSetUpTestSuiteTest test body "
    182         "called as expected.\n");
    183 }
    184 
    185 class CxxExceptionInTearDownTestSuiteTest : public Test {
    186 public:
    187  static void TearDownTestSuite() {
    188    throw std::runtime_error("Standard C++ exception");
    189  }
    190 };
    191 
    192 TEST_F(CxxExceptionInTearDownTestSuiteTest,
    193       ThrowsExceptionInTearDownTestSuite) {}
    194 
    195 class CxxExceptionInSetUpTest : public Test {
    196 public:
    197  static void TearDownTestSuite() {
    198    printf("%s",
    199           "CxxExceptionInSetUpTest::TearDownTestSuite() "
    200           "called as expected.\n");
    201  }
    202 
    203 protected:
    204  ~CxxExceptionInSetUpTest() override {
    205    printf("%s",
    206           "CxxExceptionInSetUpTest destructor "
    207           "called as expected.\n");
    208  }
    209 
    210  void SetUp() override { throw std::runtime_error("Standard C++ exception"); }
    211 
    212  void TearDown() override {
    213    printf("%s",
    214           "CxxExceptionInSetUpTest::TearDown() "
    215           "called as expected.\n");
    216  }
    217 };
    218 
    219 TEST_F(CxxExceptionInSetUpTest, ThrowsExceptionInSetUp) {
    220  ADD_FAILURE() << "CxxExceptionInSetUpTest test body "
    221                << "called unexpectedly.";
    222 }
    223 
    224 class CxxExceptionInTearDownTest : public Test {
    225 public:
    226  static void TearDownTestSuite() {
    227    printf("%s",
    228           "CxxExceptionInTearDownTest::TearDownTestSuite() "
    229           "called as expected.\n");
    230  }
    231 
    232 protected:
    233  ~CxxExceptionInTearDownTest() override {
    234    printf("%s",
    235           "CxxExceptionInTearDownTest destructor "
    236           "called as expected.\n");
    237  }
    238 
    239  void TearDown() override {
    240    throw std::runtime_error("Standard C++ exception");
    241  }
    242 };
    243 
    244 TEST_F(CxxExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
    245 
    246 class CxxExceptionInTestBodyTest : public Test {
    247 public:
    248  static void TearDownTestSuite() {
    249    printf("%s",
    250           "CxxExceptionInTestBodyTest::TearDownTestSuite() "
    251           "called as expected.\n");
    252  }
    253 
    254 protected:
    255  ~CxxExceptionInTestBodyTest() override {
    256    printf("%s",
    257           "CxxExceptionInTestBodyTest destructor "
    258           "called as expected.\n");
    259  }
    260 
    261  void TearDown() override {
    262    printf("%s",
    263           "CxxExceptionInTestBodyTest::TearDown() "
    264           "called as expected.\n");
    265  }
    266 };
    267 
    268 TEST_F(CxxExceptionInTestBodyTest, ThrowsStdCxxException) {
    269  throw std::runtime_error("Standard C++ exception");
    270 }
    271 
    272 TEST(CxxExceptionTest, ThrowsNonStdCxxException) {
    273  throw "C-string";
    274 }
    275 
    276 // This terminate handler aborts the program using exit() rather than abort().
    277 // This avoids showing pop-ups on Windows systems and core dumps on Unix-like
    278 // ones.
    279 void TerminateHandler() {
    280  fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program.");
    281  fflush(nullptr);
    282  exit(3);
    283 }
    284 
    285 #endif  // GTEST_HAS_EXCEPTIONS
    286 
    287 int main(int argc, char** argv) {
    288 #if GTEST_HAS_EXCEPTIONS
    289  std::set_terminate(&TerminateHandler);
    290 #endif
    291  testing::InitGoogleTest(&argc, argv);
    292  return RUN_ALL_TESTS();
    293 }