gtest-death-test.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. // Author: wan@google.com (Zhanyong Wan)
  31. //
  32. // The Google C++ Testing Framework (Google Test)
  33. //
  34. // This header file defines the public API for death tests. It is
  35. // #included by gtest.h so a user doesn't need to include this
  36. // directly.
  37. #ifndef GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
  38. #define GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
  39. #include "gtest/internal/gtest-death-test-internal.h"
  40. namespace testing {
  41. // This flag controls the style of death tests. Valid values are "threadsafe",
  42. // meaning that the death test child process will re-execute the test binary
  43. // from the start, running only a single death test, or "fast",
  44. // meaning that the child process will execute the test logic immediately
  45. // after forking.
  46. GTEST_DECLARE_string_(death_test_style);
  47. #if GTEST_HAS_DEATH_TEST
  48. namespace internal {
  49. // Returns a Boolean value indicating whether the caller is currently
  50. // executing in the context of the death test child process. Tools such as
  51. // Valgrind heap checkers may need this to modify their behavior in death
  52. // tests. IMPORTANT: This is an internal utility. Using it may break the
  53. // implementation of death tests. User code MUST NOT use it.
  54. GTEST_API_ bool InDeathTestChild();
  55. } // namespace internal
  56. // The following macros are useful for writing death tests.
  57. // Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is
  58. // executed:
  59. //
  60. // 1. It generates a warning if there is more than one active
  61. // thread. This is because it's safe to fork() or clone() only
  62. // when there is a single thread.
  63. //
  64. // 2. The parent process clone()s a sub-process and runs the death
  65. // test in it; the sub-process exits with code 0 at the end of the
  66. // death test, if it hasn't exited already.
  67. //
  68. // 3. The parent process waits for the sub-process to terminate.
  69. //
  70. // 4. The parent process checks the exit code and error message of
  71. // the sub-process.
  72. //
  73. // Examples:
  74. //
  75. // ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number");
  76. // for (int i = 0; i < 5; i++) {
  77. // EXPECT_DEATH(server.ProcessRequest(i),
  78. // "Invalid request .* in ProcessRequest()")
  79. // << "Failed to die on request " << i;
  80. // }
  81. //
  82. // ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting");
  83. //
  84. // bool KilledBySIGHUP(int exit_code) {
  85. // return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP;
  86. // }
  87. //
  88. // ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!");
  89. //
  90. // On the regular expressions used in death tests:
  91. //
  92. // On POSIX-compliant systems (*nix), we use the <regex.h> library,
  93. // which uses the POSIX extended regex syntax.
  94. //
  95. // On other platforms (e.g. Windows), we only support a simple regex
  96. // syntax implemented as part of Google Test. This limited
  97. // implementation should be enough most of the time when writing
  98. // death tests; though it lacks many features you can find in PCRE
  99. // or POSIX extended regex syntax. For example, we don't support
  100. // union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and
  101. // repetition count ("x{5,7}"), among others.
  102. //
  103. // Below is the syntax that we do support. We chose it to be a
  104. // subset of both PCRE and POSIX extended regex, so it's easy to
  105. // learn wherever you come from. In the following: 'A' denotes a
  106. // literal character, period (.), or a single \\ escape sequence;
  107. // 'x' and 'y' denote regular expressions; 'm' and 'n' are for
  108. // natural numbers.
  109. //
  110. // c matches any literal character c
  111. // \\d matches any decimal digit
  112. // \\D matches any character that's not a decimal digit
  113. // \\f matches \f
  114. // \\n matches \n
  115. // \\r matches \r
  116. // \\s matches any ASCII whitespace, including \n
  117. // \\S matches any character that's not a whitespace
  118. // \\t matches \t
  119. // \\v matches \v
  120. // \\w matches any letter, _, or decimal digit
  121. // \\W matches any character that \\w doesn't match
  122. // \\c matches any literal character c, which must be a punctuation
  123. // . matches any single character except \n
  124. // A? matches 0 or 1 occurrences of A
  125. // A* matches 0 or many occurrences of A
  126. // A+ matches 1 or many occurrences of A
  127. // ^ matches the beginning of a string (not that of each line)
  128. // $ matches the end of a string (not that of each line)
  129. // xy matches x followed by y
  130. //
  131. // If you accidentally use PCRE or POSIX extended regex features
  132. // not implemented by us, you will get a run-time failure. In that
  133. // case, please try to rewrite your regular expression within the
  134. // above syntax.
  135. //
  136. // This implementation is *not* meant to be as highly tuned or robust
  137. // as a compiled regex library, but should perform well enough for a
  138. // death test, which already incurs significant overhead by launching
  139. // a child process.
  140. //
  141. // Known caveats:
  142. //
  143. // A "threadsafe" style death test obtains the path to the test
  144. // program from argv[0] and re-executes it in the sub-process. For
  145. // simplicity, the current implementation doesn't search the PATH
  146. // when launching the sub-process. This means that the user must
  147. // invoke the test program via a path that contains at least one
  148. // path separator (e.g. path/to/foo_test and
  149. // /absolute/path/to/bar_test are fine, but foo_test is not). This
  150. // is rarely a problem as people usually don't put the test binary
  151. // directory in PATH.
  152. //
  153. // TODO(wan@google.com): make thread-safe death tests search the PATH.
  154. // Asserts that a given statement causes the program to exit, with an
  155. // integer exit status that satisfies predicate, and emitting error output
  156. // that matches regex.
  157. # define ASSERT_EXIT(statement, predicate, regex) \
  158. GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_FATAL_FAILURE_)
  159. // Like ASSERT_EXIT, but continues on to successive tests in the
  160. // test case, if any:
  161. # define EXPECT_EXIT(statement, predicate, regex) \
  162. GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_NONFATAL_FAILURE_)
  163. // Asserts that a given statement causes the program to exit, either by
  164. // explicitly exiting with a nonzero exit code or being killed by a
  165. // signal, and emitting error output that matches regex.
  166. # define ASSERT_DEATH(statement, regex) \
  167. ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
  168. // Like ASSERT_DEATH, but continues on to successive tests in the
  169. // test case, if any:
  170. # define EXPECT_DEATH(statement, regex) \
  171. EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
  172. // Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:
  173. // Tests that an exit code describes a normal exit with a given exit code.
  174. class GTEST_API_ ExitedWithCode {
  175. public:
  176. explicit ExitedWithCode(int exit_code);
  177. bool operator()(int exit_status) const;
  178. private:
  179. // No implementation - assignment is unsupported.
  180. void operator=(const ExitedWithCode& other);
  181. const int exit_code_;
  182. };
  183. # if !GTEST_OS_WINDOWS
  184. // Tests that an exit code describes an exit due to termination by a
  185. // given signal.
  186. class GTEST_API_ KilledBySignal {
  187. public:
  188. explicit KilledBySignal(int signum);
  189. bool operator()(int exit_status) const;
  190. private:
  191. const int signum_;
  192. };
  193. # endif // !GTEST_OS_WINDOWS
  194. // EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode.
  195. // The death testing framework causes this to have interesting semantics,
  196. // since the sideeffects of the call are only visible in opt mode, and not
  197. // in debug mode.
  198. //
  199. // In practice, this can be used to test functions that utilize the
  200. // LOG(DFATAL) macro using the following style:
  201. //
  202. // int DieInDebugOr12(int* sideeffect) {
  203. // if (sideeffect) {
  204. // *sideeffect = 12;
  205. // }
  206. // LOG(DFATAL) << "death";
  207. // return 12;
  208. // }
  209. //
  210. // TEST(TestCase, TestDieOr12WorksInDgbAndOpt) {
  211. // int sideeffect = 0;
  212. // // Only asserts in dbg.
  213. // EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death");
  214. //
  215. // #ifdef NDEBUG
  216. // // opt-mode has sideeffect visible.
  217. // EXPECT_EQ(12, sideeffect);
  218. // #else
  219. // // dbg-mode no visible sideeffect.
  220. // EXPECT_EQ(0, sideeffect);
  221. // #endif
  222. // }
  223. //
  224. // This will assert that DieInDebugReturn12InOpt() crashes in debug
  225. // mode, usually due to a DCHECK or LOG(DFATAL), but returns the
  226. // appropriate fallback value (12 in this case) in opt mode. If you
  227. // need to test that a function has appropriate side-effects in opt
  228. // mode, include assertions against the side-effects. A general
  229. // pattern for this is:
  230. //
  231. // EXPECT_DEBUG_DEATH({
  232. // // Side-effects here will have an effect after this statement in
  233. // // opt mode, but none in debug mode.
  234. // EXPECT_EQ(12, DieInDebugOr12(&sideeffect));
  235. // }, "death");
  236. //
  237. # ifdef NDEBUG
  238. # define EXPECT_DEBUG_DEATH(statement, regex) \
  239. GTEST_EXECUTE_STATEMENT_(statement, regex)
  240. # define ASSERT_DEBUG_DEATH(statement, regex) \
  241. GTEST_EXECUTE_STATEMENT_(statement, regex)
  242. # else
  243. # define EXPECT_DEBUG_DEATH(statement, regex) \
  244. EXPECT_DEATH(statement, regex)
  245. # define ASSERT_DEBUG_DEATH(statement, regex) \
  246. ASSERT_DEATH(statement, regex)
  247. # endif // NDEBUG for EXPECT_DEBUG_DEATH
  248. #endif // GTEST_HAS_DEATH_TEST
  249. // EXPECT_DEATH_IF_SUPPORTED(statement, regex) and
  250. // ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if
  251. // death tests are supported; otherwise they just issue a warning. This is
  252. // useful when you are combining death test assertions with normal test
  253. // assertions in one test.
  254. #if GTEST_HAS_DEATH_TEST
  255. # define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
  256. EXPECT_DEATH(statement, regex)
  257. # define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
  258. ASSERT_DEATH(statement, regex)
  259. #else
  260. # define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
  261. GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, )
  262. # define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
  263. GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, return)
  264. #endif
  265. } // namespace testing
  266. #endif // GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_