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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
mojo / core / test / multiprocess_test_helper.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/core/test/multiprocess_test_helper.h"
#include <functional>
#include <set>
#include <string>
#include <utility>
#include "base/base_paths.h"
#include "base/base_switches.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/lazy_instance.h"
#include "base/memory/ref_counted.h"
#include "base/notreached.h"
#include "base/path_service.h"
#include "base/process/kill.h"
#include "base/process/process_handle.h"
#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/task_runner.h"
#include "build/build_config.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/platform/platform_channel_endpoint.h"
#include "mojo/public/cpp/platform/platform_channel_server_endpoint.h"
#include "mojo/public/cpp/system/invitation.h"
#include "mojo/public/cpp/system/isolated_connection.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "testing/gtest/include/gtest/gtest.h"
#if !BUILDFLAG(IS_FUCHSIA)
#include "mojo/public/cpp/platform/named_platform_channel.h"
#endif
namespace mojo {
namespace core {
namespace test {
namespace {
#if !BUILDFLAG(IS_FUCHSIA)
const char kNamedPipeName[] = "named-pipe-name";
#endif
const char kRunAsBrokerClient[] = "run-as-broker-client";
const char kAcceptInvitationAsync[] = "accept-invitation-async";
const char kTestChildMessagePipeName[] = "test_pipe";
const char kDisableAllCapabilities[] = "disable-all-capabilities";
// For use (and only valid) in a test child process:
base::LazyInstance<IsolatedConnection>::Leaky g_child_isolated_connection;
int RunClientFunction(base::OnceCallback<int(MojoHandle)> handler,
bool pass_pipe_ownership_to_main) {
CHECK(MultiprocessTestHelper::primordial_pipe.is_valid());
ScopedMessagePipeHandle pipe =
std::move(MultiprocessTestHelper::primordial_pipe);
MessagePipeHandle pipe_handle =
pass_pipe_ownership_to_main ? pipe.release() : pipe.get();
return std::move(handler).Run(pipe_handle.value());
}
} // namespace
MultiprocessTestHelper::MultiprocessTestHelper() = default;
MultiprocessTestHelper::~MultiprocessTestHelper() {
CHECK(!test_child_.IsValid());
}
ScopedMessagePipeHandle MultiprocessTestHelper::StartChild(
const std::string& test_child_name,
LaunchType launch_type) {
return StartChildWithExtraSwitch(test_child_name, std::string(),
std::string(), launch_type);
}
ScopedMessagePipeHandle MultiprocessTestHelper::StartChildWithExtraSwitch(
const std::string& test_child_name,
const std::string& switch_string,
const std::string& switch_value,
LaunchType launch_type) {
CHECK(!test_child_name.empty());
CHECK(!test_child_.IsValid());
std::string test_child_main = test_child_name + "TestChildMain";
// Manually construct the new child's commandline to avoid copying unwanted
// values.
base::CommandLine command_line(
base::GetMultiProcessTestChildBaseCommandLine().GetProgram());
std::set<std::string> uninherited_args;
uninherited_args.insert("mojo-platform-channel-handle");
uninherited_args.insert(switches::kTestChildProcess);
std::string enable_overrides;
std::string disable_overrides;
base::FeatureList::GetInstance()->GetCommandLineFeatureOverrides(
&enable_overrides, &disable_overrides);
command_line.AppendSwitchASCII(switches::kEnableFeatures, enable_overrides);
command_line.AppendSwitchASCII(switches::kDisableFeatures, disable_overrides);
// Copy commandline switches from the parent process, except for the
// multiprocess client name and mojo message pipe handle; this allows test
// clients to spawn other test clients.
for (const auto& entry :
base::CommandLine::ForCurrentProcess()->GetSwitches()) {
if (!base::Contains(uninherited_args, entry.first)) {
command_line.AppendSwitchNative(entry.first, entry.second);
}
}
#if !BUILDFLAG(IS_FUCHSIA)
NamedPlatformChannel::ServerName server_name;
#endif
PlatformChannel channel;
base::LaunchOptions options;
switch (launch_type) {
case LaunchType::CHILD:
case LaunchType::CHILD_WITHOUT_CAPABILITIES:
case LaunchType::PEER:
case LaunchType::ASYNC:
channel.PrepareToPassRemoteEndpoint(&options, &command_line);
break;
#if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
case LaunchType::NAMED_CHILD:
case LaunchType::NAMED_PEER: {
#if BUILDFLAG(IS_MAC)
server_name = NamedPlatformChannel::ServerNameFromUTF8(
"mojo.test." + base::NumberToString(base::RandUint64()));
#elif BUILDFLAG(IS_POSIX)
base::FilePath temp_dir;
CHECK(base::PathService::Get(base::DIR_TEMP, &temp_dir));
server_name =
temp_dir.AppendASCII(base::NumberToString(base::RandUint64()))
.value();
#elif BUILDFLAG(IS_WIN)
server_name = base::NumberToWString(base::RandUint64());
#else
#error "Platform not yet supported."
#endif
command_line.AppendSwitchNative(kNamedPipeName, server_name);
break;
}
#endif // !BUILDFLAG(IS_FUCHSIA)
}
if (!switch_string.empty()) {
CHECK(!command_line.HasSwitch(switch_string));
if (!switch_value.empty())
command_line.AppendSwitchASCII(switch_string, switch_value);
else
command_line.AppendSwitch(switch_string);
}
#if BUILDFLAG(IS_WIN)
options.start_hidden = true;
#endif
// NOTE: In the case of named pipes, it's important that the server handle be
// created before the child process is launched; otherwise the server binding
// the pipe path can race with child's connection to the pipe.
PlatformChannelEndpoint local_channel_endpoint;
PlatformChannelServerEndpoint server_endpoint;
switch (launch_type) {
case LaunchType::CHILD:
case LaunchType::CHILD_WITHOUT_CAPABILITIES:
case LaunchType::PEER:
case LaunchType::ASYNC:
local_channel_endpoint = channel.TakeLocalEndpoint();
break;
#if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
case LaunchType::NAMED_CHILD:
case LaunchType::NAMED_PEER: {
NamedPlatformChannel::Options channel_options;
channel_options.server_name = server_name;
NamedPlatformChannel named_channel(channel_options);
server_endpoint = named_channel.TakeServerEndpoint();
break;
}
#endif // !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
};
OutgoingInvitation child_invitation;
ScopedMessagePipeHandle pipe;
switch (launch_type) {
case LaunchType::CHILD_WITHOUT_CAPABILITIES:
case LaunchType::ASYNC:
// It's one or the other
command_line.AppendSwitch(launch_type == LaunchType::ASYNC
? kAcceptInvitationAsync
: kDisableAllCapabilities);
[[fallthrough]];
case LaunchType::CHILD:
#if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
case LaunchType::NAMED_CHILD:
#endif
pipe = child_invitation.AttachMessagePipe(kTestChildMessagePipeName);
command_line.AppendSwitch(kRunAsBrokerClient);
break;
case LaunchType::PEER:
#if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
case LaunchType::NAMED_PEER:
#endif
isolated_connection_ = std::make_unique<IsolatedConnection>();
if (local_channel_endpoint.is_valid()) {
pipe = isolated_connection_->Connect(std::move(local_channel_endpoint));
} else {
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_WIN)
DCHECK(server_endpoint.is_valid());
pipe = isolated_connection_->Connect(std::move(server_endpoint));
#else
NOTREACHED();
#endif
}
break;
}
test_child_ =
base::SpawnMultiProcessTestChild(test_child_main, command_line, options);
if (launch_type == LaunchType::CHILD ||
launch_type == LaunchType::CHILD_WITHOUT_CAPABILITIES ||
launch_type == LaunchType::PEER || launch_type == LaunchType::ASYNC) {
channel.RemoteProcessLaunchAttempted();
}
if (launch_type == LaunchType::CHILD ||
launch_type == LaunchType::CHILD_WITHOUT_CAPABILITIES) {
DCHECK(local_channel_endpoint.is_valid());
OutgoingInvitation::Send(std::move(child_invitation), test_child_.Handle(),
std::move(local_channel_endpoint),
ProcessErrorCallback());
} else if (launch_type == LaunchType::ASYNC) {
DCHECK(local_channel_endpoint.is_valid());
OutgoingInvitation::SendAsync(
std::move(child_invitation), test_child_.Handle(),
std::move(local_channel_endpoint), ProcessErrorCallback());
}
#if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
else if (launch_type == LaunchType::NAMED_CHILD) {
DCHECK(server_endpoint.is_valid());
OutgoingInvitation::Send(std::move(child_invitation), test_child_.Handle(),
std::move(server_endpoint),
ProcessErrorCallback());
}
#endif // !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
CHECK(test_child_.IsValid());
return pipe;
}
int MultiprocessTestHelper::WaitForChildShutdown() {
CHECK(test_child_.IsValid());
int rv = -1;
WaitForMultiprocessTestChildExit(test_child_, TestTimeouts::action_timeout(),
&rv);
test_child_.Close();
return rv;
}
bool MultiprocessTestHelper::WaitForChildTestShutdown() {
return WaitForChildShutdown() == 0;
}
// static
void MultiprocessTestHelper::ChildSetup() {
CHECK(base::CommandLine::InitializedForCurrentProcess());
auto& command_line = *base::CommandLine::ForCurrentProcess();
const bool run_as_broker_client = command_line.HasSwitch(kRunAsBrokerClient);
const bool async = command_line.HasSwitch(kAcceptInvitationAsync);
PlatformChannelEndpoint endpoint;
#if !BUILDFLAG(IS_FUCHSIA)
NamedPlatformChannel::ServerName named_pipe(
command_line.GetSwitchValueNative(kNamedPipeName));
if (!named_pipe.empty()) {
endpoint = NamedPlatformChannel::ConnectToServer(named_pipe);
} else
#endif // !BUILDFLAG(IS_FUCHSIA)
{
endpoint =
PlatformChannel::RecoverPassedEndpointFromCommandLine(command_line);
}
if (run_as_broker_client) {
IncomingInvitation invitation;
if (async)
invitation = IncomingInvitation::AcceptAsync(std::move(endpoint));
else
invitation = IncomingInvitation::Accept(std::move(endpoint));
primordial_pipe = invitation.ExtractMessagePipe(kTestChildMessagePipeName);
} else {
primordial_pipe =
g_child_isolated_connection.Get().Connect(std::move(endpoint));
}
}
// static
int MultiprocessTestHelper::RunClientMain(
base::OnceCallback<int(MojoHandle)> main,
bool pass_pipe_ownership_to_main) {
return RunClientFunction(std::move(main), pass_pipe_ownership_to_main);
}
// static
int MultiprocessTestHelper::RunClientTestMain(
base::OnceCallback<void(MojoHandle)> main) {
return RunClientFunction(
base::BindOnce(
[](base::OnceCallback<void(MojoHandle)> main, MojoHandle handle) {
std::move(main).Run(handle);
return (::testing::Test::HasFatalFailure() ||
::testing::Test::HasNonfatalFailure())
? 1
: 0;
},
std::move(main)),
true /* pass_pipe_ownership_to_main */);
}
ScopedMessagePipeHandle MultiprocessTestHelper::primordial_pipe;
} // namespace test
} // namespace core
} // namespace mojo