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 EXPECT_TRUE(EndsWith(scoped_name
, test_str
, true));
65 EXPECT_NE(std::string::npos
, scoped_name
.find(version_info
.Version()));
68 class ServiceProcessStateTest
: public base::MultiProcessTest
{
70 ServiceProcessStateTest();
71 ~ServiceProcessStateTest() override
;
72 void SetUp() override
;
73 base::MessageLoopProxy
* IOMessageLoopProxy() {
74 return io_thread_
.message_loop_proxy().get();
76 void LaunchAndWait(const std::string
& name
);
79 // This is used to release the ServiceProcessState singleton after each test.
80 base::ShadowingAtExitManager at_exit_manager_
;
81 base::Thread io_thread_
;
84 ServiceProcessStateTest::ServiceProcessStateTest()
85 : io_thread_("ServiceProcessStateTestThread") {
88 ServiceProcessStateTest::~ServiceProcessStateTest() {
91 void ServiceProcessStateTest::SetUp() {
92 base::Thread::Options
options(base::MessageLoop::TYPE_IO
, 0);
93 ASSERT_TRUE(io_thread_
.StartWithOptions(options
));
96 void ServiceProcessStateTest::LaunchAndWait(const std::string
& name
) {
97 base::ProcessHandle handle
= SpawnChild(name
);
100 ASSERT_TRUE(base::WaitForExitCode(handle
, &exit_code
));
101 ASSERT_EQ(exit_code
, 0);
104 TEST_F(ServiceProcessStateTest
, Singleton
) {
105 ServiceProcessState state
;
106 ASSERT_TRUE(state
.Initialize());
107 LaunchAndWait("ServiceProcessStateTestSingleton");
110 // http://crbug.com/396390
111 TEST_F(ServiceProcessStateTest
, DISABLED_ReadyState
) {
112 ASSERT_FALSE(CheckServiceProcessReady());
113 ServiceProcessState state
;
114 ASSERT_TRUE(state
.Initialize());
115 ASSERT_TRUE(state
.SignalReady(IOMessageLoopProxy(), base::Closure()));
116 LaunchAndWait("ServiceProcessStateTestReadyTrue");
117 state
.SignalStopped();
118 LaunchAndWait("ServiceProcessStateTestReadyFalse");
121 TEST_F(ServiceProcessStateTest
, AutoRun
) {
122 ServiceProcessState state
;
123 ASSERT_TRUE(state
.AddToAutoRun());
124 scoped_ptr
<CommandLine
> autorun_command_line
;
126 std::string value_name
= GetServiceProcessScopedName("_service_run");
127 base::string16 value
;
128 EXPECT_TRUE(base::win::ReadCommandFromAutoRun(HKEY_CURRENT_USER
,
129 base::UTF8ToWide(value_name
),
131 autorun_command_line
.reset(new CommandLine(CommandLine::FromString(value
)));
132 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
133 #if defined(GOOGLE_CHROME_BUILD)
134 std::string base_desktop_name
= "google-chrome-service.desktop";
135 #else // CHROMIUM_BUILD
136 std::string base_desktop_name
= "chromium-service.desktop";
138 std::string exec_value
;
139 EXPECT_TRUE(AutoStart::GetAutostartFileValue(
140 GetServiceProcessScopedName(base_desktop_name
), "Exec", &exec_value
));
142 // Make sure |exec_value| doesn't contain strings a shell would
144 ASSERT_EQ(std::string::npos
, exec_value
.find('#'));
145 ASSERT_EQ(std::string::npos
, exec_value
.find('\n'));
146 ASSERT_EQ(std::string::npos
, exec_value
.find('"'));
147 ASSERT_EQ(std::string::npos
, exec_value
.find('\''));
149 CommandLine::StringVector argv
;
150 base::SplitString(exec_value
, ' ', &argv
);
151 ASSERT_GE(argv
.size(), 2U)
152 << "Expected at least one command-line option in: " << exec_value
;
153 autorun_command_line
.reset(new CommandLine(argv
));
154 #endif // defined(OS_WIN)
155 if (autorun_command_line
.get()) {
156 EXPECT_EQ(autorun_command_line
->GetSwitchValueASCII(switches::kProcessType
),
157 std::string(switches::kServiceProcess
));
159 ASSERT_TRUE(state
.RemoveFromAutoRun());
161 EXPECT_FALSE(base::win::ReadCommandFromAutoRun(HKEY_CURRENT_USER
,
162 base::UTF8ToWide(value_name
),
164 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
165 EXPECT_FALSE(AutoStart::GetAutostartFileValue(
166 GetServiceProcessScopedName(base_desktop_name
), "Exec", &exec_value
));
167 #endif // defined(OS_WIN)
170 // http://crbug.com/396390
171 TEST_F(ServiceProcessStateTest
, DISABLED_SharedMem
) {
175 // On Posix, named shared memory uses a file on disk. This file
176 // could be lying around from previous crashes which could cause
177 // GetServiceProcessPid to lie. On Windows, we use a named event so we
178 // don't have this issue. Until we have a more stable shared memory
179 // implementation on Posix, this check will only execute on Windows.
180 ASSERT_FALSE(GetServiceProcessData(&version
, &pid
));
181 #endif // defined(OS_WIN)
182 ServiceProcessState state
;
183 ASSERT_TRUE(state
.Initialize());
184 ASSERT_TRUE(GetServiceProcessData(&version
, &pid
));
185 ASSERT_EQ(base::GetCurrentProcId(), pid
);
188 TEST_F(ServiceProcessStateTest
, MAYBE_ForceShutdown
) {
189 base::ProcessHandle handle
= SpawnChild("ServiceProcessStateTestShutdown");
191 for (int i
= 0; !CheckServiceProcessReady() && i
< 10; ++i
) {
192 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
194 ASSERT_TRUE(CheckServiceProcessReady());
197 ASSERT_TRUE(GetServiceProcessData(&version
, &pid
));
198 ASSERT_TRUE(ForceServiceProcessShutdown(version
, pid
));
200 ASSERT_TRUE(base::WaitForExitCodeWithTimeout(handle
,
201 &exit_code
, TestTimeouts::action_max_timeout()));
202 base::CloseProcessHandle(handle
);
203 ASSERT_EQ(exit_code
, 0);
206 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestSingleton
) {
207 ServiceProcessState state
;
208 EXPECT_FALSE(state
.Initialize());
212 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestReadyTrue
) {
213 EXPECT_TRUE(CheckServiceProcessReady());
217 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestReadyFalse
) {
218 EXPECT_FALSE(CheckServiceProcessReady());
222 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestShutdown
) {
223 base::MessageLoop message_loop
;
224 message_loop
.set_thread_name("ServiceProcessStateTestShutdownMainThread");
225 base::Thread
io_thread_("ServiceProcessStateTestShutdownIOThread");
226 base::Thread::Options
options(base::MessageLoop::TYPE_IO
, 0);
227 EXPECT_TRUE(io_thread_
.StartWithOptions(options
));
228 ServiceProcessState state
;
229 EXPECT_TRUE(state
.Initialize());
230 EXPECT_TRUE(state
.SignalReady(
231 io_thread_
.message_loop_proxy().get(),
232 base::Bind(&ShutdownTask
, base::MessageLoop::current())));
233 message_loop
.PostDelayedTask(FROM_HERE
,
234 base::MessageLoop::QuitClosure(),
235 TestTimeouts::action_max_timeout());
236 EXPECT_FALSE(g_good_shutdown
);
238 EXPECT_TRUE(g_good_shutdown
);
244 #include <CoreFoundation/CoreFoundation.h>
246 #include "base/files/file_path.h"
247 #include "base/files/file_util.h"
248 #include "base/files/scoped_temp_dir.h"
249 #include "base/mac/mac_util.h"
250 #include "base/test/test_timeouts.h"
251 #include "base/threading/thread.h"
252 #include "chrome/common/mac/launchd.h"
253 #include "chrome/common/mac/mock_launchd.h"
254 #include "testing/gtest/include/gtest/gtest.h"
256 class ServiceProcessStateFileManipulationTest
: public ::testing::Test
{
258 ServiceProcessStateFileManipulationTest()
259 : io_thread_("ServiceProcessStateFileManipulationTest_IO") {
261 virtual ~ServiceProcessStateFileManipulationTest() { }
263 virtual void SetUp() {
264 base::Thread::Options options
;
265 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
266 ASSERT_TRUE(io_thread_
.StartWithOptions(options
));
267 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
268 ASSERT_TRUE(MockLaunchd::MakeABundle(GetTempDirPath(),
272 mock_launchd_
.reset(new MockLaunchd(executable_path_
, &loop_
,
274 scoped_launchd_instance_
.reset(
275 new Launchd::ScopedInstance(mock_launchd_
.get()));
276 ASSERT_TRUE(service_process_state_
.Initialize());
277 ASSERT_TRUE(service_process_state_
.SignalReady(
278 io_thread_
.message_loop_proxy().get(), base::Closure()));
279 loop_
.PostDelayedTask(FROM_HERE
,
280 base::MessageLoop::QuitClosure(),
281 TestTimeouts::action_max_timeout());
284 const MockLaunchd
* mock_launchd() const { return mock_launchd_
.get(); }
285 const base::FilePath
& executable_path() const { return executable_path_
; }
286 const base::FilePath
& bundle_path() const { return bundle_path_
; }
287 const base::FilePath
& GetTempDirPath() const { return temp_dir_
.path(); }
289 base::MessageLoopProxy
* GetIOMessageLoopProxy() {
290 return io_thread_
.message_loop_proxy().get();
292 void Run() { loop_
.Run(); }
295 base::ScopedTempDir temp_dir_
;
296 base::MessageLoopForUI loop_
;
297 base::Thread io_thread_
;
298 base::FilePath executable_path_
, bundle_path_
;
299 scoped_ptr
<MockLaunchd
> mock_launchd_
;
300 scoped_ptr
<Launchd::ScopedInstance
> scoped_launchd_instance_
;
301 ServiceProcessState service_process_state_
;
304 void DeleteFunc(const base::FilePath
& file
) {
305 EXPECT_TRUE(base::DeleteFile(file
, true));
308 void MoveFunc(const base::FilePath
& from
, const base::FilePath
& to
) {
309 EXPECT_TRUE(base::Move(from
, to
));
312 void ChangeAttr(const base::FilePath
& from
, int mode
) {
313 EXPECT_EQ(chmod(from
.value().c_str(), mode
), 0);
316 class ScopedAttributesRestorer
{
318 ScopedAttributesRestorer(const base::FilePath
& path
, int mode
)
319 : path_(path
), mode_(mode
) {
321 ~ScopedAttributesRestorer() {
322 ChangeAttr(path_
, mode_
);
325 base::FilePath path_
;
329 void TrashFunc(const base::FilePath
& src
) {
332 EXPECT_TRUE(base::mac::FSRefFromPath(src
.value(), &path_ref
));
333 OSStatus status
= FSMoveObjectToTrashSync(&path_ref
,
335 kFSFileOperationDefaultOptions
);
336 EXPECT_EQ(status
, noErr
) << "FSMoveObjectToTrashSync " << status
;
339 TEST_F(ServiceProcessStateFileManipulationTest
, VerifyLaunchD
) {
340 // There have been problems where launchd has gotten into a bad state, usually
341 // because something had deleted all the files in /tmp. launchd depends on
342 // a Unix Domain Socket that it creates at /tmp/launchd*/sock.
343 // The symptom of this problem is that the service process connect fails
344 // on Mac and "launch_msg(): Socket is not connected" appears.
345 // This test is designed to make sure that launchd is working.
346 // http://crbug/75518
347 // Note: This particular problem no longer affects launchd in 10.10+, since
348 // there is no user owned launchd process and sockets are no longer made at
349 // /tmp/launchd*/sock. This test is still useful as a sanity check to make
350 // sure that launchd appears to be working.
352 CommandLine
cl(base::FilePath("/bin/launchctl"));
353 cl
.AppendArg("limit");
357 ASSERT_TRUE(base::GetAppOutputWithExitCode(cl
, &output
, &exit_code
)
359 << " exit_code:" << exit_code
<< " " << output
;
362 TEST_F(ServiceProcessStateFileManipulationTest
, DeleteFile
) {
363 GetIOMessageLoopProxy()->PostTask(
365 base::Bind(&DeleteFunc
, executable_path()));
367 ASSERT_TRUE(mock_launchd()->remove_called());
368 ASSERT_TRUE(mock_launchd()->delete_called());
371 TEST_F(ServiceProcessStateFileManipulationTest
, DeleteBundle
) {
372 GetIOMessageLoopProxy()->PostTask(
374 base::Bind(&DeleteFunc
, bundle_path()));
376 ASSERT_TRUE(mock_launchd()->remove_called());
377 ASSERT_TRUE(mock_launchd()->delete_called());
380 TEST_F(ServiceProcessStateFileManipulationTest
, MoveBundle
) {
381 base::FilePath new_loc
= GetTempDirPath().AppendASCII("MoveBundle");
382 GetIOMessageLoopProxy()->PostTask(
384 base::Bind(&MoveFunc
, bundle_path(), new_loc
));
386 ASSERT_TRUE(mock_launchd()->restart_called());
387 ASSERT_TRUE(mock_launchd()->write_called());
390 TEST_F(ServiceProcessStateFileManipulationTest
, MoveFile
) {
391 base::FilePath new_loc
= GetTempDirPath().AppendASCII("MoveFile");
392 GetIOMessageLoopProxy()->PostTask(
394 base::Bind(&MoveFunc
, executable_path(), new_loc
));
396 ASSERT_TRUE(mock_launchd()->remove_called());
397 ASSERT_TRUE(mock_launchd()->delete_called());
400 TEST_F(ServiceProcessStateFileManipulationTest
, TrashBundle
) {
402 ASSERT_TRUE(base::mac::FSRefFromPath(bundle_path().value(), &bundle_ref
));
403 GetIOMessageLoopProxy()->PostTask(
405 base::Bind(&TrashFunc
, bundle_path()));
407 ASSERT_TRUE(mock_launchd()->remove_called());
408 ASSERT_TRUE(mock_launchd()->delete_called());
409 std::string
path(base::mac::PathFromFSRef(bundle_ref
));
410 base::FilePath
file_path(path
);
411 ASSERT_TRUE(base::DeleteFile(file_path
, true));
414 TEST_F(ServiceProcessStateFileManipulationTest
, ChangeAttr
) {
415 ScopedAttributesRestorer
restorer(bundle_path(), 0777);
416 GetIOMessageLoopProxy()->PostTask(
418 base::Bind(&ChangeAttr
, bundle_path(), 0222));
420 ASSERT_TRUE(mock_launchd()->remove_called());
421 ASSERT_TRUE(mock_launchd()->delete_called());