Chromium Code Reviews| 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_client_socket.h" | 5 #include "net/udp/udp_client_socket.h" |
| 6 #include "net/udp/udp_server_socket.h" | 6 #include "net/udp/udp_server_socket.h" |
| 7 | 7 |
| 8 #if defined(OS_MACOSX) | |
| 9 #include <unistd.h> | |
| 10 #endif // defined(OS_MACOSX) | |
| 11 | |
| 8 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 9 #include "base/bind.h" | 13 #include "base/bind.h" |
| 10 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 11 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 12 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 13 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
| 14 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 15 #include "net/base/net_log_unittest.h" | 19 #include "net/base/net_log_unittest.h" |
| 16 #include "net/base/net_test_suite.h" | 20 #include "net/base/net_test_suite.h" |
| 17 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 return; | 128 return; |
| 125 *address = IPEndPoint(ip_number, port); | 129 *address = IPEndPoint(ip_number, port); |
| 126 } | 130 } |
| 127 | 131 |
| 128 TEST_F(UDPSocketTest, Connect) { | 132 TEST_F(UDPSocketTest, Connect) { |
| 129 const int kPort = 9999; | 133 const int kPort = 9999; |
| 130 std::string simple_message("hello world!"); | 134 std::string simple_message("hello world!"); |
| 131 | 135 |
| 132 // Setup the server to listen. | 136 // Setup the server to listen. |
| 133 IPEndPoint bind_address; | 137 IPEndPoint bind_address; |
| 134 CreateUDPAddress("0.0.0.0", kPort, &bind_address); | 138 CreateUDPAddress("127.0.0.1", kPort, &bind_address); |
| 135 CapturingNetLog server_log; | 139 CapturingNetLog server_log; |
| 136 scoped_ptr<UDPServerSocket> server( | 140 scoped_ptr<UDPServerSocket> server( |
| 137 new UDPServerSocket(&server_log, NetLog::Source())); | 141 new UDPServerSocket(&server_log, NetLog::Source())); |
| 142 server->AllowAddressReuse(); | |
| 138 int rv = server->Listen(bind_address); | 143 int rv = server->Listen(bind_address); |
| 139 EXPECT_EQ(OK, rv); | 144 EXPECT_EQ(OK, rv); |
| 140 | 145 |
| 141 // Setup the client. | 146 // Setup the client. |
| 142 IPEndPoint server_address; | 147 IPEndPoint server_address; |
| 143 CreateUDPAddress("127.0.0.1", kPort, &server_address); | 148 CreateUDPAddress("127.0.0.1", kPort, &server_address); |
| 144 CapturingNetLog client_log; | 149 CapturingNetLog client_log; |
| 145 scoped_ptr<UDPClientSocket> client( | 150 scoped_ptr<UDPClientSocket> client( |
| 146 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, | 151 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, |
| 147 RandIntCallback(), | 152 RandIntCallback(), |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 client_entries, 2, NetLog::TYPE_UDP_CONNECT)); | 200 client_entries, 2, NetLog::TYPE_UDP_CONNECT)); |
| 196 EXPECT_TRUE(LogContainsEvent( | 201 EXPECT_TRUE(LogContainsEvent( |
| 197 client_entries, 3, NetLog::TYPE_UDP_BYTES_SENT, NetLog::PHASE_NONE)); | 202 client_entries, 3, NetLog::TYPE_UDP_BYTES_SENT, NetLog::PHASE_NONE)); |
| 198 EXPECT_TRUE(LogContainsEvent( | 203 EXPECT_TRUE(LogContainsEvent( |
| 199 client_entries, 4, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); | 204 client_entries, 4, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); |
| 200 EXPECT_TRUE(LogContainsEndEvent( | 205 EXPECT_TRUE(LogContainsEndEvent( |
| 201 client_entries, 5, NetLog::TYPE_SOCKET_ALIVE)); | 206 client_entries, 5, NetLog::TYPE_SOCKET_ALIVE)); |
| 202 } | 207 } |
| 203 | 208 |
| 204 TEST_F(UDPSocketTest, Broadcast) { | 209 TEST_F(UDPSocketTest, Broadcast) { |
| 210 #if defined(OS_MACOSX) | |
| 211 if (getuid() != 0) { | |
| 212 LOG(ERROR) << | |
| 213 "Skipping Broadcast test because it requires root permissions on OSX"; | |
|
mmenke
2012/09/06 14:29:07
nit: Think "Skipping test, as UDP broadcast requi
ygorshenin1
2012/09/06 17:29:34
Done.
| |
| 214 return; | |
| 215 } | |
| 216 #endif // defined(OS_MACOSX) | |
| 205 const int kPort = 9999; | 217 const int kPort = 9999; |
| 206 std::string first_message("first message"), second_message("second message"); | 218 std::string first_message("first message"), second_message("second message"); |
| 207 | 219 |
| 208 IPEndPoint broadcast_address; | 220 IPEndPoint broadcast_address; |
| 209 CreateUDPAddress("255.255.255.255", kPort, &broadcast_address); | 221 CreateUDPAddress("255.255.255.255", kPort, &broadcast_address); |
| 210 IPEndPoint listen_address; | 222 IPEndPoint listen_address; |
| 211 CreateUDPAddress("0.0.0.0", kPort, &listen_address); | 223 CreateUDPAddress("0.0.0.0", kPort, &listen_address); |
| 212 | 224 |
| 213 CapturingNetLog server1_log, server2_log; | 225 CapturingNetLog server1_log, server2_log; |
| 214 scoped_ptr<UDPServerSocket> server1( | 226 scoped_ptr<UDPServerSocket> server1( |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 // not bind the client's reads to only be from that endpoint, and that we need | 340 // not bind the client's reads to only be from that endpoint, and that we need |
| 329 // to always use recvfrom() to disambiguate. | 341 // to always use recvfrom() to disambiguate. |
| 330 TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { | 342 TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { |
| 331 const int kPort1 = 9999; | 343 const int kPort1 = 9999; |
| 332 const int kPort2 = 10000; | 344 const int kPort2 = 10000; |
| 333 std::string simple_message("hello world!"); | 345 std::string simple_message("hello world!"); |
| 334 std::string foreign_message("BAD MESSAGE TO GET!!"); | 346 std::string foreign_message("BAD MESSAGE TO GET!!"); |
| 335 | 347 |
| 336 // Setup the first server to listen. | 348 // Setup the first server to listen. |
| 337 IPEndPoint bind_address; | 349 IPEndPoint bind_address; |
| 338 CreateUDPAddress("0.0.0.0", kPort1, &bind_address); | 350 CreateUDPAddress("127.0.0.1", kPort1, &bind_address); |
| 339 UDPServerSocket server1(NULL, NetLog::Source()); | 351 UDPServerSocket server1(NULL, NetLog::Source()); |
| 352 server1.AllowAddressReuse(); | |
| 340 int rv = server1.Listen(bind_address); | 353 int rv = server1.Listen(bind_address); |
| 341 EXPECT_EQ(OK, rv); | 354 EXPECT_EQ(OK, rv); |
| 342 | 355 |
| 343 // Setup the second server to listen. | 356 // Setup the second server to listen. |
| 344 CreateUDPAddress("0.0.0.0", kPort2, &bind_address); | 357 CreateUDPAddress("127.0.0.1", kPort2, &bind_address); |
| 345 UDPServerSocket server2(NULL, NetLog::Source()); | 358 UDPServerSocket server2(NULL, NetLog::Source()); |
| 359 server2.AllowAddressReuse(); | |
| 346 rv = server2.Listen(bind_address); | 360 rv = server2.Listen(bind_address); |
| 347 EXPECT_EQ(OK, rv); | 361 EXPECT_EQ(OK, rv); |
| 348 | 362 |
| 349 // Setup the client, connected to server 1. | 363 // Setup the client, connected to server 1. |
| 350 IPEndPoint server_address; | 364 IPEndPoint server_address; |
| 351 CreateUDPAddress("127.0.0.1", kPort1, &server_address); | 365 CreateUDPAddress("127.0.0.1", kPort1, &server_address); |
| 352 UDPClientSocket client(DatagramSocket::DEFAULT_BIND, | 366 UDPClientSocket client(DatagramSocket::DEFAULT_BIND, |
| 353 RandIntCallback(), | 367 RandIntCallback(), |
| 354 NULL, | 368 NULL, |
| 355 NetLog::Source()); | 369 NetLog::Source()); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 EXPECT_EQ(rv, ERR_IO_PENDING); | 492 EXPECT_EQ(rv, ERR_IO_PENDING); |
| 479 | 493 |
| 480 server.Close(); | 494 server.Close(); |
| 481 | 495 |
| 482 EXPECT_FALSE(callback.have_result()); | 496 EXPECT_FALSE(callback.have_result()); |
| 483 } | 497 } |
| 484 | 498 |
| 485 } // namespace | 499 } // namespace |
| 486 | 500 |
| 487 } // namespace net | 501 } // namespace net |
| OLD | NEW |