xref: /freebsd/contrib/googletest/googletest/test/googletest-death-test-test.cc (revision 5ca8c28cd8c725b81781201cfdb5f9969396f934)
1b89a7cc2SEnji Cooper // Copyright 2005, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper 
30b89a7cc2SEnji Cooper //
31b89a7cc2SEnji Cooper // Tests for death tests.
32b89a7cc2SEnji Cooper 
33*5ca8c28cSEnji Cooper #include <stdlib.h>
34*5ca8c28cSEnji Cooper 
35b89a7cc2SEnji Cooper #include "gtest/gtest-death-test.h"
36b89a7cc2SEnji Cooper #include "gtest/gtest.h"
37b89a7cc2SEnji Cooper #include "gtest/internal/gtest-filepath.h"
38b89a7cc2SEnji Cooper 
39b89a7cc2SEnji Cooper using testing::internal::AlwaysFalse;
40b89a7cc2SEnji Cooper using testing::internal::AlwaysTrue;
41b89a7cc2SEnji Cooper 
4228f6c2f2SEnji Cooper #ifdef GTEST_HAS_DEATH_TEST
43b89a7cc2SEnji Cooper 
4428f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
45b89a7cc2SEnji Cooper #include <direct.h>  // For chdir().
4628f6c2f2SEnji Cooper #include <fcntl.h>   // For O_BINARY
4728f6c2f2SEnji Cooper #include <io.h>
48b89a7cc2SEnji Cooper #else
49b89a7cc2SEnji Cooper #include <sys/wait.h>  // For waitpid.
5028f6c2f2SEnji Cooper #include <unistd.h>
51b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS
52b89a7cc2SEnji Cooper 
53b89a7cc2SEnji Cooper #include <limits.h>
54b89a7cc2SEnji Cooper #include <signal.h>
55b89a7cc2SEnji Cooper #include <stdio.h>
56b89a7cc2SEnji Cooper 
5728f6c2f2SEnji Cooper #include <string>
5828f6c2f2SEnji Cooper #include <vector>
5928f6c2f2SEnji Cooper 
6028f6c2f2SEnji Cooper #ifdef GTEST_OS_LINUX
61b89a7cc2SEnji Cooper #include <sys/time.h>
62b89a7cc2SEnji Cooper #endif  // GTEST_OS_LINUX
63b89a7cc2SEnji Cooper 
64b89a7cc2SEnji Cooper #include "gtest/gtest-spi.h"
65b89a7cc2SEnji Cooper #include "src/gtest-internal-inl.h"
66b89a7cc2SEnji Cooper 
67b89a7cc2SEnji Cooper namespace posix = ::testing::internal::posix;
68b89a7cc2SEnji Cooper 
6928f6c2f2SEnji Cooper using testing::ContainsRegex;
7028f6c2f2SEnji Cooper using testing::Matcher;
71b89a7cc2SEnji Cooper using testing::Message;
72b89a7cc2SEnji Cooper using testing::internal::DeathTest;
73b89a7cc2SEnji Cooper using testing::internal::DeathTestFactory;
74b89a7cc2SEnji Cooper using testing::internal::FilePath;
75b89a7cc2SEnji Cooper using testing::internal::GetLastErrnoDescription;
76b89a7cc2SEnji Cooper using testing::internal::GetUnitTestImpl;
77b89a7cc2SEnji Cooper using testing::internal::InDeathTestChild;
78b89a7cc2SEnji Cooper using testing::internal::ParseNaturalNumber;
79b89a7cc2SEnji Cooper 
80b89a7cc2SEnji Cooper namespace testing {
81b89a7cc2SEnji Cooper namespace internal {
82b89a7cc2SEnji Cooper 
83b89a7cc2SEnji Cooper // A helper class whose objects replace the death test factory for a
84b89a7cc2SEnji Cooper // single UnitTest object during their lifetimes.
85b89a7cc2SEnji Cooper class ReplaceDeathTestFactory {
86b89a7cc2SEnji Cooper  public:
ReplaceDeathTestFactory(DeathTestFactory * new_factory)87b89a7cc2SEnji Cooper   explicit ReplaceDeathTestFactory(DeathTestFactory* new_factory)
88b89a7cc2SEnji Cooper       : unit_test_impl_(GetUnitTestImpl()) {
89b89a7cc2SEnji Cooper     old_factory_ = unit_test_impl_->death_test_factory_.release();
90b89a7cc2SEnji Cooper     unit_test_impl_->death_test_factory_.reset(new_factory);
91b89a7cc2SEnji Cooper   }
92b89a7cc2SEnji Cooper 
~ReplaceDeathTestFactory()93b89a7cc2SEnji Cooper   ~ReplaceDeathTestFactory() {
94b89a7cc2SEnji Cooper     unit_test_impl_->death_test_factory_.release();
95b89a7cc2SEnji Cooper     unit_test_impl_->death_test_factory_.reset(old_factory_);
96b89a7cc2SEnji Cooper   }
9728f6c2f2SEnji Cooper 
98b89a7cc2SEnji Cooper  private:
99b89a7cc2SEnji Cooper   // Prevents copying ReplaceDeathTestFactory objects.
100b89a7cc2SEnji Cooper   ReplaceDeathTestFactory(const ReplaceDeathTestFactory&);
101b89a7cc2SEnji Cooper   void operator=(const ReplaceDeathTestFactory&);
102b89a7cc2SEnji Cooper 
103b89a7cc2SEnji Cooper   UnitTestImpl* unit_test_impl_;
104b89a7cc2SEnji Cooper   DeathTestFactory* old_factory_;
105b89a7cc2SEnji Cooper };
106b89a7cc2SEnji Cooper 
107b89a7cc2SEnji Cooper }  // namespace internal
108b89a7cc2SEnji Cooper }  // namespace testing
109b89a7cc2SEnji Cooper 
11028f6c2f2SEnji Cooper namespace {
11128f6c2f2SEnji Cooper 
DieWithMessage(const::std::string & message)112b89a7cc2SEnji Cooper void DieWithMessage(const ::std::string& message) {
113b89a7cc2SEnji Cooper   fprintf(stderr, "%s", message.c_str());
114b89a7cc2SEnji Cooper   fflush(stderr);  // Make sure the text is printed before the process exits.
115b89a7cc2SEnji Cooper 
116*5ca8c28cSEnji Cooper   // We call _Exit() instead of exit(), as the former is a direct
117b89a7cc2SEnji Cooper   // system call and thus safer in the presence of threads.  exit()
118b89a7cc2SEnji Cooper   // will invoke user-defined exit-hooks, which may do dangerous
119b89a7cc2SEnji Cooper   // things that conflict with death tests.
120b89a7cc2SEnji Cooper   //
121*5ca8c28cSEnji Cooper   // Some compilers can recognize that _Exit() never returns and issue the
122b89a7cc2SEnji Cooper   // 'unreachable code' warning for code following this function, unless
123b89a7cc2SEnji Cooper   // fooled by a fake condition.
124*5ca8c28cSEnji Cooper   if (AlwaysTrue()) _Exit(1);
125b89a7cc2SEnji Cooper }
126b89a7cc2SEnji Cooper 
DieInside(const::std::string & function)127b89a7cc2SEnji Cooper void DieInside(const ::std::string& function) {
128b89a7cc2SEnji Cooper   DieWithMessage("death inside " + function + "().");
129b89a7cc2SEnji Cooper }
130b89a7cc2SEnji Cooper 
131b89a7cc2SEnji Cooper // Tests that death tests work.
132b89a7cc2SEnji Cooper 
133b89a7cc2SEnji Cooper class TestForDeathTest : public testing::Test {
134b89a7cc2SEnji Cooper  protected:
TestForDeathTest()135b89a7cc2SEnji Cooper   TestForDeathTest() : original_dir_(FilePath::GetCurrentDir()) {}
136b89a7cc2SEnji Cooper 
~TestForDeathTest()13728f6c2f2SEnji Cooper   ~TestForDeathTest() override { posix::ChDir(original_dir_.c_str()); }
138b89a7cc2SEnji Cooper 
139b89a7cc2SEnji Cooper   // A static member function that's expected to die.
StaticMemberFunction()140b89a7cc2SEnji Cooper   static void StaticMemberFunction() { DieInside("StaticMemberFunction"); }
141b89a7cc2SEnji Cooper 
142b89a7cc2SEnji Cooper   // A method of the test fixture that may die.
MemberFunction()143b89a7cc2SEnji Cooper   void MemberFunction() {
14428f6c2f2SEnji Cooper     if (should_die_) DieInside("MemberFunction");
145b89a7cc2SEnji Cooper   }
146b89a7cc2SEnji Cooper 
14728f6c2f2SEnji Cooper   // True if and only if MemberFunction() should die.
148b89a7cc2SEnji Cooper   bool should_die_;
149b89a7cc2SEnji Cooper   const FilePath original_dir_;
150b89a7cc2SEnji Cooper };
151b89a7cc2SEnji Cooper 
152b89a7cc2SEnji Cooper // A class with a member function that may die.
153b89a7cc2SEnji Cooper class MayDie {
154b89a7cc2SEnji Cooper  public:
MayDie(bool should_die)155b89a7cc2SEnji Cooper   explicit MayDie(bool should_die) : should_die_(should_die) {}
156b89a7cc2SEnji Cooper 
157b89a7cc2SEnji Cooper   // A member function that may die.
MemberFunction() const158b89a7cc2SEnji Cooper   void MemberFunction() const {
15928f6c2f2SEnji Cooper     if (should_die_) DieInside("MayDie::MemberFunction");
160b89a7cc2SEnji Cooper   }
161b89a7cc2SEnji Cooper 
162b89a7cc2SEnji Cooper  private:
16328f6c2f2SEnji Cooper   // True if and only if MemberFunction() should die.
164b89a7cc2SEnji Cooper   bool should_die_;
165b89a7cc2SEnji Cooper };
166b89a7cc2SEnji Cooper 
167b89a7cc2SEnji Cooper // A global function that's expected to die.
GlobalFunction()168b89a7cc2SEnji Cooper void GlobalFunction() { DieInside("GlobalFunction"); }
169b89a7cc2SEnji Cooper 
170b89a7cc2SEnji Cooper // A non-void function that's expected to die.
NonVoidFunction()171b89a7cc2SEnji Cooper int NonVoidFunction() {
172b89a7cc2SEnji Cooper   DieInside("NonVoidFunction");
173b89a7cc2SEnji Cooper   return 1;
174b89a7cc2SEnji Cooper }
175b89a7cc2SEnji Cooper 
176b89a7cc2SEnji Cooper // A unary function that may die.
DieIf(bool should_die)177b89a7cc2SEnji Cooper void DieIf(bool should_die) {
17828f6c2f2SEnji Cooper   if (should_die) DieInside("DieIf");
179b89a7cc2SEnji Cooper }
180b89a7cc2SEnji Cooper 
181b89a7cc2SEnji Cooper // A binary function that may die.
DieIfLessThan(int x,int y)182b89a7cc2SEnji Cooper bool DieIfLessThan(int x, int y) {
183b89a7cc2SEnji Cooper   if (x < y) {
184b89a7cc2SEnji Cooper     DieInside("DieIfLessThan");
185b89a7cc2SEnji Cooper   }
186b89a7cc2SEnji Cooper   return true;
187b89a7cc2SEnji Cooper }
188b89a7cc2SEnji Cooper 
189b89a7cc2SEnji Cooper // Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture.
DeathTestSubroutine()190b89a7cc2SEnji Cooper void DeathTestSubroutine() {
191b89a7cc2SEnji Cooper   EXPECT_DEATH(GlobalFunction(), "death.*GlobalFunction");
192b89a7cc2SEnji Cooper   ASSERT_DEATH(GlobalFunction(), "death.*GlobalFunction");
193b89a7cc2SEnji Cooper }
194b89a7cc2SEnji Cooper 
195b89a7cc2SEnji Cooper // Death in dbg, not opt.
DieInDebugElse12(int * sideeffect)196b89a7cc2SEnji Cooper int DieInDebugElse12(int* sideeffect) {
197b89a7cc2SEnji Cooper   if (sideeffect) *sideeffect = 12;
198b89a7cc2SEnji Cooper 
199b89a7cc2SEnji Cooper #ifndef NDEBUG
200b89a7cc2SEnji Cooper 
201b89a7cc2SEnji Cooper   DieInside("DieInDebugElse12");
202b89a7cc2SEnji Cooper 
203b89a7cc2SEnji Cooper #endif  // NDEBUG
204b89a7cc2SEnji Cooper 
205b89a7cc2SEnji Cooper   return 12;
206b89a7cc2SEnji Cooper }
207b89a7cc2SEnji Cooper 
20828f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
20928f6c2f2SEnji Cooper 
21028f6c2f2SEnji Cooper // Death in dbg due to Windows CRT assertion failure, not opt.
DieInCRTDebugElse12(int * sideeffect)21128f6c2f2SEnji Cooper int DieInCRTDebugElse12(int* sideeffect) {
21228f6c2f2SEnji Cooper   if (sideeffect) *sideeffect = 12;
21328f6c2f2SEnji Cooper 
21428f6c2f2SEnji Cooper   // Create an invalid fd by closing a valid one
21528f6c2f2SEnji Cooper   int fdpipe[2];
21628f6c2f2SEnji Cooper   EXPECT_EQ(_pipe(fdpipe, 256, O_BINARY), 0);
21728f6c2f2SEnji Cooper   EXPECT_EQ(_close(fdpipe[0]), 0);
21828f6c2f2SEnji Cooper   EXPECT_EQ(_close(fdpipe[1]), 0);
21928f6c2f2SEnji Cooper 
22028f6c2f2SEnji Cooper   // _dup() should crash in debug mode
22128f6c2f2SEnji Cooper   EXPECT_EQ(_dup(fdpipe[0]), -1);
22228f6c2f2SEnji Cooper 
22328f6c2f2SEnji Cooper   return 12;
22428f6c2f2SEnji Cooper }
22528f6c2f2SEnji Cooper 
22628f6c2f2SEnji Cooper #endif  // GTEST_OS_WINDOWS
22728f6c2f2SEnji Cooper 
22828f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_FUCHSIA)
229b89a7cc2SEnji Cooper 
230b89a7cc2SEnji Cooper // Tests the ExitedWithCode predicate.
TEST(ExitStatusPredicateTest,ExitedWithCode)231b89a7cc2SEnji Cooper TEST(ExitStatusPredicateTest, ExitedWithCode) {
232b89a7cc2SEnji Cooper   // On Windows, the process's exit code is the same as its exit status,
233b89a7cc2SEnji Cooper   // so the predicate just compares the its input with its parameter.
234b89a7cc2SEnji Cooper   EXPECT_TRUE(testing::ExitedWithCode(0)(0));
235b89a7cc2SEnji Cooper   EXPECT_TRUE(testing::ExitedWithCode(1)(1));
236b89a7cc2SEnji Cooper   EXPECT_TRUE(testing::ExitedWithCode(42)(42));
237b89a7cc2SEnji Cooper   EXPECT_FALSE(testing::ExitedWithCode(0)(1));
238b89a7cc2SEnji Cooper   EXPECT_FALSE(testing::ExitedWithCode(1)(0));
239b89a7cc2SEnji Cooper }
240b89a7cc2SEnji Cooper 
241b89a7cc2SEnji Cooper #else
242b89a7cc2SEnji Cooper 
243*5ca8c28cSEnji Cooper // Returns the exit status of a process that calls _Exit(2) with a
244b89a7cc2SEnji Cooper // given exit code.  This is a helper function for the
245b89a7cc2SEnji Cooper // ExitStatusPredicateTest test suite.
NormalExitStatus(int exit_code)246b89a7cc2SEnji Cooper static int NormalExitStatus(int exit_code) {
247b89a7cc2SEnji Cooper   pid_t child_pid = fork();
248b89a7cc2SEnji Cooper   if (child_pid == 0) {
249*5ca8c28cSEnji Cooper     _Exit(exit_code);
250b89a7cc2SEnji Cooper   }
251b89a7cc2SEnji Cooper   int status;
252b89a7cc2SEnji Cooper   waitpid(child_pid, &status, 0);
253b89a7cc2SEnji Cooper   return status;
254b89a7cc2SEnji Cooper }
255b89a7cc2SEnji Cooper 
256b89a7cc2SEnji Cooper // Returns the exit status of a process that raises a given signal.
257b89a7cc2SEnji Cooper // If the signal does not cause the process to die, then it returns
258b89a7cc2SEnji Cooper // instead the exit status of a process that exits normally with exit
259b89a7cc2SEnji Cooper // code 1.  This is a helper function for the ExitStatusPredicateTest
260b89a7cc2SEnji Cooper // test suite.
KilledExitStatus(int signum)261b89a7cc2SEnji Cooper static int KilledExitStatus(int signum) {
262b89a7cc2SEnji Cooper   pid_t child_pid = fork();
263b89a7cc2SEnji Cooper   if (child_pid == 0) {
264b89a7cc2SEnji Cooper     raise(signum);
265*5ca8c28cSEnji Cooper     _Exit(1);
266b89a7cc2SEnji Cooper   }
267b89a7cc2SEnji Cooper   int status;
268b89a7cc2SEnji Cooper   waitpid(child_pid, &status, 0);
269b89a7cc2SEnji Cooper   return status;
270b89a7cc2SEnji Cooper }
271b89a7cc2SEnji Cooper 
272b89a7cc2SEnji Cooper // Tests the ExitedWithCode predicate.
TEST(ExitStatusPredicateTest,ExitedWithCode)273b89a7cc2SEnji Cooper TEST(ExitStatusPredicateTest, ExitedWithCode) {
274b89a7cc2SEnji Cooper   const int status0 = NormalExitStatus(0);
275b89a7cc2SEnji Cooper   const int status1 = NormalExitStatus(1);
276b89a7cc2SEnji Cooper   const int status42 = NormalExitStatus(42);
277b89a7cc2SEnji Cooper   const testing::ExitedWithCode pred0(0);
278b89a7cc2SEnji Cooper   const testing::ExitedWithCode pred1(1);
279b89a7cc2SEnji Cooper   const testing::ExitedWithCode pred42(42);
280b89a7cc2SEnji Cooper   EXPECT_PRED1(pred0, status0);
281b89a7cc2SEnji Cooper   EXPECT_PRED1(pred1, status1);
282b89a7cc2SEnji Cooper   EXPECT_PRED1(pred42, status42);
283b89a7cc2SEnji Cooper   EXPECT_FALSE(pred0(status1));
284b89a7cc2SEnji Cooper   EXPECT_FALSE(pred42(status0));
285b89a7cc2SEnji Cooper   EXPECT_FALSE(pred1(status42));
286b89a7cc2SEnji Cooper }
287b89a7cc2SEnji Cooper 
288b89a7cc2SEnji Cooper // Tests the KilledBySignal predicate.
TEST(ExitStatusPredicateTest,KilledBySignal)289b89a7cc2SEnji Cooper TEST(ExitStatusPredicateTest, KilledBySignal) {
290b89a7cc2SEnji Cooper   const int status_segv = KilledExitStatus(SIGSEGV);
291b89a7cc2SEnji Cooper   const int status_kill = KilledExitStatus(SIGKILL);
292b89a7cc2SEnji Cooper   const testing::KilledBySignal pred_segv(SIGSEGV);
293b89a7cc2SEnji Cooper   const testing::KilledBySignal pred_kill(SIGKILL);
294*5ca8c28cSEnji Cooper #if !(defined(GTEST_OS_LINUX_ANDROID) && __ANDROID_API__ <= 21)
295b89a7cc2SEnji Cooper   EXPECT_PRED1(pred_segv, status_segv);
296*5ca8c28cSEnji Cooper #endif
297b89a7cc2SEnji Cooper   EXPECT_PRED1(pred_kill, status_kill);
298b89a7cc2SEnji Cooper   EXPECT_FALSE(pred_segv(status_kill));
299b89a7cc2SEnji Cooper   EXPECT_FALSE(pred_kill(status_segv));
300b89a7cc2SEnji Cooper }
301b89a7cc2SEnji Cooper 
302b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
303b89a7cc2SEnji Cooper 
30428f6c2f2SEnji Cooper // The following code intentionally tests a suboptimal syntax.
30528f6c2f2SEnji Cooper #ifdef __GNUC__
30628f6c2f2SEnji Cooper #pragma GCC diagnostic push
30728f6c2f2SEnji Cooper #pragma GCC diagnostic ignored "-Wdangling-else"
30828f6c2f2SEnji Cooper #pragma GCC diagnostic ignored "-Wempty-body"
30928f6c2f2SEnji Cooper #pragma GCC diagnostic ignored "-Wpragmas"
31028f6c2f2SEnji Cooper #endif
311b89a7cc2SEnji Cooper // Tests that the death test macros expand to code which may or may not
312b89a7cc2SEnji Cooper // be followed by operator<<, and that in either case the complete text
313b89a7cc2SEnji Cooper // comprises only a single C++ statement.
TEST_F(TestForDeathTest,SingleStatement)314b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, SingleStatement) {
315b89a7cc2SEnji Cooper   if (AlwaysFalse())
316b89a7cc2SEnji Cooper     // This would fail if executed; this is a compilation test only
317b89a7cc2SEnji Cooper     ASSERT_DEATH(return, "");
318b89a7cc2SEnji Cooper 
319b89a7cc2SEnji Cooper   if (AlwaysTrue())
320*5ca8c28cSEnji Cooper     EXPECT_DEATH(_Exit(1), "");
321b89a7cc2SEnji Cooper   else
322b89a7cc2SEnji Cooper     // This empty "else" branch is meant to ensure that EXPECT_DEATH
323b89a7cc2SEnji Cooper     // doesn't expand into an "if" statement without an "else"
324b89a7cc2SEnji Cooper     ;
325b89a7cc2SEnji Cooper 
32628f6c2f2SEnji Cooper   if (AlwaysFalse()) ASSERT_DEATH(return, "") << "did not die";
327b89a7cc2SEnji Cooper 
328b89a7cc2SEnji Cooper   if (AlwaysFalse())
329b89a7cc2SEnji Cooper     ;
330b89a7cc2SEnji Cooper   else
331*5ca8c28cSEnji Cooper     EXPECT_DEATH(_Exit(1), "") << 1 << 2 << 3;
332b89a7cc2SEnji Cooper }
33328f6c2f2SEnji Cooper #ifdef __GNUC__
33428f6c2f2SEnji Cooper #pragma GCC diagnostic pop
33528f6c2f2SEnji Cooper #endif
336b89a7cc2SEnji Cooper 
337b89a7cc2SEnji Cooper // Tests that death test macros expand to code which interacts well with switch
338b89a7cc2SEnji Cooper // statements.
TEST_F(TestForDeathTest,SwitchStatement)339b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, SwitchStatement) {
340b89a7cc2SEnji Cooper   // Microsoft compiler usually complains about switch statements without
341b89a7cc2SEnji Cooper   // case labels. We suppress that warning for this test.
342b89a7cc2SEnji Cooper   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4065)
343b89a7cc2SEnji Cooper 
344b89a7cc2SEnji Cooper   switch (0)
345b89a7cc2SEnji Cooper   default:
346*5ca8c28cSEnji Cooper     ASSERT_DEATH(_Exit(1), "") << "exit in default switch handler";
347b89a7cc2SEnji Cooper 
348b89a7cc2SEnji Cooper   switch (0)
349b89a7cc2SEnji Cooper   case 0:
350*5ca8c28cSEnji Cooper     EXPECT_DEATH(_Exit(1), "") << "exit in switch case";
351b89a7cc2SEnji Cooper 
352b89a7cc2SEnji Cooper   GTEST_DISABLE_MSC_WARNINGS_POP_()
353b89a7cc2SEnji Cooper }
354b89a7cc2SEnji Cooper 
355b89a7cc2SEnji Cooper // Tests that a static member function can be used in a "fast" style
356b89a7cc2SEnji Cooper // death test.
TEST_F(TestForDeathTest,StaticMemberFunctionFastStyle)357b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, StaticMemberFunctionFastStyle) {
35828f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "fast");
359b89a7cc2SEnji Cooper   ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember");
360b89a7cc2SEnji Cooper }
361b89a7cc2SEnji Cooper 
362b89a7cc2SEnji Cooper // Tests that a method of the test fixture can be used in a "fast"
363b89a7cc2SEnji Cooper // style death test.
TEST_F(TestForDeathTest,MemberFunctionFastStyle)364b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, MemberFunctionFastStyle) {
36528f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "fast");
366b89a7cc2SEnji Cooper   should_die_ = true;
367b89a7cc2SEnji Cooper   EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction");
368b89a7cc2SEnji Cooper }
369b89a7cc2SEnji Cooper 
ChangeToRootDir()370b89a7cc2SEnji Cooper void ChangeToRootDir() { posix::ChDir(GTEST_PATH_SEP_); }
371b89a7cc2SEnji Cooper 
372b89a7cc2SEnji Cooper // Tests that death tests work even if the current directory has been
373b89a7cc2SEnji Cooper // changed.
TEST_F(TestForDeathTest,FastDeathTestInChangedDir)374b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, FastDeathTestInChangedDir) {
37528f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "fast");
376b89a7cc2SEnji Cooper 
377b89a7cc2SEnji Cooper   ChangeToRootDir();
378*5ca8c28cSEnji Cooper   EXPECT_EXIT(_Exit(1), testing::ExitedWithCode(1), "");
379b89a7cc2SEnji Cooper 
380b89a7cc2SEnji Cooper   ChangeToRootDir();
381*5ca8c28cSEnji Cooper   ASSERT_DEATH(_Exit(1), "");
382b89a7cc2SEnji Cooper }
383b89a7cc2SEnji Cooper 
38428f6c2f2SEnji Cooper #ifdef GTEST_OS_LINUX
SigprofAction(int,siginfo_t *,void *)38528f6c2f2SEnji Cooper void SigprofAction(int, siginfo_t*, void*) { /* no op */
38628f6c2f2SEnji Cooper }
387b89a7cc2SEnji Cooper 
388b89a7cc2SEnji Cooper // Sets SIGPROF action and ITIMER_PROF timer (interval: 1ms).
SetSigprofActionAndTimer()389b89a7cc2SEnji Cooper void SetSigprofActionAndTimer() {
390b89a7cc2SEnji Cooper   struct sigaction signal_action;
391b89a7cc2SEnji Cooper   memset(&signal_action, 0, sizeof(signal_action));
392b89a7cc2SEnji Cooper   sigemptyset(&signal_action.sa_mask);
393b89a7cc2SEnji Cooper   signal_action.sa_sigaction = SigprofAction;
394b89a7cc2SEnji Cooper   signal_action.sa_flags = SA_RESTART | SA_SIGINFO;
39528f6c2f2SEnji Cooper   ASSERT_EQ(0, sigaction(SIGPROF, &signal_action, nullptr));
39628f6c2f2SEnji Cooper   // timer comes second, to avoid SIGPROF premature delivery, as suggested at
39728f6c2f2SEnji Cooper   // https://www.gnu.org/software/libc/manual/html_node/Setting-an-Alarm.html
39828f6c2f2SEnji Cooper   struct itimerval timer;
39928f6c2f2SEnji Cooper   timer.it_interval.tv_sec = 0;
40028f6c2f2SEnji Cooper   timer.it_interval.tv_usec = 1;
40128f6c2f2SEnji Cooper   timer.it_value = timer.it_interval;
40228f6c2f2SEnji Cooper   ASSERT_EQ(0, setitimer(ITIMER_PROF, &timer, nullptr));
403b89a7cc2SEnji Cooper }
404b89a7cc2SEnji Cooper 
405b89a7cc2SEnji Cooper // Disables ITIMER_PROF timer and ignores SIGPROF signal.
DisableSigprofActionAndTimer(struct sigaction * old_signal_action)406b89a7cc2SEnji Cooper void DisableSigprofActionAndTimer(struct sigaction* old_signal_action) {
407b89a7cc2SEnji Cooper   struct itimerval timer;
408b89a7cc2SEnji Cooper   timer.it_interval.tv_sec = 0;
409b89a7cc2SEnji Cooper   timer.it_interval.tv_usec = 0;
410b89a7cc2SEnji Cooper   timer.it_value = timer.it_interval;
41128f6c2f2SEnji Cooper   ASSERT_EQ(0, setitimer(ITIMER_PROF, &timer, nullptr));
412b89a7cc2SEnji Cooper   struct sigaction signal_action;
413b89a7cc2SEnji Cooper   memset(&signal_action, 0, sizeof(signal_action));
414b89a7cc2SEnji Cooper   sigemptyset(&signal_action.sa_mask);
415b89a7cc2SEnji Cooper   signal_action.sa_handler = SIG_IGN;
416b89a7cc2SEnji Cooper   ASSERT_EQ(0, sigaction(SIGPROF, &signal_action, old_signal_action));
417b89a7cc2SEnji Cooper }
418b89a7cc2SEnji Cooper 
419b89a7cc2SEnji Cooper // Tests that death tests work when SIGPROF handler and timer are set.
TEST_F(TestForDeathTest,FastSigprofActionSet)420b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, FastSigprofActionSet) {
42128f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "fast");
422b89a7cc2SEnji Cooper   SetSigprofActionAndTimer();
423*5ca8c28cSEnji Cooper   EXPECT_DEATH(_Exit(1), "");
424b89a7cc2SEnji Cooper   struct sigaction old_signal_action;
425b89a7cc2SEnji Cooper   DisableSigprofActionAndTimer(&old_signal_action);
426b89a7cc2SEnji Cooper   EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction);
427b89a7cc2SEnji Cooper }
428b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,ThreadSafeSigprofActionSet)429b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ThreadSafeSigprofActionSet) {
43028f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "threadsafe");
431b89a7cc2SEnji Cooper   SetSigprofActionAndTimer();
432*5ca8c28cSEnji Cooper   EXPECT_DEATH(_Exit(1), "");
433b89a7cc2SEnji Cooper   struct sigaction old_signal_action;
434b89a7cc2SEnji Cooper   DisableSigprofActionAndTimer(&old_signal_action);
435b89a7cc2SEnji Cooper   EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction);
436b89a7cc2SEnji Cooper }
437b89a7cc2SEnji Cooper #endif  // GTEST_OS_LINUX
438b89a7cc2SEnji Cooper 
439b89a7cc2SEnji Cooper // Repeats a representative sample of death tests in the "threadsafe" style:
440b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,StaticMemberFunctionThreadsafeStyle)441b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, StaticMemberFunctionThreadsafeStyle) {
44228f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "threadsafe");
443b89a7cc2SEnji Cooper   ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember");
444b89a7cc2SEnji Cooper }
445b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,MemberFunctionThreadsafeStyle)446b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, MemberFunctionThreadsafeStyle) {
44728f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "threadsafe");
448b89a7cc2SEnji Cooper   should_die_ = true;
449b89a7cc2SEnji Cooper   EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction");
450b89a7cc2SEnji Cooper }
451b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,ThreadsafeDeathTestInLoop)452b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ThreadsafeDeathTestInLoop) {
45328f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "threadsafe");
454b89a7cc2SEnji Cooper 
455b89a7cc2SEnji Cooper   for (int i = 0; i < 3; ++i)
456*5ca8c28cSEnji Cooper     EXPECT_EXIT(_Exit(i), testing::ExitedWithCode(i), "") << ": i = " << i;
457b89a7cc2SEnji Cooper }
458b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,ThreadsafeDeathTestInChangedDir)459b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ThreadsafeDeathTestInChangedDir) {
46028f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "threadsafe");
461b89a7cc2SEnji Cooper 
462b89a7cc2SEnji Cooper   ChangeToRootDir();
463*5ca8c28cSEnji Cooper   EXPECT_EXIT(_Exit(1), testing::ExitedWithCode(1), "");
464b89a7cc2SEnji Cooper 
465b89a7cc2SEnji Cooper   ChangeToRootDir();
466*5ca8c28cSEnji Cooper   ASSERT_DEATH(_Exit(1), "");
467b89a7cc2SEnji Cooper }
468b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,MixedStyles)469b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, MixedStyles) {
47028f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "threadsafe");
471*5ca8c28cSEnji Cooper   EXPECT_DEATH(_Exit(1), "");
47228f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "fast");
473*5ca8c28cSEnji Cooper   EXPECT_DEATH(_Exit(1), "");
474b89a7cc2SEnji Cooper }
475b89a7cc2SEnji Cooper 
476b89a7cc2SEnji Cooper #if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
477b89a7cc2SEnji Cooper 
478b89a7cc2SEnji Cooper bool pthread_flag;
479b89a7cc2SEnji Cooper 
SetPthreadFlag()48028f6c2f2SEnji Cooper void SetPthreadFlag() { pthread_flag = true; }
481b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,DoesNotExecuteAtforkHooks)482b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DoesNotExecuteAtforkHooks) {
48328f6c2f2SEnji Cooper   if (!GTEST_FLAG_GET(death_test_use_fork)) {
48428f6c2f2SEnji Cooper     GTEST_FLAG_SET(death_test_style, "threadsafe");
485b89a7cc2SEnji Cooper     pthread_flag = false;
48628f6c2f2SEnji Cooper     ASSERT_EQ(0, pthread_atfork(&SetPthreadFlag, nullptr, nullptr));
487*5ca8c28cSEnji Cooper     ASSERT_DEATH(_Exit(1), "");
488b89a7cc2SEnji Cooper     ASSERT_FALSE(pthread_flag);
489b89a7cc2SEnji Cooper   }
490b89a7cc2SEnji Cooper }
491b89a7cc2SEnji Cooper 
492b89a7cc2SEnji Cooper #endif  // GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
493b89a7cc2SEnji Cooper 
494b89a7cc2SEnji Cooper // Tests that a method of another class can be used in a death test.
TEST_F(TestForDeathTest,MethodOfAnotherClass)495b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, MethodOfAnotherClass) {
496b89a7cc2SEnji Cooper   const MayDie x(true);
497b89a7cc2SEnji Cooper   ASSERT_DEATH(x.MemberFunction(), "MayDie\\:\\:MemberFunction");
498b89a7cc2SEnji Cooper }
499b89a7cc2SEnji Cooper 
500b89a7cc2SEnji Cooper // Tests that a global function can be used in a death test.
TEST_F(TestForDeathTest,GlobalFunction)501b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, GlobalFunction) {
502b89a7cc2SEnji Cooper   EXPECT_DEATH(GlobalFunction(), "GlobalFunction");
503b89a7cc2SEnji Cooper }
504b89a7cc2SEnji Cooper 
505b89a7cc2SEnji Cooper // Tests that any value convertible to an RE works as a second
506b89a7cc2SEnji Cooper // argument to EXPECT_DEATH.
TEST_F(TestForDeathTest,AcceptsAnythingConvertibleToRE)507b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AcceptsAnythingConvertibleToRE) {
508b89a7cc2SEnji Cooper   static const char regex_c_str[] = "GlobalFunction";
509b89a7cc2SEnji Cooper   EXPECT_DEATH(GlobalFunction(), regex_c_str);
510b89a7cc2SEnji Cooper 
511b89a7cc2SEnji Cooper   const testing::internal::RE regex(regex_c_str);
512b89a7cc2SEnji Cooper   EXPECT_DEATH(GlobalFunction(), regex);
513b89a7cc2SEnji Cooper 
514b89a7cc2SEnji Cooper   const ::std::string regex_std_str(regex_c_str);
515b89a7cc2SEnji Cooper   EXPECT_DEATH(GlobalFunction(), regex_std_str);
516b89a7cc2SEnji Cooper 
51728f6c2f2SEnji Cooper   // This one is tricky; a temporary pointer into another temporary.  Reference
51828f6c2f2SEnji Cooper   // lifetime extension of the pointer is not sufficient.
51928f6c2f2SEnji Cooper   EXPECT_DEATH(GlobalFunction(), ::std::string(regex_c_str).c_str());
520b89a7cc2SEnji Cooper }
521b89a7cc2SEnji Cooper 
522b89a7cc2SEnji Cooper // Tests that a non-void function can be used in a death test.
TEST_F(TestForDeathTest,NonVoidFunction)523b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, NonVoidFunction) {
524b89a7cc2SEnji Cooper   ASSERT_DEATH(NonVoidFunction(), "NonVoidFunction");
525b89a7cc2SEnji Cooper }
526b89a7cc2SEnji Cooper 
527b89a7cc2SEnji Cooper // Tests that functions that take parameter(s) can be used in a death test.
TEST_F(TestForDeathTest,FunctionWithParameter)528b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, FunctionWithParameter) {
529b89a7cc2SEnji Cooper   EXPECT_DEATH(DieIf(true), "DieIf\\(\\)");
530b89a7cc2SEnji Cooper   EXPECT_DEATH(DieIfLessThan(2, 3), "DieIfLessThan");
531b89a7cc2SEnji Cooper }
532b89a7cc2SEnji Cooper 
533b89a7cc2SEnji Cooper // Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture.
TEST_F(TestForDeathTest,OutsideFixture)53428f6c2f2SEnji Cooper TEST_F(TestForDeathTest, OutsideFixture) { DeathTestSubroutine(); }
535b89a7cc2SEnji Cooper 
536b89a7cc2SEnji Cooper // Tests that death tests can be done inside a loop.
TEST_F(TestForDeathTest,InsideLoop)537b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, InsideLoop) {
538b89a7cc2SEnji Cooper   for (int i = 0; i < 5; i++) {
539b89a7cc2SEnji Cooper     EXPECT_DEATH(DieIfLessThan(-1, i), "DieIfLessThan") << "where i == " << i;
540b89a7cc2SEnji Cooper   }
541b89a7cc2SEnji Cooper }
542b89a7cc2SEnji Cooper 
543b89a7cc2SEnji Cooper // Tests that a compound statement can be used in a death test.
TEST_F(TestForDeathTest,CompoundStatement)544b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, CompoundStatement) {
54528f6c2f2SEnji Cooper   EXPECT_DEATH(
54628f6c2f2SEnji Cooper       {  // NOLINT
547b89a7cc2SEnji Cooper         const int x = 2;
548b89a7cc2SEnji Cooper         const int y = x + 1;
549b89a7cc2SEnji Cooper         DieIfLessThan(x, y);
550b89a7cc2SEnji Cooper       },
551b89a7cc2SEnji Cooper       "DieIfLessThan");
552b89a7cc2SEnji Cooper }
553b89a7cc2SEnji Cooper 
554b89a7cc2SEnji Cooper // Tests that code that doesn't die causes a death test to fail.
TEST_F(TestForDeathTest,DoesNotDie)555b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DoesNotDie) {
55628f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(DieIf(false), "DieIf"), "failed to die");
557b89a7cc2SEnji Cooper }
558b89a7cc2SEnji Cooper 
559b89a7cc2SEnji Cooper // Tests that a death test fails when the error message isn't expected.
TEST_F(TestForDeathTest,ErrorMessageMismatch)560b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ErrorMessageMismatch) {
56128f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
56228f6c2f2SEnji Cooper       {  // NOLINT
56328f6c2f2SEnji Cooper         EXPECT_DEATH(DieIf(true), "DieIfLessThan")
56428f6c2f2SEnji Cooper             << "End of death test message.";
56528f6c2f2SEnji Cooper       },
56628f6c2f2SEnji Cooper       "died but not with expected error");
567b89a7cc2SEnji Cooper }
568b89a7cc2SEnji Cooper 
56928f6c2f2SEnji Cooper // On exit, *aborted will be true if and only if the EXPECT_DEATH()
57028f6c2f2SEnji Cooper // statement aborted the function.
ExpectDeathTestHelper(bool * aborted)571b89a7cc2SEnji Cooper void ExpectDeathTestHelper(bool* aborted) {
572b89a7cc2SEnji Cooper   *aborted = true;
573b89a7cc2SEnji Cooper   EXPECT_DEATH(DieIf(false), "DieIf");  // This assertion should fail.
574b89a7cc2SEnji Cooper   *aborted = false;
575b89a7cc2SEnji Cooper }
576b89a7cc2SEnji Cooper 
577b89a7cc2SEnji Cooper // Tests that EXPECT_DEATH doesn't abort the test on failure.
TEST_F(TestForDeathTest,EXPECT_DEATH)578b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, EXPECT_DEATH) {
579b89a7cc2SEnji Cooper   bool aborted = true;
58028f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE(ExpectDeathTestHelper(&aborted), "failed to die");
581b89a7cc2SEnji Cooper   EXPECT_FALSE(aborted);
582b89a7cc2SEnji Cooper }
583b89a7cc2SEnji Cooper 
584b89a7cc2SEnji Cooper // Tests that ASSERT_DEATH does abort the test on failure.
TEST_F(TestForDeathTest,ASSERT_DEATH)585b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ASSERT_DEATH) {
586b89a7cc2SEnji Cooper   static bool aborted;
58728f6c2f2SEnji Cooper   EXPECT_FATAL_FAILURE(
58828f6c2f2SEnji Cooper       {  // NOLINT
589b89a7cc2SEnji Cooper         aborted = true;
590b89a7cc2SEnji Cooper         ASSERT_DEATH(DieIf(false), "DieIf");  // This assertion should fail.
591b89a7cc2SEnji Cooper         aborted = false;
59228f6c2f2SEnji Cooper       },
59328f6c2f2SEnji Cooper       "failed to die");
594b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
595b89a7cc2SEnji Cooper }
596b89a7cc2SEnji Cooper 
597b89a7cc2SEnji Cooper // Tests that EXPECT_DEATH evaluates the arguments exactly once.
TEST_F(TestForDeathTest,SingleEvaluation)598b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, SingleEvaluation) {
599b89a7cc2SEnji Cooper   int x = 3;
600b89a7cc2SEnji Cooper   EXPECT_DEATH(DieIf((++x) == 4), "DieIf");
601b89a7cc2SEnji Cooper 
602b89a7cc2SEnji Cooper   const char* regex = "DieIf";
603b89a7cc2SEnji Cooper   const char* regex_save = regex;
604b89a7cc2SEnji Cooper   EXPECT_DEATH(DieIfLessThan(3, 4), regex++);
605b89a7cc2SEnji Cooper   EXPECT_EQ(regex_save + 1, regex);
606b89a7cc2SEnji Cooper }
607b89a7cc2SEnji Cooper 
608b89a7cc2SEnji Cooper // Tests that run-away death tests are reported as failures.
TEST_F(TestForDeathTest,RunawayIsFailure)609b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, RunawayIsFailure) {
610b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(static_cast<void>(0), "Foo"),
611b89a7cc2SEnji Cooper                           "failed to die.");
612b89a7cc2SEnji Cooper }
613b89a7cc2SEnji Cooper 
614b89a7cc2SEnji Cooper // Tests that death tests report executing 'return' in the statement as
615b89a7cc2SEnji Cooper // failure.
TEST_F(TestForDeathTest,ReturnIsFailure)616b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ReturnIsFailure) {
617b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(ASSERT_DEATH(return, "Bar"),
618b89a7cc2SEnji Cooper                        "illegal return in test statement.");
619b89a7cc2SEnji Cooper }
620b89a7cc2SEnji Cooper 
621b89a7cc2SEnji Cooper // Tests that EXPECT_DEBUG_DEATH works as expected, that is, you can stream a
622b89a7cc2SEnji Cooper // message to it, and in debug mode it:
623b89a7cc2SEnji Cooper // 1. Asserts on death.
624b89a7cc2SEnji Cooper // 2. Has no side effect.
625b89a7cc2SEnji Cooper //
626b89a7cc2SEnji Cooper // And in opt mode, it:
627b89a7cc2SEnji Cooper // 1.  Has side effects but does not assert.
TEST_F(TestForDeathTest,TestExpectDebugDeath)628b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, TestExpectDebugDeath) {
629b89a7cc2SEnji Cooper   int sideeffect = 0;
630b89a7cc2SEnji Cooper 
631b89a7cc2SEnji Cooper   // Put the regex in a local variable to make sure we don't get an "unused"
632b89a7cc2SEnji Cooper   // warning in opt mode.
633b89a7cc2SEnji Cooper   const char* regex = "death.*DieInDebugElse12";
634b89a7cc2SEnji Cooper 
635b89a7cc2SEnji Cooper   EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), regex)
636b89a7cc2SEnji Cooper       << "Must accept a streamed message";
637b89a7cc2SEnji Cooper 
638b89a7cc2SEnji Cooper #ifdef NDEBUG
639b89a7cc2SEnji Cooper 
640b89a7cc2SEnji Cooper   // Checks that the assignment occurs in opt mode (sideeffect).
641b89a7cc2SEnji Cooper   EXPECT_EQ(12, sideeffect);
642b89a7cc2SEnji Cooper 
643b89a7cc2SEnji Cooper #else
644b89a7cc2SEnji Cooper 
645b89a7cc2SEnji Cooper   // Checks that the assignment does not occur in dbg mode (no sideeffect).
646b89a7cc2SEnji Cooper   EXPECT_EQ(0, sideeffect);
647b89a7cc2SEnji Cooper 
648b89a7cc2SEnji Cooper #endif
649b89a7cc2SEnji Cooper }
650b89a7cc2SEnji Cooper 
65128f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
65228f6c2f2SEnji Cooper 
65328f6c2f2SEnji Cooper // https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/crtsetreportmode
65428f6c2f2SEnji Cooper // In debug mode, the calls to _CrtSetReportMode and _CrtSetReportFile enable
65528f6c2f2SEnji Cooper // the dumping of assertions to stderr. Tests that EXPECT_DEATH works as
65628f6c2f2SEnji Cooper // expected when in CRT debug mode (compiled with /MTd or /MDd, which defines
65728f6c2f2SEnji Cooper // _DEBUG) the Windows CRT crashes the process with an assertion failure.
65828f6c2f2SEnji Cooper // 1. Asserts on death.
65928f6c2f2SEnji Cooper // 2. Has no side effect (doesn't pop up a window or wait for user input).
66028f6c2f2SEnji Cooper #ifdef _DEBUG
TEST_F(TestForDeathTest,CRTDebugDeath)66128f6c2f2SEnji Cooper TEST_F(TestForDeathTest, CRTDebugDeath) {
66228f6c2f2SEnji Cooper   EXPECT_DEATH(DieInCRTDebugElse12(nullptr), "dup.* : Assertion failed")
66328f6c2f2SEnji Cooper       << "Must accept a streamed message";
66428f6c2f2SEnji Cooper }
66528f6c2f2SEnji Cooper #endif  // _DEBUG
66628f6c2f2SEnji Cooper 
66728f6c2f2SEnji Cooper #endif  // GTEST_OS_WINDOWS
66828f6c2f2SEnji Cooper 
669b89a7cc2SEnji Cooper // Tests that ASSERT_DEBUG_DEATH works as expected, that is, you can stream a
670b89a7cc2SEnji Cooper // message to it, and in debug mode it:
671b89a7cc2SEnji Cooper // 1. Asserts on death.
672b89a7cc2SEnji Cooper // 2. Has no side effect.
673b89a7cc2SEnji Cooper //
674b89a7cc2SEnji Cooper // And in opt mode, it:
675b89a7cc2SEnji Cooper // 1.  Has side effects but does not assert.
TEST_F(TestForDeathTest,TestAssertDebugDeath)676b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, TestAssertDebugDeath) {
677b89a7cc2SEnji Cooper   int sideeffect = 0;
678b89a7cc2SEnji Cooper 
679b89a7cc2SEnji Cooper   ASSERT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), "death.*DieInDebugElse12")
680b89a7cc2SEnji Cooper       << "Must accept a streamed message";
681b89a7cc2SEnji Cooper 
682b89a7cc2SEnji Cooper #ifdef NDEBUG
683b89a7cc2SEnji Cooper 
684b89a7cc2SEnji Cooper   // Checks that the assignment occurs in opt mode (sideeffect).
685b89a7cc2SEnji Cooper   EXPECT_EQ(12, sideeffect);
686b89a7cc2SEnji Cooper 
687b89a7cc2SEnji Cooper #else
688b89a7cc2SEnji Cooper 
689b89a7cc2SEnji Cooper   // Checks that the assignment does not occur in dbg mode (no sideeffect).
690b89a7cc2SEnji Cooper   EXPECT_EQ(0, sideeffect);
691b89a7cc2SEnji Cooper 
692b89a7cc2SEnji Cooper #endif
693b89a7cc2SEnji Cooper }
694b89a7cc2SEnji Cooper 
695b89a7cc2SEnji Cooper #ifndef NDEBUG
696b89a7cc2SEnji Cooper 
ExpectDebugDeathHelper(bool * aborted)697b89a7cc2SEnji Cooper void ExpectDebugDeathHelper(bool* aborted) {
698b89a7cc2SEnji Cooper   *aborted = true;
699b89a7cc2SEnji Cooper   EXPECT_DEBUG_DEATH(return, "") << "This is expected to fail.";
700b89a7cc2SEnji Cooper   *aborted = false;
701b89a7cc2SEnji Cooper }
702b89a7cc2SEnji Cooper 
70328f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
TEST(PopUpDeathTest,DoesNotShowPopUpOnAbort)704b89a7cc2SEnji Cooper TEST(PopUpDeathTest, DoesNotShowPopUpOnAbort) {
70528f6c2f2SEnji Cooper   printf(
70628f6c2f2SEnji Cooper       "This test should be considered failing if it shows "
707b89a7cc2SEnji Cooper       "any pop-up dialogs.\n");
708b89a7cc2SEnji Cooper   fflush(stdout);
709b89a7cc2SEnji Cooper 
71028f6c2f2SEnji Cooper   EXPECT_DEATH(
71128f6c2f2SEnji Cooper       {
71228f6c2f2SEnji Cooper         GTEST_FLAG_SET(catch_exceptions, false);
713b89a7cc2SEnji Cooper         abort();
71428f6c2f2SEnji Cooper       },
71528f6c2f2SEnji Cooper       "");
716b89a7cc2SEnji Cooper }
717b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS
718b89a7cc2SEnji Cooper 
719b89a7cc2SEnji Cooper // Tests that EXPECT_DEBUG_DEATH in debug mode does not abort
720b89a7cc2SEnji Cooper // the function.
TEST_F(TestForDeathTest,ExpectDebugDeathDoesNotAbort)721b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ExpectDebugDeathDoesNotAbort) {
722b89a7cc2SEnji Cooper   bool aborted = true;
723b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(ExpectDebugDeathHelper(&aborted), "");
724b89a7cc2SEnji Cooper   EXPECT_FALSE(aborted);
725b89a7cc2SEnji Cooper }
726b89a7cc2SEnji Cooper 
AssertDebugDeathHelper(bool * aborted)727b89a7cc2SEnji Cooper void AssertDebugDeathHelper(bool* aborted) {
728b89a7cc2SEnji Cooper   *aborted = true;
729b89a7cc2SEnji Cooper   GTEST_LOG_(INFO) << "Before ASSERT_DEBUG_DEATH";
730b89a7cc2SEnji Cooper   ASSERT_DEBUG_DEATH(GTEST_LOG_(INFO) << "In ASSERT_DEBUG_DEATH"; return, "")
731b89a7cc2SEnji Cooper       << "This is expected to fail.";
732b89a7cc2SEnji Cooper   GTEST_LOG_(INFO) << "After ASSERT_DEBUG_DEATH";
733b89a7cc2SEnji Cooper   *aborted = false;
734b89a7cc2SEnji Cooper }
735b89a7cc2SEnji Cooper 
736b89a7cc2SEnji Cooper // Tests that ASSERT_DEBUG_DEATH in debug mode aborts the function on
737b89a7cc2SEnji Cooper // failure.
TEST_F(TestForDeathTest,AssertDebugDeathAborts)738b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts) {
739b89a7cc2SEnji Cooper   static bool aborted;
740b89a7cc2SEnji Cooper   aborted = false;
741b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
742b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
743b89a7cc2SEnji Cooper }
744b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,AssertDebugDeathAborts2)745b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts2) {
746b89a7cc2SEnji Cooper   static bool aborted;
747b89a7cc2SEnji Cooper   aborted = false;
748b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
749b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
750b89a7cc2SEnji Cooper }
751b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,AssertDebugDeathAborts3)752b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts3) {
753b89a7cc2SEnji Cooper   static bool aborted;
754b89a7cc2SEnji Cooper   aborted = false;
755b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
756b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
757b89a7cc2SEnji Cooper }
758b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,AssertDebugDeathAborts4)759b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts4) {
760b89a7cc2SEnji Cooper   static bool aborted;
761b89a7cc2SEnji Cooper   aborted = false;
762b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
763b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
764b89a7cc2SEnji Cooper }
765b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,AssertDebugDeathAborts5)766b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts5) {
767b89a7cc2SEnji Cooper   static bool aborted;
768b89a7cc2SEnji Cooper   aborted = false;
769b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
770b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
771b89a7cc2SEnji Cooper }
772b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,AssertDebugDeathAborts6)773b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts6) {
774b89a7cc2SEnji Cooper   static bool aborted;
775b89a7cc2SEnji Cooper   aborted = false;
776b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
777b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
778b89a7cc2SEnji Cooper }
779b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,AssertDebugDeathAborts7)780b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts7) {
781b89a7cc2SEnji Cooper   static bool aborted;
782b89a7cc2SEnji Cooper   aborted = false;
783b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
784b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
785b89a7cc2SEnji Cooper }
786b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,AssertDebugDeathAborts8)787b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts8) {
788b89a7cc2SEnji Cooper   static bool aborted;
789b89a7cc2SEnji Cooper   aborted = false;
790b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
791b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
792b89a7cc2SEnji Cooper }
793b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,AssertDebugDeathAborts9)794b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts9) {
795b89a7cc2SEnji Cooper   static bool aborted;
796b89a7cc2SEnji Cooper   aborted = false;
797b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
798b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
799b89a7cc2SEnji Cooper }
800b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,AssertDebugDeathAborts10)801b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts10) {
802b89a7cc2SEnji Cooper   static bool aborted;
803b89a7cc2SEnji Cooper   aborted = false;
804b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
805b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
806b89a7cc2SEnji Cooper }
807b89a7cc2SEnji Cooper 
808b89a7cc2SEnji Cooper #endif  // _NDEBUG
809b89a7cc2SEnji Cooper 
810b89a7cc2SEnji Cooper // Tests the *_EXIT family of macros, using a variety of predicates.
TestExitMacros()811b89a7cc2SEnji Cooper static void TestExitMacros() {
812*5ca8c28cSEnji Cooper   EXPECT_EXIT(_Exit(1), testing::ExitedWithCode(1), "");
813*5ca8c28cSEnji Cooper   ASSERT_EXIT(_Exit(42), testing::ExitedWithCode(42), "");
814b89a7cc2SEnji Cooper 
81528f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
816b89a7cc2SEnji Cooper 
817b89a7cc2SEnji Cooper   // Of all signals effects on the process exit code, only those of SIGABRT
818b89a7cc2SEnji Cooper   // are documented on Windows.
819b89a7cc2SEnji Cooper   // See https://msdn.microsoft.com/en-us/query-bi/m/dwwzkt4c.
820b89a7cc2SEnji Cooper   EXPECT_EXIT(raise(SIGABRT), testing::ExitedWithCode(3), "") << "b_ar";
821b89a7cc2SEnji Cooper 
82228f6c2f2SEnji Cooper #elif !defined(GTEST_OS_FUCHSIA)
823b89a7cc2SEnji Cooper 
824b89a7cc2SEnji Cooper   // Fuchsia has no unix signals.
825b89a7cc2SEnji Cooper   EXPECT_EXIT(raise(SIGKILL), testing::KilledBySignal(SIGKILL), "") << "foo";
826b89a7cc2SEnji Cooper   ASSERT_EXIT(raise(SIGUSR2), testing::KilledBySignal(SIGUSR2), "") << "bar";
827b89a7cc2SEnji Cooper 
82828f6c2f2SEnji Cooper   EXPECT_FATAL_FAILURE(
82928f6c2f2SEnji Cooper       {  // NOLINT
830*5ca8c28cSEnji Cooper         ASSERT_EXIT(_Exit(0), testing::KilledBySignal(SIGSEGV), "")
831b89a7cc2SEnji Cooper             << "This failure is expected, too.";
83228f6c2f2SEnji Cooper       },
83328f6c2f2SEnji Cooper       "This failure is expected, too.");
834b89a7cc2SEnji Cooper 
835b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS
836b89a7cc2SEnji Cooper 
83728f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
83828f6c2f2SEnji Cooper       {  // NOLINT
839b89a7cc2SEnji Cooper         EXPECT_EXIT(raise(SIGSEGV), testing::ExitedWithCode(0), "")
840b89a7cc2SEnji Cooper             << "This failure is expected.";
84128f6c2f2SEnji Cooper       },
84228f6c2f2SEnji Cooper       "This failure is expected.");
843b89a7cc2SEnji Cooper }
844b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,ExitMacros)84528f6c2f2SEnji Cooper TEST_F(TestForDeathTest, ExitMacros) { TestExitMacros(); }
846b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,ExitMacrosUsingFork)847b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ExitMacrosUsingFork) {
84828f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_use_fork, true);
849b89a7cc2SEnji Cooper   TestExitMacros();
850b89a7cc2SEnji Cooper }
851b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,InvalidStyle)852b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, InvalidStyle) {
85328f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "rococo");
85428f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
85528f6c2f2SEnji Cooper       {  // NOLINT
856*5ca8c28cSEnji Cooper         EXPECT_DEATH(_Exit(0), "") << "This failure is expected.";
85728f6c2f2SEnji Cooper       },
85828f6c2f2SEnji Cooper       "This failure is expected.");
859b89a7cc2SEnji Cooper }
860b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,DeathTestFailedOutput)861b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DeathTestFailedOutput) {
86228f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "fast");
863b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
86428f6c2f2SEnji Cooper       EXPECT_DEATH(DieWithMessage("death\n"), "expected message"),
865b89a7cc2SEnji Cooper       "Actual msg:\n"
866b89a7cc2SEnji Cooper       "[  DEATH   ] death\n");
867b89a7cc2SEnji Cooper }
868b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,DeathTestUnexpectedReturnOutput)869b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DeathTestUnexpectedReturnOutput) {
87028f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "fast");
87128f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(
87228f6c2f2SEnji Cooper                               {
873b89a7cc2SEnji Cooper                                 fprintf(stderr, "returning\n");
874b89a7cc2SEnji Cooper                                 fflush(stderr);
875b89a7cc2SEnji Cooper                                 return;
87628f6c2f2SEnji Cooper                               },
87728f6c2f2SEnji Cooper                               ""),
878b89a7cc2SEnji Cooper                           "    Result: illegal return in test statement.\n"
879b89a7cc2SEnji Cooper                           " Error msg:\n"
880b89a7cc2SEnji Cooper                           "[  DEATH   ] returning\n");
881b89a7cc2SEnji Cooper }
882b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,DeathTestBadExitCodeOutput)883b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DeathTestBadExitCodeOutput) {
88428f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "fast");
885b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
886b89a7cc2SEnji Cooper       EXPECT_EXIT(DieWithMessage("exiting with rc 1\n"),
88728f6c2f2SEnji Cooper                   testing::ExitedWithCode(3), "expected message"),
888b89a7cc2SEnji Cooper       "    Result: died but not with expected exit code:\n"
889b89a7cc2SEnji Cooper       "            Exited with exit status 1\n"
890b89a7cc2SEnji Cooper       "Actual msg:\n"
891b89a7cc2SEnji Cooper       "[  DEATH   ] exiting with rc 1\n");
892b89a7cc2SEnji Cooper }
893b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,DeathTestMultiLineMatchFail)894b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DeathTestMultiLineMatchFail) {
89528f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "fast");
896b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
897b89a7cc2SEnji Cooper       EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"),
898b89a7cc2SEnji Cooper                    "line 1\nxyz\nline 3\n"),
899b89a7cc2SEnji Cooper       "Actual msg:\n"
900b89a7cc2SEnji Cooper       "[  DEATH   ] line 1\n"
901b89a7cc2SEnji Cooper       "[  DEATH   ] line 2\n"
902b89a7cc2SEnji Cooper       "[  DEATH   ] line 3\n");
903b89a7cc2SEnji Cooper }
904b89a7cc2SEnji Cooper 
TEST_F(TestForDeathTest,DeathTestMultiLineMatchPass)905b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DeathTestMultiLineMatchPass) {
90628f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "fast");
907b89a7cc2SEnji Cooper   EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"),
908b89a7cc2SEnji Cooper                "line 1\nline 2\nline 3\n");
909b89a7cc2SEnji Cooper }
910b89a7cc2SEnji Cooper 
911b89a7cc2SEnji Cooper // A DeathTestFactory that returns MockDeathTests.
912b89a7cc2SEnji Cooper class MockDeathTestFactory : public DeathTestFactory {
913b89a7cc2SEnji Cooper  public:
914b89a7cc2SEnji Cooper   MockDeathTestFactory();
91528f6c2f2SEnji Cooper   bool Create(const char* statement,
91628f6c2f2SEnji Cooper               testing::Matcher<const std::string&> matcher, const char* file,
91728f6c2f2SEnji Cooper               int line, DeathTest** test) override;
918b89a7cc2SEnji Cooper 
919b89a7cc2SEnji Cooper   // Sets the parameters for subsequent calls to Create.
92028f6c2f2SEnji Cooper   void SetParameters(bool create, DeathTest::TestRole role, int status,
92128f6c2f2SEnji Cooper                      bool passed);
922b89a7cc2SEnji Cooper 
923b89a7cc2SEnji Cooper   // Accessors.
AssumeRoleCalls() const924b89a7cc2SEnji Cooper   int AssumeRoleCalls() const { return assume_role_calls_; }
WaitCalls() const925b89a7cc2SEnji Cooper   int WaitCalls() const { return wait_calls_; }
PassedCalls() const926b89a7cc2SEnji Cooper   size_t PassedCalls() const { return passed_args_.size(); }
PassedArgument(int n) const92728f6c2f2SEnji Cooper   bool PassedArgument(int n) const {
92828f6c2f2SEnji Cooper     return passed_args_[static_cast<size_t>(n)];
92928f6c2f2SEnji Cooper   }
AbortCalls() const930b89a7cc2SEnji Cooper   size_t AbortCalls() const { return abort_args_.size(); }
AbortArgument(int n) const931b89a7cc2SEnji Cooper   DeathTest::AbortReason AbortArgument(int n) const {
93228f6c2f2SEnji Cooper     return abort_args_[static_cast<size_t>(n)];
933b89a7cc2SEnji Cooper   }
TestDeleted() const934b89a7cc2SEnji Cooper   bool TestDeleted() const { return test_deleted_; }
935b89a7cc2SEnji Cooper 
936b89a7cc2SEnji Cooper  private:
937b89a7cc2SEnji Cooper   friend class MockDeathTest;
938b89a7cc2SEnji Cooper   // If true, Create will return a MockDeathTest; otherwise it returns
939b89a7cc2SEnji Cooper   // NULL.
940b89a7cc2SEnji Cooper   bool create_;
941b89a7cc2SEnji Cooper   // The value a MockDeathTest will return from its AssumeRole method.
942b89a7cc2SEnji Cooper   DeathTest::TestRole role_;
943b89a7cc2SEnji Cooper   // The value a MockDeathTest will return from its Wait method.
944b89a7cc2SEnji Cooper   int status_;
945b89a7cc2SEnji Cooper   // The value a MockDeathTest will return from its Passed method.
946b89a7cc2SEnji Cooper   bool passed_;
947b89a7cc2SEnji Cooper 
948b89a7cc2SEnji Cooper   // Number of times AssumeRole was called.
949b89a7cc2SEnji Cooper   int assume_role_calls_;
950b89a7cc2SEnji Cooper   // Number of times Wait was called.
951b89a7cc2SEnji Cooper   int wait_calls_;
952b89a7cc2SEnji Cooper   // The arguments to the calls to Passed since the last call to
953b89a7cc2SEnji Cooper   // SetParameters.
954b89a7cc2SEnji Cooper   std::vector<bool> passed_args_;
955b89a7cc2SEnji Cooper   // The arguments to the calls to Abort since the last call to
956b89a7cc2SEnji Cooper   // SetParameters.
957b89a7cc2SEnji Cooper   std::vector<DeathTest::AbortReason> abort_args_;
958b89a7cc2SEnji Cooper   // True if the last MockDeathTest returned by Create has been
959b89a7cc2SEnji Cooper   // deleted.
960b89a7cc2SEnji Cooper   bool test_deleted_;
961b89a7cc2SEnji Cooper };
962b89a7cc2SEnji Cooper 
963b89a7cc2SEnji Cooper // A DeathTest implementation useful in testing.  It returns values set
964b89a7cc2SEnji Cooper // at its creation from its various inherited DeathTest methods, and
965b89a7cc2SEnji Cooper // reports calls to those methods to its parent MockDeathTestFactory
966b89a7cc2SEnji Cooper // object.
967b89a7cc2SEnji Cooper class MockDeathTest : public DeathTest {
968b89a7cc2SEnji Cooper  public:
MockDeathTest(MockDeathTestFactory * parent,TestRole role,int status,bool passed)96928f6c2f2SEnji Cooper   MockDeathTest(MockDeathTestFactory* parent, TestRole role, int status,
97028f6c2f2SEnji Cooper                 bool passed)
97128f6c2f2SEnji Cooper       : parent_(parent), role_(role), status_(status), passed_(passed) {}
~MockDeathTest()97228f6c2f2SEnji Cooper   ~MockDeathTest() override { parent_->test_deleted_ = true; }
AssumeRole()97328f6c2f2SEnji Cooper   TestRole AssumeRole() override {
974b89a7cc2SEnji Cooper     ++parent_->assume_role_calls_;
975b89a7cc2SEnji Cooper     return role_;
976b89a7cc2SEnji Cooper   }
Wait()97728f6c2f2SEnji Cooper   int Wait() override {
978b89a7cc2SEnji Cooper     ++parent_->wait_calls_;
979b89a7cc2SEnji Cooper     return status_;
980b89a7cc2SEnji Cooper   }
Passed(bool exit_status_ok)98128f6c2f2SEnji Cooper   bool Passed(bool exit_status_ok) override {
982b89a7cc2SEnji Cooper     parent_->passed_args_.push_back(exit_status_ok);
983b89a7cc2SEnji Cooper     return passed_;
984b89a7cc2SEnji Cooper   }
Abort(AbortReason reason)98528f6c2f2SEnji Cooper   void Abort(AbortReason reason) override {
986b89a7cc2SEnji Cooper     parent_->abort_args_.push_back(reason);
987b89a7cc2SEnji Cooper   }
988b89a7cc2SEnji Cooper 
989b89a7cc2SEnji Cooper  private:
990b89a7cc2SEnji Cooper   MockDeathTestFactory* const parent_;
991b89a7cc2SEnji Cooper   const TestRole role_;
992b89a7cc2SEnji Cooper   const int status_;
993b89a7cc2SEnji Cooper   const bool passed_;
994b89a7cc2SEnji Cooper };
995b89a7cc2SEnji Cooper 
996b89a7cc2SEnji Cooper // MockDeathTestFactory constructor.
MockDeathTestFactory()997b89a7cc2SEnji Cooper MockDeathTestFactory::MockDeathTestFactory()
998b89a7cc2SEnji Cooper     : create_(true),
999b89a7cc2SEnji Cooper       role_(DeathTest::OVERSEE_TEST),
1000b89a7cc2SEnji Cooper       status_(0),
1001b89a7cc2SEnji Cooper       passed_(true),
1002b89a7cc2SEnji Cooper       assume_role_calls_(0),
1003b89a7cc2SEnji Cooper       wait_calls_(0),
1004b89a7cc2SEnji Cooper       passed_args_(),
100528f6c2f2SEnji Cooper       abort_args_() {}
1006b89a7cc2SEnji Cooper 
1007b89a7cc2SEnji Cooper // Sets the parameters for subsequent calls to Create.
SetParameters(bool create,DeathTest::TestRole role,int status,bool passed)100828f6c2f2SEnji Cooper void MockDeathTestFactory::SetParameters(bool create, DeathTest::TestRole role,
1009b89a7cc2SEnji Cooper                                          int status, bool passed) {
1010b89a7cc2SEnji Cooper   create_ = create;
1011b89a7cc2SEnji Cooper   role_ = role;
1012b89a7cc2SEnji Cooper   status_ = status;
1013b89a7cc2SEnji Cooper   passed_ = passed;
1014b89a7cc2SEnji Cooper 
1015b89a7cc2SEnji Cooper   assume_role_calls_ = 0;
1016b89a7cc2SEnji Cooper   wait_calls_ = 0;
1017b89a7cc2SEnji Cooper   passed_args_.clear();
1018b89a7cc2SEnji Cooper   abort_args_.clear();
1019b89a7cc2SEnji Cooper }
1020b89a7cc2SEnji Cooper 
1021b89a7cc2SEnji Cooper // Sets test to NULL (if create_ is false) or to the address of a new
1022b89a7cc2SEnji Cooper // MockDeathTest object with parameters taken from the last call
1023b89a7cc2SEnji Cooper // to SetParameters (if create_ is true).  Always returns true.
Create(const char *,testing::Matcher<const std::string &>,const char *,int,DeathTest ** test)102428f6c2f2SEnji Cooper bool MockDeathTestFactory::Create(
102528f6c2f2SEnji Cooper     const char* /*statement*/, testing::Matcher<const std::string&> /*matcher*/,
102628f6c2f2SEnji Cooper     const char* /*file*/, int /*line*/, DeathTest** test) {
1027b89a7cc2SEnji Cooper   test_deleted_ = false;
1028b89a7cc2SEnji Cooper   if (create_) {
1029b89a7cc2SEnji Cooper     *test = new MockDeathTest(this, role_, status_, passed_);
1030b89a7cc2SEnji Cooper   } else {
103128f6c2f2SEnji Cooper     *test = nullptr;
1032b89a7cc2SEnji Cooper   }
1033b89a7cc2SEnji Cooper   return true;
1034b89a7cc2SEnji Cooper }
1035b89a7cc2SEnji Cooper 
1036b89a7cc2SEnji Cooper // A test fixture for testing the logic of the GTEST_DEATH_TEST_ macro.
1037b89a7cc2SEnji Cooper // It installs a MockDeathTestFactory that is used for the duration
1038b89a7cc2SEnji Cooper // of the test case.
1039b89a7cc2SEnji Cooper class MacroLogicDeathTest : public testing::Test {
1040b89a7cc2SEnji Cooper  protected:
1041b89a7cc2SEnji Cooper   static testing::internal::ReplaceDeathTestFactory* replacer_;
1042b89a7cc2SEnji Cooper   static MockDeathTestFactory* factory_;
1043b89a7cc2SEnji Cooper 
SetUpTestSuite()104428f6c2f2SEnji Cooper   static void SetUpTestSuite() {
1045b89a7cc2SEnji Cooper     factory_ = new MockDeathTestFactory;
1046b89a7cc2SEnji Cooper     replacer_ = new testing::internal::ReplaceDeathTestFactory(factory_);
1047b89a7cc2SEnji Cooper   }
1048b89a7cc2SEnji Cooper 
TearDownTestSuite()104928f6c2f2SEnji Cooper   static void TearDownTestSuite() {
1050b89a7cc2SEnji Cooper     delete replacer_;
105128f6c2f2SEnji Cooper     replacer_ = nullptr;
1052b89a7cc2SEnji Cooper     delete factory_;
105328f6c2f2SEnji Cooper     factory_ = nullptr;
1054b89a7cc2SEnji Cooper   }
1055b89a7cc2SEnji Cooper 
1056b89a7cc2SEnji Cooper   // Runs a death test that breaks the rules by returning.  Such a death
1057b89a7cc2SEnji Cooper   // test cannot be run directly from a test routine that uses a
1058b89a7cc2SEnji Cooper   // MockDeathTest, or the remainder of the routine will not be executed.
RunReturningDeathTest(bool * flag)1059b89a7cc2SEnji Cooper   static void RunReturningDeathTest(bool* flag) {
106028f6c2f2SEnji Cooper     ASSERT_DEATH(
106128f6c2f2SEnji Cooper         {  // NOLINT
1062b89a7cc2SEnji Cooper           *flag = true;
1063b89a7cc2SEnji Cooper           return;
106428f6c2f2SEnji Cooper         },
106528f6c2f2SEnji Cooper         "");
1066b89a7cc2SEnji Cooper   }
1067b89a7cc2SEnji Cooper };
1068b89a7cc2SEnji Cooper 
106928f6c2f2SEnji Cooper testing::internal::ReplaceDeathTestFactory* MacroLogicDeathTest::replacer_ =
107028f6c2f2SEnji Cooper     nullptr;
107128f6c2f2SEnji Cooper MockDeathTestFactory* MacroLogicDeathTest::factory_ = nullptr;
1072b89a7cc2SEnji Cooper 
1073b89a7cc2SEnji Cooper // Test that nothing happens when the factory doesn't return a DeathTest:
TEST_F(MacroLogicDeathTest,NothingHappens)1074b89a7cc2SEnji Cooper TEST_F(MacroLogicDeathTest, NothingHappens) {
1075b89a7cc2SEnji Cooper   bool flag = false;
1076b89a7cc2SEnji Cooper   factory_->SetParameters(false, DeathTest::OVERSEE_TEST, 0, true);
1077b89a7cc2SEnji Cooper   EXPECT_DEATH(flag = true, "");
1078b89a7cc2SEnji Cooper   EXPECT_FALSE(flag);
1079b89a7cc2SEnji Cooper   EXPECT_EQ(0, factory_->AssumeRoleCalls());
1080b89a7cc2SEnji Cooper   EXPECT_EQ(0, factory_->WaitCalls());
1081b89a7cc2SEnji Cooper   EXPECT_EQ(0U, factory_->PassedCalls());
1082b89a7cc2SEnji Cooper   EXPECT_EQ(0U, factory_->AbortCalls());
1083b89a7cc2SEnji Cooper   EXPECT_FALSE(factory_->TestDeleted());
1084b89a7cc2SEnji Cooper }
1085b89a7cc2SEnji Cooper 
1086b89a7cc2SEnji Cooper // Test that the parent process doesn't run the death test code,
1087b89a7cc2SEnji Cooper // and that the Passed method returns false when the (simulated)
1088b89a7cc2SEnji Cooper // child process exits with status 0:
TEST_F(MacroLogicDeathTest,ChildExitsSuccessfully)1089b89a7cc2SEnji Cooper TEST_F(MacroLogicDeathTest, ChildExitsSuccessfully) {
1090b89a7cc2SEnji Cooper   bool flag = false;
1091b89a7cc2SEnji Cooper   factory_->SetParameters(true, DeathTest::OVERSEE_TEST, 0, true);
1092b89a7cc2SEnji Cooper   EXPECT_DEATH(flag = true, "");
1093b89a7cc2SEnji Cooper   EXPECT_FALSE(flag);
1094b89a7cc2SEnji Cooper   EXPECT_EQ(1, factory_->AssumeRoleCalls());
1095b89a7cc2SEnji Cooper   EXPECT_EQ(1, factory_->WaitCalls());
1096b89a7cc2SEnji Cooper   ASSERT_EQ(1U, factory_->PassedCalls());
1097b89a7cc2SEnji Cooper   EXPECT_FALSE(factory_->PassedArgument(0));
1098b89a7cc2SEnji Cooper   EXPECT_EQ(0U, factory_->AbortCalls());
1099b89a7cc2SEnji Cooper   EXPECT_TRUE(factory_->TestDeleted());
1100b89a7cc2SEnji Cooper }
1101b89a7cc2SEnji Cooper 
1102b89a7cc2SEnji Cooper // Tests that the Passed method was given the argument "true" when
1103b89a7cc2SEnji Cooper // the (simulated) child process exits with status 1:
TEST_F(MacroLogicDeathTest,ChildExitsUnsuccessfully)1104b89a7cc2SEnji Cooper TEST_F(MacroLogicDeathTest, ChildExitsUnsuccessfully) {
1105b89a7cc2SEnji Cooper   bool flag = false;
1106b89a7cc2SEnji Cooper   factory_->SetParameters(true, DeathTest::OVERSEE_TEST, 1, true);
1107b89a7cc2SEnji Cooper   EXPECT_DEATH(flag = true, "");
1108b89a7cc2SEnji Cooper   EXPECT_FALSE(flag);
1109b89a7cc2SEnji Cooper   EXPECT_EQ(1, factory_->AssumeRoleCalls());
1110b89a7cc2SEnji Cooper   EXPECT_EQ(1, factory_->WaitCalls());
1111b89a7cc2SEnji Cooper   ASSERT_EQ(1U, factory_->PassedCalls());
1112b89a7cc2SEnji Cooper   EXPECT_TRUE(factory_->PassedArgument(0));
1113b89a7cc2SEnji Cooper   EXPECT_EQ(0U, factory_->AbortCalls());
1114b89a7cc2SEnji Cooper   EXPECT_TRUE(factory_->TestDeleted());
1115b89a7cc2SEnji Cooper }
1116b89a7cc2SEnji Cooper 
1117b89a7cc2SEnji Cooper // Tests that the (simulated) child process executes the death test
1118b89a7cc2SEnji Cooper // code, and is aborted with the correct AbortReason if it
1119b89a7cc2SEnji Cooper // executes a return statement.
TEST_F(MacroLogicDeathTest,ChildPerformsReturn)1120b89a7cc2SEnji Cooper TEST_F(MacroLogicDeathTest, ChildPerformsReturn) {
1121b89a7cc2SEnji Cooper   bool flag = false;
1122b89a7cc2SEnji Cooper   factory_->SetParameters(true, DeathTest::EXECUTE_TEST, 0, true);
1123b89a7cc2SEnji Cooper   RunReturningDeathTest(&flag);
1124b89a7cc2SEnji Cooper   EXPECT_TRUE(flag);
1125b89a7cc2SEnji Cooper   EXPECT_EQ(1, factory_->AssumeRoleCalls());
1126b89a7cc2SEnji Cooper   EXPECT_EQ(0, factory_->WaitCalls());
1127b89a7cc2SEnji Cooper   EXPECT_EQ(0U, factory_->PassedCalls());
1128b89a7cc2SEnji Cooper   EXPECT_EQ(1U, factory_->AbortCalls());
1129b89a7cc2SEnji Cooper   EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT,
1130b89a7cc2SEnji Cooper             factory_->AbortArgument(0));
1131b89a7cc2SEnji Cooper   EXPECT_TRUE(factory_->TestDeleted());
1132b89a7cc2SEnji Cooper }
1133b89a7cc2SEnji Cooper 
1134b89a7cc2SEnji Cooper // Tests that the (simulated) child process is aborted with the
1135b89a7cc2SEnji Cooper // correct AbortReason if it does not die.
TEST_F(MacroLogicDeathTest,ChildDoesNotDie)1136b89a7cc2SEnji Cooper TEST_F(MacroLogicDeathTest, ChildDoesNotDie) {
1137b89a7cc2SEnji Cooper   bool flag = false;
1138b89a7cc2SEnji Cooper   factory_->SetParameters(true, DeathTest::EXECUTE_TEST, 0, true);
1139b89a7cc2SEnji Cooper   EXPECT_DEATH(flag = true, "");
1140b89a7cc2SEnji Cooper   EXPECT_TRUE(flag);
1141b89a7cc2SEnji Cooper   EXPECT_EQ(1, factory_->AssumeRoleCalls());
1142b89a7cc2SEnji Cooper   EXPECT_EQ(0, factory_->WaitCalls());
1143b89a7cc2SEnji Cooper   EXPECT_EQ(0U, factory_->PassedCalls());
1144b89a7cc2SEnji Cooper   // This time there are two calls to Abort: one since the test didn't
1145b89a7cc2SEnji Cooper   // die, and another from the ReturnSentinel when it's destroyed.  The
1146b89a7cc2SEnji Cooper   // sentinel normally isn't destroyed if a test doesn't die, since
1147*5ca8c28cSEnji Cooper   // _Exit(2) is called in that case by ForkingDeathTest, but not by
1148b89a7cc2SEnji Cooper   // our MockDeathTest.
1149b89a7cc2SEnji Cooper   ASSERT_EQ(2U, factory_->AbortCalls());
115028f6c2f2SEnji Cooper   EXPECT_EQ(DeathTest::TEST_DID_NOT_DIE, factory_->AbortArgument(0));
1151b89a7cc2SEnji Cooper   EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT,
1152b89a7cc2SEnji Cooper             factory_->AbortArgument(1));
1153b89a7cc2SEnji Cooper   EXPECT_TRUE(factory_->TestDeleted());
1154b89a7cc2SEnji Cooper }
1155b89a7cc2SEnji Cooper 
1156b89a7cc2SEnji Cooper // Tests that a successful death test does not register a successful
1157b89a7cc2SEnji Cooper // test part.
TEST(SuccessRegistrationDeathTest,NoSuccessPart)1158b89a7cc2SEnji Cooper TEST(SuccessRegistrationDeathTest, NoSuccessPart) {
1159*5ca8c28cSEnji Cooper   EXPECT_DEATH(_Exit(1), "");
1160b89a7cc2SEnji Cooper   EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count());
1161b89a7cc2SEnji Cooper }
1162b89a7cc2SEnji Cooper 
TEST(StreamingAssertionsDeathTest,DeathTest)1163b89a7cc2SEnji Cooper TEST(StreamingAssertionsDeathTest, DeathTest) {
1164*5ca8c28cSEnji Cooper   EXPECT_DEATH(_Exit(1), "") << "unexpected failure";
1165*5ca8c28cSEnji Cooper   ASSERT_DEATH(_Exit(1), "") << "unexpected failure";
116628f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
116728f6c2f2SEnji Cooper       {  // NOLINT
1168*5ca8c28cSEnji Cooper         EXPECT_DEATH(_Exit(0), "") << "expected failure";
116928f6c2f2SEnji Cooper       },
117028f6c2f2SEnji Cooper       "expected failure");
117128f6c2f2SEnji Cooper   EXPECT_FATAL_FAILURE(
117228f6c2f2SEnji Cooper       {  // NOLINT
1173*5ca8c28cSEnji Cooper         ASSERT_DEATH(_Exit(0), "") << "expected failure";
117428f6c2f2SEnji Cooper       },
117528f6c2f2SEnji Cooper       "expected failure");
1176b89a7cc2SEnji Cooper }
1177b89a7cc2SEnji Cooper 
1178b89a7cc2SEnji Cooper // Tests that GetLastErrnoDescription returns an empty string when the
1179b89a7cc2SEnji Cooper // last error is 0 and non-empty string when it is non-zero.
TEST(GetLastErrnoDescription,GetLastErrnoDescriptionWorks)1180b89a7cc2SEnji Cooper TEST(GetLastErrnoDescription, GetLastErrnoDescriptionWorks) {
1181b89a7cc2SEnji Cooper   errno = ENOENT;
1182b89a7cc2SEnji Cooper   EXPECT_STRNE("", GetLastErrnoDescription().c_str());
1183b89a7cc2SEnji Cooper   errno = 0;
1184b89a7cc2SEnji Cooper   EXPECT_STREQ("", GetLastErrnoDescription().c_str());
1185b89a7cc2SEnji Cooper }
1186b89a7cc2SEnji Cooper 
118728f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
TEST(AutoHandleTest,AutoHandleWorks)1188b89a7cc2SEnji Cooper TEST(AutoHandleTest, AutoHandleWorks) {
1189b89a7cc2SEnji Cooper   HANDLE handle = ::CreateEvent(NULL, FALSE, FALSE, NULL);
1190b89a7cc2SEnji Cooper   ASSERT_NE(INVALID_HANDLE_VALUE, handle);
1191b89a7cc2SEnji Cooper 
1192b89a7cc2SEnji Cooper   // Tests that the AutoHandle is correctly initialized with a handle.
1193b89a7cc2SEnji Cooper   testing::internal::AutoHandle auto_handle(handle);
1194b89a7cc2SEnji Cooper   EXPECT_EQ(handle, auto_handle.Get());
1195b89a7cc2SEnji Cooper 
1196b89a7cc2SEnji Cooper   // Tests that Reset assigns INVALID_HANDLE_VALUE.
1197b89a7cc2SEnji Cooper   // Note that this cannot verify whether the original handle is closed.
1198b89a7cc2SEnji Cooper   auto_handle.Reset();
1199b89a7cc2SEnji Cooper   EXPECT_EQ(INVALID_HANDLE_VALUE, auto_handle.Get());
1200b89a7cc2SEnji Cooper 
1201b89a7cc2SEnji Cooper   // Tests that Reset assigns the new handle.
1202b89a7cc2SEnji Cooper   // Note that this cannot verify whether the original handle is closed.
1203b89a7cc2SEnji Cooper   handle = ::CreateEvent(NULL, FALSE, FALSE, NULL);
1204b89a7cc2SEnji Cooper   ASSERT_NE(INVALID_HANDLE_VALUE, handle);
1205b89a7cc2SEnji Cooper   auto_handle.Reset(handle);
1206b89a7cc2SEnji Cooper   EXPECT_EQ(handle, auto_handle.Get());
1207b89a7cc2SEnji Cooper 
1208b89a7cc2SEnji Cooper   // Tests that AutoHandle contains INVALID_HANDLE_VALUE by default.
1209b89a7cc2SEnji Cooper   testing::internal::AutoHandle auto_handle2;
1210b89a7cc2SEnji Cooper   EXPECT_EQ(INVALID_HANDLE_VALUE, auto_handle2.Get());
1211b89a7cc2SEnji Cooper }
1212b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS
1213b89a7cc2SEnji Cooper 
121428f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
1215b89a7cc2SEnji Cooper typedef unsigned __int64 BiggestParsable;
1216b89a7cc2SEnji Cooper typedef signed __int64 BiggestSignedParsable;
1217b89a7cc2SEnji Cooper #else
1218b89a7cc2SEnji Cooper typedef unsigned long long BiggestParsable;
1219b89a7cc2SEnji Cooper typedef signed long long BiggestSignedParsable;
1220b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS
1221b89a7cc2SEnji Cooper 
1222b89a7cc2SEnji Cooper // We cannot use std::numeric_limits<T>::max() as it clashes with the
1223b89a7cc2SEnji Cooper // max() macro defined by <windows.h>.
1224b89a7cc2SEnji Cooper const BiggestParsable kBiggestParsableMax = ULLONG_MAX;
1225b89a7cc2SEnji Cooper const BiggestSignedParsable kBiggestSignedParsableMax = LLONG_MAX;
1226b89a7cc2SEnji Cooper 
TEST(ParseNaturalNumberTest,RejectsInvalidFormat)1227b89a7cc2SEnji Cooper TEST(ParseNaturalNumberTest, RejectsInvalidFormat) {
1228b89a7cc2SEnji Cooper   BiggestParsable result = 0;
1229b89a7cc2SEnji Cooper 
1230b89a7cc2SEnji Cooper   // Rejects non-numbers.
1231b89a7cc2SEnji Cooper   EXPECT_FALSE(ParseNaturalNumber("non-number string", &result));
1232b89a7cc2SEnji Cooper 
1233b89a7cc2SEnji Cooper   // Rejects numbers with whitespace prefix.
1234b89a7cc2SEnji Cooper   EXPECT_FALSE(ParseNaturalNumber(" 123", &result));
1235b89a7cc2SEnji Cooper 
1236b89a7cc2SEnji Cooper   // Rejects negative numbers.
1237b89a7cc2SEnji Cooper   EXPECT_FALSE(ParseNaturalNumber("-123", &result));
1238b89a7cc2SEnji Cooper 
1239b89a7cc2SEnji Cooper   // Rejects numbers starting with a plus sign.
1240b89a7cc2SEnji Cooper   EXPECT_FALSE(ParseNaturalNumber("+123", &result));
1241b89a7cc2SEnji Cooper   errno = 0;
1242b89a7cc2SEnji Cooper }
1243b89a7cc2SEnji Cooper 
TEST(ParseNaturalNumberTest,RejectsOverflownNumbers)1244b89a7cc2SEnji Cooper TEST(ParseNaturalNumberTest, RejectsOverflownNumbers) {
1245b89a7cc2SEnji Cooper   BiggestParsable result = 0;
1246b89a7cc2SEnji Cooper 
1247b89a7cc2SEnji Cooper   EXPECT_FALSE(ParseNaturalNumber("99999999999999999999999", &result));
1248b89a7cc2SEnji Cooper 
1249b89a7cc2SEnji Cooper   signed char char_result = 0;
1250b89a7cc2SEnji Cooper   EXPECT_FALSE(ParseNaturalNumber("200", &char_result));
1251b89a7cc2SEnji Cooper   errno = 0;
1252b89a7cc2SEnji Cooper }
1253b89a7cc2SEnji Cooper 
TEST(ParseNaturalNumberTest,AcceptsValidNumbers)1254b89a7cc2SEnji Cooper TEST(ParseNaturalNumberTest, AcceptsValidNumbers) {
1255b89a7cc2SEnji Cooper   BiggestParsable result = 0;
1256b89a7cc2SEnji Cooper 
1257b89a7cc2SEnji Cooper   result = 0;
1258b89a7cc2SEnji Cooper   ASSERT_TRUE(ParseNaturalNumber("123", &result));
1259b89a7cc2SEnji Cooper   EXPECT_EQ(123U, result);
1260b89a7cc2SEnji Cooper 
1261b89a7cc2SEnji Cooper   // Check 0 as an edge case.
1262b89a7cc2SEnji Cooper   result = 1;
1263b89a7cc2SEnji Cooper   ASSERT_TRUE(ParseNaturalNumber("0", &result));
1264b89a7cc2SEnji Cooper   EXPECT_EQ(0U, result);
1265b89a7cc2SEnji Cooper 
1266b89a7cc2SEnji Cooper   result = 1;
1267b89a7cc2SEnji Cooper   ASSERT_TRUE(ParseNaturalNumber("00000", &result));
1268b89a7cc2SEnji Cooper   EXPECT_EQ(0U, result);
1269b89a7cc2SEnji Cooper }
1270b89a7cc2SEnji Cooper 
TEST(ParseNaturalNumberTest,AcceptsTypeLimits)1271b89a7cc2SEnji Cooper TEST(ParseNaturalNumberTest, AcceptsTypeLimits) {
1272b89a7cc2SEnji Cooper   Message msg;
1273b89a7cc2SEnji Cooper   msg << kBiggestParsableMax;
1274b89a7cc2SEnji Cooper 
1275b89a7cc2SEnji Cooper   BiggestParsable result = 0;
1276b89a7cc2SEnji Cooper   EXPECT_TRUE(ParseNaturalNumber(msg.GetString(), &result));
1277b89a7cc2SEnji Cooper   EXPECT_EQ(kBiggestParsableMax, result);
1278b89a7cc2SEnji Cooper 
1279b89a7cc2SEnji Cooper   Message msg2;
1280b89a7cc2SEnji Cooper   msg2 << kBiggestSignedParsableMax;
1281b89a7cc2SEnji Cooper 
1282b89a7cc2SEnji Cooper   BiggestSignedParsable signed_result = 0;
1283b89a7cc2SEnji Cooper   EXPECT_TRUE(ParseNaturalNumber(msg2.GetString(), &signed_result));
1284b89a7cc2SEnji Cooper   EXPECT_EQ(kBiggestSignedParsableMax, signed_result);
1285b89a7cc2SEnji Cooper 
1286b89a7cc2SEnji Cooper   Message msg3;
1287b89a7cc2SEnji Cooper   msg3 << INT_MAX;
1288b89a7cc2SEnji Cooper 
1289b89a7cc2SEnji Cooper   int int_result = 0;
1290b89a7cc2SEnji Cooper   EXPECT_TRUE(ParseNaturalNumber(msg3.GetString(), &int_result));
1291b89a7cc2SEnji Cooper   EXPECT_EQ(INT_MAX, int_result);
1292b89a7cc2SEnji Cooper 
1293b89a7cc2SEnji Cooper   Message msg4;
1294b89a7cc2SEnji Cooper   msg4 << UINT_MAX;
1295b89a7cc2SEnji Cooper 
1296b89a7cc2SEnji Cooper   unsigned int uint_result = 0;
1297b89a7cc2SEnji Cooper   EXPECT_TRUE(ParseNaturalNumber(msg4.GetString(), &uint_result));
1298b89a7cc2SEnji Cooper   EXPECT_EQ(UINT_MAX, uint_result);
1299b89a7cc2SEnji Cooper }
1300b89a7cc2SEnji Cooper 
TEST(ParseNaturalNumberTest,WorksForShorterIntegers)1301b89a7cc2SEnji Cooper TEST(ParseNaturalNumberTest, WorksForShorterIntegers) {
1302b89a7cc2SEnji Cooper   short short_result = 0;
1303b89a7cc2SEnji Cooper   ASSERT_TRUE(ParseNaturalNumber("123", &short_result));
1304b89a7cc2SEnji Cooper   EXPECT_EQ(123, short_result);
1305b89a7cc2SEnji Cooper 
1306b89a7cc2SEnji Cooper   signed char char_result = 0;
1307b89a7cc2SEnji Cooper   ASSERT_TRUE(ParseNaturalNumber("123", &char_result));
1308b89a7cc2SEnji Cooper   EXPECT_EQ(123, char_result);
1309b89a7cc2SEnji Cooper }
1310b89a7cc2SEnji Cooper 
131128f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
TEST(EnvironmentTest,HandleFitsIntoSizeT)1312b89a7cc2SEnji Cooper TEST(EnvironmentTest, HandleFitsIntoSizeT) {
1313b89a7cc2SEnji Cooper   ASSERT_TRUE(sizeof(HANDLE) <= sizeof(size_t));
1314b89a7cc2SEnji Cooper }
1315b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS
1316b89a7cc2SEnji Cooper 
1317b89a7cc2SEnji Cooper // Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED trigger
1318b89a7cc2SEnji Cooper // failures when death tests are available on the system.
TEST(ConditionalDeathMacrosDeathTest,ExpectsDeathWhenDeathTestsAvailable)1319b89a7cc2SEnji Cooper TEST(ConditionalDeathMacrosDeathTest, ExpectsDeathWhenDeathTestsAvailable) {
1320b89a7cc2SEnji Cooper   EXPECT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestExpectMacro"),
1321b89a7cc2SEnji Cooper                             "death inside CondDeathTestExpectMacro");
1322b89a7cc2SEnji Cooper   ASSERT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestAssertMacro"),
1323b89a7cc2SEnji Cooper                             "death inside CondDeathTestAssertMacro");
1324b89a7cc2SEnji Cooper 
1325b89a7cc2SEnji Cooper   // Empty statement will not crash, which must trigger a failure.
1326b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(EXPECT_DEATH_IF_SUPPORTED(;, ""), "");
1327b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(ASSERT_DEATH_IF_SUPPORTED(;, ""), "");
1328b89a7cc2SEnji Cooper }
1329b89a7cc2SEnji Cooper 
TEST(InDeathTestChildDeathTest,ReportsDeathTestCorrectlyInFastStyle)1330b89a7cc2SEnji Cooper TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInFastStyle) {
133128f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "fast");
1332b89a7cc2SEnji Cooper   EXPECT_FALSE(InDeathTestChild());
133328f6c2f2SEnji Cooper   EXPECT_DEATH(
133428f6c2f2SEnji Cooper       {
1335b89a7cc2SEnji Cooper         fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside");
1336b89a7cc2SEnji Cooper         fflush(stderr);
1337*5ca8c28cSEnji Cooper         _Exit(1);
133828f6c2f2SEnji Cooper       },
133928f6c2f2SEnji Cooper       "Inside");
1340b89a7cc2SEnji Cooper }
1341b89a7cc2SEnji Cooper 
TEST(InDeathTestChildDeathTest,ReportsDeathTestCorrectlyInThreadSafeStyle)1342b89a7cc2SEnji Cooper TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInThreadSafeStyle) {
134328f6c2f2SEnji Cooper   GTEST_FLAG_SET(death_test_style, "threadsafe");
1344b89a7cc2SEnji Cooper   EXPECT_FALSE(InDeathTestChild());
134528f6c2f2SEnji Cooper   EXPECT_DEATH(
134628f6c2f2SEnji Cooper       {
1347b89a7cc2SEnji Cooper         fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside");
1348b89a7cc2SEnji Cooper         fflush(stderr);
1349*5ca8c28cSEnji Cooper         _Exit(1);
135028f6c2f2SEnji Cooper       },
135128f6c2f2SEnji Cooper       "Inside");
1352b89a7cc2SEnji Cooper }
1353b89a7cc2SEnji Cooper 
DieWithMessage(const char * message)135428f6c2f2SEnji Cooper void DieWithMessage(const char* message) {
135528f6c2f2SEnji Cooper   fputs(message, stderr);
135628f6c2f2SEnji Cooper   fflush(stderr);  // Make sure the text is printed before the process exits.
1357*5ca8c28cSEnji Cooper   _Exit(1);
135828f6c2f2SEnji Cooper }
135928f6c2f2SEnji Cooper 
TEST(MatcherDeathTest,DoesNotBreakBareRegexMatching)136028f6c2f2SEnji Cooper TEST(MatcherDeathTest, DoesNotBreakBareRegexMatching) {
136128f6c2f2SEnji Cooper   // googletest tests this, of course; here we ensure that including googlemock
136228f6c2f2SEnji Cooper   // has not broken it.
136328f6c2f2SEnji Cooper #ifdef GTEST_USES_POSIX_RE
136428f6c2f2SEnji Cooper   EXPECT_DEATH(DieWithMessage("O, I die, Horatio."), "I d[aeiou]e");
136528f6c2f2SEnji Cooper #else
136628f6c2f2SEnji Cooper   EXPECT_DEATH(DieWithMessage("O, I die, Horatio."), "I di?e");
136728f6c2f2SEnji Cooper #endif
136828f6c2f2SEnji Cooper }
136928f6c2f2SEnji Cooper 
TEST(MatcherDeathTest,MonomorphicMatcherMatches)137028f6c2f2SEnji Cooper TEST(MatcherDeathTest, MonomorphicMatcherMatches) {
137128f6c2f2SEnji Cooper   EXPECT_DEATH(DieWithMessage("Behind O, I am slain!"),
137228f6c2f2SEnji Cooper                Matcher<const std::string&>(ContainsRegex("I am slain")));
137328f6c2f2SEnji Cooper }
137428f6c2f2SEnji Cooper 
TEST(MatcherDeathTest,MonomorphicMatcherDoesNotMatch)137528f6c2f2SEnji Cooper TEST(MatcherDeathTest, MonomorphicMatcherDoesNotMatch) {
137628f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
137728f6c2f2SEnji Cooper       EXPECT_DEATH(
137828f6c2f2SEnji Cooper           DieWithMessage("Behind O, I am slain!"),
137928f6c2f2SEnji Cooper           Matcher<const std::string&>(ContainsRegex("Ow, I am slain"))),
138028f6c2f2SEnji Cooper       "Expected: contains regular expression \"Ow, I am slain\"");
138128f6c2f2SEnji Cooper }
138228f6c2f2SEnji Cooper 
TEST(MatcherDeathTest,PolymorphicMatcherMatches)138328f6c2f2SEnji Cooper TEST(MatcherDeathTest, PolymorphicMatcherMatches) {
138428f6c2f2SEnji Cooper   EXPECT_DEATH(DieWithMessage("The rest is silence."),
138528f6c2f2SEnji Cooper                ContainsRegex("rest is silence"));
138628f6c2f2SEnji Cooper }
138728f6c2f2SEnji Cooper 
TEST(MatcherDeathTest,PolymorphicMatcherDoesNotMatch)138828f6c2f2SEnji Cooper TEST(MatcherDeathTest, PolymorphicMatcherDoesNotMatch) {
138928f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
139028f6c2f2SEnji Cooper       EXPECT_DEATH(DieWithMessage("The rest is silence."),
139128f6c2f2SEnji Cooper                    ContainsRegex("rest is science")),
139228f6c2f2SEnji Cooper       "Expected: contains regular expression \"rest is science\"");
139328f6c2f2SEnji Cooper }
139428f6c2f2SEnji Cooper 
139528f6c2f2SEnji Cooper }  // namespace
139628f6c2f2SEnji Cooper 
1397b89a7cc2SEnji Cooper #else  // !GTEST_HAS_DEATH_TEST follows
1398b89a7cc2SEnji Cooper 
139928f6c2f2SEnji Cooper namespace {
140028f6c2f2SEnji Cooper 
1401b89a7cc2SEnji Cooper using testing::internal::CaptureStderr;
1402b89a7cc2SEnji Cooper using testing::internal::GetCapturedStderr;
1403b89a7cc2SEnji Cooper 
1404b89a7cc2SEnji Cooper // Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED are still
1405b89a7cc2SEnji Cooper // defined but do not trigger failures when death tests are not available on
1406b89a7cc2SEnji Cooper // the system.
TEST(ConditionalDeathMacrosTest,WarnsWhenDeathTestsNotAvailable)1407b89a7cc2SEnji Cooper TEST(ConditionalDeathMacrosTest, WarnsWhenDeathTestsNotAvailable) {
1408b89a7cc2SEnji Cooper   // Empty statement will not crash, but that should not trigger a failure
1409b89a7cc2SEnji Cooper   // when death tests are not supported.
1410b89a7cc2SEnji Cooper   CaptureStderr();
1411b89a7cc2SEnji Cooper   EXPECT_DEATH_IF_SUPPORTED(;, "");
1412b89a7cc2SEnji Cooper   std::string output = GetCapturedStderr();
1413b89a7cc2SEnji Cooper   ASSERT_TRUE(NULL != strstr(output.c_str(),
1414b89a7cc2SEnji Cooper                              "Death tests are not supported on this platform"));
1415b89a7cc2SEnji Cooper   ASSERT_TRUE(NULL != strstr(output.c_str(), ";"));
1416b89a7cc2SEnji Cooper 
1417b89a7cc2SEnji Cooper   // The streamed message should not be printed as there is no test failure.
1418b89a7cc2SEnji Cooper   CaptureStderr();
1419b89a7cc2SEnji Cooper   EXPECT_DEATH_IF_SUPPORTED(;, "") << "streamed message";
1420b89a7cc2SEnji Cooper   output = GetCapturedStderr();
1421b89a7cc2SEnji Cooper   ASSERT_TRUE(NULL == strstr(output.c_str(), "streamed message"));
1422b89a7cc2SEnji Cooper 
1423b89a7cc2SEnji Cooper   CaptureStderr();
1424b89a7cc2SEnji Cooper   ASSERT_DEATH_IF_SUPPORTED(;, "");  // NOLINT
1425b89a7cc2SEnji Cooper   output = GetCapturedStderr();
1426b89a7cc2SEnji Cooper   ASSERT_TRUE(NULL != strstr(output.c_str(),
1427b89a7cc2SEnji Cooper                              "Death tests are not supported on this platform"));
1428b89a7cc2SEnji Cooper   ASSERT_TRUE(NULL != strstr(output.c_str(), ";"));
1429b89a7cc2SEnji Cooper 
1430b89a7cc2SEnji Cooper   CaptureStderr();
1431b89a7cc2SEnji Cooper   ASSERT_DEATH_IF_SUPPORTED(;, "") << "streamed message";  // NOLINT
1432b89a7cc2SEnji Cooper   output = GetCapturedStderr();
1433b89a7cc2SEnji Cooper   ASSERT_TRUE(NULL == strstr(output.c_str(), "streamed message"));
1434b89a7cc2SEnji Cooper }
1435b89a7cc2SEnji Cooper 
FuncWithAssert(int * n)1436b89a7cc2SEnji Cooper void FuncWithAssert(int* n) {
1437b89a7cc2SEnji Cooper   ASSERT_DEATH_IF_SUPPORTED(return;, "");
1438b89a7cc2SEnji Cooper   (*n)++;
1439b89a7cc2SEnji Cooper }
1440b89a7cc2SEnji Cooper 
1441b89a7cc2SEnji Cooper // Tests that ASSERT_DEATH_IF_SUPPORTED does not return from the current
1442b89a7cc2SEnji Cooper // function (as ASSERT_DEATH does) if death tests are not supported.
TEST(ConditionalDeathMacrosTest,AssertDeatDoesNotReturnhIfUnsupported)1443b89a7cc2SEnji Cooper TEST(ConditionalDeathMacrosTest, AssertDeatDoesNotReturnhIfUnsupported) {
1444b89a7cc2SEnji Cooper   int n = 0;
1445b89a7cc2SEnji Cooper   FuncWithAssert(&n);
1446b89a7cc2SEnji Cooper   EXPECT_EQ(1, n);
1447b89a7cc2SEnji Cooper }
1448b89a7cc2SEnji Cooper 
144928f6c2f2SEnji Cooper }  // namespace
145028f6c2f2SEnji Cooper 
1451b89a7cc2SEnji Cooper #endif  // !GTEST_HAS_DEATH_TEST
1452b89a7cc2SEnji Cooper 
145328f6c2f2SEnji Cooper namespace {
145428f6c2f2SEnji Cooper 
145528f6c2f2SEnji Cooper // The following code intentionally tests a suboptimal syntax.
145628f6c2f2SEnji Cooper #ifdef __GNUC__
145728f6c2f2SEnji Cooper #pragma GCC diagnostic push
145828f6c2f2SEnji Cooper #pragma GCC diagnostic ignored "-Wdangling-else"
145928f6c2f2SEnji Cooper #pragma GCC diagnostic ignored "-Wempty-body"
146028f6c2f2SEnji Cooper #pragma GCC diagnostic ignored "-Wpragmas"
146128f6c2f2SEnji Cooper #endif
1462b89a7cc2SEnji Cooper // Tests that the death test macros expand to code which may or may not
1463b89a7cc2SEnji Cooper // be followed by operator<<, and that in either case the complete text
1464b89a7cc2SEnji Cooper // comprises only a single C++ statement.
1465b89a7cc2SEnji Cooper //
1466b89a7cc2SEnji Cooper // The syntax should work whether death tests are available or not.
TEST(ConditionalDeathMacrosSyntaxDeathTest,SingleStatement)1467b89a7cc2SEnji Cooper TEST(ConditionalDeathMacrosSyntaxDeathTest, SingleStatement) {
1468b89a7cc2SEnji Cooper   if (AlwaysFalse())
1469b89a7cc2SEnji Cooper     // This would fail if executed; this is a compilation test only
1470b89a7cc2SEnji Cooper     ASSERT_DEATH_IF_SUPPORTED(return, "");
1471b89a7cc2SEnji Cooper 
1472b89a7cc2SEnji Cooper   if (AlwaysTrue())
1473*5ca8c28cSEnji Cooper     EXPECT_DEATH_IF_SUPPORTED(_Exit(1), "");
1474b89a7cc2SEnji Cooper   else
1475b89a7cc2SEnji Cooper     // This empty "else" branch is meant to ensure that EXPECT_DEATH
1476b89a7cc2SEnji Cooper     // doesn't expand into an "if" statement without an "else"
1477b89a7cc2SEnji Cooper     ;  // NOLINT
1478b89a7cc2SEnji Cooper 
147928f6c2f2SEnji Cooper   if (AlwaysFalse()) ASSERT_DEATH_IF_SUPPORTED(return, "") << "did not die";
1480b89a7cc2SEnji Cooper 
1481b89a7cc2SEnji Cooper   if (AlwaysFalse())
1482b89a7cc2SEnji Cooper     ;  // NOLINT
1483b89a7cc2SEnji Cooper   else
1484*5ca8c28cSEnji Cooper     EXPECT_DEATH_IF_SUPPORTED(_Exit(1), "") << 1 << 2 << 3;
1485b89a7cc2SEnji Cooper }
148628f6c2f2SEnji Cooper #ifdef __GNUC__
148728f6c2f2SEnji Cooper #pragma GCC diagnostic pop
148828f6c2f2SEnji Cooper #endif
1489b89a7cc2SEnji Cooper 
1490b89a7cc2SEnji Cooper // Tests that conditional death test macros expand to code which interacts
1491b89a7cc2SEnji Cooper // well with switch statements.
TEST(ConditionalDeathMacrosSyntaxDeathTest,SwitchStatement)1492b89a7cc2SEnji Cooper TEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) {
1493b89a7cc2SEnji Cooper   // Microsoft compiler usually complains about switch statements without
1494b89a7cc2SEnji Cooper   // case labels. We suppress that warning for this test.
1495b89a7cc2SEnji Cooper   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4065)
1496b89a7cc2SEnji Cooper 
1497b89a7cc2SEnji Cooper   switch (0)
1498b89a7cc2SEnji Cooper   default:
1499*5ca8c28cSEnji Cooper     ASSERT_DEATH_IF_SUPPORTED(_Exit(1), "") << "exit in default switch handler";
1500b89a7cc2SEnji Cooper 
1501b89a7cc2SEnji Cooper   switch (0)
1502b89a7cc2SEnji Cooper   case 0:
1503*5ca8c28cSEnji Cooper     EXPECT_DEATH_IF_SUPPORTED(_Exit(1), "") << "exit in switch case";
1504b89a7cc2SEnji Cooper 
1505b89a7cc2SEnji Cooper   GTEST_DISABLE_MSC_WARNINGS_POP_()
1506b89a7cc2SEnji Cooper }
1507b89a7cc2SEnji Cooper 
1508b89a7cc2SEnji Cooper // Tests that a test case whose name ends with "DeathTest" works fine
1509b89a7cc2SEnji Cooper // on Windows.
TEST(NotADeathTest,Test)151028f6c2f2SEnji Cooper TEST(NotADeathTest, Test) { SUCCEED(); }
151128f6c2f2SEnji Cooper 
151228f6c2f2SEnji Cooper }  // namespace
1513