Allow the minimum time between banner trigger visits to be varied via field trials.
[chromium-blink-merge.git] / chrome / browser / process_singleton_posix_unittest.cc
blob48d9d2fd246744de59e779cdd996cf05ae00a891
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
3 // found in the LICENSE file.
5 #include "chrome/browser/process_singleton.h"
7 #include <fcntl.h>
8 #include <signal.h>
9 #include <sys/types.h>
10 #include <sys/un.h>
11 #include <sys/wait.h>
12 #include <unistd.h>
14 #include <string>
15 #include <vector>
17 #include "base/bind.h"
18 #include "base/command_line.h"
19 #include "base/files/file_path.h"
20 #include "base/files/file_util.h"
21 #include "base/files/scoped_temp_dir.h"
22 #include "base/location.h"
23 #include "base/posix/eintr_wrapper.h"
24 #include "base/single_thread_task_runner.h"
25 #include "base/strings/stringprintf.h"
26 #include "base/synchronization/waitable_event.h"
27 #include "base/test/test_timeouts.h"
28 #include "base/test/thread_test_helper.h"
29 #include "base/threading/thread.h"
30 #include "chrome/common/chrome_constants.h"
31 #include "content/public/test/test_browser_thread.h"
32 #include "net/base/net_util.h"
33 #include "testing/gtest/include/gtest/gtest.h"
35 using content::BrowserThread;
37 namespace {
39 class ProcessSingletonPosixTest : public testing::Test {
40 public:
41 // A ProcessSingleton exposing some protected methods for testing.
42 class TestableProcessSingleton : public ProcessSingleton {
43 public:
44 explicit TestableProcessSingleton(const base::FilePath& user_data_dir)
45 : ProcessSingleton(
46 user_data_dir,
47 base::Bind(&TestableProcessSingleton::NotificationCallback,
48 base::Unretained(this))) {}
50 std::vector<base::CommandLine::StringVector> callback_command_lines_;
52 using ProcessSingleton::NotifyOtherProcessWithTimeout;
53 using ProcessSingleton::NotifyOtherProcessWithTimeoutOrCreate;
54 using ProcessSingleton::OverrideCurrentPidForTesting;
55 using ProcessSingleton::OverrideKillCallbackForTesting;
57 private:
58 bool NotificationCallback(const base::CommandLine& command_line,
59 const base::FilePath& current_directory) {
60 callback_command_lines_.push_back(command_line.argv());
61 return true;
65 ProcessSingletonPosixTest()
66 : kill_callbacks_(0),
67 io_thread_(BrowserThread::IO),
68 wait_event_(true, false),
69 signal_event_(true, false),
70 process_singleton_on_thread_(NULL) {
71 io_thread_.StartIOThread();
74 void SetUp() override {
75 testing::Test::SetUp();
77 ProcessSingleton::DisablePromptForTesting();
78 // Put the lock in a temporary directory. Doesn't need to be a
79 // full profile to test this code.
80 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
81 // Use a long directory name to ensure that the socket isn't opened through
82 // the symlink.
83 user_data_path_ = temp_dir_.path().Append(
84 std::string(sizeof(sockaddr_un::sun_path), 'a'));
85 ASSERT_TRUE(CreateDirectory(user_data_path_));
87 lock_path_ = user_data_path_.Append(chrome::kSingletonLockFilename);
88 socket_path_ = user_data_path_.Append(chrome::kSingletonSocketFilename);
89 cookie_path_ = user_data_path_.Append(chrome::kSingletonCookieFilename);
92 void TearDown() override {
93 scoped_refptr<base::ThreadTestHelper> io_helper(new base::ThreadTestHelper(
94 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get()));
95 ASSERT_TRUE(io_helper->Run());
97 // Destruct the ProcessSingleton object before the IO thread so that its
98 // internals are destructed properly.
99 if (process_singleton_on_thread_) {
100 worker_thread_->task_runner()->PostTask(
101 FROM_HERE,
102 base::Bind(&ProcessSingletonPosixTest::DestructProcessSingleton,
103 base::Unretained(this)));
105 scoped_refptr<base::ThreadTestHelper> helper(
106 new base::ThreadTestHelper(worker_thread_->task_runner().get()));
107 ASSERT_TRUE(helper->Run());
110 io_thread_.Stop();
111 testing::Test::TearDown();
114 void CreateProcessSingletonOnThread() {
115 ASSERT_EQ(NULL, worker_thread_.get());
116 worker_thread_.reset(new base::Thread("BlockingThread"));
117 worker_thread_->Start();
119 worker_thread_->task_runner()->PostTask(
120 FROM_HERE,
121 base::Bind(&ProcessSingletonPosixTest::CreateProcessSingletonInternal,
122 base::Unretained(this)));
124 scoped_refptr<base::ThreadTestHelper> helper(
125 new base::ThreadTestHelper(worker_thread_->task_runner().get()));
126 ASSERT_TRUE(helper->Run());
129 TestableProcessSingleton* CreateProcessSingleton() {
130 return new TestableProcessSingleton(user_data_path_);
133 void VerifyFiles() {
134 struct stat statbuf;
135 ASSERT_EQ(0, lstat(lock_path_.value().c_str(), &statbuf));
136 ASSERT_TRUE(S_ISLNK(statbuf.st_mode));
137 char buf[PATH_MAX];
138 ssize_t len = readlink(lock_path_.value().c_str(), buf, PATH_MAX);
139 ASSERT_GT(len, 0);
141 ASSERT_EQ(0, lstat(socket_path_.value().c_str(), &statbuf));
142 ASSERT_TRUE(S_ISLNK(statbuf.st_mode));
144 len = readlink(socket_path_.value().c_str(), buf, PATH_MAX);
145 ASSERT_GT(len, 0);
146 base::FilePath socket_target_path = base::FilePath(std::string(buf, len));
148 ASSERT_EQ(0, lstat(socket_target_path.value().c_str(), &statbuf));
149 ASSERT_TRUE(S_ISSOCK(statbuf.st_mode));
151 len = readlink(cookie_path_.value().c_str(), buf, PATH_MAX);
152 ASSERT_GT(len, 0);
153 std::string cookie(buf, len);
155 base::FilePath remote_cookie_path = socket_target_path.DirName().
156 Append(chrome::kSingletonCookieFilename);
157 len = readlink(remote_cookie_path.value().c_str(), buf, PATH_MAX);
158 ASSERT_GT(len, 0);
159 EXPECT_EQ(cookie, std::string(buf, len));
162 ProcessSingleton::NotifyResult NotifyOtherProcess(bool override_kill) {
163 scoped_ptr<TestableProcessSingleton> process_singleton(
164 CreateProcessSingleton());
165 base::CommandLine command_line(
166 base::CommandLine::ForCurrentProcess()->GetProgram());
167 command_line.AppendArg("about:blank");
168 if (override_kill) {
169 process_singleton->OverrideCurrentPidForTesting(
170 base::GetCurrentProcId() + 1);
171 process_singleton->OverrideKillCallbackForTesting(
172 base::Bind(&ProcessSingletonPosixTest::KillCallback,
173 base::Unretained(this)));
176 return process_singleton->NotifyOtherProcessWithTimeout(
177 command_line, kRetryAttempts, timeout(), true);
180 // A helper method to call ProcessSingleton::NotifyOtherProcessOrCreate().
181 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate(
182 const std::string& url) {
183 scoped_ptr<TestableProcessSingleton> process_singleton(
184 CreateProcessSingleton());
185 base::CommandLine command_line(
186 base::CommandLine::ForCurrentProcess()->GetProgram());
187 command_line.AppendArg(url);
188 return process_singleton->NotifyOtherProcessWithTimeoutOrCreate(
189 command_line, kRetryAttempts, timeout());
192 void CheckNotified() {
193 ASSERT_TRUE(process_singleton_on_thread_ != NULL);
194 ASSERT_EQ(1u, process_singleton_on_thread_->callback_command_lines_.size());
195 bool found = false;
196 for (size_t i = 0;
197 i < process_singleton_on_thread_->callback_command_lines_[0].size();
198 ++i) {
199 if (process_singleton_on_thread_->callback_command_lines_[0][i] ==
200 "about:blank") {
201 found = true;
202 break;
205 ASSERT_TRUE(found);
206 ASSERT_EQ(0, kill_callbacks_);
209 void BlockWorkerThread() {
210 worker_thread_->task_runner()->PostTask(
211 FROM_HERE, base::Bind(&ProcessSingletonPosixTest::BlockThread,
212 base::Unretained(this)));
215 void UnblockWorkerThread() {
216 wait_event_.Signal(); // Unblock the worker thread for shutdown.
217 signal_event_.Wait(); // Ensure thread unblocks before continuing.
220 void BlockThread() {
221 wait_event_.Wait();
222 signal_event_.Signal();
225 base::FilePath user_data_path_;
226 base::FilePath lock_path_;
227 base::FilePath socket_path_;
228 base::FilePath cookie_path_;
229 int kill_callbacks_;
231 private:
232 static const int kRetryAttempts = 2;
234 base::TimeDelta timeout() const {
235 return TestTimeouts::tiny_timeout() * kRetryAttempts;
238 void CreateProcessSingletonInternal() {
239 ASSERT_TRUE(!process_singleton_on_thread_);
240 process_singleton_on_thread_ = CreateProcessSingleton();
241 ASSERT_EQ(ProcessSingleton::PROCESS_NONE,
242 process_singleton_on_thread_->NotifyOtherProcessOrCreate());
245 void DestructProcessSingleton() {
246 ASSERT_TRUE(process_singleton_on_thread_);
247 delete process_singleton_on_thread_;
250 void KillCallback(int pid) {
251 kill_callbacks_++;
254 base::MessageLoop message_loop_;
255 content::TestBrowserThread io_thread_;
256 base::ScopedTempDir temp_dir_;
257 base::WaitableEvent wait_event_;
258 base::WaitableEvent signal_event_;
260 scoped_ptr<base::Thread> worker_thread_;
261 TestableProcessSingleton* process_singleton_on_thread_;
264 } // namespace
266 // Test if the socket file and symbol link created by ProcessSingletonPosix
267 // are valid.
268 // If this test flakes, use http://crbug.com/74554.
269 TEST_F(ProcessSingletonPosixTest, CheckSocketFile) {
270 CreateProcessSingletonOnThread();
271 VerifyFiles();
274 // TODO(james.su@gmail.com): port following tests to Windows.
275 // Test success case of NotifyOtherProcess().
276 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessSuccess) {
277 CreateProcessSingletonOnThread();
278 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, NotifyOtherProcess(true));
279 CheckNotified();
282 // Test failure case of NotifyOtherProcess().
283 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessFailure) {
284 CreateProcessSingletonOnThread();
286 BlockWorkerThread();
287 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcess(true));
288 ASSERT_EQ(1, kill_callbacks_);
289 UnblockWorkerThread();
292 // Test that we don't kill ourselves by accident if a lockfile with the same pid
293 // happens to exist.
294 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessNoSuicide) {
295 CreateProcessSingletonOnThread();
296 // Replace lockfile with one containing our own pid.
297 EXPECT_EQ(0, unlink(lock_path_.value().c_str()));
298 std::string symlink_content = base::StringPrintf(
299 "%s%c%u",
300 net::GetHostName().c_str(),
301 '-',
302 base::GetCurrentProcId());
303 EXPECT_EQ(0, symlink(symlink_content.c_str(), lock_path_.value().c_str()));
305 // Remove socket so that we will not be able to notify the existing browser.
306 EXPECT_EQ(0, unlink(socket_path_.value().c_str()));
308 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, NotifyOtherProcess(false));
309 // If we've gotten to this point without killing ourself, the test succeeded.
312 // Test that we can still notify a process on the same host even after the
313 // hostname changed.
314 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessHostChanged) {
315 CreateProcessSingletonOnThread();
316 EXPECT_EQ(0, unlink(lock_path_.value().c_str()));
317 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str()));
319 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, NotifyOtherProcess(false));
320 CheckNotified();
323 // Test that we fail when lock says process is on another host and we can't
324 // notify it over the socket.
325 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessDifferingHost) {
326 CreateProcessSingletonOnThread();
328 BlockWorkerThread();
330 EXPECT_EQ(0, unlink(lock_path_.value().c_str()));
331 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str()));
333 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, NotifyOtherProcess(false));
335 ASSERT_EQ(0, unlink(lock_path_.value().c_str()));
337 UnblockWorkerThread();
340 // Test that we fail when lock says process is on another host and we can't
341 // notify it over the socket.
342 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessOrCreate_DifferingHost) {
343 CreateProcessSingletonOnThread();
345 BlockWorkerThread();
347 EXPECT_EQ(0, unlink(lock_path_.value().c_str()));
348 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str()));
350 std::string url("about:blank");
351 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, NotifyOtherProcessOrCreate(url));
353 ASSERT_EQ(0, unlink(lock_path_.value().c_str()));
355 UnblockWorkerThread();
358 // Test that Create fails when another browser is using the profile directory.
359 TEST_F(ProcessSingletonPosixTest, CreateFailsWithExistingBrowser) {
360 CreateProcessSingletonOnThread();
362 scoped_ptr<TestableProcessSingleton> process_singleton(
363 CreateProcessSingleton());
364 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1);
365 EXPECT_FALSE(process_singleton->Create());
368 // Test that Create fails when another browser is using the profile directory
369 // but with the old socket location.
370 TEST_F(ProcessSingletonPosixTest, CreateChecksCompatibilitySocket) {
371 CreateProcessSingletonOnThread();
372 scoped_ptr<TestableProcessSingleton> process_singleton(
373 CreateProcessSingleton());
374 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1);
376 // Do some surgery so as to look like the old configuration.
377 char buf[PATH_MAX];
378 ssize_t len = readlink(socket_path_.value().c_str(), buf, sizeof(buf));
379 ASSERT_GT(len, 0);
380 base::FilePath socket_target_path = base::FilePath(std::string(buf, len));
381 ASSERT_EQ(0, unlink(socket_path_.value().c_str()));
382 ASSERT_EQ(0, rename(socket_target_path.value().c_str(),
383 socket_path_.value().c_str()));
384 ASSERT_EQ(0, unlink(cookie_path_.value().c_str()));
386 EXPECT_FALSE(process_singleton->Create());
389 // Test that we fail when lock says process is on another host and we can't
390 // notify it over the socket before of a bad cookie.
391 TEST_F(ProcessSingletonPosixTest, NotifyOtherProcessOrCreate_BadCookie) {
392 CreateProcessSingletonOnThread();
393 // Change the cookie.
394 EXPECT_EQ(0, unlink(cookie_path_.value().c_str()));
395 EXPECT_EQ(0, symlink("INCORRECTCOOKIE", cookie_path_.value().c_str()));
397 // Also change the hostname, so the remote does not retry.
398 EXPECT_EQ(0, unlink(lock_path_.value().c_str()));
399 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str()));
401 std::string url("about:blank");
402 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, NotifyOtherProcessOrCreate(url));
405 #if defined(OS_MACOSX)
406 // Test that if there is an existing lock file, and we could not flock()
407 // it, then exit.
408 TEST_F(ProcessSingletonPosixTest, CreateRespectsOldMacLock) {
409 scoped_ptr<TestableProcessSingleton> process_singleton(
410 CreateProcessSingleton());
411 base::ScopedFD lock_fd(HANDLE_EINTR(
412 open(lock_path_.value().c_str(), O_RDWR | O_CREAT | O_EXLOCK, 0644)));
413 ASSERT_TRUE(lock_fd.is_valid());
414 EXPECT_FALSE(process_singleton->Create());
415 base::File::Info info;
416 EXPECT_TRUE(base::GetFileInfo(lock_path_, &info));
417 EXPECT_FALSE(info.is_directory);
418 EXPECT_FALSE(info.is_symbolic_link);
421 // Test that if there is an existing lock file, and it's not locked, we replace
422 // it.
423 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) {
424 scoped_ptr<TestableProcessSingleton> process_singleton(
425 CreateProcessSingleton());
426 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0));
427 EXPECT_TRUE(process_singleton->Create());
428 VerifyFiles();
430 #endif // defined(OS_MACOSX)