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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 waited += kInterval; | 105 waited += kInterval; |
106 } while (status == base::TERMINATION_STATUS_STILL_RUNNING && | 106 } while (status == base::TERMINATION_STATUS_STILL_RUNNING && |
107 waited.InMilliseconds() < TestTimeouts::action_max_timeout_ms()); | 107 waited.InMilliseconds() < TestTimeouts::action_max_timeout_ms()); |
108 | 108 |
109 return status; | 109 return status; |
110 } | 110 } |
111 | 111 |
112 } // namespace | 112 } // namespace |
113 | 113 |
114 class ProcessUtilTest : public base::MultiProcessTest { | 114 class ProcessUtilTest : public base::MultiProcessTest { |
115 public: | |
115 #if defined(OS_POSIX) | 116 #if defined(OS_POSIX) |
116 public: | |
117 // Spawn a child process that counts how many file descriptors are open. | 117 // Spawn a child process that counts how many file descriptors are open. |
118 int CountOpenFDsInChild(); | 118 int CountOpenFDsInChild(); |
119 #endif | 119 #endif |
120 // Converts the filename to a platform specific filepath. | |
121 // On Android files can not be created in arbitrary directories. | |
122 static const std::string GetSignalFilePath(const char* filename); | |
120 }; | 123 }; |
121 | 124 |
125 const std::string ProcessUtilTest::GetSignalFilePath(const char* filename) { | |
126 #if !defined(OS_ANDROID) | |
127 return filename; | |
Mark Mentovai
2012/06/12 19:12:28
Indentation.
nilesh
2012/06/12 20:46:10
Done.
| |
128 #else | |
129 FilePath tmp_dir; | |
130 PathService::Get(base::DIR_CACHE, &tmp_dir); | |
131 return tmp_dir.value() + "/" + filename; | |
Mark Mentovai
2012/06/12 19:12:28
Since you have a FilePath, use Append. Don’t build
nilesh
2012/06/12 20:46:10
Done thanks.
| |
132 #endif | |
133 } | |
134 | |
122 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) { | 135 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) { |
123 return 0; | 136 return 0; |
124 } | 137 } |
125 | 138 |
126 TEST_F(ProcessUtilTest, SpawnChild) { | 139 TEST_F(ProcessUtilTest, SpawnChild) { |
127 base::ProcessHandle handle = this->SpawnChild("SimpleChildProcess", false); | 140 base::ProcessHandle handle = this->SpawnChild("SimpleChildProcess", false); |
128 ASSERT_NE(base::kNullProcessHandle, handle); | 141 ASSERT_NE(base::kNullProcessHandle, handle); |
129 EXPECT_TRUE(base::WaitForSingleProcess( | 142 EXPECT_TRUE(base::WaitForSingleProcess( |
130 handle, TestTimeouts::action_max_timeout_ms())); | 143 handle, TestTimeouts::action_max_timeout_ms())); |
131 base::CloseProcessHandle(handle); | 144 base::CloseProcessHandle(handle); |
132 } | 145 } |
133 | 146 |
134 MULTIPROCESS_TEST_MAIN(SlowChildProcess) { | 147 MULTIPROCESS_TEST_MAIN(SlowChildProcess) { |
135 WaitToDie(kSignalFileSlow); | 148 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileSlow).c_str()); |
136 return 0; | 149 return 0; |
137 } | 150 } |
138 | 151 |
139 TEST_F(ProcessUtilTest, KillSlowChild) { | 152 TEST_F(ProcessUtilTest, KillSlowChild) { |
140 remove(kSignalFileSlow); | 153 const std::string signal_file = |
154 ProcessUtilTest::GetSignalFilePath(kSignalFileSlow); | |
155 remove(signal_file.c_str()); | |
141 base::ProcessHandle handle = this->SpawnChild("SlowChildProcess", false); | 156 base::ProcessHandle handle = this->SpawnChild("SlowChildProcess", false); |
142 ASSERT_NE(base::kNullProcessHandle, handle); | 157 ASSERT_NE(base::kNullProcessHandle, handle); |
143 SignalChildren(kSignalFileSlow); | 158 SignalChildren(signal_file.c_str()); |
144 EXPECT_TRUE(base::WaitForSingleProcess( | 159 EXPECT_TRUE(base::WaitForSingleProcess( |
145 handle, TestTimeouts::action_max_timeout_ms())); | 160 handle, TestTimeouts::action_max_timeout_ms())); |
146 base::CloseProcessHandle(handle); | 161 base::CloseProcessHandle(handle); |
147 remove(kSignalFileSlow); | 162 remove(signal_file.c_str()); |
148 } | 163 } |
149 | 164 |
150 // Times out on Linux and Win, flakes on other platforms, http://crbug.com/95058 | 165 // Times out on Linux and Win, flakes on other platforms, http://crbug.com/95058 |
151 TEST_F(ProcessUtilTest, DISABLED_GetTerminationStatusExit) { | 166 TEST_F(ProcessUtilTest, DISABLED_GetTerminationStatusExit) { |
152 remove(kSignalFileSlow); | 167 const std::string signal_file = |
168 ProcessUtilTest::GetSignalFilePath(kSignalFileSlow); | |
169 remove(signal_file.c_str()); | |
153 base::ProcessHandle handle = this->SpawnChild("SlowChildProcess", false); | 170 base::ProcessHandle handle = this->SpawnChild("SlowChildProcess", false); |
154 ASSERT_NE(base::kNullProcessHandle, handle); | 171 ASSERT_NE(base::kNullProcessHandle, handle); |
155 | 172 |
156 int exit_code = 42; | 173 int exit_code = 42; |
157 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, | 174 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, |
158 base::GetTerminationStatus(handle, &exit_code)); | 175 base::GetTerminationStatus(handle, &exit_code)); |
159 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); | 176 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); |
160 | 177 |
161 SignalChildren(kSignalFileSlow); | 178 SignalChildren(signal_file.c_str()); |
162 exit_code = 42; | 179 exit_code = 42; |
163 base::TerminationStatus status = | 180 base::TerminationStatus status = |
164 WaitForChildTermination(handle, &exit_code); | 181 WaitForChildTermination(handle, &exit_code); |
165 EXPECT_EQ(base::TERMINATION_STATUS_NORMAL_TERMINATION, status); | 182 EXPECT_EQ(base::TERMINATION_STATUS_NORMAL_TERMINATION, status); |
166 EXPECT_EQ(0, exit_code); | 183 EXPECT_EQ(0, exit_code); |
167 base::CloseProcessHandle(handle); | 184 base::CloseProcessHandle(handle); |
168 remove(kSignalFileSlow); | 185 remove(signal_file.c_str()); |
169 } | 186 } |
170 | 187 |
171 #if defined(OS_WIN) | 188 #if defined(OS_WIN) |
172 // TODO(cpu): figure out how to test this in other platforms. | 189 // TODO(cpu): figure out how to test this in other platforms. |
173 TEST_F(ProcessUtilTest, GetProcId) { | 190 TEST_F(ProcessUtilTest, GetProcId) { |
174 base::ProcessId id1 = base::GetProcId(GetCurrentProcess()); | 191 base::ProcessId id1 = base::GetProcId(GetCurrentProcess()); |
175 EXPECT_NE(0ul, id1); | 192 EXPECT_NE(0ul, id1); |
176 base::ProcessHandle handle = this->SpawnChild("SimpleChildProcess", false); | 193 base::ProcessHandle handle = this->SpawnChild("SimpleChildProcess", false); |
177 ASSERT_NE(base::kNullProcessHandle, handle); | 194 ASSERT_NE(base::kNullProcessHandle, handle); |
178 base::ProcessId id2 = base::GetProcId(handle); | 195 base::ProcessId id2 = base::GetProcId(handle); |
(...skipping 26 matching lines...) Expand all Loading... | |
205 // This test is disabled on Mac, since it's flaky due to ReportCrash | 222 // This test is disabled on Mac, since it's flaky due to ReportCrash |
206 // taking a variable amount of time to parse and load the debug and | 223 // taking a variable amount of time to parse and load the debug and |
207 // symbol data for this unit test's executable before firing the | 224 // symbol data for this unit test's executable before firing the |
208 // signal handler. | 225 // signal handler. |
209 // | 226 // |
210 // TODO(gspencer): turn this test process into a very small program | 227 // TODO(gspencer): turn this test process into a very small program |
211 // with no symbols (instead of using the multiprocess testing | 228 // with no symbols (instead of using the multiprocess testing |
212 // framework) to reduce the ReportCrash overhead. | 229 // framework) to reduce the ReportCrash overhead. |
213 | 230 |
214 MULTIPROCESS_TEST_MAIN(CrashingChildProcess) { | 231 MULTIPROCESS_TEST_MAIN(CrashingChildProcess) { |
215 WaitToDie(kSignalFileCrash); | 232 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileCrash).c_str()); |
216 #if defined(OS_POSIX) | 233 #if defined(OS_POSIX) |
217 // Have to disable to signal handler for segv so we can get a crash | 234 // Have to disable to signal handler for segv so we can get a crash |
218 // instead of an abnormal termination through the crash dump handler. | 235 // instead of an abnormal termination through the crash dump handler. |
219 ::signal(SIGSEGV, SIG_DFL); | 236 ::signal(SIGSEGV, SIG_DFL); |
220 #endif | 237 #endif |
221 // Make this process have a segmentation fault. | 238 // Make this process have a segmentation fault. |
222 volatile int* oops = NULL; | 239 volatile int* oops = NULL; |
223 *oops = 0xDEAD; | 240 *oops = 0xDEAD; |
224 return 1; | 241 return 1; |
225 } | 242 } |
226 | 243 |
227 // This test intentionally crashes, so we don't need to run it under | 244 // This test intentionally crashes, so we don't need to run it under |
228 // AddressSanitizer. | 245 // AddressSanitizer. |
229 #if defined(ADDRESS_SANITIZER) | 246 #if defined(ADDRESS_SANITIZER) |
230 #define MAYBE_GetTerminationStatusCrash DISABLED_GetTerminationStatusCrash | 247 #define MAYBE_GetTerminationStatusCrash DISABLED_GetTerminationStatusCrash |
231 #else | 248 #else |
232 #define MAYBE_GetTerminationStatusCrash GetTerminationStatusCrash | 249 #define MAYBE_GetTerminationStatusCrash GetTerminationStatusCrash |
233 #endif | 250 #endif |
234 TEST_F(ProcessUtilTest, MAYBE_GetTerminationStatusCrash) { | 251 TEST_F(ProcessUtilTest, MAYBE_GetTerminationStatusCrash) { |
235 remove(kSignalFileCrash); | 252 const std::string signal_file = |
253 ProcessUtilTest::GetSignalFilePath(kSignalFileCrash); | |
254 remove(signal_file.c_str()); | |
236 base::ProcessHandle handle = this->SpawnChild("CrashingChildProcess", | 255 base::ProcessHandle handle = this->SpawnChild("CrashingChildProcess", |
237 false); | 256 false); |
238 ASSERT_NE(base::kNullProcessHandle, handle); | 257 ASSERT_NE(base::kNullProcessHandle, handle); |
239 | 258 |
240 int exit_code = 42; | 259 int exit_code = 42; |
241 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, | 260 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, |
242 base::GetTerminationStatus(handle, &exit_code)); | 261 base::GetTerminationStatus(handle, &exit_code)); |
243 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); | 262 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); |
244 | 263 |
245 SignalChildren(kSignalFileCrash); | 264 SignalChildren(signal_file.c_str()); |
246 exit_code = 42; | 265 exit_code = 42; |
247 base::TerminationStatus status = | 266 base::TerminationStatus status = |
248 WaitForChildTermination(handle, &exit_code); | 267 WaitForChildTermination(handle, &exit_code); |
249 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_CRASHED, status); | 268 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_CRASHED, status); |
250 | 269 |
251 #if defined(OS_WIN) | 270 #if defined(OS_WIN) |
252 EXPECT_EQ(0xc0000005, exit_code); | 271 EXPECT_EQ(0xc0000005, exit_code); |
253 #elif defined(OS_POSIX) | 272 #elif defined(OS_POSIX) |
254 int signaled = WIFSIGNALED(exit_code); | 273 int signaled = WIFSIGNALED(exit_code); |
255 EXPECT_NE(0, signaled); | 274 EXPECT_NE(0, signaled); |
256 int signal = WTERMSIG(exit_code); | 275 int signal = WTERMSIG(exit_code); |
257 EXPECT_EQ(SIGSEGV, signal); | 276 EXPECT_EQ(SIGSEGV, signal); |
258 #endif | 277 #endif |
259 base::CloseProcessHandle(handle); | 278 base::CloseProcessHandle(handle); |
260 | 279 |
261 // Reset signal handlers back to "normal". | 280 // Reset signal handlers back to "normal". |
262 base::EnableInProcessStackDumping(); | 281 base::EnableInProcessStackDumping(); |
263 remove(kSignalFileCrash); | 282 remove(signal_file.c_str()); |
264 } | 283 } |
265 #endif // !defined(OS_MACOSX) | 284 #endif // !defined(OS_MACOSX) |
266 | 285 |
267 MULTIPROCESS_TEST_MAIN(KilledChildProcess) { | 286 MULTIPROCESS_TEST_MAIN(KilledChildProcess) { |
268 WaitToDie(kSignalFileKill); | 287 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileKill).c_str()); |
269 #if defined(OS_WIN) | 288 #if defined(OS_WIN) |
270 // Kill ourselves. | 289 // Kill ourselves. |
271 HANDLE handle = ::OpenProcess(PROCESS_ALL_ACCESS, 0, ::GetCurrentProcessId()); | 290 HANDLE handle = ::OpenProcess(PROCESS_ALL_ACCESS, 0, ::GetCurrentProcessId()); |
272 ::TerminateProcess(handle, kExpectedKilledExitCode); | 291 ::TerminateProcess(handle, kExpectedKilledExitCode); |
273 #elif defined(OS_POSIX) | 292 #elif defined(OS_POSIX) |
274 // Send a SIGKILL to this process, just like the OOM killer would. | 293 // Send a SIGKILL to this process, just like the OOM killer would. |
275 ::kill(getpid(), SIGKILL); | 294 ::kill(getpid(), SIGKILL); |
276 #endif | 295 #endif |
277 return 1; | 296 return 1; |
278 } | 297 } |
279 | 298 |
280 TEST_F(ProcessUtilTest, GetTerminationStatusKill) { | 299 TEST_F(ProcessUtilTest, GetTerminationStatusKill) { |
281 remove(kSignalFileKill); | 300 const std::string signal_file = |
301 ProcessUtilTest::GetSignalFilePath(kSignalFileKill); | |
302 remove(signal_file.c_str()); | |
282 base::ProcessHandle handle = this->SpawnChild("KilledChildProcess", | 303 base::ProcessHandle handle = this->SpawnChild("KilledChildProcess", |
283 false); | 304 false); |
284 ASSERT_NE(base::kNullProcessHandle, handle); | 305 ASSERT_NE(base::kNullProcessHandle, handle); |
285 | 306 |
286 int exit_code = 42; | 307 int exit_code = 42; |
287 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, | 308 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING, |
288 base::GetTerminationStatus(handle, &exit_code)); | 309 base::GetTerminationStatus(handle, &exit_code)); |
289 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); | 310 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); |
290 | 311 |
291 SignalChildren(kSignalFileKill); | 312 SignalChildren(signal_file.c_str()); |
292 exit_code = 42; | 313 exit_code = 42; |
293 base::TerminationStatus status = | 314 base::TerminationStatus status = |
294 WaitForChildTermination(handle, &exit_code); | 315 WaitForChildTermination(handle, &exit_code); |
295 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_WAS_KILLED, status); | 316 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_WAS_KILLED, status); |
296 #if defined(OS_WIN) | 317 #if defined(OS_WIN) |
297 EXPECT_EQ(kExpectedKilledExitCode, exit_code); | 318 EXPECT_EQ(kExpectedKilledExitCode, exit_code); |
298 #elif defined(OS_POSIX) | 319 #elif defined(OS_POSIX) |
299 int signaled = WIFSIGNALED(exit_code); | 320 int signaled = WIFSIGNALED(exit_code); |
300 EXPECT_NE(0, signaled); | 321 EXPECT_NE(0, signaled); |
301 int signal = WTERMSIG(exit_code); | 322 int signal = WTERMSIG(exit_code); |
302 EXPECT_EQ(SIGKILL, signal); | 323 EXPECT_EQ(SIGKILL, signal); |
303 #endif | 324 #endif |
304 base::CloseProcessHandle(handle); | 325 base::CloseProcessHandle(handle); |
305 remove(kSignalFileKill); | 326 remove(signal_file.c_str()); |
306 } | 327 } |
307 | 328 |
308 // Ensure that the priority of a process is restored correctly after | 329 // Ensure that the priority of a process is restored correctly after |
309 // backgrounding and restoring. | 330 // backgrounding and restoring. |
310 // Note: a platform may not be willing or able to lower the priority of | 331 // Note: a platform may not be willing or able to lower the priority of |
311 // a process. The calls to SetProcessBackground should be noops then. | 332 // a process. The calls to SetProcessBackground should be noops then. |
312 TEST_F(ProcessUtilTest, SetProcessBackgrounded) { | 333 TEST_F(ProcessUtilTest, SetProcessBackgrounded) { |
313 base::ProcessHandle handle = this->SpawnChild("SimpleChildProcess", false); | 334 base::ProcessHandle handle = this->SpawnChild("SimpleChildProcess", false); |
314 base::Process process(handle); | 335 base::Process process(handle); |
315 int old_priority = process.GetPriority(); | 336 int old_priority = process.GetPriority(); |
(...skipping 868 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1184 SetUpInDeathAssert(); | 1205 SetUpInDeathAssert(); |
1185 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {} | 1206 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {} |
1186 }, ""); | 1207 }, ""); |
1187 } | 1208 } |
1188 | 1209 |
1189 #endif // !ARCH_CPU_64_BITS | 1210 #endif // !ARCH_CPU_64_BITS |
1190 #endif // OS_MACOSX | 1211 #endif // OS_MACOSX |
1191 | 1212 |
1192 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && | 1213 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && |
1193 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER) | 1214 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER) |
OLD | NEW |