gtest-death-test-internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. // The Google C++ Testing and Mocking Framework (Google Test)
  31. //
  32. // This header file defines internal utilities needed for implementing
  33. // death tests. They are subject to change without notice.
  34. // GOOGLETEST_CM0001 DO NOT DELETE
  35. #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
  36. #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
  37. #include "gtest/internal/gtest-internal.h"
  38. #include <stdio.h>
  39. namespace testing {
  40. namespace internal {
  41. GTEST_DECLARE_string_(internal_run_death_test);
  42. // Names of the flags (needed for parsing Google Test flags).
  43. const char kDeathTestStyleFlag[] = "death_test_style";
  44. const char kDeathTestUseFork[] = "death_test_use_fork";
  45. const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
  46. #if GTEST_HAS_DEATH_TEST
  47. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
  48. /* class A needs to have dll-interface to be used by clients of class B */)
  49. // DeathTest is a class that hides much of the complexity of the
  50. // GTEST_DEATH_TEST_ macro. It is abstract; its static Create method
  51. // returns a concrete class that depends on the prevailing death test
  52. // style, as defined by the --gtest_death_test_style and/or
  53. // --gtest_internal_run_death_test flags.
  54. // In describing the results of death tests, these terms are used with
  55. // the corresponding definitions:
  56. //
  57. // exit status: The integer exit information in the format specified
  58. // by wait(2)
  59. // exit code: The integer code passed to exit(3), _exit(2), or
  60. // returned from main()
  61. class GTEST_API_ DeathTest {
  62. public:
  63. // Create returns false if there was an error determining the
  64. // appropriate action to take for the current death test; for example,
  65. // if the gtest_death_test_style flag is set to an invalid value.
  66. // The LastMessage method will return a more detailed message in that
  67. // case. Otherwise, the DeathTest pointer pointed to by the "test"
  68. // argument is set. If the death test should be skipped, the pointer
  69. // is set to NULL; otherwise, it is set to the address of a new concrete
  70. // DeathTest object that controls the execution of the current test.
  71. static bool Create(const char* statement, const RE* regex,
  72. const char* file, int line, DeathTest** test);
  73. DeathTest();
  74. virtual ~DeathTest() { }
  75. // A helper class that aborts a death test when it's deleted.
  76. class ReturnSentinel {
  77. public:
  78. explicit ReturnSentinel(DeathTest* test) : test_(test) { }
  79. ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
  80. private:
  81. DeathTest* const test_;
  82. GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel);
  83. } GTEST_ATTRIBUTE_UNUSED_;
  84. // An enumeration of possible roles that may be taken when a death
  85. // test is encountered. EXECUTE means that the death test logic should
  86. // be executed immediately. OVERSEE means that the program should prepare
  87. // the appropriate environment for a child process to execute the death
  88. // test, then wait for it to complete.
  89. enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
  90. // An enumeration of the three reasons that a test might be aborted.
  91. enum AbortReason {
  92. TEST_ENCOUNTERED_RETURN_STATEMENT,
  93. TEST_THREW_EXCEPTION,
  94. TEST_DID_NOT_DIE
  95. };
  96. // Assumes one of the above roles.
  97. virtual TestRole AssumeRole() = 0;
  98. // Waits for the death test to finish and returns its status.
  99. virtual int Wait() = 0;
  100. // Returns true if the death test passed; that is, the test process
  101. // exited during the test, its exit status matches a user-supplied
  102. // predicate, and its stderr output matches a user-supplied regular
  103. // expression.
  104. // The user-supplied predicate may be a macro expression rather
  105. // than a function pointer or functor, or else Wait and Passed could
  106. // be combined.
  107. virtual bool Passed(bool exit_status_ok) = 0;
  108. // Signals that the death test did not die as expected.
  109. virtual void Abort(AbortReason reason) = 0;
  110. // Returns a human-readable outcome message regarding the outcome of
  111. // the last death test.
  112. static const char* LastMessage();
  113. static void set_last_death_test_message(const std::string& message);
  114. private:
  115. // A string containing a description of the outcome of the last death test.
  116. static std::string last_death_test_message_;
  117. GTEST_DISALLOW_COPY_AND_ASSIGN_(DeathTest);
  118. };
  119. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
  120. // Factory interface for death tests. May be mocked out for testing.
  121. class DeathTestFactory {
  122. public:
  123. virtual ~DeathTestFactory() { }
  124. virtual bool Create(const char* statement, const RE* regex,
  125. const char* file, int line, DeathTest** test) = 0;
  126. };
  127. // A concrete DeathTestFactory implementation for normal use.
  128. class DefaultDeathTestFactory : public DeathTestFactory {
  129. public:
  130. virtual bool Create(const char* statement, const RE* regex,
  131. const char* file, int line, DeathTest** test);
  132. };
  133. // Returns true if exit_status describes a process that was terminated
  134. // by a signal, or exited normally with a nonzero exit code.
  135. GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
  136. // Traps C++ exceptions escaping statement and reports them as test
  137. // failures. Note that trapping SEH exceptions is not implemented here.
  138. # if GTEST_HAS_EXCEPTIONS
  139. # define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
  140. try { \
  141. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
  142. } catch (const ::std::exception& gtest_exception) { \
  143. fprintf(\
  144. stderr, \
  145. "\n%s: Caught std::exception-derived exception escaping the " \
  146. "death test statement. Exception message: %s\n", \
  147. ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \
  148. gtest_exception.what()); \
  149. fflush(stderr); \
  150. death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
  151. } catch (...) { \
  152. death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
  153. }
  154. # else
  155. # define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
  156. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
  157. # endif
  158. // This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*,
  159. // ASSERT_EXIT*, and EXPECT_EXIT*.
  160. # define GTEST_DEATH_TEST_(statement, predicate, regex, fail) \
  161. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  162. if (::testing::internal::AlwaysTrue()) { \
  163. const ::testing::internal::RE& gtest_regex = (regex); \
  164. ::testing::internal::DeathTest* gtest_dt; \
  165. if (!::testing::internal::DeathTest::Create(#statement, &gtest_regex, \
  166. __FILE__, __LINE__, &gtest_dt)) { \
  167. goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
  168. } \
  169. if (gtest_dt != NULL) { \
  170. ::testing::internal::scoped_ptr< ::testing::internal::DeathTest> \
  171. gtest_dt_ptr(gtest_dt); \
  172. switch (gtest_dt->AssumeRole()) { \
  173. case ::testing::internal::DeathTest::OVERSEE_TEST: \
  174. if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \
  175. goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
  176. } \
  177. break; \
  178. case ::testing::internal::DeathTest::EXECUTE_TEST: { \
  179. ::testing::internal::DeathTest::ReturnSentinel \
  180. gtest_sentinel(gtest_dt); \
  181. GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \
  182. gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
  183. break; \
  184. } \
  185. default: \
  186. break; \
  187. } \
  188. } \
  189. } else \
  190. GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__): \
  191. fail(::testing::internal::DeathTest::LastMessage())
  192. // The symbol "fail" here expands to something into which a message
  193. // can be streamed.
  194. // This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in
  195. // NDEBUG mode. In this case we need the statements to be executed and the macro
  196. // must accept a streamed message even though the message is never printed.
  197. // The regex object is not evaluated, but it is used to prevent "unused"
  198. // warnings and to avoid an expression that doesn't compile in debug mode.
  199. #define GTEST_EXECUTE_STATEMENT_(statement, regex) \
  200. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  201. if (::testing::internal::AlwaysTrue()) { \
  202. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
  203. } else if (!::testing::internal::AlwaysTrue()) { \
  204. const ::testing::internal::RE& gtest_regex = (regex); \
  205. static_cast<void>(gtest_regex); \
  206. } else \
  207. ::testing::Message()
  208. // A class representing the parsed contents of the
  209. // --gtest_internal_run_death_test flag, as it existed when
  210. // RUN_ALL_TESTS was called.
  211. class InternalRunDeathTestFlag {
  212. public:
  213. InternalRunDeathTestFlag(const std::string& a_file,
  214. int a_line,
  215. int an_index,
  216. int a_write_fd)
  217. : file_(a_file), line_(a_line), index_(an_index),
  218. write_fd_(a_write_fd) {}
  219. ~InternalRunDeathTestFlag() {
  220. if (write_fd_ >= 0)
  221. posix::Close(write_fd_);
  222. }
  223. const std::string& file() const { return file_; }
  224. int line() const { return line_; }
  225. int index() const { return index_; }
  226. int write_fd() const { return write_fd_; }
  227. private:
  228. std::string file_;
  229. int line_;
  230. int index_;
  231. int write_fd_;
  232. GTEST_DISALLOW_COPY_AND_ASSIGN_(InternalRunDeathTestFlag);
  233. };
  234. // Returns a newly created InternalRunDeathTestFlag object with fields
  235. // initialized from the GTEST_FLAG(internal_run_death_test) flag if
  236. // the flag is specified; otherwise returns NULL.
  237. InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
  238. #endif // GTEST_HAS_DEATH_TEST
  239. } // namespace internal
  240. } // namespace testing
  241. #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_