00001 // Copyright 2005, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // Author: wan@google.com (Zhanyong Wan) 00031 // 00032 // The Google C++ Testing Framework (Google Test) 00033 // 00034 // This header file defines the public API for death tests. It is 00035 // #included by gtest.h so a user doesn't need to include this 00036 // directly. 00037 00038 #ifndef GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ 00039 #define GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ 00040 00041 #include "gtest/internal/gtest-death-test-internal.h" 00042 00043 namespace testing { 00044 00045 // This flag controls the style of death tests. Valid values are "threadsafe", 00046 // meaning that the death test child process will re-execute the test binary 00047 // from the start, running only a single death test, or "fast", 00048 // meaning that the child process will execute the test logic immediately 00049 // after forking. 00050 GTEST_DECLARE_string_(death_test_style); 00051 00052 #if GTEST_HAS_DEATH_TEST 00053 00054 namespace internal { 00055 00056 // Returns a Boolean value indicating whether the caller is currently 00057 // executing in the context of the death test child process. Tools such as 00058 // Valgrind heap checkers may need this to modify their behavior in death 00059 // tests. IMPORTANT: This is an internal utility. Using it may break the 00060 // implementation of death tests. User code MUST NOT use it. 00061 GTEST_API_ bool InDeathTestChild(); 00062 00063 } // namespace internal 00064 00065 // The following macros are useful for writing death tests. 00066 00067 // Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is 00068 // executed: 00069 // 00070 // 1. It generates a warning if there is more than one active 00071 // thread. This is because it's safe to fork() or clone() only 00072 // when there is a single thread. 00073 // 00074 // 2. The parent process clone()s a sub-process and runs the death 00075 // test in it; the sub-process exits with code 0 at the end of the 00076 // death test, if it hasn't exited already. 00077 // 00078 // 3. The parent process waits for the sub-process to terminate. 00079 // 00080 // 4. The parent process checks the exit code and error message of 00081 // the sub-process. 00082 // 00083 // Examples: 00084 // 00085 // ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number"); 00086 // for (int i = 0; i < 5; i++) { 00087 // EXPECT_DEATH(server.ProcessRequest(i), 00088 // "Invalid request .* in ProcessRequest()") 00089 // << "Failed to die on request " << i; 00090 // } 00091 // 00092 // ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting"); 00093 // 00094 // bool KilledBySIGHUP(int exit_code) { 00095 // return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP; 00096 // } 00097 // 00098 // ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!"); 00099 // 00100 // On the regular expressions used in death tests: 00101 // 00102 // On POSIX-compliant systems (*nix), we use the <regex.h> library, 00103 // which uses the POSIX extended regex syntax. 00104 // 00105 // On other platforms (e.g. Windows), we only support a simple regex 00106 // syntax implemented as part of Google Test. This limited 00107 // implementation should be enough most of the time when writing 00108 // death tests; though it lacks many features you can find in PCRE 00109 // or POSIX extended regex syntax. For example, we don't support 00110 // union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and 00111 // repetition count ("x{5,7}"), among others. 00112 // 00113 // Below is the syntax that we do support. We chose it to be a 00114 // subset of both PCRE and POSIX extended regex, so it's easy to 00115 // learn wherever you come from. In the following: 'A' denotes a 00116 // literal character, period (.), or a single \\ escape sequence; 00117 // 'x' and 'y' denote regular expressions; 'm' and 'n' are for 00118 // natural numbers. 00119 // 00120 // c matches any literal character c 00121 // \\d matches any decimal digit 00122 // \\D matches any character that's not a decimal digit 00123 // \\f matches \f 00124 // \\n matches \n 00125 // \\r matches \r 00126 // \\s matches any ASCII whitespace, including \n 00127 // \\S matches any character that's not a whitespace 00128 // \\t matches \t 00129 // \\v matches \v 00130 // \\w matches any letter, _, or decimal digit 00131 // \\W matches any character that \\w doesn't match 00132 // \\c matches any literal character c, which must be a punctuation 00133 // . matches any single character except \n 00134 // A? matches 0 or 1 occurrences of A 00135 // A* matches 0 or many occurrences of A 00136 // A+ matches 1 or many occurrences of A 00137 // ^ matches the beginning of a string (not that of each line) 00138 // $ matches the end of a string (not that of each line) 00139 // xy matches x followed by y 00140 // 00141 // If you accidentally use PCRE or POSIX extended regex features 00142 // not implemented by us, you will get a run-time failure. In that 00143 // case, please try to rewrite your regular expression within the 00144 // above syntax. 00145 // 00146 // This implementation is *not* meant to be as highly tuned or robust 00147 // as a compiled regex library, but should perform well enough for a 00148 // death test, which already incurs significant overhead by launching 00149 // a child process. 00150 // 00151 // Known caveats: 00152 // 00153 // A "threadsafe" style death test obtains the path to the test 00154 // program from argv[0] and re-executes it in the sub-process. For 00155 // simplicity, the current implementation doesn't search the PATH 00156 // when launching the sub-process. This means that the user must 00157 // invoke the test program via a path that contains at least one 00158 // path separator (e.g. path/to/foo_test and 00159 // /absolute/path/to/bar_test are fine, but foo_test is not). This 00160 // is rarely a problem as people usually don't put the test binary 00161 // directory in PATH. 00162 // 00163 // TODO(wan@google.com): make thread-safe death tests search the PATH. 00164 00165 // Asserts that a given statement causes the program to exit, with an 00166 // integer exit status that satisfies predicate, and emitting error output 00167 // that matches regex. 00168 # define ASSERT_EXIT(statement, predicate, regex) \ 00169 GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_FATAL_FAILURE_) 00170 00171 // Like ASSERT_EXIT, but continues on to successive tests in the 00172 // test case, if any: 00173 # define EXPECT_EXIT(statement, predicate, regex) \ 00174 GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_NONFATAL_FAILURE_) 00175 00176 // Asserts that a given statement causes the program to exit, either by 00177 // explicitly exiting with a nonzero exit code or being killed by a 00178 // signal, and emitting error output that matches regex. 00179 # define ASSERT_DEATH(statement, regex) \ 00180 ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex) 00181 00182 // Like ASSERT_DEATH, but continues on to successive tests in the 00183 // test case, if any: 00184 # define EXPECT_DEATH(statement, regex) \ 00185 EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex) 00186 00187 // Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*: 00188 00189 // Tests that an exit code describes a normal exit with a given exit code. 00190 class GTEST_API_ ExitedWithCode { 00191 public: 00192 explicit ExitedWithCode(int exit_code); 00193 bool operator()(int exit_status) const; 00194 private: 00195 // No implementation - assignment is unsupported. 00196 void operator=(const ExitedWithCode& other); 00197 00198 const int exit_code_; 00199 }; 00200 00201 # if !GTEST_OS_WINDOWS 00202 // Tests that an exit code describes an exit due to termination by a 00203 // given signal. 00204 class GTEST_API_ KilledBySignal { 00205 public: 00206 explicit KilledBySignal(int signum); 00207 bool operator()(int exit_status) const; 00208 private: 00209 const int signum_; 00210 }; 00211 # endif // !GTEST_OS_WINDOWS 00212 00213 // EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode. 00214 // The death testing framework causes this to have interesting semantics, 00215 // since the sideeffects of the call are only visible in opt mode, and not 00216 // in debug mode. 00217 // 00218 // In practice, this can be used to test functions that utilize the 00219 // LOG(DFATAL) macro using the following style: 00220 // 00221 // int DieInDebugOr12(int* sideeffect) { 00222 // if (sideeffect) { 00223 // *sideeffect = 12; 00224 // } 00225 // LOG(DFATAL) << "death"; 00226 // return 12; 00227 // } 00228 // 00229 // TEST(TestCase, TestDieOr12WorksInDgbAndOpt) { 00230 // int sideeffect = 0; 00231 // // Only asserts in dbg. 00232 // EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death"); 00233 // 00234 // #ifdef NDEBUG 00235 // // opt-mode has sideeffect visible. 00236 // EXPECT_EQ(12, sideeffect); 00237 // #else 00238 // // dbg-mode no visible sideeffect. 00239 // EXPECT_EQ(0, sideeffect); 00240 // #endif 00241 // } 00242 // 00243 // This will assert that DieInDebugReturn12InOpt() crashes in debug 00244 // mode, usually due to a DCHECK or LOG(DFATAL), but returns the 00245 // appropriate fallback value (12 in this case) in opt mode. If you 00246 // need to test that a function has appropriate side-effects in opt 00247 // mode, include assertions against the side-effects. A general 00248 // pattern for this is: 00249 // 00250 // EXPECT_DEBUG_DEATH({ 00251 // // Side-effects here will have an effect after this statement in 00252 // // opt mode, but none in debug mode. 00253 // EXPECT_EQ(12, DieInDebugOr12(&sideeffect)); 00254 // }, "death"); 00255 // 00256 # ifdef NDEBUG 00257 00258 # define EXPECT_DEBUG_DEATH(statement, regex) \ 00259 GTEST_EXECUTE_STATEMENT_(statement, regex) 00260 00261 # define ASSERT_DEBUG_DEATH(statement, regex) \ 00262 GTEST_EXECUTE_STATEMENT_(statement, regex) 00263 00264 # else 00265 00266 # define EXPECT_DEBUG_DEATH(statement, regex) \ 00267 EXPECT_DEATH(statement, regex) 00268 00269 # define ASSERT_DEBUG_DEATH(statement, regex) \ 00270 ASSERT_DEATH(statement, regex) 00271 00272 # endif // NDEBUG for EXPECT_DEBUG_DEATH 00273 #endif // GTEST_HAS_DEATH_TEST 00274 00275 // EXPECT_DEATH_IF_SUPPORTED(statement, regex) and 00276 // ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if 00277 // death tests are supported; otherwise they just issue a warning. This is 00278 // useful when you are combining death test assertions with normal test 00279 // assertions in one test. 00280 #if GTEST_HAS_DEATH_TEST 00281 # define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ 00282 EXPECT_DEATH(statement, regex) 00283 # define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ 00284 ASSERT_DEATH(statement, regex) 00285 #else 00286 # define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ 00287 GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, ) 00288 # define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ 00289 GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, return) 00290 #endif 00291 00292 } // namespace testing 00293 00294 #endif // GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_