OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/at_exit.h" | |
6 #include "base/macros.h" | 5 #include "base/macros.h" |
7 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "mojo/public/cpp/application/application_connection.h" |
| 9 #include "mojo/public/cpp/application/application_impl.h" |
| 10 #include "mojo/public/cpp/application/application_test_base.h" |
8 #include "mojo/public/cpp/bindings/callback.h" | 11 #include "mojo/public/cpp/bindings/callback.h" |
9 #include "mojo/services/network/public/cpp/udp_socket_wrapper.h" | 12 #include "mojo/services/network/public/cpp/udp_socket_wrapper.h" |
10 #include "mojo/services/network/public/interfaces/network_service.mojom.h" | 13 #include "mojo/services/network/public/interfaces/network_service.mojom.h" |
11 #include "mojo/services/network/public/interfaces/udp_socket.mojom.h" | 14 #include "mojo/services/network/public/interfaces/udp_socket.mojom.h" |
12 #include "mojo/shell/shell_test_helper.h" | |
13 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "url/gurl.h" | |
16 | 17 |
17 namespace mojo { | 18 namespace mojo { |
18 namespace service { | 19 namespace service { |
19 namespace { | 20 namespace { |
20 | 21 |
21 NetAddressPtr GetLocalHostWithAnyPort() { | 22 NetAddressPtr GetLocalHostWithAnyPort() { |
22 NetAddressPtr addr(NetAddress::New()); | 23 NetAddressPtr addr(NetAddress::New()); |
23 addr->family = NET_ADDRESS_FAMILY_IPV4; | 24 addr->family = NET_ADDRESS_FAMILY_IPV4; |
24 addr->ipv4 = NetAddressIPv4::New(); | 25 addr->ipv4 = NetAddressIPv4::New(); |
25 addr->ipv4->port = 0; | 26 addr->ipv4->port = 0; |
(...skipping 30 matching lines...) Expand all Loading... |
56 | 57 |
57 base::RunLoop run_loop; | 58 base::RunLoop run_loop; |
58 run_loop_ = &run_loop; | 59 run_loop_ = &run_loop; |
59 run_loop.Run(); | 60 run_loop.Run(); |
60 run_loop_ = nullptr; | 61 run_loop_ = nullptr; |
61 } | 62 } |
62 | 63 |
63 protected: | 64 protected: |
64 struct StateBase : public CallbackType::Runnable { | 65 struct StateBase : public CallbackType::Runnable { |
65 StateBase() : test_callback_(nullptr) {} | 66 StateBase() : test_callback_(nullptr) {} |
66 virtual ~StateBase() {} | 67 ~StateBase() override {} |
67 | 68 |
68 void set_test_callback(TestCallbackBase* test_callback) { | 69 void set_test_callback(TestCallbackBase* test_callback) { |
69 test_callback_ = test_callback; | 70 test_callback_ = test_callback; |
70 } | 71 } |
71 | 72 |
72 protected: | 73 protected: |
73 void NotifyRun() const { | 74 void NotifyRun() const { |
74 if (test_callback_) { | 75 if (test_callback_) { |
75 test_callback_->ran_ = true; | 76 test_callback_->ran_ = true; |
76 if (test_callback_->run_loop_) | 77 if (test_callback_->run_loop_) |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 } | 215 } |
215 NotifyRun(); | 216 NotifyRun(); |
216 } | 217 } |
217 }; | 218 }; |
218 | 219 |
219 NetworkErrorPtr result_; | 220 NetworkErrorPtr result_; |
220 NetAddressPtr src_addr_; | 221 NetAddressPtr src_addr_; |
221 Array<uint8_t> data_; | 222 Array<uint8_t> data_; |
222 }; | 223 }; |
223 | 224 |
224 class UDPSocketTest : public testing::Test { | 225 class UDPSocketAppTest : public test::ApplicationTestBase { |
225 public: | 226 public: |
226 UDPSocketTest() {} | 227 UDPSocketAppTest() {} |
227 virtual ~UDPSocketTest() {} | 228 ~UDPSocketAppTest() override {} |
228 | 229 |
229 virtual void SetUp() override { | 230 void SetUp() override { |
230 test_helper_.Init(); | 231 ApplicationTestBase::SetUp(); |
231 | 232 |
232 test_helper_.application_manager()->ConnectToService( | 233 ApplicationConnection* connection = |
233 GURL("mojo:network_service"), &network_service_); | 234 application_impl()->ConnectToApplication("mojo:network_service"); |
| 235 connection->ConnectToService(&network_service_); |
234 | 236 |
235 network_service_->CreateUDPSocket(GetProxy(&udp_socket_)); | 237 network_service_->CreateUDPSocket(GetProxy(&udp_socket_)); |
236 udp_socket_.set_client(&udp_socket_client_); | 238 udp_socket_.set_client(&udp_socket_client_); |
237 } | 239 } |
238 | 240 |
239 protected: | 241 protected: |
240 struct ReceiveResult { | 242 struct ReceiveResult { |
241 NetworkErrorPtr result; | 243 NetworkErrorPtr result; |
242 NetAddressPtr addr; | 244 NetAddressPtr addr; |
243 Array<uint8_t> data; | 245 Array<uint8_t> data; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 if (GetReceiveResults()->size() == count) | 288 if (GetReceiveResults()->size() == count) |
287 return; | 289 return; |
288 | 290 |
289 udp_socket_client_.expected_receive_count_ = count; | 291 udp_socket_client_.expected_receive_count_ = count; |
290 base::RunLoop run_loop; | 292 base::RunLoop run_loop; |
291 udp_socket_client_.run_loop_ = &run_loop; | 293 udp_socket_client_.run_loop_ = &run_loop; |
292 run_loop.Run(); | 294 run_loop.Run(); |
293 udp_socket_client_.run_loop_ = nullptr; | 295 udp_socket_client_.run_loop_ = nullptr; |
294 } | 296 } |
295 | 297 |
296 base::ShadowingAtExitManager at_exit_; | |
297 shell::ShellTestHelper test_helper_; | |
298 | |
299 NetworkServicePtr network_service_; | 298 NetworkServicePtr network_service_; |
300 UDPSocketPtr udp_socket_; | 299 UDPSocketPtr udp_socket_; |
301 UDPSocketClientImpl udp_socket_client_; | 300 UDPSocketClientImpl udp_socket_client_; |
302 | 301 |
303 DISALLOW_COPY_AND_ASSIGN(UDPSocketTest); | 302 DISALLOW_COPY_AND_ASSIGN(UDPSocketAppTest); |
304 }; | 303 }; |
305 | 304 |
306 } // namespace | 305 } // namespace |
307 | 306 |
308 TEST_F(UDPSocketTest, Settings) { | 307 TEST_F(UDPSocketAppTest, Settings) { |
309 TestCallback callback1; | 308 TestCallback callback1; |
310 udp_socket_->AllowAddressReuse(callback1.callback()); | 309 udp_socket_->AllowAddressReuse(callback1.callback()); |
311 callback1.WaitForResult(); | 310 callback1.WaitForResult(); |
312 EXPECT_EQ(net::OK, callback1.result()->code); | 311 EXPECT_EQ(net::OK, callback1.result()->code); |
313 | 312 |
314 // Should fail because the socket hasn't been bound. | 313 // Should fail because the socket hasn't been bound. |
315 TestCallback callback2; | 314 TestCallback callback2; |
316 udp_socket_->SetSendBufferSize(1024, callback2.callback()); | 315 udp_socket_->SetSendBufferSize(1024, callback2.callback()); |
317 callback2.WaitForResult(); | 316 callback2.WaitForResult(); |
318 EXPECT_NE(net::OK, callback2.result()->code); | 317 EXPECT_NE(net::OK, callback2.result()->code); |
(...skipping 30 matching lines...) Expand all Loading... |
349 udp_socket_->NegotiateMaxPendingSendRequests(0, callback8.callback()); | 348 udp_socket_->NegotiateMaxPendingSendRequests(0, callback8.callback()); |
350 callback8.WaitForResult(); | 349 callback8.WaitForResult(); |
351 EXPECT_GT(callback8.result(), 0u); | 350 EXPECT_GT(callback8.result(), 0u); |
352 | 351 |
353 TestCallbackWithUint32 callback9; | 352 TestCallbackWithUint32 callback9; |
354 udp_socket_->NegotiateMaxPendingSendRequests(16, callback9.callback()); | 353 udp_socket_->NegotiateMaxPendingSendRequests(16, callback9.callback()); |
355 callback9.WaitForResult(); | 354 callback9.WaitForResult(); |
356 EXPECT_GT(callback9.result(), 0u); | 355 EXPECT_GT(callback9.result(), 0u); |
357 } | 356 } |
358 | 357 |
359 TEST_F(UDPSocketTest, TestReadWrite) { | 358 TEST_F(UDPSocketAppTest, TestReadWrite) { |
360 TestCallbackWithAddress callback1; | 359 TestCallbackWithAddress callback1; |
361 udp_socket_->Bind(GetLocalHostWithAnyPort(), callback1.callback()); | 360 udp_socket_->Bind(GetLocalHostWithAnyPort(), callback1.callback()); |
362 callback1.WaitForResult(); | 361 callback1.WaitForResult(); |
363 ASSERT_EQ(net::OK, callback1.result()->code); | 362 ASSERT_EQ(net::OK, callback1.result()->code); |
364 ASSERT_NE(0u, callback1.net_address()->ipv4->port); | 363 ASSERT_NE(0u, callback1.net_address()->ipv4->port); |
365 | 364 |
366 NetAddressPtr server_addr = callback1.net_address().Clone(); | 365 NetAddressPtr server_addr = callback1.net_address().Clone(); |
367 | 366 |
368 UDPSocketPtr client_socket; | 367 UDPSocketPtr client_socket; |
369 network_service_->CreateUDPSocket(GetProxy(&client_socket)); | 368 network_service_->CreateUDPSocket(GetProxy(&client_socket)); |
(...skipping 25 matching lines...) Expand all Loading... |
395 scoped_ptr<ReceiveResult> result(GetReceiveResults()->front()); | 394 scoped_ptr<ReceiveResult> result(GetReceiveResults()->front()); |
396 GetReceiveResults()->pop(); | 395 GetReceiveResults()->pop(); |
397 | 396 |
398 EXPECT_EQ(static_cast<int>(kDatagramSize), result->result->code); | 397 EXPECT_EQ(static_cast<int>(kDatagramSize), result->result->code); |
399 EXPECT_TRUE(result->addr.Equals(client_addr)); | 398 EXPECT_TRUE(result->addr.Equals(client_addr)); |
400 EXPECT_TRUE(result->data.Equals( | 399 EXPECT_TRUE(result->data.Equals( |
401 CreateTestMessage(static_cast<uint8_t>(i), kDatagramSize))); | 400 CreateTestMessage(static_cast<uint8_t>(i), kDatagramSize))); |
402 } | 401 } |
403 } | 402 } |
404 | 403 |
405 TEST_F(UDPSocketTest, TestUDPSocketWrapper) { | 404 TEST_F(UDPSocketAppTest, TestUDPSocketWrapper) { |
406 UDPSocketWrapper udp_socket(udp_socket_.Pass(), 4, 4); | 405 UDPSocketWrapper udp_socket(udp_socket_.Pass(), 4, 4); |
407 | 406 |
408 TestCallbackWithAddress callback1; | 407 TestCallbackWithAddress callback1; |
409 udp_socket.Bind(GetLocalHostWithAnyPort(), callback1.callback()); | 408 udp_socket.Bind(GetLocalHostWithAnyPort(), callback1.callback()); |
410 callback1.WaitForResult(); | 409 callback1.WaitForResult(); |
411 ASSERT_EQ(net::OK, callback1.result()->code); | 410 ASSERT_EQ(net::OK, callback1.result()->code); |
412 ASSERT_NE(0u, callback1.net_address()->ipv4->port); | 411 ASSERT_NE(0u, callback1.net_address()->ipv4->port); |
413 | 412 |
414 NetAddressPtr server_addr = callback1.net_address().Clone(); | 413 NetAddressPtr server_addr = callback1.net_address().Clone(); |
415 | 414 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 receive_callbacks[j].result()->code); | 448 receive_callbacks[j].result()->code); |
450 EXPECT_TRUE(receive_callbacks[j].src_addr().Equals(client_addr)); | 449 EXPECT_TRUE(receive_callbacks[j].src_addr().Equals(client_addr)); |
451 EXPECT_TRUE(receive_callbacks[j].data().Equals( | 450 EXPECT_TRUE(receive_callbacks[j].data().Equals( |
452 CreateTestMessage(static_cast<uint8_t>(j), kDatagramSize))); | 451 CreateTestMessage(static_cast<uint8_t>(j), kDatagramSize))); |
453 } | 452 } |
454 } | 453 } |
455 } | 454 } |
456 | 455 |
457 } // namespace service | 456 } // namespace service |
458 } // namespace mojo | 457 } // namespace mojo |
OLD | NEW |