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 // Author: vladl@google.com (Vlad Losev)
31 //
32 // Tests for Google Test itself. Tests in this file throw C++ or SEH
33 // exceptions, and the output is verified by gtest_catch_exceptions_test.py.
34 
35 #include "gtest/gtest.h"
36 
37 #include <stdio.h>  // NOLINT
38 #include <stdlib.h>  // For exit().
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:
SehExceptionInConstructorTest()55   SehExceptionInConstructorTest() { RaiseException(42, 0, 0, NULL); }
56 };
57 
TEST_F(SehExceptionInConstructorTest,ThrowsExceptionInConstructor)58 TEST_F(SehExceptionInConstructorTest, ThrowsExceptionInConstructor) {}
59 
60 class SehExceptionInDestructorTest : public Test {
61  public:
~SehExceptionInDestructorTest()62   ~SehExceptionInDestructorTest() { RaiseException(42, 0, 0, NULL); }
63 };
64 
TEST_F(SehExceptionInDestructorTest,ThrowsExceptionInDestructor)65 TEST_F(SehExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
66 
67 class SehExceptionInSetUpTestCaseTest : public Test {
68  public:
SetUpTestCase()69   static void SetUpTestCase() { RaiseException(42, 0, 0, NULL); }
70 };
71 
TEST_F(SehExceptionInSetUpTestCaseTest,ThrowsExceptionInSetUpTestCase)72 TEST_F(SehExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {}
73 
74 class SehExceptionInTearDownTestCaseTest : public Test {
75  public:
TearDownTestCase()76   static void TearDownTestCase() { RaiseException(42, 0, 0, NULL); }
77 };
78 
TEST_F(SehExceptionInTearDownTestCaseTest,ThrowsExceptionInTearDownTestCase)79 TEST_F(SehExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {}
80 
81 class SehExceptionInSetUpTest : public Test {
82  protected:
SetUp()83   virtual void SetUp() { RaiseException(42, 0, 0, NULL); }
84 };
85 
TEST_F(SehExceptionInSetUpTest,ThrowsExceptionInSetUp)86 TEST_F(SehExceptionInSetUpTest, ThrowsExceptionInSetUp) {}
87 
88 class SehExceptionInTearDownTest : public Test {
89  protected:
TearDown()90   virtual void TearDown() { RaiseException(42, 0, 0, NULL); }
91 };
92 
TEST_F(SehExceptionInTearDownTest,ThrowsExceptionInTearDown)93 TEST_F(SehExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
94 
TEST(SehExceptionTest,ThrowsSehException)95 TEST(SehExceptionTest, ThrowsSehException) {
96   RaiseException(42, 0, 0, NULL);
97 }
98 
99 #endif  // GTEST_HAS_SEH
100 
101 #if GTEST_HAS_EXCEPTIONS
102 
103 class CxxExceptionInConstructorTest : public Test {
104  public:
CxxExceptionInConstructorTest()105   CxxExceptionInConstructorTest() {
106     // Without this macro VC++ complains about unreachable code at the end of
107     // the constructor.
108     GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
109         throw std::runtime_error("Standard C++ exception"));
110   }
111 
TearDownTestCase()112   static void TearDownTestCase() {
113     printf("%s",
114            "CxxExceptionInConstructorTest::TearDownTestCase() "
115            "called as expected.\n");
116   }
117 
118  protected:
~CxxExceptionInConstructorTest()119   ~CxxExceptionInConstructorTest() {
120     ADD_FAILURE() << "CxxExceptionInConstructorTest destructor "
121                   << "called unexpectedly.";
122   }
123 
SetUp()124   virtual void SetUp() {
125     ADD_FAILURE() << "CxxExceptionInConstructorTest::SetUp() "
126                   << "called unexpectedly.";
127   }
128 
TearDown()129   virtual void TearDown() {
130     ADD_FAILURE() << "CxxExceptionInConstructorTest::TearDown() "
131                   << "called unexpectedly.";
132   }
133 };
134 
TEST_F(CxxExceptionInConstructorTest,ThrowsExceptionInConstructor)135 TEST_F(CxxExceptionInConstructorTest, ThrowsExceptionInConstructor) {
136   ADD_FAILURE() << "CxxExceptionInConstructorTest test body "
137                 << "called unexpectedly.";
138 }
139 
140 // Exceptions in destructors are not supported in C++11.
141 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) &&  __cplusplus < 201103L
142 class CxxExceptionInDestructorTest : public Test {
143  public:
TearDownTestCase()144   static void TearDownTestCase() {
145     printf("%s",
146            "CxxExceptionInDestructorTest::TearDownTestCase() "
147            "called as expected.\n");
148   }
149 
150  protected:
~CxxExceptionInDestructorTest()151   ~CxxExceptionInDestructorTest() {
152     GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
153         throw std::runtime_error("Standard C++ exception"));
154   }
155 };
156 
TEST_F(CxxExceptionInDestructorTest,ThrowsExceptionInDestructor)157 TEST_F(CxxExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
158 #endif  // C++11 mode
159 
160 class CxxExceptionInSetUpTestCaseTest : public Test {
161  public:
CxxExceptionInSetUpTestCaseTest()162   CxxExceptionInSetUpTestCaseTest() {
163     printf("%s",
164            "CxxExceptionInSetUpTestCaseTest constructor "
165            "called as expected.\n");
166   }
167 
SetUpTestCase()168   static void SetUpTestCase() {
169     throw std::runtime_error("Standard C++ exception");
170   }
171 
TearDownTestCase()172   static void TearDownTestCase() {
173     printf("%s",
174            "CxxExceptionInSetUpTestCaseTest::TearDownTestCase() "
175            "called as expected.\n");
176   }
177 
178  protected:
~CxxExceptionInSetUpTestCaseTest()179   ~CxxExceptionInSetUpTestCaseTest() {
180     printf("%s",
181            "CxxExceptionInSetUpTestCaseTest destructor "
182            "called as expected.\n");
183   }
184 
SetUp()185   virtual void SetUp() {
186     printf("%s",
187            "CxxExceptionInSetUpTestCaseTest::SetUp() "
188            "called as expected.\n");
189   }
190 
TearDown()191   virtual void TearDown() {
192     printf("%s",
193            "CxxExceptionInSetUpTestCaseTest::TearDown() "
194            "called as expected.\n");
195   }
196 };
197 
TEST_F(CxxExceptionInSetUpTestCaseTest,ThrowsExceptionInSetUpTestCase)198 TEST_F(CxxExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {
199   printf("%s",
200          "CxxExceptionInSetUpTestCaseTest test body "
201          "called as expected.\n");
202 }
203 
204 class CxxExceptionInTearDownTestCaseTest : public Test {
205  public:
TearDownTestCase()206   static void TearDownTestCase() {
207     throw std::runtime_error("Standard C++ exception");
208   }
209 };
210 
TEST_F(CxxExceptionInTearDownTestCaseTest,ThrowsExceptionInTearDownTestCase)211 TEST_F(CxxExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {}
212 
213 class CxxExceptionInSetUpTest : public Test {
214  public:
TearDownTestCase()215   static void TearDownTestCase() {
216     printf("%s",
217            "CxxExceptionInSetUpTest::TearDownTestCase() "
218            "called as expected.\n");
219   }
220 
221  protected:
~CxxExceptionInSetUpTest()222   ~CxxExceptionInSetUpTest() {
223     printf("%s",
224            "CxxExceptionInSetUpTest destructor "
225            "called as expected.\n");
226   }
227 
SetUp()228   virtual void SetUp() { throw std::runtime_error("Standard C++ exception"); }
229 
TearDown()230   virtual void TearDown() {
231     printf("%s",
232            "CxxExceptionInSetUpTest::TearDown() "
233            "called as expected.\n");
234   }
235 };
236 
TEST_F(CxxExceptionInSetUpTest,ThrowsExceptionInSetUp)237 TEST_F(CxxExceptionInSetUpTest, ThrowsExceptionInSetUp) {
238   ADD_FAILURE() << "CxxExceptionInSetUpTest test body "
239                 << "called unexpectedly.";
240 }
241 
242 class CxxExceptionInTearDownTest : public Test {
243  public:
TearDownTestCase()244   static void TearDownTestCase() {
245     printf("%s",
246            "CxxExceptionInTearDownTest::TearDownTestCase() "
247            "called as expected.\n");
248   }
249 
250  protected:
~CxxExceptionInTearDownTest()251   ~CxxExceptionInTearDownTest() {
252     printf("%s",
253            "CxxExceptionInTearDownTest destructor "
254            "called as expected.\n");
255   }
256 
TearDown()257   virtual void TearDown() {
258     throw std::runtime_error("Standard C++ exception");
259   }
260 };
261 
TEST_F(CxxExceptionInTearDownTest,ThrowsExceptionInTearDown)262 TEST_F(CxxExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
263 
264 class CxxExceptionInTestBodyTest : public Test {
265  public:
TearDownTestCase()266   static void TearDownTestCase() {
267     printf("%s",
268            "CxxExceptionInTestBodyTest::TearDownTestCase() "
269            "called as expected.\n");
270   }
271 
272  protected:
~CxxExceptionInTestBodyTest()273   ~CxxExceptionInTestBodyTest() {
274     printf("%s",
275            "CxxExceptionInTestBodyTest destructor "
276            "called as expected.\n");
277   }
278 
TearDown()279   virtual void TearDown() {
280     printf("%s",
281            "CxxExceptionInTestBodyTest::TearDown() "
282            "called as expected.\n");
283   }
284 };
285 
TEST_F(CxxExceptionInTestBodyTest,ThrowsStdCxxException)286 TEST_F(CxxExceptionInTestBodyTest, ThrowsStdCxxException) {
287   throw std::runtime_error("Standard C++ exception");
288 }
289 
TEST(CxxExceptionTest,ThrowsNonStdCxxException)290 TEST(CxxExceptionTest, ThrowsNonStdCxxException) {
291   throw "C-string";
292 }
293 
294 // This terminate handler aborts the program using exit() rather than abort().
295 // This avoids showing pop-ups on Windows systems and core dumps on Unix-like
296 // ones.
TerminateHandler()297 void TerminateHandler() {
298   fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program.");
299   fflush(NULL);
300   exit(3);
301 }
302 
303 #endif  // GTEST_HAS_EXCEPTIONS
304 
main(int argc,char ** argv)305 int main(int argc, char** argv) {
306 #if GTEST_HAS_EXCEPTIONS
307   std::set_terminate(&TerminateHandler);
308 #endif
309   testing::InitGoogleTest(&argc, argv);
310   return RUN_ALL_TESTS();
311 }
312