Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1018)

Side by Side Diff: mojo/shell/tests/connect/connect_unittest.cc

Issue 1758633002: Convert Connect apptest to a unittest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@25tests
Patch Set: . Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <signal.h>
5 #include <stddef.h> 6 #include <stddef.h>
6 #include <stdint.h> 7 #include <stdint.h>
7 8
8 #include <utility> 9 #include <utility>
9 10
10 #include "base/bind.h" 11 #include "base/bind.h"
11 #include "base/macros.h" 12 #include "base/macros.h"
12 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/test/launcher/unit_test_launcher.h"
15 #include "base/test/test_suite.h"
16 #include "mojo/edk/embedder/embedder.h"
13 #include "mojo/public/cpp/bindings/binding_set.h" 17 #include "mojo/public/cpp/bindings/binding_set.h"
14 #include "mojo/shell/public/cpp/application_test_base.h" 18 #include "mojo/shell/background/background_shell_main.h"
19 #include "mojo/shell/public/cpp/shell_test.h"
15 #include "mojo/shell/public/interfaces/application_manager.mojom.h" 20 #include "mojo/shell/public/interfaces/application_manager.mojom.h"
16 #include "mojo/shell/tests/connect/connect_test.mojom.h" 21 #include "mojo/shell/tests/connect/connect_test.mojom.h"
17 22
18 // Tests that multiple applications can be packaged in a single Mojo application 23 // Tests that multiple applications can be packaged in a single Mojo application
19 // implementing ShellClientFactory; that these applications can be specified by 24 // implementing ShellClientFactory; that these applications can be specified by
20 // the package's manifest and are thus registered with the PackageManager. 25 // the package's manifest and are thus registered with the PackageManager.
21 26
22 namespace mojo { 27 namespace mojo {
23 namespace shell { 28 namespace shell {
24 namespace { 29 namespace {
25 const char kTestPackageName[] = "mojo:connect_test_package"; 30 const char kTestPackageName[] = "mojo:connect_test_package";
26 const char kTestAppName[] = "mojo:connect_test_app"; 31 const char kTestAppName[] = "mojo:connect_test_app";
27 const char kTestAppAName[] = "mojo:connect_test_a"; 32 const char kTestAppAName[] = "mojo:connect_test_a";
28 const char kTestAppBName[] = "mojo:connect_test_b"; 33 const char kTestAppBName[] = "mojo:connect_test_b";
29 34
30 void ReceiveTitle(std::string* out_name, 35 void ReceiveTitle(std::string* out_name,
31 base::RunLoop* loop, 36 base::RunLoop* loop,
32 const String& name) { 37 const String& name) {
33 *out_name = name; 38 *out_name = name;
34 loop->Quit(); 39 loop->Quit();
35 } 40 }
36 41
37 void QuitLoop(base::RunLoop* loop) { 42 void QuitLoop(base::RunLoop* loop) {
38 loop->Quit(); 43 loop->Quit();
39 } 44 }
40 45
41 } // namespace 46 } // namespace
42 47
43 class ConnectApptest : public mojo::test::ApplicationTestBase, 48 class ConnectTest : public mojo::test::ShellTest,
44 public InterfaceFactory<test::mojom::ExposedInterface>, 49 public InterfaceFactory<test::mojom::ExposedInterface>,
45 public test::mojom::ExposedInterface { 50 public test::mojom::ExposedInterface {
46 public: 51 public:
47 ConnectApptest() {} 52 ConnectTest() : ShellTest("mojo:connect_unittests") {}
48 ~ConnectApptest() override {} 53 ~ConnectTest() override {}
49 54
50 protected: 55 protected:
51 void set_ping_callback(const base::Closure& callback) { 56 void set_ping_callback(const base::Closure& callback) {
52 ping_callback_ = callback; 57 ping_callback_ = callback;
53 } 58 }
54 59
55 void CompareConnectionState( 60 void CompareConnectionState(
56 const std::string& connection_local_name, 61 const std::string& connection_local_name,
57 const std::string& connection_remote_name, 62 const std::string& connection_remote_name,
58 uint32_t connection_remote_userid, 63 uint32_t connection_remote_userid,
59 uint32_t connection_remote_id, 64 uint32_t connection_remote_id,
60 const std::string& initialize_local_name, 65 const std::string& initialize_local_name,
61 uint32_t initialize_id, 66 uint32_t initialize_id,
62 uint32_t initialize_userid) { 67 uint32_t initialize_userid) {
63 EXPECT_EQ(connection_local_name, connection_state_->connection_local_name); 68 EXPECT_EQ(connection_local_name, connection_state_->connection_local_name);
64 EXPECT_EQ(connection_remote_name, 69 EXPECT_EQ(connection_remote_name,
65 connection_state_->connection_remote_name); 70 connection_state_->connection_remote_name);
66 EXPECT_EQ(connection_remote_userid, 71 EXPECT_EQ(connection_remote_userid,
67 connection_state_->connection_remote_userid); 72 connection_state_->connection_remote_userid);
68 EXPECT_EQ(connection_remote_id, connection_state_->connection_remote_id); 73 EXPECT_EQ(connection_remote_id, connection_state_->connection_remote_id);
69 EXPECT_EQ(initialize_local_name, connection_state_->initialize_local_name); 74 EXPECT_EQ(initialize_local_name, connection_state_->initialize_local_name);
70 EXPECT_EQ(initialize_id, connection_state_->initialize_id); 75 EXPECT_EQ(initialize_id, connection_state_->initialize_id);
71 EXPECT_EQ(initialize_userid, connection_state_->initialize_userid); 76 EXPECT_EQ(initialize_userid, connection_state_->initialize_userid);
72 } 77 }
73 78
74 private: 79 private:
75 // mojo::test::ApplicationTestBase: 80 // mojo::test::ShellTest:
76 void SetUp() override { 81 void SetUp() override {
77 mojo::test::ApplicationTestBase::SetUp(); 82 mojo::test::ShellTest::SetUp();
78 // We need to connect to the package first to force the shell to read the 83 // We need to connect to the package first to force the shell to read the
79 // package app's manifest and register aliases for the applications it 84 // package app's manifest and register aliases for the applications it
80 // provides. 85 // provides.
81 test::mojom::ConnectTestServicePtr root_service; 86 test::mojom::ConnectTestServicePtr root_service;
82 scoped_ptr<Connection> connection = connector()->Connect(kTestPackageName); 87 scoped_ptr<Connection> connection = connector()->Connect(kTestPackageName);
83 connection->GetInterface(&root_service); 88 connection->GetInterface(&root_service);
84 base::RunLoop run_loop; 89 base::RunLoop run_loop;
85 std::string root_name; 90 std::string root_name;
86 root_service->GetTitle(base::Bind(&ReceiveTitle, &root_name, &run_loop)); 91 root_service->GetTitle(base::Bind(&ReceiveTitle, &root_name, &run_loop));
87 run_loop.Run(); 92 run_loop.Run();
88 } 93 }
89 94
90 // InterfaceFactory<test::mojom::ExposedInterface>: 95 // InterfaceFactory<test::mojom::ExposedInterface>:
91 void Create(Connection* connection, 96 void Create(Connection* connection,
92 test::mojom::ExposedInterfaceRequest request) override { 97 test::mojom::ExposedInterfaceRequest request) override {
93 bindings_.AddBinding(this, std::move(request)); 98 bindings_.AddBinding(this, std::move(request));
94 } 99 }
95 void ConnectionAccepted(test::mojom::ConnectionStatePtr state) override { 100 void ConnectionAccepted(test::mojom::ConnectionStatePtr state) override {
96 connection_state_ = std::move(state); 101 connection_state_ = std::move(state);
97 ping_callback_.Run(); 102 ping_callback_.Run();
98 } 103 }
99 104
100 base::Closure ping_callback_; 105 base::Closure ping_callback_;
101 test::mojom::ConnectionStatePtr connection_state_; 106 test::mojom::ConnectionStatePtr connection_state_;
102 107
103 BindingSet<test::mojom::ExposedInterface> bindings_; 108 BindingSet<test::mojom::ExposedInterface> bindings_;
104 109
105 DISALLOW_COPY_AND_ASSIGN(ConnectApptest); 110 DISALLOW_COPY_AND_ASSIGN(ConnectTest);
106 }; 111 };
107 112
108 // Ensure the connection was properly established and that a round trip 113 // Ensure the connection was properly established and that a round trip
109 // method call/response is completed. 114 // method call/response is completed.
110 TEST_F(ConnectApptest, Connect) { 115 TEST_F(ConnectTest, Connect) {
111 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); 116 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName);
112 test::mojom::ConnectTestServicePtr service; 117 test::mojom::ConnectTestServicePtr service;
113 connection->GetInterface(&service); 118 connection->GetInterface(&service);
114 base::RunLoop run_loop; 119 base::RunLoop run_loop;
115 std::string title; 120 std::string title;
116 service->GetTitle(base::Bind(&ReceiveTitle, &title, &run_loop)); 121 service->GetTitle(base::Bind(&ReceiveTitle, &title, &run_loop));
117 run_loop.Run(); 122 run_loop.Run();
118 EXPECT_EQ("APP", title); 123 EXPECT_EQ("APP", title);
119 uint32_t id = mojom::Connector::kInvalidApplicationID; 124 uint32_t id = mojom::Connector::kInvalidApplicationID;
120 EXPECT_TRUE(connection->GetRemoteApplicationID(&id)); 125 EXPECT_TRUE(connection->GetRemoteApplicationID(&id));
121 EXPECT_NE(id, mojom::Connector::kInvalidApplicationID); 126 EXPECT_NE(id, mojom::Connector::kInvalidApplicationID);
122 EXPECT_EQ(connection->GetRemoteApplicationName(), kTestAppName); 127 EXPECT_EQ(connection->GetRemoteApplicationName(), kTestAppName);
123 } 128 }
124 129
125 // BlockedInterface should not be exposed to this application because it is not 130 // BlockedInterface should not be exposed to this application because it is not
126 // in our CapabilityFilter whitelist. 131 // in our CapabilityFilter whitelist.
127 TEST_F(ConnectApptest, BlockedInterface) { 132 TEST_F(ConnectTest, BlockedInterface) {
128 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); 133 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName);
129 base::RunLoop run_loop; 134 base::RunLoop run_loop;
130 test::mojom::BlockedInterfacePtr blocked; 135 test::mojom::BlockedInterfacePtr blocked;
131 connection->GetInterface(&blocked); 136 connection->GetInterface(&blocked);
132 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop)); 137 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop));
133 std::string title = "unchanged"; 138 std::string title = "unchanged";
134 blocked->GetTitleBlocked(base::Bind(&ReceiveTitle, &title, &run_loop)); 139 blocked->GetTitleBlocked(base::Bind(&ReceiveTitle, &title, &run_loop));
135 run_loop.Run(); 140 run_loop.Run();
136 EXPECT_EQ("unchanged", title); 141 EXPECT_EQ("unchanged", title);
137 } 142 }
138 143
139 // Connects to an app provided by a package. 144 // Connects to an app provided by a package.
140 TEST_F(ConnectApptest, PackagedApp) { 145 TEST_F(ConnectTest, PackagedApp) {
141 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); 146 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName);
142 test::mojom::ConnectTestServicePtr service_a; 147 test::mojom::ConnectTestServicePtr service_a;
143 connection->GetInterface(&service_a); 148 connection->GetInterface(&service_a);
144 base::RunLoop run_loop; 149 base::RunLoop run_loop;
145 std::string a_name; 150 std::string a_name;
146 service_a->GetTitle(base::Bind(&ReceiveTitle, &a_name, &run_loop)); 151 service_a->GetTitle(base::Bind(&ReceiveTitle, &a_name, &run_loop));
147 run_loop.Run(); 152 run_loop.Run();
148 EXPECT_EQ("A", a_name); 153 EXPECT_EQ("A", a_name);
149 uint32_t id = mojom::Connector::kInvalidApplicationID; 154 uint32_t id = mojom::Connector::kInvalidApplicationID;
150 EXPECT_TRUE(connection->GetRemoteApplicationID(&id)); 155 EXPECT_TRUE(connection->GetRemoteApplicationID(&id));
151 EXPECT_NE(id, mojom::Connector::kInvalidApplicationID); 156 EXPECT_NE(id, mojom::Connector::kInvalidApplicationID);
152 EXPECT_EQ(connection->GetRemoteApplicationName(), kTestAppAName); 157 EXPECT_EQ(connection->GetRemoteApplicationName(), kTestAppAName);
153 } 158 }
154 159
155 // Ask the target application to attempt to connect to a third application 160 // Ask the target application to attempt to connect to a third application
156 // provided by a package whose id is permitted by the primary target's 161 // provided by a package whose id is permitted by the primary target's
157 // CapabilityFilter but whose package is not. The connection should be 162 // CapabilityFilter but whose package is not. The connection should be
158 // blocked and the returned title should be "uninitialized". 163 // blocked and the returned title should be "uninitialized".
159 TEST_F(ConnectApptest, BlockedPackage) { 164 TEST_F(ConnectTest, BlockedPackage) {
160 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); 165 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName);
161 test::mojom::StandaloneAppPtr standalone_app; 166 test::mojom::StandaloneAppPtr standalone_app;
162 connection->GetInterface(&standalone_app); 167 connection->GetInterface(&standalone_app);
163 base::RunLoop run_loop; 168 base::RunLoop run_loop;
164 std::string title; 169 std::string title;
165 standalone_app->ConnectToAllowedAppInBlockedPackage( 170 standalone_app->ConnectToAllowedAppInBlockedPackage(
166 base::Bind(&ReceiveTitle, &title, &run_loop)); 171 base::Bind(&ReceiveTitle, &title, &run_loop));
167 run_loop.Run(); 172 run_loop.Run();
168 EXPECT_EQ("uninitialized", title); 173 EXPECT_EQ("uninitialized", title);
169 } 174 }
170 175
171 // BlockedInterface should not be exposed to this application because it is not 176 // BlockedInterface should not be exposed to this application because it is not
172 // in our CapabilityFilter whitelist. 177 // in our CapabilityFilter whitelist.
173 TEST_F(ConnectApptest, PackagedApp_BlockedInterface) { 178 TEST_F(ConnectTest, PackagedApp_BlockedInterface) {
174 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); 179 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName);
175 base::RunLoop run_loop; 180 base::RunLoop run_loop;
176 test::mojom::BlockedInterfacePtr blocked; 181 test::mojom::BlockedInterfacePtr blocked;
177 connection->GetInterface(&blocked); 182 connection->GetInterface(&blocked);
178 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop)); 183 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop));
179 run_loop.Run(); 184 run_loop.Run();
180 } 185 }
181 186
182 // Connection to another application provided by the same package, blocked 187 // Connection to another application provided by the same package, blocked
183 // because it's not in the capability filter whitelist. 188 // because it's not in the capability filter whitelist.
184 TEST_F(ConnectApptest, BlockedPackagedApplication) { 189 TEST_F(ConnectTest, BlockedPackagedApplication) {
185 scoped_ptr<Connection> connection = connector()->Connect(kTestAppBName); 190 scoped_ptr<Connection> connection = connector()->Connect(kTestAppBName);
186 test::mojom::ConnectTestServicePtr service_b; 191 test::mojom::ConnectTestServicePtr service_b;
187 connection->GetInterface(&service_b); 192 connection->GetInterface(&service_b);
188 base::RunLoop run_loop; 193 base::RunLoop run_loop;
189 connection->SetRemoteInterfaceProviderConnectionErrorHandler( 194 connection->SetRemoteInterfaceProviderConnectionErrorHandler(
190 base::Bind(&QuitLoop, &run_loop)); 195 base::Bind(&QuitLoop, &run_loop));
191 run_loop.Run(); 196 run_loop.Run();
192 uint32_t id = mojom::Connector::kInvalidApplicationID; 197 uint32_t id = mojom::Connector::kInvalidApplicationID;
193 EXPECT_TRUE(connection->GetRemoteApplicationID(&id)); 198 EXPECT_TRUE(connection->GetRemoteApplicationID(&id));
194 EXPECT_EQ(id, mojom::Connector::kInvalidApplicationID); 199 EXPECT_EQ(id, mojom::Connector::kInvalidApplicationID);
195 } 200 }
196 201
197 // Tests that we can expose an interface to targets on outbound connections. 202 // Tests that we can expose an interface to targets on outbound connections.
198 // TODO(beng): Currently all interfaces on outbound connections are exposed. 203 // TODO(beng): Currently all interfaces on outbound connections are exposed.
199 // See ConnectorImpl::Connect(). 204 // See ConnectorImpl::Connect().
200 TEST_F(ConnectApptest, LocalInterface) { 205 TEST_F(ConnectTest, LocalInterface) {
201 // Connect to a standalone application. 206 // Connect to a standalone application.
202 { 207 {
203 test::mojom::ConnectTestServicePtr service; 208 test::mojom::ConnectTestServicePtr service;
204 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); 209 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName);
205 connection->GetInterface(&service); 210 connection->GetInterface(&service);
206 connection->AddInterface<test::mojom::ExposedInterface>(this); 211 connection->AddInterface<test::mojom::ExposedInterface>(this);
207 212
208 uint32_t remote_id = mojom::Connector::kInvalidApplicationID; 213 uint32_t remote_id = mojom::Connector::kInvalidApplicationID;
209 { 214 {
210 base::RunLoop run_loop; 215 base::RunLoop run_loop;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 CompareConnectionState( 256 CompareConnectionState(
252 kTestAppAName, test_name(), test_userid(), test_instance_id(), 257 kTestAppAName, test_name(), test_userid(), test_instance_id(),
253 kTestAppAName, remote_id, connection->GetRemoteUserID()); 258 kTestAppAName, remote_id, connection->GetRemoteUserID());
254 } 259 }
255 260
256 } 261 }
257 } 262 }
258 263
259 } // namespace shell 264 } // namespace shell
260 } // namespace mojo 265 } // namespace mojo
266
267 int MasterProcessMain(int argc, char** argv) {
268 base::TestSuite test_suite(argc, argv);
269 mojo::edk::Init();
270 return base::LaunchUnitTests(
271 argc, argv,
272 base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite)));
273 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698