OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #define _CRT_SECURE_NO_WARNINGS | 5 #define _CRT_SECURE_NO_WARNINGS |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 const char kShellPath[] = "/system/bin/sh"; | 68 const char kShellPath[] = "/system/bin/sh"; |
69 const char kPosixShell[] = "sh"; | 69 const char kPosixShell[] = "sh"; |
70 #else | 70 #else |
71 const char kShellPath[] = "/bin/sh"; | 71 const char kShellPath[] = "/bin/sh"; |
72 const char kPosixShell[] = "bash"; | 72 const char kPosixShell[] = "bash"; |
73 #endif | 73 #endif |
74 | 74 |
75 const char kSignalFileSlow[] = "SlowChildProcess.die"; | 75 const char kSignalFileSlow[] = "SlowChildProcess.die"; |
76 const char kSignalFileKill[] = "KilledChildProcess.die"; | 76 const char kSignalFileKill[] = "KilledChildProcess.die"; |
77 | 77 |
| 78 #if defined(OS_POSIX) |
| 79 const char kSignalFileTerm[] = "TerminatedChildProcess.die"; |
| 80 #endif |
| 81 |
78 #if defined(OS_WIN) | 82 #if defined(OS_WIN) |
79 const int kExpectedStillRunningExitCode = 0x102; | 83 const int kExpectedStillRunningExitCode = 0x102; |
80 const int kExpectedKilledExitCode = 1; | 84 const int kExpectedKilledExitCode = 1; |
81 #else | 85 #else |
82 const int kExpectedStillRunningExitCode = 0; | 86 const int kExpectedStillRunningExitCode = 0; |
83 #endif | 87 #endif |
84 | 88 |
85 // Sleeps until file filename is created. | 89 // Sleeps until file filename is created. |
86 void WaitToDie(const char* filename) { | 90 void WaitToDie(const char* filename) { |
87 FILE* fp; | 91 FILE* fp; |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 // Kill ourselves. | 283 // Kill ourselves. |
280 HANDLE handle = ::OpenProcess(PROCESS_ALL_ACCESS, 0, ::GetCurrentProcessId()); | 284 HANDLE handle = ::OpenProcess(PROCESS_ALL_ACCESS, 0, ::GetCurrentProcessId()); |
281 ::TerminateProcess(handle, kExpectedKilledExitCode); | 285 ::TerminateProcess(handle, kExpectedKilledExitCode); |
282 #elif defined(OS_POSIX) | 286 #elif defined(OS_POSIX) |
283 // Send a SIGKILL to this process, just like the OOM killer would. | 287 // Send a SIGKILL to this process, just like the OOM killer would. |
284 ::kill(getpid(), SIGKILL); | 288 ::kill(getpid(), SIGKILL); |
285 #endif | 289 #endif |
286 return 1; | 290 return 1; |
287 } | 291 } |
288 | 292 |
289 TEST_F(ProcessUtilTest, GetTerminationStatusKill) { | 293 #if defined(OS_POSIX) |
| 294 MULTIPROCESS_TEST_MAIN(TerminatedChildProcess) { |
| 295 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileTerm).c_str()); |
| 296 // Send a SIGTERM to this process. |
| 297 ::kill(getpid(), SIGTERM); |
| 298 return 1; |
| 299 } |
| 300 #endif |
| 301 |
| 302 TEST_F(ProcessUtilTest, GetTerminationStatusSigKill) { |
290 const std::string signal_file = | 303 const std::string signal_file = |
291 ProcessUtilTest::GetSignalFilePath(kSignalFileKill); | 304 ProcessUtilTest::GetSignalFilePath(kSignalFileKill); |
292 remove(signal_file.c_str()); | 305 remove(signal_file.c_str()); |
293 base::Process process = SpawnChild("KilledChildProcess"); | 306 base::Process process = SpawnChild("KilledChildProcess"); |
294 ASSERT_TRUE(process.IsValid()); | 307 ASSERT_TRUE(process.IsValid()); |
295 | 308 |
296 int exit_code = 42; | 309 int exit_code = 42; |
297 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, | 310 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, |
298 base::GetTerminationStatus(process.Handle(), &exit_code)); | 311 base::GetTerminationStatus(process.Handle(), &exit_code)); |
299 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); | 312 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); |
300 | 313 |
301 SignalChildren(signal_file.c_str()); | 314 SignalChildren(signal_file.c_str()); |
302 exit_code = 42; | 315 exit_code = 42; |
303 base::TerminationStatus status = | 316 base::TerminationStatus status = |
304 WaitForChildTermination(process.Handle(), &exit_code); | 317 WaitForChildTermination(process.Handle(), &exit_code); |
| 318 #if defined(OS_CHROMEOS) |
| 319 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM, status); |
| 320 #else |
305 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_WAS_KILLED, status); | 321 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_WAS_KILLED, status); |
| 322 #endif |
| 323 |
306 #if defined(OS_WIN) | 324 #if defined(OS_WIN) |
307 EXPECT_EQ(kExpectedKilledExitCode, exit_code); | 325 EXPECT_EQ(kExpectedKilledExitCode, exit_code); |
308 #elif defined(OS_POSIX) | 326 #elif defined(OS_POSIX) |
309 int signaled = WIFSIGNALED(exit_code); | 327 int signaled = WIFSIGNALED(exit_code); |
310 EXPECT_NE(0, signaled); | 328 EXPECT_NE(0, signaled); |
311 int signal = WTERMSIG(exit_code); | 329 int signal = WTERMSIG(exit_code); |
312 EXPECT_EQ(SIGKILL, signal); | 330 EXPECT_EQ(SIGKILL, signal); |
313 #endif | 331 #endif |
314 remove(signal_file.c_str()); | 332 remove(signal_file.c_str()); |
315 } | 333 } |
316 | 334 |
| 335 #if defined(OS_POSIX) |
| 336 TEST_F(ProcessUtilTest, GetTerminationStatusSigTerm) { |
| 337 const std::string signal_file = |
| 338 ProcessUtilTest::GetSignalFilePath(kSignalFileTerm); |
| 339 remove(signal_file.c_str()); |
| 340 base::Process process = SpawnChild("TerminatedChildProcess"); |
| 341 ASSERT_TRUE(process.IsValid()); |
| 342 |
| 343 int exit_code = 42; |
| 344 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, |
| 345 base::GetTerminationStatus(process.Handle(), &exit_code)); |
| 346 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); |
| 347 |
| 348 SignalChildren(signal_file.c_str()); |
| 349 exit_code = 42; |
| 350 base::TerminationStatus status = |
| 351 WaitForChildTermination(process.Handle(), &exit_code); |
| 352 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_WAS_KILLED, status); |
| 353 |
| 354 int signaled = WIFSIGNALED(exit_code); |
| 355 EXPECT_NE(0, signaled); |
| 356 int signal = WTERMSIG(exit_code); |
| 357 EXPECT_EQ(SIGTERM, signal); |
| 358 remove(signal_file.c_str()); |
| 359 } |
| 360 #endif |
| 361 |
317 #if defined(OS_WIN) | 362 #if defined(OS_WIN) |
318 // TODO(estade): if possible, port this test. | 363 // TODO(estade): if possible, port this test. |
319 TEST_F(ProcessUtilTest, GetAppOutput) { | 364 TEST_F(ProcessUtilTest, GetAppOutput) { |
320 // Let's create a decently long message. | 365 // Let's create a decently long message. |
321 std::string message; | 366 std::string message; |
322 for (int i = 0; i < 1025; i++) { // 1025 so it does not end on a kilo-byte | 367 for (int i = 0; i < 1025; i++) { // 1025 so it does not end on a kilo-byte |
323 // boundary. | 368 // boundary. |
324 message += "Hello!"; | 369 message += "Hello!"; |
325 } | 370 } |
326 // cmd.exe's echo always adds a \r\n to its output. | 371 // cmd.exe's echo always adds a \r\n to its output. |
(...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 options.current_directory = base::FilePath("/dev/null"); | 1103 options.current_directory = base::FilePath("/dev/null"); |
1059 | 1104 |
1060 base::Process process(SpawnChildWithOptions("SimpleChildProcess", options)); | 1105 base::Process process(SpawnChildWithOptions("SimpleChildProcess", options)); |
1061 ASSERT_TRUE(process.IsValid()); | 1106 ASSERT_TRUE(process.IsValid()); |
1062 | 1107 |
1063 int exit_code = kSuccess; | 1108 int exit_code = kSuccess; |
1064 EXPECT_TRUE(process.WaitForExit(&exit_code)); | 1109 EXPECT_TRUE(process.WaitForExit(&exit_code)); |
1065 EXPECT_NE(kSuccess, exit_code); | 1110 EXPECT_NE(kSuccess, exit_code); |
1066 } | 1111 } |
1067 #endif | 1112 #endif |
OLD | NEW |