process_singleton_linux_uitest.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
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 3// found in the LICENSE file. 4 5#include "chrome/browser/process_singleton.h" 6 7#include <sys/types.h> 8#include <sys/wait.h> 9#include <signal.h> 10#include <unistd.h> 11#include <vector> 12#include <string> 13 14#include "base/eintr_wrapper.h" 15#include "base/path_service.h" 16#include "base/string_util.h" 17#include "base/thread.h" 18#include "base/utf_string_conversions.h" 19#include "chrome/browser/ui/browser.h" 20#include "chrome/common/chrome_constants.h" 21#include "chrome/common/chrome_paths.h" 22#include "chrome/common/chrome_switches.h" 23#include "chrome/test/chrome_process_util.h" 24#include "chrome/test/ui/ui_test.h" 25#include "net/base/net_util.h" 26#include "testing/gtest/include/gtest/gtest.h" 27 28namespace { 29 30class ProcessSingletonLinuxTest : public UITest { 31 public: 32 virtual void SetUp() { 33 UITest::SetUp(); 34 lock_path_ = user_data_dir().Append(chrome::kSingletonLockFilename); 35 socket_path_ = user_data_dir().Append(chrome::kSingletonSocketFilename); 36 cookie_path_ = user_data_dir().Append(chrome::kSingletonCookieFilename); 37 } 38 39 virtual void TearDown() { 40 UITest::TearDown(); 41 42 // Check that the test cleaned up after itself. 43 struct stat statbuf; 44 bool lock_exists = lstat(lock_path_.value().c_str(), &statbuf) == 0; 45 EXPECT_FALSE(lock_exists); 46 47 if (lock_exists) { 48 // Unlink to prevent failing future tests if the lock still exists. 49 EXPECT_EQ(unlink(lock_path_.value().c_str()), 0); 50 } 51 } 52 53 FilePath lock_path_; 54 FilePath socket_path_; 55 FilePath cookie_path_; 56}; 57 58ProcessSingleton* CreateProcessSingleton() { 59 FilePath user_data_dir; 60 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); 61 62 return new ProcessSingleton(user_data_dir); 63} 64 65CommandLine CommandLineForUrl(const std::string& url) { 66 // Hack: mutate the current process's command line so we don't show a dialog. 67 // Note that this only works if we have no loose values on the command line, 68 // but that's fine for unit tests. In a UI test we disable error dialogs 69 // when spawning Chrome, but this test hits the ProcessSingleton directly. 70 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); 71 if (!cmd_line->HasSwitch(switches::kNoProcessSingletonDialog)) 72 cmd_line->AppendSwitch(switches::kNoProcessSingletonDialog); 73 74 CommandLine new_cmd_line(*cmd_line); 75 new_cmd_line.AppendArg(url); 76 return new_cmd_line; 77} 78 79// A helper method to call ProcessSingleton::NotifyOtherProcess(). 80// |url| will be added to CommandLine for current process, so that it can be 81// sent to browser process by ProcessSingleton::NotifyOtherProcess(). 82ProcessSingleton::NotifyResult NotifyOtherProcess(const std::string& url, 83 int timeout_ms) { 84 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); 85 return process_singleton->NotifyOtherProcessWithTimeout( 86 CommandLineForUrl(url), timeout_ms / 1000, true); 87} 88 89// A helper method to call ProcessSingleton::NotifyOtherProcessOrCreate(). 90// |url| will be added to CommandLine for current process, so that it can be 91// sent to browser process by ProcessSingleton::NotifyOtherProcessOrCreate(). 92ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate( 93 const std::string& url, 94 int timeout_ms) { 95 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); 96 return process_singleton->NotifyOtherProcessWithTimeoutOrCreate( 97 CommandLineForUrl(url), timeout_ms / 1000); 98} 99 100} // namespace 101 102// Test if the socket file and symbol link created by ProcessSingletonLinux 103// are valid. When running this test, the ProcessSingleton object is already 104// initiated by UITest. So we just test against this existing object. 105TEST_F(ProcessSingletonLinuxTest, CheckSocketFile) { 106 struct stat statbuf; 107 ASSERT_EQ(0, lstat(lock_path_.value().c_str(), &statbuf)); 108 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); 109 char buf[PATH_MAX]; 110 ssize_t len = readlink(lock_path_.value().c_str(), buf, PATH_MAX); 111 ASSERT_GT(len, 0); 112 113 ASSERT_EQ(0, lstat(socket_path_.value().c_str(), &statbuf)); 114 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); 115 116 len = readlink(socket_path_.value().c_str(), buf, PATH_MAX); 117 ASSERT_GT(len, 0); 118 FilePath socket_target_path = FilePath(std::string(buf, len)); 119 120 ASSERT_EQ(0, lstat(socket_target_path.value().c_str(), &statbuf)); 121 ASSERT_TRUE(S_ISSOCK(statbuf.st_mode)); 122 123 len = readlink(cookie_path_.value().c_str(), buf, PATH_MAX); 124 ASSERT_GT(len, 0); 125 std::string cookie(buf, len); 126 127 FilePath remote_cookie_path = socket_target_path.DirName(). 128 Append(chrome::kSingletonCookieFilename); 129 len = readlink(remote_cookie_path.value().c_str(), buf, PATH_MAX); 130 ASSERT_GT(len, 0); 131 EXPECT_EQ(cookie, std::string(buf, len)); 132} 133 134#if defined(OS_LINUX) && defined(TOOLKIT_VIEWS) 135// The following tests in linux/view does not pass without a window manager, 136// which is true in build/try bots. 137// See http://crbug.com/30953. 138#define NotifyOtherProcessSuccess FAILS_NotifyOtherProcessSuccess 139#define NotifyOtherProcessHostChanged FAILS_NotifyOtherProcessHostChanged 140#endif 141 142// TODO(james.su@gmail.com): port following tests to Windows. 143// Test success case of NotifyOtherProcess(). 144TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessSuccess) { 145 std::string url("about:blank"); 146 int original_tab_count = GetTabCount(); 147 148 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, 149 NotifyOtherProcess(url, action_timeout_ms())); 150 EXPECT_EQ(original_tab_count + 1, GetTabCount()); 151 EXPECT_EQ(url, GetActiveTabURL().spec()); 152} 153 154// Test failure case of NotifyOtherProcess(). 155TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessFailure) { 156 base::ProcessId pid = browser_process_id(); 157 158 ASSERT_GE(pid, 1); 159 160 // Block the browser process, then it'll be killed by 161 // ProcessSingleton::NotifyOtherProcess(). 162 kill(pid, SIGSTOP); 163 164 // Wait to make sure the browser process is actually stopped. 165 // It's necessary when running with valgrind. 166 EXPECT_GE(HANDLE_EINTR(waitpid(pid, 0, WUNTRACED)), 0); 167 168 std::string url("about:blank"); 169 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, 170 NotifyOtherProcess(url, action_timeout_ms())); 171 172 // Wait for a while to make sure the browser process is actually killed. 173 EXPECT_FALSE(CrashAwareSleep(sleep_timeout_ms())); 174} 175 176// Test that we don't kill ourselves by accident if a lockfile with the same pid 177// happens to exist. 178// TODO(mattm): This doesn't really need to be a uitest. (We don't use the 179// uitest created browser process, but we do use some uitest provided stuff like 180// the user_data_dir and the NotifyOtherProcess function in this file, which 181// would have to be duplicated or shared if this test was moved into a 182// unittest.) 183TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessNoSuicide) { 184 // Replace lockfile with one containing our own pid. 185 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); 186 std::string symlink_content = StringPrintf( 187 "%s%c%u", 188 net::GetHostName().c_str(), 189 '-', 190 base::GetCurrentProcId()); 191 EXPECT_EQ(0, symlink(symlink_content.c_str(), lock_path_.value().c_str())); 192 193 // Remove socket so that we will not be able to notify the existing browser. 194 EXPECT_EQ(0, unlink(socket_path_.value().c_str())); 195 196 std::string url("about:blank"); 197 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, 198 NotifyOtherProcess(url, action_timeout_ms())); 199 // If we've gotten to this point without killing ourself, the test succeeded. 200} 201 202// Test that we can still notify a process on the same host even after the 203// hostname changed. 204TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessHostChanged) { 205 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); 206 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); 207 208 int original_tab_count = GetTabCount(); 209 210 std::string url("about:blank"); 211 EXPECT_EQ(ProcessSingleton::PROCESS_NOTIFIED, 212 NotifyOtherProcess(url, action_timeout_ms())); 213 EXPECT_EQ(original_tab_count + 1, GetTabCount()); 214 EXPECT_EQ(url, GetActiveTabURL().spec()); 215} 216 217// Test that we fail when lock says process is on another host and we can't 218// notify it over the socket. 219TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessDifferingHost) { 220 base::ProcessId pid = browser_process_id(); 221 222 ASSERT_GE(pid, 1); 223 224 // Kill the browser process, so that it does not respond on the socket. 225 kill(pid, SIGKILL); 226 // Wait for a while to make sure the browser process is actually killed. 227 EXPECT_FALSE(CrashAwareSleep(sleep_timeout_ms())); 228 229 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); 230 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); 231 232 std::string url("about:blank"); 233 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, 234 NotifyOtherProcess(url, action_timeout_ms())); 235 236 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); 237} 238 239// Test that we fail when lock says process is on another host and we can't 240// notify it over the socket. 241TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessOrCreate_DifferingHost) { 242 base::ProcessId pid = browser_process_id(); 243 244 ASSERT_GE(pid, 1); 245 246 // Kill the browser process, so that it does not respond on the socket. 247 kill(pid, SIGKILL); 248 // Wait for a while to make sure the browser process is actually killed. 249 EXPECT_FALSE(CrashAwareSleep(sleep_timeout_ms())); 250 251 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); 252 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); 253 254 std::string url("about:blank"); 255 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, 256 NotifyOtherProcessOrCreate(url, action_timeout_ms())); 257 258 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); 259} 260 261// Test that Create fails when another browser is using the profile directory. 262TEST_F(ProcessSingletonLinuxTest, CreateFailsWithExistingBrowser) { 263 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); 264 EXPECT_FALSE(process_singleton->Create()); 265} 266 267// Test that Create fails when another browser is using the profile directory 268// but with the old socket location. 269TEST_F(ProcessSingletonLinuxTest, CreateChecksCompatibilitySocket) { 270 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); 271 272 // Do some surgery so as to look like the old configuration. 273 char buf[PATH_MAX]; 274 ssize_t len = readlink(socket_path_.value().c_str(), buf, sizeof(buf)); 275 ASSERT_GT(len, 0); 276 FilePath socket_target_path = FilePath(std::string(buf, len)); 277 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); 278 ASSERT_EQ(0, rename(socket_target_path.value().c_str(), 279 socket_path_.value().c_str())); 280 ASSERT_EQ(0, unlink(cookie_path_.value().c_str())); 281 282 EXPECT_FALSE(process_singleton->Create()); 283} 284 285// Test that we fail when lock says process is on another host and we can't 286// notify it over the socket before of a bad cookie. 287TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessOrCreate_BadCookie) { 288 // Change the cookie. 289 EXPECT_EQ(0, unlink(cookie_path_.value().c_str())); 290 EXPECT_EQ(0, symlink("INCORRECTCOOKIE", cookie_path_.value().c_str())); 291 292 // Also change the hostname, so the remote does not retry. 293 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); 294 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); 295 296 std::string url("about:blank"); 297 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, 298 NotifyOtherProcessOrCreate(url, action_timeout_ms())); 299} 300