| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 class ProcessSingletonLinuxTest : public UITest { | 32 class ProcessSingletonLinuxTest : public UITest { |
| 33 public: | 33 public: |
| 34 virtual void SetUp() { | 34 virtual void SetUp() { |
| 35 UITest::SetUp(); | 35 UITest::SetUp(); |
| 36 lock_path_ = user_data_dir().Append(chrome::kSingletonLockFilename); | 36 lock_path_ = user_data_dir().Append(chrome::kSingletonLockFilename); |
| 37 socket_path_ = user_data_dir().Append(chrome::kSingletonSocketFilename); | 37 socket_path_ = user_data_dir().Append(chrome::kSingletonSocketFilename); |
| 38 cookie_path_ = user_data_dir().Append(chrome::kSingletonCookieFilename); |
| 38 } | 39 } |
| 39 | 40 |
| 40 virtual void TearDown() { | 41 virtual void TearDown() { |
| 41 UITest::TearDown(); | 42 UITest::TearDown(); |
| 42 | 43 |
| 43 // Check that the test cleaned up after itself. | 44 // Check that the test cleaned up after itself. |
| 44 struct stat statbuf; | 45 struct stat statbuf; |
| 45 bool lock_exists = lstat(lock_path_.value().c_str(), &statbuf) == 0; | 46 bool lock_exists = lstat(lock_path_.value().c_str(), &statbuf) == 0; |
| 46 EXPECT_FALSE(lock_exists); | 47 EXPECT_FALSE(lock_exists); |
| 47 | 48 |
| 48 if (lock_exists) { | 49 if (lock_exists) { |
| 49 // Unlink to prevent failing future tests if the lock still exists. | 50 // Unlink to prevent failing future tests if the lock still exists. |
| 50 EXPECT_EQ(unlink(lock_path_.value().c_str()), 0); | 51 EXPECT_EQ(unlink(lock_path_.value().c_str()), 0); |
| 51 } | 52 } |
| 52 } | 53 } |
| 53 | 54 |
| 54 FilePath lock_path_; | 55 FilePath lock_path_; |
| 55 FilePath socket_path_; | 56 FilePath socket_path_; |
| 57 FilePath cookie_path_; |
| 56 }; | 58 }; |
| 57 | 59 |
| 58 ProcessSingleton* CreateProcessSingleton() { | 60 ProcessSingleton* CreateProcessSingleton() { |
| 59 FilePath user_data_dir; | 61 FilePath user_data_dir; |
| 60 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | 62 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 61 | 63 |
| 62 return new ProcessSingleton(user_data_dir); | 64 return new ProcessSingleton(user_data_dir); |
| 63 } | 65 } |
| 64 | 66 |
| 65 CommandLine CommandLineForUrl(const std::string& url) { | 67 CommandLine CommandLineForUrl(const std::string& url) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 101 |
| 100 } // namespace | 102 } // namespace |
| 101 | 103 |
| 102 // Test if the socket file and symbol link created by ProcessSingletonLinux | 104 // Test if the socket file and symbol link created by ProcessSingletonLinux |
| 103 // are valid. When running this test, the ProcessSingleton object is already | 105 // are valid. When running this test, the ProcessSingleton object is already |
| 104 // initiated by UITest. So we just test against this existing object. | 106 // initiated by UITest. So we just test against this existing object. |
| 105 TEST_F(ProcessSingletonLinuxTest, CheckSocketFile) { | 107 TEST_F(ProcessSingletonLinuxTest, CheckSocketFile) { |
| 106 struct stat statbuf; | 108 struct stat statbuf; |
| 107 ASSERT_EQ(0, lstat(lock_path_.value().c_str(), &statbuf)); | 109 ASSERT_EQ(0, lstat(lock_path_.value().c_str(), &statbuf)); |
| 108 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); | 110 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); |
| 109 char buf[PATH_MAX + 1]; | 111 char buf[PATH_MAX]; |
| 110 ssize_t len = readlink(lock_path_.value().c_str(), buf, PATH_MAX); | 112 ssize_t len = readlink(lock_path_.value().c_str(), buf, PATH_MAX); |
| 111 ASSERT_GT(len, 0); | 113 ASSERT_GT(len, 0); |
| 112 buf[len] = '\0'; | |
| 113 | 114 |
| 114 ASSERT_EQ(0, lstat(socket_path_.value().c_str(), &statbuf)); | 115 ASSERT_EQ(0, lstat(socket_path_.value().c_str(), &statbuf)); |
| 116 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); |
| 117 |
| 118 len = readlink(socket_path_.value().c_str(), buf, PATH_MAX); |
| 119 ASSERT_GT(len, 0); |
| 120 FilePath socket_target_path = FilePath(std::string(buf, len)); |
| 121 |
| 122 ASSERT_EQ(0, lstat(socket_target_path.value().c_str(), &statbuf)); |
| 115 ASSERT_TRUE(S_ISSOCK(statbuf.st_mode)); | 123 ASSERT_TRUE(S_ISSOCK(statbuf.st_mode)); |
| 124 |
| 125 len = readlink(cookie_path_.value().c_str(), buf, PATH_MAX); |
| 126 ASSERT_GT(len, 0); |
| 127 std::string cookie(buf, len); |
| 128 |
| 129 FilePath remote_cookie_path = socket_target_path.DirName(). |
| 130 Append(chrome::kSingletonCookieFilename); |
| 131 len = readlink(remote_cookie_path.value().c_str(), buf, PATH_MAX); |
| 132 ASSERT_GT(len, 0); |
| 133 EXPECT_EQ(cookie, std::string(buf, len)); |
| 116 } | 134 } |
| 117 | 135 |
| 118 #if defined(OS_LINUX) && defined(TOOLKIT_VIEWS) | 136 #if defined(OS_LINUX) && defined(TOOLKIT_VIEWS) |
| 119 // The following tests in linux/view does not pass without a window manager, | 137 // The following tests in linux/view does not pass without a window manager, |
| 120 // which is true in build/try bots. | 138 // which is true in build/try bots. |
| 121 // See http://crbug.com/30953. | 139 // See http://crbug.com/30953. |
| 122 #define NotifyOtherProcessSuccess FAILS_NotifyOtherProcessSuccess | 140 #define NotifyOtherProcessSuccess FAILS_NotifyOtherProcessSuccess |
| 123 #define NotifyOtherProcessHostChanged FAILS_NotifyOtherProcessHostChanged | 141 #define NotifyOtherProcessHostChanged FAILS_NotifyOtherProcessHostChanged |
| 124 #endif | 142 #endif |
| 125 | 143 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 NotifyOtherProcessOrCreate(url, action_timeout_ms())); | 258 NotifyOtherProcessOrCreate(url, action_timeout_ms())); |
| 241 | 259 |
| 242 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); | 260 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); |
| 243 } | 261 } |
| 244 | 262 |
| 245 // Test that Create fails when another browser is using the profile directory. | 263 // Test that Create fails when another browser is using the profile directory. |
| 246 TEST_F(ProcessSingletonLinuxTest, CreateFailsWithExistingBrowser) { | 264 TEST_F(ProcessSingletonLinuxTest, CreateFailsWithExistingBrowser) { |
| 247 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); | 265 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); |
| 248 EXPECT_FALSE(process_singleton->Create()); | 266 EXPECT_FALSE(process_singleton->Create()); |
| 249 } | 267 } |
| 268 |
| 269 // Test that Create fails when another browser is using the profile directory |
| 270 // but with the old socket location. |
| 271 TEST_F(ProcessSingletonLinuxTest, CreateChecksCompatibilitySocket) { |
| 272 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); |
| 273 |
| 274 // Do some surgery so as to look like the old configuration. |
| 275 char buf[PATH_MAX]; |
| 276 ssize_t len = readlink(socket_path_.value().c_str(), buf, sizeof(buf)); |
| 277 ASSERT_GT(len, 0); |
| 278 FilePath socket_target_path = FilePath(std::string(buf, len)); |
| 279 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); |
| 280 ASSERT_EQ(0, rename(socket_target_path.value().c_str(), |
| 281 socket_path_.value().c_str())); |
| 282 ASSERT_EQ(0, unlink(cookie_path_.value().c_str())); |
| 283 |
| 284 EXPECT_FALSE(process_singleton->Create()); |
| 285 } |
| 286 |
| 287 // Test that we fail when lock says process is on another host and we can't |
| 288 // notify it over the socket before of a bad cookie. |
| 289 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessOrCreate_BadCookie) { |
| 290 // Change the cookie. |
| 291 EXPECT_EQ(0, unlink(cookie_path_.value().c_str())); |
| 292 EXPECT_EQ(0, symlink("INCORRECTCOOKIE", cookie_path_.value().c_str())); |
| 293 |
| 294 // Also change the hostname, so the remote does not retry. |
| 295 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
| 296 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
| 297 |
| 298 std::string url("about:blank"); |
| 299 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, |
| 300 NotifyOtherProcessOrCreate(url, action_timeout_ms())); |
| 301 } |
| OLD | NEW |