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 |
124 ProcessSingleton::NotifyResult NotifyOtherProcess( | 127 ProcessSingleton::NotifyResult NotifyOtherProcess( |
125 bool override_kill, | 128 bool override_kill, |
126 base::TimeDelta timeout) { | 129 base::TimeDelta timeout) { |
127 scoped_ptr<TestableProcessSingleton> process_singleton( | 130 scoped_ptr<TestableProcessSingleton> process_singleton( |
128 CreateProcessSingleton()); | 131 CreateProcessSingleton()); |
129 CommandLine command_line(CommandLine::ForCurrentProcess()->GetProgram()); | 132 CommandLine command_line(CommandLine::ForCurrentProcess()->GetProgram()); |
130 command_line.AppendArg("about:blank"); | 133 command_line.AppendArg("about:blank"); |
131 if (override_kill) { | 134 if (override_kill) { |
132 process_singleton->OverrideCurrentPidForTesting( | 135 process_singleton->OverrideCurrentPidForTesting( |
133 base::GetCurrentProcId() + 1); | 136 base::GetCurrentProcId() + 1); |
134 process_singleton->OverrideKillCallbackForTesting( | 137 process_singleton->OverrideKillCallbackForTesting( |
135 base::Bind(&ProcessSingletonLinuxTest::KillCallback, | 138 base::Bind(&ProcessSingletonPosixTest::KillCallback, |
136 base::Unretained(this))); | 139 base::Unretained(this))); |
137 } | 140 } |
138 | 141 |
139 return process_singleton->NotifyOtherProcessWithTimeout( | 142 return process_singleton->NotifyOtherProcessWithTimeout( |
140 command_line, timeout.InSeconds(), true); | 143 command_line, timeout.InSeconds(), true); |
141 } | 144 } |
142 | 145 |
143 // A helper method to call ProcessSingleton::NotifyOtherProcessOrCreate(). | 146 // A helper method to call ProcessSingleton::NotifyOtherProcessOrCreate(). |
144 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate( | 147 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate( |
145 const std::string& url, | 148 const std::string& url, |
(...skipping 19 matching lines...) Expand all Loading... | |
165 break; | 168 break; |
166 } | 169 } |
167 } | 170 } |
168 ASSERT_TRUE(found); | 171 ASSERT_TRUE(found); |
169 ASSERT_EQ(0, kill_callbacks_); | 172 ASSERT_EQ(0, kill_callbacks_); |
170 } | 173 } |
171 | 174 |
172 void BlockWorkerThread() { | 175 void BlockWorkerThread() { |
173 worker_thread_->message_loop()->PostTask( | 176 worker_thread_->message_loop()->PostTask( |
174 FROM_HERE, | 177 FROM_HERE, |
175 base::Bind(&ProcessSingletonLinuxTest::BlockThread, | 178 base::Bind(&ProcessSingletonPosixTest::BlockThread, |
176 base::Unretained(this))); | 179 base::Unretained(this))); |
177 } | 180 } |
178 | 181 |
179 void UnblockWorkerThread() { | 182 void UnblockWorkerThread() { |
180 wait_event_.Signal(); // Unblock the worker thread for shutdown. | 183 wait_event_.Signal(); // Unblock the worker thread for shutdown. |
181 signal_event_.Wait(); // Ensure thread unblocks before continuing. | 184 signal_event_.Wait(); // Ensure thread unblocks before continuing. |
182 } | 185 } |
183 | 186 |
184 void BlockThread() { | 187 void BlockThread() { |
185 wait_event_.Wait(); | 188 wait_event_.Wait(); |
(...skipping 26 matching lines...) Expand all Loading... | |
212 base::ScopedTempDir temp_dir_; | 215 base::ScopedTempDir temp_dir_; |
213 base::WaitableEvent wait_event_; | 216 base::WaitableEvent wait_event_; |
214 base::WaitableEvent signal_event_; | 217 base::WaitableEvent signal_event_; |
215 | 218 |
216 scoped_ptr<base::Thread> worker_thread_; | 219 scoped_ptr<base::Thread> worker_thread_; |
217 TestableProcessSingleton* process_singleton_on_thread_; | 220 TestableProcessSingleton* process_singleton_on_thread_; |
218 }; | 221 }; |
219 | 222 |
220 } // namespace | 223 } // namespace |
221 | 224 |
222 // Test if the socket file and symbol link created by ProcessSingletonLinux | 225 // Test if the socket file and symbol link created by ProcessSingletonPosix |
223 // are valid. | 226 // are valid. |
224 // If this test flakes, use http://crbug.com/74554. | 227 // If this test flakes, use http://crbug.com/74554. |
225 TEST_F(ProcessSingletonLinuxTest, CheckSocketFile) { | 228 TEST_F(ProcessSingletonPosixTest, CheckSocketFile) { |
226 CreateProcessSingletonOnThread(); | 229 CreateProcessSingletonOnThread(); |
227 struct stat statbuf; | 230 struct stat statbuf; |
228 ASSERT_EQ(0, lstat(lock_path_.value().c_str(), &statbuf)); | 231 ASSERT_EQ(0, lstat(lock_path_.value().c_str(), &statbuf)); |
229 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); | 232 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); |
230 char buf[PATH_MAX]; | 233 char buf[PATH_MAX]; |
231 ssize_t len = readlink(lock_path_.value().c_str(), buf, PATH_MAX); | 234 ssize_t len = readlink(lock_path_.value().c_str(), buf, PATH_MAX); |
232 ASSERT_GT(len, 0); | 235 ASSERT_GT(len, 0); |
233 | 236 |
234 ASSERT_EQ(0, lstat(socket_path_.value().c_str(), &statbuf)); | 237 ASSERT_EQ(0, lstat(socket_path_.value().c_str(), &statbuf)); |
235 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); | 238 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); |
(...skipping 11 matching lines...) Expand all Loading... | |
247 | 250 |
248 base::FilePath remote_cookie_path = socket_target_path.DirName(). | 251 base::FilePath remote_cookie_path = socket_target_path.DirName(). |
249 Append(chrome::kSingletonCookieFilename); | 252 Append(chrome::kSingletonCookieFilename); |
250 len = readlink(remote_cookie_path.value().c_str(), buf, PATH_MAX); | 253 len = readlink(remote_cookie_path.value().c_str(), buf, PATH_MAX); |
251 ASSERT_GT(len, 0); | 254 ASSERT_GT(len, 0); |
252 EXPECT_EQ(cookie, std::string(buf, len)); | 255 EXPECT_EQ(cookie, std::string(buf, len)); |
253 } | 256 } |
254 | 257 |
255 // TODO(james.su@gmail.com): port following tests to Windows. | 258 // TODO(james.su@gmail.com): port following tests to Windows. |
256 // Test success case of NotifyOtherProcess(). | 259 // Test success case of NotifyOtherProcess(). |
257 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessSuccess) { | 260 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessSuccess) { |
258 CreateProcessSingletonOnThread(); | 261 CreateProcessSingletonOnThread(); |
259 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, | 262 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, |
260 NotifyOtherProcess(true, TestTimeouts::action_timeout())); | 263 NotifyOtherProcess(true, TestTimeouts::action_timeout())); |
261 CheckNotified(); | 264 CheckNotified(); |
262 } | 265 } |
263 | 266 |
264 // Test failure case of NotifyOtherProcess(). | 267 // Test failure case of NotifyOtherProcess(). |
265 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessFailure) { | 268 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessFailure) { |
266 CreateProcessSingletonOnThread(); | 269 CreateProcessSingletonOnThread(); |
267 | 270 |
268 BlockWorkerThread(); | 271 BlockWorkerThread(); |
269 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, | 272 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, |
270 NotifyOtherProcess(true, TestTimeouts::action_timeout())); | 273 NotifyOtherProcess(true, TestTimeouts::action_timeout())); |
271 | 274 |
272 ASSERT_EQ(1, kill_callbacks_); | 275 ASSERT_EQ(1, kill_callbacks_); |
273 UnblockWorkerThread(); | 276 UnblockWorkerThread(); |
274 } | 277 } |
275 | 278 |
276 // Test that we don't kill ourselves by accident if a lockfile with the same pid | 279 // Test that we don't kill ourselves by accident if a lockfile with the same pid |
277 // happens to exist. | 280 // happens to exist. |
278 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessNoSuicide) { | 281 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessNoSuicide) { |
279 CreateProcessSingletonOnThread(); | 282 CreateProcessSingletonOnThread(); |
280 // Replace lockfile with one containing our own pid. | 283 // Replace lockfile with one containing our own pid. |
281 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 284 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
282 std::string symlink_content = base::StringPrintf( | 285 std::string symlink_content = base::StringPrintf( |
283 "%s%c%u", | 286 "%s%c%u", |
284 net::GetHostName().c_str(), | 287 net::GetHostName().c_str(), |
285 '-', | 288 '-', |
286 base::GetCurrentProcId()); | 289 base::GetCurrentProcId()); |
287 EXPECT_EQ(0, symlink(symlink_content.c_str(), lock_path_.value().c_str())); | 290 EXPECT_EQ(0, symlink(symlink_content.c_str(), lock_path_.value().c_str())); |
288 | 291 |
289 // Remove socket so that we will not be able to notify the existing browser. | 292 // Remove socket so that we will not be able to notify the existing browser. |
290 EXPECT_EQ(0, unlink(socket_path_.value().c_str())); | 293 EXPECT_EQ(0, unlink(socket_path_.value().c_str())); |
291 | 294 |
292 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, | 295 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, |
293 NotifyOtherProcess(false, TestTimeouts::action_timeout())); | 296 NotifyOtherProcess(false, TestTimeouts::action_timeout())); |
294 // If we've gotten to this point without killing ourself, the test succeeded. | 297 // If we've gotten to this point without killing ourself, the test succeeded. |
295 } | 298 } |
296 | 299 |
297 // Test that we can still notify a process on the same host even after the | 300 // Test that we can still notify a process on the same host even after the |
298 // hostname changed. | 301 // hostname changed. |
299 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessHostChanged) { | 302 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessHostChanged) { |
300 CreateProcessSingletonOnThread(); | 303 CreateProcessSingletonOnThread(); |
301 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 304 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
302 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 305 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
303 | 306 |
304 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, | 307 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, |
305 NotifyOtherProcess(false, TestTimeouts::action_timeout())); | 308 NotifyOtherProcess(false, TestTimeouts::action_timeout())); |
306 CheckNotified(); | 309 CheckNotified(); |
307 } | 310 } |
308 | 311 |
309 // Test that we fail when lock says process is on another host and we can't | 312 // Test that we fail when lock says process is on another host and we can't |
310 // notify it over the socket. | 313 // notify it over the socket. |
311 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessDifferingHost) { | 314 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessDifferingHost) { |
312 CreateProcessSingletonOnThread(); | 315 CreateProcessSingletonOnThread(); |
313 | 316 |
314 BlockWorkerThread(); | 317 BlockWorkerThread(); |
315 | 318 |
316 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 319 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
317 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 320 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
318 | 321 |
319 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, | 322 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, |
320 NotifyOtherProcess(false, TestTimeouts::action_timeout())); | 323 NotifyOtherProcess(false, TestTimeouts::action_timeout())); |
321 | 324 |
322 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); | 325 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); |
323 | 326 |
324 UnblockWorkerThread(); | 327 UnblockWorkerThread(); |
325 } | 328 } |
326 | 329 |
327 // Test that we fail when lock says process is on another host and we can't | 330 // Test that we fail when lock says process is on another host and we can't |
328 // notify it over the socket. | 331 // notify it over the socket. |
329 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessOrCreate_DifferingHost) { | 332 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessOrCreate_DifferingHost) { |
330 CreateProcessSingletonOnThread(); | 333 CreateProcessSingletonOnThread(); |
331 | 334 |
332 BlockWorkerThread(); | 335 BlockWorkerThread(); |
333 | 336 |
334 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 337 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
335 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 338 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
336 | 339 |
337 std::string url("about:blank"); | 340 std::string url("about:blank"); |
338 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, | 341 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, |
339 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout())); | 342 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout())); |
340 | 343 |
341 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); | 344 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); |
342 | 345 |
343 UnblockWorkerThread(); | 346 UnblockWorkerThread(); |
344 } | 347 } |
345 | 348 |
346 // Test that Create fails when another browser is using the profile directory. | 349 // Test that Create fails when another browser is using the profile directory. |
347 TEST_F(ProcessSingletonLinuxTest, CreateFailsWithExistingBrowser) { | 350 TEST_F(ProcessSingletonPosixTest, CreateFailsWithExistingBrowser) { |
348 CreateProcessSingletonOnThread(); | 351 CreateProcessSingletonOnThread(); |
349 | 352 |
350 scoped_ptr<TestableProcessSingleton> process_singleton( | 353 scoped_ptr<TestableProcessSingleton> process_singleton( |
351 CreateProcessSingleton()); | 354 CreateProcessSingleton()); |
352 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1); | 355 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1); |
353 EXPECT_FALSE(process_singleton->Create()); | 356 EXPECT_FALSE(process_singleton->Create()); |
354 } | 357 } |
355 | 358 |
356 // Test that Create fails when another browser is using the profile directory | 359 // Test that Create fails when another browser is using the profile directory |
357 // but with the old socket location. | 360 // but with the old socket location. |
358 TEST_F(ProcessSingletonLinuxTest, CreateChecksCompatibilitySocket) { | 361 TEST_F(ProcessSingletonPosixTest, CreateChecksCompatibilitySocket) { |
359 CreateProcessSingletonOnThread(); | 362 CreateProcessSingletonOnThread(); |
360 scoped_ptr<TestableProcessSingleton> process_singleton( | 363 scoped_ptr<TestableProcessSingleton> process_singleton( |
361 CreateProcessSingleton()); | 364 CreateProcessSingleton()); |
362 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1); | 365 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1); |
363 | 366 |
364 // Do some surgery so as to look like the old configuration. | 367 // Do some surgery so as to look like the old configuration. |
365 char buf[PATH_MAX]; | 368 char buf[PATH_MAX]; |
366 ssize_t len = readlink(socket_path_.value().c_str(), buf, sizeof(buf)); | 369 ssize_t len = readlink(socket_path_.value().c_str(), buf, sizeof(buf)); |
367 ASSERT_GT(len, 0); | 370 ASSERT_GT(len, 0); |
368 base::FilePath socket_target_path = base::FilePath(std::string(buf, len)); | 371 base::FilePath socket_target_path = base::FilePath(std::string(buf, len)); |
369 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); | 372 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); |
370 ASSERT_EQ(0, rename(socket_target_path.value().c_str(), | 373 ASSERT_EQ(0, rename(socket_target_path.value().c_str(), |
371 socket_path_.value().c_str())); | 374 socket_path_.value().c_str())); |
372 ASSERT_EQ(0, unlink(cookie_path_.value().c_str())); | 375 ASSERT_EQ(0, unlink(cookie_path_.value().c_str())); |
373 | 376 |
374 EXPECT_FALSE(process_singleton->Create()); | 377 EXPECT_FALSE(process_singleton->Create()); |
375 } | 378 } |
376 | 379 |
377 // Test that we fail when lock says process is on another host and we can't | 380 // 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. | 381 // notify it over the socket before of a bad cookie. |
379 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessOrCreate_BadCookie) { | 382 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessOrCreate_BadCookie) { |
380 CreateProcessSingletonOnThread(); | 383 CreateProcessSingletonOnThread(); |
381 // Change the cookie. | 384 // Change the cookie. |
382 EXPECT_EQ(0, unlink(cookie_path_.value().c_str())); | 385 EXPECT_EQ(0, unlink(cookie_path_.value().c_str())); |
383 EXPECT_EQ(0, symlink("INCORRECTCOOKIE", cookie_path_.value().c_str())); | 386 EXPECT_EQ(0, symlink("INCORRECTCOOKIE", cookie_path_.value().c_str())); |
384 | 387 |
385 // Also change the hostname, so the remote does not retry. | 388 // Also change the hostname, so the remote does not retry. |
386 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 389 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
387 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 390 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
388 | 391 |
389 std::string url("about:blank"); | 392 std::string url("about:blank"); |
390 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, | 393 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, |
391 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout())); | 394 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout())); |
392 } | 395 } |
393 | 396 |
397 #if defined(OS_MACOSX) | |
398 // Test that if there is an existing lock file, and we could not flock() | |
399 // it, then exit. | |
400 TEST_F(ProcessSingletonPosixTest, CreateRespectsOldMacLock) { | |
401 scoped_ptr<TestableProcessSingleton> process_singleton( | |
402 CreateProcessSingleton()); | |
403 int lock_fd = HANDLE_EINTR(open(lock_path_.value().c_str(), | |
404 O_RDWR | O_CREAT | O_EXLOCK, 0644)); | |
405 ASSERT_NE(-1, lock_fd); | |
406 EXPECT_FALSE(process_singleton->Create()); | |
mattm
2014/04/24 22:58:51
maybe also double check that after that the lockpa
jackhou1
2014/04/28 05:20:16
Done.
| |
407 } | |
408 | |
409 // Test that if there is an existing lock file, and it's not locked, we replace | |
410 // it. | |
411 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) { | |
412 scoped_ptr<TestableProcessSingleton> process_singleton( | |
413 CreateProcessSingleton()); | |
414 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0)); | |
415 EXPECT_TRUE(process_singleton->Create()); | |
416 EXPECT_TRUE(base::IsLink(lock_path_)); | |
417 } | |
418 #endif // defined(OS_MACOSX) | |
OLD | NEW |