OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/udp/udp_socket.h" | 5 #include "net/udp/udp_socket.h" |
6 | 6 |
7 #include "net/udp/udp_client_socket.h" | 7 #include "net/udp/udp_client_socket.h" |
8 #include "net/udp/udp_server_socket.h" | 8 #include "net/udp/udp_server_socket.h" |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
19 #include "net/base/ip_address.h" | 19 #include "net/base/ip_address.h" |
20 #include "net/base/ip_endpoint.h" | 20 #include "net/base/ip_endpoint.h" |
21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
22 #include "net/base/test_completion_callback.h" | 22 #include "net/base/test_completion_callback.h" |
23 #include "net/log/test_net_log.h" | 23 #include "net/log/test_net_log.h" |
24 #include "net/log/test_net_log_entry.h" | 24 #include "net/log/test_net_log_entry.h" |
25 #include "net/log/test_net_log_util.h" | 25 #include "net/log/test_net_log_util.h" |
| 26 #include "net/test/gtest_util.h" |
26 #include "net/test/net_test_suite.h" | 27 #include "net/test/net_test_suite.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
28 #include "testing/platform_test.h" | 30 #include "testing/platform_test.h" |
29 | 31 |
30 #if defined(OS_IOS) | 32 #if defined(OS_IOS) |
31 #include <TargetConditionals.h> | 33 #include <TargetConditionals.h> |
32 #endif | 34 #endif |
33 | 35 |
| 36 using net::test::IsError; |
| 37 using net::test::IsOk; |
| 38 |
34 namespace net { | 39 namespace net { |
35 | 40 |
36 namespace { | 41 namespace { |
37 | 42 |
38 class UDPSocketTest : public PlatformTest { | 43 class UDPSocketTest : public PlatformTest { |
39 public: | 44 public: |
40 UDPSocketTest() : buffer_(new IOBufferWithSize(kMaxRead)) {} | 45 UDPSocketTest() : buffer_(new IOBufferWithSize(kMaxRead)) {} |
41 | 46 |
42 // Blocks until data is read from the socket. | 47 // Blocks until data is read from the socket. |
43 std::string RecvFromSocket(UDPServerSocket* socket) { | 48 std::string RecvFromSocket(UDPServerSocket* socket) { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 CreateUDPAddress("127.0.0.1", kPort, &bind_address); | 165 CreateUDPAddress("127.0.0.1", kPort, &bind_address); |
161 TestNetLog server_log; | 166 TestNetLog server_log; |
162 std::unique_ptr<UDPServerSocket> server( | 167 std::unique_ptr<UDPServerSocket> server( |
163 new UDPServerSocket(&server_log, NetLog::Source())); | 168 new UDPServerSocket(&server_log, NetLog::Source())); |
164 #if defined(OS_WIN) | 169 #if defined(OS_WIN) |
165 if (use_nonblocking_io) | 170 if (use_nonblocking_io) |
166 server->UseNonBlockingIO(); | 171 server->UseNonBlockingIO(); |
167 #endif | 172 #endif |
168 server->AllowAddressReuse(); | 173 server->AllowAddressReuse(); |
169 int rv = server->Listen(bind_address); | 174 int rv = server->Listen(bind_address); |
170 ASSERT_EQ(OK, rv); | 175 ASSERT_THAT(rv, IsOk()); |
171 | 176 |
172 // Setup the client. | 177 // Setup the client. |
173 IPEndPoint server_address; | 178 IPEndPoint server_address; |
174 CreateUDPAddress("127.0.0.1", kPort, &server_address); | 179 CreateUDPAddress("127.0.0.1", kPort, &server_address); |
175 TestNetLog client_log; | 180 TestNetLog client_log; |
176 std::unique_ptr<UDPClientSocket> client( | 181 std::unique_ptr<UDPClientSocket> client( |
177 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | 182 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
178 &client_log, NetLog::Source())); | 183 &client_log, NetLog::Source())); |
179 #if defined(OS_WIN) | 184 #if defined(OS_WIN) |
180 if (use_nonblocking_io) | 185 if (use_nonblocking_io) |
181 client->UseNonBlockingIO(); | 186 client->UseNonBlockingIO(); |
182 #endif | 187 #endif |
183 | 188 |
184 rv = client->Connect(server_address); | 189 rv = client->Connect(server_address); |
185 EXPECT_EQ(OK, rv); | 190 EXPECT_THAT(rv, IsOk()); |
186 | 191 |
187 // Client sends to the server. | 192 // Client sends to the server. |
188 rv = WriteSocket(client.get(), simple_message); | 193 rv = WriteSocket(client.get(), simple_message); |
189 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); | 194 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
190 | 195 |
191 // Server waits for message. | 196 // Server waits for message. |
192 std::string str = RecvFromSocket(server.get()); | 197 std::string str = RecvFromSocket(server.get()); |
193 DCHECK(simple_message == str); | 198 DCHECK(simple_message == str); |
194 | 199 |
195 // Server echoes reply. | 200 // Server echoes reply. |
196 rv = SendToSocket(server.get(), simple_message); | 201 rv = SendToSocket(server.get(), simple_message); |
197 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); | 202 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
198 | 203 |
199 // Client waits for response. | 204 // Client waits for response. |
200 str = ReadSocket(client.get()); | 205 str = ReadSocket(client.get()); |
201 DCHECK(simple_message == str); | 206 DCHECK(simple_message == str); |
202 | 207 |
203 // Test asynchronous read. Server waits for message. | 208 // Test asynchronous read. Server waits for message. |
204 base::RunLoop run_loop; | 209 base::RunLoop run_loop; |
205 int read_result = 0; | 210 int read_result = 0; |
206 rv = server->RecvFrom( | 211 rv = server->RecvFrom( |
207 buffer_.get(), kMaxRead, &recv_from_address_, | 212 buffer_.get(), kMaxRead, &recv_from_address_, |
208 base::Bind(&ReadCompleteCallback, &read_result, run_loop.QuitClosure())); | 213 base::Bind(&ReadCompleteCallback, &read_result, run_loop.QuitClosure())); |
209 EXPECT_EQ(ERR_IO_PENDING, rv); | 214 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
210 | 215 |
211 // Client sends to the server. | 216 // Client sends to the server. |
212 base::ThreadTaskRunnerHandle::Get()->PostTask( | 217 base::ThreadTaskRunnerHandle::Get()->PostTask( |
213 FROM_HERE, | 218 FROM_HERE, |
214 base::Bind(&UDPSocketTest::WriteSocketIgnoreResult, | 219 base::Bind(&UDPSocketTest::WriteSocketIgnoreResult, |
215 base::Unretained(this), client.get(), simple_message)); | 220 base::Unretained(this), client.get(), simple_message)); |
216 run_loop.Run(); | 221 run_loop.Run(); |
217 EXPECT_EQ(simple_message.length(), static_cast<size_t>(read_result)); | 222 EXPECT_EQ(simple_message.length(), static_cast<size_t>(read_result)); |
218 EXPECT_EQ(simple_message, std::string(buffer_->data(), read_result)); | 223 EXPECT_EQ(simple_message, std::string(buffer_->data(), read_result)); |
219 | 224 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 std::unique_ptr<UDPServerSocket> server1( | 295 std::unique_ptr<UDPServerSocket> server1( |
291 new UDPServerSocket(&server1_log, NetLog::Source())); | 296 new UDPServerSocket(&server1_log, NetLog::Source())); |
292 std::unique_ptr<UDPServerSocket> server2( | 297 std::unique_ptr<UDPServerSocket> server2( |
293 new UDPServerSocket(&server2_log, NetLog::Source())); | 298 new UDPServerSocket(&server2_log, NetLog::Source())); |
294 server1->AllowAddressReuse(); | 299 server1->AllowAddressReuse(); |
295 server1->AllowBroadcast(); | 300 server1->AllowBroadcast(); |
296 server2->AllowAddressReuse(); | 301 server2->AllowAddressReuse(); |
297 server2->AllowBroadcast(); | 302 server2->AllowBroadcast(); |
298 | 303 |
299 int rv = server1->Listen(listen_address); | 304 int rv = server1->Listen(listen_address); |
300 EXPECT_EQ(OK, rv); | 305 EXPECT_THAT(rv, IsOk()); |
301 rv = server2->Listen(listen_address); | 306 rv = server2->Listen(listen_address); |
302 EXPECT_EQ(OK, rv); | 307 EXPECT_THAT(rv, IsOk()); |
303 | 308 |
304 rv = SendToSocket(server1.get(), first_message, broadcast_address); | 309 rv = SendToSocket(server1.get(), first_message, broadcast_address); |
305 ASSERT_EQ(static_cast<int>(first_message.size()), rv); | 310 ASSERT_EQ(static_cast<int>(first_message.size()), rv); |
306 std::string str = RecvFromSocket(server1.get()); | 311 std::string str = RecvFromSocket(server1.get()); |
307 ASSERT_EQ(first_message, str); | 312 ASSERT_EQ(first_message, str); |
308 str = RecvFromSocket(server2.get()); | 313 str = RecvFromSocket(server2.get()); |
309 ASSERT_EQ(first_message, str); | 314 ASSERT_EQ(first_message, str); |
310 | 315 |
311 rv = SendToSocket(server2.get(), second_message, broadcast_address); | 316 rv = SendToSocket(server2.get(), second_message, broadcast_address); |
312 ASSERT_EQ(static_cast<int>(second_message.size()), rv); | 317 ASSERT_EQ(static_cast<int>(second_message.size()), rv); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 | 362 |
358 // Create and connect sockets and save port numbers. | 363 // Create and connect sockets and save port numbers. |
359 std::deque<int> used_ports; | 364 std::deque<int> used_ports; |
360 for (int i = 0; i < kBindRetries; ++i) { | 365 for (int i = 0; i < kBindRetries; ++i) { |
361 UDPClientSocket* socket = | 366 UDPClientSocket* socket = |
362 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, | 367 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, |
363 RandIntCallback(), | 368 RandIntCallback(), |
364 NULL, | 369 NULL, |
365 NetLog::Source()); | 370 NetLog::Source()); |
366 sockets.push_back(socket); | 371 sockets.push_back(socket); |
367 EXPECT_EQ(OK, socket->Connect(peer_address)); | 372 EXPECT_THAT(socket->Connect(peer_address), IsOk()); |
368 | 373 |
369 IPEndPoint client_address; | 374 IPEndPoint client_address; |
370 EXPECT_EQ(OK, socket->GetLocalAddress(&client_address)); | 375 EXPECT_THAT(socket->GetLocalAddress(&client_address), IsOk()); |
371 used_ports.push_back(client_address.port()); | 376 used_ports.push_back(client_address.port()); |
372 } | 377 } |
373 | 378 |
374 // Free the last socket, its local port is still in |used_ports|. | 379 // Free the last socket, its local port is still in |used_ports|. |
375 delete sockets.back(); | 380 delete sockets.back(); |
376 sockets.pop_back(); | 381 sockets.pop_back(); |
377 | 382 |
378 TestPrng test_prng(used_ports); | 383 TestPrng test_prng(used_ports); |
379 RandIntCallback rand_int_cb = | 384 RandIntCallback rand_int_cb = |
380 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | 385 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); |
381 | 386 |
382 // Create a socket with random binding policy and connect. | 387 // Create a socket with random binding policy and connect. |
383 std::unique_ptr<UDPClientSocket> test_socket(new UDPClientSocket( | 388 std::unique_ptr<UDPClientSocket> test_socket(new UDPClientSocket( |
384 DatagramSocket::RANDOM_BIND, rand_int_cb, NULL, NetLog::Source())); | 389 DatagramSocket::RANDOM_BIND, rand_int_cb, NULL, NetLog::Source())); |
385 EXPECT_EQ(OK, test_socket->Connect(peer_address)); | 390 EXPECT_THAT(test_socket->Connect(peer_address), IsOk()); |
386 | 391 |
387 // Make sure that the last port number in the |used_ports| was used. | 392 // Make sure that the last port number in the |used_ports| was used. |
388 IPEndPoint client_address; | 393 IPEndPoint client_address; |
389 EXPECT_EQ(OK, test_socket->GetLocalAddress(&client_address)); | 394 EXPECT_THAT(test_socket->GetLocalAddress(&client_address), IsOk()); |
390 EXPECT_EQ(used_ports.back(), client_address.port()); | 395 EXPECT_EQ(used_ports.back(), client_address.port()); |
391 | 396 |
392 STLDeleteElements(&sockets); | 397 STLDeleteElements(&sockets); |
393 } | 398 } |
394 | 399 |
395 // Return a privileged port (under 1024) so binding will fail. | 400 // Return a privileged port (under 1024) so binding will fail. |
396 int PrivilegedRand(int min, int max) { | 401 int PrivilegedRand(int min, int max) { |
397 // Chosen by fair dice roll. Guaranteed to be random. | 402 // Chosen by fair dice roll. Guaranteed to be random. |
398 return 4; | 403 return 4; |
399 } | 404 } |
400 | 405 |
401 #if defined(OS_IOS) && !TARGET_IPHONE_SIMULATOR | 406 #if defined(OS_IOS) && !TARGET_IPHONE_SIMULATOR |
402 // TODO(droger): On iOS this test fails on device (but passes on simulator). | 407 // TODO(droger): On iOS this test fails on device (but passes on simulator). |
403 // See http://crbug.com/227760. | 408 // See http://crbug.com/227760. |
404 #define MAYBE_ConnectFail DISABLED_ConnectFail | 409 #define MAYBE_ConnectFail DISABLED_ConnectFail |
405 #else | 410 #else |
406 #define MAYBE_ConnectFail ConnectFail | 411 #define MAYBE_ConnectFail ConnectFail |
407 #endif | 412 #endif |
408 TEST_F(UDPSocketTest, MAYBE_ConnectFail) { | 413 TEST_F(UDPSocketTest, MAYBE_ConnectFail) { |
409 IPEndPoint peer_address; | 414 IPEndPoint peer_address; |
410 CreateUDPAddress("0.0.0.0", 53, &peer_address); | 415 CreateUDPAddress("0.0.0.0", 53, &peer_address); |
411 | 416 |
412 std::unique_ptr<UDPSocket> socket(new UDPSocket(DatagramSocket::RANDOM_BIND, | 417 std::unique_ptr<UDPSocket> socket(new UDPSocket(DatagramSocket::RANDOM_BIND, |
413 base::Bind(&PrivilegedRand), | 418 base::Bind(&PrivilegedRand), |
414 NULL, NetLog::Source())); | 419 NULL, NetLog::Source())); |
415 int rv = socket->Open(peer_address.GetFamily()); | 420 int rv = socket->Open(peer_address.GetFamily()); |
416 EXPECT_EQ(OK, rv); | 421 EXPECT_THAT(rv, IsOk()); |
417 rv = socket->Connect(peer_address); | 422 rv = socket->Connect(peer_address); |
418 // Connect should have failed since we couldn't bind to that port, | 423 // Connect should have failed since we couldn't bind to that port, |
419 EXPECT_NE(OK, rv); | 424 EXPECT_NE(OK, rv); |
420 // Make sure that UDPSocket actually closed the socket. | 425 // Make sure that UDPSocket actually closed the socket. |
421 EXPECT_FALSE(socket->is_connected()); | 426 EXPECT_FALSE(socket->is_connected()); |
422 } | 427 } |
423 | 428 |
424 // In this test, we verify that connect() on a socket will have the effect | 429 // In this test, we verify that connect() on a socket will have the effect |
425 // of filtering reads on this socket only to data read from the destination | 430 // of filtering reads on this socket only to data read from the destination |
426 // we connected to. | 431 // we connected to. |
427 // | 432 // |
428 // The purpose of this test is that some documentation indicates that connect | 433 // The purpose of this test is that some documentation indicates that connect |
429 // binds the client's sends to send to a particular server endpoint, but does | 434 // binds the client's sends to send to a particular server endpoint, but does |
430 // not bind the client's reads to only be from that endpoint, and that we need | 435 // not bind the client's reads to only be from that endpoint, and that we need |
431 // to always use recvfrom() to disambiguate. | 436 // to always use recvfrom() to disambiguate. |
432 TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { | 437 TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { |
433 const uint16_t kPort1 = 9999; | 438 const uint16_t kPort1 = 9999; |
434 const uint16_t kPort2 = 10000; | 439 const uint16_t kPort2 = 10000; |
435 std::string simple_message("hello world!"); | 440 std::string simple_message("hello world!"); |
436 std::string foreign_message("BAD MESSAGE TO GET!!"); | 441 std::string foreign_message("BAD MESSAGE TO GET!!"); |
437 | 442 |
438 // Setup the first server to listen. | 443 // Setup the first server to listen. |
439 IPEndPoint bind_address; | 444 IPEndPoint bind_address; |
440 CreateUDPAddress("127.0.0.1", kPort1, &bind_address); | 445 CreateUDPAddress("127.0.0.1", kPort1, &bind_address); |
441 UDPServerSocket server1(NULL, NetLog::Source()); | 446 UDPServerSocket server1(NULL, NetLog::Source()); |
442 server1.AllowAddressReuse(); | 447 server1.AllowAddressReuse(); |
443 int rv = server1.Listen(bind_address); | 448 int rv = server1.Listen(bind_address); |
444 ASSERT_EQ(OK, rv); | 449 ASSERT_THAT(rv, IsOk()); |
445 | 450 |
446 // Setup the second server to listen. | 451 // Setup the second server to listen. |
447 CreateUDPAddress("127.0.0.1", kPort2, &bind_address); | 452 CreateUDPAddress("127.0.0.1", kPort2, &bind_address); |
448 UDPServerSocket server2(NULL, NetLog::Source()); | 453 UDPServerSocket server2(NULL, NetLog::Source()); |
449 server2.AllowAddressReuse(); | 454 server2.AllowAddressReuse(); |
450 rv = server2.Listen(bind_address); | 455 rv = server2.Listen(bind_address); |
451 ASSERT_EQ(OK, rv); | 456 ASSERT_THAT(rv, IsOk()); |
452 | 457 |
453 // Setup the client, connected to server 1. | 458 // Setup the client, connected to server 1. |
454 IPEndPoint server_address; | 459 IPEndPoint server_address; |
455 CreateUDPAddress("127.0.0.1", kPort1, &server_address); | 460 CreateUDPAddress("127.0.0.1", kPort1, &server_address); |
456 UDPClientSocket client(DatagramSocket::DEFAULT_BIND, | 461 UDPClientSocket client(DatagramSocket::DEFAULT_BIND, |
457 RandIntCallback(), | 462 RandIntCallback(), |
458 NULL, | 463 NULL, |
459 NetLog::Source()); | 464 NetLog::Source()); |
460 rv = client.Connect(server_address); | 465 rv = client.Connect(server_address); |
461 EXPECT_EQ(OK, rv); | 466 EXPECT_THAT(rv, IsOk()); |
462 | 467 |
463 // Client sends to server1. | 468 // Client sends to server1. |
464 rv = WriteSocket(&client, simple_message); | 469 rv = WriteSocket(&client, simple_message); |
465 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); | 470 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
466 | 471 |
467 // Server1 waits for message. | 472 // Server1 waits for message. |
468 std::string str = RecvFromSocket(&server1); | 473 std::string str = RecvFromSocket(&server1); |
469 DCHECK(simple_message == str); | 474 DCHECK(simple_message == str); |
470 | 475 |
471 // Get the client's address. | 476 // Get the client's address. |
472 IPEndPoint client_address; | 477 IPEndPoint client_address; |
473 rv = client.GetLocalAddress(&client_address); | 478 rv = client.GetLocalAddress(&client_address); |
474 EXPECT_EQ(OK, rv); | 479 EXPECT_THAT(rv, IsOk()); |
475 | 480 |
476 // Server2 sends reply. | 481 // Server2 sends reply. |
477 rv = SendToSocket(&server2, foreign_message, | 482 rv = SendToSocket(&server2, foreign_message, |
478 client_address); | 483 client_address); |
479 EXPECT_EQ(foreign_message.length(), static_cast<size_t>(rv)); | 484 EXPECT_EQ(foreign_message.length(), static_cast<size_t>(rv)); |
480 | 485 |
481 // Server1 sends reply. | 486 // Server1 sends reply. |
482 rv = SendToSocket(&server1, simple_message, | 487 rv = SendToSocket(&server1, simple_message, |
483 client_address); | 488 client_address); |
484 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); | 489 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 if (tests[i].may_fail && rv == ERR_ADDRESS_UNREACHABLE) { | 526 if (tests[i].may_fail && rv == ERR_ADDRESS_UNREACHABLE) { |
522 // Connect() may return ERR_ADDRESS_UNREACHABLE for IPv6 | 527 // Connect() may return ERR_ADDRESS_UNREACHABLE for IPv6 |
523 // addresses if IPv6 is not configured. | 528 // addresses if IPv6 is not configured. |
524 continue; | 529 continue; |
525 } | 530 } |
526 | 531 |
527 EXPECT_LE(ERR_IO_PENDING, rv); | 532 EXPECT_LE(ERR_IO_PENDING, rv); |
528 | 533 |
529 IPEndPoint fetched_local_address; | 534 IPEndPoint fetched_local_address; |
530 rv = client.GetLocalAddress(&fetched_local_address); | 535 rv = client.GetLocalAddress(&fetched_local_address); |
531 EXPECT_EQ(OK, rv); | 536 EXPECT_THAT(rv, IsOk()); |
532 | 537 |
533 // TODO(mbelshe): figure out how to verify the IP and port. | 538 // TODO(mbelshe): figure out how to verify the IP and port. |
534 // The port is dynamically generated by the udp stack. | 539 // The port is dynamically generated by the udp stack. |
535 // The IP is the real IP of the client, not necessarily | 540 // The IP is the real IP of the client, not necessarily |
536 // loopback. | 541 // loopback. |
537 //EXPECT_EQ(local_address.address(), fetched_local_address.address()); | 542 //EXPECT_EQ(local_address.address(), fetched_local_address.address()); |
538 | 543 |
539 IPEndPoint fetched_remote_address; | 544 IPEndPoint fetched_remote_address; |
540 rv = client.GetPeerAddress(&fetched_remote_address); | 545 rv = client.GetPeerAddress(&fetched_remote_address); |
541 EXPECT_EQ(OK, rv); | 546 EXPECT_THAT(rv, IsOk()); |
542 | 547 |
543 EXPECT_EQ(remote_address, fetched_remote_address); | 548 EXPECT_EQ(remote_address, fetched_remote_address); |
544 } | 549 } |
545 } | 550 } |
546 | 551 |
547 TEST_F(UDPSocketTest, ServerGetLocalAddress) { | 552 TEST_F(UDPSocketTest, ServerGetLocalAddress) { |
548 IPEndPoint bind_address; | 553 IPEndPoint bind_address; |
549 CreateUDPAddress("127.0.0.1", 0, &bind_address); | 554 CreateUDPAddress("127.0.0.1", 0, &bind_address); |
550 UDPServerSocket server(NULL, NetLog::Source()); | 555 UDPServerSocket server(NULL, NetLog::Source()); |
551 int rv = server.Listen(bind_address); | 556 int rv = server.Listen(bind_address); |
552 EXPECT_EQ(OK, rv); | 557 EXPECT_THAT(rv, IsOk()); |
553 | 558 |
554 IPEndPoint local_address; | 559 IPEndPoint local_address; |
555 rv = server.GetLocalAddress(&local_address); | 560 rv = server.GetLocalAddress(&local_address); |
556 EXPECT_EQ(rv, 0); | 561 EXPECT_EQ(rv, 0); |
557 | 562 |
558 // Verify that port was allocated. | 563 // Verify that port was allocated. |
559 EXPECT_GT(local_address.port(), 0); | 564 EXPECT_GT(local_address.port(), 0); |
560 EXPECT_EQ(local_address.address(), bind_address.address()); | 565 EXPECT_EQ(local_address.address(), bind_address.address()); |
561 } | 566 } |
562 | 567 |
563 TEST_F(UDPSocketTest, ServerGetPeerAddress) { | 568 TEST_F(UDPSocketTest, ServerGetPeerAddress) { |
564 IPEndPoint bind_address; | 569 IPEndPoint bind_address; |
565 CreateUDPAddress("127.0.0.1", 0, &bind_address); | 570 CreateUDPAddress("127.0.0.1", 0, &bind_address); |
566 UDPServerSocket server(NULL, NetLog::Source()); | 571 UDPServerSocket server(NULL, NetLog::Source()); |
567 int rv = server.Listen(bind_address); | 572 int rv = server.Listen(bind_address); |
568 EXPECT_EQ(OK, rv); | 573 EXPECT_THAT(rv, IsOk()); |
569 | 574 |
570 IPEndPoint peer_address; | 575 IPEndPoint peer_address; |
571 rv = server.GetPeerAddress(&peer_address); | 576 rv = server.GetPeerAddress(&peer_address); |
572 EXPECT_EQ(rv, ERR_SOCKET_NOT_CONNECTED); | 577 EXPECT_EQ(rv, ERR_SOCKET_NOT_CONNECTED); |
573 } | 578 } |
574 | 579 |
575 // Close the socket while read is pending. | 580 // Close the socket while read is pending. |
576 TEST_F(UDPSocketTest, CloseWithPendingRead) { | 581 TEST_F(UDPSocketTest, CloseWithPendingRead) { |
577 IPEndPoint bind_address; | 582 IPEndPoint bind_address; |
578 CreateUDPAddress("127.0.0.1", 0, &bind_address); | 583 CreateUDPAddress("127.0.0.1", 0, &bind_address); |
579 UDPServerSocket server(NULL, NetLog::Source()); | 584 UDPServerSocket server(NULL, NetLog::Source()); |
580 int rv = server.Listen(bind_address); | 585 int rv = server.Listen(bind_address); |
581 EXPECT_EQ(OK, rv); | 586 EXPECT_THAT(rv, IsOk()); |
582 | 587 |
583 TestCompletionCallback callback; | 588 TestCompletionCallback callback; |
584 IPEndPoint from; | 589 IPEndPoint from; |
585 rv = server.RecvFrom(buffer_.get(), kMaxRead, &from, callback.callback()); | 590 rv = server.RecvFrom(buffer_.get(), kMaxRead, &from, callback.callback()); |
586 EXPECT_EQ(rv, ERR_IO_PENDING); | 591 EXPECT_EQ(rv, ERR_IO_PENDING); |
587 | 592 |
588 server.Close(); | 593 server.Close(); |
589 | 594 |
590 EXPECT_FALSE(callback.have_result()); | 595 EXPECT_FALSE(callback.have_result()); |
591 } | 596 } |
(...skipping 13 matching lines...) Expand all Loading... |
605 | 610 |
606 IPEndPoint bind_address; | 611 IPEndPoint bind_address; |
607 CreateUDPAddress("0.0.0.0", kPort, &bind_address); | 612 CreateUDPAddress("0.0.0.0", kPort, &bind_address); |
608 IPAddress group_ip; | 613 IPAddress group_ip; |
609 EXPECT_TRUE(group_ip.AssignFromIPLiteral(kGroup)); | 614 EXPECT_TRUE(group_ip.AssignFromIPLiteral(kGroup)); |
610 | 615 |
611 UDPSocket socket(DatagramSocket::DEFAULT_BIND, | 616 UDPSocket socket(DatagramSocket::DEFAULT_BIND, |
612 RandIntCallback(), | 617 RandIntCallback(), |
613 NULL, | 618 NULL, |
614 NetLog::Source()); | 619 NetLog::Source()); |
615 EXPECT_EQ(OK, socket.Open(bind_address.GetFamily())); | 620 EXPECT_THAT(socket.Open(bind_address.GetFamily()), IsOk()); |
616 EXPECT_EQ(OK, socket.Bind(bind_address)); | 621 EXPECT_THAT(socket.Bind(bind_address), IsOk()); |
617 EXPECT_EQ(OK, socket.JoinGroup(group_ip)); | 622 EXPECT_THAT(socket.JoinGroup(group_ip), IsOk()); |
618 // Joining group multiple times. | 623 // Joining group multiple times. |
619 EXPECT_NE(OK, socket.JoinGroup(group_ip)); | 624 EXPECT_NE(OK, socket.JoinGroup(group_ip)); |
620 EXPECT_EQ(OK, socket.LeaveGroup(group_ip)); | 625 EXPECT_THAT(socket.LeaveGroup(group_ip), IsOk()); |
621 // Leaving group multiple times. | 626 // Leaving group multiple times. |
622 EXPECT_NE(OK, socket.LeaveGroup(group_ip)); | 627 EXPECT_NE(OK, socket.LeaveGroup(group_ip)); |
623 | 628 |
624 socket.Close(); | 629 socket.Close(); |
625 } | 630 } |
626 | 631 |
627 TEST_F(UDPSocketTest, MulticastOptions) { | 632 TEST_F(UDPSocketTest, MulticastOptions) { |
628 const uint16_t kPort = 9999; | 633 const uint16_t kPort = 9999; |
629 IPEndPoint bind_address; | 634 IPEndPoint bind_address; |
630 CreateUDPAddress("0.0.0.0", kPort, &bind_address); | 635 CreateUDPAddress("0.0.0.0", kPort, &bind_address); |
631 | 636 |
632 UDPSocket socket(DatagramSocket::DEFAULT_BIND, | 637 UDPSocket socket(DatagramSocket::DEFAULT_BIND, |
633 RandIntCallback(), | 638 RandIntCallback(), |
634 NULL, | 639 NULL, |
635 NetLog::Source()); | 640 NetLog::Source()); |
636 // Before binding. | 641 // Before binding. |
637 EXPECT_EQ(OK, socket.SetMulticastLoopbackMode(false)); | 642 EXPECT_THAT(socket.SetMulticastLoopbackMode(false), IsOk()); |
638 EXPECT_EQ(OK, socket.SetMulticastLoopbackMode(true)); | 643 EXPECT_THAT(socket.SetMulticastLoopbackMode(true), IsOk()); |
639 EXPECT_EQ(OK, socket.SetMulticastTimeToLive(0)); | 644 EXPECT_THAT(socket.SetMulticastTimeToLive(0), IsOk()); |
640 EXPECT_EQ(OK, socket.SetMulticastTimeToLive(3)); | 645 EXPECT_THAT(socket.SetMulticastTimeToLive(3), IsOk()); |
641 EXPECT_NE(OK, socket.SetMulticastTimeToLive(-1)); | 646 EXPECT_NE(OK, socket.SetMulticastTimeToLive(-1)); |
642 EXPECT_EQ(OK, socket.SetMulticastInterface(0)); | 647 EXPECT_THAT(socket.SetMulticastInterface(0), IsOk()); |
643 | 648 |
644 EXPECT_EQ(OK, socket.Open(bind_address.GetFamily())); | 649 EXPECT_THAT(socket.Open(bind_address.GetFamily()), IsOk()); |
645 EXPECT_EQ(OK, socket.Bind(bind_address)); | 650 EXPECT_THAT(socket.Bind(bind_address), IsOk()); |
646 | 651 |
647 EXPECT_NE(OK, socket.SetMulticastLoopbackMode(false)); | 652 EXPECT_NE(OK, socket.SetMulticastLoopbackMode(false)); |
648 EXPECT_NE(OK, socket.SetMulticastTimeToLive(0)); | 653 EXPECT_NE(OK, socket.SetMulticastTimeToLive(0)); |
649 EXPECT_NE(OK, socket.SetMulticastInterface(0)); | 654 EXPECT_NE(OK, socket.SetMulticastInterface(0)); |
650 | 655 |
651 socket.Close(); | 656 socket.Close(); |
652 } | 657 } |
653 | 658 |
654 // Checking that DSCP bits are set correctly is difficult, | 659 // Checking that DSCP bits are set correctly is difficult, |
655 // but let's check that the code doesn't crash at least. | 660 // but let's check that the code doesn't crash at least. |
656 TEST_F(UDPSocketTest, SetDSCP) { | 661 TEST_F(UDPSocketTest, SetDSCP) { |
657 // Setup the server to listen. | 662 // Setup the server to listen. |
658 IPEndPoint bind_address; | 663 IPEndPoint bind_address; |
659 UDPSocket client(DatagramSocket::DEFAULT_BIND, | 664 UDPSocket client(DatagramSocket::DEFAULT_BIND, |
660 RandIntCallback(), | 665 RandIntCallback(), |
661 NULL, | 666 NULL, |
662 NetLog::Source()); | 667 NetLog::Source()); |
663 // We need a real IP, but we won't actually send anything to it. | 668 // We need a real IP, but we won't actually send anything to it. |
664 CreateUDPAddress("8.8.8.8", 9999, &bind_address); | 669 CreateUDPAddress("8.8.8.8", 9999, &bind_address); |
665 int rv = client.Open(bind_address.GetFamily()); | 670 int rv = client.Open(bind_address.GetFamily()); |
666 EXPECT_EQ(OK, rv); | 671 EXPECT_THAT(rv, IsOk()); |
667 | 672 |
668 rv = client.Connect(bind_address); | 673 rv = client.Connect(bind_address); |
669 if (rv != OK) { | 674 if (rv != OK) { |
670 // Let's try localhost then.. | 675 // Let's try localhost then.. |
671 CreateUDPAddress("127.0.0.1", 9999, &bind_address); | 676 CreateUDPAddress("127.0.0.1", 9999, &bind_address); |
672 rv = client.Connect(bind_address); | 677 rv = client.Connect(bind_address); |
673 } | 678 } |
674 EXPECT_EQ(OK, rv); | 679 EXPECT_THAT(rv, IsOk()); |
675 | 680 |
676 client.SetDiffServCodePoint(DSCP_NO_CHANGE); | 681 client.SetDiffServCodePoint(DSCP_NO_CHANGE); |
677 client.SetDiffServCodePoint(DSCP_AF41); | 682 client.SetDiffServCodePoint(DSCP_AF41); |
678 client.SetDiffServCodePoint(DSCP_DEFAULT); | 683 client.SetDiffServCodePoint(DSCP_DEFAULT); |
679 client.SetDiffServCodePoint(DSCP_CS2); | 684 client.SetDiffServCodePoint(DSCP_CS2); |
680 client.SetDiffServCodePoint(DSCP_NO_CHANGE); | 685 client.SetDiffServCodePoint(DSCP_NO_CHANGE); |
681 client.SetDiffServCodePoint(DSCP_DEFAULT); | 686 client.SetDiffServCodePoint(DSCP_DEFAULT); |
682 client.Close(); | 687 client.Close(); |
683 } | 688 } |
684 | 689 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
764 TEST_F(UDPSocketTest, SetDSCPFake) { | 769 TEST_F(UDPSocketTest, SetDSCPFake) { |
765 // Setup the server to listen. | 770 // Setup the server to listen. |
766 IPEndPoint bind_address; | 771 IPEndPoint bind_address; |
767 // We need a real IP, but we won't actually send anything to it. | 772 // We need a real IP, but we won't actually send anything to it. |
768 CreateUDPAddress("8.8.8.8", 9999, &bind_address); | 773 CreateUDPAddress("8.8.8.8", 9999, &bind_address); |
769 UDPSocket client(DatagramSocket::DEFAULT_BIND, | 774 UDPSocket client(DatagramSocket::DEFAULT_BIND, |
770 RandIntCallback(), | 775 RandIntCallback(), |
771 NULL, | 776 NULL, |
772 NetLog::Source()); | 777 NetLog::Source()); |
773 int rv = client.SetDiffServCodePoint(DSCP_AF41); | 778 int rv = client.SetDiffServCodePoint(DSCP_AF41); |
774 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, rv); | 779 EXPECT_THAT(rv, IsError(ERR_SOCKET_NOT_CONNECTED)); |
775 | 780 |
776 rv = client.Open(bind_address.GetFamily()); | 781 rv = client.Open(bind_address.GetFamily()); |
777 EXPECT_EQ(OK, rv); | 782 EXPECT_THAT(rv, IsOk()); |
778 | 783 |
779 rv = client.Connect(bind_address); | 784 rv = client.Connect(bind_address); |
780 EXPECT_EQ(OK, rv); | 785 EXPECT_THAT(rv, IsOk()); |
781 | 786 |
782 QwaveAPI& qos(QwaveAPI::Get()); | 787 QwaveAPI& qos(QwaveAPI::Get()); |
783 qos.create_handle_func_ = FakeQOSCreateHandleFAIL; | 788 qos.create_handle_func_ = FakeQOSCreateHandleFAIL; |
784 qos.close_handle_func_ = FakeQOSCloseHandle; | 789 qos.close_handle_func_ = FakeQOSCloseHandle; |
785 qos.add_socket_to_flow_func_ = FakeQOSAddSocketToFlow; | 790 qos.add_socket_to_flow_func_ = FakeQOSAddSocketToFlow; |
786 qos.remove_socket_from_flow_func_ = FakeQOSRemoveSocketFromFlow; | 791 qos.remove_socket_from_flow_func_ = FakeQOSRemoveSocketFromFlow; |
787 qos.set_flow_func_ = FakeQOSSetFlow; | 792 qos.set_flow_func_ = FakeQOSSetFlow; |
788 qos.qwave_supported_ = true; | 793 qos.qwave_supported_ = true; |
789 | 794 |
790 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); | 795 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_NO_CHANGE), IsOk()); |
791 EXPECT_EQ(ERROR_NOT_SUPPORTED, client.SetDiffServCodePoint(DSCP_AF41)); | 796 EXPECT_EQ(ERROR_NOT_SUPPORTED, client.SetDiffServCodePoint(DSCP_AF41)); |
792 qos.create_handle_func_ = FakeQOSCreateHandle; | 797 qos.create_handle_func_ = FakeQOSCreateHandle; |
793 g_expected_dscp = DSCP_AF41; | 798 g_expected_dscp = DSCP_AF41; |
794 g_expected_traffic_type = QOSTrafficTypeAudioVideo; | 799 g_expected_traffic_type = QOSTrafficTypeAudioVideo; |
795 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_AF41)); | 800 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_AF41), IsOk()); |
796 g_expected_dscp = DSCP_DEFAULT; | 801 g_expected_dscp = DSCP_DEFAULT; |
797 g_expected_traffic_type = QOSTrafficTypeBestEffort; | 802 g_expected_traffic_type = QOSTrafficTypeBestEffort; |
798 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); | 803 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_DEFAULT), IsOk()); |
799 g_expected_dscp = DSCP_CS2; | 804 g_expected_dscp = DSCP_CS2; |
800 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; | 805 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; |
801 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_CS2)); | 806 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_CS2), IsOk()); |
802 g_expected_dscp = DSCP_CS3; | 807 g_expected_dscp = DSCP_CS3; |
803 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; | 808 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; |
804 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); | 809 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_NO_CHANGE), IsOk()); |
805 g_expected_dscp = DSCP_DEFAULT; | 810 g_expected_dscp = DSCP_DEFAULT; |
806 g_expected_traffic_type = QOSTrafficTypeBestEffort; | 811 g_expected_traffic_type = QOSTrafficTypeBestEffort; |
807 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); | 812 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_DEFAULT), IsOk()); |
808 client.Close(); | 813 client.Close(); |
809 } | 814 } |
810 #endif | 815 #endif |
811 | 816 |
812 } // namespace net | 817 } // namespace net |
OLD | NEW |