| 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/net_log_event_type.h" | 23 #include "net/log/net_log_event_type.h" |
| 24 #include "net/log/net_log_source.h" |
| 24 #include "net/log/test_net_log.h" | 25 #include "net/log/test_net_log.h" |
| 25 #include "net/log/test_net_log_entry.h" | 26 #include "net/log/test_net_log_entry.h" |
| 26 #include "net/log/test_net_log_util.h" | 27 #include "net/log/test_net_log_util.h" |
| 27 #include "net/test/gtest_util.h" | 28 #include "net/test/gtest_util.h" |
| 28 #include "net/test/net_test_suite.h" | 29 #include "net/test/net_test_suite.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 31 #include "testing/platform_test.h" | 32 #include "testing/platform_test.h" |
| 32 | 33 |
| 33 #if defined(OS_ANDROID) | 34 #if defined(OS_ANDROID) |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 | 164 |
| 164 void UDPSocketTest::ConnectTest(bool use_nonblocking_io) { | 165 void UDPSocketTest::ConnectTest(bool use_nonblocking_io) { |
| 165 const uint16_t kPort = 9999; | 166 const uint16_t kPort = 9999; |
| 166 std::string simple_message("hello world!"); | 167 std::string simple_message("hello world!"); |
| 167 | 168 |
| 168 // Setup the server to listen. | 169 // Setup the server to listen. |
| 169 IPEndPoint bind_address; | 170 IPEndPoint bind_address; |
| 170 CreateUDPAddress("127.0.0.1", kPort, &bind_address); | 171 CreateUDPAddress("127.0.0.1", kPort, &bind_address); |
| 171 TestNetLog server_log; | 172 TestNetLog server_log; |
| 172 std::unique_ptr<UDPServerSocket> server( | 173 std::unique_ptr<UDPServerSocket> server( |
| 173 new UDPServerSocket(&server_log, NetLog::Source())); | 174 new UDPServerSocket(&server_log, NetLogSource())); |
| 174 if (use_nonblocking_io) | 175 if (use_nonblocking_io) |
| 175 server->UseNonBlockingIO(); | 176 server->UseNonBlockingIO(); |
| 176 server->AllowAddressReuse(); | 177 server->AllowAddressReuse(); |
| 177 int rv = server->Listen(bind_address); | 178 int rv = server->Listen(bind_address); |
| 178 ASSERT_THAT(rv, IsOk()); | 179 ASSERT_THAT(rv, IsOk()); |
| 179 | 180 |
| 180 // Setup the client. | 181 // Setup the client. |
| 181 IPEndPoint server_address; | 182 IPEndPoint server_address; |
| 182 CreateUDPAddress("127.0.0.1", kPort, &server_address); | 183 CreateUDPAddress("127.0.0.1", kPort, &server_address); |
| 183 TestNetLog client_log; | 184 TestNetLog client_log; |
| 184 std::unique_ptr<UDPClientSocket> client( | 185 std::unique_ptr<UDPClientSocket> client( |
| 185 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | 186 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
| 186 &client_log, NetLog::Source())); | 187 &client_log, NetLogSource())); |
| 187 if (use_nonblocking_io) | 188 if (use_nonblocking_io) |
| 188 client->UseNonBlockingIO(); | 189 client->UseNonBlockingIO(); |
| 189 | 190 |
| 190 rv = client->Connect(server_address); | 191 rv = client->Connect(server_address); |
| 191 EXPECT_THAT(rv, IsOk()); | 192 EXPECT_THAT(rv, IsOk()); |
| 192 | 193 |
| 193 // Client sends to the server. | 194 // Client sends to the server. |
| 194 rv = WriteSocket(client.get(), simple_message); | 195 rv = WriteSocket(client.get(), simple_message); |
| 195 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); | 196 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
| 196 | 197 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 const uint16_t kPort = 9999; | 295 const uint16_t kPort = 9999; |
| 295 std::string first_message("first message"), second_message("second message"); | 296 std::string first_message("first message"), second_message("second message"); |
| 296 | 297 |
| 297 IPEndPoint broadcast_address; | 298 IPEndPoint broadcast_address; |
| 298 CreateUDPAddress("255.255.255.255", kPort, &broadcast_address); | 299 CreateUDPAddress("255.255.255.255", kPort, &broadcast_address); |
| 299 IPEndPoint listen_address; | 300 IPEndPoint listen_address; |
| 300 CreateUDPAddress("0.0.0.0", kPort, &listen_address); | 301 CreateUDPAddress("0.0.0.0", kPort, &listen_address); |
| 301 | 302 |
| 302 TestNetLog server1_log, server2_log; | 303 TestNetLog server1_log, server2_log; |
| 303 std::unique_ptr<UDPServerSocket> server1( | 304 std::unique_ptr<UDPServerSocket> server1( |
| 304 new UDPServerSocket(&server1_log, NetLog::Source())); | 305 new UDPServerSocket(&server1_log, NetLogSource())); |
| 305 std::unique_ptr<UDPServerSocket> server2( | 306 std::unique_ptr<UDPServerSocket> server2( |
| 306 new UDPServerSocket(&server2_log, NetLog::Source())); | 307 new UDPServerSocket(&server2_log, NetLogSource())); |
| 307 server1->AllowAddressReuse(); | 308 server1->AllowAddressReuse(); |
| 308 server1->AllowBroadcast(); | 309 server1->AllowBroadcast(); |
| 309 server2->AllowAddressReuse(); | 310 server2->AllowAddressReuse(); |
| 310 server2->AllowBroadcast(); | 311 server2->AllowBroadcast(); |
| 311 | 312 |
| 312 int rv = server1->Listen(listen_address); | 313 int rv = server1->Listen(listen_address); |
| 313 EXPECT_THAT(rv, IsOk()); | 314 EXPECT_THAT(rv, IsOk()); |
| 314 rv = server2->Listen(listen_address); | 315 rv = server2->Listen(listen_address); |
| 315 EXPECT_THAT(rv, IsOk()); | 316 EXPECT_THAT(rv, IsOk()); |
| 316 | 317 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 }; | 365 }; |
| 365 | 366 |
| 366 TEST_F(UDPSocketTest, ConnectRandomBind) { | 367 TEST_F(UDPSocketTest, ConnectRandomBind) { |
| 367 std::vector<UDPClientSocket*> sockets; | 368 std::vector<UDPClientSocket*> sockets; |
| 368 IPEndPoint peer_address; | 369 IPEndPoint peer_address; |
| 369 CreateUDPAddress("127.0.0.1", 53, &peer_address); | 370 CreateUDPAddress("127.0.0.1", 53, &peer_address); |
| 370 | 371 |
| 371 // Create and connect sockets and save port numbers. | 372 // Create and connect sockets and save port numbers. |
| 372 std::deque<int> used_ports; | 373 std::deque<int> used_ports; |
| 373 for (int i = 0; i < kBindRetries; ++i) { | 374 for (int i = 0; i < kBindRetries; ++i) { |
| 374 UDPClientSocket* socket = | 375 UDPClientSocket* socket = new UDPClientSocket( |
| 375 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, | 376 DatagramSocket::DEFAULT_BIND, RandIntCallback(), NULL, NetLogSource()); |
| 376 RandIntCallback(), | |
| 377 NULL, | |
| 378 NetLog::Source()); | |
| 379 sockets.push_back(socket); | 377 sockets.push_back(socket); |
| 380 EXPECT_THAT(socket->Connect(peer_address), IsOk()); | 378 EXPECT_THAT(socket->Connect(peer_address), IsOk()); |
| 381 | 379 |
| 382 IPEndPoint client_address; | 380 IPEndPoint client_address; |
| 383 EXPECT_THAT(socket->GetLocalAddress(&client_address), IsOk()); | 381 EXPECT_THAT(socket->GetLocalAddress(&client_address), IsOk()); |
| 384 used_ports.push_back(client_address.port()); | 382 used_ports.push_back(client_address.port()); |
| 385 } | 383 } |
| 386 | 384 |
| 387 // Free the last socket, its local port is still in |used_ports|. | 385 // Free the last socket, its local port is still in |used_ports|. |
| 388 delete sockets.back(); | 386 delete sockets.back(); |
| 389 sockets.pop_back(); | 387 sockets.pop_back(); |
| 390 | 388 |
| 391 TestPrng test_prng(used_ports); | 389 TestPrng test_prng(used_ports); |
| 392 RandIntCallback rand_int_cb = | 390 RandIntCallback rand_int_cb = |
| 393 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | 391 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); |
| 394 | 392 |
| 395 // Create a socket with random binding policy and connect. | 393 // Create a socket with random binding policy and connect. |
| 396 std::unique_ptr<UDPClientSocket> test_socket(new UDPClientSocket( | 394 std::unique_ptr<UDPClientSocket> test_socket(new UDPClientSocket( |
| 397 DatagramSocket::RANDOM_BIND, rand_int_cb, NULL, NetLog::Source())); | 395 DatagramSocket::RANDOM_BIND, rand_int_cb, NULL, NetLogSource())); |
| 398 EXPECT_THAT(test_socket->Connect(peer_address), IsOk()); | 396 EXPECT_THAT(test_socket->Connect(peer_address), IsOk()); |
| 399 | 397 |
| 400 // Make sure that the last port number in the |used_ports| was used. | 398 // Make sure that the last port number in the |used_ports| was used. |
| 401 IPEndPoint client_address; | 399 IPEndPoint client_address; |
| 402 EXPECT_THAT(test_socket->GetLocalAddress(&client_address), IsOk()); | 400 EXPECT_THAT(test_socket->GetLocalAddress(&client_address), IsOk()); |
| 403 EXPECT_EQ(used_ports.back(), client_address.port()); | 401 EXPECT_EQ(used_ports.back(), client_address.port()); |
| 404 | 402 |
| 405 base::STLDeleteElements(&sockets); | 403 base::STLDeleteElements(&sockets); |
| 406 } | 404 } |
| 407 | 405 |
| 408 // Return a privileged port (under 1024) so binding will fail. | 406 // Return a privileged port (under 1024) so binding will fail. |
| 409 int PrivilegedRand(int min, int max) { | 407 int PrivilegedRand(int min, int max) { |
| 410 // Chosen by fair dice roll. Guaranteed to be random. | 408 // Chosen by fair dice roll. Guaranteed to be random. |
| 411 return 4; | 409 return 4; |
| 412 } | 410 } |
| 413 | 411 |
| 414 #if defined(OS_IOS) && !TARGET_IPHONE_SIMULATOR | 412 #if defined(OS_IOS) && !TARGET_IPHONE_SIMULATOR |
| 415 // TODO(droger): On iOS this test fails on device (but passes on simulator). | 413 // TODO(droger): On iOS this test fails on device (but passes on simulator). |
| 416 // See http://crbug.com/227760. | 414 // See http://crbug.com/227760. |
| 417 #define MAYBE_ConnectFail DISABLED_ConnectFail | 415 #define MAYBE_ConnectFail DISABLED_ConnectFail |
| 418 #else | 416 #else |
| 419 #define MAYBE_ConnectFail ConnectFail | 417 #define MAYBE_ConnectFail ConnectFail |
| 420 #endif | 418 #endif |
| 421 TEST_F(UDPSocketTest, MAYBE_ConnectFail) { | 419 TEST_F(UDPSocketTest, MAYBE_ConnectFail) { |
| 422 IPEndPoint peer_address; | 420 IPEndPoint peer_address; |
| 423 CreateUDPAddress("0.0.0.0", 53, &peer_address); | 421 CreateUDPAddress("0.0.0.0", 53, &peer_address); |
| 424 | 422 |
| 425 std::unique_ptr<UDPSocket> socket(new UDPSocket(DatagramSocket::RANDOM_BIND, | 423 std::unique_ptr<UDPSocket> socket(new UDPSocket(DatagramSocket::RANDOM_BIND, |
| 426 base::Bind(&PrivilegedRand), | 424 base::Bind(&PrivilegedRand), |
| 427 NULL, NetLog::Source())); | 425 NULL, NetLogSource())); |
| 428 int rv = socket->Open(peer_address.GetFamily()); | 426 int rv = socket->Open(peer_address.GetFamily()); |
| 429 EXPECT_THAT(rv, IsOk()); | 427 EXPECT_THAT(rv, IsOk()); |
| 430 rv = socket->Connect(peer_address); | 428 rv = socket->Connect(peer_address); |
| 431 // Connect should have failed since we couldn't bind to that port, | 429 // Connect should have failed since we couldn't bind to that port, |
| 432 EXPECT_NE(OK, rv); | 430 EXPECT_NE(OK, rv); |
| 433 // Make sure that UDPSocket actually closed the socket. | 431 // Make sure that UDPSocket actually closed the socket. |
| 434 EXPECT_FALSE(socket->is_connected()); | 432 EXPECT_FALSE(socket->is_connected()); |
| 435 } | 433 } |
| 436 | 434 |
| 437 // In this test, we verify that connect() on a socket will have the effect | 435 // In this test, we verify that connect() on a socket will have the effect |
| 438 // of filtering reads on this socket only to data read from the destination | 436 // of filtering reads on this socket only to data read from the destination |
| 439 // we connected to. | 437 // we connected to. |
| 440 // | 438 // |
| 441 // The purpose of this test is that some documentation indicates that connect | 439 // The purpose of this test is that some documentation indicates that connect |
| 442 // binds the client's sends to send to a particular server endpoint, but does | 440 // binds the client's sends to send to a particular server endpoint, but does |
| 443 // not bind the client's reads to only be from that endpoint, and that we need | 441 // not bind the client's reads to only be from that endpoint, and that we need |
| 444 // to always use recvfrom() to disambiguate. | 442 // to always use recvfrom() to disambiguate. |
| 445 TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { | 443 TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { |
| 446 const uint16_t kPort1 = 9999; | 444 const uint16_t kPort1 = 9999; |
| 447 const uint16_t kPort2 = 10000; | 445 const uint16_t kPort2 = 10000; |
| 448 std::string simple_message("hello world!"); | 446 std::string simple_message("hello world!"); |
| 449 std::string foreign_message("BAD MESSAGE TO GET!!"); | 447 std::string foreign_message("BAD MESSAGE TO GET!!"); |
| 450 | 448 |
| 451 // Setup the first server to listen. | 449 // Setup the first server to listen. |
| 452 IPEndPoint bind_address; | 450 IPEndPoint bind_address; |
| 453 CreateUDPAddress("127.0.0.1", kPort1, &bind_address); | 451 CreateUDPAddress("127.0.0.1", kPort1, &bind_address); |
| 454 UDPServerSocket server1(NULL, NetLog::Source()); | 452 UDPServerSocket server1(NULL, NetLogSource()); |
| 455 server1.AllowAddressReuse(); | 453 server1.AllowAddressReuse(); |
| 456 int rv = server1.Listen(bind_address); | 454 int rv = server1.Listen(bind_address); |
| 457 ASSERT_THAT(rv, IsOk()); | 455 ASSERT_THAT(rv, IsOk()); |
| 458 | 456 |
| 459 // Setup the second server to listen. | 457 // Setup the second server to listen. |
| 460 CreateUDPAddress("127.0.0.1", kPort2, &bind_address); | 458 CreateUDPAddress("127.0.0.1", kPort2, &bind_address); |
| 461 UDPServerSocket server2(NULL, NetLog::Source()); | 459 UDPServerSocket server2(NULL, NetLogSource()); |
| 462 server2.AllowAddressReuse(); | 460 server2.AllowAddressReuse(); |
| 463 rv = server2.Listen(bind_address); | 461 rv = server2.Listen(bind_address); |
| 464 ASSERT_THAT(rv, IsOk()); | 462 ASSERT_THAT(rv, IsOk()); |
| 465 | 463 |
| 466 // Setup the client, connected to server 1. | 464 // Setup the client, connected to server 1. |
| 467 IPEndPoint server_address; | 465 IPEndPoint server_address; |
| 468 CreateUDPAddress("127.0.0.1", kPort1, &server_address); | 466 CreateUDPAddress("127.0.0.1", kPort1, &server_address); |
| 469 UDPClientSocket client(DatagramSocket::DEFAULT_BIND, | 467 UDPClientSocket client(DatagramSocket::DEFAULT_BIND, RandIntCallback(), NULL, |
| 470 RandIntCallback(), | 468 NetLogSource()); |
| 471 NULL, | |
| 472 NetLog::Source()); | |
| 473 rv = client.Connect(server_address); | 469 rv = client.Connect(server_address); |
| 474 EXPECT_THAT(rv, IsOk()); | 470 EXPECT_THAT(rv, IsOk()); |
| 475 | 471 |
| 476 // Client sends to server1. | 472 // Client sends to server1. |
| 477 rv = WriteSocket(&client, simple_message); | 473 rv = WriteSocket(&client, simple_message); |
| 478 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); | 474 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
| 479 | 475 |
| 480 // Server1 waits for message. | 476 // Server1 waits for message. |
| 481 std::string str = RecvFromSocket(&server1); | 477 std::string str = RecvFromSocket(&server1); |
| 482 DCHECK(simple_message == str); | 478 DCHECK(simple_message == str); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 for (size_t i = 0; i < arraysize(tests); i++) { | 515 for (size_t i = 0; i < arraysize(tests); i++) { |
| 520 SCOPED_TRACE(std::string("Connecting from ") + tests[i].local_address + | 516 SCOPED_TRACE(std::string("Connecting from ") + tests[i].local_address + |
| 521 std::string(" to ") + tests[i].remote_address); | 517 std::string(" to ") + tests[i].remote_address); |
| 522 | 518 |
| 523 IPAddress ip_address; | 519 IPAddress ip_address; |
| 524 EXPECT_TRUE(ip_address.AssignFromIPLiteral(tests[i].remote_address)); | 520 EXPECT_TRUE(ip_address.AssignFromIPLiteral(tests[i].remote_address)); |
| 525 IPEndPoint remote_address(ip_address, 80); | 521 IPEndPoint remote_address(ip_address, 80); |
| 526 EXPECT_TRUE(ip_address.AssignFromIPLiteral(tests[i].local_address)); | 522 EXPECT_TRUE(ip_address.AssignFromIPLiteral(tests[i].local_address)); |
| 527 IPEndPoint local_address(ip_address, 80); | 523 IPEndPoint local_address(ip_address, 80); |
| 528 | 524 |
| 529 UDPClientSocket client(DatagramSocket::DEFAULT_BIND, | 525 UDPClientSocket client(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
| 530 RandIntCallback(), | 526 NULL, NetLogSource()); |
| 531 NULL, | |
| 532 NetLog::Source()); | |
| 533 int rv = client.Connect(remote_address); | 527 int rv = client.Connect(remote_address); |
| 534 if (tests[i].may_fail && rv == ERR_ADDRESS_UNREACHABLE) { | 528 if (tests[i].may_fail && rv == ERR_ADDRESS_UNREACHABLE) { |
| 535 // Connect() may return ERR_ADDRESS_UNREACHABLE for IPv6 | 529 // Connect() may return ERR_ADDRESS_UNREACHABLE for IPv6 |
| 536 // addresses if IPv6 is not configured. | 530 // addresses if IPv6 is not configured. |
| 537 continue; | 531 continue; |
| 538 } | 532 } |
| 539 | 533 |
| 540 EXPECT_LE(ERR_IO_PENDING, rv); | 534 EXPECT_LE(ERR_IO_PENDING, rv); |
| 541 | 535 |
| 542 IPEndPoint fetched_local_address; | 536 IPEndPoint fetched_local_address; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 553 rv = client.GetPeerAddress(&fetched_remote_address); | 547 rv = client.GetPeerAddress(&fetched_remote_address); |
| 554 EXPECT_THAT(rv, IsOk()); | 548 EXPECT_THAT(rv, IsOk()); |
| 555 | 549 |
| 556 EXPECT_EQ(remote_address, fetched_remote_address); | 550 EXPECT_EQ(remote_address, fetched_remote_address); |
| 557 } | 551 } |
| 558 } | 552 } |
| 559 | 553 |
| 560 TEST_F(UDPSocketTest, ServerGetLocalAddress) { | 554 TEST_F(UDPSocketTest, ServerGetLocalAddress) { |
| 561 IPEndPoint bind_address; | 555 IPEndPoint bind_address; |
| 562 CreateUDPAddress("127.0.0.1", 0, &bind_address); | 556 CreateUDPAddress("127.0.0.1", 0, &bind_address); |
| 563 UDPServerSocket server(NULL, NetLog::Source()); | 557 UDPServerSocket server(NULL, NetLogSource()); |
| 564 int rv = server.Listen(bind_address); | 558 int rv = server.Listen(bind_address); |
| 565 EXPECT_THAT(rv, IsOk()); | 559 EXPECT_THAT(rv, IsOk()); |
| 566 | 560 |
| 567 IPEndPoint local_address; | 561 IPEndPoint local_address; |
| 568 rv = server.GetLocalAddress(&local_address); | 562 rv = server.GetLocalAddress(&local_address); |
| 569 EXPECT_EQ(rv, 0); | 563 EXPECT_EQ(rv, 0); |
| 570 | 564 |
| 571 // Verify that port was allocated. | 565 // Verify that port was allocated. |
| 572 EXPECT_GT(local_address.port(), 0); | 566 EXPECT_GT(local_address.port(), 0); |
| 573 EXPECT_EQ(local_address.address(), bind_address.address()); | 567 EXPECT_EQ(local_address.address(), bind_address.address()); |
| 574 } | 568 } |
| 575 | 569 |
| 576 TEST_F(UDPSocketTest, ServerGetPeerAddress) { | 570 TEST_F(UDPSocketTest, ServerGetPeerAddress) { |
| 577 IPEndPoint bind_address; | 571 IPEndPoint bind_address; |
| 578 CreateUDPAddress("127.0.0.1", 0, &bind_address); | 572 CreateUDPAddress("127.0.0.1", 0, &bind_address); |
| 579 UDPServerSocket server(NULL, NetLog::Source()); | 573 UDPServerSocket server(NULL, NetLogSource()); |
| 580 int rv = server.Listen(bind_address); | 574 int rv = server.Listen(bind_address); |
| 581 EXPECT_THAT(rv, IsOk()); | 575 EXPECT_THAT(rv, IsOk()); |
| 582 | 576 |
| 583 IPEndPoint peer_address; | 577 IPEndPoint peer_address; |
| 584 rv = server.GetPeerAddress(&peer_address); | 578 rv = server.GetPeerAddress(&peer_address); |
| 585 EXPECT_EQ(rv, ERR_SOCKET_NOT_CONNECTED); | 579 EXPECT_EQ(rv, ERR_SOCKET_NOT_CONNECTED); |
| 586 } | 580 } |
| 587 | 581 |
| 588 TEST_F(UDPSocketTest, ClientSetDoNotFragment) { | 582 TEST_F(UDPSocketTest, ClientSetDoNotFragment) { |
| 589 for (std::string ip : {"127.0.0.1", "::1"}) { | 583 for (std::string ip : {"127.0.0.1", "::1"}) { |
| 590 LOG(INFO) << "ip: " << ip; | 584 LOG(INFO) << "ip: " << ip; |
| 591 UDPClientSocket client(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | 585 UDPClientSocket client(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
| 592 nullptr, NetLog::Source()); | 586 nullptr, NetLogSource()); |
| 593 IPAddress ip_address; | 587 IPAddress ip_address; |
| 594 EXPECT_TRUE(ip_address.AssignFromIPLiteral(ip)); | 588 EXPECT_TRUE(ip_address.AssignFromIPLiteral(ip)); |
| 595 IPEndPoint remote_address(ip_address, 80); | 589 IPEndPoint remote_address(ip_address, 80); |
| 596 int rv = client.Connect(remote_address); | 590 int rv = client.Connect(remote_address); |
| 597 // May fail on IPv6 is IPv6 is not configured. | 591 // May fail on IPv6 is IPv6 is not configured. |
| 598 if (ip_address.IsIPv6() && rv == ERR_ADDRESS_UNREACHABLE) | 592 if (ip_address.IsIPv6() && rv == ERR_ADDRESS_UNREACHABLE) |
| 599 return; | 593 return; |
| 600 EXPECT_THAT(rv, IsOk()); | 594 EXPECT_THAT(rv, IsOk()); |
| 601 | 595 |
| 602 #if defined(OS_MACOSX) | 596 #if defined(OS_MACOSX) |
| 603 EXPECT_EQ(ERR_NOT_IMPLEMENTED, client.SetDoNotFragment()); | 597 EXPECT_EQ(ERR_NOT_IMPLEMENTED, client.SetDoNotFragment()); |
| 604 #else | 598 #else |
| 605 rv = client.SetDoNotFragment(); | 599 rv = client.SetDoNotFragment(); |
| 606 EXPECT_THAT(rv, IsOk()); | 600 EXPECT_THAT(rv, IsOk()); |
| 607 #endif | 601 #endif |
| 608 } | 602 } |
| 609 } | 603 } |
| 610 | 604 |
| 611 TEST_F(UDPSocketTest, ServerSetDoNotFragment) { | 605 TEST_F(UDPSocketTest, ServerSetDoNotFragment) { |
| 612 for (std::string ip : {"127.0.0.1", "::1"}) { | 606 for (std::string ip : {"127.0.0.1", "::1"}) { |
| 613 LOG(INFO) << "ip: " << ip; | 607 LOG(INFO) << "ip: " << ip; |
| 614 IPEndPoint bind_address; | 608 IPEndPoint bind_address; |
| 615 CreateUDPAddress(ip, 0, &bind_address); | 609 CreateUDPAddress(ip, 0, &bind_address); |
| 616 UDPServerSocket server(nullptr, NetLog::Source()); | 610 UDPServerSocket server(nullptr, NetLogSource()); |
| 617 int rv = server.Listen(bind_address); | 611 int rv = server.Listen(bind_address); |
| 618 // May fail on IPv6 is IPv6 is not configure | 612 // May fail on IPv6 is IPv6 is not configure |
| 619 if (bind_address.address().IsIPv6() && rv == ERR_ADDRESS_INVALID) | 613 if (bind_address.address().IsIPv6() && rv == ERR_ADDRESS_INVALID) |
| 620 return; | 614 return; |
| 621 EXPECT_THAT(rv, IsOk()); | 615 EXPECT_THAT(rv, IsOk()); |
| 622 | 616 |
| 623 #if defined(OS_MACOSX) | 617 #if defined(OS_MACOSX) |
| 624 EXPECT_EQ(ERR_NOT_IMPLEMENTED, server.SetDoNotFragment()); | 618 EXPECT_EQ(ERR_NOT_IMPLEMENTED, server.SetDoNotFragment()); |
| 625 #else | 619 #else |
| 626 rv = server.SetDoNotFragment(); | 620 rv = server.SetDoNotFragment(); |
| 627 EXPECT_THAT(rv, IsOk()); | 621 EXPECT_THAT(rv, IsOk()); |
| 628 #endif | 622 #endif |
| 629 } | 623 } |
| 630 } | 624 } |
| 631 | 625 |
| 632 // Close the socket while read is pending. | 626 // Close the socket while read is pending. |
| 633 TEST_F(UDPSocketTest, CloseWithPendingRead) { | 627 TEST_F(UDPSocketTest, CloseWithPendingRead) { |
| 634 IPEndPoint bind_address; | 628 IPEndPoint bind_address; |
| 635 CreateUDPAddress("127.0.0.1", 0, &bind_address); | 629 CreateUDPAddress("127.0.0.1", 0, &bind_address); |
| 636 UDPServerSocket server(NULL, NetLog::Source()); | 630 UDPServerSocket server(NULL, NetLogSource()); |
| 637 int rv = server.Listen(bind_address); | 631 int rv = server.Listen(bind_address); |
| 638 EXPECT_THAT(rv, IsOk()); | 632 EXPECT_THAT(rv, IsOk()); |
| 639 | 633 |
| 640 TestCompletionCallback callback; | 634 TestCompletionCallback callback; |
| 641 IPEndPoint from; | 635 IPEndPoint from; |
| 642 rv = server.RecvFrom(buffer_.get(), kMaxRead, &from, callback.callback()); | 636 rv = server.RecvFrom(buffer_.get(), kMaxRead, &from, callback.callback()); |
| 643 EXPECT_EQ(rv, ERR_IO_PENDING); | 637 EXPECT_EQ(rv, ERR_IO_PENDING); |
| 644 | 638 |
| 645 server.Close(); | 639 server.Close(); |
| 646 | 640 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 658 | 652 |
| 659 TEST_F(UDPSocketTest, MAYBE_JoinMulticastGroup) { | 653 TEST_F(UDPSocketTest, MAYBE_JoinMulticastGroup) { |
| 660 const uint16_t kPort = 9999; | 654 const uint16_t kPort = 9999; |
| 661 const char kGroup[] = "237.132.100.17"; | 655 const char kGroup[] = "237.132.100.17"; |
| 662 | 656 |
| 663 IPEndPoint bind_address; | 657 IPEndPoint bind_address; |
| 664 CreateUDPAddress("0.0.0.0", kPort, &bind_address); | 658 CreateUDPAddress("0.0.0.0", kPort, &bind_address); |
| 665 IPAddress group_ip; | 659 IPAddress group_ip; |
| 666 EXPECT_TRUE(group_ip.AssignFromIPLiteral(kGroup)); | 660 EXPECT_TRUE(group_ip.AssignFromIPLiteral(kGroup)); |
| 667 | 661 |
| 668 UDPSocket socket(DatagramSocket::DEFAULT_BIND, | 662 UDPSocket socket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), NULL, |
| 669 RandIntCallback(), | 663 NetLogSource()); |
| 670 NULL, | |
| 671 NetLog::Source()); | |
| 672 EXPECT_THAT(socket.Open(bind_address.GetFamily()), IsOk()); | 664 EXPECT_THAT(socket.Open(bind_address.GetFamily()), IsOk()); |
| 673 EXPECT_THAT(socket.Bind(bind_address), IsOk()); | 665 EXPECT_THAT(socket.Bind(bind_address), IsOk()); |
| 674 EXPECT_THAT(socket.JoinGroup(group_ip), IsOk()); | 666 EXPECT_THAT(socket.JoinGroup(group_ip), IsOk()); |
| 675 // Joining group multiple times. | 667 // Joining group multiple times. |
| 676 EXPECT_NE(OK, socket.JoinGroup(group_ip)); | 668 EXPECT_NE(OK, socket.JoinGroup(group_ip)); |
| 677 EXPECT_THAT(socket.LeaveGroup(group_ip), IsOk()); | 669 EXPECT_THAT(socket.LeaveGroup(group_ip), IsOk()); |
| 678 // Leaving group multiple times. | 670 // Leaving group multiple times. |
| 679 EXPECT_NE(OK, socket.LeaveGroup(group_ip)); | 671 EXPECT_NE(OK, socket.LeaveGroup(group_ip)); |
| 680 | 672 |
| 681 socket.Close(); | 673 socket.Close(); |
| 682 } | 674 } |
| 683 | 675 |
| 684 TEST_F(UDPSocketTest, MulticastOptions) { | 676 TEST_F(UDPSocketTest, MulticastOptions) { |
| 685 const uint16_t kPort = 9999; | 677 const uint16_t kPort = 9999; |
| 686 IPEndPoint bind_address; | 678 IPEndPoint bind_address; |
| 687 CreateUDPAddress("0.0.0.0", kPort, &bind_address); | 679 CreateUDPAddress("0.0.0.0", kPort, &bind_address); |
| 688 | 680 |
| 689 UDPSocket socket(DatagramSocket::DEFAULT_BIND, | 681 UDPSocket socket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), NULL, |
| 690 RandIntCallback(), | 682 NetLogSource()); |
| 691 NULL, | |
| 692 NetLog::Source()); | |
| 693 // Before binding. | 683 // Before binding. |
| 694 EXPECT_THAT(socket.SetMulticastLoopbackMode(false), IsOk()); | 684 EXPECT_THAT(socket.SetMulticastLoopbackMode(false), IsOk()); |
| 695 EXPECT_THAT(socket.SetMulticastLoopbackMode(true), IsOk()); | 685 EXPECT_THAT(socket.SetMulticastLoopbackMode(true), IsOk()); |
| 696 EXPECT_THAT(socket.SetMulticastTimeToLive(0), IsOk()); | 686 EXPECT_THAT(socket.SetMulticastTimeToLive(0), IsOk()); |
| 697 EXPECT_THAT(socket.SetMulticastTimeToLive(3), IsOk()); | 687 EXPECT_THAT(socket.SetMulticastTimeToLive(3), IsOk()); |
| 698 EXPECT_NE(OK, socket.SetMulticastTimeToLive(-1)); | 688 EXPECT_NE(OK, socket.SetMulticastTimeToLive(-1)); |
| 699 EXPECT_THAT(socket.SetMulticastInterface(0), IsOk()); | 689 EXPECT_THAT(socket.SetMulticastInterface(0), IsOk()); |
| 700 | 690 |
| 701 EXPECT_THAT(socket.Open(bind_address.GetFamily()), IsOk()); | 691 EXPECT_THAT(socket.Open(bind_address.GetFamily()), IsOk()); |
| 702 EXPECT_THAT(socket.Bind(bind_address), IsOk()); | 692 EXPECT_THAT(socket.Bind(bind_address), IsOk()); |
| 703 | 693 |
| 704 EXPECT_NE(OK, socket.SetMulticastLoopbackMode(false)); | 694 EXPECT_NE(OK, socket.SetMulticastLoopbackMode(false)); |
| 705 EXPECT_NE(OK, socket.SetMulticastTimeToLive(0)); | 695 EXPECT_NE(OK, socket.SetMulticastTimeToLive(0)); |
| 706 EXPECT_NE(OK, socket.SetMulticastInterface(0)); | 696 EXPECT_NE(OK, socket.SetMulticastInterface(0)); |
| 707 | 697 |
| 708 socket.Close(); | 698 socket.Close(); |
| 709 } | 699 } |
| 710 | 700 |
| 711 // Checking that DSCP bits are set correctly is difficult, | 701 // Checking that DSCP bits are set correctly is difficult, |
| 712 // but let's check that the code doesn't crash at least. | 702 // but let's check that the code doesn't crash at least. |
| 713 TEST_F(UDPSocketTest, SetDSCP) { | 703 TEST_F(UDPSocketTest, SetDSCP) { |
| 714 // Setup the server to listen. | 704 // Setup the server to listen. |
| 715 IPEndPoint bind_address; | 705 IPEndPoint bind_address; |
| 716 UDPSocket client(DatagramSocket::DEFAULT_BIND, | 706 UDPSocket client(DatagramSocket::DEFAULT_BIND, RandIntCallback(), NULL, |
| 717 RandIntCallback(), | 707 NetLogSource()); |
| 718 NULL, | |
| 719 NetLog::Source()); | |
| 720 // We need a real IP, but we won't actually send anything to it. | 708 // We need a real IP, but we won't actually send anything to it. |
| 721 CreateUDPAddress("8.8.8.8", 9999, &bind_address); | 709 CreateUDPAddress("8.8.8.8", 9999, &bind_address); |
| 722 int rv = client.Open(bind_address.GetFamily()); | 710 int rv = client.Open(bind_address.GetFamily()); |
| 723 EXPECT_THAT(rv, IsOk()); | 711 EXPECT_THAT(rv, IsOk()); |
| 724 | 712 |
| 725 rv = client.Connect(bind_address); | 713 rv = client.Connect(bind_address); |
| 726 if (rv != OK) { | 714 if (rv != OK) { |
| 727 // Let's try localhost then.. | 715 // Let's try localhost then.. |
| 728 CreateUDPAddress("127.0.0.1", 9999, &bind_address); | 716 CreateUDPAddress("127.0.0.1", 9999, &bind_address); |
| 729 rv = client.Connect(bind_address); | 717 rv = client.Connect(bind_address); |
| 730 } | 718 } |
| 731 EXPECT_THAT(rv, IsOk()); | 719 EXPECT_THAT(rv, IsOk()); |
| 732 | 720 |
| 733 client.SetDiffServCodePoint(DSCP_NO_CHANGE); | 721 client.SetDiffServCodePoint(DSCP_NO_CHANGE); |
| 734 client.SetDiffServCodePoint(DSCP_AF41); | 722 client.SetDiffServCodePoint(DSCP_AF41); |
| 735 client.SetDiffServCodePoint(DSCP_DEFAULT); | 723 client.SetDiffServCodePoint(DSCP_DEFAULT); |
| 736 client.SetDiffServCodePoint(DSCP_CS2); | 724 client.SetDiffServCodePoint(DSCP_CS2); |
| 737 client.SetDiffServCodePoint(DSCP_NO_CHANGE); | 725 client.SetDiffServCodePoint(DSCP_NO_CHANGE); |
| 738 client.SetDiffServCodePoint(DSCP_DEFAULT); | 726 client.SetDiffServCodePoint(DSCP_DEFAULT); |
| 739 client.Close(); | 727 client.Close(); |
| 740 } | 728 } |
| 741 | 729 |
| 742 TEST_F(UDPSocketTest, TestBindToNetwork) { | 730 TEST_F(UDPSocketTest, TestBindToNetwork) { |
| 743 UDPSocket socket(DatagramSocket::RANDOM_BIND, base::Bind(&PrivilegedRand), | 731 UDPSocket socket(DatagramSocket::RANDOM_BIND, base::Bind(&PrivilegedRand), |
| 744 NULL, NetLog::Source()); | 732 NULL, NetLogSource()); |
| 745 ASSERT_EQ(OK, socket.Open(ADDRESS_FAMILY_IPV4)); | 733 ASSERT_EQ(OK, socket.Open(ADDRESS_FAMILY_IPV4)); |
| 746 // Test unsuccessful binding, by attempting to bind to a bogus NetworkHandle. | 734 // Test unsuccessful binding, by attempting to bind to a bogus NetworkHandle. |
| 747 int rv = socket.BindToNetwork(65536); | 735 int rv = socket.BindToNetwork(65536); |
| 748 #if !defined(OS_ANDROID) | 736 #if !defined(OS_ANDROID) |
| 749 EXPECT_EQ(ERR_NOT_IMPLEMENTED, rv); | 737 EXPECT_EQ(ERR_NOT_IMPLEMENTED, rv); |
| 750 #else | 738 #else |
| 751 if (base::android::BuildInfo::GetInstance()->sdk_int() < | 739 if (base::android::BuildInfo::GetInstance()->sdk_int() < |
| 752 base::android::SDK_VERSION_LOLLIPOP) { | 740 base::android::SDK_VERSION_LOLLIPOP) { |
| 753 EXPECT_EQ(ERR_NOT_IMPLEMENTED, rv); | 741 EXPECT_EQ(ERR_NOT_IMPLEMENTED, rv); |
| 754 } else if (base::android::BuildInfo::GetInstance()->sdk_int() >= | 742 } else if (base::android::BuildInfo::GetInstance()->sdk_int() >= |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 863 } // namespace | 851 } // namespace |
| 864 | 852 |
| 865 // Mock out the Qwave functions and make sure they are | 853 // Mock out the Qwave functions and make sure they are |
| 866 // called correctly. Must be in net namespace for friendship | 854 // called correctly. Must be in net namespace for friendship |
| 867 // reasons. | 855 // reasons. |
| 868 TEST_F(UDPSocketTest, SetDSCPFake) { | 856 TEST_F(UDPSocketTest, SetDSCPFake) { |
| 869 // Setup the server to listen. | 857 // Setup the server to listen. |
| 870 IPEndPoint bind_address; | 858 IPEndPoint bind_address; |
| 871 // We need a real IP, but we won't actually send anything to it. | 859 // We need a real IP, but we won't actually send anything to it. |
| 872 CreateUDPAddress("8.8.8.8", 9999, &bind_address); | 860 CreateUDPAddress("8.8.8.8", 9999, &bind_address); |
| 873 UDPSocket client(DatagramSocket::DEFAULT_BIND, | 861 UDPSocket client(DatagramSocket::DEFAULT_BIND, RandIntCallback(), NULL, |
| 874 RandIntCallback(), | 862 NetLogSource()); |
| 875 NULL, | |
| 876 NetLog::Source()); | |
| 877 int rv = client.SetDiffServCodePoint(DSCP_AF41); | 863 int rv = client.SetDiffServCodePoint(DSCP_AF41); |
| 878 EXPECT_THAT(rv, IsError(ERR_SOCKET_NOT_CONNECTED)); | 864 EXPECT_THAT(rv, IsError(ERR_SOCKET_NOT_CONNECTED)); |
| 879 | 865 |
| 880 rv = client.Open(bind_address.GetFamily()); | 866 rv = client.Open(bind_address.GetFamily()); |
| 881 EXPECT_THAT(rv, IsOk()); | 867 EXPECT_THAT(rv, IsOk()); |
| 882 | 868 |
| 883 rv = client.Connect(bind_address); | 869 rv = client.Connect(bind_address); |
| 884 EXPECT_THAT(rv, IsOk()); | 870 EXPECT_THAT(rv, IsOk()); |
| 885 | 871 |
| 886 QwaveAPI& qos(QwaveAPI::Get()); | 872 QwaveAPI& qos(QwaveAPI::Get()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 907 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; | 893 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; |
| 908 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_NO_CHANGE), IsOk()); | 894 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_NO_CHANGE), IsOk()); |
| 909 g_expected_dscp = DSCP_DEFAULT; | 895 g_expected_dscp = DSCP_DEFAULT; |
| 910 g_expected_traffic_type = QOSTrafficTypeBestEffort; | 896 g_expected_traffic_type = QOSTrafficTypeBestEffort; |
| 911 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_DEFAULT), IsOk()); | 897 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_DEFAULT), IsOk()); |
| 912 client.Close(); | 898 client.Close(); |
| 913 } | 899 } |
| 914 #endif | 900 #endif |
| 915 | 901 |
| 916 } // namespace net | 902 } // namespace net |
| OLD | NEW |