OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "chrome/browser/process_singleton.h" | 5 #include "chrome/browser/process_singleton.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <limits.h> | 8 #include <limits.h> |
9 #include <signal.h> | 9 #include <signal.h> |
10 #include <stddef.h> | 10 #include <stddef.h> |
11 #include <sys/types.h> | 11 #include <sys/types.h> |
12 #include <sys/un.h> | 12 #include <sys/un.h> |
13 #include <sys/wait.h> | 13 #include <sys/wait.h> |
14 #include <unistd.h> | 14 #include <unistd.h> |
15 | 15 |
16 #include <memory> | 16 #include <memory> |
17 #include <string> | 17 #include <string> |
18 #include <vector> | 18 #include <vector> |
19 | 19 |
20 #include "base/bind.h" | 20 #include "base/bind.h" |
21 #include "base/command_line.h" | 21 #include "base/command_line.h" |
22 #include "base/files/file_path.h" | 22 #include "base/files/file_path.h" |
23 #include "base/files/file_util.h" | 23 #include "base/files/file_util.h" |
24 #include "base/files/scoped_temp_dir.h" | 24 #include "base/files/scoped_temp_dir.h" |
25 #include "base/location.h" | 25 #include "base/location.h" |
26 #include "base/posix/eintr_wrapper.h" | 26 #include "base/posix/eintr_wrapper.h" |
27 #include "base/single_thread_task_runner.h" | 27 #include "base/single_thread_task_runner.h" |
28 #include "base/strings/stringprintf.h" | 28 #include "base/strings/stringprintf.h" |
29 #include "base/synchronization/waitable_event.h" | 29 #include "base/synchronization/waitable_event.h" |
30 #include "base/test/histogram_tester.h" | |
30 #include "base/test/test_timeouts.h" | 31 #include "base/test/test_timeouts.h" |
31 #include "base/test/thread_test_helper.h" | 32 #include "base/test/thread_test_helper.h" |
32 #include "base/threading/thread.h" | 33 #include "base/threading/thread.h" |
33 #include "build/build_config.h" | 34 #include "build/build_config.h" |
34 #include "chrome/common/chrome_constants.h" | 35 #include "chrome/common/chrome_constants.h" |
35 #include "content/public/browser/browser_thread.h" | 36 #include "content/public/browser/browser_thread.h" |
36 #include "content/public/test/test_browser_thread_bundle.h" | 37 #include "content/public/test/test_browser_thread_bundle.h" |
37 #include "net/base/network_interfaces.h" | 38 #include "net/base/network_interfaces.h" |
38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
39 | 40 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 // TODO(james.su@gmail.com): port following tests to Windows. | 280 // TODO(james.su@gmail.com): port following tests to Windows. |
280 // Test success case of NotifyOtherProcess(). | 281 // Test success case of NotifyOtherProcess(). |
281 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessSuccess) { | 282 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessSuccess) { |
282 CreateProcessSingletonOnThread(); | 283 CreateProcessSingletonOnThread(); |
283 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, NotifyOtherProcess(true)); | 284 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, NotifyOtherProcess(true)); |
284 CheckNotified(); | 285 CheckNotified(); |
285 } | 286 } |
286 | 287 |
287 // Test failure case of NotifyOtherProcess(). | 288 // Test failure case of NotifyOtherProcess(). |
288 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessFailure) { | 289 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessFailure) { |
290 base::HistogramTester histogram_tester; | |
289 CreateProcessSingletonOnThread(); | 291 CreateProcessSingletonOnThread(); |
290 | 292 |
291 BlockWorkerThread(); | 293 BlockWorkerThread(); |
292 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcess(true)); | 294 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcess(true)); |
293 ASSERT_EQ(1, kill_callbacks_); | 295 ASSERT_EQ(1, kill_callbacks_); |
294 UnblockWorkerThread(); | 296 UnblockWorkerThread(); |
297 histogram_tester.ExpectUniqueSample( | |
298 "Chrome.ProcessSingleton.RemoteHungProcessTerminateReason", | |
299 ProcessSingleton::SOCKET_READ_FAILED, 1u); | |
295 } | 300 } |
296 | 301 |
297 // Test that we don't kill ourselves by accident if a lockfile with the same pid | 302 // Test that we don't kill ourselves by accident if a lockfile with the same pid |
298 // happens to exist. | 303 // happens to exist. |
299 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessNoSuicide) { | 304 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessNoSuicide) { |
305 base::HistogramTester histogram_tester; | |
300 CreateProcessSingletonOnThread(); | 306 CreateProcessSingletonOnThread(); |
301 // Replace lockfile with one containing our own pid. | 307 // Replace lockfile with one containing our own pid. |
302 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 308 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
303 std::string symlink_content = base::StringPrintf( | 309 std::string symlink_content = base::StringPrintf( |
304 "%s%c%u", | 310 "%s%c%u", |
305 net::GetHostName().c_str(), | 311 net::GetHostName().c_str(), |
306 '-', | 312 '-', |
307 base::GetCurrentProcId()); | 313 base::GetCurrentProcId()); |
308 EXPECT_EQ(0, symlink(symlink_content.c_str(), lock_path_.value().c_str())); | 314 EXPECT_EQ(0, symlink(symlink_content.c_str(), lock_path_.value().c_str())); |
309 | 315 |
310 // Remove socket so that we will not be able to notify the existing browser. | 316 // Remove socket so that we will not be able to notify the existing browser. |
311 EXPECT_EQ(0, unlink(socket_path_.value().c_str())); | 317 EXPECT_EQ(0, unlink(socket_path_.value().c_str())); |
312 | 318 |
319 // Pretend we are browser process. | |
320 ProcessSingleton::SkipIsChromeProcessCheckForTesting(); | |
Alexey Seren
2017/05/15 08:40:48
Previously ProcessSingletonPosixTest.NotifyOtherPr
| |
321 | |
313 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcess(false)); | 322 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcess(false)); |
314 // If we've gotten to this point without killing ourself, the test succeeded. | 323 // If we've gotten to this point without killing ourself, the test succeeded. |
324 histogram_tester.ExpectUniqueSample( | |
325 "Chrome.ProcessSingleton.RemoteProcessInteractionResult", | |
326 ProcessSingleton::SAME_BROWSER_INSTANCE, 1u); | |
315 } | 327 } |
316 | 328 |
317 // Test that we can still notify a process on the same host even after the | 329 // Test that we can still notify a process on the same host even after the |
318 // hostname changed. | 330 // hostname changed. |
319 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessHostChanged) { | 331 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessHostChanged) { |
320 CreateProcessSingletonOnThread(); | 332 CreateProcessSingletonOnThread(); |
321 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 333 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
322 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 334 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
323 | 335 |
324 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, NotifyOtherProcess(false)); | 336 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, NotifyOtherProcess(false)); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
401 | 413 |
402 // Also change the hostname, so the remote does not retry. | 414 // Also change the hostname, so the remote does not retry. |
403 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 415 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
404 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 416 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
405 | 417 |
406 std::string url("about:blank"); | 418 std::string url("about:blank"); |
407 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, NotifyOtherProcessOrCreate(url)); | 419 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, NotifyOtherProcessOrCreate(url)); |
408 } | 420 } |
409 | 421 |
410 TEST_F(ProcessSingletonPosixTest, IgnoreSocketSymlinkWithTooLongTarget) { | 422 TEST_F(ProcessSingletonPosixTest, IgnoreSocketSymlinkWithTooLongTarget) { |
423 base::HistogramTester histogram_tester; | |
411 CreateProcessSingletonOnThread(); | 424 CreateProcessSingletonOnThread(); |
412 // Change the symlink to one with a too-long target. | 425 // Change the symlink to one with a too-long target. |
413 char buf[PATH_MAX]; | 426 char buf[PATH_MAX]; |
414 ssize_t len = readlink(socket_path_.value().c_str(), buf, PATH_MAX); | 427 ssize_t len = readlink(socket_path_.value().c_str(), buf, PATH_MAX); |
415 ASSERT_GT(len, 0); | 428 ASSERT_GT(len, 0); |
416 base::FilePath socket_target_path = base::FilePath(std::string(buf, len)); | 429 base::FilePath socket_target_path = base::FilePath(std::string(buf, len)); |
417 base::FilePath long_socket_target_path = socket_target_path.DirName().Append( | 430 base::FilePath long_socket_target_path = socket_target_path.DirName().Append( |
418 std::string(sizeof(sockaddr_un::sun_path), 'b')); | 431 std::string(sizeof(sockaddr_un::sun_path), 'b')); |
419 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); | 432 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); |
420 ASSERT_EQ(0, symlink(long_socket_target_path.value().c_str(), | 433 ASSERT_EQ(0, symlink(long_socket_target_path.value().c_str(), |
421 socket_path_.value().c_str())); | 434 socket_path_.value().c_str())); |
422 | 435 |
423 // A new ProcessSingleton should ignore the invalid socket path target. | 436 // A new ProcessSingleton should ignore the invalid socket path target. |
424 std::string url("about:blank"); | 437 std::string url("about:blank"); |
425 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcessOrCreate(url)); | 438 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcessOrCreate(url)); |
439 | |
440 // Lock file contains PID of unit_tests process. It is non browser process so | |
441 // we treat lock file as orphaned. | |
442 histogram_tester.ExpectUniqueSample( | |
443 "Chrome.ProcessSingleton.RemoteProcessInteractionResult", | |
444 ProcessSingleton::ORPHANED_LOCK_FILE, 1u); | |
426 } | 445 } |
427 | 446 |
428 #if defined(OS_MACOSX) | 447 #if defined(OS_MACOSX) |
429 // Test that if there is an existing lock file, and we could not flock() | 448 // Test that if there is an existing lock file, and we could not flock() |
430 // it, then exit. | 449 // it, then exit. |
431 TEST_F(ProcessSingletonPosixTest, CreateRespectsOldMacLock) { | 450 TEST_F(ProcessSingletonPosixTest, CreateRespectsOldMacLock) { |
432 std::unique_ptr<TestableProcessSingleton> process_singleton( | 451 std::unique_ptr<TestableProcessSingleton> process_singleton( |
433 CreateProcessSingleton()); | 452 CreateProcessSingleton()); |
434 base::ScopedFD lock_fd(HANDLE_EINTR( | 453 base::ScopedFD lock_fd(HANDLE_EINTR( |
435 open(lock_path_.value().c_str(), O_RDWR | O_CREAT | O_EXLOCK, 0644))); | 454 open(lock_path_.value().c_str(), O_RDWR | O_CREAT | O_EXLOCK, 0644))); |
436 ASSERT_TRUE(lock_fd.is_valid()); | 455 ASSERT_TRUE(lock_fd.is_valid()); |
437 EXPECT_FALSE(process_singleton->Create()); | 456 EXPECT_FALSE(process_singleton->Create()); |
438 base::File::Info info; | 457 base::File::Info info; |
439 EXPECT_TRUE(base::GetFileInfo(lock_path_, &info)); | 458 EXPECT_TRUE(base::GetFileInfo(lock_path_, &info)); |
440 EXPECT_FALSE(info.is_directory); | 459 EXPECT_FALSE(info.is_directory); |
441 EXPECT_FALSE(info.is_symbolic_link); | 460 EXPECT_FALSE(info.is_symbolic_link); |
442 } | 461 } |
443 | 462 |
444 // Test that if there is an existing lock file, and it's not locked, we replace | 463 // Test that if there is an existing lock file, and it's not locked, we replace |
445 // it. | 464 // it. |
446 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) { | 465 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) { |
447 std::unique_ptr<TestableProcessSingleton> process_singleton( | 466 std::unique_ptr<TestableProcessSingleton> process_singleton( |
448 CreateProcessSingleton()); | 467 CreateProcessSingleton()); |
449 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0)); | 468 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0)); |
450 EXPECT_TRUE(process_singleton->Create()); | 469 EXPECT_TRUE(process_singleton->Create()); |
451 VerifyFiles(); | 470 VerifyFiles(); |
452 } | 471 } |
453 #endif // defined(OS_MACOSX) | 472 #endif // defined(OS_MACOSX) |
OLD | NEW |