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