summaryrefslogtreecommitdiffstats
path: root/mojo/shell/tests/connect/connect_unittest.cc
blob: 355f6baca5cbc6c9d2b1be2ab62b4e28c2207a5d (plain)
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
// 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 <stddef.h>
#include <stdint.h>

#include <utility>

#include "base/bind.h"
#include "base/guid.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/test/test_suite.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "mojo/shell/public/cpp/names.h"
#include "mojo/shell/public/cpp/shell_test.h"
#include "mojo/shell/public/interfaces/shell.mojom.h"
#include "mojo/shell/tests/connect/connect_test.mojom.h"

// Tests that multiple applications can be packaged in a single Mojo application
// implementing ShellClientFactory; that these applications can be specified by
// the package's manifest and are thus registered with the PackageManager.

namespace mojo {
namespace shell {
namespace {
const char kTestPackageName[] = "mojo:connect_test_package";
const char kTestAppName[] = "mojo:connect_test_app";
const char kTestAppAName[] = "mojo:connect_test_a";
const char kTestAppBName[] = "mojo:connect_test_b";
const char kTestClassAppName[] = "mojo:connect_test_class_app";
const char kTestSingletonAppName[] = "mojo:connect_test_singleton_app";
const char kTestDriverName[] = "exe:connect_test_driver";

void ReceiveOneString(std::string* out_string,
                      base::RunLoop* loop,
                      const String& in_string) {
  *out_string = in_string;
  loop->Quit();
}

void ReceiveTwoStrings(std::string* out_string_1, std::string* out_string_2,
                       base::RunLoop* loop,
                       const String& in_string_1, const String& in_string_2) {
  *out_string_1 = in_string_1;
  *out_string_2 = in_string_2;
  loop->Quit();
}

void ReceiveConnectionResult(mojom::ConnectResult* out_result,
                             Identity* out_target,
                             base::RunLoop* loop,
                             int32_t in_result,
                             mojom::IdentityPtr in_identity) {
  *out_result = static_cast<shell::mojom::ConnectResult>(in_result);
  *out_target = in_identity.To<Identity>();
  loop->Quit();
}

void QuitLoop(base::RunLoop* loop) {
  loop->Quit();
}

}  // namespace

class ConnectTest : public mojo::test::ShellTest,
                    public InterfaceFactory<test::mojom::ExposedInterface>,
                    public test::mojom::ExposedInterface {
 public:
  ConnectTest() : ShellTest("mojo:connect_unittests") {}
  ~ConnectTest() override {}

 protected:
  scoped_ptr<Connection> ConnectTo(Connector::ConnectParams* params) {
    scoped_ptr<Connection> connection = connector()->Connect(params);
    base::RunLoop loop;
    connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop));
    loop.Run();
    return connection;
  }

  void set_ping_callback(const base::Closure& callback) {
    ping_callback_ = callback;
  }

  void CompareConnectionState(
      const std::string& connection_local_name,
      const std::string& connection_remote_name,
      const std::string& connection_remote_userid,
      uint32_t connection_remote_id,
      const std::string& initialize_local_name,
      const std::string& initialize_userid,
      uint32_t initialize_id) {
    EXPECT_EQ(connection_local_name, connection_state_->connection_local_name);
    EXPECT_EQ(connection_remote_name,
              connection_state_->connection_remote_name);
    EXPECT_EQ(connection_remote_userid,
              connection_state_->connection_remote_userid);
    EXPECT_EQ(connection_remote_id, connection_state_->connection_remote_id);
    EXPECT_EQ(initialize_local_name, connection_state_->initialize_local_name);
    EXPECT_EQ(initialize_id, connection_state_->initialize_id);
    EXPECT_EQ(initialize_userid, connection_state_->initialize_userid);
  }

 private:
  // mojo::test::ShellTest:
  void SetUp() override {
    mojo::test::ShellTest::SetUp();
    // We need to connect to the package first to force the shell to read the
    // package app's manifest and register aliases for the applications it
    // provides.
    test::mojom::ConnectTestServicePtr root_service;
    scoped_ptr<Connection> connection = connector()->Connect(kTestPackageName);
    connection->GetInterface(&root_service);
    base::RunLoop run_loop;
    std::string root_name;
    root_service->GetTitle(
        base::Bind(&ReceiveOneString, &root_name, &run_loop));
    run_loop.Run();
  }

  // InterfaceFactory<test::mojom::ExposedInterface>:
  void Create(Connection* connection,
              test::mojom::ExposedInterfaceRequest request) override {
    bindings_.AddBinding(this, std::move(request));
  }
  void ConnectionAccepted(test::mojom::ConnectionStatePtr state) override {
    connection_state_ = std::move(state);
    ping_callback_.Run();
  }

  base::Closure ping_callback_;
  test::mojom::ConnectionStatePtr connection_state_;

  BindingSet<test::mojom::ExposedInterface> bindings_;

  DISALLOW_COPY_AND_ASSIGN(ConnectTest);
};

// Ensure the connection was properly established and that a round trip
// method call/response is completed.
TEST_F(ConnectTest, Connect) {
  scoped_ptr<Connection> connection = connector()->Connect(kTestAppName);
  test::mojom::ConnectTestServicePtr service;
  connection->GetInterface(&service);
  base::RunLoop run_loop;
  std::string title;
  service->GetTitle(base::Bind(&ReceiveOneString, &title, &run_loop));
  run_loop.Run();
  EXPECT_EQ("APP", title);
  EXPECT_FALSE(connection->IsPending());
  EXPECT_NE(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID());
  EXPECT_EQ(connection->GetRemoteIdentity().name(), kTestAppName);
}

TEST_F(ConnectTest, Instances) {
  Connector::ConnectParams params_a(
      Identity(kTestAppName, mojom::kInheritUserID, "A"));
  scoped_ptr<Connection> connection_a1 = ConnectTo(&params_a);
  scoped_ptr<Connection> connection_a2 = ConnectTo(&params_a);
  std::string instance_a1, instance_a2;
  test::mojom::ConnectTestServicePtr service_a1;
  {
    connection_a1->GetInterface(&service_a1);
    base::RunLoop loop;
    service_a1->GetInstance(base::Bind(&ReceiveOneString, &instance_a1, &loop));
    loop.Run();
  }
  test::mojom::ConnectTestServicePtr service_a2;
  {
    connection_a2->GetInterface(&service_a2);
    base::RunLoop loop;
    service_a2->GetInstance(base::Bind(&ReceiveOneString, &instance_a2, &loop));
    loop.Run();
  }
  EXPECT_EQ(instance_a1, instance_a2);

  Connector::ConnectParams params_b(
      Identity(kTestAppName, mojom::kInheritUserID, "B"));
  scoped_ptr<Connection> connection_b = ConnectTo(&params_b);
  std::string instance_b;
  test::mojom::ConnectTestServicePtr service_b;
  {
    connection_b->GetInterface(&service_b);
    base::RunLoop loop;
    service_b->GetInstance(base::Bind(&ReceiveOneString, &instance_b, &loop));
    loop.Run();
  }

  EXPECT_NE(instance_a1, instance_b);
}

// When both the unresolved and resolved instance names are their default
// values, the instance name from the unresolved name must be used.
// (The case where the instance names differ is covered by
// LifecycleTest.PackagedApp_CrashCrashesOtherProvidedApp).
TEST_F(ConnectTest, PreferUnresolvedDefaultInstanceName) {
  // Connect to an app with no manifest-supplied instance name provided by a
  // package, the instance name must be derived from the application instance
  // name, not the package.
  scoped_ptr<Connection> connection = connector()->Connect(kTestAppName);
  {
    base::RunLoop loop;
    connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop));
    loop.Run();
  }

  std::string instance;
  {
    test::mojom::ConnectTestServicePtr service;
    connection->GetInterface(&service);
    base::RunLoop loop;
    service->GetInstance(base::Bind(&ReceiveOneString, &instance, &loop));
    loop.Run();
  }
  EXPECT_EQ(GetNamePath(kTestAppName), instance);
}

// BlockedInterface should not be exposed to this application because it is not
// in our CapabilityFilter whitelist.
TEST_F(ConnectTest, BlockedInterface) {
  scoped_ptr<Connection> connection = connector()->Connect(kTestAppName);
  base::RunLoop run_loop;
  test::mojom::BlockedInterfacePtr blocked;
  connection->GetInterface(&blocked);
  blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop));
  std::string title = "unchanged";
  blocked->GetTitleBlocked(base::Bind(&ReceiveOneString, &title, &run_loop));
  run_loop.Run();
  EXPECT_EQ("unchanged", title);
}

// Connects to an app provided by a package.
TEST_F(ConnectTest, PackagedApp) {
  scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName);
  test::mojom::ConnectTestServicePtr service_a;
  connection->GetInterface(&service_a);
  base::RunLoop run_loop;
  std::string a_name;
  service_a->GetTitle(base::Bind(&ReceiveOneString, &a_name, &run_loop));
  run_loop.Run();
  EXPECT_EQ("A", a_name);
  EXPECT_FALSE(connection->IsPending());
  EXPECT_NE(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID());
  EXPECT_EQ(connection->GetRemoteIdentity().name(), kTestAppAName);
}

// Ask the target application to attempt to connect to a third application
// provided by a package whose id is permitted by the primary target's
// CapabilityFilter but whose package is not. The connection should be
// blocked and the returned title should be "uninitialized".
TEST_F(ConnectTest, BlockedPackage) {
  scoped_ptr<Connection> connection = connector()->Connect(kTestAppName);
  test::mojom::StandaloneAppPtr standalone_app;
  connection->GetInterface(&standalone_app);
  base::RunLoop run_loop;
  std::string title;
  standalone_app->ConnectToAllowedAppInBlockedPackage(
      base::Bind(&ReceiveOneString, &title, &run_loop));
  run_loop.Run();
  EXPECT_EQ("uninitialized", title);
}

// BlockedInterface should not be exposed to this application because it is not
// in our CapabilityFilter whitelist.
TEST_F(ConnectTest, PackagedApp_BlockedInterface) {
  scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName);
  base::RunLoop run_loop;
  test::mojom::BlockedInterfacePtr blocked;
  connection->GetInterface(&blocked);
  blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop));
  run_loop.Run();
}

// Connection to another application provided by the same package, blocked
// because it's not in the capability filter whitelist.
TEST_F(ConnectTest, BlockedPackagedApplication) {
  scoped_ptr<Connection> connection = connector()->Connect(kTestAppBName);
  test::mojom::ConnectTestServicePtr service_b;
  connection->GetInterface(&service_b);
  base::RunLoop run_loop;
  connection->SetConnectionLostClosure(base::Bind(&QuitLoop, &run_loop));
  run_loop.Run();
  EXPECT_FALSE(connection->IsPending());
  EXPECT_EQ(mojom::ConnectResult::ACCESS_DENIED, connection->GetResult());
  EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID());
}

TEST_F(ConnectTest, CapabilityClasses) {
  scoped_ptr<Connection> connection = connector()->Connect(kTestAppName);
  test::mojom::StandaloneAppPtr standalone_app;
  connection->GetInterface(&standalone_app);
  std::string string1, string2;
  base::RunLoop loop;
  standalone_app->ConnectToClassInterface(
      base::Bind(&ReceiveTwoStrings, &string1, &string2, &loop));
  loop.Run();
  EXPECT_EQ("PONG", string1);
  EXPECT_EQ("CLASS APP", string2);
}

TEST_F(ConnectTest, ConnectAsDifferentUser_Allowed) {
  scoped_ptr<Connection> connection = connector()->Connect(kTestAppName);
  test::mojom::UserIdTestPtr user_id_test;
  connection->GetInterface(&user_id_test);
  shell::mojom::ConnectResult result;
  Identity target(kTestClassAppName, base::GenerateGUID());
  Identity result_identity;
  {
    base::RunLoop loop;
    user_id_test->ConnectToClassAppAsDifferentUser(
        mojom::Identity::From(target),
        base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop));
    loop.Run();
  }
  EXPECT_EQ(result, shell::mojom::ConnectResult::SUCCEEDED);
  EXPECT_EQ(target, result_identity);
}

TEST_F(ConnectTest, ConnectAsDifferentUser_Blocked) {
  scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName);
  test::mojom::UserIdTestPtr user_id_test;
  connection->GetInterface(&user_id_test);
  shell::mojom::ConnectResult result;
  Identity target(kTestClassAppName, base::GenerateGUID());
  Identity result_identity;
  {
    base::RunLoop loop;
    user_id_test->ConnectToClassAppAsDifferentUser(
        mojom::Identity::From(target),
        base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop));
    loop.Run();
  }
  EXPECT_EQ(shell::mojom::ConnectResult::ACCESS_DENIED, result);
  EXPECT_FALSE(target == result_identity);
}

// There are various other tests (shell, lifecycle) that test valid client
// process specifications. This is the only one for blocking.
TEST_F(ConnectTest, ConnectToClientProcess_Blocked) {
  scoped_ptr<Connection> connection = connector()->Connect(kTestDriverName);
  test::mojom::ClientProcessTestPtr client_process_test;
  connection->GetInterface(&client_process_test);
  shell::mojom::ConnectResult result;
  Identity result_identity;
  {
    base::RunLoop loop;
    client_process_test->LaunchAndConnectToProcess(
        base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop));
    loop.Run();
  }
  EXPECT_EQ(shell::mojom::ConnectResult::ACCESS_DENIED, result);
}

// Verifies that a client with the "all_users" capability class can receive
// connections from clients run as other users.
TEST_F(ConnectTest, AllUsersSingleton) {
  // Connect to an instance with an explicitly different user_id. This supplied
  // user id should be ignored by the shell (which will generate its own
  // synthetic user id for all-user singleton instances).
  const std::string singleton_userid = base::GenerateGUID();
  Connector::ConnectParams params(
      Identity(kTestSingletonAppName, singleton_userid));
  scoped_ptr<Connection> connection = connector()->Connect(&params);
  {
    base::RunLoop loop;
    connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop));
    loop.Run();
    EXPECT_NE(connection->GetRemoteIdentity().user_id(), singleton_userid);
  }
  // This connects using the current client's user_id. It should be bound to the
  // same service started above, with the same shell-generated user id.
  scoped_ptr<Connection> inherit_connection =
      connector()->Connect(kTestSingletonAppName);
  {
    base::RunLoop loop;
    inherit_connection->AddConnectionCompletedClosure(
        base::Bind(&QuitLoop, &loop));
    loop.Run();
    EXPECT_EQ(inherit_connection->GetRemoteIdentity().user_id(),
              connection->GetRemoteIdentity().user_id());
  }
}

// Tests that we can expose an interface to targets on outbound connections.
TEST_F(ConnectTest, LocalInterface) {
  // Connect to a standalone application.
  {
    test::mojom::ConnectTestServicePtr service;
    scoped_ptr<Connection> connection = connector()->Connect(kTestAppName);
    connection->GetInterface(&service);
    connection->AddInterface<test::mojom::ExposedInterface>(this);

    uint32_t remote_id = shell::mojom::kInvalidInstanceID;
    {
      base::RunLoop run_loop;
      EXPECT_TRUE(connection->IsPending());
      EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID());
      connection->AddConnectionCompletedClosure(
          base::Bind(&QuitLoop, &run_loop));
      run_loop.Run();
      EXPECT_FALSE(connection->IsPending());
      remote_id = connection->GetRemoteInstanceID();
      EXPECT_NE(mojom::kInvalidInstanceID, remote_id);
    }

    {
      base::RunLoop run_loop;
      set_ping_callback(base::Bind(&QuitLoop, &run_loop));
      run_loop.Run();
      CompareConnectionState(
          kTestAppName, test_name(), test_userid(), test_instance_id(),
          kTestAppName, connection->GetRemoteIdentity().user_id(), remote_id);
    }
  }

  // Connect to an application provided by a package.
  {
    test::mojom::ConnectTestServicePtr service_a;
    scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName);
    connection->GetInterface(&service_a);
    connection->AddInterface<test::mojom::ExposedInterface>(this);

    uint32_t remote_id = shell::mojom::kInvalidInstanceID;
    {
      base::RunLoop run_loop;
      EXPECT_TRUE(connection->IsPending());
      EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID());
      connection->AddConnectionCompletedClosure(
          base::Bind(&QuitLoop, &run_loop));
      run_loop.Run();
      EXPECT_FALSE(connection->IsPending());
      remote_id = connection->GetRemoteInstanceID();
      EXPECT_NE(mojom::kInvalidInstanceID, remote_id);
    }

    {
      base::RunLoop run_loop;
      set_ping_callback(base::Bind(&QuitLoop, &run_loop));
      run_loop.Run();
      CompareConnectionState(
          kTestAppAName, test_name(), test_userid(), test_instance_id(),
          kTestAppAName, connection->GetRemoteIdentity().user_id(), remote_id);
    }

  }
}

}  // namespace shell
}  // namespace mojo