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
49using testing::Test;
50
51#if GTEST_HAS_SEH
52
53class SehExceptionInConstructorTest : public Test {
54 public:
55  SehExceptionInConstructorTest() { RaiseException(42, 0, 0, NULL); }
56};
57
58TEST_F(SehExceptionInConstructorTest, ThrowsExceptionInConstructor) {}
59
60class SehExceptionInDestructorTest : public Test {
61 public:
62  ~SehExceptionInDestructorTest() { RaiseException(42, 0, 0, NULL); }
63};
64
65TEST_F(SehExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
66
67class SehExceptionInSetUpTestCaseTest : public Test {
68 public:
69  static void SetUpTestCase() { RaiseException(42, 0, 0, NULL); }
70};
71
72TEST_F(SehExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {}
73
74class SehExceptionInTearDownTestCaseTest : public Test {
75 public:
76  static void TearDownTestCase() { RaiseException(42, 0, 0, NULL); }
77};
78
79TEST_F(SehExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {}
80
81class SehExceptionInSetUpTest : public Test {
82 protected:
83  virtual void SetUp() { RaiseException(42, 0, 0, NULL); }
84};
85
86TEST_F(SehExceptionInSetUpTest, ThrowsExceptionInSetUp) {}
87
88class SehExceptionInTearDownTest : public Test {
89 protected:
90  virtual void TearDown() { RaiseException(42, 0, 0, NULL); }
91};
92
93TEST_F(SehExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
94
95TEST(SehExceptionTest, ThrowsSehException) {
96  RaiseException(42, 0, 0, NULL);
97}
98
99#endif  // GTEST_HAS_SEH
100
101#if GTEST_HAS_EXCEPTIONS
102
103class CxxExceptionInConstructorTest : public Test {
104 public:
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
112  static void TearDownTestCase() {
113    printf("%s",
114           "CxxExceptionInConstructorTest::TearDownTestCase() "
115           "called as expected.\n");
116  }
117
118 protected:
119  ~CxxExceptionInConstructorTest() {
120    ADD_FAILURE() << "CxxExceptionInConstructorTest destructor "
121                  << "called unexpectedly.";
122  }
123
124  virtual void SetUp() {
125    ADD_FAILURE() << "CxxExceptionInConstructorTest::SetUp() "
126                  << "called unexpectedly.";
127  }
128
129  virtual void TearDown() {
130    ADD_FAILURE() << "CxxExceptionInConstructorTest::TearDown() "
131                  << "called unexpectedly.";
132  }
133};
134
135TEST_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 && _MSC_VER < 1900
142class CxxExceptionInDestructorTest : public Test {
143 public:
144  static void TearDownTestCase() {
145    printf("%s",
146           "CxxExceptionInDestructorTest::TearDownTestCase() "
147           "called as expected.\n");
148  }
149
150 protected:
151  ~CxxExceptionInDestructorTest() {
152    GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
153        throw std::runtime_error("Standard C++ exception"));
154  }
155};
156
157TEST_F(CxxExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
158#endif  // C++11 mode
159
160class CxxExceptionInSetUpTestCaseTest : public Test {
161 public:
162  CxxExceptionInSetUpTestCaseTest() {
163    printf("%s",
164           "CxxExceptionInSetUpTestCaseTest constructor "
165           "called as expected.\n");
166  }
167
168  static void SetUpTestCase() {
169    throw std::runtime_error("Standard C++ exception");
170  }
171
172  static void TearDownTestCase() {
173    printf("%s",
174           "CxxExceptionInSetUpTestCaseTest::TearDownTestCase() "
175           "called as expected.\n");
176  }
177
178 protected:
179  ~CxxExceptionInSetUpTestCaseTest() {
180    printf("%s",
181           "CxxExceptionInSetUpTestCaseTest destructor "
182           "called as expected.\n");
183  }
184
185  virtual void SetUp() {
186    printf("%s",
187           "CxxExceptionInSetUpTestCaseTest::SetUp() "
188           "called as expected.\n");
189  }
190
191  virtual void TearDown() {
192    printf("%s",
193           "CxxExceptionInSetUpTestCaseTest::TearDown() "
194           "called as expected.\n");
195  }
196};
197
198TEST_F(CxxExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {
199  printf("%s",
200         "CxxExceptionInSetUpTestCaseTest test body "
201         "called as expected.\n");
202}
203
204class CxxExceptionInTearDownTestCaseTest : public Test {
205 public:
206  static void TearDownTestCase() {
207    throw std::runtime_error("Standard C++ exception");
208  }
209};
210
211TEST_F(CxxExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {}
212
213class CxxExceptionInSetUpTest : public Test {
214 public:
215  static void TearDownTestCase() {
216    printf("%s",
217           "CxxExceptionInSetUpTest::TearDownTestCase() "
218           "called as expected.\n");
219  }
220
221 protected:
222  ~CxxExceptionInSetUpTest() {
223    printf("%s",
224           "CxxExceptionInSetUpTest destructor "
225           "called as expected.\n");
226  }
227
228  virtual void SetUp() { throw std::runtime_error("Standard C++ exception"); }
229
230  virtual void TearDown() {
231    printf("%s",
232           "CxxExceptionInSetUpTest::TearDown() "
233           "called as expected.\n");
234  }
235};
236
237TEST_F(CxxExceptionInSetUpTest, ThrowsExceptionInSetUp) {
238  ADD_FAILURE() << "CxxExceptionInSetUpTest test body "
239                << "called unexpectedly.";
240}
241
242class CxxExceptionInTearDownTest : public Test {
243 public:
244  static void TearDownTestCase() {
245    printf("%s",
246           "CxxExceptionInTearDownTest::TearDownTestCase() "
247           "called as expected.\n");
248  }
249
250 protected:
251  ~CxxExceptionInTearDownTest() {
252    printf("%s",
253           "CxxExceptionInTearDownTest destructor "
254           "called as expected.\n");
255  }
256
257  virtual void TearDown() {
258    throw std::runtime_error("Standard C++ exception");
259  }
260};
261
262TEST_F(CxxExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
263
264class CxxExceptionInTestBodyTest : public Test {
265 public:
266  static void TearDownTestCase() {
267    printf("%s",
268           "CxxExceptionInTestBodyTest::TearDownTestCase() "
269           "called as expected.\n");
270  }
271
272 protected:
273  ~CxxExceptionInTestBodyTest() {
274    printf("%s",
275           "CxxExceptionInTestBodyTest destructor "
276           "called as expected.\n");
277  }
278
279  virtual void TearDown() {
280    printf("%s",
281           "CxxExceptionInTestBodyTest::TearDown() "
282           "called as expected.\n");
283  }
284};
285
286TEST_F(CxxExceptionInTestBodyTest, ThrowsStdCxxException) {
287  throw std::runtime_error("Standard C++ exception");
288}
289
290TEST(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.
297void 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
305int 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