38#if GTEST_HAS_DEATH_TEST
41# include <crt_externs.h>
66# include <lib/fdio/io.h>
67# include <lib/fdio/spawn.h>
68# include <zircon/processargs.h>
69# include <zircon/syscalls.h>
92 "Indicates how to run a death test in a forked child process: "
93 "\"threadsafe\" (child process re-executes the test binary "
94 "from the beginning, running only the specific death test) or "
95 "\"fast\" (child process runs the death test immediately "
101 "Instructs to use fork()/_exit() instead of clone() in death tests. "
102 "Ignored and always uses fork() on POSIX systems where clone() is not "
103 "implemented. Useful when running under valgrind or similar tools if "
104 "those do not support clone(). Valgrind 3.3.1 will just fail if "
105 "it sees an unsupported combination of clone() flags. "
106 "It is not recommended to use this flag w/o valgrind though it will "
107 "work in 99% of the cases. Once valgrind is fixed, this flag will "
108 "most likely be removed.");
112 internal_run_death_test,
"",
113 "Indicates the file, line number, temporal index of "
114 "the single death test to run, and a file descriptor to "
115 "which a success code may be sent, all separated by "
116 "the '|' characters. This flag is specified if and only if the current "
117 "process is a sub-process launched for running a thread-safe "
118 "death test. FOR INTERNAL USE ONLY.");
121#if GTEST_HAS_DEATH_TEST
127# if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA
128static bool g_in_fast_death_test_child =
false;
136bool InDeathTestChild() {
137# if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
141 return !
GTEST_FLAG(internal_run_death_test).empty();
145 if (
GTEST_FLAG(death_test_style) ==
"threadsafe")
146 return !
GTEST_FLAG(internal_run_death_test).empty();
148 return g_in_fast_death_test_child;
155ExitedWithCode::ExitedWithCode(
int exit_code) : exit_code_(exit_code) {
159bool ExitedWithCode::operator()(
int exit_status)
const {
160# if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
162 return exit_status == exit_code_;
166 return WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == exit_code_;
171# if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA
173KilledBySignal::KilledBySignal(
int signum) : signum_(signum) {
177bool KilledBySignal::operator()(
int exit_status)
const {
178# if defined(GTEST_KILLED_BY_SIGNAL_OVERRIDE_)
181 if (GTEST_KILLED_BY_SIGNAL_OVERRIDE_(signum_, exit_status, &result)) {
186 return WIFSIGNALED(exit_status) && WTERMSIG(exit_status) == signum_;
196static std::string ExitSummary(
int exit_code) {
199# if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
201 m <<
"Exited with exit status " << exit_code;
205 if (WIFEXITED(exit_code)) {
206 m <<
"Exited with exit status " << WEXITSTATUS(exit_code);
207 }
else if (WIFSIGNALED(exit_code)) {
208 m <<
"Terminated by signal " << WTERMSIG(exit_code);
211 if (WCOREDUMP(exit_code)) {
212 m <<
" (core dumped)";
217 return m.GetString();
222bool ExitedUnsuccessfully(
int exit_status) {
223 return !ExitedWithCode(0)(exit_status);
226# if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA
231static std::string DeathTestThreadWarning(
size_t thread_count) {
233 msg <<
"Death tests use fork(), which is unsafe particularly"
234 <<
" in a threaded context. For this test, " <<
GTEST_NAME_ <<
" ";
235 if (thread_count == 0)
236 msg <<
"couldn't detect the number of threads.";
238 msg <<
"detected " << thread_count <<
" threads.";
239 return msg.GetString();
244static const char kDeathTestLived =
'L';
245static const char kDeathTestReturned =
'R';
246static const char kDeathTestThrew =
'T';
247static const char kDeathTestInternalError =
'I';
252static const int kFuchsiaReadPipeFd = 3;
265enum DeathTestOutcome { IN_PROGRESS, DIED, LIVED, RETURNED, THREW };
272static void DeathTestAbort(
const std::string& message) {
276 const InternalRunDeathTestFlag*
const flag =
280 fputc(kDeathTestInternalError, parent);
281 fprintf(parent,
"%s", message.c_str());
285 fprintf(stderr,
"%s", message.c_str());
293# define GTEST_DEATH_TEST_CHECK_(expression) \
295 if (!::testing::internal::IsTrue(expression)) { \
297 ::std::string("CHECK failed: File ") + __FILE__ + ", line " \
298 + ::testing::internal::StreamableToString(__LINE__) + ": " \
301 } while (::testing::internal::AlwaysFalse())
310# define GTEST_DEATH_TEST_CHECK_SYSCALL_(expression) \
314 gtest_retval = (expression); \
315 } while (gtest_retval == -1 && errno == EINTR); \
316 if (gtest_retval == -1) { \
318 ::std::string("CHECK failed: File ") + __FILE__ + ", line " \
319 + ::testing::internal::StreamableToString(__LINE__) + ": " \
320 + #expression + " != -1"); \
322 } while (::testing::internal::AlwaysFalse())
325std::string GetLastErrnoDescription() {
333static void FailFromInternalError(
int fd) {
339 while ((num_read =
posix::Read(fd, buffer, 255)) > 0) {
340 buffer[num_read] =
'\0';
343 }
while (num_read == -1 && errno == EINTR);
348 const int last_error = errno;
349 GTEST_LOG_(FATAL) <<
"Error while reading death test internal: "
350 << GetLastErrnoDescription() <<
" [" << last_error <<
"]";
356DeathTest::DeathTest() {
359 DeathTestAbort(
"Cannot run a death test outside of a TEST or "
366bool DeathTest::Create(
const char* statement,
const RE* regex,
367 const char* file,
int line, DeathTest** test) {
369 statement, regex, file, line, test);
372const char* DeathTest::LastMessage() {
373 return last_death_test_message_.c_str();
376void DeathTest::set_last_death_test_message(
const std::string& message) {
377 last_death_test_message_ = message;
380std::string DeathTest::last_death_test_message_;
383class DeathTestImpl :
public DeathTest {
385 DeathTestImpl(
const char* a_statement,
const RE* a_regex)
386 : statement_(a_statement),
390 outcome_(IN_PROGRESS),
395 ~DeathTestImpl() { GTEST_DEATH_TEST_CHECK_(read_fd_ == -1); }
397 void Abort(AbortReason reason);
398 virtual bool Passed(
bool status_ok);
400 const char* statement()
const {
return statement_; }
401 const RE* regex()
const {
return regex_; }
402 bool spawned()
const {
return spawned_; }
403 void set_spawned(
bool is_spawned) { spawned_ = is_spawned; }
404 int status()
const {
return status_; }
405 void set_status(
int a_status) { status_ = a_status; }
406 DeathTestOutcome outcome()
const {
return outcome_; }
407 void set_outcome(DeathTestOutcome an_outcome) { outcome_ = an_outcome; }
408 int read_fd()
const {
return read_fd_; }
409 void set_read_fd(
int fd) { read_fd_ = fd; }
410 int write_fd()
const {
return write_fd_; }
411 void set_write_fd(
int fd) { write_fd_ = fd; }
417 void ReadAndInterpretStatusByte();
422 const char*
const statement_;
425 const RE*
const regex_;
431 DeathTestOutcome outcome_;
446void DeathTestImpl::ReadAndInterpretStatusByte() {
456 }
while (bytes_read == -1 && errno == EINTR);
458 if (bytes_read == 0) {
460 }
else if (bytes_read == 1) {
462 case kDeathTestReturned:
463 set_outcome(RETURNED);
465 case kDeathTestThrew:
468 case kDeathTestLived:
471 case kDeathTestInternalError:
472 FailFromInternalError(read_fd());
475 GTEST_LOG_(FATAL) <<
"Death test child process reported "
476 <<
"unexpected status byte ("
477 <<
static_cast<unsigned int>(flag) <<
")";
480 GTEST_LOG_(FATAL) <<
"Read from death test child process failed: "
481 << GetLastErrnoDescription();
483 GTEST_DEATH_TEST_CHECK_SYSCALL_(
posix::Close(read_fd()));
491void DeathTestImpl::Abort(AbortReason reason) {
495 const char status_ch =
496 reason == TEST_DID_NOT_DIE ? kDeathTestLived :
497 reason == TEST_THREW_EXCEPTION ? kDeathTestThrew : kDeathTestReturned;
499 GTEST_DEATH_TEST_CHECK_SYSCALL_(
posix::Write(write_fd(), &status_ch, 1));
514static ::std::string FormatDeathTestOutput(const ::std::string& output) {
516 for (
size_t at = 0; ; ) {
517 const size_t line_end = output.find(
'\n', at);
519 if (line_end == ::std::string::npos) {
520 ret += output.substr(at);
523 ret += output.substr(at, line_end + 1 - at);
551bool DeathTestImpl::Passed(
bool status_ok) {
560 buffer <<
"Death test: " << statement() <<
"\n";
563 buffer <<
" Result: failed to die.\n"
564 <<
" Error msg:\n" << FormatDeathTestOutput(error_message);
567 buffer <<
" Result: threw an exception.\n"
568 <<
" Error msg:\n" << FormatDeathTestOutput(error_message);
571 buffer <<
" Result: illegal return in test statement.\n"
572 <<
" Error msg:\n" << FormatDeathTestOutput(error_message);
586 buffer <<
" Result: died but not with expected error.\n"
587 <<
" Expected: " << regex()->pattern() <<
"\n"
588 <<
"Actual msg:\n" << FormatDeathTestOutput(error_message);
591 buffer <<
" Result: died but not with expected exit code:\n"
592 <<
" " << ExitSummary(status()) <<
"\n"
593 <<
"Actual msg:\n" << FormatDeathTestOutput(error_message);
599 <<
"DeathTest::Passed somehow called before conclusion of test";
602 DeathTest::set_last_death_test_message(buffer.GetString());
635class WindowsDeathTest :
public DeathTestImpl {
637 WindowsDeathTest(
const char* a_statement,
641 : DeathTestImpl(a_statement, a_regex), file_(file), line_(line) {}
645 virtual TestRole AssumeRole();
649 const char*
const file_;
653 AutoHandle write_handle_;
655 AutoHandle child_handle_;
660 AutoHandle event_handle_;
666int WindowsDeathTest::Wait() {
672 const HANDLE wait_handles[2] = { child_handle_.Get(), event_handle_.Get() };
673 switch (::WaitForMultipleObjects(2,
678 case WAIT_OBJECT_0 + 1:
681 GTEST_DEATH_TEST_CHECK_(
false);
686 write_handle_.Reset();
687 event_handle_.Reset();
689 ReadAndInterpretStatusByte();
695 GTEST_DEATH_TEST_CHECK_(
696 WAIT_OBJECT_0 == ::WaitForSingleObject(child_handle_.Get(),
699 GTEST_DEATH_TEST_CHECK_(
700 ::GetExitCodeProcess(child_handle_.Get(), &status_code) !=
FALSE);
701 child_handle_.Reset();
702 set_status(
static_cast<int>(status_code));
711DeathTest::TestRole WindowsDeathTest::AssumeRole() {
713 const InternalRunDeathTestFlag*
const flag =
714 impl->internal_run_death_test_flag();
715 const TestInfo*
const info = impl->current_test_info();
716 const int death_test_index = info->result()->death_test_count();
721 set_write_fd(flag->write_fd());
727 SECURITY_ATTRIBUTES handles_are_inheritable = {
728 sizeof(SECURITY_ATTRIBUTES), NULL,
TRUE };
729 HANDLE read_handle, write_handle;
730 GTEST_DEATH_TEST_CHECK_(
731 ::CreatePipe(&read_handle, &write_handle, &handles_are_inheritable,
734 set_read_fd(::_open_osfhandle(
reinterpret_cast<intptr_t>(read_handle),
736 write_handle_.Reset(write_handle);
737 event_handle_.Reset(::CreateEvent(
738 &handles_are_inheritable,
742 GTEST_DEATH_TEST_CHECK_(event_handle_.Get() != NULL);
743 const std::string filter_flag =
745 info->test_case_name() +
"." + info->name();
746 const std::string internal_flag =
757 char executable_path[_MAX_PATH + 1];
758 GTEST_DEATH_TEST_CHECK_(
759 _MAX_PATH + 1 != ::GetModuleFileNameA(NULL,
763 std::string command_line =
764 std::string(::GetCommandLineA()) +
" " + filter_flag +
" \"" +
765 internal_flag +
"\"";
767 DeathTest::set_last_death_test_message(
"");
774 STARTUPINFOA startup_info;
775 memset(&startup_info, 0,
sizeof(STARTUPINFO));
776 startup_info.dwFlags = STARTF_USESTDHANDLES;
777 startup_info.hStdInput = ::GetStdHandle(STD_INPUT_HANDLE);
778 startup_info.hStdOutput = ::GetStdHandle(STD_OUTPUT_HANDLE);
779 startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE);
781 PROCESS_INFORMATION process_info;
782 GTEST_DEATH_TEST_CHECK_(::CreateProcessA(
784 const_cast<char*
>(command_line.c_str()),
792 &process_info) !=
FALSE);
793 child_handle_.Reset(process_info.hProcess);
794 ::CloseHandle(process_info.hThread);
799# elif GTEST_OS_FUCHSIA
801class FuchsiaDeathTest :
public DeathTestImpl {
803 FuchsiaDeathTest(
const char* a_statement,
807 : DeathTestImpl(a_statement, a_regex), file_(file), line_(line) {}
811 virtual TestRole AssumeRole();
815 const char*
const file_;
819 zx_handle_t child_process_;
826 args_.push_back(NULL);
830 for (std::vector<char*>::iterator i = args_.begin(); i != args_.end();
835 void AddArgument(
const char* argument) {
839 template <
typename Str>
840 void AddArguments(const ::std::vector<Str>& arguments) {
841 for (typename ::std::vector<Str>::const_iterator i = arguments.begin();
842 i != arguments.end();
847 char*
const* Argv() {
852 return args_.size() - 1;
856 std::vector<char*> args_;
862int FuchsiaDeathTest::Wait() {
867 zx_status_t status_zx;
868 zx_signals_t signals;
869 status_zx = zx_object_wait_one(
871 ZX_PROCESS_TERMINATED,
874 GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK);
876 ReadAndInterpretStatusByte();
878 zx_info_process_t buffer;
879 status_zx = zx_object_get_info(
886 GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK);
888 GTEST_DEATH_TEST_CHECK_(buffer.exited);
889 set_status(buffer.return_code);
898DeathTest::TestRole FuchsiaDeathTest::AssumeRole() {
900 const InternalRunDeathTestFlag*
const flag =
901 impl->internal_run_death_test_flag();
902 const TestInfo*
const info = impl->current_test_info();
903 const int death_test_index = info->result()->death_test_count();
908 set_write_fd(kFuchsiaReadPipeFd);
917 const std::string filter_flag =
919 + info->test_case_name() +
"." + info->name();
920 const std::string internal_flag =
926 args.AddArguments(GetInjectableArgvs());
927 args.AddArgument(filter_flag.c_str());
928 args.AddArgument(internal_flag.c_str());
932 zx_handle_t child_pipe_handle;
934 status = fdio_pipe_half(&child_pipe_handle, &type);
935 GTEST_DEATH_TEST_CHECK_(status >= 0);
939 fdio_spawn_action_t add_handle_action = {
940 .action = FDIO_SPAWN_ACTION_ADD_HANDLE,
942 .id = PA_HND(type, kFuchsiaReadPipeFd),
943 .handle = child_pipe_handle
948 status = fdio_spawn_etc(ZX_HANDLE_INVALID, FDIO_SPAWN_CLONE_ALL,
949 args.Argv()[0], args.Argv(),
nullptr, 1,
950 &add_handle_action, &child_process_,
nullptr);
951 GTEST_DEATH_TEST_CHECK_(status == ZX_OK);
962class ForkingDeathTest :
public DeathTestImpl {
964 ForkingDeathTest(
const char* statement,
const RE* regex);
970 void set_child_pid(pid_t child_pid) { child_pid_ = child_pid; }
978ForkingDeathTest::ForkingDeathTest(
const char* a_statement,
const RE* a_regex)
979 : DeathTestImpl(a_statement, a_regex),
985int ForkingDeathTest::Wait() {
989 ReadAndInterpretStatusByte();
992 GTEST_DEATH_TEST_CHECK_SYSCALL_(waitpid(child_pid_, &status_value, 0));
993 set_status(status_value);
999class NoExecDeathTest :
public ForkingDeathTest {
1001 NoExecDeathTest(
const char* a_statement,
const RE* a_regex) :
1002 ForkingDeathTest(a_statement, a_regex) { }
1003 virtual TestRole AssumeRole();
1008DeathTest::TestRole NoExecDeathTest::AssumeRole() {
1010 if (thread_count != 1) {
1011 GTEST_LOG_(WARNING) << DeathTestThreadWarning(thread_count);
1015 GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1);
1017 DeathTest::set_last_death_test_message(
"");
1028 const pid_t child_pid = fork();
1029 GTEST_DEATH_TEST_CHECK_(child_pid != -1);
1030 set_child_pid(child_pid);
1031 if (child_pid == 0) {
1032 GTEST_DEATH_TEST_CHECK_SYSCALL_(
close(pipe_fd[0]));
1033 set_write_fd(pipe_fd[1]);
1040 GetUnitTestImpl()->listeners()->SuppressEventForwarding();
1041 g_in_fast_death_test_child =
true;
1042 return EXECUTE_TEST;
1044 GTEST_DEATH_TEST_CHECK_SYSCALL_(
close(pipe_fd[1]));
1045 set_read_fd(pipe_fd[0]);
1047 return OVERSEE_TEST;
1054class ExecDeathTest :
public ForkingDeathTest {
1056 ExecDeathTest(
const char* a_statement,
const RE* a_regex,
1057 const char* file,
int line) :
1058 ForkingDeathTest(a_statement, a_regex), file_(file), line_(line) { }
1059 virtual TestRole AssumeRole();
1061 static ::std::vector<std::string> GetArgvsForDeathTestChildProcess() {
1062 ::std::vector<std::string> args = GetInjectableArgvs();
1063# if defined(GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_)
1064 ::std::vector<std::string> extra_args =
1065 GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_();
1066 args.insert(args.end(), extra_args.begin(), extra_args.end());
1071 const char*
const file_;
1080 args_.push_back(NULL);
1084 for (std::vector<char*>::iterator i = args_.begin(); i != args_.end();
1089 void AddArgument(
const char* argument) {
1093 template <
typename Str>
1094 void AddArguments(const ::std::vector<Str>& arguments) {
1095 for (typename ::std::vector<Str>::const_iterator i = arguments.begin();
1096 i != arguments.end();
1101 char*
const* Argv() {
1106 std::vector<char*> args_;
1111struct ExecDeathTestArgs {
1117inline char** GetEnviron() {
1121 return *_NSGetEnviron();
1126extern "C" char** environ;
1127inline char** GetEnviron() {
return environ; }
1134static int ExecDeathTestChildMain(
void* child_arg) {
1135 ExecDeathTestArgs*
const args =
static_cast<ExecDeathTestArgs*
>(child_arg);
1136 GTEST_DEATH_TEST_CHECK_SYSCALL_(
close(args->close_fd));
1141 const char*
const original_dir =
1144 if (chdir(original_dir) != 0) {
1145 DeathTestAbort(std::string(
"chdir(\"") + original_dir +
"\") failed: " +
1146 GetLastErrnoDescription());
1147 return EXIT_FAILURE;
1155 execve(args->argv[0], args->argv, GetEnviron());
1156 DeathTestAbort(std::string(
"execve(") + args->argv[0] +
", ...) in " +
1157 original_dir +
" failed: " +
1158 GetLastErrnoDescription());
1159 return EXIT_FAILURE;
1173static void StackLowerThanAddress(
const void* ptr,
1175static void StackLowerThanAddress(
const void* ptr,
bool* result) {
1177 *result = (&
dummy < ptr);
1182static bool StackGrowsDown() {
1185 StackLowerThanAddress(&dummy, &result);
1197static pid_t ExecDeathTestSpawnChild(
char*
const*
argv,
int close_fd) {
1198 ExecDeathTestArgs args = {
argv, close_fd };
1199 pid_t child_pid = -1;
1204 const int cwd_fd =
open(
".", O_RDONLY);
1205 GTEST_DEATH_TEST_CHECK_(cwd_fd != -1);
1206 GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(cwd_fd, F_SETFD, FD_CLOEXEC));
1210 const char*
const original_dir =
1213 if (chdir(original_dir) != 0) {
1214 DeathTestAbort(std::string(
"chdir(\"") + original_dir +
"\") failed: " +
1215 GetLastErrnoDescription());
1216 return EXIT_FAILURE;
1221 GTEST_DEATH_TEST_CHECK_SYSCALL_(fd_flags = fcntl(close_fd, F_GETFD));
1222 GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(close_fd, F_SETFD,
1223 fd_flags | FD_CLOEXEC));
1224 struct inheritance inherit = {0};
1226 child_pid = spawn(args.argv[0], 0, NULL, &inherit, args.argv, GetEnviron());
1228 GTEST_DEATH_TEST_CHECK_(fchdir(cwd_fd) != -1);
1229 GTEST_DEATH_TEST_CHECK_SYSCALL_(
close(cwd_fd));
1236 struct sigaction saved_sigprof_action;
1237 struct sigaction ignore_sigprof_action;
1238 memset(&ignore_sigprof_action, 0,
sizeof(ignore_sigprof_action));
1239 sigemptyset(&ignore_sigprof_action.sa_mask);
1240 ignore_sigprof_action.sa_handler = SIG_IGN;
1241 GTEST_DEATH_TEST_CHECK_SYSCALL_(sigaction(
1242 SIGPROF, &ignore_sigprof_action, &saved_sigprof_action));
1246 const bool use_fork =
GTEST_FLAG(death_test_use_fork);
1249 static const bool stack_grows_down = StackGrowsDown();
1250 const size_t stack_size = getpagesize();
1252 void*
const stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
1253 MAP_ANON | MAP_PRIVATE, -1, 0);
1254 GTEST_DEATH_TEST_CHECK_(stack != MAP_FAILED);
1262 const size_t kMaxStackAlignment = 64;
1263 void*
const stack_top =
1264 static_cast<char*
>(
stack) +
1265 (stack_grows_down ? stack_size - kMaxStackAlignment : 0);
1266 GTEST_DEATH_TEST_CHECK_(stack_size > kMaxStackAlignment &&
1267 reinterpret_cast<intptr_t>(stack_top) % kMaxStackAlignment == 0);
1269 child_pid = clone(&ExecDeathTestChildMain, stack_top, SIGCHLD, &args);
1271 GTEST_DEATH_TEST_CHECK_(munmap(stack, stack_size) != -1);
1274 const bool use_fork =
true;
1277 if (use_fork && (child_pid = fork()) == 0) {
1278 ExecDeathTestChildMain(&args);
1283 GTEST_DEATH_TEST_CHECK_SYSCALL_(
1284 sigaction(SIGPROF, &saved_sigprof_action, NULL));
1287 GTEST_DEATH_TEST_CHECK_(child_pid != -1);
1295DeathTest::TestRole ExecDeathTest::AssumeRole() {
1296 const UnitTestImpl*
const impl = GetUnitTestImpl();
1297 const InternalRunDeathTestFlag*
const flag =
1298 impl->internal_run_death_test_flag();
1299 const TestInfo*
const info = impl->current_test_info();
1300 const int death_test_index = info->result()->death_test_count();
1303 set_write_fd(flag->write_fd());
1304 return EXECUTE_TEST;
1308 GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1);
1311 GTEST_DEATH_TEST_CHECK_(fcntl(pipe_fd[1], F_SETFD, 0) != -1);
1313 const std::string filter_flag =
1315 + info->test_case_name() +
"." + info->name();
1316 const std::string internal_flag =
1318 + file_ +
"|" + StreamableToString(line_) +
"|"
1319 + StreamableToString(death_test_index) +
"|"
1320 + StreamableToString(pipe_fd[1]);
1322 args.AddArguments(GetArgvsForDeathTestChildProcess());
1323 args.AddArgument(filter_flag.c_str());
1324 args.AddArgument(internal_flag.c_str());
1326 DeathTest::set_last_death_test_message(
"");
1333 const pid_t child_pid = ExecDeathTestSpawnChild(args.Argv(), pipe_fd[0]);
1334 GTEST_DEATH_TEST_CHECK_SYSCALL_(
close(pipe_fd[1]));
1335 set_child_pid(child_pid);
1336 set_read_fd(pipe_fd[0]);
1338 return OVERSEE_TEST;
1348bool DefaultDeathTestFactory::Create(
const char* statement,
const RE* regex,
1349 const char* file,
int line,
1351 UnitTestImpl*
const impl = GetUnitTestImpl();
1352 const InternalRunDeathTestFlag*
const flag =
1353 impl->internal_run_death_test_flag();
1354 const int death_test_index = impl->current_test_info()
1355 ->increment_death_test_count();
1358 if (death_test_index > flag->index()) {
1359 DeathTest::set_last_death_test_message(
1360 "Death test count (" + StreamableToString(death_test_index)
1361 +
") somehow exceeded expected maximum ("
1362 + StreamableToString(flag->index()) +
")");
1366 if (!(flag->file() == file && flag->line() == line &&
1367 flag->index() == death_test_index)) {
1373# if GTEST_OS_WINDOWS
1375 if (
GTEST_FLAG(death_test_style) ==
"threadsafe" ||
1377 *
test =
new WindowsDeathTest(statement, regex, file, line);
1380# elif GTEST_OS_FUCHSIA
1382 if (
GTEST_FLAG(death_test_style) ==
"threadsafe" ||
1384 *
test =
new FuchsiaDeathTest(statement, regex, file, line);
1389 if (
GTEST_FLAG(death_test_style) ==
"threadsafe") {
1390 *
test =
new ExecDeathTest(statement, regex, file, line);
1391 }
else if (
GTEST_FLAG(death_test_style) ==
"fast") {
1392 *
test =
new NoExecDeathTest(statement, regex);
1398 DeathTest::set_last_death_test_message(
1399 "Unknown death test style \"" +
GTEST_FLAG(death_test_style)
1400 +
"\" encountered");
1407# if GTEST_OS_WINDOWS
1411static int GetStatusFileDescriptor(
unsigned int parent_process_id,
1412 size_t write_handle_as_size_t,
1413 size_t event_handle_as_size_t) {
1414 AutoHandle parent_process_handle(::OpenProcess(PROCESS_DUP_HANDLE,
1416 parent_process_id));
1417 if (parent_process_handle.Get() == INVALID_HANDLE_VALUE) {
1418 DeathTestAbort(
"Unable to open parent process " +
1419 StreamableToString(parent_process_id));
1426 const HANDLE write_handle =
1427 reinterpret_cast<HANDLE
>(write_handle_as_size_t);
1428 HANDLE dup_write_handle;
1433 if (!::DuplicateHandle(parent_process_handle.Get(), write_handle,
1434 ::GetCurrentProcess(), &dup_write_handle,
1438 DUPLICATE_SAME_ACCESS)) {
1439 DeathTestAbort(
"Unable to duplicate the pipe handle " +
1440 StreamableToString(write_handle_as_size_t) +
1441 " from the parent process " +
1442 StreamableToString(parent_process_id));
1445 const HANDLE event_handle =
reinterpret_cast<HANDLE
>(event_handle_as_size_t);
1446 HANDLE dup_event_handle;
1448 if (!::DuplicateHandle(parent_process_handle.Get(), event_handle,
1449 ::GetCurrentProcess(), &dup_event_handle,
1452 DUPLICATE_SAME_ACCESS)) {
1453 DeathTestAbort(
"Unable to duplicate the event handle " +
1454 StreamableToString(event_handle_as_size_t) +
1455 " from the parent process " +
1456 StreamableToString(parent_process_id));
1459 const int write_fd =
1460 ::_open_osfhandle(
reinterpret_cast<intptr_t>(dup_write_handle), O_APPEND);
1461 if (write_fd == -1) {
1462 DeathTestAbort(
"Unable to convert pipe handle " +
1463 StreamableToString(write_handle_as_size_t) +
1464 " to a file descriptor");
1469 ::SetEvent(dup_event_handle);
1478InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() {
1479 if (
GTEST_FLAG(internal_run_death_test) ==
"")
return NULL;
1485 ::std::vector< ::std::string> fields;
1489# if GTEST_OS_WINDOWS
1491 unsigned int parent_process_id = 0;
1492 size_t write_handle_as_size_t = 0;
1493 size_t event_handle_as_size_t = 0;
1495 if (fields.size() != 6
1496 || !ParseNaturalNumber(fields[1], &line)
1497 || !ParseNaturalNumber(fields[2], &index)
1498 || !ParseNaturalNumber(fields[3], &parent_process_id)
1499 || !ParseNaturalNumber(fields[4], &write_handle_as_size_t)
1500 || !ParseNaturalNumber(fields[5], &event_handle_as_size_t)) {
1501 DeathTestAbort(
"Bad --gtest_internal_run_death_test flag: " +
1504 write_fd = GetStatusFileDescriptor(parent_process_id,
1505 write_handle_as_size_t,
1506 event_handle_as_size_t);
1508# elif GTEST_OS_FUCHSIA
1510 if (fields.size() != 3
1511 || !ParseNaturalNumber(fields[1], &line)
1512 || !ParseNaturalNumber(fields[2], &index)) {
1513 DeathTestAbort(
"Bad --gtest_internal_run_death_test flag: "
1519 if (fields.size() != 4
1520 || !ParseNaturalNumber(fields[1], &line)
1521 || !ParseNaturalNumber(fields[2], &index)
1522 || !ParseNaturalNumber(fields[3], &write_fd)) {
1523 DeathTestAbort(
"Bad --gtest_internal_run_death_test flag: "
1529 return new InternalRunDeathTestFlag(fields[0], line, index, write_fd);
static UnitTest * GetInstance()
const char * original_working_dir() const
static bool PartialMatch(const ::std::string &str, const RE &re)
TestInfo * current_test_info()
#define GTEST_FLAG_PREFIX_
#define GTEST_DEFINE_bool_(name, default_val, doc)
#define GTEST_DEFAULT_DEATH_TEST_STYLE
#define GTEST_DEFINE_string_(name, default_val, doc)
#define GTEST_LOG_(severity)
#define GTEST_CHECK_(condition)
#define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
void close(T *e, websocketpp::connection_hdl hdl)
constexpr enabler dummy
An instance to use in EnableIf.
int Read(int fd, void *buf, unsigned int count)
char * StrDup(const char *src)
const char * StrError(int errnum)
FILE * FDOpen(int fd, const char *mode)
int Write(int fd, const void *buf, unsigned int count)
GTEST_API_ std::string GetCapturedStderr()
GTEST_API_ size_t GetThreadCount()
bool BoolFromGTestEnv(const char *flag, bool default_val)
const char * StringFromGTestEnv(const char *flag, const char *default_val)
const char kInternalRunDeathTestFlag[]
void SplitString(const ::std::string &str, char delimiter, ::std::vector< ::std::string > *dest)
GTEST_API_ void CaptureStderr()
class UnitTestImpl * GetUnitTestImpl()
std::string StreamableToString(const T &streamable)
@ test
Unit testing utility error code.
memset(pInfo->slotDescription, ' ', 64)