| 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 #include "chrome/browser/process_singleton.h" | 5 #include "chrome/browser/process_singleton.h" |
| 6 | 6 |
| 7 #include <fcntl.h> |
| 7 #include <signal.h> | 8 #include <signal.h> |
| 8 #include <sys/types.h> | 9 #include <sys/types.h> |
| 9 #include <sys/wait.h> | 10 #include <sys/wait.h> |
| 10 #include <unistd.h> | 11 #include <unistd.h> |
| 11 | 12 |
| 12 #include <string> | 13 #include <string> |
| 13 #include <vector> | 14 #include <vector> |
| 14 | 15 |
| 15 #include "base/bind.h" | 16 #include "base/bind.h" |
| 16 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 18 #include "base/file_util.h" |
| 17 #include "base/files/file_path.h" | 19 #include "base/files/file_path.h" |
| 18 #include "base/files/scoped_temp_dir.h" | 20 #include "base/files/scoped_temp_dir.h" |
| 19 #include "base/message_loop/message_loop.h" | 21 #include "base/message_loop/message_loop.h" |
| 22 #include "base/posix/eintr_wrapper.h" |
| 20 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
| 21 #include "base/synchronization/waitable_event.h" | 24 #include "base/synchronization/waitable_event.h" |
| 22 #include "base/test/test_timeouts.h" | 25 #include "base/test/test_timeouts.h" |
| 23 #include "base/test/thread_test_helper.h" | 26 #include "base/test/thread_test_helper.h" |
| 24 #include "base/threading/thread.h" | 27 #include "base/threading/thread.h" |
| 25 #include "chrome/common/chrome_constants.h" | 28 #include "chrome/common/chrome_constants.h" |
| 26 #include "content/public/test/test_browser_thread.h" | 29 #include "content/public/test/test_browser_thread.h" |
| 27 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 32 |
| 30 using content::BrowserThread; | 33 using content::BrowserThread; |
| 31 | 34 |
| 32 namespace { | 35 namespace { |
| 33 | 36 |
| 34 class ProcessSingletonLinuxTest : public testing::Test { | 37 class ProcessSingletonPosixTest : public testing::Test { |
| 35 public: | 38 public: |
| 36 // A ProcessSingleton exposing some protected methods for testing. | 39 // A ProcessSingleton exposing some protected methods for testing. |
| 37 class TestableProcessSingleton : public ProcessSingleton { | 40 class TestableProcessSingleton : public ProcessSingleton { |
| 38 public: | 41 public: |
| 39 explicit TestableProcessSingleton(const base::FilePath& user_data_dir) | 42 explicit TestableProcessSingleton(const base::FilePath& user_data_dir) |
| 40 : ProcessSingleton( | 43 : ProcessSingleton( |
| 41 user_data_dir, | 44 user_data_dir, |
| 42 base::Bind(&TestableProcessSingleton::NotificationCallback, | 45 base::Bind(&TestableProcessSingleton::NotificationCallback, |
| 43 base::Unretained(this))) {} | 46 base::Unretained(this))) {} |
| 44 | 47 |
| 45 | 48 |
| 46 std::vector<CommandLine::StringVector> callback_command_lines_; | 49 std::vector<CommandLine::StringVector> callback_command_lines_; |
| 47 | 50 |
| 48 using ProcessSingleton::NotifyOtherProcessWithTimeout; | 51 using ProcessSingleton::NotifyOtherProcessWithTimeout; |
| 49 using ProcessSingleton::NotifyOtherProcessWithTimeoutOrCreate; | 52 using ProcessSingleton::NotifyOtherProcessWithTimeoutOrCreate; |
| 50 using ProcessSingleton::OverrideCurrentPidForTesting; | 53 using ProcessSingleton::OverrideCurrentPidForTesting; |
| 51 using ProcessSingleton::OverrideKillCallbackForTesting; | 54 using ProcessSingleton::OverrideKillCallbackForTesting; |
| 52 | 55 |
| 53 private: | 56 private: |
| 54 bool NotificationCallback(const CommandLine& command_line, | 57 bool NotificationCallback(const CommandLine& command_line, |
| 55 const base::FilePath& current_directory) { | 58 const base::FilePath& current_directory) { |
| 56 callback_command_lines_.push_back(command_line.argv()); | 59 callback_command_lines_.push_back(command_line.argv()); |
| 57 return true; | 60 return true; |
| 58 } | 61 } |
| 59 }; | 62 }; |
| 60 | 63 |
| 61 ProcessSingletonLinuxTest() | 64 ProcessSingletonPosixTest() |
| 62 : kill_callbacks_(0), | 65 : kill_callbacks_(0), |
| 63 io_thread_(BrowserThread::IO), | 66 io_thread_(BrowserThread::IO), |
| 64 wait_event_(true, false), | 67 wait_event_(true, false), |
| 65 signal_event_(true, false), | 68 signal_event_(true, false), |
| 66 process_singleton_on_thread_(NULL) { | 69 process_singleton_on_thread_(NULL) { |
| 67 io_thread_.StartIOThread(); | 70 io_thread_.StartIOThread(); |
| 68 } | 71 } |
| 69 | 72 |
| 70 virtual void SetUp() { | 73 virtual void SetUp() { |
| 71 testing::Test::SetUp(); | 74 testing::Test::SetUp(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 82 virtual void TearDown() { | 85 virtual void TearDown() { |
| 83 scoped_refptr<base::ThreadTestHelper> io_helper(new base::ThreadTestHelper( | 86 scoped_refptr<base::ThreadTestHelper> io_helper(new base::ThreadTestHelper( |
| 84 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get())); | 87 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get())); |
| 85 ASSERT_TRUE(io_helper->Run()); | 88 ASSERT_TRUE(io_helper->Run()); |
| 86 | 89 |
| 87 // Destruct the ProcessSingleton object before the IO thread so that its | 90 // Destruct the ProcessSingleton object before the IO thread so that its |
| 88 // internals are destructed properly. | 91 // internals are destructed properly. |
| 89 if (process_singleton_on_thread_) { | 92 if (process_singleton_on_thread_) { |
| 90 worker_thread_->message_loop()->PostTask( | 93 worker_thread_->message_loop()->PostTask( |
| 91 FROM_HERE, | 94 FROM_HERE, |
| 92 base::Bind(&ProcessSingletonLinuxTest::DestructProcessSingleton, | 95 base::Bind(&ProcessSingletonPosixTest::DestructProcessSingleton, |
| 93 base::Unretained(this))); | 96 base::Unretained(this))); |
| 94 | 97 |
| 95 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper( | 98 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper( |
| 96 worker_thread_->message_loop_proxy().get())); | 99 worker_thread_->message_loop_proxy().get())); |
| 97 ASSERT_TRUE(helper->Run()); | 100 ASSERT_TRUE(helper->Run()); |
| 98 } | 101 } |
| 99 | 102 |
| 100 io_thread_.Stop(); | 103 io_thread_.Stop(); |
| 101 testing::Test::TearDown(); | 104 testing::Test::TearDown(); |
| 102 } | 105 } |
| 103 | 106 |
| 104 void CreateProcessSingletonOnThread() { | 107 void CreateProcessSingletonOnThread() { |
| 105 ASSERT_EQ(NULL, worker_thread_.get()); | 108 ASSERT_EQ(NULL, worker_thread_.get()); |
| 106 worker_thread_.reset(new base::Thread("BlockingThread")); | 109 worker_thread_.reset(new base::Thread("BlockingThread")); |
| 107 worker_thread_->Start(); | 110 worker_thread_->Start(); |
| 108 | 111 |
| 109 worker_thread_->message_loop()->PostTask( | 112 worker_thread_->message_loop()->PostTask( |
| 110 FROM_HERE, | 113 FROM_HERE, |
| 111 base::Bind(&ProcessSingletonLinuxTest:: | 114 base::Bind(&ProcessSingletonPosixTest:: |
| 112 CreateProcessSingletonInternal, | 115 CreateProcessSingletonInternal, |
| 113 base::Unretained(this))); | 116 base::Unretained(this))); |
| 114 | 117 |
| 115 scoped_refptr<base::ThreadTestHelper> helper( | 118 scoped_refptr<base::ThreadTestHelper> helper( |
| 116 new base::ThreadTestHelper(worker_thread_->message_loop_proxy().get())); | 119 new base::ThreadTestHelper(worker_thread_->message_loop_proxy().get())); |
| 117 ASSERT_TRUE(helper->Run()); | 120 ASSERT_TRUE(helper->Run()); |
| 118 } | 121 } |
| 119 | 122 |
| 120 TestableProcessSingleton* CreateProcessSingleton() { | 123 TestableProcessSingleton* CreateProcessSingleton() { |
| 121 return new TestableProcessSingleton(temp_dir_.path()); | 124 return new TestableProcessSingleton(temp_dir_.path()); |
| 122 } | 125 } |
| 123 | 126 |
| 127 void VerifyFiles() { |
| 128 struct stat statbuf; |
| 129 ASSERT_EQ(0, lstat(lock_path_.value().c_str(), &statbuf)); |
| 130 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); |
| 131 char buf[PATH_MAX]; |
| 132 ssize_t len = readlink(lock_path_.value().c_str(), buf, PATH_MAX); |
| 133 ASSERT_GT(len, 0); |
| 134 |
| 135 ASSERT_EQ(0, lstat(socket_path_.value().c_str(), &statbuf)); |
| 136 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); |
| 137 |
| 138 len = readlink(socket_path_.value().c_str(), buf, PATH_MAX); |
| 139 ASSERT_GT(len, 0); |
| 140 base::FilePath socket_target_path = base::FilePath(std::string(buf, len)); |
| 141 |
| 142 ASSERT_EQ(0, lstat(socket_target_path.value().c_str(), &statbuf)); |
| 143 ASSERT_TRUE(S_ISSOCK(statbuf.st_mode)); |
| 144 |
| 145 len = readlink(cookie_path_.value().c_str(), buf, PATH_MAX); |
| 146 ASSERT_GT(len, 0); |
| 147 std::string cookie(buf, len); |
| 148 |
| 149 base::FilePath remote_cookie_path = socket_target_path.DirName(). |
| 150 Append(chrome::kSingletonCookieFilename); |
| 151 len = readlink(remote_cookie_path.value().c_str(), buf, PATH_MAX); |
| 152 ASSERT_GT(len, 0); |
| 153 EXPECT_EQ(cookie, std::string(buf, len)); |
| 154 } |
| 155 |
| 124 ProcessSingleton::NotifyResult NotifyOtherProcess( | 156 ProcessSingleton::NotifyResult NotifyOtherProcess( |
| 125 bool override_kill, | 157 bool override_kill, |
| 126 base::TimeDelta timeout) { | 158 base::TimeDelta timeout) { |
| 127 scoped_ptr<TestableProcessSingleton> process_singleton( | 159 scoped_ptr<TestableProcessSingleton> process_singleton( |
| 128 CreateProcessSingleton()); | 160 CreateProcessSingleton()); |
| 129 CommandLine command_line(CommandLine::ForCurrentProcess()->GetProgram()); | 161 CommandLine command_line(CommandLine::ForCurrentProcess()->GetProgram()); |
| 130 command_line.AppendArg("about:blank"); | 162 command_line.AppendArg("about:blank"); |
| 131 if (override_kill) { | 163 if (override_kill) { |
| 132 process_singleton->OverrideCurrentPidForTesting( | 164 process_singleton->OverrideCurrentPidForTesting( |
| 133 base::GetCurrentProcId() + 1); | 165 base::GetCurrentProcId() + 1); |
| 134 process_singleton->OverrideKillCallbackForTesting( | 166 process_singleton->OverrideKillCallbackForTesting( |
| 135 base::Bind(&ProcessSingletonLinuxTest::KillCallback, | 167 base::Bind(&ProcessSingletonPosixTest::KillCallback, |
| 136 base::Unretained(this))); | 168 base::Unretained(this))); |
| 137 } | 169 } |
| 138 | 170 |
| 139 return process_singleton->NotifyOtherProcessWithTimeout( | 171 return process_singleton->NotifyOtherProcessWithTimeout( |
| 140 command_line, timeout.InSeconds(), true); | 172 command_line, timeout.InSeconds(), true); |
| 141 } | 173 } |
| 142 | 174 |
| 143 // A helper method to call ProcessSingleton::NotifyOtherProcessOrCreate(). | 175 // A helper method to call ProcessSingleton::NotifyOtherProcessOrCreate(). |
| 144 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate( | 176 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate( |
| 145 const std::string& url, | 177 const std::string& url, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 165 break; | 197 break; |
| 166 } | 198 } |
| 167 } | 199 } |
| 168 ASSERT_TRUE(found); | 200 ASSERT_TRUE(found); |
| 169 ASSERT_EQ(0, kill_callbacks_); | 201 ASSERT_EQ(0, kill_callbacks_); |
| 170 } | 202 } |
| 171 | 203 |
| 172 void BlockWorkerThread() { | 204 void BlockWorkerThread() { |
| 173 worker_thread_->message_loop()->PostTask( | 205 worker_thread_->message_loop()->PostTask( |
| 174 FROM_HERE, | 206 FROM_HERE, |
| 175 base::Bind(&ProcessSingletonLinuxTest::BlockThread, | 207 base::Bind(&ProcessSingletonPosixTest::BlockThread, |
| 176 base::Unretained(this))); | 208 base::Unretained(this))); |
| 177 } | 209 } |
| 178 | 210 |
| 179 void UnblockWorkerThread() { | 211 void UnblockWorkerThread() { |
| 180 wait_event_.Signal(); // Unblock the worker thread for shutdown. | 212 wait_event_.Signal(); // Unblock the worker thread for shutdown. |
| 181 signal_event_.Wait(); // Ensure thread unblocks before continuing. | 213 signal_event_.Wait(); // Ensure thread unblocks before continuing. |
| 182 } | 214 } |
| 183 | 215 |
| 184 void BlockThread() { | 216 void BlockThread() { |
| 185 wait_event_.Wait(); | 217 wait_event_.Wait(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 212 base::ScopedTempDir temp_dir_; | 244 base::ScopedTempDir temp_dir_; |
| 213 base::WaitableEvent wait_event_; | 245 base::WaitableEvent wait_event_; |
| 214 base::WaitableEvent signal_event_; | 246 base::WaitableEvent signal_event_; |
| 215 | 247 |
| 216 scoped_ptr<base::Thread> worker_thread_; | 248 scoped_ptr<base::Thread> worker_thread_; |
| 217 TestableProcessSingleton* process_singleton_on_thread_; | 249 TestableProcessSingleton* process_singleton_on_thread_; |
| 218 }; | 250 }; |
| 219 | 251 |
| 220 } // namespace | 252 } // namespace |
| 221 | 253 |
| 222 // Test if the socket file and symbol link created by ProcessSingletonLinux | 254 // Test if the socket file and symbol link created by ProcessSingletonPosix |
| 223 // are valid. | 255 // are valid. |
| 224 // If this test flakes, use http://crbug.com/74554. | 256 // If this test flakes, use http://crbug.com/74554. |
| 225 TEST_F(ProcessSingletonLinuxTest, CheckSocketFile) { | 257 TEST_F(ProcessSingletonPosixTest, CheckSocketFile) { |
| 226 CreateProcessSingletonOnThread(); | 258 CreateProcessSingletonOnThread(); |
| 227 struct stat statbuf; | 259 VerifyFiles(); |
| 228 ASSERT_EQ(0, lstat(lock_path_.value().c_str(), &statbuf)); | |
| 229 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); | |
| 230 char buf[PATH_MAX]; | |
| 231 ssize_t len = readlink(lock_path_.value().c_str(), buf, PATH_MAX); | |
| 232 ASSERT_GT(len, 0); | |
| 233 | |
| 234 ASSERT_EQ(0, lstat(socket_path_.value().c_str(), &statbuf)); | |
| 235 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); | |
| 236 | |
| 237 len = readlink(socket_path_.value().c_str(), buf, PATH_MAX); | |
| 238 ASSERT_GT(len, 0); | |
| 239 base::FilePath socket_target_path = base::FilePath(std::string(buf, len)); | |
| 240 | |
| 241 ASSERT_EQ(0, lstat(socket_target_path.value().c_str(), &statbuf)); | |
| 242 ASSERT_TRUE(S_ISSOCK(statbuf.st_mode)); | |
| 243 | |
| 244 len = readlink(cookie_path_.value().c_str(), buf, PATH_MAX); | |
| 245 ASSERT_GT(len, 0); | |
| 246 std::string cookie(buf, len); | |
| 247 | |
| 248 base::FilePath remote_cookie_path = socket_target_path.DirName(). | |
| 249 Append(chrome::kSingletonCookieFilename); | |
| 250 len = readlink(remote_cookie_path.value().c_str(), buf, PATH_MAX); | |
| 251 ASSERT_GT(len, 0); | |
| 252 EXPECT_EQ(cookie, std::string(buf, len)); | |
| 253 } | 260 } |
| 254 | 261 |
| 255 // TODO(james.su@gmail.com): port following tests to Windows. | 262 // TODO(james.su@gmail.com): port following tests to Windows. |
| 256 // Test success case of NotifyOtherProcess(). | 263 // Test success case of NotifyOtherProcess(). |
| 257 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessSuccess) { | 264 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessSuccess) { |
| 258 CreateProcessSingletonOnThread(); | 265 CreateProcessSingletonOnThread(); |
| 259 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, | 266 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, |
| 260 NotifyOtherProcess(true, TestTimeouts::action_timeout())); | 267 NotifyOtherProcess(true, TestTimeouts::action_timeout())); |
| 261 CheckNotified(); | 268 CheckNotified(); |
| 262 } | 269 } |
| 263 | 270 |
| 264 // Test failure case of NotifyOtherProcess(). | 271 // Test failure case of NotifyOtherProcess(). |
| 265 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessFailure) { | 272 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessFailure) { |
| 266 CreateProcessSingletonOnThread(); | 273 CreateProcessSingletonOnThread(); |
| 267 | 274 |
| 268 BlockWorkerThread(); | 275 BlockWorkerThread(); |
| 269 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, | 276 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, |
| 270 NotifyOtherProcess(true, TestTimeouts::action_timeout())); | 277 NotifyOtherProcess(true, TestTimeouts::action_timeout())); |
| 271 | 278 |
| 272 ASSERT_EQ(1, kill_callbacks_); | 279 ASSERT_EQ(1, kill_callbacks_); |
| 273 UnblockWorkerThread(); | 280 UnblockWorkerThread(); |
| 274 } | 281 } |
| 275 | 282 |
| 276 // Test that we don't kill ourselves by accident if a lockfile with the same pid | 283 // Test that we don't kill ourselves by accident if a lockfile with the same pid |
| 277 // happens to exist. | 284 // happens to exist. |
| 278 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessNoSuicide) { | 285 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessNoSuicide) { |
| 279 CreateProcessSingletonOnThread(); | 286 CreateProcessSingletonOnThread(); |
| 280 // Replace lockfile with one containing our own pid. | 287 // Replace lockfile with one containing our own pid. |
| 281 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 288 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
| 282 std::string symlink_content = base::StringPrintf( | 289 std::string symlink_content = base::StringPrintf( |
| 283 "%s%c%u", | 290 "%s%c%u", |
| 284 net::GetHostName().c_str(), | 291 net::GetHostName().c_str(), |
| 285 '-', | 292 '-', |
| 286 base::GetCurrentProcId()); | 293 base::GetCurrentProcId()); |
| 287 EXPECT_EQ(0, symlink(symlink_content.c_str(), lock_path_.value().c_str())); | 294 EXPECT_EQ(0, symlink(symlink_content.c_str(), lock_path_.value().c_str())); |
| 288 | 295 |
| 289 // Remove socket so that we will not be able to notify the existing browser. | 296 // Remove socket so that we will not be able to notify the existing browser. |
| 290 EXPECT_EQ(0, unlink(socket_path_.value().c_str())); | 297 EXPECT_EQ(0, unlink(socket_path_.value().c_str())); |
| 291 | 298 |
| 292 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, | 299 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, |
| 293 NotifyOtherProcess(false, TestTimeouts::action_timeout())); | 300 NotifyOtherProcess(false, TestTimeouts::action_timeout())); |
| 294 // If we've gotten to this point without killing ourself, the test succeeded. | 301 // If we've gotten to this point without killing ourself, the test succeeded. |
| 295 } | 302 } |
| 296 | 303 |
| 297 // Test that we can still notify a process on the same host even after the | 304 // Test that we can still notify a process on the same host even after the |
| 298 // hostname changed. | 305 // hostname changed. |
| 299 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessHostChanged) { | 306 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessHostChanged) { |
| 300 CreateProcessSingletonOnThread(); | 307 CreateProcessSingletonOnThread(); |
| 301 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 308 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
| 302 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 309 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
| 303 | 310 |
| 304 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, | 311 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, |
| 305 NotifyOtherProcess(false, TestTimeouts::action_timeout())); | 312 NotifyOtherProcess(false, TestTimeouts::action_timeout())); |
| 306 CheckNotified(); | 313 CheckNotified(); |
| 307 } | 314 } |
| 308 | 315 |
| 309 // Test that we fail when lock says process is on another host and we can't | 316 // Test that we fail when lock says process is on another host and we can't |
| 310 // notify it over the socket. | 317 // notify it over the socket. |
| 311 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessDifferingHost) { | 318 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessDifferingHost) { |
| 312 CreateProcessSingletonOnThread(); | 319 CreateProcessSingletonOnThread(); |
| 313 | 320 |
| 314 BlockWorkerThread(); | 321 BlockWorkerThread(); |
| 315 | 322 |
| 316 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 323 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
| 317 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 324 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
| 318 | 325 |
| 319 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, | 326 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, |
| 320 NotifyOtherProcess(false, TestTimeouts::action_timeout())); | 327 NotifyOtherProcess(false, TestTimeouts::action_timeout())); |
| 321 | 328 |
| 322 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); | 329 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); |
| 323 | 330 |
| 324 UnblockWorkerThread(); | 331 UnblockWorkerThread(); |
| 325 } | 332 } |
| 326 | 333 |
| 327 // Test that we fail when lock says process is on another host and we can't | 334 // Test that we fail when lock says process is on another host and we can't |
| 328 // notify it over the socket. | 335 // notify it over the socket. |
| 329 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessOrCreate_DifferingHost) { | 336 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessOrCreate_DifferingHost) { |
| 330 CreateProcessSingletonOnThread(); | 337 CreateProcessSingletonOnThread(); |
| 331 | 338 |
| 332 BlockWorkerThread(); | 339 BlockWorkerThread(); |
| 333 | 340 |
| 334 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 341 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
| 335 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 342 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
| 336 | 343 |
| 337 std::string url("about:blank"); | 344 std::string url("about:blank"); |
| 338 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, | 345 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, |
| 339 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout())); | 346 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout())); |
| 340 | 347 |
| 341 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); | 348 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); |
| 342 | 349 |
| 343 UnblockWorkerThread(); | 350 UnblockWorkerThread(); |
| 344 } | 351 } |
| 345 | 352 |
| 346 // Test that Create fails when another browser is using the profile directory. | 353 // Test that Create fails when another browser is using the profile directory. |
| 347 TEST_F(ProcessSingletonLinuxTest, CreateFailsWithExistingBrowser) { | 354 TEST_F(ProcessSingletonPosixTest, CreateFailsWithExistingBrowser) { |
| 348 CreateProcessSingletonOnThread(); | 355 CreateProcessSingletonOnThread(); |
| 349 | 356 |
| 350 scoped_ptr<TestableProcessSingleton> process_singleton( | 357 scoped_ptr<TestableProcessSingleton> process_singleton( |
| 351 CreateProcessSingleton()); | 358 CreateProcessSingleton()); |
| 352 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1); | 359 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1); |
| 353 EXPECT_FALSE(process_singleton->Create()); | 360 EXPECT_FALSE(process_singleton->Create()); |
| 354 } | 361 } |
| 355 | 362 |
| 356 // Test that Create fails when another browser is using the profile directory | 363 // Test that Create fails when another browser is using the profile directory |
| 357 // but with the old socket location. | 364 // but with the old socket location. |
| 358 TEST_F(ProcessSingletonLinuxTest, CreateChecksCompatibilitySocket) { | 365 TEST_F(ProcessSingletonPosixTest, CreateChecksCompatibilitySocket) { |
| 359 CreateProcessSingletonOnThread(); | 366 CreateProcessSingletonOnThread(); |
| 360 scoped_ptr<TestableProcessSingleton> process_singleton( | 367 scoped_ptr<TestableProcessSingleton> process_singleton( |
| 361 CreateProcessSingleton()); | 368 CreateProcessSingleton()); |
| 362 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1); | 369 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1); |
| 363 | 370 |
| 364 // Do some surgery so as to look like the old configuration. | 371 // Do some surgery so as to look like the old configuration. |
| 365 char buf[PATH_MAX]; | 372 char buf[PATH_MAX]; |
| 366 ssize_t len = readlink(socket_path_.value().c_str(), buf, sizeof(buf)); | 373 ssize_t len = readlink(socket_path_.value().c_str(), buf, sizeof(buf)); |
| 367 ASSERT_GT(len, 0); | 374 ASSERT_GT(len, 0); |
| 368 base::FilePath socket_target_path = base::FilePath(std::string(buf, len)); | 375 base::FilePath socket_target_path = base::FilePath(std::string(buf, len)); |
| 369 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); | 376 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); |
| 370 ASSERT_EQ(0, rename(socket_target_path.value().c_str(), | 377 ASSERT_EQ(0, rename(socket_target_path.value().c_str(), |
| 371 socket_path_.value().c_str())); | 378 socket_path_.value().c_str())); |
| 372 ASSERT_EQ(0, unlink(cookie_path_.value().c_str())); | 379 ASSERT_EQ(0, unlink(cookie_path_.value().c_str())); |
| 373 | 380 |
| 374 EXPECT_FALSE(process_singleton->Create()); | 381 EXPECT_FALSE(process_singleton->Create()); |
| 375 } | 382 } |
| 376 | 383 |
| 377 // Test that we fail when lock says process is on another host and we can't | 384 // Test that we fail when lock says process is on another host and we can't |
| 378 // notify it over the socket before of a bad cookie. | 385 // notify it over the socket before of a bad cookie. |
| 379 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessOrCreate_BadCookie) { | 386 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessOrCreate_BadCookie) { |
| 380 CreateProcessSingletonOnThread(); | 387 CreateProcessSingletonOnThread(); |
| 381 // Change the cookie. | 388 // Change the cookie. |
| 382 EXPECT_EQ(0, unlink(cookie_path_.value().c_str())); | 389 EXPECT_EQ(0, unlink(cookie_path_.value().c_str())); |
| 383 EXPECT_EQ(0, symlink("INCORRECTCOOKIE", cookie_path_.value().c_str())); | 390 EXPECT_EQ(0, symlink("INCORRECTCOOKIE", cookie_path_.value().c_str())); |
| 384 | 391 |
| 385 // Also change the hostname, so the remote does not retry. | 392 // Also change the hostname, so the remote does not retry. |
| 386 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 393 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
| 387 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 394 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
| 388 | 395 |
| 389 std::string url("about:blank"); | 396 std::string url("about:blank"); |
| 390 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, | 397 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, |
| 391 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout())); | 398 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout())); |
| 392 } | 399 } |
| 393 | 400 |
| 401 #if defined(OS_MACOSX) |
| 402 // Test that if there is an existing lock file, and we could not flock() |
| 403 // it, then exit. |
| 404 TEST_F(ProcessSingletonPosixTest, CreateRespectsOldMacLock) { |
| 405 scoped_ptr<TestableProcessSingleton> process_singleton( |
| 406 CreateProcessSingleton()); |
| 407 base::ScopedFD lock_fd(HANDLE_EINTR( |
| 408 open(lock_path_.value().c_str(), O_RDWR | O_CREAT | O_EXLOCK, 0644))); |
| 409 ASSERT_TRUE(lock_fd.is_valid()); |
| 410 EXPECT_FALSE(process_singleton->Create()); |
| 411 base::File::Info info; |
| 412 EXPECT_TRUE(base::GetFileInfo(lock_path_, &info)); |
| 413 EXPECT_FALSE(info.is_directory); |
| 414 EXPECT_FALSE(info.is_symbolic_link); |
| 415 } |
| 416 |
| 417 // Test that if there is an existing lock file, and it's not locked, we replace |
| 418 // it. |
| 419 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) { |
| 420 scoped_ptr<TestableProcessSingleton> process_singleton( |
| 421 CreateProcessSingleton()); |
| 422 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0)); |
| 423 EXPECT_TRUE(process_singleton->Create()); |
| 424 VerifyFiles(); |
| 425 } |
| 426 #endif // defined(OS_MACOSX) |
| OLD | NEW |