OLD | NEW |
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 <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 } // namespace | 42 } // namespace |
43 | 43 |
44 class ConnectTest : public mojo::test::ShellTest, | 44 class ConnectTest : public mojo::test::ShellTest, |
45 public InterfaceFactory<test::mojom::ExposedInterface>, | 45 public InterfaceFactory<test::mojom::ExposedInterface>, |
46 public test::mojom::ExposedInterface { | 46 public test::mojom::ExposedInterface { |
47 public: | 47 public: |
48 ConnectTest() : ShellTest("mojo:connect_unittests") {} | 48 ConnectTest() : ShellTest("mojo:connect_unittests") {} |
49 ~ConnectTest() override {} | 49 ~ConnectTest() override {} |
50 | 50 |
51 protected: | 51 protected: |
| 52 scoped_ptr<Connection> ConnectTo(Connector::ConnectParams* params) { |
| 53 scoped_ptr<Connection> connection = connector()->Connect(params); |
| 54 base::RunLoop loop; |
| 55 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); |
| 56 loop.Run(); |
| 57 return connection; |
| 58 } |
| 59 |
52 void set_ping_callback(const base::Closure& callback) { | 60 void set_ping_callback(const base::Closure& callback) { |
53 ping_callback_ = callback; | 61 ping_callback_ = callback; |
54 } | 62 } |
55 | 63 |
56 void CompareConnectionState( | 64 void CompareConnectionState( |
57 const std::string& connection_local_name, | 65 const std::string& connection_local_name, |
58 const std::string& connection_remote_name, | 66 const std::string& connection_remote_name, |
59 const std::string& connection_remote_userid, | 67 const std::string& connection_remote_userid, |
60 uint32_t connection_remote_id, | 68 uint32_t connection_remote_id, |
61 const std::string& initialize_local_name, | 69 const std::string& initialize_local_name, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | 120 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); |
113 test::mojom::ConnectTestServicePtr service; | 121 test::mojom::ConnectTestServicePtr service; |
114 connection->GetInterface(&service); | 122 connection->GetInterface(&service); |
115 base::RunLoop run_loop; | 123 base::RunLoop run_loop; |
116 std::string title; | 124 std::string title; |
117 service->GetTitle(base::Bind(&ReceiveTitle, &title, &run_loop)); | 125 service->GetTitle(base::Bind(&ReceiveTitle, &title, &run_loop)); |
118 run_loop.Run(); | 126 run_loop.Run(); |
119 EXPECT_EQ("APP", title); | 127 EXPECT_EQ("APP", title); |
120 EXPECT_FALSE(connection->IsPending()); | 128 EXPECT_FALSE(connection->IsPending()); |
121 EXPECT_NE(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); | 129 EXPECT_NE(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); |
122 EXPECT_EQ(connection->GetRemoteApplicationName(), kTestAppName); | 130 EXPECT_EQ(connection->GetRemoteIdentity().name(), kTestAppName); |
| 131 } |
| 132 |
| 133 TEST_F(ConnectTest, Instances) { |
| 134 Connector::ConnectParams params_a( |
| 135 Identity(kTestAppName, mojom::kInheritUserID, "A")); |
| 136 scoped_ptr<Connection> connection_a1 = ConnectTo(¶ms_a); |
| 137 scoped_ptr<Connection> connection_a2 = ConnectTo(¶ms_a); |
| 138 std::string instance_a1, instance_a2; |
| 139 test::mojom::StandaloneAppPtr standalone_app_a1; |
| 140 { |
| 141 connection_a1->GetInterface(&standalone_app_a1); |
| 142 base::RunLoop loop; |
| 143 standalone_app_a1->GetInstance( |
| 144 base::Bind(&ReceiveTitle, &instance_a1, &loop)); |
| 145 loop.Run(); |
| 146 } |
| 147 test::mojom::StandaloneAppPtr standalone_app_a2; |
| 148 { |
| 149 connection_a2->GetInterface(&standalone_app_a2); |
| 150 base::RunLoop loop; |
| 151 standalone_app_a2->GetInstance( |
| 152 base::Bind(&ReceiveTitle, &instance_a2, &loop)); |
| 153 loop.Run(); |
| 154 } |
| 155 EXPECT_EQ(instance_a1, instance_a2); |
| 156 |
| 157 Connector::ConnectParams params_b( |
| 158 Identity(kTestAppName, mojom::kInheritUserID, "B")); |
| 159 scoped_ptr<Connection> connection_b = ConnectTo(¶ms_b); |
| 160 std::string instance_b; |
| 161 test::mojom::StandaloneAppPtr standalone_app_b; |
| 162 { |
| 163 connection_b->GetInterface(&standalone_app_b); |
| 164 base::RunLoop loop; |
| 165 standalone_app_b->GetInstance( |
| 166 base::Bind(&ReceiveTitle, &instance_b, &loop)); |
| 167 loop.Run(); |
| 168 } |
| 169 |
| 170 EXPECT_NE(instance_a1, instance_b); |
123 } | 171 } |
124 | 172 |
125 // BlockedInterface should not be exposed to this application because it is not | 173 // BlockedInterface should not be exposed to this application because it is not |
126 // in our CapabilityFilter whitelist. | 174 // in our CapabilityFilter whitelist. |
127 TEST_F(ConnectTest, BlockedInterface) { | 175 TEST_F(ConnectTest, BlockedInterface) { |
128 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | 176 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); |
129 base::RunLoop run_loop; | 177 base::RunLoop run_loop; |
130 test::mojom::BlockedInterfacePtr blocked; | 178 test::mojom::BlockedInterfacePtr blocked; |
131 connection->GetInterface(&blocked); | 179 connection->GetInterface(&blocked); |
132 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop)); | 180 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop)); |
133 std::string title = "unchanged"; | 181 std::string title = "unchanged"; |
134 blocked->GetTitleBlocked(base::Bind(&ReceiveTitle, &title, &run_loop)); | 182 blocked->GetTitleBlocked(base::Bind(&ReceiveTitle, &title, &run_loop)); |
135 run_loop.Run(); | 183 run_loop.Run(); |
136 EXPECT_EQ("unchanged", title); | 184 EXPECT_EQ("unchanged", title); |
137 } | 185 } |
138 | 186 |
139 // Connects to an app provided by a package. | 187 // Connects to an app provided by a package. |
140 TEST_F(ConnectTest, PackagedApp) { | 188 TEST_F(ConnectTest, PackagedApp) { |
141 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); | 189 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); |
142 test::mojom::ConnectTestServicePtr service_a; | 190 test::mojom::ConnectTestServicePtr service_a; |
143 connection->GetInterface(&service_a); | 191 connection->GetInterface(&service_a); |
144 base::RunLoop run_loop; | 192 base::RunLoop run_loop; |
145 std::string a_name; | 193 std::string a_name; |
146 service_a->GetTitle(base::Bind(&ReceiveTitle, &a_name, &run_loop)); | 194 service_a->GetTitle(base::Bind(&ReceiveTitle, &a_name, &run_loop)); |
147 run_loop.Run(); | 195 run_loop.Run(); |
148 EXPECT_EQ("A", a_name); | 196 EXPECT_EQ("A", a_name); |
149 EXPECT_FALSE(connection->IsPending()); | 197 EXPECT_FALSE(connection->IsPending()); |
150 EXPECT_NE(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); | 198 EXPECT_NE(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); |
151 EXPECT_EQ(connection->GetRemoteApplicationName(), kTestAppAName); | 199 EXPECT_EQ(connection->GetRemoteIdentity().name(), kTestAppAName); |
152 } | 200 } |
153 | 201 |
154 // Ask the target application to attempt to connect to a third application | 202 // Ask the target application to attempt to connect to a third application |
155 // provided by a package whose id is permitted by the primary target's | 203 // provided by a package whose id is permitted by the primary target's |
156 // CapabilityFilter but whose package is not. The connection should be | 204 // CapabilityFilter but whose package is not. The connection should be |
157 // blocked and the returned title should be "uninitialized". | 205 // blocked and the returned title should be "uninitialized". |
158 TEST_F(ConnectTest, BlockedPackage) { | 206 TEST_F(ConnectTest, BlockedPackage) { |
159 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | 207 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); |
160 test::mojom::StandaloneAppPtr standalone_app; | 208 test::mojom::StandaloneAppPtr standalone_app; |
161 connection->GetInterface(&standalone_app); | 209 connection->GetInterface(&standalone_app); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 remote_id = connection->GetRemoteInstanceID(); | 263 remote_id = connection->GetRemoteInstanceID(); |
216 EXPECT_NE(mojom::kInvalidInstanceID, remote_id); | 264 EXPECT_NE(mojom::kInvalidInstanceID, remote_id); |
217 } | 265 } |
218 | 266 |
219 { | 267 { |
220 base::RunLoop run_loop; | 268 base::RunLoop run_loop; |
221 set_ping_callback(base::Bind(&QuitLoop, &run_loop)); | 269 set_ping_callback(base::Bind(&QuitLoop, &run_loop)); |
222 run_loop.Run(); | 270 run_loop.Run(); |
223 CompareConnectionState( | 271 CompareConnectionState( |
224 kTestAppName, test_name(), test_userid(), test_instance_id(), | 272 kTestAppName, test_name(), test_userid(), test_instance_id(), |
225 kTestAppName, connection->GetRemoteUserID(), remote_id); | 273 kTestAppName, connection->GetRemoteIdentity().user_id(), remote_id); |
226 } | 274 } |
227 } | 275 } |
228 | 276 |
229 // Connect to an application provided by a package. | 277 // Connect to an application provided by a package. |
230 { | 278 { |
231 test::mojom::ConnectTestServicePtr service_a; | 279 test::mojom::ConnectTestServicePtr service_a; |
232 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); | 280 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); |
233 connection->GetInterface(&service_a); | 281 connection->GetInterface(&service_a); |
234 connection->AddInterface<test::mojom::ExposedInterface>(this); | 282 connection->AddInterface<test::mojom::ExposedInterface>(this); |
235 | 283 |
236 uint32_t remote_id = shell::mojom::kInvalidInstanceID; | 284 uint32_t remote_id = shell::mojom::kInvalidInstanceID; |
237 { | 285 { |
238 base::RunLoop run_loop; | 286 base::RunLoop run_loop; |
239 EXPECT_TRUE(connection->IsPending()); | 287 EXPECT_TRUE(connection->IsPending()); |
240 EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); | 288 EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); |
241 connection->AddConnectionCompletedClosure( | 289 connection->AddConnectionCompletedClosure( |
242 base::Bind(&QuitLoop, &run_loop)); | 290 base::Bind(&QuitLoop, &run_loop)); |
243 run_loop.Run(); | 291 run_loop.Run(); |
244 EXPECT_FALSE(connection->IsPending()); | 292 EXPECT_FALSE(connection->IsPending()); |
245 remote_id = connection->GetRemoteInstanceID(); | 293 remote_id = connection->GetRemoteInstanceID(); |
246 EXPECT_NE(mojom::kInvalidInstanceID, remote_id); | 294 EXPECT_NE(mojom::kInvalidInstanceID, remote_id); |
247 } | 295 } |
248 | 296 |
249 { | 297 { |
250 base::RunLoop run_loop; | 298 base::RunLoop run_loop; |
251 set_ping_callback(base::Bind(&QuitLoop, &run_loop)); | 299 set_ping_callback(base::Bind(&QuitLoop, &run_loop)); |
252 run_loop.Run(); | 300 run_loop.Run(); |
253 CompareConnectionState( | 301 CompareConnectionState( |
254 kTestAppAName, test_name(), test_userid(), test_instance_id(), | 302 kTestAppAName, test_name(), test_userid(), test_instance_id(), |
255 kTestAppAName, connection->GetRemoteUserID(), remote_id); | 303 kTestAppAName, connection->GetRemoteIdentity().user_id(), remote_id); |
256 } | 304 } |
257 | 305 |
258 } | 306 } |
259 } | 307 } |
260 | 308 |
261 } // namespace shell | 309 } // namespace shell |
262 } // namespace mojo | 310 } // namespace mojo |
OLD | NEW |