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

Side by Side Diff: services/service_manager/tests/connect/connect_unittest.cc

Issue 2804373002: Eliminate Connector::Connect(), Connection, etc. (Closed)
Patch Set: . Created 3 years, 8 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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/guid.h" 12 #include "base/guid.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/process/process.h" 15 #include "base/process/process.h"
16 #include "base/run_loop.h" 16 #include "base/run_loop.h"
17 #include "base/test/test_suite.h" 17 #include "base/test/test_suite.h"
18 #include "mojo/public/cpp/bindings/binding_set.h" 18 #include "mojo/public/cpp/bindings/binding_set.h"
19 #include "services/service_manager/public/cpp/binder_registry.h"
19 #include "services/service_manager/public/cpp/interface_factory.h" 20 #include "services/service_manager/public/cpp/interface_factory.h"
20 #include "services/service_manager/public/cpp/interface_registry.h"
21 #include "services/service_manager/public/cpp/service_test.h" 21 #include "services/service_manager/public/cpp/service_test.h"
22 #include "services/service_manager/public/interfaces/service_manager.mojom.h" 22 #include "services/service_manager/public/interfaces/service_manager.mojom.h"
23 #include "services/service_manager/tests/connect/connect_test.mojom.h" 23 #include "services/service_manager/tests/connect/connect_test.mojom.h"
24 #include "services/service_manager/tests/util.h" 24 #include "services/service_manager/tests/util.h"
25 25
26 // Tests that multiple services can be packaged in a single service by 26 // Tests that multiple services can be packaged in a single service by
27 // implementing ServiceFactory; that these services can be specified by 27 // implementing ServiceFactory; that these services can be specified by
28 // the package's manifest and are thus registered with the PackageManager. 28 // the package's manifest and are thus registered with the PackageManager.
29 29
30 namespace service_manager { 30 namespace service_manager {
(...skipping 27 matching lines...) Expand all
58 void ReceiveConnectionResult(mojom::ConnectResult* out_result, 58 void ReceiveConnectionResult(mojom::ConnectResult* out_result,
59 Identity* out_target, 59 Identity* out_target,
60 base::RunLoop* loop, 60 base::RunLoop* loop,
61 int32_t in_result, 61 int32_t in_result,
62 const service_manager::Identity& in_identity) { 62 const service_manager::Identity& in_identity) {
63 *out_result = static_cast<mojom::ConnectResult>(in_result); 63 *out_result = static_cast<mojom::ConnectResult>(in_result);
64 *out_target = in_identity; 64 *out_target = in_identity;
65 loop->Quit(); 65 loop->Quit();
66 } 66 }
67 67
68 void StartServiceResponse(base::RunLoop* quit_loop,
69 mojom::ConnectResult* out_result,
70 Identity* out_resolved_identity,
71 mojom::ConnectResult result,
72 const Identity& resolved_identity) {
73 if (quit_loop)
74 quit_loop->Quit();
75 if (out_result)
76 *out_result = result;
77 if (out_resolved_identity)
78 *out_resolved_identity = resolved_identity;
79 }
80
68 void QuitLoop(base::RunLoop* loop) { 81 void QuitLoop(base::RunLoop* loop) {
69 loop->Quit(); 82 loop->Quit();
70 } 83 }
71 84
72 } // namespace 85 } // namespace
73 86
74 class ConnectTest : public test::ServiceTest, 87 class ConnectTest : public test::ServiceTest,
75 public InterfaceFactory<test::mojom::ExposedInterface>, 88 public InterfaceFactory<test::mojom::ExposedInterface>,
76 public test::mojom::ExposedInterface { 89 public test::mojom::ExposedInterface {
77 public: 90 public:
78 ConnectTest() : ServiceTest("connect_unittests") {} 91 ConnectTest() : ServiceTest("connect_unittests") {}
79 ~ConnectTest() override {} 92 ~ConnectTest() override {}
80 93
81 protected: 94 protected:
82 std::unique_ptr<Connection> ConnectTo(const Identity& target) {
83 std::unique_ptr<Connection> connection = connector()->Connect(target);
84 base::RunLoop loop;
85 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop));
86 loop.Run();
87 return connection;
88 }
89
90 void CompareConnectionState( 95 void CompareConnectionState(
91 const std::string& connection_local_name, 96 const std::string& connection_local_name,
92 const std::string& connection_remote_name, 97 const std::string& connection_remote_name,
93 const std::string& connection_remote_userid, 98 const std::string& connection_remote_userid,
94 const std::string& initialize_local_name, 99 const std::string& initialize_local_name,
95 const std::string& initialize_userid) { 100 const std::string& initialize_userid) {
96 EXPECT_EQ(connection_remote_name, 101 EXPECT_EQ(connection_remote_name,
97 connection_state_->connection_remote_name); 102 connection_state_->connection_remote_name);
98 EXPECT_EQ(connection_remote_userid, 103 EXPECT_EQ(connection_remote_userid,
99 connection_state_->connection_remote_userid); 104 connection_state_->connection_remote_userid);
100 EXPECT_EQ(initialize_local_name, connection_state_->initialize_local_name); 105 EXPECT_EQ(initialize_local_name, connection_state_->initialize_local_name);
101 EXPECT_EQ(initialize_userid, connection_state_->initialize_userid); 106 EXPECT_EQ(initialize_userid, connection_state_->initialize_userid);
102 } 107 }
103 108
104 private: 109 private:
105 class TestService : public test::ServiceTestClient { 110 class TestService : public test::ServiceTestClient {
106 public: 111 public:
107 explicit TestService(ConnectTest* connect_test) 112 explicit TestService(ConnectTest* connect_test)
108 : test::ServiceTestClient(connect_test), 113 : test::ServiceTestClient(connect_test), connect_test_(connect_test) {
109 connect_test_(connect_test) {} 114 registry_.AddInterface<test::mojom::ExposedInterface>(connect_test_);
115 }
110 ~TestService() override {} 116 ~TestService() override {}
111 117
112 private: 118 private:
113 bool OnConnect(const ServiceInfo& remote_info, 119 void OnBindInterface(
114 InterfaceRegistry* registry) override { 120 const ServiceInfo& source_info,
115 registry->AddInterface<test::mojom::ExposedInterface>(connect_test_); 121 const std::string& interface_name,
116 return true; 122 mojo::ScopedMessagePipeHandle interface_pipe) override {
123 registry_.BindInterface(source_info.identity, interface_name,
124 std::move(interface_pipe));
117 } 125 }
118 126
119 ConnectTest* connect_test_; 127 ConnectTest* connect_test_;
128 BinderRegistry registry_;
120 129
121 DISALLOW_COPY_AND_ASSIGN(TestService); 130 DISALLOW_COPY_AND_ASSIGN(TestService);
122 }; 131 };
123 132
124 // test::ServiceTest: 133 // test::ServiceTest:
125 void SetUp() override { 134 void SetUp() override {
126 test::ServiceTest::SetUp(); 135 test::ServiceTest::SetUp();
127 // We need to connect to the package first to force the service manager to 136 // We need to connect to the package first to force the service manager to
128 // read the 137 // read the
129 // package app's manifest and register aliases for the applications it 138 // package app's manifest and register aliases for the applications it
130 // provides. 139 // provides.
131 test::mojom::ConnectTestServicePtr root_service; 140 test::mojom::ConnectTestServicePtr root_service;
132 std::unique_ptr<Connection> connection = 141 connector()->BindInterface(kTestPackageName, &root_service);
133 connector()->Connect(kTestPackageName);
134 connection->GetInterface(&root_service);
135 base::RunLoop run_loop; 142 base::RunLoop run_loop;
136 std::string root_name; 143 std::string root_name;
137 root_service->GetTitle( 144 root_service->GetTitle(
138 base::Bind(&ReceiveOneString, &root_name, &run_loop)); 145 base::Bind(&ReceiveOneString, &root_name, &run_loop));
139 run_loop.Run(); 146 run_loop.Run();
140 } 147 }
141 std::unique_ptr<Service> CreateService() override { 148 std::unique_ptr<Service> CreateService() override {
142 return base::MakeUnique<TestService>(this); 149 return base::MakeUnique<TestService>(this);
143 } 150 }
144 151
145 // InterfaceFactory<test::mojom::ExposedInterface>: 152 // InterfaceFactory<test::mojom::ExposedInterface>:
146 void Create(const Identity& remote_identity, 153 void Create(const Identity& remote_identity,
147 test::mojom::ExposedInterfaceRequest request) override { 154 test::mojom::ExposedInterfaceRequest request) override {
148 bindings_.AddBinding(this, std::move(request)); 155 bindings_.AddBinding(this, std::move(request));
149 } 156 }
150 157
151 void ConnectionAccepted(test::mojom::ConnectionStatePtr state) override { 158 void ConnectionAccepted(test::mojom::ConnectionStatePtr state) override {
152 connection_state_ = std::move(state); 159 connection_state_ = std::move(state);
153 } 160 }
154 161
155 test::mojom::ConnectionStatePtr connection_state_; 162 test::mojom::ConnectionStatePtr connection_state_;
156 163
157 mojo::BindingSet<test::mojom::ExposedInterface> bindings_; 164 mojo::BindingSet<test::mojom::ExposedInterface> bindings_;
158 165
159 DISALLOW_COPY_AND_ASSIGN(ConnectTest); 166 DISALLOW_COPY_AND_ASSIGN(ConnectTest);
160 }; 167 };
161 168
162 // Ensure the connection was properly established and that a round trip 169 // Ensure the connection was properly established and that a round trip
163 // method call/response is completed. 170 // method call/response is completed.
164 TEST_F(ConnectTest, Connect) { 171 TEST_F(ConnectTest, BindInterface) {
165 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName);
166 test::mojom::ConnectTestServicePtr service; 172 test::mojom::ConnectTestServicePtr service;
167 connection->GetInterface(&service); 173 connector()->BindInterface(kTestAppName, &service);
168 base::RunLoop run_loop; 174 base::RunLoop run_loop;
169 std::string title; 175 std::string title;
170 service->GetTitle(base::Bind(&ReceiveOneString, &title, &run_loop)); 176 service->GetTitle(base::Bind(&ReceiveOneString, &title, &run_loop));
171 run_loop.Run(); 177 run_loop.Run();
172 EXPECT_EQ("APP", title); 178 EXPECT_EQ("APP", title);
173 EXPECT_FALSE(connection->IsPending());
174 EXPECT_EQ(connection->GetRemoteIdentity().name(), kTestAppName);
175 } 179 }
176 180
177 TEST_F(ConnectTest, Instances) { 181 TEST_F(ConnectTest, Instances) {
178 Identity identity_a(kTestAppName, mojom::kInheritUserID, "A"); 182 Identity identity_a(kTestAppName, mojom::kInheritUserID, "A");
179 std::unique_ptr<Connection> connection_a1 = ConnectTo(identity_a);
180 std::unique_ptr<Connection> connection_a2 = ConnectTo(identity_a);
181 std::string instance_a1, instance_a2; 183 std::string instance_a1, instance_a2;
182 test::mojom::ConnectTestServicePtr service_a1; 184 test::mojom::ConnectTestServicePtr service_a1;
183 { 185 {
184 connection_a1->GetInterface(&service_a1); 186 connector()->BindInterface(identity_a, &service_a1);
185 base::RunLoop loop; 187 base::RunLoop loop;
186 service_a1->GetInstance(base::Bind(&ReceiveOneString, &instance_a1, &loop)); 188 service_a1->GetInstance(base::Bind(&ReceiveOneString, &instance_a1, &loop));
187 loop.Run(); 189 loop.Run();
188 } 190 }
189 test::mojom::ConnectTestServicePtr service_a2; 191 test::mojom::ConnectTestServicePtr service_a2;
190 { 192 {
191 connection_a2->GetInterface(&service_a2); 193 connector()->BindInterface(identity_a, &service_a2);
192 base::RunLoop loop; 194 base::RunLoop loop;
193 service_a2->GetInstance(base::Bind(&ReceiveOneString, &instance_a2, &loop)); 195 service_a2->GetInstance(base::Bind(&ReceiveOneString, &instance_a2, &loop));
194 loop.Run(); 196 loop.Run();
195 } 197 }
196 EXPECT_EQ(instance_a1, instance_a2); 198 EXPECT_EQ(instance_a1, instance_a2);
197 199
198 Identity identity_b(kTestAppName, mojom::kInheritUserID, "B"); 200 Identity identity_b(kTestAppName, mojom::kInheritUserID, "B");
199 std::unique_ptr<Connection> connection_b = ConnectTo(identity_b);
200 std::string instance_b; 201 std::string instance_b;
201 test::mojom::ConnectTestServicePtr service_b; 202 test::mojom::ConnectTestServicePtr service_b;
202 { 203 {
203 connection_b->GetInterface(&service_b); 204 connector()->BindInterface(identity_b, &service_b);
204 base::RunLoop loop; 205 base::RunLoop loop;
205 service_b->GetInstance(base::Bind(&ReceiveOneString, &instance_b, &loop)); 206 service_b->GetInstance(base::Bind(&ReceiveOneString, &instance_b, &loop));
206 loop.Run(); 207 loop.Run();
207 } 208 }
208 209
209 EXPECT_NE(instance_a1, instance_b); 210 EXPECT_NE(instance_a1, instance_b);
210 } 211 }
211 212
212 // BlockedInterface should not be exposed to this application because it is not 213 // BlockedInterface should not be exposed to this application because it is not
213 // in our CapabilityFilter whitelist. 214 // in our CapabilityFilter whitelist.
214 TEST_F(ConnectTest, BlockedInterface) { 215 TEST_F(ConnectTest, BlockedInterface) {
215 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName);
216 base::RunLoop run_loop; 216 base::RunLoop run_loop;
217 test::mojom::BlockedInterfacePtr blocked; 217 test::mojom::BlockedInterfacePtr blocked;
218 connection->GetInterface(&blocked); 218 connector()->BindInterface(kTestAppName, &blocked);
219 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop)); 219 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop));
220 std::string title = "unchanged"; 220 std::string title = "unchanged";
221 blocked->GetTitleBlocked(base::Bind(&ReceiveOneString, &title, &run_loop)); 221 blocked->GetTitleBlocked(base::Bind(&ReceiveOneString, &title, &run_loop));
222 run_loop.Run(); 222 run_loop.Run();
223 EXPECT_EQ("unchanged", title); 223 EXPECT_EQ("unchanged", title);
224 } 224 }
225 225
226 // Connects to an app provided by a package. 226 // Connects to an app provided by a package.
227 TEST_F(ConnectTest, PackagedApp) { 227 TEST_F(ConnectTest, PackagedApp) {
228 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppAName);
229 test::mojom::ConnectTestServicePtr service_a; 228 test::mojom::ConnectTestServicePtr service_a;
230 connection->GetInterface(&service_a); 229 connector()->BindInterface(kTestAppAName, &service_a);
230 Connector::TestApi test_api(connector());
231 Identity resolved_identity;
232 test_api.SetStartServiceCallback(
233 base::Bind(&StartServiceResponse, nullptr, nullptr, &resolved_identity));
231 base::RunLoop run_loop; 234 base::RunLoop run_loop;
232 std::string a_name; 235 std::string a_name;
233 service_a->GetTitle(base::Bind(&ReceiveOneString, &a_name, &run_loop)); 236 service_a->GetTitle(base::Bind(&ReceiveOneString, &a_name, &run_loop));
234 run_loop.Run(); 237 run_loop.Run();
235 EXPECT_EQ("A", a_name); 238 EXPECT_EQ("A", a_name);
236 EXPECT_FALSE(connection->IsPending()); 239 EXPECT_EQ(resolved_identity.name(), kTestAppAName);
237 EXPECT_EQ(connection->GetRemoteIdentity().name(), kTestAppAName);
238 } 240 }
239 241
240 // Ask the target application to attempt to connect to a third application 242 // Ask the target application to attempt to connect to a third application
241 // provided by a package whose id is permitted by the primary target's 243 // provided by a package whose id is permitted by the primary target's
242 // CapabilityFilter but whose package is not. The connection should be 244 // CapabilityFilter but whose package is not. The connection should be
243 // allowed regardless of the target's CapabilityFilter with respect to the 245 // allowed regardless of the target's CapabilityFilter with respect to the
244 // package. 246 // package.
245 TEST_F(ConnectTest, BlockedPackage) { 247 TEST_F(ConnectTest, BlockedPackage) {
246 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName);
247 test::mojom::StandaloneAppPtr standalone_app; 248 test::mojom::StandaloneAppPtr standalone_app;
248 connection->GetInterface(&standalone_app); 249 connector()->BindInterface(kTestAppName, &standalone_app);
249 base::RunLoop run_loop; 250 base::RunLoop run_loop;
250 std::string title; 251 std::string title;
251 standalone_app->ConnectToAllowedAppInBlockedPackage( 252 standalone_app->ConnectToAllowedAppInBlockedPackage(
252 base::Bind(&ReceiveOneString, &title, &run_loop)); 253 base::Bind(&ReceiveOneString, &title, &run_loop));
253 run_loop.Run(); 254 run_loop.Run();
254 EXPECT_EQ("A", title); 255 EXPECT_EQ("A", title);
255 } 256 }
256 257
257 // BlockedInterface should not be exposed to this application because it is not 258 // BlockedInterface should not be exposed to this application because it is not
258 // in our CapabilityFilter whitelist. 259 // in our CapabilityFilter whitelist.
259 TEST_F(ConnectTest, PackagedApp_BlockedInterface) { 260 TEST_F(ConnectTest, PackagedApp_BlockedInterface) {
260 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppAName);
261 base::RunLoop run_loop; 261 base::RunLoop run_loop;
262 test::mojom::BlockedInterfacePtr blocked; 262 test::mojom::BlockedInterfacePtr blocked;
263 connection->GetInterface(&blocked); 263 connector()->BindInterface(kTestAppAName, &blocked);
264 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop)); 264 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop));
265 run_loop.Run(); 265 run_loop.Run();
266 } 266 }
267 267
268 // Connection to another application provided by the same package, blocked 268 // Connection to another application provided by the same package, blocked
269 // because it's not in the capability filter whitelist. 269 // because it's not in the capability filter whitelist.
270 TEST_F(ConnectTest, BlockedPackagedApplication) { 270 TEST_F(ConnectTest, BlockedPackagedApplication) {
271 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppBName);
272 test::mojom::ConnectTestServicePtr service_b; 271 test::mojom::ConnectTestServicePtr service_b;
273 connection->GetInterface(&service_b); 272 connector()->BindInterface(kTestAppBName, &service_b);
273 Connector::TestApi test_api(connector());
274 mojom::ConnectResult result;
275 test_api.SetStartServiceCallback(
276 base::Bind(&StartServiceResponse, nullptr, &result, nullptr));
274 base::RunLoop run_loop; 277 base::RunLoop run_loop;
275 connection->SetConnectionLostClosure(base::Bind(&QuitLoop, &run_loop)); 278 service_b.set_connection_error_handler(run_loop.QuitClosure());
276 run_loop.Run(); 279 run_loop.Run();
277 EXPECT_FALSE(connection->IsPending()); 280 EXPECT_EQ(mojom::ConnectResult::ACCESS_DENIED, result);
278 EXPECT_EQ(mojom::ConnectResult::ACCESS_DENIED, connection->GetResult());
279 } 281 }
280 282
281 TEST_F(ConnectTest, CapabilityClasses) { 283 TEST_F(ConnectTest, CapabilityClasses) {
282 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName);
283 test::mojom::StandaloneAppPtr standalone_app; 284 test::mojom::StandaloneAppPtr standalone_app;
284 connection->GetInterface(&standalone_app); 285 connector()->BindInterface(kTestAppName, &standalone_app);
285 std::string string1, string2; 286 std::string string1, string2;
286 base::RunLoop loop; 287 base::RunLoop loop;
287 standalone_app->ConnectToClassInterface( 288 standalone_app->ConnectToClassInterface(
288 base::Bind(&ReceiveTwoStrings, &string1, &string2, &loop)); 289 base::Bind(&ReceiveTwoStrings, &string1, &string2, &loop));
289 loop.Run(); 290 loop.Run();
290 EXPECT_EQ("PONG", string1); 291 EXPECT_EQ("PONG", string1);
291 EXPECT_EQ("CLASS APP", string2); 292 EXPECT_EQ("CLASS APP", string2);
292 } 293 }
293 294
294 TEST_F(ConnectTest, ConnectWithoutExplicitClassBlocked) { 295 TEST_F(ConnectTest, ConnectWithoutExplicitClassBlocked) {
295 // We not be able to bind a ClassInterfacePtr since the connect_unittest app 296 // We not be able to bind a ClassInterfacePtr since the connect_unittest app
296 // does not explicitly request the "class" capability from 297 // does not explicitly request the "class" capability from
297 // connect_test_class_app. This test will hang if it is bound. 298 // connect_test_class_app. This test will hang if it is bound.
298 std::unique_ptr<Connection> connection =
299 connector()->Connect(kTestClassAppName);
300 test::mojom::ClassInterfacePtr class_interface; 299 test::mojom::ClassInterfacePtr class_interface;
301 connection->GetInterface(&class_interface); 300 connector()->BindInterface(kTestClassAppName, &class_interface);
302 base::RunLoop loop; 301 base::RunLoop loop;
303 class_interface.set_connection_error_handler(base::Bind(&QuitLoop, &loop)); 302 class_interface.set_connection_error_handler(base::Bind(&QuitLoop, &loop));
304 loop.Run(); 303 loop.Run();
305 } 304 }
306 305
307 TEST_F(ConnectTest, ConnectAsDifferentUser_Allowed) { 306 TEST_F(ConnectTest, ConnectAsDifferentUser_Allowed) {
308 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName);
309 test::mojom::UserIdTestPtr user_id_test; 307 test::mojom::UserIdTestPtr user_id_test;
310 connection->GetInterface(&user_id_test); 308 connector()->BindInterface(kTestAppName, &user_id_test);
311 mojom::ConnectResult result; 309 mojom::ConnectResult result;
312 Identity target(kTestClassAppName, base::GenerateGUID()); 310 Identity target(kTestClassAppName, base::GenerateGUID());
313 Identity result_identity; 311 Identity result_identity;
314 { 312 {
315 base::RunLoop loop; 313 base::RunLoop loop;
316 user_id_test->ConnectToClassAppAsDifferentUser( 314 user_id_test->ConnectToClassAppAsDifferentUser(
317 target, 315 target,
318 base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop)); 316 base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop));
319 loop.Run(); 317 loop.Run();
320 } 318 }
321 EXPECT_EQ(result, mojom::ConnectResult::SUCCEEDED); 319 EXPECT_EQ(result, mojom::ConnectResult::SUCCEEDED);
322 EXPECT_EQ(target, result_identity); 320 EXPECT_EQ(target, result_identity);
323 } 321 }
324 322
325 TEST_F(ConnectTest, ConnectAsDifferentUser_Blocked) { 323 TEST_F(ConnectTest, ConnectAsDifferentUser_Blocked) {
326 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppAName);
327 test::mojom::UserIdTestPtr user_id_test; 324 test::mojom::UserIdTestPtr user_id_test;
328 connection->GetInterface(&user_id_test); 325 connector()->BindInterface(kTestAppAName, &user_id_test);
329 mojom::ConnectResult result; 326 mojom::ConnectResult result;
330 Identity target(kTestClassAppName, base::GenerateGUID()); 327 Identity target(kTestClassAppName, base::GenerateGUID());
331 Identity result_identity; 328 Identity result_identity;
332 { 329 {
333 base::RunLoop loop; 330 base::RunLoop loop;
334 user_id_test->ConnectToClassAppAsDifferentUser( 331 user_id_test->ConnectToClassAppAsDifferentUser(
335 target, 332 target,
336 base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop)); 333 base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop));
337 loop.Run(); 334 loop.Run();
338 } 335 }
339 EXPECT_EQ(mojom::ConnectResult::ACCESS_DENIED, result); 336 EXPECT_EQ(mojom::ConnectResult::ACCESS_DENIED, result);
340 EXPECT_FALSE(target == result_identity); 337 EXPECT_FALSE(target == result_identity);
341 } 338 }
342 339
343 // There are various other tests (service manager, lifecycle) that test valid 340 // There are various other tests (service manager, lifecycle) that test valid
344 // client 341 // client
345 // process specifications. This is the only one for blocking. 342 // process specifications. This is the only one for blocking.
346 TEST_F(ConnectTest, ConnectToClientProcess_Blocked) { 343 TEST_F(ConnectTest, ConnectToClientProcess_Blocked) {
347 base::Process process; 344 base::Process process;
348 std::unique_ptr<service_manager::Connection> connection = 345 mojom::ConnectResult result =
349 service_manager::test::LaunchAndConnectToProcess( 346 service_manager::test::LaunchAndConnectToProcess(
350 #if defined(OS_WIN) 347 #if defined(OS_WIN)
351 "connect_test_exe.exe", 348 "connect_test_exe.exe",
352 #else 349 #else
353 "connect_test_exe", 350 "connect_test_exe",
354 #endif 351 #endif
355 service_manager::Identity("connect_test_exe", 352 service_manager::Identity("connect_test_exe",
356 service_manager::mojom::kInheritUserID), 353 service_manager::mojom::kInheritUserID),
357 connector(), &process); 354 connector(), &process);
358 EXPECT_EQ(connection->GetResult(), mojom::ConnectResult::ACCESS_DENIED); 355 EXPECT_EQ(result, mojom::ConnectResult::ACCESS_DENIED);
359 } 356 }
360 357
361 // Verifies that a client with the "all_users" capability class can receive 358 // Verifies that a client with the "all_users" capability class can receive
362 // connections from clients run as other users. 359 // connections from clients run as other users.
363 TEST_F(ConnectTest, AllUsersSingleton) { 360 TEST_F(ConnectTest, AllUsersSingleton) {
364 // Connect to an instance with an explicitly different user_id. This supplied 361 // Connect to an instance with an explicitly different user_id. This supplied
365 // user id should be ignored by the service manager (which will generate its 362 // user id should be ignored by the service manager (which will generate its
366 // own 363 // own
367 // synthetic user id for all-user singleton instances). 364 // synthetic user id for all-user singleton instances).
368 const std::string singleton_userid = base::GenerateGUID(); 365 const std::string singleton_userid = base::GenerateGUID();
369 Identity singleton_id(kTestSingletonAppName, singleton_userid); 366 Identity singleton_id(kTestSingletonAppName, singleton_userid);
370 std::unique_ptr<Connection> connection = connector()->Connect(singleton_id); 367 connector()->StartService(singleton_id);
368 Identity first_resolved_identity;
371 { 369 {
372 base::RunLoop loop; 370 base::RunLoop loop;
373 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); 371 Connector::TestApi test_api(connector());
372 test_api.SetStartServiceCallback(base::Bind(
373 &StartServiceResponse, &loop, nullptr, &first_resolved_identity));
374 loop.Run(); 374 loop.Run();
375 EXPECT_NE(connection->GetRemoteIdentity().user_id(), singleton_userid); 375 EXPECT_NE(first_resolved_identity.user_id(), singleton_userid);
376 } 376 }
377 // This connects using the current client's user_id. It should be bound to the 377 // This connects using the current client's user_id. It should be bound to the
378 // same service started above, with the same service manager-generated user 378 // same service started above, with the same service manager-generated user
379 // id. 379 // id.
380 std::unique_ptr<Connection> inherit_connection = 380 connector()->StartService(kTestSingletonAppName);
381 connector()->Connect(kTestSingletonAppName);
382 { 381 {
383 base::RunLoop loop; 382 base::RunLoop loop;
384 inherit_connection->AddConnectionCompletedClosure( 383 Connector::TestApi test_api(connector());
385 base::Bind(&QuitLoop, &loop)); 384 Identity resolved_identity;
385 test_api.SetStartServiceCallback(
386 base::Bind(&StartServiceResponse, &loop, nullptr, &resolved_identity));
386 loop.Run(); 387 loop.Run();
387 EXPECT_EQ(inherit_connection->GetRemoteIdentity().user_id(), 388 EXPECT_EQ(resolved_identity.user_id(), first_resolved_identity.user_id());
388 connection->GetRemoteIdentity().user_id());
389 } 389 }
390 } 390 }
391 391
392 } // namespace service_manager 392 } // namespace service_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698