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
3 // found in the LICENSE file.
5 #include "chrome/common/service_process_util.h"
7 #include "base/basictypes.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/process/kill.h"
12 #include "base/process/launch.h"
13 #include "base/strings/string_split.h"
15 #if !defined(OS_MACOSX)
16 #include "base/at_exit.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/test/multiprocess_test.h"
21 #include "base/test/test_timeouts.h"
22 #include "base/threading/thread.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/chrome_version_info.h"
25 #include "testing/multiprocess_func_list.h"
28 #include "base/win/win_util.h"
32 #include "chrome/common/auto_start_linux.h"
36 // This test fails http://crbug.com/84854, and is very flaky on CrOS and
37 // somewhat flaky on other Linux.
38 #define MAYBE_ForceShutdown DISABLED_ForceShutdown
40 #if defined(OS_LINUX) || defined(OS_WIN)
41 #define MAYBE_ForceShutdown DISABLED_ForceShutdown
43 #define MAYBE_ForceShutdown ForceShutdown
49 bool g_good_shutdown
= false;
51 void ShutdownTask(base::MessageLoop
* loop
) {
52 // Quit the main message loop.
53 ASSERT_FALSE(g_good_shutdown
);
54 g_good_shutdown
= true;
55 loop
->PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
60 TEST(ServiceProcessUtilTest
, ScopedVersionedName
) {
61 std::string test_str
= "test";
62 std::string scoped_name
= GetServiceProcessScopedVersionedName(test_str
);
63 chrome::VersionInfo version_info
;
64 DCHECK(version_info
.is_valid());
65 EXPECT_TRUE(EndsWith(scoped_name
, test_str
, true));
66 EXPECT_NE(std::string::npos
, scoped_name
.find(version_info
.Version()));
69 class ServiceProcessStateTest
: public base::MultiProcessTest
{
71 ServiceProcessStateTest();
72 virtual ~ServiceProcessStateTest();
74 base::MessageLoopProxy
* IOMessageLoopProxy() {
75 return io_thread_
.message_loop_proxy().get();
77 void LaunchAndWait(const std::string
& name
);
80 // This is used to release the ServiceProcessState singleton after each test.
81 base::ShadowingAtExitManager at_exit_manager_
;
82 base::Thread io_thread_
;
85 ServiceProcessStateTest::ServiceProcessStateTest()
86 : io_thread_("ServiceProcessStateTestThread") {
89 ServiceProcessStateTest::~ServiceProcessStateTest() {
92 void ServiceProcessStateTest::SetUp() {
93 base::Thread::Options
options(base::MessageLoop::TYPE_IO
, 0);
94 ASSERT_TRUE(io_thread_
.StartWithOptions(options
));
97 void ServiceProcessStateTest::LaunchAndWait(const std::string
& name
) {
98 base::ProcessHandle handle
= SpawnChild(name
);
101 ASSERT_TRUE(base::WaitForExitCode(handle
, &exit_code
));
102 ASSERT_EQ(exit_code
, 0);
105 TEST_F(ServiceProcessStateTest
, Singleton
) {
106 ServiceProcessState state
;
107 ASSERT_TRUE(state
.Initialize());
108 LaunchAndWait("ServiceProcessStateTestSingleton");
111 // http://crbug.com/396390
112 TEST_F(ServiceProcessStateTest
, DISABLED_ReadyState
) {
113 ASSERT_FALSE(CheckServiceProcessReady());
114 ServiceProcessState state
;
115 ASSERT_TRUE(state
.Initialize());
116 ASSERT_TRUE(state
.SignalReady(IOMessageLoopProxy(), base::Closure()));
117 LaunchAndWait("ServiceProcessStateTestReadyTrue");
118 state
.SignalStopped();
119 LaunchAndWait("ServiceProcessStateTestReadyFalse");
122 TEST_F(ServiceProcessStateTest
, AutoRun
) {
123 ServiceProcessState state
;
124 ASSERT_TRUE(state
.AddToAutoRun());
125 scoped_ptr
<CommandLine
> autorun_command_line
;
127 std::string value_name
= GetServiceProcessScopedName("_service_run");
128 base::string16 value
;
129 EXPECT_TRUE(base::win::ReadCommandFromAutoRun(HKEY_CURRENT_USER
,
130 base::UTF8ToWide(value_name
),
132 autorun_command_line
.reset(new CommandLine(CommandLine::FromString(value
)));
133 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
134 #if defined(GOOGLE_CHROME_BUILD)
135 std::string base_desktop_name
= "google-chrome-service.desktop";
136 #else // CHROMIUM_BUILD
137 std::string base_desktop_name
= "chromium-service.desktop";
139 std::string exec_value
;
140 EXPECT_TRUE(AutoStart::GetAutostartFileValue(
141 GetServiceProcessScopedName(base_desktop_name
), "Exec", &exec_value
));
143 // Make sure |exec_value| doesn't contain strings a shell would
145 ASSERT_EQ(std::string::npos
, exec_value
.find('#'));
146 ASSERT_EQ(std::string::npos
, exec_value
.find('\n'));
147 ASSERT_EQ(std::string::npos
, exec_value
.find('"'));
148 ASSERT_EQ(std::string::npos
, exec_value
.find('\''));
150 CommandLine::StringVector argv
;
151 base::SplitString(exec_value
, ' ', &argv
);
152 ASSERT_GE(argv
.size(), 2U)
153 << "Expected at least one command-line option in: " << exec_value
;
154 autorun_command_line
.reset(new CommandLine(argv
));
155 #endif // defined(OS_WIN)
156 if (autorun_command_line
.get()) {
157 EXPECT_EQ(autorun_command_line
->GetSwitchValueASCII(switches::kProcessType
),
158 std::string(switches::kServiceProcess
));
160 ASSERT_TRUE(state
.RemoveFromAutoRun());
162 EXPECT_FALSE(base::win::ReadCommandFromAutoRun(HKEY_CURRENT_USER
,
163 base::UTF8ToWide(value_name
),
165 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
166 EXPECT_FALSE(AutoStart::GetAutostartFileValue(
167 GetServiceProcessScopedName(base_desktop_name
), "Exec", &exec_value
));
168 #endif // defined(OS_WIN)
171 // http://crbug.com/396390
172 TEST_F(ServiceProcessStateTest
, DISABLED_SharedMem
) {
176 // On Posix, named shared memory uses a file on disk. This file
177 // could be lying around from previous crashes which could cause
178 // GetServiceProcessPid to lie. On Windows, we use a named event so we
179 // don't have this issue. Until we have a more stable shared memory
180 // implementation on Posix, this check will only execute on Windows.
181 ASSERT_FALSE(GetServiceProcessData(&version
, &pid
));
182 #endif // defined(OS_WIN)
183 ServiceProcessState state
;
184 ASSERT_TRUE(state
.Initialize());
185 ASSERT_TRUE(GetServiceProcessData(&version
, &pid
));
186 ASSERT_EQ(base::GetCurrentProcId(), pid
);
189 TEST_F(ServiceProcessStateTest
, MAYBE_ForceShutdown
) {
190 base::ProcessHandle handle
= SpawnChild("ServiceProcessStateTestShutdown");
192 for (int i
= 0; !CheckServiceProcessReady() && i
< 10; ++i
) {
193 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
195 ASSERT_TRUE(CheckServiceProcessReady());
198 ASSERT_TRUE(GetServiceProcessData(&version
, &pid
));
199 ASSERT_TRUE(ForceServiceProcessShutdown(version
, pid
));
201 ASSERT_TRUE(base::WaitForExitCodeWithTimeout(handle
,
202 &exit_code
, TestTimeouts::action_max_timeout()));
203 base::CloseProcessHandle(handle
);
204 ASSERT_EQ(exit_code
, 0);
207 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestSingleton
) {
208 ServiceProcessState state
;
209 EXPECT_FALSE(state
.Initialize());
213 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestReadyTrue
) {
214 EXPECT_TRUE(CheckServiceProcessReady());
218 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestReadyFalse
) {
219 EXPECT_FALSE(CheckServiceProcessReady());
223 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestShutdown
) {
224 base::MessageLoop message_loop
;
225 message_loop
.set_thread_name("ServiceProcessStateTestShutdownMainThread");
226 base::Thread
io_thread_("ServiceProcessStateTestShutdownIOThread");
227 base::Thread::Options
options(base::MessageLoop::TYPE_IO
, 0);
228 EXPECT_TRUE(io_thread_
.StartWithOptions(options
));
229 ServiceProcessState state
;
230 EXPECT_TRUE(state
.Initialize());
231 EXPECT_TRUE(state
.SignalReady(
232 io_thread_
.message_loop_proxy().get(),
233 base::Bind(&ShutdownTask
, base::MessageLoop::current())));
234 message_loop
.PostDelayedTask(FROM_HERE
,
235 base::MessageLoop::QuitClosure(),
236 TestTimeouts::action_max_timeout());
237 EXPECT_FALSE(g_good_shutdown
);
239 EXPECT_TRUE(g_good_shutdown
);
245 #include <CoreFoundation/CoreFoundation.h>
247 #include "base/files/file_path.h"
248 #include "base/files/file_util.h"
249 #include "base/files/scoped_temp_dir.h"
250 #include "base/mac/mac_util.h"
251 #include "base/test/test_timeouts.h"
252 #include "base/threading/thread.h"
253 #include "chrome/common/mac/launchd.h"
254 #include "chrome/common/mac/mock_launchd.h"
255 #include "testing/gtest/include/gtest/gtest.h"
257 class ServiceProcessStateFileManipulationTest
: public ::testing::Test
{
259 ServiceProcessStateFileManipulationTest()
260 : io_thread_("ServiceProcessStateFileManipulationTest_IO") {
262 virtual ~ServiceProcessStateFileManipulationTest() { }
264 virtual void SetUp() {
265 base::Thread::Options options
;
266 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
267 ASSERT_TRUE(io_thread_
.StartWithOptions(options
));
268 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
269 ASSERT_TRUE(MockLaunchd::MakeABundle(GetTempDirPath(),
273 mock_launchd_
.reset(new MockLaunchd(executable_path_
, &loop_
,
275 scoped_launchd_instance_
.reset(
276 new Launchd::ScopedInstance(mock_launchd_
.get()));
277 ASSERT_TRUE(service_process_state_
.Initialize());
278 ASSERT_TRUE(service_process_state_
.SignalReady(
279 io_thread_
.message_loop_proxy().get(), base::Closure()));
280 loop_
.PostDelayedTask(FROM_HERE
,
281 base::MessageLoop::QuitClosure(),
282 TestTimeouts::action_max_timeout());
285 const MockLaunchd
* mock_launchd() const { return mock_launchd_
.get(); }
286 const base::FilePath
& executable_path() const { return executable_path_
; }
287 const base::FilePath
& bundle_path() const { return bundle_path_
; }
288 const base::FilePath
& GetTempDirPath() const { return temp_dir_
.path(); }
290 base::MessageLoopProxy
* GetIOMessageLoopProxy() {
291 return io_thread_
.message_loop_proxy().get();
293 void Run() { loop_
.Run(); }
296 base::ScopedTempDir temp_dir_
;
297 base::MessageLoopForUI loop_
;
298 base::Thread io_thread_
;
299 base::FilePath executable_path_
, bundle_path_
;
300 scoped_ptr
<MockLaunchd
> mock_launchd_
;
301 scoped_ptr
<Launchd::ScopedInstance
> scoped_launchd_instance_
;
302 ServiceProcessState service_process_state_
;
305 void DeleteFunc(const base::FilePath
& file
) {
306 EXPECT_TRUE(base::DeleteFile(file
, true));
309 void MoveFunc(const base::FilePath
& from
, const base::FilePath
& to
) {
310 EXPECT_TRUE(base::Move(from
, to
));
313 void ChangeAttr(const base::FilePath
& from
, int mode
) {
314 EXPECT_EQ(chmod(from
.value().c_str(), mode
), 0);
317 class ScopedAttributesRestorer
{
319 ScopedAttributesRestorer(const base::FilePath
& path
, int mode
)
320 : path_(path
), mode_(mode
) {
322 ~ScopedAttributesRestorer() {
323 ChangeAttr(path_
, mode_
);
326 base::FilePath path_
;
330 void TrashFunc(const base::FilePath
& src
) {
333 EXPECT_TRUE(base::mac::FSRefFromPath(src
.value(), &path_ref
));
334 OSStatus status
= FSMoveObjectToTrashSync(&path_ref
,
336 kFSFileOperationDefaultOptions
);
337 EXPECT_EQ(status
, noErr
) << "FSMoveObjectToTrashSync " << status
;
340 TEST_F(ServiceProcessStateFileManipulationTest
, VerifyLaunchD
) {
341 // There have been problems where launchd has gotten into a bad state, usually
342 // because something had deleted all the files in /tmp. launchd depends on
343 // a Unix Domain Socket that it creates at /tmp/launchd*/sock.
344 // The symptom of this problem is that the service process connect fails
345 // on Mac and "launch_msg(): Socket is not connected" appears.
346 // This test is designed to make sure that launchd is working.
347 // http://crbug/75518
349 CommandLine
cl(base::FilePath("/bin/launchctl"));
350 cl
.AppendArg("list");
351 cl
.AppendArg("com.apple.launchctl.Aqua");
355 ASSERT_TRUE(base::GetAppOutputWithExitCode(cl
, &output
, &exit_code
)
357 << " exit_code:" << exit_code
<< " " << output
;
360 TEST_F(ServiceProcessStateFileManipulationTest
, DeleteFile
) {
361 GetIOMessageLoopProxy()->PostTask(
363 base::Bind(&DeleteFunc
, executable_path()));
365 ASSERT_TRUE(mock_launchd()->remove_called());
366 ASSERT_TRUE(mock_launchd()->delete_called());
369 TEST_F(ServiceProcessStateFileManipulationTest
, DeleteBundle
) {
370 GetIOMessageLoopProxy()->PostTask(
372 base::Bind(&DeleteFunc
, bundle_path()));
374 ASSERT_TRUE(mock_launchd()->remove_called());
375 ASSERT_TRUE(mock_launchd()->delete_called());
378 TEST_F(ServiceProcessStateFileManipulationTest
, MoveBundle
) {
379 base::FilePath new_loc
= GetTempDirPath().AppendASCII("MoveBundle");
380 GetIOMessageLoopProxy()->PostTask(
382 base::Bind(&MoveFunc
, bundle_path(), new_loc
));
384 ASSERT_TRUE(mock_launchd()->restart_called());
385 ASSERT_TRUE(mock_launchd()->write_called());
388 TEST_F(ServiceProcessStateFileManipulationTest
, MoveFile
) {
389 base::FilePath new_loc
= GetTempDirPath().AppendASCII("MoveFile");
390 GetIOMessageLoopProxy()->PostTask(
392 base::Bind(&MoveFunc
, executable_path(), new_loc
));
394 ASSERT_TRUE(mock_launchd()->remove_called());
395 ASSERT_TRUE(mock_launchd()->delete_called());
398 TEST_F(ServiceProcessStateFileManipulationTest
, TrashBundle
) {
400 ASSERT_TRUE(base::mac::FSRefFromPath(bundle_path().value(), &bundle_ref
));
401 GetIOMessageLoopProxy()->PostTask(
403 base::Bind(&TrashFunc
, bundle_path()));
405 ASSERT_TRUE(mock_launchd()->remove_called());
406 ASSERT_TRUE(mock_launchd()->delete_called());
407 std::string
path(base::mac::PathFromFSRef(bundle_ref
));
408 base::FilePath
file_path(path
);
409 ASSERT_TRUE(base::DeleteFile(file_path
, true));
412 TEST_F(ServiceProcessStateFileManipulationTest
, ChangeAttr
) {
413 ScopedAttributesRestorer
restorer(bundle_path(), 0777);
414 GetIOMessageLoopProxy()->PostTask(
416 base::Bind(&ChangeAttr
, bundle_path(), 0222));
418 ASSERT_TRUE(mock_launchd()->remove_called());
419 ASSERT_TRUE(mock_launchd()->delete_called());