1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/common/mac/mock_launchd.h"
#include <CoreFoundation/CoreFoundation.h>
#include <errno.h>
#include <stddef.h>
#include <sys/un.h>
#include <memory>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/common/mac/launchd.h"
#include "chrome/common/service_process_util.h"
#include "components/version_info/version_info.h"
#include "testing/gtest/include/gtest/gtest.h"
// static
bool MockLaunchd::MakeABundle(const base::FilePath& dst,
const std::string& name,
base::FilePath* bundle_root,
base::FilePath* executable) {
*bundle_root = dst.Append(name + std::string(".app"));
base::FilePath contents = bundle_root->AppendASCII("Contents");
base::FilePath mac_os = contents.AppendASCII("MacOS");
*executable = mac_os.Append(name);
base::FilePath info_plist = contents.Append("Info.plist");
if (!base::CreateDirectory(mac_os)) {
return false;
}
const char* data = "#! testbundle\n";
int len = strlen(data);
if (base::WriteFile(*executable, data, len) != len) {
return false;
}
if (chmod(executable->value().c_str(), 0555) != 0) {
return false;
}
const char info_plist_format[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" "
"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
"<plist version=\"1.0\">\n"
"<dict>\n"
" <key>CFBundleDevelopmentRegion</key>\n"
" <string>English</string>\n"
" <key>CFBundleExecutable</key>\n"
" <string>%s</string>\n"
" <key>CFBundleIdentifier</key>\n"
" <string>com.test.%s</string>\n"
" <key>CFBundleInfoDictionaryVersion</key>\n"
" <string>6.0</string>\n"
" <key>CFBundleShortVersionString</key>\n"
" <string>%s</string>\n"
" <key>CFBundleVersion</key>\n"
" <string>1</string>\n"
"</dict>\n"
"</plist>\n";
std::string info_plist_data =
base::StringPrintf(info_plist_format, name.c_str(), name.c_str(),
version_info::GetVersionNumber().c_str());
len = info_plist_data.length();
if (base::WriteFile(info_plist, info_plist_data.c_str(), len) != len) {
return false;
}
const UInt8* bundle_root_path =
reinterpret_cast<const UInt8*>(bundle_root->value().c_str());
base::ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(
kCFAllocatorDefault, bundle_root_path, bundle_root->value().length(),
true));
base::ScopedCFTypeRef<CFBundleRef> bundle(
CFBundleCreate(kCFAllocatorDefault, url));
return bundle.get();
}
MockLaunchd::MockLaunchd(
const base::FilePath& file,
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
base::OnceClosure quit_closure,
bool as_service)
: file_(file),
pipe_name_(GetServiceProcessServerName()),
main_task_runner_(std::move(main_task_runner)),
quit_closure_(std::move(quit_closure)),
as_service_(as_service),
restart_called_(false),
remove_called_(false),
checkin_called_(false),
write_called_(false),
delete_called_(false) {}
MockLaunchd::~MockLaunchd() {}
bool MockLaunchd::GetJobInfo(const std::string& label,
mac::services::JobInfo* info) {
if (!as_service_) {
std::unique_ptr<MultiProcessLock> running_lock = TakeNamedLock(pipe_name_);
if (running_lock.get())
return false;
}
info->program = file_.value();
info->pid = base::GetCurrentProcId();
return true;
}
bool MockLaunchd::RemoveJob(const std::string& label) {
remove_called_ = true;
std::move(quit_closure_).Run();
return true;
}
bool MockLaunchd::RestartJob(Domain domain,
Type type,
CFStringRef name,
CFStringRef session_type) {
restart_called_ = true;
std::move(quit_closure_).Run();
return true;
}
CFMutableDictionaryRef MockLaunchd::CreatePlistFromFile(Domain domain,
Type type,
CFStringRef name) {
NSString* ns_program = base::SysUTF8ToNSString(file_.value());
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary:@{
@LAUNCH_JOBKEY_PROGRAM : ns_program,
@LAUNCH_JOBKEY_PROGRAMARGUMENTS : @[ ns_program ],
}];
// Callers expect to be given a reference but dictionaryWithDictionary: is
// autoreleased, so it's necessary to do a manual retain here.
return base::mac::NSToCFCast([dict retain]);
}
bool MockLaunchd::WritePlistToFile(Domain domain,
Type type,
CFStringRef name,
CFDictionaryRef dict) {
write_called_ = true;
return true;
}
bool MockLaunchd::DeletePlist(Domain domain, Type type, CFStringRef name) {
delete_called_ = true;
return true;
}
void MockLaunchd::SignalReady() {
ASSERT_TRUE(as_service_);
running_lock_ = TakeNamedLock(pipe_name_);
}
|