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) { RaiseException(42, 0, 0, NULL); } 97 98 #endif // GTEST_HAS_SEH 99 100 #if GTEST_HAS_EXCEPTIONS 101 102 class CxxExceptionInConstructorTest : public Test { 103 public: 104 CxxExceptionInConstructorTest() { 105 // Without this macro VC++ complains about unreachable code at the end of 106 // the constructor. 107 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_( 108 throw std::runtime_error("Standard C++ exception")); 109 } 110 111 static void TearDownTestSuite() { 112 printf("%s", 113 "CxxExceptionInConstructorTest::TearDownTestSuite() " 114 "called as expected.\n"); 115 } 116 117 protected: 118 ~CxxExceptionInConstructorTest() override { 119 ADD_FAILURE() << "CxxExceptionInConstructorTest destructor " 120 << "called unexpectedly."; 121 } 122 123 void SetUp() override { 124 ADD_FAILURE() << "CxxExceptionInConstructorTest::SetUp() " 125 << "called unexpectedly."; 126 } 127 128 void TearDown() override { 129 ADD_FAILURE() << "CxxExceptionInConstructorTest::TearDown() " 130 << "called unexpectedly."; 131 } 132 }; 133 134 TEST_F(CxxExceptionInConstructorTest, ThrowsExceptionInConstructor) { 135 ADD_FAILURE() << "CxxExceptionInConstructorTest test body " 136 << "called unexpectedly."; 137 } 138 139 class CxxExceptionInSetUpTestSuiteTest : public Test { 140 public: 141 CxxExceptionInSetUpTestSuiteTest() { 142 printf("%s", 143 "CxxExceptionInSetUpTestSuiteTest constructor " 144 "called as expected.\n"); 145 } 146 147 static void SetUpTestSuite() { 148 throw std::runtime_error("Standard C++ exception"); 149 } 150 151 static void TearDownTestSuite() { 152 printf("%s", 153 "CxxExceptionInSetUpTestSuiteTest::TearDownTestSuite() " 154 "called as expected.\n"); 155 } 156 157 protected: 158 ~CxxExceptionInSetUpTestSuiteTest() override { 159 printf("%s", 160 "CxxExceptionInSetUpTestSuiteTest destructor " 161 "called as expected.\n"); 162 } 163 164 void SetUp() override { 165 printf("%s", 166 "CxxExceptionInSetUpTestSuiteTest::SetUp() " 167 "called as expected.\n"); 168 } 169 170 void TearDown() override { 171 printf("%s", 172 "CxxExceptionInSetUpTestSuiteTest::TearDown() " 173 "called as expected.\n"); 174 } 175 }; 176 177 TEST_F(CxxExceptionInSetUpTestSuiteTest, ThrowsExceptionInSetUpTestSuite) { 178 printf("%s", 179 "CxxExceptionInSetUpTestSuiteTest test body " 180 "called as expected.\n"); 181 } 182 183 class CxxExceptionInTearDownTestSuiteTest : public Test { 184 public: 185 static void TearDownTestSuite() { 186 throw std::runtime_error("Standard C++ exception"); 187 } 188 }; 189 190 TEST_F(CxxExceptionInTearDownTestSuiteTest, 191 ThrowsExceptionInTearDownTestSuite) {} 192 193 class CxxExceptionInSetUpTest : public Test { 194 public: 195 static void TearDownTestSuite() { 196 printf("%s", 197 "CxxExceptionInSetUpTest::TearDownTestSuite() " 198 "called as expected.\n"); 199 } 200 201 protected: 202 ~CxxExceptionInSetUpTest() override { 203 printf("%s", 204 "CxxExceptionInSetUpTest destructor " 205 "called as expected.\n"); 206 } 207 208 void SetUp() override { throw std::runtime_error("Standard C++ exception"); } 209 210 void TearDown() override { 211 printf("%s", 212 "CxxExceptionInSetUpTest::TearDown() " 213 "called as expected.\n"); 214 } 215 }; 216 217 TEST_F(CxxExceptionInSetUpTest, ThrowsExceptionInSetUp) { 218 ADD_FAILURE() << "CxxExceptionInSetUpTest test body " 219 << "called unexpectedly."; 220 } 221 222 class CxxExceptionInTearDownTest : public Test { 223 public: 224 static void TearDownTestSuite() { 225 printf("%s", 226 "CxxExceptionInTearDownTest::TearDownTestSuite() " 227 "called as expected.\n"); 228 } 229 230 protected: 231 ~CxxExceptionInTearDownTest() override { 232 printf("%s", 233 "CxxExceptionInTearDownTest destructor " 234 "called as expected.\n"); 235 } 236 237 void TearDown() override { 238 throw std::runtime_error("Standard C++ exception"); 239 } 240 }; 241 242 TEST_F(CxxExceptionInTearDownTest, ThrowsExceptionInTearDown) {} 243 244 class CxxExceptionInTestBodyTest : public Test { 245 public: 246 static void TearDownTestSuite() { 247 printf("%s", 248 "CxxExceptionInTestBodyTest::TearDownTestSuite() " 249 "called as expected.\n"); 250 } 251 252 protected: 253 ~CxxExceptionInTestBodyTest() override { 254 printf("%s", 255 "CxxExceptionInTestBodyTest destructor " 256 "called as expected.\n"); 257 } 258 259 void TearDown() override { 260 printf("%s", 261 "CxxExceptionInTestBodyTest::TearDown() " 262 "called as expected.\n"); 263 } 264 }; 265 266 TEST_F(CxxExceptionInTestBodyTest, ThrowsStdCxxException) { 267 throw std::runtime_error("Standard C++ exception"); 268 } 269 270 TEST(CxxExceptionTest, ThrowsNonStdCxxException) { throw "C-string"; } 271 272 // This terminate handler aborts the program using exit() rather than abort(). 273 // This avoids showing pop-ups on Windows systems and core dumps on Unix-like 274 // ones. 275 void TerminateHandler() { 276 fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program."); 277 fflush(nullptr); 278 exit(3); 279 } 280 281 #endif // GTEST_HAS_EXCEPTIONS 282 283 int main(int argc, char** argv) { 284 #if GTEST_HAS_EXCEPTIONS 285 std::set_terminate(&TerminateHandler); 286 #endif 287 testing::InitGoogleTest(&argc, argv); 288 return RUN_ALL_TESTS(); 289 } 290