xref: /freebsd/contrib/googletest/googletest/test/googletest-death-test-test.cc (revision b89a7cc2ed6e4398d5be502f5bb5885d1ec6ff0f)
1*b89a7cc2SEnji Cooper // Copyright 2005, Google Inc.
2*b89a7cc2SEnji Cooper // All rights reserved.
3*b89a7cc2SEnji Cooper //
4*b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5*b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6*b89a7cc2SEnji Cooper // met:
7*b89a7cc2SEnji Cooper //
8*b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9*b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10*b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11*b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12*b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13*b89a7cc2SEnji Cooper // distribution.
14*b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15*b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16*b89a7cc2SEnji Cooper // this software without specific prior written permission.
17*b89a7cc2SEnji Cooper //
18*b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*b89a7cc2SEnji Cooper 
30*b89a7cc2SEnji Cooper //
31*b89a7cc2SEnji Cooper // Tests for death tests.
32*b89a7cc2SEnji Cooper 
33*b89a7cc2SEnji Cooper #include "gtest/gtest-death-test.h"
34*b89a7cc2SEnji Cooper #include "gtest/gtest.h"
35*b89a7cc2SEnji Cooper #include "gtest/internal/gtest-filepath.h"
36*b89a7cc2SEnji Cooper 
37*b89a7cc2SEnji Cooper using testing::internal::AlwaysFalse;
38*b89a7cc2SEnji Cooper using testing::internal::AlwaysTrue;
39*b89a7cc2SEnji Cooper 
40*b89a7cc2SEnji Cooper #if GTEST_HAS_DEATH_TEST
41*b89a7cc2SEnji Cooper 
42*b89a7cc2SEnji Cooper # if GTEST_OS_WINDOWS
43*b89a7cc2SEnji Cooper #  include <direct.h>          // For chdir().
44*b89a7cc2SEnji Cooper # else
45*b89a7cc2SEnji Cooper #  include <unistd.h>
46*b89a7cc2SEnji Cooper #  include <sys/wait.h>        // For waitpid.
47*b89a7cc2SEnji Cooper # endif  // GTEST_OS_WINDOWS
48*b89a7cc2SEnji Cooper 
49*b89a7cc2SEnji Cooper # include <limits.h>
50*b89a7cc2SEnji Cooper # include <signal.h>
51*b89a7cc2SEnji Cooper # include <stdio.h>
52*b89a7cc2SEnji Cooper 
53*b89a7cc2SEnji Cooper # if GTEST_OS_LINUX
54*b89a7cc2SEnji Cooper #  include <sys/time.h>
55*b89a7cc2SEnji Cooper # endif  // GTEST_OS_LINUX
56*b89a7cc2SEnji Cooper 
57*b89a7cc2SEnji Cooper # include "gtest/gtest-spi.h"
58*b89a7cc2SEnji Cooper # include "src/gtest-internal-inl.h"
59*b89a7cc2SEnji Cooper 
60*b89a7cc2SEnji Cooper namespace posix = ::testing::internal::posix;
61*b89a7cc2SEnji Cooper 
62*b89a7cc2SEnji Cooper using testing::Message;
63*b89a7cc2SEnji Cooper using testing::internal::DeathTest;
64*b89a7cc2SEnji Cooper using testing::internal::DeathTestFactory;
65*b89a7cc2SEnji Cooper using testing::internal::FilePath;
66*b89a7cc2SEnji Cooper using testing::internal::GetLastErrnoDescription;
67*b89a7cc2SEnji Cooper using testing::internal::GetUnitTestImpl;
68*b89a7cc2SEnji Cooper using testing::internal::InDeathTestChild;
69*b89a7cc2SEnji Cooper using testing::internal::ParseNaturalNumber;
70*b89a7cc2SEnji Cooper 
71*b89a7cc2SEnji Cooper namespace testing {
72*b89a7cc2SEnji Cooper namespace internal {
73*b89a7cc2SEnji Cooper 
74*b89a7cc2SEnji Cooper // A helper class whose objects replace the death test factory for a
75*b89a7cc2SEnji Cooper // single UnitTest object during their lifetimes.
76*b89a7cc2SEnji Cooper class ReplaceDeathTestFactory {
77*b89a7cc2SEnji Cooper  public:
78*b89a7cc2SEnji Cooper   explicit ReplaceDeathTestFactory(DeathTestFactory* new_factory)
79*b89a7cc2SEnji Cooper       : unit_test_impl_(GetUnitTestImpl()) {
80*b89a7cc2SEnji Cooper     old_factory_ = unit_test_impl_->death_test_factory_.release();
81*b89a7cc2SEnji Cooper     unit_test_impl_->death_test_factory_.reset(new_factory);
82*b89a7cc2SEnji Cooper   }
83*b89a7cc2SEnji Cooper 
84*b89a7cc2SEnji Cooper   ~ReplaceDeathTestFactory() {
85*b89a7cc2SEnji Cooper     unit_test_impl_->death_test_factory_.release();
86*b89a7cc2SEnji Cooper     unit_test_impl_->death_test_factory_.reset(old_factory_);
87*b89a7cc2SEnji Cooper   }
88*b89a7cc2SEnji Cooper  private:
89*b89a7cc2SEnji Cooper   // Prevents copying ReplaceDeathTestFactory objects.
90*b89a7cc2SEnji Cooper   ReplaceDeathTestFactory(const ReplaceDeathTestFactory&);
91*b89a7cc2SEnji Cooper   void operator=(const ReplaceDeathTestFactory&);
92*b89a7cc2SEnji Cooper 
93*b89a7cc2SEnji Cooper   UnitTestImpl* unit_test_impl_;
94*b89a7cc2SEnji Cooper   DeathTestFactory* old_factory_;
95*b89a7cc2SEnji Cooper };
96*b89a7cc2SEnji Cooper 
97*b89a7cc2SEnji Cooper }  // namespace internal
98*b89a7cc2SEnji Cooper }  // namespace testing
99*b89a7cc2SEnji Cooper 
100*b89a7cc2SEnji Cooper void DieWithMessage(const ::std::string& message) {
101*b89a7cc2SEnji Cooper   fprintf(stderr, "%s", message.c_str());
102*b89a7cc2SEnji Cooper   fflush(stderr);  // Make sure the text is printed before the process exits.
103*b89a7cc2SEnji Cooper 
104*b89a7cc2SEnji Cooper   // We call _exit() instead of exit(), as the former is a direct
105*b89a7cc2SEnji Cooper   // system call and thus safer in the presence of threads.  exit()
106*b89a7cc2SEnji Cooper   // will invoke user-defined exit-hooks, which may do dangerous
107*b89a7cc2SEnji Cooper   // things that conflict with death tests.
108*b89a7cc2SEnji Cooper   //
109*b89a7cc2SEnji Cooper   // Some compilers can recognize that _exit() never returns and issue the
110*b89a7cc2SEnji Cooper   // 'unreachable code' warning for code following this function, unless
111*b89a7cc2SEnji Cooper   // fooled by a fake condition.
112*b89a7cc2SEnji Cooper   if (AlwaysTrue())
113*b89a7cc2SEnji Cooper     _exit(1);
114*b89a7cc2SEnji Cooper }
115*b89a7cc2SEnji Cooper 
116*b89a7cc2SEnji Cooper void DieInside(const ::std::string& function) {
117*b89a7cc2SEnji Cooper   DieWithMessage("death inside " + function + "().");
118*b89a7cc2SEnji Cooper }
119*b89a7cc2SEnji Cooper 
120*b89a7cc2SEnji Cooper // Tests that death tests work.
121*b89a7cc2SEnji Cooper 
122*b89a7cc2SEnji Cooper class TestForDeathTest : public testing::Test {
123*b89a7cc2SEnji Cooper  protected:
124*b89a7cc2SEnji Cooper   TestForDeathTest() : original_dir_(FilePath::GetCurrentDir()) {}
125*b89a7cc2SEnji Cooper 
126*b89a7cc2SEnji Cooper   virtual ~TestForDeathTest() {
127*b89a7cc2SEnji Cooper     posix::ChDir(original_dir_.c_str());
128*b89a7cc2SEnji Cooper   }
129*b89a7cc2SEnji Cooper 
130*b89a7cc2SEnji Cooper   // A static member function that's expected to die.
131*b89a7cc2SEnji Cooper   static void StaticMemberFunction() { DieInside("StaticMemberFunction"); }
132*b89a7cc2SEnji Cooper 
133*b89a7cc2SEnji Cooper   // A method of the test fixture that may die.
134*b89a7cc2SEnji Cooper   void MemberFunction() {
135*b89a7cc2SEnji Cooper     if (should_die_)
136*b89a7cc2SEnji Cooper       DieInside("MemberFunction");
137*b89a7cc2SEnji Cooper   }
138*b89a7cc2SEnji Cooper 
139*b89a7cc2SEnji Cooper   // True iff MemberFunction() should die.
140*b89a7cc2SEnji Cooper   bool should_die_;
141*b89a7cc2SEnji Cooper   const FilePath original_dir_;
142*b89a7cc2SEnji Cooper };
143*b89a7cc2SEnji Cooper 
144*b89a7cc2SEnji Cooper // A class with a member function that may die.
145*b89a7cc2SEnji Cooper class MayDie {
146*b89a7cc2SEnji Cooper  public:
147*b89a7cc2SEnji Cooper   explicit MayDie(bool should_die) : should_die_(should_die) {}
148*b89a7cc2SEnji Cooper 
149*b89a7cc2SEnji Cooper   // A member function that may die.
150*b89a7cc2SEnji Cooper   void MemberFunction() const {
151*b89a7cc2SEnji Cooper     if (should_die_)
152*b89a7cc2SEnji Cooper       DieInside("MayDie::MemberFunction");
153*b89a7cc2SEnji Cooper   }
154*b89a7cc2SEnji Cooper 
155*b89a7cc2SEnji Cooper  private:
156*b89a7cc2SEnji Cooper   // True iff MemberFunction() should die.
157*b89a7cc2SEnji Cooper   bool should_die_;
158*b89a7cc2SEnji Cooper };
159*b89a7cc2SEnji Cooper 
160*b89a7cc2SEnji Cooper // A global function that's expected to die.
161*b89a7cc2SEnji Cooper void GlobalFunction() { DieInside("GlobalFunction"); }
162*b89a7cc2SEnji Cooper 
163*b89a7cc2SEnji Cooper // A non-void function that's expected to die.
164*b89a7cc2SEnji Cooper int NonVoidFunction() {
165*b89a7cc2SEnji Cooper   DieInside("NonVoidFunction");
166*b89a7cc2SEnji Cooper   return 1;
167*b89a7cc2SEnji Cooper }
168*b89a7cc2SEnji Cooper 
169*b89a7cc2SEnji Cooper // A unary function that may die.
170*b89a7cc2SEnji Cooper void DieIf(bool should_die) {
171*b89a7cc2SEnji Cooper   if (should_die)
172*b89a7cc2SEnji Cooper     DieInside("DieIf");
173*b89a7cc2SEnji Cooper }
174*b89a7cc2SEnji Cooper 
175*b89a7cc2SEnji Cooper // A binary function that may die.
176*b89a7cc2SEnji Cooper bool DieIfLessThan(int x, int y) {
177*b89a7cc2SEnji Cooper   if (x < y) {
178*b89a7cc2SEnji Cooper     DieInside("DieIfLessThan");
179*b89a7cc2SEnji Cooper   }
180*b89a7cc2SEnji Cooper   return true;
181*b89a7cc2SEnji Cooper }
182*b89a7cc2SEnji Cooper 
183*b89a7cc2SEnji Cooper // Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture.
184*b89a7cc2SEnji Cooper void DeathTestSubroutine() {
185*b89a7cc2SEnji Cooper   EXPECT_DEATH(GlobalFunction(), "death.*GlobalFunction");
186*b89a7cc2SEnji Cooper   ASSERT_DEATH(GlobalFunction(), "death.*GlobalFunction");
187*b89a7cc2SEnji Cooper }
188*b89a7cc2SEnji Cooper 
189*b89a7cc2SEnji Cooper // Death in dbg, not opt.
190*b89a7cc2SEnji Cooper int DieInDebugElse12(int* sideeffect) {
191*b89a7cc2SEnji Cooper   if (sideeffect) *sideeffect = 12;
192*b89a7cc2SEnji Cooper 
193*b89a7cc2SEnji Cooper # ifndef NDEBUG
194*b89a7cc2SEnji Cooper 
195*b89a7cc2SEnji Cooper   DieInside("DieInDebugElse12");
196*b89a7cc2SEnji Cooper 
197*b89a7cc2SEnji Cooper # endif  // NDEBUG
198*b89a7cc2SEnji Cooper 
199*b89a7cc2SEnji Cooper   return 12;
200*b89a7cc2SEnji Cooper }
201*b89a7cc2SEnji Cooper 
202*b89a7cc2SEnji Cooper # if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
203*b89a7cc2SEnji Cooper 
204*b89a7cc2SEnji Cooper // Tests the ExitedWithCode predicate.
205*b89a7cc2SEnji Cooper TEST(ExitStatusPredicateTest, ExitedWithCode) {
206*b89a7cc2SEnji Cooper   // On Windows, the process's exit code is the same as its exit status,
207*b89a7cc2SEnji Cooper   // so the predicate just compares the its input with its parameter.
208*b89a7cc2SEnji Cooper   EXPECT_TRUE(testing::ExitedWithCode(0)(0));
209*b89a7cc2SEnji Cooper   EXPECT_TRUE(testing::ExitedWithCode(1)(1));
210*b89a7cc2SEnji Cooper   EXPECT_TRUE(testing::ExitedWithCode(42)(42));
211*b89a7cc2SEnji Cooper   EXPECT_FALSE(testing::ExitedWithCode(0)(1));
212*b89a7cc2SEnji Cooper   EXPECT_FALSE(testing::ExitedWithCode(1)(0));
213*b89a7cc2SEnji Cooper }
214*b89a7cc2SEnji Cooper 
215*b89a7cc2SEnji Cooper # else
216*b89a7cc2SEnji Cooper 
217*b89a7cc2SEnji Cooper // Returns the exit status of a process that calls _exit(2) with a
218*b89a7cc2SEnji Cooper // given exit code.  This is a helper function for the
219*b89a7cc2SEnji Cooper // ExitStatusPredicateTest test suite.
220*b89a7cc2SEnji Cooper static int NormalExitStatus(int exit_code) {
221*b89a7cc2SEnji Cooper   pid_t child_pid = fork();
222*b89a7cc2SEnji Cooper   if (child_pid == 0) {
223*b89a7cc2SEnji Cooper     _exit(exit_code);
224*b89a7cc2SEnji Cooper   }
225*b89a7cc2SEnji Cooper   int status;
226*b89a7cc2SEnji Cooper   waitpid(child_pid, &status, 0);
227*b89a7cc2SEnji Cooper   return status;
228*b89a7cc2SEnji Cooper }
229*b89a7cc2SEnji Cooper 
230*b89a7cc2SEnji Cooper // Returns the exit status of a process that raises a given signal.
231*b89a7cc2SEnji Cooper // If the signal does not cause the process to die, then it returns
232*b89a7cc2SEnji Cooper // instead the exit status of a process that exits normally with exit
233*b89a7cc2SEnji Cooper // code 1.  This is a helper function for the ExitStatusPredicateTest
234*b89a7cc2SEnji Cooper // test suite.
235*b89a7cc2SEnji Cooper static int KilledExitStatus(int signum) {
236*b89a7cc2SEnji Cooper   pid_t child_pid = fork();
237*b89a7cc2SEnji Cooper   if (child_pid == 0) {
238*b89a7cc2SEnji Cooper     raise(signum);
239*b89a7cc2SEnji Cooper     _exit(1);
240*b89a7cc2SEnji Cooper   }
241*b89a7cc2SEnji Cooper   int status;
242*b89a7cc2SEnji Cooper   waitpid(child_pid, &status, 0);
243*b89a7cc2SEnji Cooper   return status;
244*b89a7cc2SEnji Cooper }
245*b89a7cc2SEnji Cooper 
246*b89a7cc2SEnji Cooper // Tests the ExitedWithCode predicate.
247*b89a7cc2SEnji Cooper TEST(ExitStatusPredicateTest, ExitedWithCode) {
248*b89a7cc2SEnji Cooper   const int status0  = NormalExitStatus(0);
249*b89a7cc2SEnji Cooper   const int status1  = NormalExitStatus(1);
250*b89a7cc2SEnji Cooper   const int status42 = NormalExitStatus(42);
251*b89a7cc2SEnji Cooper   const testing::ExitedWithCode pred0(0);
252*b89a7cc2SEnji Cooper   const testing::ExitedWithCode pred1(1);
253*b89a7cc2SEnji Cooper   const testing::ExitedWithCode pred42(42);
254*b89a7cc2SEnji Cooper   EXPECT_PRED1(pred0,  status0);
255*b89a7cc2SEnji Cooper   EXPECT_PRED1(pred1,  status1);
256*b89a7cc2SEnji Cooper   EXPECT_PRED1(pred42, status42);
257*b89a7cc2SEnji Cooper   EXPECT_FALSE(pred0(status1));
258*b89a7cc2SEnji Cooper   EXPECT_FALSE(pred42(status0));
259*b89a7cc2SEnji Cooper   EXPECT_FALSE(pred1(status42));
260*b89a7cc2SEnji Cooper }
261*b89a7cc2SEnji Cooper 
262*b89a7cc2SEnji Cooper // Tests the KilledBySignal predicate.
263*b89a7cc2SEnji Cooper TEST(ExitStatusPredicateTest, KilledBySignal) {
264*b89a7cc2SEnji Cooper   const int status_segv = KilledExitStatus(SIGSEGV);
265*b89a7cc2SEnji Cooper   const int status_kill = KilledExitStatus(SIGKILL);
266*b89a7cc2SEnji Cooper   const testing::KilledBySignal pred_segv(SIGSEGV);
267*b89a7cc2SEnji Cooper   const testing::KilledBySignal pred_kill(SIGKILL);
268*b89a7cc2SEnji Cooper   EXPECT_PRED1(pred_segv, status_segv);
269*b89a7cc2SEnji Cooper   EXPECT_PRED1(pred_kill, status_kill);
270*b89a7cc2SEnji Cooper   EXPECT_FALSE(pred_segv(status_kill));
271*b89a7cc2SEnji Cooper   EXPECT_FALSE(pred_kill(status_segv));
272*b89a7cc2SEnji Cooper }
273*b89a7cc2SEnji Cooper 
274*b89a7cc2SEnji Cooper # endif  // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
275*b89a7cc2SEnji Cooper 
276*b89a7cc2SEnji Cooper // Tests that the death test macros expand to code which may or may not
277*b89a7cc2SEnji Cooper // be followed by operator<<, and that in either case the complete text
278*b89a7cc2SEnji Cooper // comprises only a single C++ statement.
279*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, SingleStatement) {
280*b89a7cc2SEnji Cooper   if (AlwaysFalse())
281*b89a7cc2SEnji Cooper     // This would fail if executed; this is a compilation test only
282*b89a7cc2SEnji Cooper     ASSERT_DEATH(return, "");
283*b89a7cc2SEnji Cooper 
284*b89a7cc2SEnji Cooper   if (AlwaysTrue())
285*b89a7cc2SEnji Cooper     EXPECT_DEATH(_exit(1), "");
286*b89a7cc2SEnji Cooper   else
287*b89a7cc2SEnji Cooper     // This empty "else" branch is meant to ensure that EXPECT_DEATH
288*b89a7cc2SEnji Cooper     // doesn't expand into an "if" statement without an "else"
289*b89a7cc2SEnji Cooper     ;
290*b89a7cc2SEnji Cooper 
291*b89a7cc2SEnji Cooper   if (AlwaysFalse())
292*b89a7cc2SEnji Cooper     ASSERT_DEATH(return, "") << "did not die";
293*b89a7cc2SEnji Cooper 
294*b89a7cc2SEnji Cooper   if (AlwaysFalse())
295*b89a7cc2SEnji Cooper     ;
296*b89a7cc2SEnji Cooper   else
297*b89a7cc2SEnji Cooper     EXPECT_DEATH(_exit(1), "") << 1 << 2 << 3;
298*b89a7cc2SEnji Cooper }
299*b89a7cc2SEnji Cooper 
300*b89a7cc2SEnji Cooper void DieWithEmbeddedNul() {
301*b89a7cc2SEnji Cooper   fprintf(stderr, "Hello%cmy null world.\n", '\0');
302*b89a7cc2SEnji Cooper   fflush(stderr);
303*b89a7cc2SEnji Cooper   _exit(1);
304*b89a7cc2SEnji Cooper }
305*b89a7cc2SEnji Cooper 
306*b89a7cc2SEnji Cooper # if GTEST_USES_PCRE
307*b89a7cc2SEnji Cooper 
308*b89a7cc2SEnji Cooper // Tests that EXPECT_DEATH and ASSERT_DEATH work when the error
309*b89a7cc2SEnji Cooper // message has a NUL character in it.
310*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, EmbeddedNulInMessage) {
311*b89a7cc2SEnji Cooper   EXPECT_DEATH(DieWithEmbeddedNul(), "my null world");
312*b89a7cc2SEnji Cooper   ASSERT_DEATH(DieWithEmbeddedNul(), "my null world");
313*b89a7cc2SEnji Cooper }
314*b89a7cc2SEnji Cooper 
315*b89a7cc2SEnji Cooper # endif  // GTEST_USES_PCRE
316*b89a7cc2SEnji Cooper 
317*b89a7cc2SEnji Cooper // Tests that death test macros expand to code which interacts well with switch
318*b89a7cc2SEnji Cooper // statements.
319*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, SwitchStatement) {
320*b89a7cc2SEnji Cooper   // Microsoft compiler usually complains about switch statements without
321*b89a7cc2SEnji Cooper   // case labels. We suppress that warning for this test.
322*b89a7cc2SEnji Cooper   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4065)
323*b89a7cc2SEnji Cooper 
324*b89a7cc2SEnji Cooper   switch (0)
325*b89a7cc2SEnji Cooper     default:
326*b89a7cc2SEnji Cooper       ASSERT_DEATH(_exit(1), "") << "exit in default switch handler";
327*b89a7cc2SEnji Cooper 
328*b89a7cc2SEnji Cooper   switch (0)
329*b89a7cc2SEnji Cooper     case 0:
330*b89a7cc2SEnji Cooper       EXPECT_DEATH(_exit(1), "") << "exit in switch case";
331*b89a7cc2SEnji Cooper 
332*b89a7cc2SEnji Cooper   GTEST_DISABLE_MSC_WARNINGS_POP_()
333*b89a7cc2SEnji Cooper }
334*b89a7cc2SEnji Cooper 
335*b89a7cc2SEnji Cooper // Tests that a static member function can be used in a "fast" style
336*b89a7cc2SEnji Cooper // death test.
337*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, StaticMemberFunctionFastStyle) {
338*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "fast";
339*b89a7cc2SEnji Cooper   ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember");
340*b89a7cc2SEnji Cooper }
341*b89a7cc2SEnji Cooper 
342*b89a7cc2SEnji Cooper // Tests that a method of the test fixture can be used in a "fast"
343*b89a7cc2SEnji Cooper // style death test.
344*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, MemberFunctionFastStyle) {
345*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "fast";
346*b89a7cc2SEnji Cooper   should_die_ = true;
347*b89a7cc2SEnji Cooper   EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction");
348*b89a7cc2SEnji Cooper }
349*b89a7cc2SEnji Cooper 
350*b89a7cc2SEnji Cooper void ChangeToRootDir() { posix::ChDir(GTEST_PATH_SEP_); }
351*b89a7cc2SEnji Cooper 
352*b89a7cc2SEnji Cooper // Tests that death tests work even if the current directory has been
353*b89a7cc2SEnji Cooper // changed.
354*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, FastDeathTestInChangedDir) {
355*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "fast";
356*b89a7cc2SEnji Cooper 
357*b89a7cc2SEnji Cooper   ChangeToRootDir();
358*b89a7cc2SEnji Cooper   EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
359*b89a7cc2SEnji Cooper 
360*b89a7cc2SEnji Cooper   ChangeToRootDir();
361*b89a7cc2SEnji Cooper   ASSERT_DEATH(_exit(1), "");
362*b89a7cc2SEnji Cooper }
363*b89a7cc2SEnji Cooper 
364*b89a7cc2SEnji Cooper # if GTEST_OS_LINUX
365*b89a7cc2SEnji Cooper void SigprofAction(int, siginfo_t*, void*) { /* no op */ }
366*b89a7cc2SEnji Cooper 
367*b89a7cc2SEnji Cooper // Sets SIGPROF action and ITIMER_PROF timer (interval: 1ms).
368*b89a7cc2SEnji Cooper void SetSigprofActionAndTimer() {
369*b89a7cc2SEnji Cooper   struct itimerval timer;
370*b89a7cc2SEnji Cooper   timer.it_interval.tv_sec = 0;
371*b89a7cc2SEnji Cooper   timer.it_interval.tv_usec = 1;
372*b89a7cc2SEnji Cooper   timer.it_value = timer.it_interval;
373*b89a7cc2SEnji Cooper   ASSERT_EQ(0, setitimer(ITIMER_PROF, &timer, NULL));
374*b89a7cc2SEnji Cooper   struct sigaction signal_action;
375*b89a7cc2SEnji Cooper   memset(&signal_action, 0, sizeof(signal_action));
376*b89a7cc2SEnji Cooper   sigemptyset(&signal_action.sa_mask);
377*b89a7cc2SEnji Cooper   signal_action.sa_sigaction = SigprofAction;
378*b89a7cc2SEnji Cooper   signal_action.sa_flags = SA_RESTART | SA_SIGINFO;
379*b89a7cc2SEnji Cooper   ASSERT_EQ(0, sigaction(SIGPROF, &signal_action, NULL));
380*b89a7cc2SEnji Cooper }
381*b89a7cc2SEnji Cooper 
382*b89a7cc2SEnji Cooper // Disables ITIMER_PROF timer and ignores SIGPROF signal.
383*b89a7cc2SEnji Cooper void DisableSigprofActionAndTimer(struct sigaction* old_signal_action) {
384*b89a7cc2SEnji Cooper   struct itimerval timer;
385*b89a7cc2SEnji Cooper   timer.it_interval.tv_sec = 0;
386*b89a7cc2SEnji Cooper   timer.it_interval.tv_usec = 0;
387*b89a7cc2SEnji Cooper   timer.it_value = timer.it_interval;
388*b89a7cc2SEnji Cooper   ASSERT_EQ(0, setitimer(ITIMER_PROF, &timer, NULL));
389*b89a7cc2SEnji Cooper   struct sigaction signal_action;
390*b89a7cc2SEnji Cooper   memset(&signal_action, 0, sizeof(signal_action));
391*b89a7cc2SEnji Cooper   sigemptyset(&signal_action.sa_mask);
392*b89a7cc2SEnji Cooper   signal_action.sa_handler = SIG_IGN;
393*b89a7cc2SEnji Cooper   ASSERT_EQ(0, sigaction(SIGPROF, &signal_action, old_signal_action));
394*b89a7cc2SEnji Cooper }
395*b89a7cc2SEnji Cooper 
396*b89a7cc2SEnji Cooper // Tests that death tests work when SIGPROF handler and timer are set.
397*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, FastSigprofActionSet) {
398*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "fast";
399*b89a7cc2SEnji Cooper   SetSigprofActionAndTimer();
400*b89a7cc2SEnji Cooper   EXPECT_DEATH(_exit(1), "");
401*b89a7cc2SEnji Cooper   struct sigaction old_signal_action;
402*b89a7cc2SEnji Cooper   DisableSigprofActionAndTimer(&old_signal_action);
403*b89a7cc2SEnji Cooper   EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction);
404*b89a7cc2SEnji Cooper }
405*b89a7cc2SEnji Cooper 
406*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ThreadSafeSigprofActionSet) {
407*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "threadsafe";
408*b89a7cc2SEnji Cooper   SetSigprofActionAndTimer();
409*b89a7cc2SEnji Cooper   EXPECT_DEATH(_exit(1), "");
410*b89a7cc2SEnji Cooper   struct sigaction old_signal_action;
411*b89a7cc2SEnji Cooper   DisableSigprofActionAndTimer(&old_signal_action);
412*b89a7cc2SEnji Cooper   EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction);
413*b89a7cc2SEnji Cooper }
414*b89a7cc2SEnji Cooper # endif  // GTEST_OS_LINUX
415*b89a7cc2SEnji Cooper 
416*b89a7cc2SEnji Cooper // Repeats a representative sample of death tests in the "threadsafe" style:
417*b89a7cc2SEnji Cooper 
418*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, StaticMemberFunctionThreadsafeStyle) {
419*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "threadsafe";
420*b89a7cc2SEnji Cooper   ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember");
421*b89a7cc2SEnji Cooper }
422*b89a7cc2SEnji Cooper 
423*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, MemberFunctionThreadsafeStyle) {
424*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "threadsafe";
425*b89a7cc2SEnji Cooper   should_die_ = true;
426*b89a7cc2SEnji Cooper   EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction");
427*b89a7cc2SEnji Cooper }
428*b89a7cc2SEnji Cooper 
429*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ThreadsafeDeathTestInLoop) {
430*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "threadsafe";
431*b89a7cc2SEnji Cooper 
432*b89a7cc2SEnji Cooper   for (int i = 0; i < 3; ++i)
433*b89a7cc2SEnji Cooper     EXPECT_EXIT(_exit(i), testing::ExitedWithCode(i), "") << ": i = " << i;
434*b89a7cc2SEnji Cooper }
435*b89a7cc2SEnji Cooper 
436*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ThreadsafeDeathTestInChangedDir) {
437*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "threadsafe";
438*b89a7cc2SEnji Cooper 
439*b89a7cc2SEnji Cooper   ChangeToRootDir();
440*b89a7cc2SEnji Cooper   EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
441*b89a7cc2SEnji Cooper 
442*b89a7cc2SEnji Cooper   ChangeToRootDir();
443*b89a7cc2SEnji Cooper   ASSERT_DEATH(_exit(1), "");
444*b89a7cc2SEnji Cooper }
445*b89a7cc2SEnji Cooper 
446*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, MixedStyles) {
447*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "threadsafe";
448*b89a7cc2SEnji Cooper   EXPECT_DEATH(_exit(1), "");
449*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "fast";
450*b89a7cc2SEnji Cooper   EXPECT_DEATH(_exit(1), "");
451*b89a7cc2SEnji Cooper }
452*b89a7cc2SEnji Cooper 
453*b89a7cc2SEnji Cooper # if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
454*b89a7cc2SEnji Cooper 
455*b89a7cc2SEnji Cooper namespace {
456*b89a7cc2SEnji Cooper 
457*b89a7cc2SEnji Cooper bool pthread_flag;
458*b89a7cc2SEnji Cooper 
459*b89a7cc2SEnji Cooper void SetPthreadFlag() {
460*b89a7cc2SEnji Cooper   pthread_flag = true;
461*b89a7cc2SEnji Cooper }
462*b89a7cc2SEnji Cooper 
463*b89a7cc2SEnji Cooper }  // namespace
464*b89a7cc2SEnji Cooper 
465*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DoesNotExecuteAtforkHooks) {
466*b89a7cc2SEnji Cooper   if (!testing::GTEST_FLAG(death_test_use_fork)) {
467*b89a7cc2SEnji Cooper     testing::GTEST_FLAG(death_test_style) = "threadsafe";
468*b89a7cc2SEnji Cooper     pthread_flag = false;
469*b89a7cc2SEnji Cooper     ASSERT_EQ(0, pthread_atfork(&SetPthreadFlag, NULL, NULL));
470*b89a7cc2SEnji Cooper     ASSERT_DEATH(_exit(1), "");
471*b89a7cc2SEnji Cooper     ASSERT_FALSE(pthread_flag);
472*b89a7cc2SEnji Cooper   }
473*b89a7cc2SEnji Cooper }
474*b89a7cc2SEnji Cooper 
475*b89a7cc2SEnji Cooper # endif  // GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
476*b89a7cc2SEnji Cooper 
477*b89a7cc2SEnji Cooper // Tests that a method of another class can be used in a death test.
478*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, MethodOfAnotherClass) {
479*b89a7cc2SEnji Cooper   const MayDie x(true);
480*b89a7cc2SEnji Cooper   ASSERT_DEATH(x.MemberFunction(), "MayDie\\:\\:MemberFunction");
481*b89a7cc2SEnji Cooper }
482*b89a7cc2SEnji Cooper 
483*b89a7cc2SEnji Cooper // Tests that a global function can be used in a death test.
484*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, GlobalFunction) {
485*b89a7cc2SEnji Cooper   EXPECT_DEATH(GlobalFunction(), "GlobalFunction");
486*b89a7cc2SEnji Cooper }
487*b89a7cc2SEnji Cooper 
488*b89a7cc2SEnji Cooper // Tests that any value convertible to an RE works as a second
489*b89a7cc2SEnji Cooper // argument to EXPECT_DEATH.
490*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AcceptsAnythingConvertibleToRE) {
491*b89a7cc2SEnji Cooper   static const char regex_c_str[] = "GlobalFunction";
492*b89a7cc2SEnji Cooper   EXPECT_DEATH(GlobalFunction(), regex_c_str);
493*b89a7cc2SEnji Cooper 
494*b89a7cc2SEnji Cooper   const testing::internal::RE regex(regex_c_str);
495*b89a7cc2SEnji Cooper   EXPECT_DEATH(GlobalFunction(), regex);
496*b89a7cc2SEnji Cooper 
497*b89a7cc2SEnji Cooper # if GTEST_HAS_GLOBAL_STRING
498*b89a7cc2SEnji Cooper 
499*b89a7cc2SEnji Cooper   const ::string regex_str(regex_c_str);
500*b89a7cc2SEnji Cooper   EXPECT_DEATH(GlobalFunction(), regex_str);
501*b89a7cc2SEnji Cooper 
502*b89a7cc2SEnji Cooper # endif  // GTEST_HAS_GLOBAL_STRING
503*b89a7cc2SEnji Cooper 
504*b89a7cc2SEnji Cooper # if !GTEST_USES_PCRE
505*b89a7cc2SEnji Cooper 
506*b89a7cc2SEnji Cooper   const ::std::string regex_std_str(regex_c_str);
507*b89a7cc2SEnji Cooper   EXPECT_DEATH(GlobalFunction(), regex_std_str);
508*b89a7cc2SEnji Cooper 
509*b89a7cc2SEnji Cooper # endif  // !GTEST_USES_PCRE
510*b89a7cc2SEnji Cooper }
511*b89a7cc2SEnji Cooper 
512*b89a7cc2SEnji Cooper // Tests that a non-void function can be used in a death test.
513*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, NonVoidFunction) {
514*b89a7cc2SEnji Cooper   ASSERT_DEATH(NonVoidFunction(), "NonVoidFunction");
515*b89a7cc2SEnji Cooper }
516*b89a7cc2SEnji Cooper 
517*b89a7cc2SEnji Cooper // Tests that functions that take parameter(s) can be used in a death test.
518*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, FunctionWithParameter) {
519*b89a7cc2SEnji Cooper   EXPECT_DEATH(DieIf(true), "DieIf\\(\\)");
520*b89a7cc2SEnji Cooper   EXPECT_DEATH(DieIfLessThan(2, 3), "DieIfLessThan");
521*b89a7cc2SEnji Cooper }
522*b89a7cc2SEnji Cooper 
523*b89a7cc2SEnji Cooper // Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture.
524*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, OutsideFixture) {
525*b89a7cc2SEnji Cooper   DeathTestSubroutine();
526*b89a7cc2SEnji Cooper }
527*b89a7cc2SEnji Cooper 
528*b89a7cc2SEnji Cooper // Tests that death tests can be done inside a loop.
529*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, InsideLoop) {
530*b89a7cc2SEnji Cooper   for (int i = 0; i < 5; i++) {
531*b89a7cc2SEnji Cooper     EXPECT_DEATH(DieIfLessThan(-1, i), "DieIfLessThan") << "where i == " << i;
532*b89a7cc2SEnji Cooper   }
533*b89a7cc2SEnji Cooper }
534*b89a7cc2SEnji Cooper 
535*b89a7cc2SEnji Cooper // Tests that a compound statement can be used in a death test.
536*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, CompoundStatement) {
537*b89a7cc2SEnji Cooper   EXPECT_DEATH({  // NOLINT
538*b89a7cc2SEnji Cooper     const int x = 2;
539*b89a7cc2SEnji Cooper     const int y = x + 1;
540*b89a7cc2SEnji Cooper     DieIfLessThan(x, y);
541*b89a7cc2SEnji Cooper   },
542*b89a7cc2SEnji Cooper   "DieIfLessThan");
543*b89a7cc2SEnji Cooper }
544*b89a7cc2SEnji Cooper 
545*b89a7cc2SEnji Cooper // Tests that code that doesn't die causes a death test to fail.
546*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DoesNotDie) {
547*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(DieIf(false), "DieIf"),
548*b89a7cc2SEnji Cooper                           "failed to die");
549*b89a7cc2SEnji Cooper }
550*b89a7cc2SEnji Cooper 
551*b89a7cc2SEnji Cooper // Tests that a death test fails when the error message isn't expected.
552*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ErrorMessageMismatch) {
553*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE({  // NOLINT
554*b89a7cc2SEnji Cooper     EXPECT_DEATH(DieIf(true), "DieIfLessThan") << "End of death test message.";
555*b89a7cc2SEnji Cooper   }, "died but not with expected error");
556*b89a7cc2SEnji Cooper }
557*b89a7cc2SEnji Cooper 
558*b89a7cc2SEnji Cooper // On exit, *aborted will be true iff the EXPECT_DEATH() statement
559*b89a7cc2SEnji Cooper // aborted the function.
560*b89a7cc2SEnji Cooper void ExpectDeathTestHelper(bool* aborted) {
561*b89a7cc2SEnji Cooper   *aborted = true;
562*b89a7cc2SEnji Cooper   EXPECT_DEATH(DieIf(false), "DieIf");  // This assertion should fail.
563*b89a7cc2SEnji Cooper   *aborted = false;
564*b89a7cc2SEnji Cooper }
565*b89a7cc2SEnji Cooper 
566*b89a7cc2SEnji Cooper // Tests that EXPECT_DEATH doesn't abort the test on failure.
567*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, EXPECT_DEATH) {
568*b89a7cc2SEnji Cooper   bool aborted = true;
569*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(ExpectDeathTestHelper(&aborted),
570*b89a7cc2SEnji Cooper                           "failed to die");
571*b89a7cc2SEnji Cooper   EXPECT_FALSE(aborted);
572*b89a7cc2SEnji Cooper }
573*b89a7cc2SEnji Cooper 
574*b89a7cc2SEnji Cooper // Tests that ASSERT_DEATH does abort the test on failure.
575*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ASSERT_DEATH) {
576*b89a7cc2SEnji Cooper   static bool aborted;
577*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE({  // NOLINT
578*b89a7cc2SEnji Cooper     aborted = true;
579*b89a7cc2SEnji Cooper     ASSERT_DEATH(DieIf(false), "DieIf");  // This assertion should fail.
580*b89a7cc2SEnji Cooper     aborted = false;
581*b89a7cc2SEnji Cooper   }, "failed to die");
582*b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
583*b89a7cc2SEnji Cooper }
584*b89a7cc2SEnji Cooper 
585*b89a7cc2SEnji Cooper // Tests that EXPECT_DEATH evaluates the arguments exactly once.
586*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, SingleEvaluation) {
587*b89a7cc2SEnji Cooper   int x = 3;
588*b89a7cc2SEnji Cooper   EXPECT_DEATH(DieIf((++x) == 4), "DieIf");
589*b89a7cc2SEnji Cooper 
590*b89a7cc2SEnji Cooper   const char* regex = "DieIf";
591*b89a7cc2SEnji Cooper   const char* regex_save = regex;
592*b89a7cc2SEnji Cooper   EXPECT_DEATH(DieIfLessThan(3, 4), regex++);
593*b89a7cc2SEnji Cooper   EXPECT_EQ(regex_save + 1, regex);
594*b89a7cc2SEnji Cooper }
595*b89a7cc2SEnji Cooper 
596*b89a7cc2SEnji Cooper // Tests that run-away death tests are reported as failures.
597*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, RunawayIsFailure) {
598*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(static_cast<void>(0), "Foo"),
599*b89a7cc2SEnji Cooper                           "failed to die.");
600*b89a7cc2SEnji Cooper }
601*b89a7cc2SEnji Cooper 
602*b89a7cc2SEnji Cooper // Tests that death tests report executing 'return' in the statement as
603*b89a7cc2SEnji Cooper // failure.
604*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ReturnIsFailure) {
605*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(ASSERT_DEATH(return, "Bar"),
606*b89a7cc2SEnji Cooper                        "illegal return in test statement.");
607*b89a7cc2SEnji Cooper }
608*b89a7cc2SEnji Cooper 
609*b89a7cc2SEnji Cooper // Tests that EXPECT_DEBUG_DEATH works as expected, that is, you can stream a
610*b89a7cc2SEnji Cooper // message to it, and in debug mode it:
611*b89a7cc2SEnji Cooper // 1. Asserts on death.
612*b89a7cc2SEnji Cooper // 2. Has no side effect.
613*b89a7cc2SEnji Cooper //
614*b89a7cc2SEnji Cooper // And in opt mode, it:
615*b89a7cc2SEnji Cooper // 1.  Has side effects but does not assert.
616*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, TestExpectDebugDeath) {
617*b89a7cc2SEnji Cooper   int sideeffect = 0;
618*b89a7cc2SEnji Cooper 
619*b89a7cc2SEnji Cooper   // Put the regex in a local variable to make sure we don't get an "unused"
620*b89a7cc2SEnji Cooper   // warning in opt mode.
621*b89a7cc2SEnji Cooper   const char* regex = "death.*DieInDebugElse12";
622*b89a7cc2SEnji Cooper 
623*b89a7cc2SEnji Cooper   EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), regex)
624*b89a7cc2SEnji Cooper       << "Must accept a streamed message";
625*b89a7cc2SEnji Cooper 
626*b89a7cc2SEnji Cooper # ifdef NDEBUG
627*b89a7cc2SEnji Cooper 
628*b89a7cc2SEnji Cooper   // Checks that the assignment occurs in opt mode (sideeffect).
629*b89a7cc2SEnji Cooper   EXPECT_EQ(12, sideeffect);
630*b89a7cc2SEnji Cooper 
631*b89a7cc2SEnji Cooper # else
632*b89a7cc2SEnji Cooper 
633*b89a7cc2SEnji Cooper   // Checks that the assignment does not occur in dbg mode (no sideeffect).
634*b89a7cc2SEnji Cooper   EXPECT_EQ(0, sideeffect);
635*b89a7cc2SEnji Cooper 
636*b89a7cc2SEnji Cooper # endif
637*b89a7cc2SEnji Cooper }
638*b89a7cc2SEnji Cooper 
639*b89a7cc2SEnji Cooper // Tests that ASSERT_DEBUG_DEATH works as expected, that is, you can stream a
640*b89a7cc2SEnji Cooper // message to it, and in debug mode it:
641*b89a7cc2SEnji Cooper // 1. Asserts on death.
642*b89a7cc2SEnji Cooper // 2. Has no side effect.
643*b89a7cc2SEnji Cooper //
644*b89a7cc2SEnji Cooper // And in opt mode, it:
645*b89a7cc2SEnji Cooper // 1.  Has side effects but does not assert.
646*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, TestAssertDebugDeath) {
647*b89a7cc2SEnji Cooper   int sideeffect = 0;
648*b89a7cc2SEnji Cooper 
649*b89a7cc2SEnji Cooper   ASSERT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), "death.*DieInDebugElse12")
650*b89a7cc2SEnji Cooper       << "Must accept a streamed message";
651*b89a7cc2SEnji Cooper 
652*b89a7cc2SEnji Cooper # ifdef NDEBUG
653*b89a7cc2SEnji Cooper 
654*b89a7cc2SEnji Cooper   // Checks that the assignment occurs in opt mode (sideeffect).
655*b89a7cc2SEnji Cooper   EXPECT_EQ(12, sideeffect);
656*b89a7cc2SEnji Cooper 
657*b89a7cc2SEnji Cooper # else
658*b89a7cc2SEnji Cooper 
659*b89a7cc2SEnji Cooper   // Checks that the assignment does not occur in dbg mode (no sideeffect).
660*b89a7cc2SEnji Cooper   EXPECT_EQ(0, sideeffect);
661*b89a7cc2SEnji Cooper 
662*b89a7cc2SEnji Cooper # endif
663*b89a7cc2SEnji Cooper }
664*b89a7cc2SEnji Cooper 
665*b89a7cc2SEnji Cooper # ifndef NDEBUG
666*b89a7cc2SEnji Cooper 
667*b89a7cc2SEnji Cooper void ExpectDebugDeathHelper(bool* aborted) {
668*b89a7cc2SEnji Cooper   *aborted = true;
669*b89a7cc2SEnji Cooper   EXPECT_DEBUG_DEATH(return, "") << "This is expected to fail.";
670*b89a7cc2SEnji Cooper   *aborted = false;
671*b89a7cc2SEnji Cooper }
672*b89a7cc2SEnji Cooper 
673*b89a7cc2SEnji Cooper #  if GTEST_OS_WINDOWS
674*b89a7cc2SEnji Cooper TEST(PopUpDeathTest, DoesNotShowPopUpOnAbort) {
675*b89a7cc2SEnji Cooper   printf("This test should be considered failing if it shows "
676*b89a7cc2SEnji Cooper          "any pop-up dialogs.\n");
677*b89a7cc2SEnji Cooper   fflush(stdout);
678*b89a7cc2SEnji Cooper 
679*b89a7cc2SEnji Cooper   EXPECT_DEATH({
680*b89a7cc2SEnji Cooper     testing::GTEST_FLAG(catch_exceptions) = false;
681*b89a7cc2SEnji Cooper     abort();
682*b89a7cc2SEnji Cooper   }, "");
683*b89a7cc2SEnji Cooper }
684*b89a7cc2SEnji Cooper #  endif  // GTEST_OS_WINDOWS
685*b89a7cc2SEnji Cooper 
686*b89a7cc2SEnji Cooper // Tests that EXPECT_DEBUG_DEATH in debug mode does not abort
687*b89a7cc2SEnji Cooper // the function.
688*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ExpectDebugDeathDoesNotAbort) {
689*b89a7cc2SEnji Cooper   bool aborted = true;
690*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(ExpectDebugDeathHelper(&aborted), "");
691*b89a7cc2SEnji Cooper   EXPECT_FALSE(aborted);
692*b89a7cc2SEnji Cooper }
693*b89a7cc2SEnji Cooper 
694*b89a7cc2SEnji Cooper void AssertDebugDeathHelper(bool* aborted) {
695*b89a7cc2SEnji Cooper   *aborted = true;
696*b89a7cc2SEnji Cooper   GTEST_LOG_(INFO) << "Before ASSERT_DEBUG_DEATH";
697*b89a7cc2SEnji Cooper   ASSERT_DEBUG_DEATH(GTEST_LOG_(INFO) << "In ASSERT_DEBUG_DEATH"; return, "")
698*b89a7cc2SEnji Cooper       << "This is expected to fail.";
699*b89a7cc2SEnji Cooper   GTEST_LOG_(INFO) << "After ASSERT_DEBUG_DEATH";
700*b89a7cc2SEnji Cooper   *aborted = false;
701*b89a7cc2SEnji Cooper }
702*b89a7cc2SEnji Cooper 
703*b89a7cc2SEnji Cooper // Tests that ASSERT_DEBUG_DEATH in debug mode aborts the function on
704*b89a7cc2SEnji Cooper // failure.
705*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts) {
706*b89a7cc2SEnji Cooper   static bool aborted;
707*b89a7cc2SEnji Cooper   aborted = false;
708*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
709*b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
710*b89a7cc2SEnji Cooper }
711*b89a7cc2SEnji Cooper 
712*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts2) {
713*b89a7cc2SEnji Cooper   static bool aborted;
714*b89a7cc2SEnji Cooper   aborted = false;
715*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
716*b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
717*b89a7cc2SEnji Cooper }
718*b89a7cc2SEnji Cooper 
719*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts3) {
720*b89a7cc2SEnji Cooper   static bool aborted;
721*b89a7cc2SEnji Cooper   aborted = false;
722*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
723*b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
724*b89a7cc2SEnji Cooper }
725*b89a7cc2SEnji Cooper 
726*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts4) {
727*b89a7cc2SEnji Cooper   static bool aborted;
728*b89a7cc2SEnji Cooper   aborted = false;
729*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
730*b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
731*b89a7cc2SEnji Cooper }
732*b89a7cc2SEnji Cooper 
733*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts5) {
734*b89a7cc2SEnji Cooper   static bool aborted;
735*b89a7cc2SEnji Cooper   aborted = false;
736*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
737*b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
738*b89a7cc2SEnji Cooper }
739*b89a7cc2SEnji Cooper 
740*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts6) {
741*b89a7cc2SEnji Cooper   static bool aborted;
742*b89a7cc2SEnji Cooper   aborted = false;
743*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
744*b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
745*b89a7cc2SEnji Cooper }
746*b89a7cc2SEnji Cooper 
747*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts7) {
748*b89a7cc2SEnji Cooper   static bool aborted;
749*b89a7cc2SEnji Cooper   aborted = false;
750*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
751*b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
752*b89a7cc2SEnji Cooper }
753*b89a7cc2SEnji Cooper 
754*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts8) {
755*b89a7cc2SEnji Cooper   static bool aborted;
756*b89a7cc2SEnji Cooper   aborted = false;
757*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
758*b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
759*b89a7cc2SEnji Cooper }
760*b89a7cc2SEnji Cooper 
761*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts9) {
762*b89a7cc2SEnji Cooper   static bool aborted;
763*b89a7cc2SEnji Cooper   aborted = false;
764*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
765*b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
766*b89a7cc2SEnji Cooper }
767*b89a7cc2SEnji Cooper 
768*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, AssertDebugDeathAborts10) {
769*b89a7cc2SEnji Cooper   static bool aborted;
770*b89a7cc2SEnji Cooper   aborted = false;
771*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
772*b89a7cc2SEnji Cooper   EXPECT_TRUE(aborted);
773*b89a7cc2SEnji Cooper }
774*b89a7cc2SEnji Cooper 
775*b89a7cc2SEnji Cooper # endif  // _NDEBUG
776*b89a7cc2SEnji Cooper 
777*b89a7cc2SEnji Cooper // Tests the *_EXIT family of macros, using a variety of predicates.
778*b89a7cc2SEnji Cooper static void TestExitMacros() {
779*b89a7cc2SEnji Cooper   EXPECT_EXIT(_exit(1),  testing::ExitedWithCode(1),  "");
780*b89a7cc2SEnji Cooper   ASSERT_EXIT(_exit(42), testing::ExitedWithCode(42), "");
781*b89a7cc2SEnji Cooper 
782*b89a7cc2SEnji Cooper # if GTEST_OS_WINDOWS
783*b89a7cc2SEnji Cooper 
784*b89a7cc2SEnji Cooper   // Of all signals effects on the process exit code, only those of SIGABRT
785*b89a7cc2SEnji Cooper   // are documented on Windows.
786*b89a7cc2SEnji Cooper   // See https://msdn.microsoft.com/en-us/query-bi/m/dwwzkt4c.
787*b89a7cc2SEnji Cooper   EXPECT_EXIT(raise(SIGABRT), testing::ExitedWithCode(3), "") << "b_ar";
788*b89a7cc2SEnji Cooper 
789*b89a7cc2SEnji Cooper # elif !GTEST_OS_FUCHSIA
790*b89a7cc2SEnji Cooper 
791*b89a7cc2SEnji Cooper   // Fuchsia has no unix signals.
792*b89a7cc2SEnji Cooper   EXPECT_EXIT(raise(SIGKILL), testing::KilledBySignal(SIGKILL), "") << "foo";
793*b89a7cc2SEnji Cooper   ASSERT_EXIT(raise(SIGUSR2), testing::KilledBySignal(SIGUSR2), "") << "bar";
794*b89a7cc2SEnji Cooper 
795*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE({  // NOLINT
796*b89a7cc2SEnji Cooper     ASSERT_EXIT(_exit(0), testing::KilledBySignal(SIGSEGV), "")
797*b89a7cc2SEnji Cooper       << "This failure is expected, too.";
798*b89a7cc2SEnji Cooper   }, "This failure is expected, too.");
799*b89a7cc2SEnji Cooper 
800*b89a7cc2SEnji Cooper # endif  // GTEST_OS_WINDOWS
801*b89a7cc2SEnji Cooper 
802*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE({  // NOLINT
803*b89a7cc2SEnji Cooper     EXPECT_EXIT(raise(SIGSEGV), testing::ExitedWithCode(0), "")
804*b89a7cc2SEnji Cooper       << "This failure is expected.";
805*b89a7cc2SEnji Cooper   }, "This failure is expected.");
806*b89a7cc2SEnji Cooper }
807*b89a7cc2SEnji Cooper 
808*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ExitMacros) {
809*b89a7cc2SEnji Cooper   TestExitMacros();
810*b89a7cc2SEnji Cooper }
811*b89a7cc2SEnji Cooper 
812*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, ExitMacrosUsingFork) {
813*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_use_fork) = true;
814*b89a7cc2SEnji Cooper   TestExitMacros();
815*b89a7cc2SEnji Cooper }
816*b89a7cc2SEnji Cooper 
817*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, InvalidStyle) {
818*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "rococo";
819*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE({  // NOLINT
820*b89a7cc2SEnji Cooper     EXPECT_DEATH(_exit(0), "") << "This failure is expected.";
821*b89a7cc2SEnji Cooper   }, "This failure is expected.");
822*b89a7cc2SEnji Cooper }
823*b89a7cc2SEnji Cooper 
824*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DeathTestFailedOutput) {
825*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "fast";
826*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
827*b89a7cc2SEnji Cooper       EXPECT_DEATH(DieWithMessage("death\n"),
828*b89a7cc2SEnji Cooper                    "expected message"),
829*b89a7cc2SEnji Cooper       "Actual msg:\n"
830*b89a7cc2SEnji Cooper       "[  DEATH   ] death\n");
831*b89a7cc2SEnji Cooper }
832*b89a7cc2SEnji Cooper 
833*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DeathTestUnexpectedReturnOutput) {
834*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "fast";
835*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
836*b89a7cc2SEnji Cooper       EXPECT_DEATH({
837*b89a7cc2SEnji Cooper           fprintf(stderr, "returning\n");
838*b89a7cc2SEnji Cooper           fflush(stderr);
839*b89a7cc2SEnji Cooper           return;
840*b89a7cc2SEnji Cooper         }, ""),
841*b89a7cc2SEnji Cooper       "    Result: illegal return in test statement.\n"
842*b89a7cc2SEnji Cooper       " Error msg:\n"
843*b89a7cc2SEnji Cooper       "[  DEATH   ] returning\n");
844*b89a7cc2SEnji Cooper }
845*b89a7cc2SEnji Cooper 
846*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DeathTestBadExitCodeOutput) {
847*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "fast";
848*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
849*b89a7cc2SEnji Cooper       EXPECT_EXIT(DieWithMessage("exiting with rc 1\n"),
850*b89a7cc2SEnji Cooper                   testing::ExitedWithCode(3),
851*b89a7cc2SEnji Cooper                   "expected message"),
852*b89a7cc2SEnji Cooper       "    Result: died but not with expected exit code:\n"
853*b89a7cc2SEnji Cooper       "            Exited with exit status 1\n"
854*b89a7cc2SEnji Cooper       "Actual msg:\n"
855*b89a7cc2SEnji Cooper       "[  DEATH   ] exiting with rc 1\n");
856*b89a7cc2SEnji Cooper }
857*b89a7cc2SEnji Cooper 
858*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DeathTestMultiLineMatchFail) {
859*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "fast";
860*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
861*b89a7cc2SEnji Cooper       EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"),
862*b89a7cc2SEnji Cooper                    "line 1\nxyz\nline 3\n"),
863*b89a7cc2SEnji Cooper       "Actual msg:\n"
864*b89a7cc2SEnji Cooper       "[  DEATH   ] line 1\n"
865*b89a7cc2SEnji Cooper       "[  DEATH   ] line 2\n"
866*b89a7cc2SEnji Cooper       "[  DEATH   ] line 3\n");
867*b89a7cc2SEnji Cooper }
868*b89a7cc2SEnji Cooper 
869*b89a7cc2SEnji Cooper TEST_F(TestForDeathTest, DeathTestMultiLineMatchPass) {
870*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "fast";
871*b89a7cc2SEnji Cooper   EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"),
872*b89a7cc2SEnji Cooper                "line 1\nline 2\nline 3\n");
873*b89a7cc2SEnji Cooper }
874*b89a7cc2SEnji Cooper 
875*b89a7cc2SEnji Cooper // A DeathTestFactory that returns MockDeathTests.
876*b89a7cc2SEnji Cooper class MockDeathTestFactory : public DeathTestFactory {
877*b89a7cc2SEnji Cooper  public:
878*b89a7cc2SEnji Cooper   MockDeathTestFactory();
879*b89a7cc2SEnji Cooper   virtual bool Create(const char* statement,
880*b89a7cc2SEnji Cooper                       const ::testing::internal::RE* regex,
881*b89a7cc2SEnji Cooper                       const char* file, int line, DeathTest** test);
882*b89a7cc2SEnji Cooper 
883*b89a7cc2SEnji Cooper   // Sets the parameters for subsequent calls to Create.
884*b89a7cc2SEnji Cooper   void SetParameters(bool create, DeathTest::TestRole role,
885*b89a7cc2SEnji Cooper                      int status, bool passed);
886*b89a7cc2SEnji Cooper 
887*b89a7cc2SEnji Cooper   // Accessors.
888*b89a7cc2SEnji Cooper   int AssumeRoleCalls() const { return assume_role_calls_; }
889*b89a7cc2SEnji Cooper   int WaitCalls() const { return wait_calls_; }
890*b89a7cc2SEnji Cooper   size_t PassedCalls() const { return passed_args_.size(); }
891*b89a7cc2SEnji Cooper   bool PassedArgument(int n) const { return passed_args_[n]; }
892*b89a7cc2SEnji Cooper   size_t AbortCalls() const { return abort_args_.size(); }
893*b89a7cc2SEnji Cooper   DeathTest::AbortReason AbortArgument(int n) const {
894*b89a7cc2SEnji Cooper     return abort_args_[n];
895*b89a7cc2SEnji Cooper   }
896*b89a7cc2SEnji Cooper   bool TestDeleted() const { return test_deleted_; }
897*b89a7cc2SEnji Cooper 
898*b89a7cc2SEnji Cooper  private:
899*b89a7cc2SEnji Cooper   friend class MockDeathTest;
900*b89a7cc2SEnji Cooper   // If true, Create will return a MockDeathTest; otherwise it returns
901*b89a7cc2SEnji Cooper   // NULL.
902*b89a7cc2SEnji Cooper   bool create_;
903*b89a7cc2SEnji Cooper   // The value a MockDeathTest will return from its AssumeRole method.
904*b89a7cc2SEnji Cooper   DeathTest::TestRole role_;
905*b89a7cc2SEnji Cooper   // The value a MockDeathTest will return from its Wait method.
906*b89a7cc2SEnji Cooper   int status_;
907*b89a7cc2SEnji Cooper   // The value a MockDeathTest will return from its Passed method.
908*b89a7cc2SEnji Cooper   bool passed_;
909*b89a7cc2SEnji Cooper 
910*b89a7cc2SEnji Cooper   // Number of times AssumeRole was called.
911*b89a7cc2SEnji Cooper   int assume_role_calls_;
912*b89a7cc2SEnji Cooper   // Number of times Wait was called.
913*b89a7cc2SEnji Cooper   int wait_calls_;
914*b89a7cc2SEnji Cooper   // The arguments to the calls to Passed since the last call to
915*b89a7cc2SEnji Cooper   // SetParameters.
916*b89a7cc2SEnji Cooper   std::vector<bool> passed_args_;
917*b89a7cc2SEnji Cooper   // The arguments to the calls to Abort since the last call to
918*b89a7cc2SEnji Cooper   // SetParameters.
919*b89a7cc2SEnji Cooper   std::vector<DeathTest::AbortReason> abort_args_;
920*b89a7cc2SEnji Cooper   // True if the last MockDeathTest returned by Create has been
921*b89a7cc2SEnji Cooper   // deleted.
922*b89a7cc2SEnji Cooper   bool test_deleted_;
923*b89a7cc2SEnji Cooper };
924*b89a7cc2SEnji Cooper 
925*b89a7cc2SEnji Cooper 
926*b89a7cc2SEnji Cooper // A DeathTest implementation useful in testing.  It returns values set
927*b89a7cc2SEnji Cooper // at its creation from its various inherited DeathTest methods, and
928*b89a7cc2SEnji Cooper // reports calls to those methods to its parent MockDeathTestFactory
929*b89a7cc2SEnji Cooper // object.
930*b89a7cc2SEnji Cooper class MockDeathTest : public DeathTest {
931*b89a7cc2SEnji Cooper  public:
932*b89a7cc2SEnji Cooper   MockDeathTest(MockDeathTestFactory *parent,
933*b89a7cc2SEnji Cooper                 TestRole role, int status, bool passed) :
934*b89a7cc2SEnji Cooper       parent_(parent), role_(role), status_(status), passed_(passed) {
935*b89a7cc2SEnji Cooper   }
936*b89a7cc2SEnji Cooper   virtual ~MockDeathTest() {
937*b89a7cc2SEnji Cooper     parent_->test_deleted_ = true;
938*b89a7cc2SEnji Cooper   }
939*b89a7cc2SEnji Cooper   virtual TestRole AssumeRole() {
940*b89a7cc2SEnji Cooper     ++parent_->assume_role_calls_;
941*b89a7cc2SEnji Cooper     return role_;
942*b89a7cc2SEnji Cooper   }
943*b89a7cc2SEnji Cooper   virtual int Wait() {
944*b89a7cc2SEnji Cooper     ++parent_->wait_calls_;
945*b89a7cc2SEnji Cooper     return status_;
946*b89a7cc2SEnji Cooper   }
947*b89a7cc2SEnji Cooper   virtual bool Passed(bool exit_status_ok) {
948*b89a7cc2SEnji Cooper     parent_->passed_args_.push_back(exit_status_ok);
949*b89a7cc2SEnji Cooper     return passed_;
950*b89a7cc2SEnji Cooper   }
951*b89a7cc2SEnji Cooper   virtual void Abort(AbortReason reason) {
952*b89a7cc2SEnji Cooper     parent_->abort_args_.push_back(reason);
953*b89a7cc2SEnji Cooper   }
954*b89a7cc2SEnji Cooper 
955*b89a7cc2SEnji Cooper  private:
956*b89a7cc2SEnji Cooper   MockDeathTestFactory* const parent_;
957*b89a7cc2SEnji Cooper   const TestRole role_;
958*b89a7cc2SEnji Cooper   const int status_;
959*b89a7cc2SEnji Cooper   const bool passed_;
960*b89a7cc2SEnji Cooper };
961*b89a7cc2SEnji Cooper 
962*b89a7cc2SEnji Cooper 
963*b89a7cc2SEnji Cooper // MockDeathTestFactory constructor.
964*b89a7cc2SEnji Cooper MockDeathTestFactory::MockDeathTestFactory()
965*b89a7cc2SEnji Cooper     : create_(true),
966*b89a7cc2SEnji Cooper       role_(DeathTest::OVERSEE_TEST),
967*b89a7cc2SEnji Cooper       status_(0),
968*b89a7cc2SEnji Cooper       passed_(true),
969*b89a7cc2SEnji Cooper       assume_role_calls_(0),
970*b89a7cc2SEnji Cooper       wait_calls_(0),
971*b89a7cc2SEnji Cooper       passed_args_(),
972*b89a7cc2SEnji Cooper       abort_args_() {
973*b89a7cc2SEnji Cooper }
974*b89a7cc2SEnji Cooper 
975*b89a7cc2SEnji Cooper 
976*b89a7cc2SEnji Cooper // Sets the parameters for subsequent calls to Create.
977*b89a7cc2SEnji Cooper void MockDeathTestFactory::SetParameters(bool create,
978*b89a7cc2SEnji Cooper                                          DeathTest::TestRole role,
979*b89a7cc2SEnji Cooper                                          int status, bool passed) {
980*b89a7cc2SEnji Cooper   create_ = create;
981*b89a7cc2SEnji Cooper   role_ = role;
982*b89a7cc2SEnji Cooper   status_ = status;
983*b89a7cc2SEnji Cooper   passed_ = passed;
984*b89a7cc2SEnji Cooper 
985*b89a7cc2SEnji Cooper   assume_role_calls_ = 0;
986*b89a7cc2SEnji Cooper   wait_calls_ = 0;
987*b89a7cc2SEnji Cooper   passed_args_.clear();
988*b89a7cc2SEnji Cooper   abort_args_.clear();
989*b89a7cc2SEnji Cooper }
990*b89a7cc2SEnji Cooper 
991*b89a7cc2SEnji Cooper 
992*b89a7cc2SEnji Cooper // Sets test to NULL (if create_ is false) or to the address of a new
993*b89a7cc2SEnji Cooper // MockDeathTest object with parameters taken from the last call
994*b89a7cc2SEnji Cooper // to SetParameters (if create_ is true).  Always returns true.
995*b89a7cc2SEnji Cooper bool MockDeathTestFactory::Create(const char* /*statement*/,
996*b89a7cc2SEnji Cooper                                   const ::testing::internal::RE* /*regex*/,
997*b89a7cc2SEnji Cooper                                   const char* /*file*/,
998*b89a7cc2SEnji Cooper                                   int /*line*/,
999*b89a7cc2SEnji Cooper                                   DeathTest** test) {
1000*b89a7cc2SEnji Cooper   test_deleted_ = false;
1001*b89a7cc2SEnji Cooper   if (create_) {
1002*b89a7cc2SEnji Cooper     *test = new MockDeathTest(this, role_, status_, passed_);
1003*b89a7cc2SEnji Cooper   } else {
1004*b89a7cc2SEnji Cooper     *test = NULL;
1005*b89a7cc2SEnji Cooper   }
1006*b89a7cc2SEnji Cooper   return true;
1007*b89a7cc2SEnji Cooper }
1008*b89a7cc2SEnji Cooper 
1009*b89a7cc2SEnji Cooper // A test fixture for testing the logic of the GTEST_DEATH_TEST_ macro.
1010*b89a7cc2SEnji Cooper // It installs a MockDeathTestFactory that is used for the duration
1011*b89a7cc2SEnji Cooper // of the test case.
1012*b89a7cc2SEnji Cooper class MacroLogicDeathTest : public testing::Test {
1013*b89a7cc2SEnji Cooper  protected:
1014*b89a7cc2SEnji Cooper   static testing::internal::ReplaceDeathTestFactory* replacer_;
1015*b89a7cc2SEnji Cooper   static MockDeathTestFactory* factory_;
1016*b89a7cc2SEnji Cooper 
1017*b89a7cc2SEnji Cooper   static void SetUpTestCase() {
1018*b89a7cc2SEnji Cooper     factory_ = new MockDeathTestFactory;
1019*b89a7cc2SEnji Cooper     replacer_ = new testing::internal::ReplaceDeathTestFactory(factory_);
1020*b89a7cc2SEnji Cooper   }
1021*b89a7cc2SEnji Cooper 
1022*b89a7cc2SEnji Cooper   static void TearDownTestCase() {
1023*b89a7cc2SEnji Cooper     delete replacer_;
1024*b89a7cc2SEnji Cooper     replacer_ = NULL;
1025*b89a7cc2SEnji Cooper     delete factory_;
1026*b89a7cc2SEnji Cooper     factory_ = NULL;
1027*b89a7cc2SEnji Cooper   }
1028*b89a7cc2SEnji Cooper 
1029*b89a7cc2SEnji Cooper   // Runs a death test that breaks the rules by returning.  Such a death
1030*b89a7cc2SEnji Cooper   // test cannot be run directly from a test routine that uses a
1031*b89a7cc2SEnji Cooper   // MockDeathTest, or the remainder of the routine will not be executed.
1032*b89a7cc2SEnji Cooper   static void RunReturningDeathTest(bool* flag) {
1033*b89a7cc2SEnji Cooper     ASSERT_DEATH({  // NOLINT
1034*b89a7cc2SEnji Cooper       *flag = true;
1035*b89a7cc2SEnji Cooper       return;
1036*b89a7cc2SEnji Cooper     }, "");
1037*b89a7cc2SEnji Cooper   }
1038*b89a7cc2SEnji Cooper };
1039*b89a7cc2SEnji Cooper 
1040*b89a7cc2SEnji Cooper testing::internal::ReplaceDeathTestFactory* MacroLogicDeathTest::replacer_
1041*b89a7cc2SEnji Cooper     = NULL;
1042*b89a7cc2SEnji Cooper MockDeathTestFactory* MacroLogicDeathTest::factory_ = NULL;
1043*b89a7cc2SEnji Cooper 
1044*b89a7cc2SEnji Cooper 
1045*b89a7cc2SEnji Cooper // Test that nothing happens when the factory doesn't return a DeathTest:
1046*b89a7cc2SEnji Cooper TEST_F(MacroLogicDeathTest, NothingHappens) {
1047*b89a7cc2SEnji Cooper   bool flag = false;
1048*b89a7cc2SEnji Cooper   factory_->SetParameters(false, DeathTest::OVERSEE_TEST, 0, true);
1049*b89a7cc2SEnji Cooper   EXPECT_DEATH(flag = true, "");
1050*b89a7cc2SEnji Cooper   EXPECT_FALSE(flag);
1051*b89a7cc2SEnji Cooper   EXPECT_EQ(0, factory_->AssumeRoleCalls());
1052*b89a7cc2SEnji Cooper   EXPECT_EQ(0, factory_->WaitCalls());
1053*b89a7cc2SEnji Cooper   EXPECT_EQ(0U, factory_->PassedCalls());
1054*b89a7cc2SEnji Cooper   EXPECT_EQ(0U, factory_->AbortCalls());
1055*b89a7cc2SEnji Cooper   EXPECT_FALSE(factory_->TestDeleted());
1056*b89a7cc2SEnji Cooper }
1057*b89a7cc2SEnji Cooper 
1058*b89a7cc2SEnji Cooper // Test that the parent process doesn't run the death test code,
1059*b89a7cc2SEnji Cooper // and that the Passed method returns false when the (simulated)
1060*b89a7cc2SEnji Cooper // child process exits with status 0:
1061*b89a7cc2SEnji Cooper TEST_F(MacroLogicDeathTest, ChildExitsSuccessfully) {
1062*b89a7cc2SEnji Cooper   bool flag = false;
1063*b89a7cc2SEnji Cooper   factory_->SetParameters(true, DeathTest::OVERSEE_TEST, 0, true);
1064*b89a7cc2SEnji Cooper   EXPECT_DEATH(flag = true, "");
1065*b89a7cc2SEnji Cooper   EXPECT_FALSE(flag);
1066*b89a7cc2SEnji Cooper   EXPECT_EQ(1, factory_->AssumeRoleCalls());
1067*b89a7cc2SEnji Cooper   EXPECT_EQ(1, factory_->WaitCalls());
1068*b89a7cc2SEnji Cooper   ASSERT_EQ(1U, factory_->PassedCalls());
1069*b89a7cc2SEnji Cooper   EXPECT_FALSE(factory_->PassedArgument(0));
1070*b89a7cc2SEnji Cooper   EXPECT_EQ(0U, factory_->AbortCalls());
1071*b89a7cc2SEnji Cooper   EXPECT_TRUE(factory_->TestDeleted());
1072*b89a7cc2SEnji Cooper }
1073*b89a7cc2SEnji Cooper 
1074*b89a7cc2SEnji Cooper // Tests that the Passed method was given the argument "true" when
1075*b89a7cc2SEnji Cooper // the (simulated) child process exits with status 1:
1076*b89a7cc2SEnji Cooper TEST_F(MacroLogicDeathTest, ChildExitsUnsuccessfully) {
1077*b89a7cc2SEnji Cooper   bool flag = false;
1078*b89a7cc2SEnji Cooper   factory_->SetParameters(true, DeathTest::OVERSEE_TEST, 1, true);
1079*b89a7cc2SEnji Cooper   EXPECT_DEATH(flag = true, "");
1080*b89a7cc2SEnji Cooper   EXPECT_FALSE(flag);
1081*b89a7cc2SEnji Cooper   EXPECT_EQ(1, factory_->AssumeRoleCalls());
1082*b89a7cc2SEnji Cooper   EXPECT_EQ(1, factory_->WaitCalls());
1083*b89a7cc2SEnji Cooper   ASSERT_EQ(1U, factory_->PassedCalls());
1084*b89a7cc2SEnji Cooper   EXPECT_TRUE(factory_->PassedArgument(0));
1085*b89a7cc2SEnji Cooper   EXPECT_EQ(0U, factory_->AbortCalls());
1086*b89a7cc2SEnji Cooper   EXPECT_TRUE(factory_->TestDeleted());
1087*b89a7cc2SEnji Cooper }
1088*b89a7cc2SEnji Cooper 
1089*b89a7cc2SEnji Cooper // Tests that the (simulated) child process executes the death test
1090*b89a7cc2SEnji Cooper // code, and is aborted with the correct AbortReason if it
1091*b89a7cc2SEnji Cooper // executes a return statement.
1092*b89a7cc2SEnji Cooper TEST_F(MacroLogicDeathTest, ChildPerformsReturn) {
1093*b89a7cc2SEnji Cooper   bool flag = false;
1094*b89a7cc2SEnji Cooper   factory_->SetParameters(true, DeathTest::EXECUTE_TEST, 0, true);
1095*b89a7cc2SEnji Cooper   RunReturningDeathTest(&flag);
1096*b89a7cc2SEnji Cooper   EXPECT_TRUE(flag);
1097*b89a7cc2SEnji Cooper   EXPECT_EQ(1, factory_->AssumeRoleCalls());
1098*b89a7cc2SEnji Cooper   EXPECT_EQ(0, factory_->WaitCalls());
1099*b89a7cc2SEnji Cooper   EXPECT_EQ(0U, factory_->PassedCalls());
1100*b89a7cc2SEnji Cooper   EXPECT_EQ(1U, factory_->AbortCalls());
1101*b89a7cc2SEnji Cooper   EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT,
1102*b89a7cc2SEnji Cooper             factory_->AbortArgument(0));
1103*b89a7cc2SEnji Cooper   EXPECT_TRUE(factory_->TestDeleted());
1104*b89a7cc2SEnji Cooper }
1105*b89a7cc2SEnji Cooper 
1106*b89a7cc2SEnji Cooper // Tests that the (simulated) child process is aborted with the
1107*b89a7cc2SEnji Cooper // correct AbortReason if it does not die.
1108*b89a7cc2SEnji Cooper TEST_F(MacroLogicDeathTest, ChildDoesNotDie) {
1109*b89a7cc2SEnji Cooper   bool flag = false;
1110*b89a7cc2SEnji Cooper   factory_->SetParameters(true, DeathTest::EXECUTE_TEST, 0, true);
1111*b89a7cc2SEnji Cooper   EXPECT_DEATH(flag = true, "");
1112*b89a7cc2SEnji Cooper   EXPECT_TRUE(flag);
1113*b89a7cc2SEnji Cooper   EXPECT_EQ(1, factory_->AssumeRoleCalls());
1114*b89a7cc2SEnji Cooper   EXPECT_EQ(0, factory_->WaitCalls());
1115*b89a7cc2SEnji Cooper   EXPECT_EQ(0U, factory_->PassedCalls());
1116*b89a7cc2SEnji Cooper   // This time there are two calls to Abort: one since the test didn't
1117*b89a7cc2SEnji Cooper   // die, and another from the ReturnSentinel when it's destroyed.  The
1118*b89a7cc2SEnji Cooper   // sentinel normally isn't destroyed if a test doesn't die, since
1119*b89a7cc2SEnji Cooper   // _exit(2) is called in that case by ForkingDeathTest, but not by
1120*b89a7cc2SEnji Cooper   // our MockDeathTest.
1121*b89a7cc2SEnji Cooper   ASSERT_EQ(2U, factory_->AbortCalls());
1122*b89a7cc2SEnji Cooper   EXPECT_EQ(DeathTest::TEST_DID_NOT_DIE,
1123*b89a7cc2SEnji Cooper             factory_->AbortArgument(0));
1124*b89a7cc2SEnji Cooper   EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT,
1125*b89a7cc2SEnji Cooper             factory_->AbortArgument(1));
1126*b89a7cc2SEnji Cooper   EXPECT_TRUE(factory_->TestDeleted());
1127*b89a7cc2SEnji Cooper }
1128*b89a7cc2SEnji Cooper 
1129*b89a7cc2SEnji Cooper // Tests that a successful death test does not register a successful
1130*b89a7cc2SEnji Cooper // test part.
1131*b89a7cc2SEnji Cooper TEST(SuccessRegistrationDeathTest, NoSuccessPart) {
1132*b89a7cc2SEnji Cooper   EXPECT_DEATH(_exit(1), "");
1133*b89a7cc2SEnji Cooper   EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count());
1134*b89a7cc2SEnji Cooper }
1135*b89a7cc2SEnji Cooper 
1136*b89a7cc2SEnji Cooper TEST(StreamingAssertionsDeathTest, DeathTest) {
1137*b89a7cc2SEnji Cooper   EXPECT_DEATH(_exit(1), "") << "unexpected failure";
1138*b89a7cc2SEnji Cooper   ASSERT_DEATH(_exit(1), "") << "unexpected failure";
1139*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE({  // NOLINT
1140*b89a7cc2SEnji Cooper     EXPECT_DEATH(_exit(0), "") << "expected failure";
1141*b89a7cc2SEnji Cooper   }, "expected failure");
1142*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE({  // NOLINT
1143*b89a7cc2SEnji Cooper     ASSERT_DEATH(_exit(0), "") << "expected failure";
1144*b89a7cc2SEnji Cooper   }, "expected failure");
1145*b89a7cc2SEnji Cooper }
1146*b89a7cc2SEnji Cooper 
1147*b89a7cc2SEnji Cooper // Tests that GetLastErrnoDescription returns an empty string when the
1148*b89a7cc2SEnji Cooper // last error is 0 and non-empty string when it is non-zero.
1149*b89a7cc2SEnji Cooper TEST(GetLastErrnoDescription, GetLastErrnoDescriptionWorks) {
1150*b89a7cc2SEnji Cooper   errno = ENOENT;
1151*b89a7cc2SEnji Cooper   EXPECT_STRNE("", GetLastErrnoDescription().c_str());
1152*b89a7cc2SEnji Cooper   errno = 0;
1153*b89a7cc2SEnji Cooper   EXPECT_STREQ("", GetLastErrnoDescription().c_str());
1154*b89a7cc2SEnji Cooper }
1155*b89a7cc2SEnji Cooper 
1156*b89a7cc2SEnji Cooper # if GTEST_OS_WINDOWS
1157*b89a7cc2SEnji Cooper TEST(AutoHandleTest, AutoHandleWorks) {
1158*b89a7cc2SEnji Cooper   HANDLE handle = ::CreateEvent(NULL, FALSE, FALSE, NULL);
1159*b89a7cc2SEnji Cooper   ASSERT_NE(INVALID_HANDLE_VALUE, handle);
1160*b89a7cc2SEnji Cooper 
1161*b89a7cc2SEnji Cooper   // Tests that the AutoHandle is correctly initialized with a handle.
1162*b89a7cc2SEnji Cooper   testing::internal::AutoHandle auto_handle(handle);
1163*b89a7cc2SEnji Cooper   EXPECT_EQ(handle, auto_handle.Get());
1164*b89a7cc2SEnji Cooper 
1165*b89a7cc2SEnji Cooper   // Tests that Reset assigns INVALID_HANDLE_VALUE.
1166*b89a7cc2SEnji Cooper   // Note that this cannot verify whether the original handle is closed.
1167*b89a7cc2SEnji Cooper   auto_handle.Reset();
1168*b89a7cc2SEnji Cooper   EXPECT_EQ(INVALID_HANDLE_VALUE, auto_handle.Get());
1169*b89a7cc2SEnji Cooper 
1170*b89a7cc2SEnji Cooper   // Tests that Reset assigns the new handle.
1171*b89a7cc2SEnji Cooper   // Note that this cannot verify whether the original handle is closed.
1172*b89a7cc2SEnji Cooper   handle = ::CreateEvent(NULL, FALSE, FALSE, NULL);
1173*b89a7cc2SEnji Cooper   ASSERT_NE(INVALID_HANDLE_VALUE, handle);
1174*b89a7cc2SEnji Cooper   auto_handle.Reset(handle);
1175*b89a7cc2SEnji Cooper   EXPECT_EQ(handle, auto_handle.Get());
1176*b89a7cc2SEnji Cooper 
1177*b89a7cc2SEnji Cooper   // Tests that AutoHandle contains INVALID_HANDLE_VALUE by default.
1178*b89a7cc2SEnji Cooper   testing::internal::AutoHandle auto_handle2;
1179*b89a7cc2SEnji Cooper   EXPECT_EQ(INVALID_HANDLE_VALUE, auto_handle2.Get());
1180*b89a7cc2SEnji Cooper }
1181*b89a7cc2SEnji Cooper # endif  // GTEST_OS_WINDOWS
1182*b89a7cc2SEnji Cooper 
1183*b89a7cc2SEnji Cooper # if GTEST_OS_WINDOWS
1184*b89a7cc2SEnji Cooper typedef unsigned __int64 BiggestParsable;
1185*b89a7cc2SEnji Cooper typedef signed __int64 BiggestSignedParsable;
1186*b89a7cc2SEnji Cooper # else
1187*b89a7cc2SEnji Cooper typedef unsigned long long BiggestParsable;
1188*b89a7cc2SEnji Cooper typedef signed long long BiggestSignedParsable;
1189*b89a7cc2SEnji Cooper # endif  // GTEST_OS_WINDOWS
1190*b89a7cc2SEnji Cooper 
1191*b89a7cc2SEnji Cooper // We cannot use std::numeric_limits<T>::max() as it clashes with the
1192*b89a7cc2SEnji Cooper // max() macro defined by <windows.h>.
1193*b89a7cc2SEnji Cooper const BiggestParsable kBiggestParsableMax = ULLONG_MAX;
1194*b89a7cc2SEnji Cooper const BiggestSignedParsable kBiggestSignedParsableMax = LLONG_MAX;
1195*b89a7cc2SEnji Cooper 
1196*b89a7cc2SEnji Cooper TEST(ParseNaturalNumberTest, RejectsInvalidFormat) {
1197*b89a7cc2SEnji Cooper   BiggestParsable result = 0;
1198*b89a7cc2SEnji Cooper 
1199*b89a7cc2SEnji Cooper   // Rejects non-numbers.
1200*b89a7cc2SEnji Cooper   EXPECT_FALSE(ParseNaturalNumber("non-number string", &result));
1201*b89a7cc2SEnji Cooper 
1202*b89a7cc2SEnji Cooper   // Rejects numbers with whitespace prefix.
1203*b89a7cc2SEnji Cooper   EXPECT_FALSE(ParseNaturalNumber(" 123", &result));
1204*b89a7cc2SEnji Cooper 
1205*b89a7cc2SEnji Cooper   // Rejects negative numbers.
1206*b89a7cc2SEnji Cooper   EXPECT_FALSE(ParseNaturalNumber("-123", &result));
1207*b89a7cc2SEnji Cooper 
1208*b89a7cc2SEnji Cooper   // Rejects numbers starting with a plus sign.
1209*b89a7cc2SEnji Cooper   EXPECT_FALSE(ParseNaturalNumber("+123", &result));
1210*b89a7cc2SEnji Cooper   errno = 0;
1211*b89a7cc2SEnji Cooper }
1212*b89a7cc2SEnji Cooper 
1213*b89a7cc2SEnji Cooper TEST(ParseNaturalNumberTest, RejectsOverflownNumbers) {
1214*b89a7cc2SEnji Cooper   BiggestParsable result = 0;
1215*b89a7cc2SEnji Cooper 
1216*b89a7cc2SEnji Cooper   EXPECT_FALSE(ParseNaturalNumber("99999999999999999999999", &result));
1217*b89a7cc2SEnji Cooper 
1218*b89a7cc2SEnji Cooper   signed char char_result = 0;
1219*b89a7cc2SEnji Cooper   EXPECT_FALSE(ParseNaturalNumber("200", &char_result));
1220*b89a7cc2SEnji Cooper   errno = 0;
1221*b89a7cc2SEnji Cooper }
1222*b89a7cc2SEnji Cooper 
1223*b89a7cc2SEnji Cooper TEST(ParseNaturalNumberTest, AcceptsValidNumbers) {
1224*b89a7cc2SEnji Cooper   BiggestParsable result = 0;
1225*b89a7cc2SEnji Cooper 
1226*b89a7cc2SEnji Cooper   result = 0;
1227*b89a7cc2SEnji Cooper   ASSERT_TRUE(ParseNaturalNumber("123", &result));
1228*b89a7cc2SEnji Cooper   EXPECT_EQ(123U, result);
1229*b89a7cc2SEnji Cooper 
1230*b89a7cc2SEnji Cooper   // Check 0 as an edge case.
1231*b89a7cc2SEnji Cooper   result = 1;
1232*b89a7cc2SEnji Cooper   ASSERT_TRUE(ParseNaturalNumber("0", &result));
1233*b89a7cc2SEnji Cooper   EXPECT_EQ(0U, result);
1234*b89a7cc2SEnji Cooper 
1235*b89a7cc2SEnji Cooper   result = 1;
1236*b89a7cc2SEnji Cooper   ASSERT_TRUE(ParseNaturalNumber("00000", &result));
1237*b89a7cc2SEnji Cooper   EXPECT_EQ(0U, result);
1238*b89a7cc2SEnji Cooper }
1239*b89a7cc2SEnji Cooper 
1240*b89a7cc2SEnji Cooper TEST(ParseNaturalNumberTest, AcceptsTypeLimits) {
1241*b89a7cc2SEnji Cooper   Message msg;
1242*b89a7cc2SEnji Cooper   msg << kBiggestParsableMax;
1243*b89a7cc2SEnji Cooper 
1244*b89a7cc2SEnji Cooper   BiggestParsable result = 0;
1245*b89a7cc2SEnji Cooper   EXPECT_TRUE(ParseNaturalNumber(msg.GetString(), &result));
1246*b89a7cc2SEnji Cooper   EXPECT_EQ(kBiggestParsableMax, result);
1247*b89a7cc2SEnji Cooper 
1248*b89a7cc2SEnji Cooper   Message msg2;
1249*b89a7cc2SEnji Cooper   msg2 << kBiggestSignedParsableMax;
1250*b89a7cc2SEnji Cooper 
1251*b89a7cc2SEnji Cooper   BiggestSignedParsable signed_result = 0;
1252*b89a7cc2SEnji Cooper   EXPECT_TRUE(ParseNaturalNumber(msg2.GetString(), &signed_result));
1253*b89a7cc2SEnji Cooper   EXPECT_EQ(kBiggestSignedParsableMax, signed_result);
1254*b89a7cc2SEnji Cooper 
1255*b89a7cc2SEnji Cooper   Message msg3;
1256*b89a7cc2SEnji Cooper   msg3 << INT_MAX;
1257*b89a7cc2SEnji Cooper 
1258*b89a7cc2SEnji Cooper   int int_result = 0;
1259*b89a7cc2SEnji Cooper   EXPECT_TRUE(ParseNaturalNumber(msg3.GetString(), &int_result));
1260*b89a7cc2SEnji Cooper   EXPECT_EQ(INT_MAX, int_result);
1261*b89a7cc2SEnji Cooper 
1262*b89a7cc2SEnji Cooper   Message msg4;
1263*b89a7cc2SEnji Cooper   msg4 << UINT_MAX;
1264*b89a7cc2SEnji Cooper 
1265*b89a7cc2SEnji Cooper   unsigned int uint_result = 0;
1266*b89a7cc2SEnji Cooper   EXPECT_TRUE(ParseNaturalNumber(msg4.GetString(), &uint_result));
1267*b89a7cc2SEnji Cooper   EXPECT_EQ(UINT_MAX, uint_result);
1268*b89a7cc2SEnji Cooper }
1269*b89a7cc2SEnji Cooper 
1270*b89a7cc2SEnji Cooper TEST(ParseNaturalNumberTest, WorksForShorterIntegers) {
1271*b89a7cc2SEnji Cooper   short short_result = 0;
1272*b89a7cc2SEnji Cooper   ASSERT_TRUE(ParseNaturalNumber("123", &short_result));
1273*b89a7cc2SEnji Cooper   EXPECT_EQ(123, short_result);
1274*b89a7cc2SEnji Cooper 
1275*b89a7cc2SEnji Cooper   signed char char_result = 0;
1276*b89a7cc2SEnji Cooper   ASSERT_TRUE(ParseNaturalNumber("123", &char_result));
1277*b89a7cc2SEnji Cooper   EXPECT_EQ(123, char_result);
1278*b89a7cc2SEnji Cooper }
1279*b89a7cc2SEnji Cooper 
1280*b89a7cc2SEnji Cooper # if GTEST_OS_WINDOWS
1281*b89a7cc2SEnji Cooper TEST(EnvironmentTest, HandleFitsIntoSizeT) {
1282*b89a7cc2SEnji Cooper   // FIXME: Remove this test after this condition is verified
1283*b89a7cc2SEnji Cooper   // in a static assertion in gtest-death-test.cc in the function
1284*b89a7cc2SEnji Cooper   // GetStatusFileDescriptor.
1285*b89a7cc2SEnji Cooper   ASSERT_TRUE(sizeof(HANDLE) <= sizeof(size_t));
1286*b89a7cc2SEnji Cooper }
1287*b89a7cc2SEnji Cooper # endif  // GTEST_OS_WINDOWS
1288*b89a7cc2SEnji Cooper 
1289*b89a7cc2SEnji Cooper // Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED trigger
1290*b89a7cc2SEnji Cooper // failures when death tests are available on the system.
1291*b89a7cc2SEnji Cooper TEST(ConditionalDeathMacrosDeathTest, ExpectsDeathWhenDeathTestsAvailable) {
1292*b89a7cc2SEnji Cooper   EXPECT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestExpectMacro"),
1293*b89a7cc2SEnji Cooper                             "death inside CondDeathTestExpectMacro");
1294*b89a7cc2SEnji Cooper   ASSERT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestAssertMacro"),
1295*b89a7cc2SEnji Cooper                             "death inside CondDeathTestAssertMacro");
1296*b89a7cc2SEnji Cooper 
1297*b89a7cc2SEnji Cooper   // Empty statement will not crash, which must trigger a failure.
1298*b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(EXPECT_DEATH_IF_SUPPORTED(;, ""), "");
1299*b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(ASSERT_DEATH_IF_SUPPORTED(;, ""), "");
1300*b89a7cc2SEnji Cooper }
1301*b89a7cc2SEnji Cooper 
1302*b89a7cc2SEnji Cooper TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInFastStyle) {
1303*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "fast";
1304*b89a7cc2SEnji Cooper   EXPECT_FALSE(InDeathTestChild());
1305*b89a7cc2SEnji Cooper   EXPECT_DEATH({
1306*b89a7cc2SEnji Cooper     fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside");
1307*b89a7cc2SEnji Cooper     fflush(stderr);
1308*b89a7cc2SEnji Cooper     _exit(1);
1309*b89a7cc2SEnji Cooper   }, "Inside");
1310*b89a7cc2SEnji Cooper }
1311*b89a7cc2SEnji Cooper 
1312*b89a7cc2SEnji Cooper TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInThreadSafeStyle) {
1313*b89a7cc2SEnji Cooper   testing::GTEST_FLAG(death_test_style) = "threadsafe";
1314*b89a7cc2SEnji Cooper   EXPECT_FALSE(InDeathTestChild());
1315*b89a7cc2SEnji Cooper   EXPECT_DEATH({
1316*b89a7cc2SEnji Cooper     fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside");
1317*b89a7cc2SEnji Cooper     fflush(stderr);
1318*b89a7cc2SEnji Cooper     _exit(1);
1319*b89a7cc2SEnji Cooper   }, "Inside");
1320*b89a7cc2SEnji Cooper }
1321*b89a7cc2SEnji Cooper 
1322*b89a7cc2SEnji Cooper #else  // !GTEST_HAS_DEATH_TEST follows
1323*b89a7cc2SEnji Cooper 
1324*b89a7cc2SEnji Cooper using testing::internal::CaptureStderr;
1325*b89a7cc2SEnji Cooper using testing::internal::GetCapturedStderr;
1326*b89a7cc2SEnji Cooper 
1327*b89a7cc2SEnji Cooper // Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED are still
1328*b89a7cc2SEnji Cooper // defined but do not trigger failures when death tests are not available on
1329*b89a7cc2SEnji Cooper // the system.
1330*b89a7cc2SEnji Cooper TEST(ConditionalDeathMacrosTest, WarnsWhenDeathTestsNotAvailable) {
1331*b89a7cc2SEnji Cooper   // Empty statement will not crash, but that should not trigger a failure
1332*b89a7cc2SEnji Cooper   // when death tests are not supported.
1333*b89a7cc2SEnji Cooper   CaptureStderr();
1334*b89a7cc2SEnji Cooper   EXPECT_DEATH_IF_SUPPORTED(;, "");
1335*b89a7cc2SEnji Cooper   std::string output = GetCapturedStderr();
1336*b89a7cc2SEnji Cooper   ASSERT_TRUE(NULL != strstr(output.c_str(),
1337*b89a7cc2SEnji Cooper                              "Death tests are not supported on this platform"));
1338*b89a7cc2SEnji Cooper   ASSERT_TRUE(NULL != strstr(output.c_str(), ";"));
1339*b89a7cc2SEnji Cooper 
1340*b89a7cc2SEnji Cooper   // The streamed message should not be printed as there is no test failure.
1341*b89a7cc2SEnji Cooper   CaptureStderr();
1342*b89a7cc2SEnji Cooper   EXPECT_DEATH_IF_SUPPORTED(;, "") << "streamed message";
1343*b89a7cc2SEnji Cooper   output = GetCapturedStderr();
1344*b89a7cc2SEnji Cooper   ASSERT_TRUE(NULL == strstr(output.c_str(), "streamed message"));
1345*b89a7cc2SEnji Cooper 
1346*b89a7cc2SEnji Cooper   CaptureStderr();
1347*b89a7cc2SEnji Cooper   ASSERT_DEATH_IF_SUPPORTED(;, "");  // NOLINT
1348*b89a7cc2SEnji Cooper   output = GetCapturedStderr();
1349*b89a7cc2SEnji Cooper   ASSERT_TRUE(NULL != strstr(output.c_str(),
1350*b89a7cc2SEnji Cooper                              "Death tests are not supported on this platform"));
1351*b89a7cc2SEnji Cooper   ASSERT_TRUE(NULL != strstr(output.c_str(), ";"));
1352*b89a7cc2SEnji Cooper 
1353*b89a7cc2SEnji Cooper   CaptureStderr();
1354*b89a7cc2SEnji Cooper   ASSERT_DEATH_IF_SUPPORTED(;, "") << "streamed message";  // NOLINT
1355*b89a7cc2SEnji Cooper   output = GetCapturedStderr();
1356*b89a7cc2SEnji Cooper   ASSERT_TRUE(NULL == strstr(output.c_str(), "streamed message"));
1357*b89a7cc2SEnji Cooper }
1358*b89a7cc2SEnji Cooper 
1359*b89a7cc2SEnji Cooper void FuncWithAssert(int* n) {
1360*b89a7cc2SEnji Cooper   ASSERT_DEATH_IF_SUPPORTED(return;, "");
1361*b89a7cc2SEnji Cooper   (*n)++;
1362*b89a7cc2SEnji Cooper }
1363*b89a7cc2SEnji Cooper 
1364*b89a7cc2SEnji Cooper // Tests that ASSERT_DEATH_IF_SUPPORTED does not return from the current
1365*b89a7cc2SEnji Cooper // function (as ASSERT_DEATH does) if death tests are not supported.
1366*b89a7cc2SEnji Cooper TEST(ConditionalDeathMacrosTest, AssertDeatDoesNotReturnhIfUnsupported) {
1367*b89a7cc2SEnji Cooper   int n = 0;
1368*b89a7cc2SEnji Cooper   FuncWithAssert(&n);
1369*b89a7cc2SEnji Cooper   EXPECT_EQ(1, n);
1370*b89a7cc2SEnji Cooper }
1371*b89a7cc2SEnji Cooper 
1372*b89a7cc2SEnji Cooper #endif  // !GTEST_HAS_DEATH_TEST
1373*b89a7cc2SEnji Cooper 
1374*b89a7cc2SEnji Cooper // Tests that the death test macros expand to code which may or may not
1375*b89a7cc2SEnji Cooper // be followed by operator<<, and that in either case the complete text
1376*b89a7cc2SEnji Cooper // comprises only a single C++ statement.
1377*b89a7cc2SEnji Cooper //
1378*b89a7cc2SEnji Cooper // The syntax should work whether death tests are available or not.
1379*b89a7cc2SEnji Cooper TEST(ConditionalDeathMacrosSyntaxDeathTest, SingleStatement) {
1380*b89a7cc2SEnji Cooper   if (AlwaysFalse())
1381*b89a7cc2SEnji Cooper     // This would fail if executed; this is a compilation test only
1382*b89a7cc2SEnji Cooper     ASSERT_DEATH_IF_SUPPORTED(return, "");
1383*b89a7cc2SEnji Cooper 
1384*b89a7cc2SEnji Cooper   if (AlwaysTrue())
1385*b89a7cc2SEnji Cooper     EXPECT_DEATH_IF_SUPPORTED(_exit(1), "");
1386*b89a7cc2SEnji Cooper   else
1387*b89a7cc2SEnji Cooper     // This empty "else" branch is meant to ensure that EXPECT_DEATH
1388*b89a7cc2SEnji Cooper     // doesn't expand into an "if" statement without an "else"
1389*b89a7cc2SEnji Cooper     ;  // NOLINT
1390*b89a7cc2SEnji Cooper 
1391*b89a7cc2SEnji Cooper   if (AlwaysFalse())
1392*b89a7cc2SEnji Cooper     ASSERT_DEATH_IF_SUPPORTED(return, "") << "did not die";
1393*b89a7cc2SEnji Cooper 
1394*b89a7cc2SEnji Cooper   if (AlwaysFalse())
1395*b89a7cc2SEnji Cooper     ;  // NOLINT
1396*b89a7cc2SEnji Cooper   else
1397*b89a7cc2SEnji Cooper     EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << 1 << 2 << 3;
1398*b89a7cc2SEnji Cooper }
1399*b89a7cc2SEnji Cooper 
1400*b89a7cc2SEnji Cooper // Tests that conditional death test macros expand to code which interacts
1401*b89a7cc2SEnji Cooper // well with switch statements.
1402*b89a7cc2SEnji Cooper TEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) {
1403*b89a7cc2SEnji Cooper   // Microsoft compiler usually complains about switch statements without
1404*b89a7cc2SEnji Cooper   // case labels. We suppress that warning for this test.
1405*b89a7cc2SEnji Cooper   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4065)
1406*b89a7cc2SEnji Cooper 
1407*b89a7cc2SEnji Cooper   switch (0)
1408*b89a7cc2SEnji Cooper     default:
1409*b89a7cc2SEnji Cooper       ASSERT_DEATH_IF_SUPPORTED(_exit(1), "")
1410*b89a7cc2SEnji Cooper           << "exit in default switch handler";
1411*b89a7cc2SEnji Cooper 
1412*b89a7cc2SEnji Cooper   switch (0)
1413*b89a7cc2SEnji Cooper     case 0:
1414*b89a7cc2SEnji Cooper       EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in switch case";
1415*b89a7cc2SEnji Cooper 
1416*b89a7cc2SEnji Cooper   GTEST_DISABLE_MSC_WARNINGS_POP_()
1417*b89a7cc2SEnji Cooper }
1418*b89a7cc2SEnji Cooper 
1419*b89a7cc2SEnji Cooper // Tests that a test case whose name ends with "DeathTest" works fine
1420*b89a7cc2SEnji Cooper // on Windows.
1421*b89a7cc2SEnji Cooper TEST(NotADeathTest, Test) {
1422*b89a7cc2SEnji Cooper   SUCCEED();
1423*b89a7cc2SEnji Cooper }
1424