This source file includes following definitions.
- WaitToDie
- SignalChildren
- WaitForChildTermination
- GetSignalFilePath
- MULTIPROCESS_TEST_MAIN
- TEST_F
- MULTIPROCESS_TEST_MAIN
- TEST_F
- TEST_F
- TEST_F
- MULTIPROCESS_TEST_MAIN
- TEST_F
- MULTIPROCESS_TEST_MAIN
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- MULTIPROCESS_TEST_MAIN
- TEST_F
- GetMaxFilesOpenInProcess
- MULTIPROCESS_TEST_MAIN
- CountOpenFDsInChild
- TEST_F
- TestLaunchProcess
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- IsProcessDead
- TEST_F
- MULTIPROCESS_TEST_MAIN
- TEST_F
- MULTIPROCESS_TEST_MAIN
#define _CRT_SECURE_NO_WARNINGS
#include <limits>
#include "base/command_line.h"
#include "base/debug/alias.h"
#include "base/debug/stack_trace.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/posix/eintr_wrapper.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/process/memory.h"
#include "base/process/process.h"
#include "base/process/process_metrics.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/multiprocess_test.h"
#include "base/test/test_timeouts.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"
#if defined(OS_LINUX)
#include <malloc.h>
#include <sched.h>
#endif
#if defined(OS_POSIX)
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/wait.h>
#endif
#if defined(OS_WIN)
#include <windows.h>
#include "base/win/windows_version.h"
#endif
#if defined(OS_MACOSX)
#include <mach/vm_param.h>
#include <malloc/malloc.h>
#endif
using base::FilePath;
namespace {
#if defined(OS_ANDROID)
const char kShellPath[] = "/system/bin/sh";
const char kPosixShell[] = "sh";
#else
const char kShellPath[] = "/bin/sh";
const char kPosixShell[] = "bash";
#endif
const char kSignalFileSlow[] = "SlowChildProcess.die";
const char kSignalFileKill[] = "KilledChildProcess.die";
#if defined(OS_WIN)
const int kExpectedStillRunningExitCode = 0x102;
const int kExpectedKilledExitCode = 1;
#else
const int kExpectedStillRunningExitCode = 0;
#endif
void WaitToDie(const char* filename) {
FILE* fp;
do {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
fp = fopen(filename, "r");
} while (!fp);
fclose(fp);
}
void SignalChildren(const char* filename) {
FILE* fp = fopen(filename, "w");
fclose(fp);
}
base::TerminationStatus WaitForChildTermination(base::ProcessHandle handle,
int* exit_code) {
base::TerminationStatus status = base::TERMINATION_STATUS_STILL_RUNNING;
const base::TimeDelta kInterval = base::TimeDelta::FromMilliseconds(20);
base::TimeDelta waited;
do {
status = base::GetTerminationStatus(handle, exit_code);
base::PlatformThread::Sleep(kInterval);
waited += kInterval;
} while (status == base::TERMINATION_STATUS_STILL_RUNNING &&
#if defined(OS_ANDROID)
waited < TestTimeouts::large_test_timeout());
#else
waited < TestTimeouts::action_max_timeout());
#endif
return status;
}
}
class ProcessUtilTest : public base::MultiProcessTest {
public:
#if defined(OS_POSIX)
int CountOpenFDsInChild();
#endif
static std::string GetSignalFilePath(const char* filename);
};
std::string ProcessUtilTest::GetSignalFilePath(const char* filename) {
#if !defined(OS_ANDROID)
return filename;
#else
FilePath tmp_dir;
PathService::Get(base::DIR_CACHE, &tmp_dir);
tmp_dir = tmp_dir.Append(filename);
return tmp_dir.value();
#endif
}
MULTIPROCESS_TEST_MAIN(SimpleChildProcess) {
return 0;
}
TEST_F(ProcessUtilTest, SpawnChild) {
base::ProcessHandle handle = SpawnChild("SimpleChildProcess");
ASSERT_NE(base::kNullProcessHandle, handle);
EXPECT_TRUE(base::WaitForSingleProcess(
handle, TestTimeouts::action_max_timeout()));
base::CloseProcessHandle(handle);
}
MULTIPROCESS_TEST_MAIN(SlowChildProcess) {
WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileSlow).c_str());
return 0;
}
TEST_F(ProcessUtilTest, KillSlowChild) {
const std::string signal_file =
ProcessUtilTest::GetSignalFilePath(kSignalFileSlow);
remove(signal_file.c_str());
base::ProcessHandle handle = SpawnChild("SlowChildProcess");
ASSERT_NE(base::kNullProcessHandle, handle);
SignalChildren(signal_file.c_str());
EXPECT_TRUE(base::WaitForSingleProcess(
handle, TestTimeouts::action_max_timeout()));
base::CloseProcessHandle(handle);
remove(signal_file.c_str());
}
TEST_F(ProcessUtilTest, DISABLED_GetTerminationStatusExit) {
const std::string signal_file =
ProcessUtilTest::GetSignalFilePath(kSignalFileSlow);
remove(signal_file.c_str());
base::ProcessHandle handle = SpawnChild("SlowChildProcess");
ASSERT_NE(base::kNullProcessHandle, handle);
int exit_code = 42;
EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING,
base::GetTerminationStatus(handle, &exit_code));
EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
SignalChildren(signal_file.c_str());
exit_code = 42;
base::TerminationStatus status =
WaitForChildTermination(handle, &exit_code);
EXPECT_EQ(base::TERMINATION_STATUS_NORMAL_TERMINATION, status);
EXPECT_EQ(0, exit_code);
base::CloseProcessHandle(handle);
remove(signal_file.c_str());
}
#if defined(OS_WIN)
TEST_F(ProcessUtilTest, GetProcId) {
base::ProcessId id1 = base::GetProcId(GetCurrentProcess());
EXPECT_NE(0ul, id1);
base::ProcessHandle handle = SpawnChild("SimpleChildProcess");
ASSERT_NE(base::kNullProcessHandle, handle);
base::ProcessId id2 = base::GetProcId(handle);
EXPECT_NE(0ul, id2);
EXPECT_NE(id1, id2);
base::CloseProcessHandle(handle);
}
#endif
#if !defined(OS_MACOSX)
const char kSignalFileCrash[] = "CrashingChildProcess.die";
MULTIPROCESS_TEST_MAIN(CrashingChildProcess) {
WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileCrash).c_str());
#if defined(OS_POSIX)
::signal(SIGSEGV, SIG_DFL);
#endif
volatile int* oops = NULL;
*oops = 0xDEAD;
return 1;
}
#if defined(ADDRESS_SANITIZER) || \
(defined(OS_WIN) && defined(ARCH_CPU_X86_64)) || defined(SYZYASAN)
#define MAYBE_GetTerminationStatusCrash DISABLED_GetTerminationStatusCrash
#else
#define MAYBE_GetTerminationStatusCrash GetTerminationStatusCrash
#endif
TEST_F(ProcessUtilTest, MAYBE_GetTerminationStatusCrash) {
const std::string signal_file =
ProcessUtilTest::GetSignalFilePath(kSignalFileCrash);
remove(signal_file.c_str());
base::ProcessHandle handle = SpawnChild("CrashingChildProcess");
ASSERT_NE(base::kNullProcessHandle, handle);
int exit_code = 42;
EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING,
base::GetTerminationStatus(handle, &exit_code));
EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
SignalChildren(signal_file.c_str());
exit_code = 42;
base::TerminationStatus status =
WaitForChildTermination(handle, &exit_code);
EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_CRASHED, status);
#if defined(OS_WIN)
EXPECT_EQ(0xc0000005, exit_code);
#elif defined(OS_POSIX)
int signaled = WIFSIGNALED(exit_code);
EXPECT_NE(0, signaled);
int signal = WTERMSIG(exit_code);
EXPECT_EQ(SIGSEGV, signal);
#endif
base::CloseProcessHandle(handle);
base::debug::EnableInProcessStackDumping();
remove(signal_file.c_str());
}
#endif
MULTIPROCESS_TEST_MAIN(KilledChildProcess) {
WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileKill).c_str());
#if defined(OS_WIN)
HANDLE handle = ::OpenProcess(PROCESS_ALL_ACCESS, 0, ::GetCurrentProcessId());
::TerminateProcess(handle, kExpectedKilledExitCode);
#elif defined(OS_POSIX)
::kill(getpid(), SIGKILL);
#endif
return 1;
}
TEST_F(ProcessUtilTest, GetTerminationStatusKill) {
const std::string signal_file =
ProcessUtilTest::GetSignalFilePath(kSignalFileKill);
remove(signal_file.c_str());
base::ProcessHandle handle = SpawnChild("KilledChildProcess");
ASSERT_NE(base::kNullProcessHandle, handle);
int exit_code = 42;
EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING,
base::GetTerminationStatus(handle, &exit_code));
EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
SignalChildren(signal_file.c_str());
exit_code = 42;
base::TerminationStatus status =
WaitForChildTermination(handle, &exit_code);
EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_WAS_KILLED, status);
#if defined(OS_WIN)
EXPECT_EQ(kExpectedKilledExitCode, exit_code);
#elif defined(OS_POSIX)
int signaled = WIFSIGNALED(exit_code);
EXPECT_NE(0, signaled);
int signal = WTERMSIG(exit_code);
EXPECT_EQ(SIGKILL, signal);
#endif
base::CloseProcessHandle(handle);
remove(signal_file.c_str());
}
TEST_F(ProcessUtilTest, SetProcessBackgrounded) {
base::ProcessHandle handle = SpawnChild("SimpleChildProcess");
base::Process process(handle);
int old_priority = process.GetPriority();
#if defined(OS_WIN)
EXPECT_TRUE(process.SetProcessBackgrounded(true));
EXPECT_TRUE(process.IsProcessBackgrounded());
EXPECT_TRUE(process.SetProcessBackgrounded(false));
EXPECT_FALSE(process.IsProcessBackgrounded());
#else
process.SetProcessBackgrounded(true);
process.SetProcessBackgrounded(false);
#endif
int new_priority = process.GetPriority();
EXPECT_EQ(old_priority, new_priority);
}
TEST_F(ProcessUtilTest, SetProcessBackgroundedSelf) {
base::Process process(base::Process::Current().handle());
int old_priority = process.GetPriority();
#if defined(OS_WIN)
EXPECT_TRUE(process.SetProcessBackgrounded(true));
EXPECT_TRUE(process.IsProcessBackgrounded());
EXPECT_TRUE(process.SetProcessBackgrounded(false));
EXPECT_FALSE(process.IsProcessBackgrounded());
#else
process.SetProcessBackgrounded(true);
process.SetProcessBackgrounded(false);
#endif
int new_priority = process.GetPriority();
EXPECT_EQ(old_priority, new_priority);
}
#if defined(OS_WIN)
TEST_F(ProcessUtilTest, GetAppOutput) {
std::string message;
for (int i = 0; i < 1025; i++) {
message += "Hello!";
}
std::string expected(message);
expected += "\r\n";
FilePath cmd(L"cmd.exe");
CommandLine cmd_line(cmd);
cmd_line.AppendArg("/c");
cmd_line.AppendArg("echo " + message + "");
std::string output;
ASSERT_TRUE(base::GetAppOutput(cmd_line, &output));
EXPECT_EQ(expected, output);
CommandLine other_cmd_line(cmd);
other_cmd_line.AppendArg("/c");
cmd_line.AppendArg("echo " + message + " >&2");
output.clear();
ASSERT_TRUE(base::GetAppOutput(other_cmd_line, &output));
EXPECT_EQ("", output);
}
TEST_F(ProcessUtilTest, LaunchAsUser) {
base::UserTokenHandle token;
ASSERT_TRUE(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token));
base::LaunchOptions options;
options.as_user = token;
EXPECT_TRUE(base::LaunchProcess(MakeCmdLine("SimpleChildProcess"), options,
NULL));
}
static const char kEventToTriggerHandleSwitch[] = "event-to-trigger-handle";
MULTIPROCESS_TEST_MAIN(TriggerEventChildProcess) {
std::string handle_value_string =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
kEventToTriggerHandleSwitch);
CHECK(!handle_value_string.empty());
uint64 handle_value_uint64;
CHECK(base::StringToUint64(handle_value_string, &handle_value_uint64));
base::WaitableEvent event(reinterpret_cast<HANDLE>(handle_value_uint64));
event.Signal();
return 0;
}
TEST_F(ProcessUtilTest, InheritSpecifiedHandles) {
SECURITY_ATTRIBUTES security_attributes = {};
security_attributes.nLength = static_cast<DWORD>(sizeof(security_attributes));
security_attributes.lpSecurityDescriptor = NULL;
security_attributes.bInheritHandle = true;
base::WaitableEvent event(
CreateEvent(&security_attributes, true, false, NULL));
base::HandlesToInheritVector handles_to_inherit;
handles_to_inherit.push_back(event.handle());
base::LaunchOptions options;
options.handles_to_inherit = &handles_to_inherit;
CommandLine cmd_line = MakeCmdLine("TriggerEventChildProcess");
cmd_line.AppendSwitchASCII(kEventToTriggerHandleSwitch,
base::Uint64ToString(reinterpret_cast<uint64>(event.handle())));
if (base::win::GetVersion() < base::win::VERSION_VISTA) {
EXPECT_FALSE(base::LaunchProcess(cmd_line, options, NULL));
return;
}
ASSERT_TRUE(base::LaunchProcess(cmd_line, options, NULL));
EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
}
#endif
#if defined(OS_POSIX)
namespace {
int GetMaxFilesOpenInProcess() {
struct rlimit rlim;
if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
return 0;
}
rlim_t max_int = static_cast<rlim_t>(std::numeric_limits<int32>::max());
if (rlim.rlim_cur > max_int) {
return max_int;
}
return rlim.rlim_cur;
}
const int kChildPipe = 20;
}
MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess) {
int num_open_files = 0;
int write_pipe = kChildPipe;
int max_files = GetMaxFilesOpenInProcess();
for (int i = STDERR_FILENO + 1; i < max_files; i++) {
if (i != kChildPipe) {
int fd;
if ((fd = HANDLE_EINTR(dup(i))) != -1) {
close(fd);
num_open_files += 1;
}
}
}
int written = HANDLE_EINTR(write(write_pipe, &num_open_files,
sizeof(num_open_files)));
DCHECK_EQ(static_cast<size_t>(written), sizeof(num_open_files));
int ret = IGNORE_EINTR(close(write_pipe));
DPCHECK(ret == 0);
return 0;
}
int ProcessUtilTest::CountOpenFDsInChild() {
int fds[2];
if (pipe(fds) < 0)
NOTREACHED();
base::FileHandleMappingVector fd_mapping_vec;
fd_mapping_vec.push_back(std::pair<int, int>(fds[1], kChildPipe));
base::LaunchOptions options;
options.fds_to_remap = &fd_mapping_vec;
base::ProcessHandle handle =
SpawnChildWithOptions("ProcessUtilsLeakFDChildProcess", options);
CHECK(handle);
int ret = IGNORE_EINTR(close(fds[1]));
DPCHECK(ret == 0);
int num_open_files = -1;
ssize_t bytes_read =
HANDLE_EINTR(read(fds[0], &num_open_files, sizeof(num_open_files)));
CHECK_EQ(bytes_read, static_cast<ssize_t>(sizeof(num_open_files)));
#if defined(THREAD_SANITIZER)
CHECK(base::WaitForSingleProcess(handle, base::TimeDelta::FromSeconds(3)));
#else
CHECK(base::WaitForSingleProcess(handle, base::TimeDelta::FromSeconds(1)));
#endif
base::CloseProcessHandle(handle);
ret = IGNORE_EINTR(close(fds[0]));
DPCHECK(ret == 0);
return num_open_files;
}
#if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER)
#define MAYBE_FDRemapping DISABLED_FDRemapping
#else
#define MAYBE_FDRemapping FDRemapping
#endif
TEST_F(ProcessUtilTest, MAYBE_FDRemapping) {
int fds_before = CountOpenFDsInChild();
int dev_null = open("/dev/null", O_RDONLY);
int sockets[2];
socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
int fds_after = CountOpenFDsInChild();
ASSERT_EQ(fds_after, fds_before);
int ret;
ret = IGNORE_EINTR(close(sockets[0]));
DPCHECK(ret == 0);
ret = IGNORE_EINTR(close(sockets[1]));
DPCHECK(ret == 0);
ret = IGNORE_EINTR(close(dev_null));
DPCHECK(ret == 0);
}
namespace {
std::string TestLaunchProcess(const base::EnvironmentMap& env_changes,
const int clone_flags) {
std::vector<std::string> args;
base::FileHandleMappingVector fds_to_remap;
args.push_back(kPosixShell);
args.push_back("-c");
args.push_back("echo $BASE_TEST");
int fds[2];
PCHECK(pipe(fds) == 0);
fds_to_remap.push_back(std::make_pair(fds[1], 1));
base::LaunchOptions options;
options.wait = true;
options.environ = env_changes;
options.fds_to_remap = &fds_to_remap;
#if defined(OS_LINUX)
options.clone_flags = clone_flags;
#else
CHECK_EQ(0, clone_flags);
#endif
EXPECT_TRUE(base::LaunchProcess(args, options, NULL));
PCHECK(IGNORE_EINTR(close(fds[1])) == 0);
char buf[512];
const ssize_t n = HANDLE_EINTR(read(fds[0], buf, sizeof(buf)));
PCHECK(n > 0);
PCHECK(IGNORE_EINTR(close(fds[0])) == 0);
return std::string(buf, n);
}
const char kLargeString[] =
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789";
}
TEST_F(ProcessUtilTest, LaunchProcess) {
base::EnvironmentMap env_changes;
const int no_clone_flags = 0;
const char kBaseTest[] = "BASE_TEST";
env_changes[kBaseTest] = "bar";
EXPECT_EQ("bar\n", TestLaunchProcess(env_changes, no_clone_flags));
env_changes.clear();
EXPECT_EQ(0, setenv(kBaseTest, "testing", 1 ));
EXPECT_EQ("testing\n", TestLaunchProcess(env_changes, no_clone_flags));
env_changes[kBaseTest] = std::string();
EXPECT_EQ("\n", TestLaunchProcess(env_changes, no_clone_flags));
env_changes[kBaseTest] = "foo";
EXPECT_EQ("foo\n", TestLaunchProcess(env_changes, no_clone_flags));
env_changes.clear();
EXPECT_EQ(0, setenv(kBaseTest, kLargeString, 1 ));
EXPECT_EQ(std::string(kLargeString) + "\n",
TestLaunchProcess(env_changes, no_clone_flags));
env_changes[kBaseTest] = "wibble";
EXPECT_EQ("wibble\n", TestLaunchProcess(env_changes, no_clone_flags));
#if defined(OS_LINUX)
if (!RunningOnValgrind()) {
EXPECT_EQ("wibble\n", TestLaunchProcess(env_changes, CLONE_FS | SIGCHLD));
}
#endif
}
TEST_F(ProcessUtilTest, GetAppOutput) {
std::string output;
#if defined(OS_ANDROID)
std::vector<std::string> argv;
argv.push_back("sh");
argv.push_back("-c");
argv.push_back("exit 0");
EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output));
EXPECT_STREQ("", output.c_str());
argv[2] = "exit 1";
EXPECT_FALSE(base::GetAppOutput(CommandLine(argv), &output));
EXPECT_STREQ("", output.c_str());
argv[2] = "echo foobar42";
EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output));
EXPECT_STREQ("foobar42\n", output.c_str());
#else
EXPECT_TRUE(base::GetAppOutput(CommandLine(FilePath("true")), &output));
EXPECT_STREQ("", output.c_str());
EXPECT_FALSE(base::GetAppOutput(CommandLine(FilePath("false")), &output));
std::vector<std::string> argv;
argv.push_back("/bin/echo");
argv.push_back("-n");
argv.push_back("foobar42");
EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output));
EXPECT_STREQ("foobar42", output.c_str());
#endif
}
TEST_F(ProcessUtilTest, GetAppOutputRestricted) {
std::vector<std::string> argv;
argv.push_back(std::string(kShellPath));
argv.push_back("-c");
argv.push_back("exit 0");
std::string output = "abc";
EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 100));
EXPECT_STREQ("", output.c_str());
argv[2] = "exit 1";
output = "before";
EXPECT_FALSE(base::GetAppOutputRestricted(CommandLine(argv),
&output, 100));
EXPECT_STREQ("", output.c_str());
argv[2] = "echo 123456789";
output.clear();
EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 10));
EXPECT_STREQ("123456789\n", output.c_str());
output.clear();
EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 5));
EXPECT_STREQ("12345", output.c_str());
output.clear();
EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 15));
EXPECT_STREQ("123456789\n", output.c_str());
output = "abc";
EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 0));
EXPECT_STREQ("", output.c_str());
}
#if !defined(OS_MACOSX) && !defined(OS_OPENBSD)
TEST_F(ProcessUtilTest, GetAppOutputRestrictedSIGPIPE) {
std::vector<std::string> argv;
std::string output;
argv.push_back(std::string(kShellPath));
argv.push_back("-c");
#if defined(OS_ANDROID)
argv.push_back("while echo 12345678901234567890; do :; done");
EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 10));
EXPECT_STREQ("1234567890", output.c_str());
#else
argv.push_back("yes");
EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 10));
EXPECT_STREQ("y\ny\ny\ny\ny\n", output.c_str());
#endif
}
#endif
#if defined(ADDRESS_SANITIZER) && defined(OS_MACOSX) && \
defined(ARCH_CPU_64_BITS)
#define MAYBE_GetAppOutputRestrictedNoZombies \
DISABLED_GetAppOutputRestrictedNoZombies
#else
#define MAYBE_GetAppOutputRestrictedNoZombies GetAppOutputRestrictedNoZombies
#endif
TEST_F(ProcessUtilTest, MAYBE_GetAppOutputRestrictedNoZombies) {
std::vector<std::string> argv;
argv.push_back(std::string(kShellPath));
argv.push_back("-c");
argv.push_back("echo 123456789012345678901234567890");
for (int i = 0; i < 300; i++) {
std::string output;
EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 100));
EXPECT_STREQ("123456789012345678901234567890\n", output.c_str());
}
for (int i = 0; i < 300; i++) {
std::string output;
EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 10));
EXPECT_STREQ("1234567890", output.c_str());
}
}
TEST_F(ProcessUtilTest, GetAppOutputWithExitCode) {
std::vector<std::string> argv;
std::string output;
int exit_code;
argv.push_back(std::string(kShellPath));
argv.push_back("-c");
argv.push_back("echo foo");
EXPECT_TRUE(base::GetAppOutputWithExitCode(CommandLine(argv), &output,
&exit_code));
EXPECT_STREQ("foo\n", output.c_str());
EXPECT_EQ(exit_code, 0);
output.clear();
argv[2] = "echo foo; exit 2";
EXPECT_TRUE(base::GetAppOutputWithExitCode(CommandLine(argv), &output,
&exit_code));
EXPECT_STREQ("foo\n", output.c_str());
EXPECT_EQ(exit_code, 2);
}
TEST_F(ProcessUtilTest, GetParentProcessId) {
base::ProcessId ppid = base::GetParentProcessId(base::GetCurrentProcId());
EXPECT_EQ(ppid, getppid());
}
bool IsProcessDead(base::ProcessHandle child) {
const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG));
return result == -1 && errno == ECHILD;
}
TEST_F(ProcessUtilTest, DelayedTermination) {
base::ProcessHandle child_process = SpawnChild("process_util_test_never_die");
ASSERT_TRUE(child_process);
base::EnsureProcessTerminated(child_process);
base::WaitForSingleProcess(child_process, base::TimeDelta::FromSeconds(5));
EXPECT_TRUE(IsProcessDead(child_process));
base::CloseProcessHandle(child_process);
}
MULTIPROCESS_TEST_MAIN(process_util_test_never_die) {
while (1) {
sleep(500);
}
return 0;
}
TEST_F(ProcessUtilTest, ImmediateTermination) {
base::ProcessHandle child_process =
SpawnChild("process_util_test_die_immediately");
ASSERT_TRUE(child_process);
sleep(2);
base::EnsureProcessTerminated(child_process);
EXPECT_TRUE(IsProcessDead(child_process));
base::CloseProcessHandle(child_process);
}
MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) {
return 0;
}
#endif