| OLD | NEW |
| 1 // Copyright (c) 2011 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 #include "chrome/browser/process_singleton.h" | 5 #include "chrome/browser/process_singleton.h" |
| 6 | 6 |
| 7 #include <sys/types.h> | 7 #include <sys/types.h> |
| 8 #include <sys/wait.h> | 8 #include <sys/wait.h> |
| 9 #include <signal.h> | 9 #include <signal.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| 11 #include <vector> | 11 #include <vector> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/bind.h" |
| 15 #include "base/command_line.h" |
| 14 #include "base/eintr_wrapper.h" | 16 #include "base/eintr_wrapper.h" |
| 17 #include "base/file_path.h" |
| 15 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 16 #include "base/stringprintf.h" | 19 #include "base/stringprintf.h" |
| 17 #include "base/test/test_timeouts.h" | 20 #include "base/test/test_timeouts.h" |
| 18 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
| 19 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
| 20 #include "chrome/common/chrome_constants.h" | 23 #include "chrome/common/chrome_constants.h" |
| 21 #include "chrome/common/chrome_paths.h" | 24 #include "chrome/common/chrome_paths.h" |
| 22 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
| 23 #include "chrome/test/base/chrome_process_util.h" | 26 #include "chrome/test/base/chrome_process_util.h" |
| 24 #include "chrome/test/ui/ui_test.h" | 27 #include "chrome/test/ui/ui_test.h" |
| 25 #include "net/base/net_util.h" | 28 #include "net/base/net_util.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 30 |
| 28 namespace { | 31 namespace { |
| 29 | 32 |
| 33 bool UnexpectedNotificationCallback(const CommandLine& command_line, |
| 34 const FilePath& current_directory) { |
| 35 ADD_FAILURE() << "This callback should never be invoked because the active " |
| 36 << "ProcessSingleton is that of the Browser process hosted by " |
| 37 << "UITest."; |
| 38 return false; |
| 39 } |
| 40 |
| 30 class ProcessSingletonLinuxTest : public UITest { | 41 class ProcessSingletonLinuxTest : public UITest { |
| 31 public: | 42 public: |
| 32 virtual void SetUp() { | 43 virtual void SetUp() { |
| 33 UITest::SetUp(); | 44 UITest::SetUp(); |
| 34 lock_path_ = user_data_dir().Append(chrome::kSingletonLockFilename); | 45 lock_path_ = user_data_dir().Append(chrome::kSingletonLockFilename); |
| 35 socket_path_ = user_data_dir().Append(chrome::kSingletonSocketFilename); | 46 socket_path_ = user_data_dir().Append(chrome::kSingletonSocketFilename); |
| 36 cookie_path_ = user_data_dir().Append(chrome::kSingletonCookieFilename); | 47 cookie_path_ = user_data_dir().Append(chrome::kSingletonCookieFilename); |
| 37 } | 48 } |
| 38 | 49 |
| 39 virtual void TearDown() { | 50 virtual void TearDown() { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 } | 98 } |
| 88 | 99 |
| 89 // A helper method to call ProcessSingleton::NotifyOtherProcessOrCreate(). | 100 // A helper method to call ProcessSingleton::NotifyOtherProcessOrCreate(). |
| 90 // |url| will be added to CommandLine for current process, so that it can be | 101 // |url| will be added to CommandLine for current process, so that it can be |
| 91 // sent to browser process by ProcessSingleton::NotifyOtherProcessOrCreate(). | 102 // sent to browser process by ProcessSingleton::NotifyOtherProcessOrCreate(). |
| 92 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate( | 103 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate( |
| 93 const std::string& url, | 104 const std::string& url, |
| 94 int timeout_ms) { | 105 int timeout_ms) { |
| 95 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); | 106 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); |
| 96 return process_singleton->NotifyOtherProcessWithTimeoutOrCreate( | 107 return process_singleton->NotifyOtherProcessWithTimeoutOrCreate( |
| 97 CommandLineForUrl(url), timeout_ms / 1000); | 108 CommandLineForUrl(url), |
| 109 base::Bind(&UnexpectedNotificationCallback), |
| 110 timeout_ms / 1000); |
| 98 } | 111 } |
| 99 | 112 |
| 100 } // namespace | 113 } // namespace |
| 101 | 114 |
| 102 // Test if the socket file and symbol link created by ProcessSingletonLinux | 115 // Test if the socket file and symbol link created by ProcessSingletonLinux |
| 103 // are valid. When running this test, the ProcessSingleton object is already | 116 // are valid. When running this test, the ProcessSingleton object is already |
| 104 // initiated by UITest. So we just test against this existing object. | 117 // initiated by UITest. So we just test against this existing object. |
| 105 // This test is flaky as per http://crbug.com/74554. | 118 // This test is flaky as per http://crbug.com/74554. |
| 106 TEST_F(ProcessSingletonLinuxTest, DISABLED_CheckSocketFile) { | 119 TEST_F(ProcessSingletonLinuxTest, DISABLED_CheckSocketFile) { |
| 107 struct stat statbuf; | 120 struct stat statbuf; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 std::string url("about:blank"); | 277 std::string url("about:blank"); |
| 265 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, | 278 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, |
| 266 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout_ms())); | 279 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout_ms())); |
| 267 | 280 |
| 268 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); | 281 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); |
| 269 } | 282 } |
| 270 | 283 |
| 271 // Test that Create fails when another browser is using the profile directory. | 284 // Test that Create fails when another browser is using the profile directory. |
| 272 TEST_F(ProcessSingletonLinuxTest, CreateFailsWithExistingBrowser) { | 285 TEST_F(ProcessSingletonLinuxTest, CreateFailsWithExistingBrowser) { |
| 273 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); | 286 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); |
| 274 EXPECT_FALSE(process_singleton->Create()); | 287 EXPECT_FALSE(process_singleton->Create( |
| 288 base::Bind(&UnexpectedNotificationCallback))); |
| 275 } | 289 } |
| 276 | 290 |
| 277 // Test that Create fails when another browser is using the profile directory | 291 // Test that Create fails when another browser is using the profile directory |
| 278 // but with the old socket location. | 292 // but with the old socket location. |
| 279 TEST_F(ProcessSingletonLinuxTest, CreateChecksCompatibilitySocket) { | 293 TEST_F(ProcessSingletonLinuxTest, CreateChecksCompatibilitySocket) { |
| 280 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); | 294 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); |
| 281 | 295 |
| 282 // Do some surgery so as to look like the old configuration. | 296 // Do some surgery so as to look like the old configuration. |
| 283 char buf[PATH_MAX]; | 297 char buf[PATH_MAX]; |
| 284 ssize_t len = readlink(socket_path_.value().c_str(), buf, sizeof(buf)); | 298 ssize_t len = readlink(socket_path_.value().c_str(), buf, sizeof(buf)); |
| 285 ASSERT_GT(len, 0); | 299 ASSERT_GT(len, 0); |
| 286 FilePath socket_target_path = FilePath(std::string(buf, len)); | 300 FilePath socket_target_path = FilePath(std::string(buf, len)); |
| 287 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); | 301 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); |
| 288 ASSERT_EQ(0, rename(socket_target_path.value().c_str(), | 302 ASSERT_EQ(0, rename(socket_target_path.value().c_str(), |
| 289 socket_path_.value().c_str())); | 303 socket_path_.value().c_str())); |
| 290 ASSERT_EQ(0, unlink(cookie_path_.value().c_str())); | 304 ASSERT_EQ(0, unlink(cookie_path_.value().c_str())); |
| 291 | 305 |
| 292 EXPECT_FALSE(process_singleton->Create()); | 306 EXPECT_FALSE(process_singleton->Create( |
| 307 base::Bind(&UnexpectedNotificationCallback))); |
| 293 } | 308 } |
| 294 | 309 |
| 295 // Test that we fail when lock says process is on another host and we can't | 310 // Test that we fail when lock says process is on another host and we can't |
| 296 // notify it over the socket before of a bad cookie. | 311 // notify it over the socket before of a bad cookie. |
| 297 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessOrCreate_BadCookie) { | 312 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessOrCreate_BadCookie) { |
| 298 // Change the cookie. | 313 // Change the cookie. |
| 299 EXPECT_EQ(0, unlink(cookie_path_.value().c_str())); | 314 EXPECT_EQ(0, unlink(cookie_path_.value().c_str())); |
| 300 EXPECT_EQ(0, symlink("INCORRECTCOOKIE", cookie_path_.value().c_str())); | 315 EXPECT_EQ(0, symlink("INCORRECTCOOKIE", cookie_path_.value().c_str())); |
| 301 | 316 |
| 302 // Also change the hostname, so the remote does not retry. | 317 // Also change the hostname, so the remote does not retry. |
| 303 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 318 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
| 304 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 319 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
| 305 | 320 |
| 306 std::string url("about:blank"); | 321 std::string url("about:blank"); |
| 307 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, | 322 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, |
| 308 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout_ms())); | 323 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout_ms())); |
| 309 } | 324 } |
| OLD | NEW |