1 // Copyright 2005, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // Authors: wan (at) google.com (Zhanyong Wan), eefacm (at) gmail.com (Sean Mcafee) 31 // 32 // The Google C++ Testing Framework (Google Test) 33 // 34 // This header file defines internal utilities needed for implementing 35 // death tests. They are subject to change without notice. 36 37 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ 38 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ 39 40 #include <gtest/internal/gtest-internal.h> 41 42 namespace testing { 43 namespace internal { 44 45 GTEST_DECLARE_string(internal_run_death_test); 46 47 // Names of the flags (needed for parsing Google Test flags). 48 const char kDeathTestStyleFlag[] = "death_test_style"; 49 const char kInternalRunDeathTestFlag[] = "internal_run_death_test"; 50 51 #ifdef GTEST_HAS_DEATH_TEST 52 53 // DeathTest is a class that hides much of the complexity of the 54 // GTEST_DEATH_TEST macro. It is abstract; its static Create method 55 // returns a concrete class that depends on the prevailing death test 56 // style, as defined by the --gtest_death_test_style and/or 57 // --gtest_internal_run_death_test flags. 58 59 // In describing the results of death tests, these terms are used with 60 // the corresponding definitions: 61 // 62 // exit status: The integer exit information in the format specified 63 // by wait(2) 64 // exit code: The integer code passed to exit(3), _exit(2), or 65 // returned from main() 66 class DeathTest { 67 public: 68 // Create returns false if there was an error determining the 69 // appropriate action to take for the current death test; for example, 70 // if the gtest_death_test_style flag is set to an invalid value. 71 // The LastMessage method will return a more detailed message in that 72 // case. Otherwise, the DeathTest pointer pointed to by the "test" 73 // argument is set. If the death test should be skipped, the pointer 74 // is set to NULL; otherwise, it is set to the address of a new concrete 75 // DeathTest object that controls the execution of the current test. 76 static bool Create(const char* statement, const RE* regex, 77 const char* file, int line, DeathTest** test); 78 DeathTest(); 79 virtual ~DeathTest() { } 80 81 // A helper class that aborts a death test when it's deleted. 82 class ReturnSentinel { 83 public: 84 explicit ReturnSentinel(DeathTest* test) : test_(test) { } 85 ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); } 86 private: 87 DeathTest* const test_; 88 GTEST_DISALLOW_COPY_AND_ASSIGN(ReturnSentinel); 89 } GTEST_ATTRIBUTE_UNUSED; 90 91 // An enumeration of possible roles that may be taken when a death 92 // test is encountered. EXECUTE means that the death test logic should 93 // be executed immediately. OVERSEE means that the program should prepare 94 // the appropriate environment for a child process to execute the death 95 // test, then wait for it to complete. 96 enum TestRole { OVERSEE_TEST, EXECUTE_TEST }; 97 98 // An enumeration of the two reasons that a test might be aborted. 99 enum AbortReason { TEST_ENCOUNTERED_RETURN_STATEMENT, TEST_DID_NOT_DIE }; 100 101 // Assumes one of the above roles. 102 virtual TestRole AssumeRole() = 0; 103 104 // Waits for the death test to finish and returns its status. 105 virtual int Wait() = 0; 106 107 // Returns true if the death test passed; that is, the test process 108 // exited during the test, its exit status matches a user-supplied 109 // predicate, and its stderr output matches a user-supplied regular 110 // expression. 111 // The user-supplied predicate may be a macro expression rather 112 // than a function pointer or functor, or else Wait and Passed could 113 // be combined. 114 virtual bool Passed(bool exit_status_ok) = 0; 115 116 // Signals that the death test did not die as expected. 117 virtual void Abort(AbortReason reason) = 0; 118 119 // Returns a human-readable outcome message regarding the outcome of 120 // the last death test. 121 static const char* LastMessage(); 122 123 private: 124 GTEST_DISALLOW_COPY_AND_ASSIGN(DeathTest); 125 }; 126 127 // Factory interface for death tests. May be mocked out for testing. 128 class DeathTestFactory { 129 public: 130 virtual ~DeathTestFactory() { } 131 virtual bool Create(const char* statement, const RE* regex, 132 const char* file, int line, DeathTest** test) = 0; 133 }; 134 135 // A concrete DeathTestFactory implementation for normal use. 136 class DefaultDeathTestFactory : public DeathTestFactory { 137 public: 138 virtual bool Create(const char* statement, const RE* regex, 139 const char* file, int line, DeathTest** test); 140 }; 141 142 // Returns true if exit_status describes a process that was terminated 143 // by a signal, or exited normally with a nonzero exit code. 144 bool ExitedUnsuccessfully(int exit_status); 145 146 // This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*, 147 // ASSERT_EXIT*, and EXPECT_EXIT*. 148 #define GTEST_DEATH_TEST(statement, predicate, regex, fail) \ 149 GTEST_AMBIGUOUS_ELSE_BLOCKER \ 150 if (true) { \ 151 const ::testing::internal::RE& gtest_regex = (regex); \ 152 ::testing::internal::DeathTest* gtest_dt; \ 153 if (!::testing::internal::DeathTest::Create(#statement, >est_regex, \ 154 __FILE__, __LINE__, >est_dt)) { \ 155 goto GTEST_CONCAT_TOKEN(gtest_label_, __LINE__); \ 156 } \ 157 if (gtest_dt != NULL) { \ 158 ::testing::internal::scoped_ptr< ::testing::internal::DeathTest> \ 159 gtest_dt_ptr(gtest_dt); \ 160 switch (gtest_dt->AssumeRole()) { \ 161 case ::testing::internal::DeathTest::OVERSEE_TEST: \ 162 if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \ 163 goto GTEST_CONCAT_TOKEN(gtest_label_, __LINE__); \ 164 } \ 165 break; \ 166 case ::testing::internal::DeathTest::EXECUTE_TEST: { \ 167 ::testing::internal::DeathTest::ReturnSentinel \ 168 gtest_sentinel(gtest_dt); \ 169 { statement; } \ 170 gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \ 171 break; \ 172 } \ 173 } \ 174 } \ 175 } else \ 176 GTEST_CONCAT_TOKEN(gtest_label_, __LINE__): \ 177 fail(::testing::internal::DeathTest::LastMessage()) 178 // The symbol "fail" here expands to something into which a message 179 // can be streamed. 180 181 // A struct representing the parsed contents of the 182 // --gtest_internal_run_death_test flag, as it existed when 183 // RUN_ALL_TESTS was called. 184 struct InternalRunDeathTestFlag { 185 String file; 186 int line; 187 int index; 188 int status_fd; 189 }; 190 191 // Returns a newly created InternalRunDeathTestFlag object with fields 192 // initialized from the GTEST_FLAG(internal_run_death_test) flag if 193 // the flag is specified; otherwise returns NULL. 194 InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag(); 195 196 #endif // GTEST_HAS_DEATH_TEST 197 198 } // namespace internal 199 } // namespace testing 200 201 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ 202