11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2005, Google Inc. 21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All rights reserved. 31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without 51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are 61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met: 71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Redistributions of source code must retain the above copyright 91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer. 101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Redistributions in binary form must reproduce the above 111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer 121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the 131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution. 141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Neither the name of Google Inc. nor the names of its 151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from 161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission. 171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Author: wan@google.com (Zhanyong Wan) 311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests for death tests. 331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-death-test.h" 3541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest.h" 3641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-filepath.h" 3741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 3841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::AlwaysFalse; 3941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::AlwaysTrue; 401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST 421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS 4441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <direct.h> // For chdir(). 4541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else 4641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <unistd.h> 4741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/wait.h> // For waitpid. 4841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_OS_WINDOWS 491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <limits.h> 5141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <signal.h> 5241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <stdio.h> 531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 54fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# if GTEST_OS_LINUX 55fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# include <sys/time.h> 56fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# endif // GTEST_OS_LINUX 57fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 5841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include "gtest/gtest-spi.h" 591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Indicates that this translation unit is part of Google Test's 611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// implementation. It must come before gtest-internal-inl.h is 621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// included, or there will be a compiler error. This trick is to 631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// prevent a user from accidentally including gtest-internal-inl.h in 641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// his code. 6541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_IMPLEMENTATION_ 1 6641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include "src/gtest-internal-inl.h" 6741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# undef GTEST_IMPLEMENTATION_ 681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotnamespace posix = ::testing::internal::posix; 701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::Message; 721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::internal::DeathTest; 731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::internal::DeathTestFactory; 741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::internal::FilePath; 7541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::GetLastErrnoDescription; 7641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::GetUnitTestImpl; 77fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesusing testing::internal::InDeathTestChild; 781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::internal::ParseNaturalNumber; 791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing { 811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A helper class whose objects replace the death test factory for a 841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// single UnitTest object during their lifetimes. 851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ReplaceDeathTestFactory { 861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 8741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot explicit ReplaceDeathTestFactory(DeathTestFactory* new_factory) 8841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot : unit_test_impl_(GetUnitTestImpl()) { 8941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot old_factory_ = unit_test_impl_->death_test_factory_.release(); 9041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot unit_test_impl_->death_test_factory_.reset(new_factory); 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ~ReplaceDeathTestFactory() { 9441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot unit_test_impl_->death_test_factory_.release(); 9541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot unit_test_impl_->death_test_factory_.reset(old_factory_); 961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Prevents copying ReplaceDeathTestFactory objects. 991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ReplaceDeathTestFactory(const ReplaceDeathTestFactory&); 1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void operator=(const ReplaceDeathTestFactory&); 1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot UnitTestImpl* unit_test_impl_; 1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania DeathTestFactory* old_factory_; 1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace testing 1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid DieWithMessage(const ::std::string& message) { 11041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fprintf(stderr, "%s", message.c_str()); 11141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fflush(stderr); // Make sure the text is printed before the process exits. 11241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 11341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We call _exit() instead of exit(), as the former is a direct 11441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // system call and thus safer in the presence of threads. exit() 11541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // will invoke user-defined exit-hooks, which may do dangerous 11641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // things that conflict with death tests. 11741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 11841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Some compilers can recognize that _exit() never returns and issue the 11941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 'unreachable code' warning for code following this function, unless 12041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // fooled by a fake condition. 12141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (AlwaysTrue()) 12241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot _exit(1); 12341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 12441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 12541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid DieInside(const ::std::string& function) { 12641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot DieWithMessage("death inside " + function + "()."); 12741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 12841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that death tests work. 1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestForDeathTest : public testing::Test { 1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected: 1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestForDeathTest() : original_dir_(FilePath::GetCurrentDir()) {} 1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual ~TestForDeathTest() { 13641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot posix::ChDir(original_dir_.c_str()); 1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A static member function that's expected to die. 14041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static void StaticMemberFunction() { DieInside("StaticMemberFunction"); } 1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A method of the test fixture that may die. 1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void MemberFunction() { 14441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (should_die_) 14541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot DieInside("MemberFunction"); 1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // True iff MemberFunction() should die. 1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool should_die_; 1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const FilePath original_dir_; 1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A class with a member function that may die. 1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass MayDie { 1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania explicit MayDie(bool should_die) : should_die_(should_die) {} 1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A member function that may die. 1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void MemberFunction() const { 16041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (should_die_) 16141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot DieInside("MayDie::MemberFunction"); 1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // True iff MemberFunction() should die. 1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool should_die_; 1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A global function that's expected to die. 17041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid GlobalFunction() { DieInside("GlobalFunction"); } 1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A non-void function that's expected to die. 1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint NonVoidFunction() { 17441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot DieInside("NonVoidFunction"); 1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return 1; 1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A unary function that may die. 1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid DieIf(bool should_die) { 18041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (should_die) 18141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot DieInside("DieIf"); 1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A binary function that may die. 1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool DieIfLessThan(int x, int y) { 1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (x < y) { 18741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot DieInside("DieIfLessThan"); 1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture. 1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid DeathTestSubroutine() { 1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(GlobalFunction(), "death.*GlobalFunction"); 1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(GlobalFunction(), "death.*GlobalFunction"); 1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Death in dbg, not opt. 1991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint DieInDebugElse12(int* sideeffect) { 2001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (sideeffect) *sideeffect = 12; 20141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 20241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifndef NDEBUG 20341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 20441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot DieInside("DieInDebugElse12"); 20541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 20641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // NDEBUG 20741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return 12; 2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 21141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS 2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the ExitedWithCode predicate. 2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExitStatusPredicateTest, ExitedWithCode) { 2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // On Windows, the process's exit code is the same as its exit status, 2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // so the predicate just compares the its input with its parameter. 2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(testing::ExitedWithCode(0)(0)); 2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(testing::ExitedWithCode(1)(1)); 2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(testing::ExitedWithCode(42)(42)); 2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(testing::ExitedWithCode(0)(1)); 2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(testing::ExitedWithCode(1)(0)); 2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else 2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the exit status of a process that calls _exit(2) with a 2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// given exit code. This is a helper function for the 2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ExitStatusPredicateTest test suite. 2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic int NormalExitStatus(int exit_code) { 2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania pid_t child_pid = fork(); 2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (child_pid == 0) { 2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania _exit(exit_code); 2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int status; 2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania waitpid(child_pid, &status, 0); 2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return status; 2371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 2381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the exit status of a process that raises a given signal. 2401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If the signal does not cause the process to die, then it returns 2411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// instead the exit status of a process that exits normally with exit 2421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// code 1. This is a helper function for the ExitStatusPredicateTest 2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test suite. 2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic int KilledExitStatus(int signum) { 2451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania pid_t child_pid = fork(); 2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (child_pid == 0) { 2471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania raise(signum); 2481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania _exit(1); 2491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 2501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int status; 2511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania waitpid(child_pid, &status, 0); 2521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return status; 2531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 2541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the ExitedWithCode predicate. 2561be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExitStatusPredicateTest, ExitedWithCode) { 2571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int status0 = NormalExitStatus(0); 2581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int status1 = NormalExitStatus(1); 2591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int status42 = NormalExitStatus(42); 2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const testing::ExitedWithCode pred0(0); 2611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const testing::ExitedWithCode pred1(1); 2621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const testing::ExitedWithCode pred42(42); 2631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED1(pred0, status0); 2641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED1(pred1, status1); 2651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED1(pred42, status42); 2661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(pred0(status1)); 2671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(pred42(status0)); 2681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(pred1(status42)); 2691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 2701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the KilledBySignal predicate. 2721be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExitStatusPredicateTest, KilledBySignal) { 2731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int status_segv = KilledExitStatus(SIGSEGV); 2741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int status_kill = KilledExitStatus(SIGKILL); 2751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const testing::KilledBySignal pred_segv(SIGSEGV); 2761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const testing::KilledBySignal pred_kill(SIGKILL); 2771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED1(pred_segv, status_segv); 2781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED1(pred_kill, status_kill); 2791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(pred_segv(status_kill)); 2801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(pred_kill(status_segv)); 2811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 2821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 28341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_OS_WINDOWS 2841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that the death test macros expand to code which may or may not 2861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// be followed by operator<<, and that in either case the complete text 2871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// comprises only a single C++ statement. 2881be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, SingleStatement) { 28941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (AlwaysFalse()) 2901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This would fail if executed; this is a compilation test only 2911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(return, ""); 2921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 29341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (AlwaysTrue()) 2941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(_exit(1), ""); 2951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania else 2961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This empty "else" branch is meant to ensure that EXPECT_DEATH 2971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // doesn't expand into an "if" statement without an "else" 2981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ; 2991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (AlwaysFalse()) 3011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(return, "") << "did not die"; 3021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (AlwaysFalse()) 3041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ; 3051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania else 3061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(_exit(1), "") << 1 << 2 << 3; 3071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid DieWithEmbeddedNul() { 31041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fprintf(stderr, "Hello%cmy null world.\n", '\0'); 3111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stderr); 3121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania _exit(1); 3131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 31541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_USES_PCRE 3161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_DEATH and ASSERT_DEATH work when the error 3171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// message has a NUL character in it. 3181be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, EmbeddedNulInMessage) { 3191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TODO(wan@google.com): <regex.h> doesn't support matching strings 3201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // with embedded NUL characters - find a way to workaround it. 32141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_DEATH(DieWithEmbeddedNul(), "my null world"); 32241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_DEATH(DieWithEmbeddedNul(), "my null world"); 3231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 32441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_USES_PCRE 3251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that death test macros expand to code which interacts well with switch 3271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// statements. 3281be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, SwitchStatement) { 3291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Microsoft compiler usually complains about switch statements without 3301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// case labels. We suppress that warning for this test. 33141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifdef _MSC_VER 33241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(push) 33341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(disable: 4065) 33441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // _MSC_VER 3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania switch (0) 3371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania default: 3381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(_exit(1), "") << "exit in default switch handler"; 3391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania switch (0) 3411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case 0: 3421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(_exit(1), "") << "exit in switch case"; 3431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 34441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifdef _MSC_VER 34541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(pop) 34641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // _MSC_VER 3471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a static member function can be used in a "fast" style 3501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// death test. 3511be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, StaticMemberFunctionFastStyle) { 3521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(death_test_style) = "fast"; 3531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember"); 3541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a method of the test fixture can be used in a "fast" 3571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// style death test. 3581be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, MemberFunctionFastStyle) { 3591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(death_test_style) = "fast"; 3601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania should_die_ = true; 3611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction"); 3621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 36441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid ChangeToRootDir() { posix::ChDir(GTEST_PATH_SEP_); } 3651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that death tests work even if the current directory has been 3671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// changed. 3681be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, FastDeathTestInChangedDir) { 3691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(death_test_style) = "fast"; 3701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ChangeToRootDir(); 3721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), ""); 3731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ChangeToRootDir(); 3751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(_exit(1), ""); 3761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 378fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# if GTEST_OS_LINUX 379fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid SigprofAction(int, siginfo_t*, void*) { /* no op */ } 380fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 381fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Sets SIGPROF action and ITIMER_PROF timer (interval: 1ms). 382fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid SetSigprofActionAndTimer() { 383fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes struct itimerval timer; 384fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes timer.it_interval.tv_sec = 0; 385fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes timer.it_interval.tv_usec = 1; 386fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes timer.it_value = timer.it_interval; 387fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes ASSERT_EQ(0, setitimer(ITIMER_PROF, &timer, NULL)); 388fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes struct sigaction signal_action; 389fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes memset(&signal_action, 0, sizeof(signal_action)); 390fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes sigemptyset(&signal_action.sa_mask); 391fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes signal_action.sa_sigaction = SigprofAction; 392fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes signal_action.sa_flags = SA_RESTART | SA_SIGINFO; 393fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes ASSERT_EQ(0, sigaction(SIGPROF, &signal_action, NULL)); 394fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes} 395fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 396fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Disables ITIMER_PROF timer and ignores SIGPROF signal. 397fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid DisableSigprofActionAndTimer(struct sigaction* old_signal_action) { 398fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes struct itimerval timer; 399fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes timer.it_interval.tv_sec = 0; 400fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes timer.it_interval.tv_usec = 0; 401fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes timer.it_value = timer.it_interval; 402fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes ASSERT_EQ(0, setitimer(ITIMER_PROF, &timer, NULL)); 403fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes struct sigaction signal_action; 404fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes memset(&signal_action, 0, sizeof(signal_action)); 405fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes sigemptyset(&signal_action.sa_mask); 406fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes signal_action.sa_handler = SIG_IGN; 407fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes ASSERT_EQ(0, sigaction(SIGPROF, &signal_action, old_signal_action)); 408fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes} 409fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 410fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Tests that death tests work when SIGPROF handler and timer are set. 411fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesTEST_F(TestForDeathTest, FastSigprofActionSet) { 412fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes testing::GTEST_FLAG(death_test_style) = "fast"; 413fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes SetSigprofActionAndTimer(); 414fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_DEATH(_exit(1), ""); 415fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes struct sigaction old_signal_action; 416fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes DisableSigprofActionAndTimer(&old_signal_action); 417fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction); 418fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes} 419fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 420fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesTEST_F(TestForDeathTest, ThreadSafeSigprofActionSet) { 421fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes testing::GTEST_FLAG(death_test_style) = "threadsafe"; 422fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes SetSigprofActionAndTimer(); 423fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_DEATH(_exit(1), ""); 424fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes struct sigaction old_signal_action; 425fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes DisableSigprofActionAndTimer(&old_signal_action); 426fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction); 427fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes} 428fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# endif // GTEST_OS_LINUX 429fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 4301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Repeats a representative sample of death tests in the "threadsafe" style: 4311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4321be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, StaticMemberFunctionThreadsafeStyle) { 4331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(death_test_style) = "threadsafe"; 4341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember"); 4351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4371be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, MemberFunctionThreadsafeStyle) { 4381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(death_test_style) = "threadsafe"; 4391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania should_die_ = true; 4401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction"); 4411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4431be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ThreadsafeDeathTestInLoop) { 4441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(death_test_style) = "threadsafe"; 4451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (int i = 0; i < 3; ++i) 4471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EXIT(_exit(i), testing::ExitedWithCode(i), "") << ": i = " << i; 4481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4501be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ThreadsafeDeathTestInChangedDir) { 4511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(death_test_style) = "threadsafe"; 4521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ChangeToRootDir(); 4541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), ""); 4551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ChangeToRootDir(); 4571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(_exit(1), ""); 4581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4601be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, MixedStyles) { 4611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(death_test_style) = "threadsafe"; 4621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(_exit(1), ""); 4631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(death_test_style) = "fast"; 4641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(_exit(1), ""); 4651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 467fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD 468fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 4691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace { 4701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool pthread_flag; 4721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid SetPthreadFlag() { 4741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania pthread_flag = true; 4751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace 4781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4791be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, DoesNotExecuteAtforkHooks) { 4801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!testing::GTEST_FLAG(death_test_use_fork)) { 4811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(death_test_style) = "threadsafe"; 4821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania pthread_flag = false; 4831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EQ(0, pthread_atfork(&SetPthreadFlag, NULL, NULL)); 4841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(_exit(1), ""); 4851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_FALSE(pthread_flag); 4861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_HAS_CLONE && GTEST_HAS_PTHREAD 4901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a method of another class can be used in a death test. 4921be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, MethodOfAnotherClass) { 4931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const MayDie x(true); 4941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(x.MemberFunction(), "MayDie\\:\\:MemberFunction"); 4951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a global function can be used in a death test. 4981be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, GlobalFunction) { 4991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(GlobalFunction(), "GlobalFunction"); 5001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that any value convertible to an RE works as a second 5031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// argument to EXPECT_DEATH. 5041be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, AcceptsAnythingConvertibleToRE) { 5051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const char regex_c_str[] = "GlobalFunction"; 5061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(GlobalFunction(), regex_c_str); 5071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const testing::internal::RE regex(regex_c_str); 5091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(GlobalFunction(), regex); 5101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 51141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_HAS_GLOBAL_STRING 51241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 5131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const string regex_str(regex_c_str); 5141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(GlobalFunction(), regex_str); 5151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 51641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_HAS_GLOBAL_STRING 51741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 5181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::std::string regex_std_str(regex_c_str); 5191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(GlobalFunction(), regex_std_str); 5201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a non-void function can be used in a death test. 5231be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, NonVoidFunction) { 5241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(NonVoidFunction(), "NonVoidFunction"); 5251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that functions that take parameter(s) can be used in a death test. 5281be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, FunctionWithParameter) { 5291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(DieIf(true), "DieIf\\(\\)"); 5301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(DieIfLessThan(2, 3), "DieIfLessThan"); 5311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture. 5341be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, OutsideFixture) { 5351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania DeathTestSubroutine(); 5361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that death tests can be done inside a loop. 5391be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, InsideLoop) { 5401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (int i = 0; i < 5; i++) { 5411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(DieIfLessThan(-1, i), "DieIfLessThan") << "where i == " << i; 5421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 5431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a compound statement can be used in a death test. 5461be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, CompoundStatement) { 5471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH({ // NOLINT 5481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int x = 2; 5491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int y = x + 1; 5501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania DieIfLessThan(x, y); 5511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, 5521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "DieIfLessThan"); 5531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that code that doesn't die causes a death test to fail. 5561be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, DoesNotDie) { 5571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(DieIf(false), "DieIf"), 5581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "failed to die"); 5591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a death test fails when the error message isn't expected. 5621be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ErrorMessageMismatch) { 5631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE({ // NOLINT 5641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(DieIf(true), "DieIfLessThan") << "End of death test message."; 5651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "died but not with expected error"); 5661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On exit, *aborted will be true iff the EXPECT_DEATH() statement 5691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// aborted the function. 5701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ExpectDeathTestHelper(bool* aborted) { 5711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *aborted = true; 5721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(DieIf(false), "DieIf"); // This assertion should fail. 5731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *aborted = false; 5741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_DEATH doesn't abort the test on failure. 5771be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, EXPECT_DEATH) { 5781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool aborted = true; 5791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE(ExpectDeathTestHelper(&aborted), 5801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "failed to die"); 5811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(aborted); 5821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that ASSERT_DEATH does abort the test on failure. 5851be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ASSERT_DEATH) { 5861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool aborted; 5871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE({ // NOLINT 5881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania aborted = true; 5891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(DieIf(false), "DieIf"); // This assertion should fail. 5901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania aborted = false; 5911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "failed to die"); 5921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(aborted); 5931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_DEATH evaluates the arguments exactly once. 5961be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, SingleEvaluation) { 5971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int x = 3; 5981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(DieIf((++x) == 4), "DieIf"); 5991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* regex = "DieIf"; 6011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* regex_save = regex; 6021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(DieIfLessThan(3, 4), regex++); 6031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(regex_save + 1, regex); 6041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that run-away death tests are reported as failures. 60741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, RunawayIsFailure) { 6081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(static_cast<void>(0), "Foo"), 6091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "failed to die."); 61041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 6111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 61241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that death tests report executing 'return' in the statement as 61341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// failure. 61441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, ReturnIsFailure) { 6151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE(ASSERT_DEATH(return, "Bar"), 6161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "illegal return in test statement."); 6171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 619fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Tests that EXPECT_DEBUG_DEATH works as expected, that is, you can stream a 620fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// message to it, and in debug mode it: 6211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1. Asserts on death. 6221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2. Has no side effect. 6231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 6241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// And in opt mode, it: 6251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1. Has side effects but does not assert. 6261be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, TestExpectDebugDeath) { 6271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int sideeffect = 0; 6281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 629fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), "death.*DieInDebugElse12") 630fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes << "Must accept a streamed message"; 6311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 63241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifdef NDEBUG 63341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 6341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Checks that the assignment occurs in opt mode (sideeffect). 6351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(12, sideeffect); 63641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 63741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else 63841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 6391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Checks that the assignment does not occur in dbg mode (no sideeffect). 6401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, sideeffect); 64141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 64241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif 6431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 645fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Tests that ASSERT_DEBUG_DEATH works as expected, that is, you can stream a 646fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// message to it, and in debug mode it: 647fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// 1. Asserts on death. 6481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2. Has no side effect. 6491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 650fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// And in opt mode, it: 651fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// 1. Has side effects but does not assert. 6521be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, TestAssertDebugDeath) { 6531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int sideeffect = 0; 6541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 655fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes ASSERT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), "death.*DieInDebugElse12") 656fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes << "Must accept a streamed message"; 6571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 65841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifdef NDEBUG 65941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 6601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Checks that the assignment occurs in opt mode (sideeffect). 6611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(12, sideeffect); 66241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 66341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else 66441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 6651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Checks that the assignment does not occur in dbg mode (no sideeffect). 6661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, sideeffect); 66741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 66841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif 6691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 67141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifndef NDEBUG 6721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ExpectDebugDeathHelper(bool* aborted) { 6741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *aborted = true; 6751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEBUG_DEATH(return, "") << "This is expected to fail."; 6761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *aborted = false; 6771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 67941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS 6801be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(PopUpDeathTest, DoesNotShowPopUpOnAbort) { 6811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("This test should be considered failing if it shows " 6821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "any pop-up dialogs.\n"); 6831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 6841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH({ 6861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(catch_exceptions) = false; 6871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania abort(); 6881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, ""); 6891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 69041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_OS_WINDOWS 6911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_DEBUG_DEATH in debug mode does not abort 6931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the function. 6941be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ExpectDebugDeathDoesNotAbort) { 6951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool aborted = true; 6961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE(ExpectDebugDeathHelper(&aborted), ""); 6971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(aborted); 6981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid AssertDebugDeathHelper(bool* aborted) { 7011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *aborted = true; 7021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEBUG_DEATH(return, "") << "This is expected to fail."; 7031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *aborted = false; 7041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that ASSERT_DEBUG_DEATH in debug mode aborts the function on 7071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// failure. 7081be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, AssertDebugDeathAborts) { 7091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool aborted; 7101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania aborted = false; 7111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), ""); 7121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(aborted); 7131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 71541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // _NDEBUG 7161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the *_EXIT family of macros, using a variety of predicates. 7181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic void TestExitMacros() { 7191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), ""); 7201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EXIT(_exit(42), testing::ExitedWithCode(42), ""); 7211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 72241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS 7231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Of all signals effects on the process exit code, only those of SIGABRT 7251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // are documented on Windows. 7261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // See http://msdn.microsoft.com/en-us/library/dwwzkt4c(VS.71).aspx. 727fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EXIT(raise(SIGABRT), testing::ExitedWithCode(3), "") << "b_ar"; 72841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 72941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else 73041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 7311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EXIT(raise(SIGKILL), testing::KilledBySignal(SIGKILL), "") << "foo"; 7321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EXIT(raise(SIGUSR2), testing::KilledBySignal(SIGUSR2), "") << "bar"; 7331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE({ // NOLINT 7351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EXIT(_exit(0), testing::KilledBySignal(SIGSEGV), "") 736fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes << "This failure is expected, too."; 7371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "This failure is expected, too."); 73841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 73941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_OS_WINDOWS 7401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE({ // NOLINT 7421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EXIT(raise(SIGSEGV), testing::ExitedWithCode(0), "") 743fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes << "This failure is expected."; 7441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "This failure is expected."); 7451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7471be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ExitMacros) { 7481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestExitMacros(); 7491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7511be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ExitMacrosUsingFork) { 7521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(death_test_use_fork) = true; 7531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestExitMacros(); 7541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7561be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, InvalidStyle) { 7571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(death_test_style) = "rococo"; 7581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE({ // NOLINT 7591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(_exit(0), "") << "This failure is expected."; 7601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "This failure is expected."); 7611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 76341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, DeathTestFailedOutput) { 76441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot testing::GTEST_FLAG(death_test_style) = "fast"; 76541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_NONFATAL_FAILURE( 76641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_DEATH(DieWithMessage("death\n"), 76741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "expected message"), 76841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Actual msg:\n" 76941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "[ DEATH ] death\n"); 77041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 77141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 77241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, DeathTestUnexpectedReturnOutput) { 77341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot testing::GTEST_FLAG(death_test_style) = "fast"; 77441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_NONFATAL_FAILURE( 77541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_DEATH({ 77641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fprintf(stderr, "returning\n"); 77741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fflush(stderr); 77841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return; 77941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot }, ""), 78041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot " Result: illegal return in test statement.\n" 78141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot " Error msg:\n" 78241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "[ DEATH ] returning\n"); 78341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 78441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 78541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, DeathTestBadExitCodeOutput) { 78641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot testing::GTEST_FLAG(death_test_style) = "fast"; 78741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_NONFATAL_FAILURE( 78841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_EXIT(DieWithMessage("exiting with rc 1\n"), 78941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot testing::ExitedWithCode(3), 79041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "expected message"), 79141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot " Result: died but not with expected exit code:\n" 79241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot " Exited with exit status 1\n" 79341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Actual msg:\n" 79441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "[ DEATH ] exiting with rc 1\n"); 79541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 79641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 79741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, DeathTestMultiLineMatchFail) { 79841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot testing::GTEST_FLAG(death_test_style) = "fast"; 79941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_NONFATAL_FAILURE( 80041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"), 80141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "line 1\nxyz\nline 3\n"), 80241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Actual msg:\n" 80341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "[ DEATH ] line 1\n" 80441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "[ DEATH ] line 2\n" 80541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "[ DEATH ] line 3\n"); 80641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 80741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 80841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, DeathTestMultiLineMatchPass) { 80941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot testing::GTEST_FLAG(death_test_style) = "fast"; 81041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"), 81141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "line 1\nline 2\nline 3\n"); 81241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 81341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 8141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A DeathTestFactory that returns MockDeathTests. 8151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass MockDeathTestFactory : public DeathTestFactory { 8161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 8171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania MockDeathTestFactory(); 8181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual bool Create(const char* statement, 8191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::testing::internal::RE* regex, 8201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* file, int line, DeathTest** test); 8211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets the parameters for subsequent calls to Create. 8231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void SetParameters(bool create, DeathTest::TestRole role, 8241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int status, bool passed); 8251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Accessors. 8271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int AssumeRoleCalls() const { return assume_role_calls_; } 8281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int WaitCalls() const { return wait_calls_; } 8291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int PassedCalls() const { return passed_args_.size(); } 8301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool PassedArgument(int n) const { return passed_args_[n]; } 8311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int AbortCalls() const { return abort_args_.size(); } 8321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania DeathTest::AbortReason AbortArgument(int n) const { 8331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return abort_args_[n]; 8341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 8351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool TestDeleted() const { return test_deleted_; } 8361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 8381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania friend class MockDeathTest; 8391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // If true, Create will return a MockDeathTest; otherwise it returns 8401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NULL. 8411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool create_; 8421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The value a MockDeathTest will return from its AssumeRole method. 8431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania DeathTest::TestRole role_; 8441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The value a MockDeathTest will return from its Wait method. 8451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int status_; 8461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The value a MockDeathTest will return from its Passed method. 8471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool passed_; 8481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Number of times AssumeRole was called. 8501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int assume_role_calls_; 8511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Number of times Wait was called. 8521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int wait_calls_; 8531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The arguments to the calls to Passed since the last call to 8541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // SetParameters. 8551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania std::vector<bool> passed_args_; 8561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The arguments to the calls to Abort since the last call to 8571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // SetParameters. 8581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania std::vector<DeathTest::AbortReason> abort_args_; 8591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // True if the last MockDeathTest returned by Create has been 8601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // deleted. 8611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool test_deleted_; 8621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 8631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A DeathTest implementation useful in testing. It returns values set 8661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// at its creation from its various inherited DeathTest methods, and 8671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// reports calls to those methods to its parent MockDeathTestFactory 8681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// object. 8691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass MockDeathTest : public DeathTest { 8701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 8711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania MockDeathTest(MockDeathTestFactory *parent, 8721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestRole role, int status, bool passed) : 8731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parent_(parent), role_(role), status_(status), passed_(passed) { 8741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 8751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual ~MockDeathTest() { 8761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parent_->test_deleted_ = true; 8771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 8781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual TestRole AssumeRole() { 8791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ++parent_->assume_role_calls_; 8801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return role_; 8811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 8821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual int Wait() { 8831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ++parent_->wait_calls_; 8841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return status_; 8851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 8861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual bool Passed(bool exit_status_ok) { 8871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parent_->passed_args_.push_back(exit_status_ok); 8881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return passed_; 8891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 8901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void Abort(AbortReason reason) { 8911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parent_->abort_args_.push_back(reason); 8921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 893fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 8941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 8951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania MockDeathTestFactory* const parent_; 8961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestRole role_; 8971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int status_; 8981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool passed_; 8991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 9001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// MockDeathTestFactory constructor. 9031be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaMockDeathTestFactory::MockDeathTestFactory() 9041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : create_(true), 9051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania role_(DeathTest::OVERSEE_TEST), 9061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania status_(0), 9071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania passed_(true), 9081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania assume_role_calls_(0), 9091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania wait_calls_(0), 9101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania passed_args_(), 9111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania abort_args_() { 9121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Sets the parameters for subsequent calls to Create. 9161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid MockDeathTestFactory::SetParameters(bool create, 9171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania DeathTest::TestRole role, 9181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int status, bool passed) { 9191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania create_ = create; 9201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania role_ = role; 9211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania status_ = status; 9221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania passed_ = passed; 9231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania assume_role_calls_ = 0; 9251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania wait_calls_ = 0; 9261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania passed_args_.clear(); 9271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania abort_args_.clear(); 9281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Sets test to NULL (if create_ is false) or to the address of a new 9321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// MockDeathTest object with parameters taken from the last call 9331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to SetParameters (if create_ is true). Always returns true. 93441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool MockDeathTestFactory::Create(const char* /*statement*/, 93541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const ::testing::internal::RE* /*regex*/, 93641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* /*file*/, 93741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int /*line*/, 9381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania DeathTest** test) { 9391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania test_deleted_ = false; 9401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (create_) { 9411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *test = new MockDeathTest(this, role_, status_, passed_); 9421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 9431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *test = NULL; 9441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 9461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A test fixture for testing the logic of the GTEST_DEATH_TEST_ macro. 9491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// It installs a MockDeathTestFactory that is used for the duration 9501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of the test case. 9511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass MacroLogicDeathTest : public testing::Test { 9521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected: 9531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static testing::internal::ReplaceDeathTestFactory* replacer_; 9541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static MockDeathTestFactory* factory_; 9551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static void SetUpTestCase() { 9571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania factory_ = new MockDeathTestFactory; 95841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot replacer_ = new testing::internal::ReplaceDeathTestFactory(factory_); 9591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static void TearDownTestCase() { 9621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania delete replacer_; 9631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania replacer_ = NULL; 9641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania delete factory_; 9651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania factory_ = NULL; 9661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Runs a death test that breaks the rules by returning. Such a death 9691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // test cannot be run directly from a test routine that uses a 9701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // MockDeathTest, or the remainder of the routine will not be executed. 9711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static void RunReturningDeathTest(bool* flag) { 9721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH({ // NOLINT 9731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *flag = true; 9741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return; 9751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, ""); 9761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 9781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatesting::internal::ReplaceDeathTestFactory* MacroLogicDeathTest::replacer_ 9801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania = NULL; 9811be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaMockDeathTestFactory* MacroLogicDeathTest::factory_ = NULL; 9821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Test that nothing happens when the factory doesn't return a DeathTest: 9851be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MacroLogicDeathTest, NothingHappens) { 9861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool flag = false; 9871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania factory_->SetParameters(false, DeathTest::OVERSEE_TEST, 0, true); 9881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(flag = true, ""); 9891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(flag); 9901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, factory_->AssumeRoleCalls()); 9911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, factory_->WaitCalls()); 9921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, factory_->PassedCalls()); 9931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, factory_->AbortCalls()); 9941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(factory_->TestDeleted()); 9951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Test that the parent process doesn't run the death test code, 9981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and that the Passed method returns false when the (simulated) 9991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// child process exits with status 0: 10001be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MacroLogicDeathTest, ChildExitsSuccessfully) { 10011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool flag = false; 10021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania factory_->SetParameters(true, DeathTest::OVERSEE_TEST, 0, true); 10031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(flag = true, ""); 10041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(flag); 10051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(1, factory_->AssumeRoleCalls()); 10061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(1, factory_->WaitCalls()); 10071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EQ(1, factory_->PassedCalls()); 10081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(factory_->PassedArgument(0)); 10091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, factory_->AbortCalls()); 10101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(factory_->TestDeleted()); 10111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 10121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that the Passed method was given the argument "true" when 10141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the (simulated) child process exits with status 1: 10151be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MacroLogicDeathTest, ChildExitsUnsuccessfully) { 10161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool flag = false; 10171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania factory_->SetParameters(true, DeathTest::OVERSEE_TEST, 1, true); 10181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(flag = true, ""); 10191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FALSE(flag); 10201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(1, factory_->AssumeRoleCalls()); 10211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(1, factory_->WaitCalls()); 10221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EQ(1, factory_->PassedCalls()); 10231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(factory_->PassedArgument(0)); 10241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, factory_->AbortCalls()); 10251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(factory_->TestDeleted()); 10261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 10271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that the (simulated) child process executes the death test 10291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// code, and is aborted with the correct AbortReason if it 10301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// executes a return statement. 10311be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MacroLogicDeathTest, ChildPerformsReturn) { 10321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool flag = false; 10331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania factory_->SetParameters(true, DeathTest::EXECUTE_TEST, 0, true); 10341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania RunReturningDeathTest(&flag); 10351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(flag); 10361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(1, factory_->AssumeRoleCalls()); 10371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, factory_->WaitCalls()); 10381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, factory_->PassedCalls()); 10391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(1, factory_->AbortCalls()); 10401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT, 10411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania factory_->AbortArgument(0)); 10421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(factory_->TestDeleted()); 10431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 10441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that the (simulated) child process is aborted with the 10461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// correct AbortReason if it does not die. 10471be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MacroLogicDeathTest, ChildDoesNotDie) { 10481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool flag = false; 10491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania factory_->SetParameters(true, DeathTest::EXECUTE_TEST, 0, true); 10501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(flag = true, ""); 10511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(flag); 10521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(1, factory_->AssumeRoleCalls()); 10531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, factory_->WaitCalls()); 10541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, factory_->PassedCalls()); 10551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This time there are two calls to Abort: one since the test didn't 10561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // die, and another from the ReturnSentinel when it's destroyed. The 10571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // sentinel normally isn't destroyed if a test doesn't die, since 10581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // _exit(2) is called in that case by ForkingDeathTest, but not by 10591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // our MockDeathTest. 10601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EQ(2, factory_->AbortCalls()); 10611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(DeathTest::TEST_DID_NOT_DIE, 10621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania factory_->AbortArgument(0)); 10631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT, 10641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania factory_->AbortArgument(1)); 10651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(factory_->TestDeleted()); 10661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 10671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a successful death test does not register a successful 10691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test part. 10701be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(SuccessRegistrationDeathTest, NoSuccessPart) { 10711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(_exit(1), ""); 107241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); 10731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 10741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10751be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(StreamingAssertionsDeathTest, DeathTest) { 10761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(_exit(1), "") << "unexpected failure"; 10771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(_exit(1), "") << "unexpected failure"; 10781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE({ // NOLINT 10791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(_exit(0), "") << "expected failure"; 10801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "expected failure"); 10811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE({ // NOLINT 10821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(_exit(0), "") << "expected failure"; 10831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "expected failure"); 10841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 10851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 108641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that GetLastErrnoDescription returns an empty string when the 10871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// last error is 0 and non-empty string when it is non-zero. 108841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(GetLastErrnoDescription, GetLastErrnoDescriptionWorks) { 10891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania errno = ENOENT; 109041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_STRNE("", GetLastErrnoDescription().c_str()); 10911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania errno = 0; 109241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_STREQ("", GetLastErrnoDescription().c_str()); 10931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 10941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 109541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS 10961be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(AutoHandleTest, AutoHandleWorks) { 10971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania HANDLE handle = ::CreateEvent(NULL, FALSE, FALSE, NULL); 10981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_NE(INVALID_HANDLE_VALUE, handle); 10991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Tests that the AutoHandle is correctly initialized with a handle. 11011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::internal::AutoHandle auto_handle(handle); 11021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(handle, auto_handle.Get()); 11031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Tests that Reset assigns INVALID_HANDLE_VALUE. 11051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Note that this cannot verify whether the original handle is closed. 11061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania auto_handle.Reset(); 11071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(INVALID_HANDLE_VALUE, auto_handle.Get()); 11081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Tests that Reset assigns the new handle. 11101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Note that this cannot verify whether the original handle is closed. 11111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania handle = ::CreateEvent(NULL, FALSE, FALSE, NULL); 11121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_NE(INVALID_HANDLE_VALUE, handle); 11131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania auto_handle.Reset(handle); 11141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(handle, auto_handle.Get()); 11151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Tests that AutoHandle contains INVALID_HANDLE_VALUE by default. 11171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::internal::AutoHandle auto_handle2; 11181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(INVALID_HANDLE_VALUE, auto_handle2.Get()); 11191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 112041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_OS_WINDOWS 11211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 112241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS 11231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef unsigned __int64 BiggestParsable; 11241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef signed __int64 BiggestSignedParsable; 112541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else 11261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef unsigned long long BiggestParsable; 11271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef signed long long BiggestSignedParsable; 112841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_OS_WINDOWS 11291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1130fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// We cannot use std::numeric_limits<T>::max() as it clashes with the 1131fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// max() macro defined by <windows.h>. 1132fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesconst BiggestParsable kBiggestParsableMax = ULLONG_MAX; 1133fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesconst BiggestSignedParsable kBiggestSignedParsableMax = LLONG_MAX; 1134fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 11351be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ParseNaturalNumberTest, RejectsInvalidFormat) { 11361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania BiggestParsable result = 0; 11371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Rejects non-numbers. 1139fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_FALSE(ParseNaturalNumber("non-number string", &result)); 11401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Rejects numbers with whitespace prefix. 1142fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_FALSE(ParseNaturalNumber(" 123", &result)); 11431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Rejects negative numbers. 1145fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_FALSE(ParseNaturalNumber("-123", &result)); 11461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Rejects numbers starting with a plus sign. 1148fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_FALSE(ParseNaturalNumber("+123", &result)); 11491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania errno = 0; 11501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 11511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11521be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ParseNaturalNumberTest, RejectsOverflownNumbers) { 11531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania BiggestParsable result = 0; 11541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1155fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_FALSE(ParseNaturalNumber("99999999999999999999999", &result)); 11561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania signed char char_result = 0; 1158fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_FALSE(ParseNaturalNumber("200", &char_result)); 11591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania errno = 0; 11601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 11611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11621be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ParseNaturalNumberTest, AcceptsValidNumbers) { 11631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania BiggestParsable result = 0; 11641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania result = 0; 1166fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes ASSERT_TRUE(ParseNaturalNumber("123", &result)); 116741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_EQ(123U, result); 11681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Check 0 as an edge case. 11701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania result = 1; 1171fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes ASSERT_TRUE(ParseNaturalNumber("0", &result)); 117241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_EQ(0U, result); 11731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania result = 1; 1175fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes ASSERT_TRUE(ParseNaturalNumber("00000", &result)); 117641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_EQ(0U, result); 11771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 11781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11791be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ParseNaturalNumberTest, AcceptsTypeLimits) { 11801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message msg; 11811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << kBiggestParsableMax; 11821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania BiggestParsable result = 0; 11841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(ParseNaturalNumber(msg.GetString(), &result)); 11851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(kBiggestParsableMax, result); 11861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message msg2; 11881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg2 << kBiggestSignedParsableMax; 11891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania BiggestSignedParsable signed_result = 0; 11911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(ParseNaturalNumber(msg2.GetString(), &signed_result)); 11921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(kBiggestSignedParsableMax, signed_result); 11931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message msg3; 11951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg3 << INT_MAX; 11961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int int_result = 0; 11981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(ParseNaturalNumber(msg3.GetString(), &int_result)); 11991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(INT_MAX, int_result); 12001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message msg4; 12021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg4 << UINT_MAX; 12031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania unsigned int uint_result = 0; 12051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(ParseNaturalNumber(msg4.GetString(), &uint_result)); 12061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(UINT_MAX, uint_result); 12071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 12081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12091be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ParseNaturalNumberTest, WorksForShorterIntegers) { 12101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania short short_result = 0; 1211fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes ASSERT_TRUE(ParseNaturalNumber("123", &short_result)); 12121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(123, short_result); 12131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania signed char char_result = 0; 1215fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes ASSERT_TRUE(ParseNaturalNumber("123", &char_result)); 12161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(123, char_result); 12171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 12181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 121941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS 12201be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(EnvironmentTest, HandleFitsIntoSizeT) { 12211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TODO(vladl@google.com): Remove this test after this condition is verified 12221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // in a static assertion in gtest-death-test.cc in the function 12231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // GetStatusFileDescriptor. 12241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(sizeof(HANDLE) <= sizeof(size_t)); 12251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 122641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_OS_WINDOWS 122741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 122841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED trigger 122941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// failures when death tests are available on the system. 123041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(ConditionalDeathMacrosDeathTest, ExpectsDeathWhenDeathTestsAvailable) { 123141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestExpectMacro"), 123241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "death inside CondDeathTestExpectMacro"); 123341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestAssertMacro"), 123441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "death inside CondDeathTestAssertMacro"); 123541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 123641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Empty statement will not crash, which must trigger a failure. 123741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_NONFATAL_FAILURE(EXPECT_DEATH_IF_SUPPORTED(;, ""), ""); 123841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_FATAL_FAILURE(ASSERT_DEATH_IF_SUPPORTED(;, ""), ""); 123941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 12401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 124141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else 124241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 124341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::CaptureStderr; 124441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::GetCapturedStderr; 124541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 124641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED are still 124741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// defined but do not trigger failures when death tests are not available on 124841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the system. 124941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(ConditionalDeathMacrosTest, WarnsWhenDeathTestsNotAvailable) { 125041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Empty statement will not crash, but that should not trigger a failure 125141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // when death tests are not supported. 125241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot CaptureStderr(); 125341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_DEATH_IF_SUPPORTED(;, ""); 1254fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes std::string output = GetCapturedStderr(); 125541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_TRUE(NULL != strstr(output.c_str(), 125641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Death tests are not supported on this platform")); 125741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_TRUE(NULL != strstr(output.c_str(), ";")); 125841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 125941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The streamed message should not be printed as there is no test failure. 126041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot CaptureStderr(); 126141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_DEATH_IF_SUPPORTED(;, "") << "streamed message"; 126241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot output = GetCapturedStderr(); 126341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_TRUE(NULL == strstr(output.c_str(), "streamed message")); 126441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 126541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot CaptureStderr(); 126641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_DEATH_IF_SUPPORTED(;, ""); // NOLINT 126741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot output = GetCapturedStderr(); 126841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_TRUE(NULL != strstr(output.c_str(), 126941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Death tests are not supported on this platform")); 127041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_TRUE(NULL != strstr(output.c_str(), ";")); 127141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 127241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot CaptureStderr(); 127341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_DEATH_IF_SUPPORTED(;, "") << "streamed message"; // NOLINT 127441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot output = GetCapturedStderr(); 127541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_TRUE(NULL == strstr(output.c_str(), "streamed message")); 127641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 127741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 127841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid FuncWithAssert(int* n) { 127941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_DEATH_IF_SUPPORTED(return;, ""); 128041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot (*n)++; 128141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 128241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 128341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that ASSERT_DEATH_IF_SUPPORTED does not return from the current 128441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// function (as ASSERT_DEATH does) if death tests are not supported. 128541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(ConditionalDeathMacrosTest, AssertDeatDoesNotReturnhIfUnsupported) { 128641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int n = 0; 128741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FuncWithAssert(&n); 128841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_EQ(1, n); 128941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 1290fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 1291fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesTEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInFastStyle) { 1292fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes testing::GTEST_FLAG(death_test_style) = "fast"; 1293fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_FALSE(InDeathTestChild()); 1294fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_DEATH({ 1295fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside"); 1296fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes fflush(stderr); 1297fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes _exit(1); 1298fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes }, "Inside"); 1299fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes} 1300fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 1301fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesTEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInThreadSafeStyle) { 1302fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes testing::GTEST_FLAG(death_test_style) = "threadsafe"; 1303fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_FALSE(InDeathTestChild()); 1304fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_DEATH({ 1305fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside"); 1306fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes fflush(stderr); 1307fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes _exit(1); 1308fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes }, "Inside"); 1309fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes} 1310fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 13111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_DEATH_TEST 13121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 131341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that the death test macros expand to code which may or may not 131441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// be followed by operator<<, and that in either case the complete text 131541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// comprises only a single C++ statement. 131641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 131741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The syntax should work whether death tests are available or not. 131841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(ConditionalDeathMacrosSyntaxDeathTest, SingleStatement) { 131941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (AlwaysFalse()) 132041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // This would fail if executed; this is a compilation test only 132141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_DEATH_IF_SUPPORTED(return, ""); 132241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 132341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (AlwaysTrue()) 132441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_DEATH_IF_SUPPORTED(_exit(1), ""); 132541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot else 132641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // This empty "else" branch is meant to ensure that EXPECT_DEATH 132741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // doesn't expand into an "if" statement without an "else" 132841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ; // NOLINT 132941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 133041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (AlwaysFalse()) 133141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_DEATH_IF_SUPPORTED(return, "") << "did not die"; 133241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 133341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (AlwaysFalse()) 133441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ; // NOLINT 133541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot else 133641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << 1 << 2 << 3; 133741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 133841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 133941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that conditional death test macros expand to code which interacts 134041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// well with switch statements. 134141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) { 134241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Microsoft compiler usually complains about switch statements without 134341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// case labels. We suppress that warning for this test. 134441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#ifdef _MSC_VER 134541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(push) 134641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(disable: 4065) 134741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // _MSC_VER 134841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 134941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot switch (0) 135041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot default: 135141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_DEATH_IF_SUPPORTED(_exit(1), "") 135241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "exit in default switch handler"; 135341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 135441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot switch (0) 135541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot case 0: 135641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in switch case"; 135741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 135841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#ifdef _MSC_VER 135941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(pop) 136041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // _MSC_VER 136141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 136241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 13631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a test case whose name ends with "DeathTest" works fine 13641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// on Windows. 13651be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(NotADeathTest, Test) { 13661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SUCCEED(); 13671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1368