1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #define _CRT_SECURE_NO_WARNINGS
9 #include "base/command_line.h"
10 #include "base/debug/alias.h"
11 #include "base/debug/stack_trace.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h"
16 #include "base/posix/eintr_wrapper.h"
17 #include "base/process/kill.h"
18 #include "base/process/launch.h"
19 #include "base/process/memory.h"
20 #include "base/process/process.h"
21 #include "base/process/process_metrics.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/test/multiprocess_test.h"
24 #include "base/test/test_timeouts.h"
25 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
26 #include "base/threading/platform_thread.h"
27 #include "base/threading/thread.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "testing/multiprocess_func_list.h"
41 #include <sys/resource.h>
42 #include <sys/socket.h>
48 #if defined(OS_MACOSX)
49 #include <mach/vm_param.h>
50 #include <malloc/malloc.h>
57 #if defined(OS_ANDROID)
58 const char kShellPath[] = "/system/bin/sh";
59 const char kPosixShell[] = "sh";
61 const char kShellPath[] = "/bin/sh";
62 const char kPosixShell[] = "bash";
65 const char kSignalFileSlow[] = "SlowChildProcess.die";
66 const char kSignalFileKill[] = "KilledChildProcess.die";
69 const int kExpectedStillRunningExitCode = 0x102;
70 const int kExpectedKilledExitCode = 1;
72 const int kExpectedStillRunningExitCode = 0;
75 // Sleeps until file filename is created.
76 void WaitToDie(const char* filename) {
79 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
80 fp = fopen(filename, "r");
85 // Signals children they should die now.
86 void SignalChildren(const char* filename) {
87 FILE* fp = fopen(filename, "w");
91 // Using a pipe to the child to wait for an event was considered, but
92 // there were cases in the past where pipes caused problems (other
93 // libraries closing the fds, child deadlocking). This is a simple
94 // case, so it's not worth the risk. Using wait loops is discouraged
96 base::TerminationStatus WaitForChildTermination(base::ProcessHandle handle,
98 // Now we wait until the result is something other than STILL_RUNNING.
99 base::TerminationStatus status = base::TERMINATION_STATUS_STILL_RUNNING;
100 const base::TimeDelta kInterval = base::TimeDelta::FromMilliseconds(20);
101 base::TimeDelta waited;
103 status = base::GetTerminationStatus(handle, exit_code);
104 base::PlatformThread::Sleep(kInterval);
106 } while (status == base::TERMINATION_STATUS_STILL_RUNNING &&
107 // Waiting for more time for process termination on android devices.
108 #if defined(OS_ANDROID)
109 waited < TestTimeouts::large_test_timeout());
111 waited < TestTimeouts::action_max_timeout());
119 class ProcessUtilTest : public base::MultiProcessTest {
121 #if defined(OS_POSIX)
122 // Spawn a child process that counts how many file descriptors are open.
123 int CountOpenFDsInChild();
125 // Converts the filename to a platform specific filepath.
126 // On Android files can not be created in arbitrary directories.
127 static std::string GetSignalFilePath(const char* filename);
130 std::string ProcessUtilTest::GetSignalFilePath(const char* filename) {
131 #if !defined(OS_ANDROID)
135 PathService::Get(base::DIR_CACHE, &tmp_dir);
136 tmp_dir = tmp_dir.Append(filename);
137 return tmp_dir.value();
141 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) {
145 TEST_F(ProcessUtilTest, SpawnChild) {
146 base::ProcessHandle handle = this->SpawnChild("SimpleChildProcess", false);
147 ASSERT_NE(base::kNullProcessHandle, handle);
148 EXPECT_TRUE(base::WaitForSingleProcess(
149 handle, TestTimeouts::action_max_timeout()));
150 base::CloseProcessHandle(handle);
153 MULTIPROCESS_TEST_MAIN(SlowChildProcess) {
154 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileSlow).c_str());
158 TEST_F(ProcessUtilTest, KillSlowChild) {
159 const std::string signal_file =
160 ProcessUtilTest::GetSignalFilePath(kSignalFileSlow);
161 remove(signal_file.c_str());
162 base::ProcessHandle handle = this->SpawnChild("SlowChildProcess", false);
163 ASSERT_NE(base::kNullProcessHandle, handle);
164 SignalChildren(signal_file.c_str());
165 EXPECT_TRUE(base::WaitForSingleProcess(
166 handle, TestTimeouts::action_max_timeout()));
167 base::CloseProcessHandle(handle);
168 remove(signal_file.c_str());
171 // Times out on Linux and Win, flakes on other platforms, http://crbug.com/95058
172 TEST_F(ProcessUtilTest, DISABLED_GetTerminationStatusExit) {
173 const std::string signal_file =
174 ProcessUtilTest::GetSignalFilePath(kSignalFileSlow);
175 remove(signal_file.c_str());
176 base::ProcessHandle handle = this->SpawnChild("SlowChildProcess", false);
177 ASSERT_NE(base::kNullProcessHandle, handle);
180 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING,
181 base::GetTerminationStatus(handle, &exit_code));
182 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
184 SignalChildren(signal_file.c_str());
186 base::TerminationStatus status =
187 WaitForChildTermination(handle, &exit_code);
188 EXPECT_EQ(base::TERMINATION_STATUS_NORMAL_TERMINATION, status);
189 EXPECT_EQ(0, exit_code);
190 base::CloseProcessHandle(handle);
191 remove(signal_file.c_str());
195 // TODO(cpu): figure out how to test this in other platforms.
196 TEST_F(ProcessUtilTest, GetProcId) {
197 base::ProcessId id1 = base::GetProcId(GetCurrentProcess());
199 base::ProcessHandle handle = this->SpawnChild("SimpleChildProcess", false);
200 ASSERT_NE(base::kNullProcessHandle, handle);
201 base::ProcessId id2 = base::GetProcId(handle);
204 base::CloseProcessHandle(handle);
208 #if !defined(OS_MACOSX)
209 // This test is disabled on Mac, since it's flaky due to ReportCrash
210 // taking a variable amount of time to parse and load the debug and
211 // symbol data for this unit test's executable before firing the
214 // TODO(gspencer): turn this test process into a very small program
215 // with no symbols (instead of using the multiprocess testing
216 // framework) to reduce the ReportCrash overhead.
217 const char kSignalFileCrash[] = "CrashingChildProcess.die";
219 MULTIPROCESS_TEST_MAIN(CrashingChildProcess) {
220 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileCrash).c_str());
221 #if defined(OS_POSIX)
222 // Have to disable to signal handler for segv so we can get a crash
223 // instead of an abnormal termination through the crash dump handler.
224 ::signal(SIGSEGV, SIG_DFL);
226 // Make this process have a segmentation fault.
227 volatile int* oops = NULL;
232 // This test intentionally crashes, so we don't need to run it under
234 // TODO(jschuh): crbug.com/175753 Fix this in Win64 bots.
235 #if defined(ADDRESS_SANITIZER) || (defined(OS_WIN) && defined(ARCH_CPU_X86_64))
236 #define MAYBE_GetTerminationStatusCrash DISABLED_GetTerminationStatusCrash
238 #define MAYBE_GetTerminationStatusCrash GetTerminationStatusCrash
240 TEST_F(ProcessUtilTest, MAYBE_GetTerminationStatusCrash) {
241 const std::string signal_file =
242 ProcessUtilTest::GetSignalFilePath(kSignalFileCrash);
243 remove(signal_file.c_str());
244 base::ProcessHandle handle = this->SpawnChild("CrashingChildProcess",
246 ASSERT_NE(base::kNullProcessHandle, handle);
249 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING,
250 base::GetTerminationStatus(handle, &exit_code));
251 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
253 SignalChildren(signal_file.c_str());
255 base::TerminationStatus status =
256 WaitForChildTermination(handle, &exit_code);
257 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_CRASHED, status);
260 EXPECT_EQ(0xc0000005, exit_code);
261 #elif defined(OS_POSIX)
262 int signaled = WIFSIGNALED(exit_code);
263 EXPECT_NE(0, signaled);
264 int signal = WTERMSIG(exit_code);
265 EXPECT_EQ(SIGSEGV, signal);
267 base::CloseProcessHandle(handle);
269 // Reset signal handlers back to "normal".
270 base::debug::EnableInProcessStackDumping();
271 remove(signal_file.c_str());
273 #endif // !defined(OS_MACOSX)
275 MULTIPROCESS_TEST_MAIN(KilledChildProcess) {
276 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileKill).c_str());
279 HANDLE handle = ::OpenProcess(PROCESS_ALL_ACCESS, 0, ::GetCurrentProcessId());
280 ::TerminateProcess(handle, kExpectedKilledExitCode);
281 #elif defined(OS_POSIX)
282 // Send a SIGKILL to this process, just like the OOM killer would.
283 ::kill(getpid(), SIGKILL);
288 TEST_F(ProcessUtilTest, GetTerminationStatusKill) {
289 const std::string signal_file =
290 ProcessUtilTest::GetSignalFilePath(kSignalFileKill);
291 remove(signal_file.c_str());
292 base::ProcessHandle handle = this->SpawnChild("KilledChildProcess",
294 ASSERT_NE(base::kNullProcessHandle, handle);
297 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING,
298 base::GetTerminationStatus(handle, &exit_code));
299 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
301 SignalChildren(signal_file.c_str());
303 base::TerminationStatus status =
304 WaitForChildTermination(handle, &exit_code);
305 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_WAS_KILLED, status);
307 EXPECT_EQ(kExpectedKilledExitCode, exit_code);
308 #elif defined(OS_POSIX)
309 int signaled = WIFSIGNALED(exit_code);
310 EXPECT_NE(0, signaled);
311 int signal = WTERMSIG(exit_code);
312 EXPECT_EQ(SIGKILL, signal);
314 base::CloseProcessHandle(handle);
315 remove(signal_file.c_str());
318 // Ensure that the priority of a process is restored correctly after
319 // backgrounding and restoring.
320 // Note: a platform may not be willing or able to lower the priority of
321 // a process. The calls to SetProcessBackground should be noops then.
322 TEST_F(ProcessUtilTest, SetProcessBackgrounded) {
323 base::ProcessHandle handle = this->SpawnChild("SimpleChildProcess", false);
324 base::Process process(handle);
325 int old_priority = process.GetPriority();
327 EXPECT_TRUE(process.SetProcessBackgrounded(true));
328 EXPECT_TRUE(process.IsProcessBackgrounded());
329 EXPECT_TRUE(process.SetProcessBackgrounded(false));
330 EXPECT_FALSE(process.IsProcessBackgrounded());
332 process.SetProcessBackgrounded(true);
333 process.SetProcessBackgrounded(false);
335 int new_priority = process.GetPriority();
336 EXPECT_EQ(old_priority, new_priority);
339 // Same as SetProcessBackgrounded but to this very process. It uses
340 // a different code path at least for Windows.
341 TEST_F(ProcessUtilTest, SetProcessBackgroundedSelf) {
342 base::Process process(base::Process::Current().handle());
343 int old_priority = process.GetPriority();
345 EXPECT_TRUE(process.SetProcessBackgrounded(true));
346 EXPECT_TRUE(process.IsProcessBackgrounded());
347 EXPECT_TRUE(process.SetProcessBackgrounded(false));
348 EXPECT_FALSE(process.IsProcessBackgrounded());
350 process.SetProcessBackgrounded(true);
351 process.SetProcessBackgrounded(false);
353 int new_priority = process.GetPriority();
354 EXPECT_EQ(old_priority, new_priority);
357 #if defined(OS_LINUX) || defined(OS_ANDROID)
358 TEST_F(ProcessUtilTest, GetSystemMemoryInfo) {
359 base::SystemMemoryInfoKB info;
360 EXPECT_TRUE(base::GetSystemMemoryInfo(&info));
362 // Ensure each field received a value.
363 EXPECT_GT(info.total, 0);
364 EXPECT_GT(info.free, 0);
365 EXPECT_GT(info.buffers, 0);
366 EXPECT_GT(info.cached, 0);
367 EXPECT_GT(info.active_anon, 0);
368 EXPECT_GT(info.inactive_anon, 0);
369 EXPECT_GT(info.active_file, 0);
370 EXPECT_GT(info.inactive_file, 0);
372 // All the values should be less than the total amount of memory.
373 EXPECT_LT(info.free, info.total);
374 EXPECT_LT(info.buffers, info.total);
375 EXPECT_LT(info.cached, info.total);
376 EXPECT_LT(info.active_anon, info.total);
377 EXPECT_LT(info.inactive_anon, info.total);
378 EXPECT_LT(info.active_file, info.total);
379 EXPECT_LT(info.inactive_file, info.total);
381 #if defined(OS_CHROMEOS)
382 // Chrome OS exposes shmem.
383 EXPECT_GT(info.shmem, 0);
384 EXPECT_LT(info.shmem, info.total);
385 // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects
386 // and gem_size cannot be tested here.
389 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
391 // TODO(estade): if possible, port these 2 tests.
393 TEST_F(ProcessUtilTest, CalcFreeMemory) {
394 scoped_ptr<base::ProcessMetrics> metrics(
395 base::ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess()));
396 ASSERT_TRUE(NULL != metrics.get());
398 bool using_tcmalloc = false;
400 // Detect if we are using tcmalloc
401 #if !defined(NO_TCMALLOC)
402 const char* chrome_allocator = getenv("CHROME_ALLOCATOR");
403 if (!chrome_allocator || _stricmp(chrome_allocator, "tcmalloc") == 0)
404 using_tcmalloc = true;
407 // Typical values here is ~1900 for total and ~1000 for largest. Obviously
408 // it depends in what other tests have done to this process.
409 base::FreeMBytes free_mem1 = {0};
410 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem1));
411 EXPECT_LT(10u, free_mem1.total);
412 EXPECT_LT(10u, free_mem1.largest);
413 EXPECT_GT(2048u, free_mem1.total);
414 EXPECT_GT(2048u, free_mem1.largest);
415 EXPECT_GE(free_mem1.total, free_mem1.largest);
416 EXPECT_TRUE(NULL != free_mem1.largest_ptr);
418 // Allocate 20M and check again. It should have gone down.
419 const int kAllocMB = 20;
420 scoped_ptr<char[]> alloc(new char[kAllocMB * 1024 * 1024]);
421 size_t expected_total = free_mem1.total - kAllocMB;
422 size_t expected_largest = free_mem1.largest;
424 base::FreeMBytes free_mem2 = {0};
425 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem2));
426 EXPECT_GE(free_mem2.total, free_mem2.largest);
427 // This test is flaky when using tcmalloc, because tcmalloc
428 // allocation strategy sometimes results in less than the
429 // full drop of 20Mb of free memory.
431 EXPECT_GE(expected_total, free_mem2.total);
432 EXPECT_GE(expected_largest, free_mem2.largest);
433 EXPECT_TRUE(NULL != free_mem2.largest_ptr);
436 TEST_F(ProcessUtilTest, GetAppOutput) {
437 // Let's create a decently long message.
439 for (int i = 0; i < 1025; i++) { // 1025 so it does not end on a kilo-byte
443 // cmd.exe's echo always adds a \r\n to its output.
444 std::string expected(message);
447 FilePath cmd(L"cmd.exe");
448 CommandLine cmd_line(cmd);
449 cmd_line.AppendArg("/c");
450 cmd_line.AppendArg("echo " + message + "");
452 ASSERT_TRUE(base::GetAppOutput(cmd_line, &output));
453 EXPECT_EQ(expected, output);
455 // Let's make sure stderr is ignored.
456 CommandLine other_cmd_line(cmd);
457 other_cmd_line.AppendArg("/c");
458 // http://msdn.microsoft.com/library/cc772622.aspx
459 cmd_line.AppendArg("echo " + message + " >&2");
461 ASSERT_TRUE(base::GetAppOutput(other_cmd_line, &output));
462 EXPECT_EQ("", output);
465 TEST_F(ProcessUtilTest, LaunchAsUser) {
466 base::UserTokenHandle token;
467 ASSERT_TRUE(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token));
468 std::wstring cmdline =
469 this->MakeCmdLine("SimpleChildProcess", false).GetCommandLineString();
470 base::LaunchOptions options;
471 options.as_user = token;
472 EXPECT_TRUE(base::LaunchProcess(cmdline, options, NULL));
475 #endif // defined(OS_WIN)
477 #if defined(OS_POSIX)
481 // Returns the maximum number of files that a process can have open.
482 // Returns 0 on error.
483 int GetMaxFilesOpenInProcess() {
485 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
489 // rlim_t is a uint64 - clip to maxint. We do this since FD #s are ints
490 // which are all 32 bits on the supported platforms.
491 rlim_t max_int = static_cast<rlim_t>(std::numeric_limits<int32>::max());
492 if (rlim.rlim_cur > max_int) {
496 return rlim.rlim_cur;
499 const int kChildPipe = 20; // FD # for write end of pipe in child process.
503 MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess) {
504 // This child process counts the number of open FDs, it then writes that
505 // number out to a pipe connected to the parent.
506 int num_open_files = 0;
507 int write_pipe = kChildPipe;
508 int max_files = GetMaxFilesOpenInProcess();
509 for (int i = STDERR_FILENO + 1; i < max_files; i++) {
510 if (i != kChildPipe) {
512 if ((fd = HANDLE_EINTR(dup(i))) != -1) {
519 int written = HANDLE_EINTR(write(write_pipe, &num_open_files,
520 sizeof(num_open_files)));
521 DCHECK_EQ(static_cast<size_t>(written), sizeof(num_open_files));
522 int ret = HANDLE_EINTR(close(write_pipe));
528 int ProcessUtilTest::CountOpenFDsInChild() {
533 base::FileHandleMappingVector fd_mapping_vec;
534 fd_mapping_vec.push_back(std::pair<int, int>(fds[1], kChildPipe));
535 base::ProcessHandle handle = this->SpawnChild(
536 "ProcessUtilsLeakFDChildProcess", fd_mapping_vec, false);
538 int ret = HANDLE_EINTR(close(fds[1]));
541 // Read number of open files in client process from pipe;
542 int num_open_files = -1;
544 HANDLE_EINTR(read(fds[0], &num_open_files, sizeof(num_open_files)));
545 CHECK_EQ(bytes_read, static_cast<ssize_t>(sizeof(num_open_files)));
547 #if defined(THREAD_SANITIZER) || defined(USE_HEAPCHECKER)
548 // Compiler-based ThreadSanitizer makes this test slow.
549 CHECK(base::WaitForSingleProcess(handle, base::TimeDelta::FromSeconds(3)));
551 CHECK(base::WaitForSingleProcess(handle, base::TimeDelta::FromSeconds(1)));
553 base::CloseProcessHandle(handle);
554 ret = HANDLE_EINTR(close(fds[0]));
557 return num_open_files;
560 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER)
561 // ProcessUtilTest.FDRemapping is flaky when ran under xvfb-run on Precise.
562 // The problem is 100% reproducible with both ASan and TSan.
563 // See http://crbug.com/136720.
564 #define MAYBE_FDRemapping DISABLED_FDRemapping
566 #define MAYBE_FDRemapping FDRemapping
568 TEST_F(ProcessUtilTest, MAYBE_FDRemapping) {
569 int fds_before = CountOpenFDsInChild();
571 // open some dummy fds to make sure they don't propagate over to the
573 int dev_null = open("/dev/null", O_RDONLY);
575 socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
577 int fds_after = CountOpenFDsInChild();
579 ASSERT_EQ(fds_after, fds_before);
582 ret = HANDLE_EINTR(close(sockets[0]));
584 ret = HANDLE_EINTR(close(sockets[1]));
586 ret = HANDLE_EINTR(close(dev_null));
592 std::string TestLaunchProcess(const base::EnvironmentMap& env_changes,
593 const int clone_flags) {
594 std::vector<std::string> args;
595 base::FileHandleMappingVector fds_to_remap;
597 args.push_back(kPosixShell);
598 args.push_back("-c");
599 args.push_back("echo $BASE_TEST");
602 PCHECK(pipe(fds) == 0);
604 fds_to_remap.push_back(std::make_pair(fds[1], 1));
605 base::LaunchOptions options;
607 options.environ = env_changes;
608 options.fds_to_remap = &fds_to_remap;
609 #if defined(OS_LINUX)
610 options.clone_flags = clone_flags;
612 CHECK_EQ(0, clone_flags);
614 EXPECT_TRUE(base::LaunchProcess(args, options, NULL));
615 PCHECK(HANDLE_EINTR(close(fds[1])) == 0);
618 const ssize_t n = HANDLE_EINTR(read(fds[0], buf, sizeof(buf)));
621 PCHECK(HANDLE_EINTR(close(fds[0])) == 0);
623 return std::string(buf, n);
626 const char kLargeString[] =
627 "0123456789012345678901234567890123456789012345678901234567890123456789"
628 "0123456789012345678901234567890123456789012345678901234567890123456789"
629 "0123456789012345678901234567890123456789012345678901234567890123456789"
630 "0123456789012345678901234567890123456789012345678901234567890123456789"
631 "0123456789012345678901234567890123456789012345678901234567890123456789"
632 "0123456789012345678901234567890123456789012345678901234567890123456789"
633 "0123456789012345678901234567890123456789012345678901234567890123456789";
637 TEST_F(ProcessUtilTest, LaunchProcess) {
638 base::EnvironmentMap env_changes;
639 const int no_clone_flags = 0;
641 const char kBaseTest[] = "BASE_TEST";
643 env_changes[kBaseTest] = "bar";
644 EXPECT_EQ("bar\n", TestLaunchProcess(env_changes, no_clone_flags));
647 EXPECT_EQ(0, setenv(kBaseTest, "testing", 1 /* override */));
648 EXPECT_EQ("testing\n", TestLaunchProcess(env_changes, no_clone_flags));
650 env_changes[kBaseTest] = std::string();
651 EXPECT_EQ("\n", TestLaunchProcess(env_changes, no_clone_flags));
653 env_changes[kBaseTest] = "foo";
654 EXPECT_EQ("foo\n", TestLaunchProcess(env_changes, no_clone_flags));
657 EXPECT_EQ(0, setenv(kBaseTest, kLargeString, 1 /* override */));
658 EXPECT_EQ(std::string(kLargeString) + "\n",
659 TestLaunchProcess(env_changes, no_clone_flags));
661 env_changes[kBaseTest] = "wibble";
662 EXPECT_EQ("wibble\n", TestLaunchProcess(env_changes, no_clone_flags));
664 #if defined(OS_LINUX)
665 // Test a non-trival value for clone_flags.
666 // Don't test on Valgrind as it has limited support for clone().
667 if (!RunningOnValgrind()) {
668 EXPECT_EQ("wibble\n", TestLaunchProcess(env_changes, CLONE_FS | SIGCHLD));
673 TEST_F(ProcessUtilTest, GetAppOutput) {
676 #if defined(OS_ANDROID)
677 std::vector<std::string> argv;
678 argv.push_back("sh"); // Instead of /bin/sh, force path search to find it.
679 argv.push_back("-c");
681 argv.push_back("exit 0");
682 EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output));
683 EXPECT_STREQ("", output.c_str());
686 EXPECT_FALSE(base::GetAppOutput(CommandLine(argv), &output));
687 EXPECT_STREQ("", output.c_str());
689 argv[2] = "echo foobar42";
690 EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output));
691 EXPECT_STREQ("foobar42\n", output.c_str());
693 EXPECT_TRUE(base::GetAppOutput(CommandLine(FilePath("true")), &output));
694 EXPECT_STREQ("", output.c_str());
696 EXPECT_FALSE(base::GetAppOutput(CommandLine(FilePath("false")), &output));
698 std::vector<std::string> argv;
699 argv.push_back("/bin/echo");
700 argv.push_back("-n");
701 argv.push_back("foobar42");
702 EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output));
703 EXPECT_STREQ("foobar42", output.c_str());
704 #endif // defined(OS_ANDROID)
707 TEST_F(ProcessUtilTest, GetAppOutputRestricted) {
708 // Unfortunately, since we can't rely on the path, we need to know where
709 // everything is. So let's use /bin/sh, which is on every POSIX system, and
711 std::vector<std::string> argv;
712 argv.push_back(std::string(kShellPath)); // argv[0]
713 argv.push_back("-c"); // argv[1]
715 // On success, should set |output|. We use |/bin/sh -c 'exit 0'| instead of
716 // |true| since the location of the latter may be |/bin| or |/usr/bin| (and we
717 // need absolute paths).
718 argv.push_back("exit 0"); // argv[2]; equivalent to "true"
719 std::string output = "abc";
720 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 100));
721 EXPECT_STREQ("", output.c_str());
723 argv[2] = "exit 1"; // equivalent to "false"
725 EXPECT_FALSE(base::GetAppOutputRestricted(CommandLine(argv),
727 EXPECT_STREQ("", output.c_str());
729 // Amount of output exactly equal to space allowed.
730 argv[2] = "echo 123456789"; // (the sh built-in doesn't take "-n")
732 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 10));
733 EXPECT_STREQ("123456789\n", output.c_str());
735 // Amount of output greater than space allowed.
737 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 5));
738 EXPECT_STREQ("12345", output.c_str());
740 // Amount of output less than space allowed.
742 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 15));
743 EXPECT_STREQ("123456789\n", output.c_str());
745 // Zero space allowed.
747 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 0));
748 EXPECT_STREQ("", output.c_str());
751 #if !defined(OS_MACOSX) && !defined(OS_OPENBSD)
752 // TODO(benwells): GetAppOutputRestricted should terminate applications
753 // with SIGPIPE when we have enough output. http://crbug.com/88502
754 TEST_F(ProcessUtilTest, GetAppOutputRestrictedSIGPIPE) {
755 std::vector<std::string> argv;
758 argv.push_back(std::string(kShellPath)); // argv[0]
759 argv.push_back("-c");
760 #if defined(OS_ANDROID)
761 argv.push_back("while echo 12345678901234567890; do :; done");
762 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 10));
763 EXPECT_STREQ("1234567890", output.c_str());
765 argv.push_back("yes");
766 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 10));
767 EXPECT_STREQ("y\ny\ny\ny\ny\n", output.c_str());
772 #if defined(ADDRESS_SANITIZER) && defined(OS_MACOSX) && \
773 defined(ARCH_CPU_64_BITS)
774 // Times out under AddressSanitizer on 64-bit OS X, see
775 // http://crbug.com/298197.
776 #define MAYBE_GetAppOutputRestrictedNoZombies \
777 DISABLED_GetAppOutputRestrictedNoZombies
779 #define MAYBE_GetAppOutputRestrictedNoZombies GetAppOutputRestrictedNoZombies
781 TEST_F(ProcessUtilTest, MAYBE_GetAppOutputRestrictedNoZombies) {
782 std::vector<std::string> argv;
784 argv.push_back(std::string(kShellPath)); // argv[0]
785 argv.push_back("-c"); // argv[1]
786 argv.push_back("echo 123456789012345678901234567890"); // argv[2]
788 // Run |GetAppOutputRestricted()| 300 (> default per-user processes on Mac OS
789 // 10.5) times with an output buffer big enough to capture all output.
790 for (int i = 0; i < 300; i++) {
792 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 100));
793 EXPECT_STREQ("123456789012345678901234567890\n", output.c_str());
796 // Ditto, but with an output buffer too small to capture all output.
797 for (int i = 0; i < 300; i++) {
799 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 10));
800 EXPECT_STREQ("1234567890", output.c_str());
804 TEST_F(ProcessUtilTest, GetAppOutputWithExitCode) {
805 // Test getting output from a successful application.
806 std::vector<std::string> argv;
809 argv.push_back(std::string(kShellPath)); // argv[0]
810 argv.push_back("-c"); // argv[1]
811 argv.push_back("echo foo"); // argv[2];
812 EXPECT_TRUE(base::GetAppOutputWithExitCode(CommandLine(argv), &output,
814 EXPECT_STREQ("foo\n", output.c_str());
815 EXPECT_EQ(exit_code, 0);
817 // Test getting output from an application which fails with a specific exit
820 argv[2] = "echo foo; exit 2";
821 EXPECT_TRUE(base::GetAppOutputWithExitCode(CommandLine(argv), &output,
823 EXPECT_STREQ("foo\n", output.c_str());
824 EXPECT_EQ(exit_code, 2);
827 TEST_F(ProcessUtilTest, GetParentProcessId) {
828 base::ProcessId ppid = base::GetParentProcessId(base::GetCurrentProcId());
829 EXPECT_EQ(ppid, getppid());
832 #if defined(OS_LINUX) || defined(OS_ANDROID)
833 TEST_F(ProcessUtilTest, ParseProcStatCPU) {
834 // /proc/self/stat for a process running "top".
835 const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 "
837 "12 16 0 0 " // <- These are the goods.
838 "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 "
839 "4246868 140733983044336 18446744073709551615 140244213071219 "
840 "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0";
841 EXPECT_EQ(12 + 16, base::ParseProcStatCPU(kTopStat));
843 // cat /proc/self/stat on a random other machine I have.
844 const char kSelfStat[] = "5364 (cat) R 5354 5364 5354 34819 5364 "
846 "0 0 0 0 " // <- No CPU, apparently.
847 "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 "
848 "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0";
850 EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat));
853 // Disable on Android because base_unittests runs inside a Dalvik VM that
854 // starts and stop threads (crbug.com/175563).
855 #if !defined(OS_ANDROID)
856 TEST_F(ProcessUtilTest, GetNumberOfThreads) {
857 const base::ProcessHandle current = base::GetCurrentProcessHandle();
858 const int initial_threads = base::GetNumberOfThreads(current);
859 ASSERT_GT(initial_threads, 0);
860 const int kNumAdditionalThreads = 10;
862 scoped_ptr<base::Thread> my_threads[kNumAdditionalThreads];
863 for (int i = 0; i < kNumAdditionalThreads; ++i) {
864 my_threads[i].reset(new base::Thread("GetNumberOfThreadsTest"));
865 my_threads[i]->Start();
866 ASSERT_EQ(base::GetNumberOfThreads(current), initial_threads + 1 + i);
869 // The Thread destructor will stop them.
870 ASSERT_EQ(initial_threads, base::GetNumberOfThreads(current));
872 #endif // !defined(OS_ANDROID)
874 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
876 // TODO(port): port those unit tests.
877 bool IsProcessDead(base::ProcessHandle child) {
878 // waitpid() will actually reap the process which is exactly NOT what we
879 // want to test for. The good thing is that if it can't find the process
880 // we'll get a nice value for errno which we can test for.
881 const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG));
882 return result == -1 && errno == ECHILD;
885 TEST_F(ProcessUtilTest, DelayedTermination) {
886 base::ProcessHandle child_process =
887 SpawnChild("process_util_test_never_die", false);
888 ASSERT_TRUE(child_process);
889 base::EnsureProcessTerminated(child_process);
890 base::WaitForSingleProcess(child_process, base::TimeDelta::FromSeconds(5));
892 // Check that process was really killed.
893 EXPECT_TRUE(IsProcessDead(child_process));
894 base::CloseProcessHandle(child_process);
897 MULTIPROCESS_TEST_MAIN(process_util_test_never_die) {
904 TEST_F(ProcessUtilTest, ImmediateTermination) {
905 base::ProcessHandle child_process =
906 SpawnChild("process_util_test_die_immediately", false);
907 ASSERT_TRUE(child_process);
908 // Give it time to die.
910 base::EnsureProcessTerminated(child_process);
912 // Check that process was really killed.
913 EXPECT_TRUE(IsProcessDead(child_process));
914 base::CloseProcessHandle(child_process);
917 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) {
921 #endif // defined(OS_POSIX)