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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 return tmp_dir.value(); | 135 return tmp_dir.value(); |
136 #endif | 136 #endif |
137 } | 137 } |
138 | 138 |
139 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) { | 139 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) { |
140 return 0; | 140 return 0; |
141 } | 141 } |
142 | 142 |
143 // TODO(viettrungluu): This should be in a "MultiProcessTestTest". | 143 // TODO(viettrungluu): This should be in a "MultiProcessTestTest". |
144 TEST_F(ProcessUtilTest, SpawnChild) { | 144 TEST_F(ProcessUtilTest, SpawnChild) { |
145 base::ProcessHandle handle = SpawnChild("SimpleChildProcess"); | 145 base::Process process = SpawnChild("SimpleChildProcess"); |
146 ASSERT_NE(base::kNullProcessHandle, handle); | 146 ASSERT_TRUE(process.IsValid()); |
147 EXPECT_TRUE(base::WaitForSingleProcess( | 147 EXPECT_TRUE(base::WaitForSingleProcess(process.Handle(), |
148 handle, TestTimeouts::action_max_timeout())); | 148 TestTimeouts::action_max_timeout())); |
149 base::CloseProcessHandle(handle); | |
150 } | 149 } |
151 | 150 |
152 MULTIPROCESS_TEST_MAIN(SlowChildProcess) { | 151 MULTIPROCESS_TEST_MAIN(SlowChildProcess) { |
153 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileSlow).c_str()); | 152 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileSlow).c_str()); |
154 return 0; | 153 return 0; |
155 } | 154 } |
156 | 155 |
157 TEST_F(ProcessUtilTest, KillSlowChild) { | 156 TEST_F(ProcessUtilTest, KillSlowChild) { |
158 const std::string signal_file = | 157 const std::string signal_file = |
159 ProcessUtilTest::GetSignalFilePath(kSignalFileSlow); | 158 ProcessUtilTest::GetSignalFilePath(kSignalFileSlow); |
160 remove(signal_file.c_str()); | 159 remove(signal_file.c_str()); |
161 base::ProcessHandle handle = SpawnChild("SlowChildProcess"); | 160 base::Process process = SpawnChild("SlowChildProcess"); |
162 ASSERT_NE(base::kNullProcessHandle, handle); | 161 ASSERT_TRUE(process.IsValid()); |
163 SignalChildren(signal_file.c_str()); | 162 SignalChildren(signal_file.c_str()); |
164 EXPECT_TRUE(base::WaitForSingleProcess( | 163 EXPECT_TRUE(base::WaitForSingleProcess(process.Handle(), |
165 handle, TestTimeouts::action_max_timeout())); | 164 TestTimeouts::action_max_timeout())); |
166 base::CloseProcessHandle(handle); | |
167 remove(signal_file.c_str()); | 165 remove(signal_file.c_str()); |
168 } | 166 } |
169 | 167 |
170 // Times out on Linux and Win, flakes on other platforms, http://crbug.com/95058 | 168 // Times out on Linux and Win, flakes on other platforms, http://crbug.com/95058 |
171 TEST_F(ProcessUtilTest, DISABLED_GetTerminationStatusExit) { | 169 TEST_F(ProcessUtilTest, DISABLED_GetTerminationStatusExit) { |
172 const std::string signal_file = | 170 const std::string signal_file = |
173 ProcessUtilTest::GetSignalFilePath(kSignalFileSlow); | 171 ProcessUtilTest::GetSignalFilePath(kSignalFileSlow); |
174 remove(signal_file.c_str()); | 172 remove(signal_file.c_str()); |
175 base::ProcessHandle handle = SpawnChild("SlowChildProcess"); | 173 base::Process process = SpawnChild("SlowChildProcess"); |
176 ASSERT_NE(base::kNullProcessHandle, handle); | 174 ASSERT_TRUE(process.IsValid()); |
177 | 175 |
178 int exit_code = 42; | 176 int exit_code = 42; |
179 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, | 177 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, |
180 base::GetTerminationStatus(handle, &exit_code)); | 178 base::GetTerminationStatus(process.Handle(), &exit_code)); |
181 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); | 179 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); |
182 | 180 |
183 SignalChildren(signal_file.c_str()); | 181 SignalChildren(signal_file.c_str()); |
184 exit_code = 42; | 182 exit_code = 42; |
185 base::TerminationStatus status = | 183 base::TerminationStatus status = |
186 WaitForChildTermination(handle, &exit_code); | 184 WaitForChildTermination(process.Handle(), &exit_code); |
187 EXPECT_EQ(base::TERMINATION_STATUS_NORMAL_TERMINATION, status); | 185 EXPECT_EQ(base::TERMINATION_STATUS_NORMAL_TERMINATION, status); |
188 EXPECT_EQ(0, exit_code); | 186 EXPECT_EQ(0, exit_code); |
189 base::CloseProcessHandle(handle); | |
190 remove(signal_file.c_str()); | 187 remove(signal_file.c_str()); |
191 } | 188 } |
192 | 189 |
193 #if defined(OS_WIN) | 190 #if defined(OS_WIN) |
194 // TODO(cpu): figure out how to test this in other platforms. | 191 // TODO(cpu): figure out how to test this in other platforms. |
195 TEST_F(ProcessUtilTest, GetProcId) { | 192 TEST_F(ProcessUtilTest, GetProcId) { |
196 base::ProcessId id1 = base::GetProcId(GetCurrentProcess()); | 193 base::ProcessId id1 = base::GetProcId(GetCurrentProcess()); |
197 EXPECT_NE(0ul, id1); | 194 EXPECT_NE(0ul, id1); |
198 base::ProcessHandle handle = SpawnChild("SimpleChildProcess"); | 195 base::Process process = SpawnChild("SimpleChildProcess"); |
199 ASSERT_NE(base::kNullProcessHandle, handle); | 196 ASSERT_TRUE(process.IsValid()); |
200 base::ProcessId id2 = base::GetProcId(handle); | 197 base::ProcessId id2 = process.pid(); |
201 EXPECT_NE(0ul, id2); | 198 EXPECT_NE(0ul, id2); |
202 EXPECT_NE(id1, id2); | 199 EXPECT_NE(id1, id2); |
203 base::CloseProcessHandle(handle); | |
204 } | 200 } |
205 #endif | 201 #endif |
206 | 202 |
207 #if !defined(OS_MACOSX) | 203 #if !defined(OS_MACOSX) |
208 // This test is disabled on Mac, since it's flaky due to ReportCrash | 204 // This test is disabled on Mac, since it's flaky due to ReportCrash |
209 // taking a variable amount of time to parse and load the debug and | 205 // taking a variable amount of time to parse and load the debug and |
210 // symbol data for this unit test's executable before firing the | 206 // symbol data for this unit test's executable before firing the |
211 // signal handler. | 207 // signal handler. |
212 // | 208 // |
213 // TODO(gspencer): turn this test process into a very small program | 209 // TODO(gspencer): turn this test process into a very small program |
(...skipping 18 matching lines...) Expand all Loading... |
232 // AddressSanitizer. | 228 // AddressSanitizer. |
233 #if defined(ADDRESS_SANITIZER) || defined(SYZYASAN) | 229 #if defined(ADDRESS_SANITIZER) || defined(SYZYASAN) |
234 #define MAYBE_GetTerminationStatusCrash DISABLED_GetTerminationStatusCrash | 230 #define MAYBE_GetTerminationStatusCrash DISABLED_GetTerminationStatusCrash |
235 #else | 231 #else |
236 #define MAYBE_GetTerminationStatusCrash GetTerminationStatusCrash | 232 #define MAYBE_GetTerminationStatusCrash GetTerminationStatusCrash |
237 #endif | 233 #endif |
238 TEST_F(ProcessUtilTest, MAYBE_GetTerminationStatusCrash) { | 234 TEST_F(ProcessUtilTest, MAYBE_GetTerminationStatusCrash) { |
239 const std::string signal_file = | 235 const std::string signal_file = |
240 ProcessUtilTest::GetSignalFilePath(kSignalFileCrash); | 236 ProcessUtilTest::GetSignalFilePath(kSignalFileCrash); |
241 remove(signal_file.c_str()); | 237 remove(signal_file.c_str()); |
242 base::ProcessHandle handle = SpawnChild("CrashingChildProcess"); | 238 base::Process process = SpawnChild("CrashingChildProcess"); |
243 ASSERT_NE(base::kNullProcessHandle, handle); | 239 ASSERT_TRUE(process.IsValid()); |
244 | 240 |
245 int exit_code = 42; | 241 int exit_code = 42; |
246 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, | 242 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, |
247 base::GetTerminationStatus(handle, &exit_code)); | 243 base::GetTerminationStatus(process.Handle(), &exit_code)); |
248 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); | 244 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); |
249 | 245 |
250 SignalChildren(signal_file.c_str()); | 246 SignalChildren(signal_file.c_str()); |
251 exit_code = 42; | 247 exit_code = 42; |
252 base::TerminationStatus status = | 248 base::TerminationStatus status = |
253 WaitForChildTermination(handle, &exit_code); | 249 WaitForChildTermination(process.Handle(), &exit_code); |
254 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_CRASHED, status); | 250 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_CRASHED, status); |
255 | 251 |
256 #if defined(OS_WIN) | 252 #if defined(OS_WIN) |
257 EXPECT_EQ(0xc0000005, exit_code); | 253 EXPECT_EQ(0xc0000005, exit_code); |
258 #elif defined(OS_POSIX) | 254 #elif defined(OS_POSIX) |
259 int signaled = WIFSIGNALED(exit_code); | 255 int signaled = WIFSIGNALED(exit_code); |
260 EXPECT_NE(0, signaled); | 256 EXPECT_NE(0, signaled); |
261 int signal = WTERMSIG(exit_code); | 257 int signal = WTERMSIG(exit_code); |
262 EXPECT_EQ(SIGSEGV, signal); | 258 EXPECT_EQ(SIGSEGV, signal); |
263 #endif | 259 #endif |
264 base::CloseProcessHandle(handle); | |
265 | 260 |
266 // Reset signal handlers back to "normal". | 261 // Reset signal handlers back to "normal". |
267 base::debug::EnableInProcessStackDumping(); | 262 base::debug::EnableInProcessStackDumping(); |
268 remove(signal_file.c_str()); | 263 remove(signal_file.c_str()); |
269 } | 264 } |
270 #endif // !defined(OS_MACOSX) | 265 #endif // !defined(OS_MACOSX) |
271 | 266 |
272 MULTIPROCESS_TEST_MAIN(KilledChildProcess) { | 267 MULTIPROCESS_TEST_MAIN(KilledChildProcess) { |
273 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileKill).c_str()); | 268 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileKill).c_str()); |
274 #if defined(OS_WIN) | 269 #if defined(OS_WIN) |
275 // Kill ourselves. | 270 // Kill ourselves. |
276 HANDLE handle = ::OpenProcess(PROCESS_ALL_ACCESS, 0, ::GetCurrentProcessId()); | 271 HANDLE handle = ::OpenProcess(PROCESS_ALL_ACCESS, 0, ::GetCurrentProcessId()); |
277 ::TerminateProcess(handle, kExpectedKilledExitCode); | 272 ::TerminateProcess(handle, kExpectedKilledExitCode); |
278 #elif defined(OS_POSIX) | 273 #elif defined(OS_POSIX) |
279 // Send a SIGKILL to this process, just like the OOM killer would. | 274 // Send a SIGKILL to this process, just like the OOM killer would. |
280 ::kill(getpid(), SIGKILL); | 275 ::kill(getpid(), SIGKILL); |
281 #endif | 276 #endif |
282 return 1; | 277 return 1; |
283 } | 278 } |
284 | 279 |
285 TEST_F(ProcessUtilTest, GetTerminationStatusKill) { | 280 TEST_F(ProcessUtilTest, GetTerminationStatusKill) { |
286 const std::string signal_file = | 281 const std::string signal_file = |
287 ProcessUtilTest::GetSignalFilePath(kSignalFileKill); | 282 ProcessUtilTest::GetSignalFilePath(kSignalFileKill); |
288 remove(signal_file.c_str()); | 283 remove(signal_file.c_str()); |
289 base::ProcessHandle handle = SpawnChild("KilledChildProcess"); | 284 base::Process process = SpawnChild("KilledChildProcess"); |
290 ASSERT_NE(base::kNullProcessHandle, handle); | 285 ASSERT_TRUE(process.IsValid()); |
291 | 286 |
292 int exit_code = 42; | 287 int exit_code = 42; |
293 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, | 288 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, |
294 base::GetTerminationStatus(handle, &exit_code)); | 289 base::GetTerminationStatus(process.Handle(), &exit_code)); |
295 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); | 290 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); |
296 | 291 |
297 SignalChildren(signal_file.c_str()); | 292 SignalChildren(signal_file.c_str()); |
298 exit_code = 42; | 293 exit_code = 42; |
299 base::TerminationStatus status = | 294 base::TerminationStatus status = |
300 WaitForChildTermination(handle, &exit_code); | 295 WaitForChildTermination(process.Handle(), &exit_code); |
301 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_WAS_KILLED, status); | 296 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_WAS_KILLED, status); |
302 #if defined(OS_WIN) | 297 #if defined(OS_WIN) |
303 EXPECT_EQ(kExpectedKilledExitCode, exit_code); | 298 EXPECT_EQ(kExpectedKilledExitCode, exit_code); |
304 #elif defined(OS_POSIX) | 299 #elif defined(OS_POSIX) |
305 int signaled = WIFSIGNALED(exit_code); | 300 int signaled = WIFSIGNALED(exit_code); |
306 EXPECT_NE(0, signaled); | 301 EXPECT_NE(0, signaled); |
307 int signal = WTERMSIG(exit_code); | 302 int signal = WTERMSIG(exit_code); |
308 EXPECT_EQ(SIGKILL, signal); | 303 EXPECT_EQ(SIGKILL, signal); |
309 #endif | 304 #endif |
310 base::CloseProcessHandle(handle); | |
311 remove(signal_file.c_str()); | 305 remove(signal_file.c_str()); |
312 } | 306 } |
313 | 307 |
314 #if defined(OS_WIN) | 308 #if defined(OS_WIN) |
315 // TODO(estade): if possible, port this test. | 309 // TODO(estade): if possible, port this test. |
316 TEST_F(ProcessUtilTest, GetAppOutput) { | 310 TEST_F(ProcessUtilTest, GetAppOutput) { |
317 // Let's create a decently long message. | 311 // Let's create a decently long message. |
318 std::string message; | 312 std::string message; |
319 for (int i = 0; i < 1025; i++) { // 1025 so it does not end on a kilo-byte | 313 for (int i = 0; i < 1025; i++) { // 1025 so it does not end on a kilo-byte |
320 // boundary. | 314 // boundary. |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 | 522 |
529 int ProcessUtilTest::CountOpenFDsInChild() { | 523 int ProcessUtilTest::CountOpenFDsInChild() { |
530 int fds[2]; | 524 int fds[2]; |
531 if (pipe(fds) < 0) | 525 if (pipe(fds) < 0) |
532 NOTREACHED(); | 526 NOTREACHED(); |
533 | 527 |
534 base::FileHandleMappingVector fd_mapping_vec; | 528 base::FileHandleMappingVector fd_mapping_vec; |
535 fd_mapping_vec.push_back(std::pair<int, int>(fds[1], kChildPipe)); | 529 fd_mapping_vec.push_back(std::pair<int, int>(fds[1], kChildPipe)); |
536 base::LaunchOptions options; | 530 base::LaunchOptions options; |
537 options.fds_to_remap = &fd_mapping_vec; | 531 options.fds_to_remap = &fd_mapping_vec; |
538 base::ProcessHandle handle = | 532 base::Process process = |
539 SpawnChildWithOptions("ProcessUtilsLeakFDChildProcess", options); | 533 SpawnChildWithOptions("ProcessUtilsLeakFDChildProcess", options); |
540 CHECK(handle); | 534 CHECK(process.IsValid()); |
541 int ret = IGNORE_EINTR(close(fds[1])); | 535 int ret = IGNORE_EINTR(close(fds[1])); |
542 DPCHECK(ret == 0); | 536 DPCHECK(ret == 0); |
543 | 537 |
544 // Read number of open files in client process from pipe; | 538 // Read number of open files in client process from pipe; |
545 int num_open_files = -1; | 539 int num_open_files = -1; |
546 ssize_t bytes_read = | 540 ssize_t bytes_read = |
547 HANDLE_EINTR(read(fds[0], &num_open_files, sizeof(num_open_files))); | 541 HANDLE_EINTR(read(fds[0], &num_open_files, sizeof(num_open_files))); |
548 CHECK_EQ(bytes_read, static_cast<ssize_t>(sizeof(num_open_files))); | 542 CHECK_EQ(bytes_read, static_cast<ssize_t>(sizeof(num_open_files))); |
549 | 543 |
550 #if defined(THREAD_SANITIZER) | 544 #if defined(THREAD_SANITIZER) |
551 // Compiler-based ThreadSanitizer makes this test slow. | 545 // Compiler-based ThreadSanitizer makes this test slow. |
552 CHECK(base::WaitForSingleProcess(handle, base::TimeDelta::FromSeconds(3))); | 546 CHECK(base::WaitForSingleProcess(process.Handle(), |
| 547 base::TimeDelta::FromSeconds(3))); |
553 #else | 548 #else |
554 CHECK(base::WaitForSingleProcess(handle, base::TimeDelta::FromSeconds(1))); | 549 CHECK(base::WaitForSingleProcess(process.Handle(), |
| 550 base::TimeDelta::FromSeconds(1))); |
555 #endif | 551 #endif |
556 base::CloseProcessHandle(handle); | |
557 ret = IGNORE_EINTR(close(fds[0])); | 552 ret = IGNORE_EINTR(close(fds[0])); |
558 DPCHECK(ret == 0); | 553 DPCHECK(ret == 0); |
559 | 554 |
560 return num_open_files; | 555 return num_open_files; |
561 } | 556 } |
562 | 557 |
563 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) | 558 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) |
564 // ProcessUtilTest.FDRemapping is flaky when ran under xvfb-run on Precise. | 559 // ProcessUtilTest.FDRemapping is flaky when ran under xvfb-run on Precise. |
565 // The problem is 100% reproducible with both ASan and TSan. | 560 // The problem is 100% reproducible with both ASan and TSan. |
566 // See http://crbug.com/136720. | 561 // See http://crbug.com/136720. |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
881 // TODO(port): port those unit tests. | 876 // TODO(port): port those unit tests. |
882 bool IsProcessDead(base::ProcessHandle child) { | 877 bool IsProcessDead(base::ProcessHandle child) { |
883 // waitpid() will actually reap the process which is exactly NOT what we | 878 // waitpid() will actually reap the process which is exactly NOT what we |
884 // want to test for. The good thing is that if it can't find the process | 879 // want to test for. The good thing is that if it can't find the process |
885 // we'll get a nice value for errno which we can test for. | 880 // we'll get a nice value for errno which we can test for. |
886 const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG)); | 881 const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG)); |
887 return result == -1 && errno == ECHILD; | 882 return result == -1 && errno == ECHILD; |
888 } | 883 } |
889 | 884 |
890 TEST_F(ProcessUtilTest, DelayedTermination) { | 885 TEST_F(ProcessUtilTest, DelayedTermination) { |
891 base::Process child_process(SpawnChild("process_util_test_never_die")); | 886 base::Process child_process = SpawnChild("process_util_test_never_die"); |
892 ASSERT_TRUE(child_process.IsValid()); | 887 ASSERT_TRUE(child_process.IsValid()); |
893 base::EnsureProcessTerminated(child_process.Duplicate()); | 888 base::EnsureProcessTerminated(child_process.Duplicate()); |
894 base::WaitForSingleProcess(child_process.Handle(), | 889 base::WaitForSingleProcess(child_process.Handle(), |
895 base::TimeDelta::FromSeconds(5)); | 890 base::TimeDelta::FromSeconds(5)); |
896 | 891 |
897 // Check that process was really killed. | 892 // Check that process was really killed. |
898 EXPECT_TRUE(IsProcessDead(child_process.Handle())); | 893 EXPECT_TRUE(IsProcessDead(child_process.Handle())); |
899 } | 894 } |
900 | 895 |
901 MULTIPROCESS_TEST_MAIN(process_util_test_never_die) { | 896 MULTIPROCESS_TEST_MAIN(process_util_test_never_die) { |
902 while (1) { | 897 while (1) { |
903 sleep(500); | 898 sleep(500); |
904 } | 899 } |
905 return 0; | 900 return 0; |
906 } | 901 } |
907 | 902 |
908 TEST_F(ProcessUtilTest, ImmediateTermination) { | 903 TEST_F(ProcessUtilTest, ImmediateTermination) { |
909 base::Process child_process(SpawnChild("process_util_test_die_immediately")); | 904 base::Process child_process = SpawnChild("process_util_test_die_immediately"); |
910 ASSERT_TRUE(child_process.IsValid()); | 905 ASSERT_TRUE(child_process.IsValid()); |
911 // Give it time to die. | 906 // Give it time to die. |
912 sleep(2); | 907 sleep(2); |
913 base::EnsureProcessTerminated(child_process.Duplicate()); | 908 base::EnsureProcessTerminated(child_process.Duplicate()); |
914 | 909 |
915 // Check that process was really killed. | 910 // Check that process was really killed. |
916 EXPECT_TRUE(IsProcessDead(child_process.Handle())); | 911 EXPECT_TRUE(IsProcessDead(child_process.Handle())); |
917 } | 912 } |
918 | 913 |
919 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { | 914 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { |
920 return 0; | 915 return 0; |
921 } | 916 } |
922 | 917 |
923 #endif // defined(OS_POSIX) | 918 #endif // defined(OS_POSIX) |
OLD | NEW |