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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
// Copyright 2016 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 "base/base_paths.h"
#include "base/base_switches.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/macros.h"
#include "base/path_service.h"
#include "base/process/process.h"
#include "base/run_loop.h"
#include "mojo/edk/embedder/embedder.h"
#include "mojo/edk/embedder/platform_channel_pair.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/shell/public/cpp/identity.h"
#include "mojo/shell/public/cpp/shell_test.h"
#include "mojo/shell/public/interfaces/shell.mojom.h"
#include "mojo/shell/runner/common/switches.h"
#include "mojo/shell/tests/lifecycle/lifecycle_unittest.mojom.h"
namespace mojo {
namespace shell {
namespace {
const char kTestAppName[] = "mojo:lifecycle_unittest_app";
const char kTestExeName[] = "exe:lifecycle_unittest_exe";
const char kTestPackageName[] = "mojo:lifecycle_unittest_package";
const char kTestPackageAppNameA[] = "mojo:lifecycle_unittest_package_app_a";
const char kTestPackageAppNameB[] = "mojo:lifecycle_unittest_package_app_b";
const char kTestName[] = "mojo:lifecycle_unittest";
void QuitLoop(base::RunLoop* loop) {
loop->Quit();
}
void DecrementCountAndQuitWhenZero(base::RunLoop* loop, size_t* count) {
if (!--(*count))
loop->Quit();
}
struct Instance {
Instance() : id(shell::mojom::kInvalidInstanceID), pid(0) {}
Instance(const Identity& identity, uint32_t id, uint32_t pid)
: identity(identity), id(id), pid(pid) {}
Identity identity;
uint32_t id;
uint32_t pid;
};
class InstanceState : public mojom::InstanceListener {
public:
InstanceState(mojom::InstanceListenerRequest request, base::RunLoop* loop)
: binding_(this, std::move(request)), loop_(loop) {}
~InstanceState() override {}
bool HasInstanceForName(const std::string& name) const {
return instances_.find(name) != instances_.end();
}
size_t GetNewInstanceCount() const {
return instances_.size() - initial_instances_.size();
}
void WaitForInstanceDestruction(base::RunLoop* loop) {
DCHECK(!destruction_loop_);
destruction_loop_ = loop;
// First of all check to see if we should be spinning this loop at all -
// the app(s) we're waiting on quitting may already have quit.
TryToQuitDestructionLoop();
}
private:
// mojom::InstanceListener:
void SetExistingInstances(Array<mojom::InstanceInfoPtr> instances) override {
for (const auto& instance : instances) {
Instance i(instance->identity.To<Identity>(), instance->id,
instance->pid);
initial_instances_[i.identity.name()] = i;
instances_[i.identity.name()] = i;
}
loop_->Quit();
}
void InstanceCreated(mojom::InstanceInfoPtr instance) override {
instances_[instance->identity->name] =
Instance(instance->identity.To<Identity>(), instance->id,
instance->pid);
}
void InstanceDestroyed(uint32_t id) override {
for (auto it = instances_.begin(); it != instances_.end(); ++it) {
if (it->second.id == id) {
instances_.erase(it);
break;
}
}
TryToQuitDestructionLoop();
}
void InstancePIDAvailable(uint32_t id, uint32_t pid) override {
for (auto& instance : instances_) {
if (instance.second.id == id) {
instance.second.pid = pid;
break;
}
}
}
void TryToQuitDestructionLoop() {
if (!GetNewInstanceCount() && destruction_loop_) {
destruction_loop_->Quit();
destruction_loop_ = nullptr;
}
}
// All currently running instances.
std::map<std::string, Instance> instances_;
// The initial set of instances.
std::map<std::string, Instance> initial_instances_;
Binding<mojom::InstanceListener> binding_;
base::RunLoop* loop_;
// Set when the client wants to wait for this object to track the destruction
// of an instance before proceeding.
base::RunLoop* destruction_loop_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(InstanceState);
};
}
class LifecycleTest : public mojo::test::ShellTest {
public:
LifecycleTest() : ShellTest(kTestName) {}
~LifecycleTest() override {}
protected:
// mojo::test::ShellTest:
void SetUp() override {
mojo::test::ShellTest::SetUp();
InitPackage();
instances_ = TrackInstances();
}
void TearDown() override {
instances_.reset();
mojo::test::ShellTest::TearDown();
}
bool CanRunCrashTest() {
return !base::CommandLine::ForCurrentProcess()->HasSwitch("single-process");
}
void InitPackage() {
test::mojom::LifecycleControlPtr lifecycle = ConnectTo(kTestPackageName);
base::RunLoop loop;
lifecycle.set_connection_error_handler(base::Bind(&QuitLoop, &loop));
lifecycle->GracefulQuit();
loop.Run();
}
test::mojom::LifecycleControlPtr ConnectTo(const std::string& name) {
test::mojom::LifecycleControlPtr lifecycle;
connector()->ConnectToInterface(name, &lifecycle);
PingPong(lifecycle.get());
return lifecycle;
}
base::Process LaunchProcess() {
base::FilePath target_path;
CHECK(base::PathService::Get(base::DIR_EXE, &target_path));
#if defined(OS_WIN)
target_path = target_path.Append(
FILE_PATH_LITERAL("lifecycle_unittest_exe.exe"));
#else
target_path = target_path.Append(
FILE_PATH_LITERAL("lifecycle_unittest_exe"));
#endif
base::CommandLine child_command_line(target_path);
// Forward the wait-for-debugger flag but nothing else - we don't want to
// stamp on the platform-channel flag.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kWaitForDebugger)) {
child_command_line.AppendSwitch(switches::kWaitForDebugger);
}
mojo::shell::mojom::PIDReceiverPtr receiver;
mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request =
GetProxy(&receiver);
// Create the channel to be shared with the target process. Pass one end
// on the command line.
mojo::edk::PlatformChannelPair platform_channel_pair;
mojo::edk::HandlePassingInformation handle_passing_info;
platform_channel_pair.PrepareToPassClientHandleToChildProcess(
&child_command_line, &handle_passing_info);
// Generate a token for the child to find and connect to a primordial pipe
// and pass that as well.
std::string primordial_pipe_token = mojo::edk::GenerateRandomToken();
child_command_line.AppendSwitchASCII(switches::kPrimordialPipeToken,
primordial_pipe_token);
// Allocate the pipe locally.
mojo::ScopedMessagePipeHandle pipe =
mojo::edk::CreateParentMessagePipe(primordial_pipe_token);
mojo::shell::mojom::CapabilityFilterPtr filter(
mojo::shell::mojom::CapabilityFilter::New());
mojo::Array<mojo::String> test_interfaces;
test_interfaces.push_back("*");
filter->filter.insert("*", std::move(test_interfaces));
mojo::shell::mojom::ShellPtr shell;
connector()->ConnectToInterface("mojo:shell", &shell);
mojo::shell::mojom::ShellClientFactoryPtr factory;
factory.Bind(mojo::InterfacePtrInfo<mojo::shell::mojom::ShellClientFactory>(
std::move(pipe), 0u));
base::RunLoop loop;
mojo::shell::mojom::IdentityPtr target(mojo::shell::mojom::Identity::New());
target->name = kTestExeName;
target->user_id = mojom::kInheritUserID;
target->instance = "";
shell->CreateInstance(std::move(factory), std::move(target),
std::move(filter), std::move(request),
base::Bind(&LifecycleTest::OnConnectionCompleted,
base::Unretained(this), &loop));
loop.Run();
base::LaunchOptions options;
#if defined(OS_WIN)
options.handles_to_inherit = &handle_passing_info;
#elif defined(OS_POSIX)
options.fds_to_remap = &handle_passing_info;
#endif
base::Process process = base::LaunchProcess(child_command_line, options);
DCHECK(process.IsValid());
receiver->SetPID(process.Pid());
mojo::edk::ChildProcessLaunched(process.Handle(),
platform_channel_pair.PassServerHandle());
return process;
}
void PingPong(test::mojom::LifecycleControl* lifecycle) {
base::RunLoop loop;
lifecycle->Ping(base::Bind(&QuitLoop, &loop));
loop.Run();
}
InstanceState* instances() { return instances_.get(); }
void WaitForInstanceDestruction() {
base::RunLoop loop;
instances()->WaitForInstanceDestruction(&loop);
loop.Run();
}
private:
scoped_ptr<InstanceState> TrackInstances() {
mojom::ShellPtr shell;
connector()->ConnectToInterface("mojo:shell", &shell);
mojom::InstanceListenerPtr listener;
base::RunLoop loop;
InstanceState* state = new InstanceState(GetProxy(&listener), &loop);
shell->AddInstanceListener(std::move(listener));
loop.Run();
return make_scoped_ptr(state);
}
void OnConnectionCompleted(base::RunLoop* loop,
mojom::ConnectResult result) {
loop->Quit();
}
scoped_ptr<InstanceState> instances_;
DISALLOW_COPY_AND_ASSIGN(LifecycleTest);
};
TEST_F(LifecycleTest, Standalone_GracefulQuit) {
test::mojom::LifecycleControlPtr lifecycle = ConnectTo(kTestAppName);
EXPECT_TRUE(instances()->HasInstanceForName(kTestAppName));
EXPECT_EQ(1u, instances()->GetNewInstanceCount());
base::RunLoop loop;
lifecycle.set_connection_error_handler(base::Bind(&QuitLoop, &loop));
lifecycle->GracefulQuit();
loop.Run();
WaitForInstanceDestruction();
EXPECT_FALSE(instances()->HasInstanceForName(kTestAppName));
EXPECT_EQ(0u, instances()->GetNewInstanceCount());
}
TEST_F(LifecycleTest, Standalone_Crash) {
if (!CanRunCrashTest()) {
LOG(INFO) << "Skipping Standalone_Crash test in --single-process mode.";
return;
}
test::mojom::LifecycleControlPtr lifecycle = ConnectTo(kTestAppName);
EXPECT_TRUE(instances()->HasInstanceForName(kTestAppName));
EXPECT_EQ(1u, instances()->GetNewInstanceCount());
base::RunLoop loop;
lifecycle.set_connection_error_handler(base::Bind(&QuitLoop, &loop));
lifecycle->Crash();
loop.Run();
WaitForInstanceDestruction();
EXPECT_FALSE(instances()->HasInstanceForName(kTestAppName));
EXPECT_EQ(0u, instances()->GetNewInstanceCount());
}
TEST_F(LifecycleTest, Standalone_CloseShellConnection) {
test::mojom::LifecycleControlPtr lifecycle = ConnectTo(kTestAppName);
EXPECT_TRUE(instances()->HasInstanceForName(kTestAppName));
EXPECT_EQ(1u, instances()->GetNewInstanceCount());
base::RunLoop loop;
lifecycle.set_connection_error_handler(base::Bind(&QuitLoop, &loop));
lifecycle->CloseShellConnection();
WaitForInstanceDestruction();
// |lifecycle| pipe should still be valid.
PingPong(lifecycle.get());
}
TEST_F(LifecycleTest, PackagedApp_GracefulQuit) {
test::mojom::LifecycleControlPtr lifecycle = ConnectTo(kTestPackageAppNameA);
// There should be two new instances - one for the app and one for the package
// that vended it.
EXPECT_TRUE(instances()->HasInstanceForName(kTestPackageAppNameA));
EXPECT_TRUE(instances()->HasInstanceForName(kTestPackageName));
EXPECT_EQ(2u, instances()->GetNewInstanceCount());
base::RunLoop loop;
lifecycle.set_connection_error_handler(base::Bind(&QuitLoop, &loop));
lifecycle->GracefulQuit();
loop.Run();
WaitForInstanceDestruction();
EXPECT_FALSE(instances()->HasInstanceForName(kTestPackageName));
EXPECT_FALSE(instances()->HasInstanceForName(kTestAppName));
EXPECT_EQ(0u, instances()->GetNewInstanceCount());
}
TEST_F(LifecycleTest, PackagedApp_Crash) {
if (!CanRunCrashTest()) {
LOG(INFO) << "Skipping Standalone_Crash test in --single-process mode.";
return;
}
test::mojom::LifecycleControlPtr lifecycle = ConnectTo(kTestPackageAppNameA);
// There should be two new instances - one for the app and one for the package
// that vended it.
EXPECT_TRUE(instances()->HasInstanceForName(kTestPackageAppNameA));
EXPECT_TRUE(instances()->HasInstanceForName(kTestPackageName));
EXPECT_EQ(2u, instances()->GetNewInstanceCount());
base::RunLoop loop;
lifecycle.set_connection_error_handler(base::Bind(&QuitLoop, &loop));
lifecycle->Crash();
loop.Run();
WaitForInstanceDestruction();
EXPECT_FALSE(instances()->HasInstanceForName(kTestPackageName));
EXPECT_FALSE(instances()->HasInstanceForName(kTestPackageAppNameA));
EXPECT_EQ(0u, instances()->GetNewInstanceCount());
}
TEST_F(LifecycleTest, PackagedApp_CloseShellConnection) {
test::mojom::LifecycleControlPtr lifecycle = ConnectTo(kTestPackageAppNameA);
EXPECT_TRUE(instances()->HasInstanceForName(kTestPackageAppNameA));
EXPECT_TRUE(instances()->HasInstanceForName(kTestPackageName));
EXPECT_EQ(2u, instances()->GetNewInstanceCount());
base::RunLoop loop;
lifecycle.set_connection_error_handler(base::Bind(&QuitLoop, &loop));
lifecycle->CloseShellConnection();
WaitForInstanceDestruction();
EXPECT_FALSE(instances()->HasInstanceForName(kTestPackageAppNameA));
EXPECT_FALSE(instances()->HasInstanceForName(kTestPackageName));
EXPECT_EQ(0u, instances()->GetNewInstanceCount());
// |lifecycle| pipe should still be valid.
PingPong(lifecycle.get());
}
// When a single package provides multiple apps out of one process, crashing one
// app crashes all.
TEST_F(LifecycleTest, PackagedApp_CrashCrashesOtherProvidedApp) {
if (!CanRunCrashTest()) {
LOG(INFO) << "Skipping Standalone_Crash test in --single-process mode.";
return;
}
test::mojom::LifecycleControlPtr lifecycle_a =
ConnectTo(kTestPackageAppNameA);
test::mojom::LifecycleControlPtr lifecycle_b =
ConnectTo(kTestPackageAppNameB);
test::mojom::LifecycleControlPtr lifecycle_package =
ConnectTo(kTestPackageName);
// There should be three instances, one for each packaged app and the package
// itself.
EXPECT_TRUE(instances()->HasInstanceForName(kTestPackageAppNameA));
EXPECT_TRUE(instances()->HasInstanceForName(kTestPackageAppNameB));
EXPECT_TRUE(instances()->HasInstanceForName(kTestPackageName));
size_t instance_count = instances()->GetNewInstanceCount();
EXPECT_EQ(3u, instance_count);
base::RunLoop loop;
lifecycle_a.set_connection_error_handler(
base::Bind(&DecrementCountAndQuitWhenZero, &loop, &instance_count));
lifecycle_b.set_connection_error_handler(
base::Bind(&DecrementCountAndQuitWhenZero, &loop, &instance_count));
lifecycle_package.set_connection_error_handler(
base::Bind(&DecrementCountAndQuitWhenZero, &loop, &instance_count));
// Now crash one of the packaged apps.
lifecycle_a->Crash();
loop.Run();
WaitForInstanceDestruction();
EXPECT_FALSE(instances()->HasInstanceForName(kTestPackageName));
EXPECT_FALSE(instances()->HasInstanceForName(kTestPackageAppNameA));
EXPECT_FALSE(instances()->HasInstanceForName(kTestPackageAppNameB));
EXPECT_EQ(0u, instances()->GetNewInstanceCount());
}
// When a single package provides multiple apps out of one process, crashing one
// app crashes all.
TEST_F(LifecycleTest, PackagedApp_GracefulQuitPackageQuitsAll) {
test::mojom::LifecycleControlPtr lifecycle_a =
ConnectTo(kTestPackageAppNameA);
test::mojom::LifecycleControlPtr lifecycle_b =
ConnectTo(kTestPackageAppNameB);
test::mojom::LifecycleControlPtr lifecycle_package =
ConnectTo(kTestPackageName);
// There should be three instances, one for each packaged app and the package
// itself.
EXPECT_TRUE(instances()->HasInstanceForName(kTestPackageAppNameA));
EXPECT_TRUE(instances()->HasInstanceForName(kTestPackageAppNameB));
EXPECT_TRUE(instances()->HasInstanceForName(kTestPackageName));
size_t instance_count = instances()->GetNewInstanceCount();
EXPECT_EQ(3u, instance_count);
base::RunLoop loop;
lifecycle_a.set_connection_error_handler(
base::Bind(&DecrementCountAndQuitWhenZero, &loop, &instance_count));
lifecycle_b.set_connection_error_handler(
base::Bind(&DecrementCountAndQuitWhenZero, &loop, &instance_count));
lifecycle_package.set_connection_error_handler(
base::Bind(&DecrementCountAndQuitWhenZero, &loop, &instance_count));
// Now quit the package. All the packaged apps should close.
lifecycle_package->GracefulQuit();
loop.Run();
WaitForInstanceDestruction();
EXPECT_FALSE(instances()->HasInstanceForName(kTestPackageName));
EXPECT_FALSE(instances()->HasInstanceForName(kTestPackageAppNameA));
EXPECT_FALSE(instances()->HasInstanceForName(kTestPackageAppNameB));
EXPECT_EQ(0u, instances()->GetNewInstanceCount());
}
TEST_F(LifecycleTest, Exe_GracefulQuit) {
base::Process process = LaunchProcess();
test::mojom::LifecycleControlPtr lifecycle = ConnectTo(kTestExeName);
EXPECT_TRUE(instances()->HasInstanceForName(kTestExeName));
EXPECT_EQ(1u, instances()->GetNewInstanceCount());
base::RunLoop loop;
lifecycle.set_connection_error_handler(base::Bind(&QuitLoop, &loop));
lifecycle->GracefulQuit();
loop.Run();
WaitForInstanceDestruction();
EXPECT_FALSE(instances()->HasInstanceForName(kTestExeName));
EXPECT_EQ(0u, instances()->GetNewInstanceCount());
process.Terminate(9, true);
}
TEST_F(LifecycleTest, Exe_TerminateProcess) {
base::Process process = LaunchProcess();
test::mojom::LifecycleControlPtr lifecycle = ConnectTo(kTestExeName);
EXPECT_TRUE(instances()->HasInstanceForName(kTestExeName));
EXPECT_EQ(1u, instances()->GetNewInstanceCount());
base::RunLoop loop;
lifecycle.set_connection_error_handler(base::Bind(&QuitLoop, &loop));
process.Terminate(9, true);
loop.Run();
WaitForInstanceDestruction();
EXPECT_FALSE(instances()->HasInstanceForName(kTestExeName));
EXPECT_EQ(0u, instances()->GetNewInstanceCount());
}
} // namespace shell
} // namespace mojo
|