Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 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 | // |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 30 | // Author: wan@google.com (Zhanyong Wan), vladl@google.com (Vlad Losev) |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 31 | // |
| 32 | // This file implements death tests. |
| 33 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 34 | #include "gtest/gtest-death-test.h" |
| 35 | #include "gtest/internal/gtest-port.h" |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 36 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 37 | #if GTEST_HAS_DEATH_TEST |
| 38 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 39 | # if GTEST_OS_MAC |
| 40 | # include <crt_externs.h> |
| 41 | # endif // GTEST_OS_MAC |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 42 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 43 | # include <errno.h> |
| 44 | # include <fcntl.h> |
| 45 | # include <limits.h> |
| 46 | # include <stdarg.h> |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 47 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 48 | # if GTEST_OS_WINDOWS |
| 49 | # include <windows.h> |
| 50 | # else |
| 51 | # include <sys/mman.h> |
| 52 | # include <sys/wait.h> |
| 53 | # endif // GTEST_OS_WINDOWS |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 54 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 55 | #endif // GTEST_HAS_DEATH_TEST |
| 56 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 57 | #include "gtest/gtest-message.h" |
| 58 | #include "gtest/internal/gtest-string.h" |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 59 | |
| 60 | // Indicates that this translation unit is part of Google Test's |
| 61 | // implementation. It must come before gtest-internal-inl.h is |
| 62 | // included, or there will be a compiler error. This trick is to |
| 63 | // prevent a user from accidentally including gtest-internal-inl.h in |
| 64 | // his code. |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 65 | #define GTEST_IMPLEMENTATION_ 1 |
Misha Brukman | e5f9471 | 2009-01-01 02:05:43 +0000 | [diff] [blame] | 66 | #include "gtest/internal/gtest-internal-inl.h" |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 67 | #undef GTEST_IMPLEMENTATION_ |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 68 | |
| 69 | namespace testing { |
| 70 | |
| 71 | // Constants. |
| 72 | |
| 73 | // The default death test style. |
| 74 | static const char kDefaultDeathTestStyle[] = "fast"; |
| 75 | |
| 76 | GTEST_DEFINE_string_( |
| 77 | death_test_style, |
| 78 | internal::StringFromGTestEnv("death_test_style", kDefaultDeathTestStyle), |
| 79 | "Indicates how to run a death test in a forked child process: " |
| 80 | "\"threadsafe\" (child process re-executes the test binary " |
| 81 | "from the beginning, running only the specific death test) or " |
| 82 | "\"fast\" (child process runs the death test immediately " |
| 83 | "after forking)."); |
| 84 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 85 | GTEST_DEFINE_bool_( |
| 86 | death_test_use_fork, |
| 87 | internal::BoolFromGTestEnv("death_test_use_fork", false), |
| 88 | "Instructs to use fork()/_exit() instead of clone() in death tests. " |
| 89 | "Ignored and always uses fork() on POSIX systems where clone() is not " |
| 90 | "implemented. Useful when running under valgrind or similar tools if " |
| 91 | "those do not support clone(). Valgrind 3.3.1 will just fail if " |
| 92 | "it sees an unsupported combination of clone() flags. " |
| 93 | "It is not recommended to use this flag w/o valgrind though it will " |
| 94 | "work in 99% of the cases. Once valgrind is fixed, this flag will " |
| 95 | "most likely be removed."); |
| 96 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 97 | namespace internal { |
| 98 | GTEST_DEFINE_string_( |
| 99 | internal_run_death_test, "", |
| 100 | "Indicates the file, line number, temporal index of " |
| 101 | "the single death test to run, and a file descriptor to " |
| 102 | "which a success code may be sent, all separated by " |
| 103 | "colons. This flag is specified if and only if the current " |
| 104 | "process is a sub-process launched for running a thread-safe " |
| 105 | "death test. FOR INTERNAL USE ONLY."); |
| 106 | } // namespace internal |
| 107 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 108 | #if GTEST_HAS_DEATH_TEST |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 109 | |
| 110 | // ExitedWithCode constructor. |
| 111 | ExitedWithCode::ExitedWithCode(int exit_code) : exit_code_(exit_code) { |
| 112 | } |
| 113 | |
| 114 | // ExitedWithCode function-call operator. |
| 115 | bool ExitedWithCode::operator()(int exit_status) const { |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 116 | # if GTEST_OS_WINDOWS |
| 117 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 118 | return exit_status == exit_code_; |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 119 | |
| 120 | # else |
| 121 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 122 | return WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == exit_code_; |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 123 | |
| 124 | # endif // GTEST_OS_WINDOWS |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 127 | # if !GTEST_OS_WINDOWS |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 128 | // KilledBySignal constructor. |
| 129 | KilledBySignal::KilledBySignal(int signum) : signum_(signum) { |
| 130 | } |
| 131 | |
| 132 | // KilledBySignal function-call operator. |
| 133 | bool KilledBySignal::operator()(int exit_status) const { |
| 134 | return WIFSIGNALED(exit_status) && WTERMSIG(exit_status) == signum_; |
| 135 | } |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 136 | # endif // !GTEST_OS_WINDOWS |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 137 | |
| 138 | namespace internal { |
| 139 | |
| 140 | // Utilities needed for death tests. |
| 141 | |
| 142 | // Generates a textual description of a given exit code, in the format |
| 143 | // specified by wait(2). |
| 144 | static String ExitSummary(int exit_code) { |
| 145 | Message m; |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 146 | |
| 147 | # if GTEST_OS_WINDOWS |
| 148 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 149 | m << "Exited with exit status " << exit_code; |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 150 | |
| 151 | # else |
| 152 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 153 | if (WIFEXITED(exit_code)) { |
| 154 | m << "Exited with exit status " << WEXITSTATUS(exit_code); |
| 155 | } else if (WIFSIGNALED(exit_code)) { |
| 156 | m << "Terminated by signal " << WTERMSIG(exit_code); |
| 157 | } |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 158 | # ifdef WCOREDUMP |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 159 | if (WCOREDUMP(exit_code)) { |
| 160 | m << " (core dumped)"; |
| 161 | } |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 162 | # endif |
| 163 | # endif // GTEST_OS_WINDOWS |
| 164 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 165 | return m.GetString(); |
| 166 | } |
| 167 | |
| 168 | // Returns true if exit_status describes a process that was terminated |
| 169 | // by a signal, or exited normally with a nonzero exit code. |
| 170 | bool ExitedUnsuccessfully(int exit_status) { |
| 171 | return !ExitedWithCode(0)(exit_status); |
| 172 | } |
| 173 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 174 | # if !GTEST_OS_WINDOWS |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 175 | // Generates a textual failure message when a death test finds more than |
| 176 | // one thread running, or cannot determine the number of threads, prior |
| 177 | // to executing the given statement. It is the responsibility of the |
| 178 | // caller not to pass a thread_count of 1. |
| 179 | static String DeathTestThreadWarning(size_t thread_count) { |
| 180 | Message msg; |
| 181 | msg << "Death tests use fork(), which is unsafe particularly" |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 182 | << " in a threaded context. For this test, " << GTEST_NAME_ << " "; |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 183 | if (thread_count == 0) |
| 184 | msg << "couldn't detect the number of threads."; |
| 185 | else |
| 186 | msg << "detected " << thread_count << " threads."; |
| 187 | return msg.GetString(); |
| 188 | } |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 189 | # endif // !GTEST_OS_WINDOWS |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 190 | |
| 191 | // Flag characters for reporting a death test that did not die. |
| 192 | static const char kDeathTestLived = 'L'; |
| 193 | static const char kDeathTestReturned = 'R'; |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 194 | static const char kDeathTestThrew = 'T'; |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 195 | static const char kDeathTestInternalError = 'I'; |
| 196 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 197 | // An enumeration describing all of the possible ways that a death test can |
| 198 | // conclude. DIED means that the process died while executing the test |
| 199 | // code; LIVED means that process lived beyond the end of the test code; |
| 200 | // RETURNED means that the test statement attempted to execute a return |
| 201 | // statement, which is not allowed; THREW means that the test statement |
| 202 | // returned control by throwing an exception. IN_PROGRESS means the test |
| 203 | // has not yet concluded. |
| 204 | // TODO(vladl@google.com): Unify names and possibly values for |
| 205 | // AbortReason, DeathTestOutcome, and flag characters above. |
| 206 | enum DeathTestOutcome { IN_PROGRESS, DIED, LIVED, RETURNED, THREW }; |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 207 | |
| 208 | // Routine for aborting the program which is safe to call from an |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 209 | // exec-style death test child process, in which case the error |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 210 | // message is propagated back to the parent process. Otherwise, the |
| 211 | // message is simply printed to stderr. In either case, the program |
| 212 | // then exits with status 1. |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 213 | void DeathTestAbort(const String& message) { |
| 214 | // On a POSIX system, this function may be called from a threadsafe-style |
| 215 | // death test child process, which operates on a very small stack. Use |
| 216 | // the heap for any additional non-minuscule memory requirements. |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 217 | const InternalRunDeathTestFlag* const flag = |
| 218 | GetUnitTestImpl()->internal_run_death_test_flag(); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 219 | if (flag != NULL) { |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 220 | FILE* parent = posix::FDOpen(flag->write_fd(), "w"); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 221 | fputc(kDeathTestInternalError, parent); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 222 | fprintf(parent, "%s", message.c_str()); |
| 223 | fflush(parent); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 224 | _exit(1); |
| 225 | } else { |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 226 | fprintf(stderr, "%s", message.c_str()); |
| 227 | fflush(stderr); |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 228 | posix::Abort(); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | // A replacement for CHECK that calls DeathTestAbort if the assertion |
| 233 | // fails. |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 234 | # define GTEST_DEATH_TEST_CHECK_(expression) \ |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 235 | do { \ |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 236 | if (!::testing::internal::IsTrue(expression)) { \ |
| 237 | DeathTestAbort(::testing::internal::String::Format( \ |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 238 | "CHECK failed: File %s, line %d: %s", \ |
| 239 | __FILE__, __LINE__, #expression)); \ |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 240 | } \ |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 241 | } while (::testing::internal::AlwaysFalse()) |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 242 | |
| 243 | // This macro is similar to GTEST_DEATH_TEST_CHECK_, but it is meant for |
| 244 | // evaluating any system call that fulfills two conditions: it must return |
| 245 | // -1 on failure, and set errno to EINTR when it is interrupted and |
| 246 | // should be tried again. The macro expands to a loop that repeatedly |
| 247 | // evaluates the expression as long as it evaluates to -1 and sets |
| 248 | // errno to EINTR. If the expression evaluates to -1 but errno is |
| 249 | // something other than EINTR, DeathTestAbort is called. |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 250 | # define GTEST_DEATH_TEST_CHECK_SYSCALL_(expression) \ |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 251 | do { \ |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 252 | int gtest_retval; \ |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 253 | do { \ |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 254 | gtest_retval = (expression); \ |
| 255 | } while (gtest_retval == -1 && errno == EINTR); \ |
| 256 | if (gtest_retval == -1) { \ |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 257 | DeathTestAbort(::testing::internal::String::Format( \ |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 258 | "CHECK failed: File %s, line %d: %s != -1", \ |
| 259 | __FILE__, __LINE__, #expression)); \ |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 260 | } \ |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 261 | } while (::testing::internal::AlwaysFalse()) |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 262 | |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 263 | // Returns the message describing the last system error in errno. |
| 264 | String GetLastErrnoDescription() { |
| 265 | return String(errno == 0 ? "" : posix::StrError(errno)); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 268 | // This is called from a death test parent process to read a failure |
| 269 | // message from the death test child process and log it with the FATAL |
| 270 | // severity. On Windows, the message is read from a pipe handle. On other |
| 271 | // platforms, it is read from a file descriptor. |
| 272 | static void FailFromInternalError(int fd) { |
| 273 | Message error; |
| 274 | char buffer[256]; |
| 275 | int num_read; |
| 276 | |
| 277 | do { |
| 278 | while ((num_read = posix::Read(fd, buffer, 255)) > 0) { |
| 279 | buffer[num_read] = '\0'; |
| 280 | error << buffer; |
| 281 | } |
| 282 | } while (num_read == -1 && errno == EINTR); |
| 283 | |
| 284 | if (num_read == 0) { |
| 285 | GTEST_LOG_(FATAL) << error.GetString(); |
| 286 | } else { |
| 287 | const int last_error = errno; |
| 288 | GTEST_LOG_(FATAL) << "Error while reading death test internal: " |
| 289 | << GetLastErrnoDescription() << " [" << last_error << "]"; |
| 290 | } |
| 291 | } |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 292 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 293 | // Death test constructor. Increments the running death test count |
| 294 | // for the current test. |
| 295 | DeathTest::DeathTest() { |
| 296 | TestInfo* const info = GetUnitTestImpl()->current_test_info(); |
| 297 | if (info == NULL) { |
| 298 | DeathTestAbort("Cannot run a death test outside of a TEST or " |
| 299 | "TEST_F construct"); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // Creates and returns a death test by dispatching to the current |
| 304 | // death test factory. |
| 305 | bool DeathTest::Create(const char* statement, const RE* regex, |
| 306 | const char* file, int line, DeathTest** test) { |
| 307 | return GetUnitTestImpl()->death_test_factory()->Create( |
| 308 | statement, regex, file, line, test); |
| 309 | } |
| 310 | |
| 311 | const char* DeathTest::LastMessage() { |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 312 | return last_death_test_message_.c_str(); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 315 | void DeathTest::set_last_death_test_message(const String& message) { |
| 316 | last_death_test_message_ = message; |
| 317 | } |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 318 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 319 | String DeathTest::last_death_test_message_; |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 320 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 321 | // Provides cross platform implementation for some death functionality. |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 322 | class DeathTestImpl : public DeathTest { |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 323 | protected: |
Benjamin Kramer | 57240ff | 2010-06-02 22:02:30 +0000 | [diff] [blame] | 324 | DeathTestImpl(const char* a_statement, const RE* a_regex) |
| 325 | : statement_(a_statement), |
| 326 | regex_(a_regex), |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 327 | spawned_(false), |
| 328 | status_(-1), |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 329 | outcome_(IN_PROGRESS), |
| 330 | read_fd_(-1), |
| 331 | write_fd_(-1) {} |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 332 | |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 333 | // read_fd_ is expected to be closed and cleared by a derived class. |
| 334 | ~DeathTestImpl() { GTEST_DEATH_TEST_CHECK_(read_fd_ == -1); } |
| 335 | |
| 336 | void Abort(AbortReason reason); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 337 | virtual bool Passed(bool status_ok); |
| 338 | |
| 339 | const char* statement() const { return statement_; } |
| 340 | const RE* regex() const { return regex_; } |
| 341 | bool spawned() const { return spawned_; } |
Benjamin Kramer | 57240ff | 2010-06-02 22:02:30 +0000 | [diff] [blame] | 342 | void set_spawned(bool is_spawned) { spawned_ = is_spawned; } |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 343 | int status() const { return status_; } |
Benjamin Kramer | 57240ff | 2010-06-02 22:02:30 +0000 | [diff] [blame] | 344 | void set_status(int a_status) { status_ = a_status; } |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 345 | DeathTestOutcome outcome() const { return outcome_; } |
Benjamin Kramer | 57240ff | 2010-06-02 22:02:30 +0000 | [diff] [blame] | 346 | void set_outcome(DeathTestOutcome an_outcome) { outcome_ = an_outcome; } |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 347 | int read_fd() const { return read_fd_; } |
| 348 | void set_read_fd(int fd) { read_fd_ = fd; } |
| 349 | int write_fd() const { return write_fd_; } |
| 350 | void set_write_fd(int fd) { write_fd_ = fd; } |
| 351 | |
| 352 | // Called in the parent process only. Reads the result code of the death |
| 353 | // test child process via a pipe, interprets it to set the outcome_ |
| 354 | // member, and closes read_fd_. Outputs diagnostics and terminates in |
| 355 | // case of unexpected codes. |
| 356 | void ReadAndInterpretStatusByte(); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 357 | |
| 358 | private: |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 359 | // The textual content of the code this object is testing. This class |
| 360 | // doesn't own this string and should not attempt to delete it. |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 361 | const char* const statement_; |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 362 | // The regular expression which test output must match. DeathTestImpl |
| 363 | // doesn't own this object and should not attempt to delete it. |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 364 | const RE* const regex_; |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 365 | // True if the death test child process has been successfully spawned. |
| 366 | bool spawned_; |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 367 | // The exit status of the child process. |
| 368 | int status_; |
| 369 | // How the death test concluded. |
| 370 | DeathTestOutcome outcome_; |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 371 | // Descriptor to the read end of the pipe to the child process. It is |
| 372 | // always -1 in the child process. The child keeps its write end of the |
| 373 | // pipe in write_fd_. |
| 374 | int read_fd_; |
| 375 | // Descriptor to the child's write end of the pipe to the parent process. |
| 376 | // It is always -1 in the parent process. The parent keeps its end of the |
| 377 | // pipe in read_fd_. |
| 378 | int write_fd_; |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 379 | }; |
| 380 | |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 381 | // Called in the parent process only. Reads the result code of the death |
| 382 | // test child process via a pipe, interprets it to set the outcome_ |
| 383 | // member, and closes read_fd_. Outputs diagnostics and terminates in |
| 384 | // case of unexpected codes. |
| 385 | void DeathTestImpl::ReadAndInterpretStatusByte() { |
| 386 | char flag; |
| 387 | int bytes_read; |
| 388 | |
| 389 | // The read() here blocks until data is available (signifying the |
| 390 | // failure of the death test) or until the pipe is closed (signifying |
| 391 | // its success), so it's okay to call this in the parent before |
| 392 | // the child process has exited. |
| 393 | do { |
| 394 | bytes_read = posix::Read(read_fd(), &flag, 1); |
| 395 | } while (bytes_read == -1 && errno == EINTR); |
| 396 | |
| 397 | if (bytes_read == 0) { |
| 398 | set_outcome(DIED); |
| 399 | } else if (bytes_read == 1) { |
| 400 | switch (flag) { |
| 401 | case kDeathTestReturned: |
| 402 | set_outcome(RETURNED); |
| 403 | break; |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 404 | case kDeathTestThrew: |
| 405 | set_outcome(THREW); |
| 406 | break; |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 407 | case kDeathTestLived: |
| 408 | set_outcome(LIVED); |
| 409 | break; |
| 410 | case kDeathTestInternalError: |
| 411 | FailFromInternalError(read_fd()); // Does not return. |
| 412 | break; |
| 413 | default: |
| 414 | GTEST_LOG_(FATAL) << "Death test child process reported " |
| 415 | << "unexpected status byte (" |
| 416 | << static_cast<unsigned int>(flag) << ")"; |
| 417 | } |
| 418 | } else { |
| 419 | GTEST_LOG_(FATAL) << "Read from death test child process failed: " |
| 420 | << GetLastErrnoDescription(); |
| 421 | } |
| 422 | GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Close(read_fd())); |
| 423 | set_read_fd(-1); |
| 424 | } |
| 425 | |
| 426 | // Signals that the death test code which should have exited, didn't. |
| 427 | // Should be called only in a death test child process. |
| 428 | // Writes a status byte to the child's status file descriptor, then |
| 429 | // calls _exit(1). |
| 430 | void DeathTestImpl::Abort(AbortReason reason) { |
| 431 | // The parent process considers the death test to be a failure if |
| 432 | // it finds any data in our pipe. So, here we write a single flag byte |
| 433 | // to the pipe, then exit. |
| 434 | const char status_ch = |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 435 | reason == TEST_DID_NOT_DIE ? kDeathTestLived : |
| 436 | reason == TEST_THREW_EXCEPTION ? kDeathTestThrew : kDeathTestReturned; |
| 437 | |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 438 | GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Write(write_fd(), &status_ch, 1)); |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 439 | // We are leaking the descriptor here because on some platforms (i.e., |
| 440 | // when built as Windows DLL), destructors of global objects will still |
| 441 | // run after calling _exit(). On such systems, write_fd_ will be |
| 442 | // indirectly closed from the destructor of UnitTestImpl, causing double |
| 443 | // close if it is also closed here. On debug configurations, double close |
| 444 | // may assert. As there are no in-process buffers to flush here, we are |
| 445 | // relying on the OS to close the descriptor after the process terminates |
| 446 | // when the destructors are not run. |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 447 | _exit(1); // Exits w/o any normal exit hooks (we were supposed to crash) |
| 448 | } |
| 449 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 450 | // Returns an indented copy of stderr output for a death test. |
| 451 | // This makes distinguishing death test output lines from regular log lines |
| 452 | // much easier. |
| 453 | static ::std::string FormatDeathTestOutput(const ::std::string& output) { |
| 454 | ::std::string ret; |
| 455 | for (size_t at = 0; ; ) { |
| 456 | const size_t line_end = output.find('\n', at); |
| 457 | ret += "[ DEATH ] "; |
| 458 | if (line_end == ::std::string::npos) { |
| 459 | ret += output.substr(at); |
| 460 | break; |
| 461 | } |
| 462 | ret += output.substr(at, line_end + 1 - at); |
| 463 | at = line_end + 1; |
| 464 | } |
| 465 | return ret; |
| 466 | } |
| 467 | |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 468 | // Assesses the success or failure of a death test, using both private |
| 469 | // members which have previously been set, and one argument: |
| 470 | // |
| 471 | // Private data members: |
| 472 | // outcome: An enumeration describing how the death test |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 473 | // concluded: DIED, LIVED, THREW, or RETURNED. The death test |
| 474 | // fails in the latter three cases. |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 475 | // status: The exit status of the child process. On *nix, it is in the |
| 476 | // in the format specified by wait(2). On Windows, this is the |
| 477 | // value supplied to the ExitProcess() API or a numeric code |
| 478 | // of the exception that terminated the program. |
| 479 | // regex: A regular expression object to be applied to |
| 480 | // the test's captured standard error output; the death test |
| 481 | // fails if it does not match. |
| 482 | // |
| 483 | // Argument: |
| 484 | // status_ok: true if exit_status is acceptable in the context of |
| 485 | // this particular death test, which fails if it is false |
| 486 | // |
| 487 | // Returns true iff all of the above conditions are met. Otherwise, the |
| 488 | // first failing condition, in the order given above, is the one that is |
| 489 | // reported. Also sets the last death test message string. |
| 490 | bool DeathTestImpl::Passed(bool status_ok) { |
| 491 | if (!spawned()) |
| 492 | return false; |
| 493 | |
| 494 | const String error_message = GetCapturedStderr(); |
| 495 | |
| 496 | bool success = false; |
| 497 | Message buffer; |
| 498 | |
| 499 | buffer << "Death test: " << statement() << "\n"; |
| 500 | switch (outcome()) { |
| 501 | case LIVED: |
| 502 | buffer << " Result: failed to die.\n" |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 503 | << " Error msg:\n" << FormatDeathTestOutput(error_message); |
| 504 | break; |
| 505 | case THREW: |
| 506 | buffer << " Result: threw an exception.\n" |
| 507 | << " Error msg:\n" << FormatDeathTestOutput(error_message); |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 508 | break; |
| 509 | case RETURNED: |
| 510 | buffer << " Result: illegal return in test statement.\n" |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 511 | << " Error msg:\n" << FormatDeathTestOutput(error_message); |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 512 | break; |
| 513 | case DIED: |
| 514 | if (status_ok) { |
| 515 | const bool matched = RE::PartialMatch(error_message.c_str(), *regex()); |
| 516 | if (matched) { |
| 517 | success = true; |
| 518 | } else { |
| 519 | buffer << " Result: died but not with expected error.\n" |
| 520 | << " Expected: " << regex()->pattern() << "\n" |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 521 | << "Actual msg:\n" << FormatDeathTestOutput(error_message); |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 522 | } |
| 523 | } else { |
| 524 | buffer << " Result: died but not with expected exit code:\n" |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 525 | << " " << ExitSummary(status()) << "\n" |
| 526 | << "Actual msg:\n" << FormatDeathTestOutput(error_message); |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 527 | } |
| 528 | break; |
| 529 | case IN_PROGRESS: |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 530 | GTEST_LOG_(FATAL) |
| 531 | << "DeathTest::Passed somehow called before conclusion of test"; |
| 532 | } |
| 533 | |
| 534 | DeathTest::set_last_death_test_message(buffer.GetString()); |
| 535 | return success; |
| 536 | } |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 537 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 538 | # if GTEST_OS_WINDOWS |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 539 | // WindowsDeathTest implements death tests on Windows. Due to the |
| 540 | // specifics of starting new processes on Windows, death tests there are |
| 541 | // always threadsafe, and Google Test considers the |
| 542 | // --gtest_death_test_style=fast setting to be equivalent to |
| 543 | // --gtest_death_test_style=threadsafe there. |
| 544 | // |
| 545 | // A few implementation notes: Like the Linux version, the Windows |
| 546 | // implementation uses pipes for child-to-parent communication. But due to |
| 547 | // the specifics of pipes on Windows, some extra steps are required: |
| 548 | // |
| 549 | // 1. The parent creates a communication pipe and stores handles to both |
| 550 | // ends of it. |
| 551 | // 2. The parent starts the child and provides it with the information |
| 552 | // necessary to acquire the handle to the write end of the pipe. |
| 553 | // 3. The child acquires the write end of the pipe and signals the parent |
| 554 | // using a Windows event. |
| 555 | // 4. Now the parent can release the write end of the pipe on its side. If |
| 556 | // this is done before step 3, the object's reference count goes down to |
| 557 | // 0 and it is destroyed, preventing the child from acquiring it. The |
| 558 | // parent now has to release it, or read operations on the read end of |
| 559 | // the pipe will not return when the child terminates. |
| 560 | // 5. The parent reads child's output through the pipe (outcome code and |
| 561 | // any possible error messages) from the pipe, and its stderr and then |
| 562 | // determines whether to fail the test. |
| 563 | // |
| 564 | // Note: to distinguish Win32 API calls from the local method and function |
| 565 | // calls, the former are explicitly resolved in the global namespace. |
| 566 | // |
| 567 | class WindowsDeathTest : public DeathTestImpl { |
| 568 | public: |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 569 | WindowsDeathTest(const char* a_statement, |
| 570 | const RE* a_regex, |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 571 | const char* file, |
| 572 | int line) |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 573 | : DeathTestImpl(a_statement, a_regex), file_(file), line_(line) {} |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 574 | |
| 575 | // All of these virtual functions are inherited from DeathTest. |
| 576 | virtual int Wait(); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 577 | virtual TestRole AssumeRole(); |
| 578 | |
| 579 | private: |
| 580 | // The name of the file in which the death test is located. |
| 581 | const char* const file_; |
| 582 | // The line number on which the death test is located. |
| 583 | const int line_; |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 584 | // Handle to the write end of the pipe to the child process. |
| 585 | AutoHandle write_handle_; |
| 586 | // Child process handle. |
| 587 | AutoHandle child_handle_; |
| 588 | // Event the child process uses to signal the parent that it has |
| 589 | // acquired the handle to the write end of the pipe. After seeing this |
| 590 | // event the parent can release its own handles to make sure its |
| 591 | // ReadFile() calls return when the child terminates. |
| 592 | AutoHandle event_handle_; |
| 593 | }; |
| 594 | |
| 595 | // Waits for the child in a death test to exit, returning its exit |
| 596 | // status, or 0 if no child process exists. As a side effect, sets the |
| 597 | // outcome data member. |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 598 | int WindowsDeathTest::Wait() { |
| 599 | if (!spawned()) |
| 600 | return 0; |
| 601 | |
| 602 | // Wait until the child either signals that it has acquired the write end |
| 603 | // of the pipe or it dies. |
| 604 | const HANDLE wait_handles[2] = { child_handle_.Get(), event_handle_.Get() }; |
| 605 | switch (::WaitForMultipleObjects(2, |
| 606 | wait_handles, |
| 607 | FALSE, // Waits for any of the handles. |
| 608 | INFINITE)) { |
| 609 | case WAIT_OBJECT_0: |
| 610 | case WAIT_OBJECT_0 + 1: |
| 611 | break; |
| 612 | default: |
| 613 | GTEST_DEATH_TEST_CHECK_(false); // Should not get here. |
| 614 | } |
| 615 | |
| 616 | // The child has acquired the write end of the pipe or exited. |
| 617 | // We release the handle on our side and continue. |
| 618 | write_handle_.Reset(); |
| 619 | event_handle_.Reset(); |
| 620 | |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 621 | ReadAndInterpretStatusByte(); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 622 | |
| 623 | // Waits for the child process to exit if it haven't already. This |
| 624 | // returns immediately if the child has already exited, regardless of |
| 625 | // whether previous calls to WaitForMultipleObjects synchronized on this |
| 626 | // handle or not. |
| 627 | GTEST_DEATH_TEST_CHECK_( |
| 628 | WAIT_OBJECT_0 == ::WaitForSingleObject(child_handle_.Get(), |
| 629 | INFINITE)); |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 630 | DWORD status_code; |
| 631 | GTEST_DEATH_TEST_CHECK_( |
| 632 | ::GetExitCodeProcess(child_handle_.Get(), &status_code) != FALSE); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 633 | child_handle_.Reset(); |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 634 | set_status(static_cast<int>(status_code)); |
| 635 | return status(); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 638 | // The AssumeRole process for a Windows death test. It creates a child |
| 639 | // process with the same executable as the current process to run the |
| 640 | // death test. The child process is given the --gtest_filter and |
| 641 | // --gtest_internal_run_death_test flags such that it knows to run the |
| 642 | // current death test only. |
| 643 | DeathTest::TestRole WindowsDeathTest::AssumeRole() { |
| 644 | const UnitTestImpl* const impl = GetUnitTestImpl(); |
| 645 | const InternalRunDeathTestFlag* const flag = |
| 646 | impl->internal_run_death_test_flag(); |
| 647 | const TestInfo* const info = impl->current_test_info(); |
| 648 | const int death_test_index = info->result()->death_test_count(); |
| 649 | |
| 650 | if (flag != NULL) { |
| 651 | // ParseInternalRunDeathTestFlag() has performed all the necessary |
| 652 | // processing. |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 653 | set_write_fd(flag->write_fd()); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 654 | return EXECUTE_TEST; |
| 655 | } |
| 656 | |
| 657 | // WindowsDeathTest uses an anonymous pipe to communicate results of |
| 658 | // a death test. |
| 659 | SECURITY_ATTRIBUTES handles_are_inheritable = { |
| 660 | sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; |
| 661 | HANDLE read_handle, write_handle; |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 662 | GTEST_DEATH_TEST_CHECK_( |
| 663 | ::CreatePipe(&read_handle, &write_handle, &handles_are_inheritable, |
| 664 | 0) // Default buffer size. |
| 665 | != FALSE); |
| 666 | set_read_fd(::_open_osfhandle(reinterpret_cast<intptr_t>(read_handle), |
| 667 | O_RDONLY)); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 668 | write_handle_.Reset(write_handle); |
| 669 | event_handle_.Reset(::CreateEvent( |
| 670 | &handles_are_inheritable, |
| 671 | TRUE, // The event will automatically reset to non-signaled state. |
| 672 | FALSE, // The initial state is non-signalled. |
| 673 | NULL)); // The even is unnamed. |
| 674 | GTEST_DEATH_TEST_CHECK_(event_handle_.Get() != NULL); |
| 675 | const String filter_flag = String::Format("--%s%s=%s.%s", |
| 676 | GTEST_FLAG_PREFIX_, kFilterFlag, |
| 677 | info->test_case_name(), |
| 678 | info->name()); |
| 679 | const String internal_flag = String::Format( |
| 680 | "--%s%s=%s|%d|%d|%u|%Iu|%Iu", |
| 681 | GTEST_FLAG_PREFIX_, |
| 682 | kInternalRunDeathTestFlag, |
| 683 | file_, line_, |
| 684 | death_test_index, |
| 685 | static_cast<unsigned int>(::GetCurrentProcessId()), |
| 686 | // size_t has the same with as pointers on both 32-bit and 64-bit |
| 687 | // Windows platforms. |
| 688 | // See http://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx. |
| 689 | reinterpret_cast<size_t>(write_handle), |
| 690 | reinterpret_cast<size_t>(event_handle_.Get())); |
| 691 | |
| 692 | char executable_path[_MAX_PATH + 1]; // NOLINT |
| 693 | GTEST_DEATH_TEST_CHECK_( |
| 694 | _MAX_PATH + 1 != ::GetModuleFileNameA(NULL, |
| 695 | executable_path, |
| 696 | _MAX_PATH)); |
| 697 | |
| 698 | String command_line = String::Format("%s %s \"%s\"", |
| 699 | ::GetCommandLineA(), |
| 700 | filter_flag.c_str(), |
| 701 | internal_flag.c_str()); |
| 702 | |
| 703 | DeathTest::set_last_death_test_message(""); |
| 704 | |
| 705 | CaptureStderr(); |
| 706 | // Flush the log buffers since the log streams are shared with the child. |
| 707 | FlushInfoLog(); |
| 708 | |
| 709 | // The child process will share the standard handles with the parent. |
| 710 | STARTUPINFOA startup_info; |
| 711 | memset(&startup_info, 0, sizeof(STARTUPINFO)); |
| 712 | startup_info.dwFlags = STARTF_USESTDHANDLES; |
| 713 | startup_info.hStdInput = ::GetStdHandle(STD_INPUT_HANDLE); |
| 714 | startup_info.hStdOutput = ::GetStdHandle(STD_OUTPUT_HANDLE); |
| 715 | startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE); |
| 716 | |
| 717 | PROCESS_INFORMATION process_info; |
| 718 | GTEST_DEATH_TEST_CHECK_(::CreateProcessA( |
| 719 | executable_path, |
| 720 | const_cast<char*>(command_line.c_str()), |
| 721 | NULL, // Retuned process handle is not inheritable. |
| 722 | NULL, // Retuned thread handle is not inheritable. |
| 723 | TRUE, // Child inherits all inheritable handles (for write_handle_). |
| 724 | 0x0, // Default creation flags. |
| 725 | NULL, // Inherit the parent's environment. |
| 726 | UnitTest::GetInstance()->original_working_dir(), |
| 727 | &startup_info, |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 728 | &process_info) != FALSE); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 729 | child_handle_.Reset(process_info.hProcess); |
| 730 | ::CloseHandle(process_info.hThread); |
| 731 | set_spawned(true); |
| 732 | return OVERSEE_TEST; |
| 733 | } |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 734 | # else // We are not on Windows. |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 735 | |
| 736 | // ForkingDeathTest provides implementations for most of the abstract |
| 737 | // methods of the DeathTest interface. Only the AssumeRole method is |
| 738 | // left undefined. |
| 739 | class ForkingDeathTest : public DeathTestImpl { |
| 740 | public: |
| 741 | ForkingDeathTest(const char* statement, const RE* regex); |
| 742 | |
| 743 | // All of these virtual functions are inherited from DeathTest. |
| 744 | virtual int Wait(); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 745 | |
| 746 | protected: |
| 747 | void set_child_pid(pid_t child_pid) { child_pid_ = child_pid; } |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 748 | |
| 749 | private: |
| 750 | // PID of child process during death test; 0 in the child process itself. |
| 751 | pid_t child_pid_; |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 752 | }; |
| 753 | |
| 754 | // Constructs a ForkingDeathTest. |
Benjamin Kramer | 57240ff | 2010-06-02 22:02:30 +0000 | [diff] [blame] | 755 | ForkingDeathTest::ForkingDeathTest(const char* a_statement, const RE* a_regex) |
| 756 | : DeathTestImpl(a_statement, a_regex), |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 757 | child_pid_(-1) {} |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 758 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 759 | // Waits for the child in a death test to exit, returning its exit |
| 760 | // status, or 0 if no child process exists. As a side effect, sets the |
| 761 | // outcome data member. |
| 762 | int ForkingDeathTest::Wait() { |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 763 | if (!spawned()) |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 764 | return 0; |
| 765 | |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 766 | ReadAndInterpretStatusByte(); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 767 | |
Benjamin Kramer | 57240ff | 2010-06-02 22:02:30 +0000 | [diff] [blame] | 768 | int status_value; |
| 769 | GTEST_DEATH_TEST_CHECK_SYSCALL_(waitpid(child_pid_, &status_value, 0)); |
| 770 | set_status(status_value); |
| 771 | return status_value; |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 772 | } |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 773 | |
| 774 | // A concrete death test class that forks, then immediately runs the test |
| 775 | // in the child process. |
| 776 | class NoExecDeathTest : public ForkingDeathTest { |
| 777 | public: |
Benjamin Kramer | 57240ff | 2010-06-02 22:02:30 +0000 | [diff] [blame] | 778 | NoExecDeathTest(const char* a_statement, const RE* a_regex) : |
| 779 | ForkingDeathTest(a_statement, a_regex) { } |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 780 | virtual TestRole AssumeRole(); |
| 781 | }; |
| 782 | |
| 783 | // The AssumeRole process for a fork-and-run death test. It implements a |
| 784 | // straightforward fork, with a simple pipe to transmit the status byte. |
| 785 | DeathTest::TestRole NoExecDeathTest::AssumeRole() { |
| 786 | const size_t thread_count = GetThreadCount(); |
| 787 | if (thread_count != 1) { |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 788 | GTEST_LOG_(WARNING) << DeathTestThreadWarning(thread_count); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | int pipe_fd[2]; |
| 792 | GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1); |
| 793 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 794 | DeathTest::set_last_death_test_message(""); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 795 | CaptureStderr(); |
| 796 | // When we fork the process below, the log file buffers are copied, but the |
| 797 | // file descriptors are shared. We flush all log files here so that closing |
| 798 | // the file descriptors in the child process doesn't throw off the |
| 799 | // synchronization between descriptors and buffers in the parent process. |
| 800 | // This is as close to the fork as possible to avoid a race condition in case |
| 801 | // there are multiple threads running before the death test, and another |
| 802 | // thread writes to the log file. |
| 803 | FlushInfoLog(); |
| 804 | |
| 805 | const pid_t child_pid = fork(); |
| 806 | GTEST_DEATH_TEST_CHECK_(child_pid != -1); |
| 807 | set_child_pid(child_pid); |
| 808 | if (child_pid == 0) { |
| 809 | GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[0])); |
| 810 | set_write_fd(pipe_fd[1]); |
| 811 | // Redirects all logging to stderr in the child process to prevent |
| 812 | // concurrent writes to the log files. We capture stderr in the parent |
| 813 | // process and append the child process' output to a log. |
| 814 | LogToStderr(); |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 815 | // Event forwarding to the listeners of event listener API mush be shut |
| 816 | // down in death test subprocesses. |
| 817 | GetUnitTestImpl()->listeners()->SuppressEventForwarding(); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 818 | return EXECUTE_TEST; |
| 819 | } else { |
| 820 | GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1])); |
| 821 | set_read_fd(pipe_fd[0]); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 822 | set_spawned(true); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 823 | return OVERSEE_TEST; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | // A concrete death test class that forks and re-executes the main |
| 828 | // program from the beginning, with command-line flags set that cause |
| 829 | // only this specific death test to be run. |
| 830 | class ExecDeathTest : public ForkingDeathTest { |
| 831 | public: |
Benjamin Kramer | 57240ff | 2010-06-02 22:02:30 +0000 | [diff] [blame] | 832 | ExecDeathTest(const char* a_statement, const RE* a_regex, |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 833 | const char* file, int line) : |
Benjamin Kramer | 57240ff | 2010-06-02 22:02:30 +0000 | [diff] [blame] | 834 | ForkingDeathTest(a_statement, a_regex), file_(file), line_(line) { } |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 835 | virtual TestRole AssumeRole(); |
| 836 | private: |
| 837 | // The name of the file in which the death test is located. |
| 838 | const char* const file_; |
| 839 | // The line number on which the death test is located. |
| 840 | const int line_; |
| 841 | }; |
| 842 | |
| 843 | // Utility class for accumulating command-line arguments. |
| 844 | class Arguments { |
| 845 | public: |
| 846 | Arguments() { |
| 847 | args_.push_back(NULL); |
| 848 | } |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 849 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 850 | ~Arguments() { |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 851 | for (std::vector<char*>::iterator i = args_.begin(); i != args_.end(); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 852 | ++i) { |
| 853 | free(*i); |
| 854 | } |
| 855 | } |
| 856 | void AddArgument(const char* argument) { |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 857 | args_.insert(args_.end() - 1, posix::StrDup(argument)); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | template <typename Str> |
| 861 | void AddArguments(const ::std::vector<Str>& arguments) { |
| 862 | for (typename ::std::vector<Str>::const_iterator i = arguments.begin(); |
| 863 | i != arguments.end(); |
| 864 | ++i) { |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 865 | args_.insert(args_.end() - 1, posix::StrDup(i->c_str())); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | char* const* Argv() { |
| 869 | return &args_[0]; |
| 870 | } |
| 871 | private: |
| 872 | std::vector<char*> args_; |
| 873 | }; |
| 874 | |
| 875 | // A struct that encompasses the arguments to the child process of a |
| 876 | // threadsafe-style death test process. |
| 877 | struct ExecDeathTestArgs { |
| 878 | char* const* argv; // Command-line arguments for the child's call to exec |
| 879 | int close_fd; // File descriptor to close; the read end of a pipe |
| 880 | }; |
| 881 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 882 | # if GTEST_OS_MAC |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 883 | inline char** GetEnviron() { |
| 884 | // When Google Test is built as a framework on MacOS X, the environ variable |
| 885 | // is unavailable. Apple's documentation (man environ) recommends using |
| 886 | // _NSGetEnviron() instead. |
| 887 | return *_NSGetEnviron(); |
| 888 | } |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 889 | # else |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 890 | // Some POSIX platforms expect you to declare environ. extern "C" makes |
| 891 | // it reside in the global namespace. |
| 892 | extern "C" char** environ; |
| 893 | inline char** GetEnviron() { return environ; } |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 894 | # endif // GTEST_OS_MAC |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 895 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 896 | // The main function for a threadsafe-style death test child process. |
| 897 | // This function is called in a clone()-ed process and thus must avoid |
| 898 | // any potentially unsafe operations like malloc or libc functions. |
| 899 | static int ExecDeathTestChildMain(void* child_arg) { |
| 900 | ExecDeathTestArgs* const args = static_cast<ExecDeathTestArgs*>(child_arg); |
| 901 | GTEST_DEATH_TEST_CHECK_SYSCALL_(close(args->close_fd)); |
| 902 | |
| 903 | // We need to execute the test program in the same environment where |
| 904 | // it was originally invoked. Therefore we change to the original |
| 905 | // working directory first. |
| 906 | const char* const original_dir = |
| 907 | UnitTest::GetInstance()->original_working_dir(); |
| 908 | // We can safely call chdir() as it's a direct system call. |
| 909 | if (chdir(original_dir) != 0) { |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 910 | DeathTestAbort(String::Format("chdir(\"%s\") failed: %s", |
| 911 | original_dir, |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 912 | GetLastErrnoDescription().c_str())); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 913 | return EXIT_FAILURE; |
| 914 | } |
| 915 | |
| 916 | // We can safely call execve() as it's a direct system call. We |
| 917 | // cannot use execvp() as it's a libc function and thus potentially |
| 918 | // unsafe. Since execve() doesn't search the PATH, the user must |
| 919 | // invoke the test program via a valid path that contains at least |
| 920 | // one path separator. |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 921 | execve(args->argv[0], args->argv, GetEnviron()); |
| 922 | DeathTestAbort(String::Format("execve(%s, ...) in %s failed: %s", |
| 923 | args->argv[0], |
| 924 | original_dir, |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 925 | GetLastErrnoDescription().c_str())); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 926 | return EXIT_FAILURE; |
| 927 | } |
| 928 | |
| 929 | // Two utility routines that together determine the direction the stack |
| 930 | // grows. |
| 931 | // This could be accomplished more elegantly by a single recursive |
| 932 | // function, but we want to guard against the unlikely possibility of |
| 933 | // a smart compiler optimizing the recursion away. |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 934 | // |
| 935 | // GTEST_NO_INLINE_ is required to prevent GCC 4.6 from inlining |
| 936 | // StackLowerThanAddress into StackGrowsDown, which then doesn't give |
| 937 | // correct answer. |
| 938 | bool StackLowerThanAddress(const void* ptr) GTEST_NO_INLINE_; |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 939 | bool StackLowerThanAddress(const void* ptr) { |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 940 | int dummy; |
| 941 | return &dummy < ptr; |
| 942 | } |
| 943 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 944 | bool StackGrowsDown() { |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 945 | int dummy; |
| 946 | return StackLowerThanAddress(&dummy); |
| 947 | } |
| 948 | |
| 949 | // A threadsafe implementation of fork(2) for threadsafe-style death tests |
| 950 | // that uses clone(2). It dies with an error message if anything goes |
| 951 | // wrong. |
| 952 | static pid_t ExecDeathTestFork(char* const* argv, int close_fd) { |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 953 | ExecDeathTestArgs args = { argv, close_fd }; |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 954 | pid_t child_pid = -1; |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 955 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 956 | # if GTEST_HAS_CLONE |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 957 | const bool use_fork = GTEST_FLAG(death_test_use_fork); |
| 958 | |
| 959 | if (!use_fork) { |
| 960 | static const bool stack_grows_down = StackGrowsDown(); |
| 961 | const size_t stack_size = getpagesize(); |
| 962 | // MMAP_ANONYMOUS is not defined on Mac, so we use MAP_ANON instead. |
| 963 | void* const stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE, |
| 964 | MAP_ANON | MAP_PRIVATE, -1, 0); |
| 965 | GTEST_DEATH_TEST_CHECK_(stack != MAP_FAILED); |
| 966 | void* const stack_top = |
| 967 | static_cast<char*>(stack) + (stack_grows_down ? stack_size : 0); |
| 968 | |
| 969 | child_pid = clone(&ExecDeathTestChildMain, stack_top, SIGCHLD, &args); |
| 970 | |
| 971 | GTEST_DEATH_TEST_CHECK_(munmap(stack, stack_size) != -1); |
| 972 | } |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 973 | # else |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 974 | const bool use_fork = true; |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 975 | # endif // GTEST_HAS_CLONE |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 976 | |
| 977 | if (use_fork && (child_pid = fork()) == 0) { |
| 978 | ExecDeathTestChildMain(&args); |
| 979 | _exit(0); |
| 980 | } |
| 981 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 982 | GTEST_DEATH_TEST_CHECK_(child_pid != -1); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 983 | return child_pid; |
| 984 | } |
| 985 | |
| 986 | // The AssumeRole process for a fork-and-exec death test. It re-executes the |
| 987 | // main program from the beginning, setting the --gtest_filter |
| 988 | // and --gtest_internal_run_death_test flags to cause only the current |
| 989 | // death test to be re-run. |
| 990 | DeathTest::TestRole ExecDeathTest::AssumeRole() { |
| 991 | const UnitTestImpl* const impl = GetUnitTestImpl(); |
| 992 | const InternalRunDeathTestFlag* const flag = |
| 993 | impl->internal_run_death_test_flag(); |
| 994 | const TestInfo* const info = impl->current_test_info(); |
| 995 | const int death_test_index = info->result()->death_test_count(); |
| 996 | |
| 997 | if (flag != NULL) { |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 998 | set_write_fd(flag->write_fd()); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 999 | return EXECUTE_TEST; |
| 1000 | } |
| 1001 | |
| 1002 | int pipe_fd[2]; |
| 1003 | GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1); |
| 1004 | // Clear the close-on-exec flag on the write end of the pipe, lest |
| 1005 | // it be closed when the child process does an exec: |
| 1006 | GTEST_DEATH_TEST_CHECK_(fcntl(pipe_fd[1], F_SETFD, 0) != -1); |
| 1007 | |
| 1008 | const String filter_flag = |
| 1009 | String::Format("--%s%s=%s.%s", |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1010 | GTEST_FLAG_PREFIX_, kFilterFlag, |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1011 | info->test_case_name(), info->name()); |
| 1012 | const String internal_flag = |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1013 | String::Format("--%s%s=%s|%d|%d|%d", |
| 1014 | GTEST_FLAG_PREFIX_, kInternalRunDeathTestFlag, |
| 1015 | file_, line_, death_test_index, pipe_fd[1]); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1016 | Arguments args; |
| 1017 | args.AddArguments(GetArgvs()); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1018 | args.AddArgument(filter_flag.c_str()); |
| 1019 | args.AddArgument(internal_flag.c_str()); |
| 1020 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1021 | DeathTest::set_last_death_test_message(""); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1022 | |
| 1023 | CaptureStderr(); |
| 1024 | // See the comment in NoExecDeathTest::AssumeRole for why the next line |
| 1025 | // is necessary. |
| 1026 | FlushInfoLog(); |
| 1027 | |
| 1028 | const pid_t child_pid = ExecDeathTestFork(args.Argv(), pipe_fd[0]); |
| 1029 | GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1])); |
| 1030 | set_child_pid(child_pid); |
| 1031 | set_read_fd(pipe_fd[0]); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1032 | set_spawned(true); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1033 | return OVERSEE_TEST; |
| 1034 | } |
| 1035 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 1036 | # endif // !GTEST_OS_WINDOWS |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1037 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1038 | // Creates a concrete DeathTest-derived class that depends on the |
| 1039 | // --gtest_death_test_style flag, and sets the pointer pointed to |
| 1040 | // by the "test" argument to its address. If the test should be |
| 1041 | // skipped, sets that pointer to NULL. Returns true, unless the |
| 1042 | // flag is set to an invalid value. |
| 1043 | bool DefaultDeathTestFactory::Create(const char* statement, const RE* regex, |
| 1044 | const char* file, int line, |
| 1045 | DeathTest** test) { |
| 1046 | UnitTestImpl* const impl = GetUnitTestImpl(); |
| 1047 | const InternalRunDeathTestFlag* const flag = |
| 1048 | impl->internal_run_death_test_flag(); |
| 1049 | const int death_test_index = impl->current_test_info() |
| 1050 | ->increment_death_test_count(); |
| 1051 | |
| 1052 | if (flag != NULL) { |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1053 | if (death_test_index > flag->index()) { |
| 1054 | DeathTest::set_last_death_test_message(String::Format( |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1055 | "Death test count (%d) somehow exceeded expected maximum (%d)", |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1056 | death_test_index, flag->index())); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1057 | return false; |
| 1058 | } |
| 1059 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1060 | if (!(flag->file() == file && flag->line() == line && |
| 1061 | flag->index() == death_test_index)) { |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1062 | *test = NULL; |
| 1063 | return true; |
| 1064 | } |
| 1065 | } |
| 1066 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 1067 | # if GTEST_OS_WINDOWS |
| 1068 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1069 | if (GTEST_FLAG(death_test_style) == "threadsafe" || |
| 1070 | GTEST_FLAG(death_test_style) == "fast") { |
| 1071 | *test = new WindowsDeathTest(statement, regex, file, line); |
| 1072 | } |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 1073 | |
| 1074 | # else |
| 1075 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1076 | if (GTEST_FLAG(death_test_style) == "threadsafe") { |
| 1077 | *test = new ExecDeathTest(statement, regex, file, line); |
| 1078 | } else if (GTEST_FLAG(death_test_style) == "fast") { |
| 1079 | *test = new NoExecDeathTest(statement, regex); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1080 | } |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 1081 | |
| 1082 | # endif // GTEST_OS_WINDOWS |
| 1083 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1084 | else { // NOLINT - this is more readable than unbalanced brackets inside #if. |
| 1085 | DeathTest::set_last_death_test_message(String::Format( |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1086 | "Unknown death test style \"%s\" encountered", |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1087 | GTEST_FLAG(death_test_style).c_str())); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1088 | return false; |
| 1089 | } |
| 1090 | |
| 1091 | return true; |
| 1092 | } |
| 1093 | |
| 1094 | // Splits a given string on a given delimiter, populating a given |
| 1095 | // vector with the fields. GTEST_HAS_DEATH_TEST implies that we have |
| 1096 | // ::std::string, so we can use it here. |
| 1097 | static void SplitString(const ::std::string& str, char delimiter, |
| 1098 | ::std::vector< ::std::string>* dest) { |
| 1099 | ::std::vector< ::std::string> parsed; |
| 1100 | ::std::string::size_type pos = 0; |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1101 | while (::testing::internal::AlwaysTrue()) { |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1102 | const ::std::string::size_type colon = str.find(delimiter, pos); |
| 1103 | if (colon == ::std::string::npos) { |
| 1104 | parsed.push_back(str.substr(pos)); |
| 1105 | break; |
| 1106 | } else { |
| 1107 | parsed.push_back(str.substr(pos, colon - pos)); |
| 1108 | pos = colon + 1; |
| 1109 | } |
| 1110 | } |
| 1111 | dest->swap(parsed); |
| 1112 | } |
| 1113 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 1114 | # if GTEST_OS_WINDOWS |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1115 | // Recreates the pipe and event handles from the provided parameters, |
| 1116 | // signals the event, and returns a file descriptor wrapped around the pipe |
| 1117 | // handle. This function is called in the child process only. |
| 1118 | int GetStatusFileDescriptor(unsigned int parent_process_id, |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1119 | size_t write_handle_as_size_t, |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1120 | size_t event_handle_as_size_t) { |
| 1121 | AutoHandle parent_process_handle(::OpenProcess(PROCESS_DUP_HANDLE, |
| 1122 | FALSE, // Non-inheritable. |
| 1123 | parent_process_id)); |
| 1124 | if (parent_process_handle.Get() == INVALID_HANDLE_VALUE) { |
| 1125 | DeathTestAbort(String::Format("Unable to open parent process %u", |
| 1126 | parent_process_id)); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1127 | } |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1128 | |
| 1129 | // TODO(vladl@google.com): Replace the following check with a |
| 1130 | // compile-time assertion when available. |
| 1131 | GTEST_CHECK_(sizeof(HANDLE) <= sizeof(size_t)); |
| 1132 | |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1133 | const HANDLE write_handle = |
| 1134 | reinterpret_cast<HANDLE>(write_handle_as_size_t); |
| 1135 | HANDLE dup_write_handle; |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1136 | |
| 1137 | // The newly initialized handle is accessible only in in the parent |
| 1138 | // process. To obtain one accessible within the child, we need to use |
| 1139 | // DuplicateHandle. |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1140 | if (!::DuplicateHandle(parent_process_handle.Get(), write_handle, |
| 1141 | ::GetCurrentProcess(), &dup_write_handle, |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1142 | 0x0, // Requested privileges ignored since |
| 1143 | // DUPLICATE_SAME_ACCESS is used. |
| 1144 | FALSE, // Request non-inheritable handler. |
| 1145 | DUPLICATE_SAME_ACCESS)) { |
| 1146 | DeathTestAbort(String::Format( |
| 1147 | "Unable to duplicate the pipe handle %Iu from the parent process %u", |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1148 | write_handle_as_size_t, parent_process_id)); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1149 | } |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1150 | |
| 1151 | const HANDLE event_handle = reinterpret_cast<HANDLE>(event_handle_as_size_t); |
| 1152 | HANDLE dup_event_handle; |
| 1153 | |
| 1154 | if (!::DuplicateHandle(parent_process_handle.Get(), event_handle, |
| 1155 | ::GetCurrentProcess(), &dup_event_handle, |
| 1156 | 0x0, |
| 1157 | FALSE, |
| 1158 | DUPLICATE_SAME_ACCESS)) { |
| 1159 | DeathTestAbort(String::Format( |
| 1160 | "Unable to duplicate the event handle %Iu from the parent process %u", |
| 1161 | event_handle_as_size_t, parent_process_id)); |
| 1162 | } |
| 1163 | |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1164 | const int write_fd = |
| 1165 | ::_open_osfhandle(reinterpret_cast<intptr_t>(dup_write_handle), O_APPEND); |
| 1166 | if (write_fd == -1) { |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1167 | DeathTestAbort(String::Format( |
| 1168 | "Unable to convert pipe handle %Iu to a file descriptor", |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1169 | write_handle_as_size_t)); |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | // Signals the parent that the write end of the pipe has been acquired |
| 1173 | // so the parent can release its own write end. |
| 1174 | ::SetEvent(dup_event_handle); |
| 1175 | |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1176 | return write_fd; |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1177 | } |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 1178 | # endif // GTEST_OS_WINDOWS |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1179 | |
| 1180 | // Returns a newly created InternalRunDeathTestFlag object with fields |
| 1181 | // initialized from the GTEST_FLAG(internal_run_death_test) flag if |
| 1182 | // the flag is specified; otherwise returns NULL. |
| 1183 | InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() { |
| 1184 | if (GTEST_FLAG(internal_run_death_test) == "") return NULL; |
| 1185 | |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1186 | // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we |
| 1187 | // can use it here. |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1188 | int line = -1; |
| 1189 | int index = -1; |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1190 | ::std::vector< ::std::string> fields; |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1191 | SplitString(GTEST_FLAG(internal_run_death_test).c_str(), '|', &fields); |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1192 | int write_fd = -1; |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1193 | |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 1194 | # if GTEST_OS_WINDOWS |
| 1195 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1196 | unsigned int parent_process_id = 0; |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1197 | size_t write_handle_as_size_t = 0; |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1198 | size_t event_handle_as_size_t = 0; |
| 1199 | |
| 1200 | if (fields.size() != 6 |
| 1201 | || !ParseNaturalNumber(fields[1], &line) |
| 1202 | || !ParseNaturalNumber(fields[2], &index) |
| 1203 | || !ParseNaturalNumber(fields[3], &parent_process_id) |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1204 | || !ParseNaturalNumber(fields[4], &write_handle_as_size_t) |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1205 | || !ParseNaturalNumber(fields[5], &event_handle_as_size_t)) { |
| 1206 | DeathTestAbort(String::Format( |
| 1207 | "Bad --gtest_internal_run_death_test flag: %s", |
| 1208 | GTEST_FLAG(internal_run_death_test).c_str())); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1209 | } |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1210 | write_fd = GetStatusFileDescriptor(parent_process_id, |
| 1211 | write_handle_as_size_t, |
| 1212 | event_handle_as_size_t); |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 1213 | # else |
| 1214 | |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1215 | if (fields.size() != 4 |
| 1216 | || !ParseNaturalNumber(fields[1], &line) |
| 1217 | || !ParseNaturalNumber(fields[2], &index) |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1218 | || !ParseNaturalNumber(fields[3], &write_fd)) { |
Benjamin Kramer | e4b9c93 | 2010-06-02 22:01:25 +0000 | [diff] [blame] | 1219 | DeathTestAbort(String::Format( |
| 1220 | "Bad --gtest_internal_run_death_test flag: %s", |
| 1221 | GTEST_FLAG(internal_run_death_test).c_str())); |
| 1222 | } |
Jay Foad | b33f8e3 | 2011-07-27 09:25:14 +0000 | [diff] [blame] | 1223 | |
| 1224 | # endif // GTEST_OS_WINDOWS |
| 1225 | |
Benjamin Kramer | 190f8ee | 2010-06-02 22:02:11 +0000 | [diff] [blame] | 1226 | return new InternalRunDeathTestFlag(fields[0], line, index, write_fd); |
Misha Brukman | 7ae6ff4 | 2008-12-31 17:34:06 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | } // namespace internal |
| 1230 | |
| 1231 | #endif // GTEST_HAS_DEATH_TEST |
| 1232 | |
| 1233 | } // namespace testing |