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
3 // found in the LICENSE file.
5 #include "net/udp/udp_client_socket.h"
6 #include "net/udp/udp_server_socket.h"
8 #include "base/basictypes.h"
10 #include "base/metrics/histogram.h"
11 #include "base/stl_util.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/net_log_unittest.h"
16 #include "net/base/net_util.h"
17 #include "net/base/test_completion_callback.h"
18 #include "net/test/net_test_suite.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "testing/platform_test.h"
26 class UDPSocketTest
: public PlatformTest
{
29 : buffer_(new IOBufferWithSize(kMaxRead
)) {
32 // Blocks until data is read from the socket.
33 std::string
RecvFromSocket(UDPServerSocket
* socket
) {
34 TestCompletionCallback callback
;
36 int rv
= socket
->RecvFrom(
37 buffer_
.get(), kMaxRead
, &recv_from_address_
, callback
.callback());
38 if (rv
== ERR_IO_PENDING
)
39 rv
= callback
.WaitForResult();
41 return std::string(); // error!
42 return std::string(buffer_
->data(), rv
);
45 // Loop until |msg| has been written to the socket or until an
47 // If |address| is specified, then it is used for the destination
48 // to send to. Otherwise, will send to the last socket this server
50 int SendToSocket(UDPServerSocket
* socket
, std::string msg
) {
51 return SendToSocket(socket
, msg
, recv_from_address_
);
54 int SendToSocket(UDPServerSocket
* socket
,
56 const IPEndPoint
& address
) {
57 TestCompletionCallback callback
;
59 int length
= msg
.length();
60 scoped_refptr
<StringIOBuffer
> io_buffer(new StringIOBuffer(msg
));
61 scoped_refptr
<DrainableIOBuffer
> buffer(
62 new DrainableIOBuffer(io_buffer
.get(), length
));
65 while (buffer
->BytesRemaining()) {
66 int rv
= socket
->SendTo(
67 buffer
.get(), buffer
->BytesRemaining(), address
, callback
.callback());
68 if (rv
== ERR_IO_PENDING
)
69 rv
= callback
.WaitForResult();
71 return bytes_sent
> 0 ? bytes_sent
: rv
;
73 buffer
->DidConsume(rv
);
78 std::string
ReadSocket(UDPClientSocket
* socket
) {
79 TestCompletionCallback callback
;
81 int rv
= socket
->Read(buffer_
.get(), kMaxRead
, callback
.callback());
82 if (rv
== ERR_IO_PENDING
)
83 rv
= callback
.WaitForResult();
85 return std::string(); // error!
86 return std::string(buffer_
->data(), rv
);
89 // Loop until |msg| has been written to the socket or until an
91 int WriteSocket(UDPClientSocket
* socket
, std::string msg
) {
92 TestCompletionCallback callback
;
94 int length
= msg
.length();
95 scoped_refptr
<StringIOBuffer
> io_buffer(new StringIOBuffer(msg
));
96 scoped_refptr
<DrainableIOBuffer
> buffer(
97 new DrainableIOBuffer(io_buffer
.get(), length
));
100 while (buffer
->BytesRemaining()) {
101 int rv
= socket
->Write(
102 buffer
.get(), buffer
->BytesRemaining(), callback
.callback());
103 if (rv
== ERR_IO_PENDING
)
104 rv
= callback
.WaitForResult();
106 return bytes_sent
> 0 ? bytes_sent
: rv
;
108 buffer
->DidConsume(rv
);
114 static const int kMaxRead
= 1024;
115 scoped_refptr
<IOBufferWithSize
> buffer_
;
116 IPEndPoint recv_from_address_
;
119 // Creates and address from an ip/port and returns it in |address|.
120 void CreateUDPAddress(std::string ip_str
, int port
, IPEndPoint
* address
) {
121 IPAddressNumber ip_number
;
122 bool rv
= ParseIPLiteralToNumber(ip_str
, &ip_number
);
125 *address
= IPEndPoint(ip_number
, port
);
128 TEST_F(UDPSocketTest
, Connect
) {
129 const int kPort
= 9999;
130 std::string
simple_message("hello world!");
132 // Setup the server to listen.
133 IPEndPoint bind_address
;
134 CreateUDPAddress("127.0.0.1", kPort
, &bind_address
);
135 CapturingNetLog server_log
;
136 scoped_ptr
<UDPServerSocket
> server(
137 new UDPServerSocket(&server_log
, NetLog::Source()));
138 server
->AllowAddressReuse();
139 int rv
= server
->Listen(bind_address
);
143 IPEndPoint server_address
;
144 CreateUDPAddress("127.0.0.1", kPort
, &server_address
);
145 CapturingNetLog client_log
;
146 scoped_ptr
<UDPClientSocket
> client(
147 new UDPClientSocket(DatagramSocket::DEFAULT_BIND
,
151 rv
= client
->Connect(server_address
);
154 // Client sends to the server.
155 rv
= WriteSocket(client
.get(), simple_message
);
156 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(rv
));
158 // Server waits for message.
159 std::string str
= RecvFromSocket(server
.get());
160 DCHECK(simple_message
== str
);
162 // Server echoes reply.
163 rv
= SendToSocket(server
.get(), simple_message
);
164 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(rv
));
166 // Client waits for response.
167 str
= ReadSocket(client
.get());
168 DCHECK(simple_message
== str
);
170 // Delete sockets so they log their final events.
174 // Check the server's log.
175 CapturingNetLog::CapturedEntryList server_entries
;
176 server_log
.GetEntries(&server_entries
);
177 EXPECT_EQ(4u, server_entries
.size());
178 EXPECT_TRUE(LogContainsBeginEvent(
179 server_entries
, 0, NetLog::TYPE_SOCKET_ALIVE
));
180 EXPECT_TRUE(LogContainsEvent(
181 server_entries
, 1, NetLog::TYPE_UDP_BYTES_RECEIVED
, NetLog::PHASE_NONE
));
182 EXPECT_TRUE(LogContainsEvent(
183 server_entries
, 2, NetLog::TYPE_UDP_BYTES_SENT
, NetLog::PHASE_NONE
));
184 EXPECT_TRUE(LogContainsEndEvent(
185 server_entries
, 3, NetLog::TYPE_SOCKET_ALIVE
));
187 // Check the client's log.
188 CapturingNetLog::CapturedEntryList client_entries
;
189 client_log
.GetEntries(&client_entries
);
190 EXPECT_EQ(6u, client_entries
.size());
191 EXPECT_TRUE(LogContainsBeginEvent(
192 client_entries
, 0, NetLog::TYPE_SOCKET_ALIVE
));
193 EXPECT_TRUE(LogContainsBeginEvent(
194 client_entries
, 1, NetLog::TYPE_UDP_CONNECT
));
195 EXPECT_TRUE(LogContainsEndEvent(
196 client_entries
, 2, NetLog::TYPE_UDP_CONNECT
));
197 EXPECT_TRUE(LogContainsEvent(
198 client_entries
, 3, NetLog::TYPE_UDP_BYTES_SENT
, NetLog::PHASE_NONE
));
199 EXPECT_TRUE(LogContainsEvent(
200 client_entries
, 4, NetLog::TYPE_UDP_BYTES_RECEIVED
, NetLog::PHASE_NONE
));
201 EXPECT_TRUE(LogContainsEndEvent(
202 client_entries
, 5, NetLog::TYPE_SOCKET_ALIVE
));
205 #if defined(OS_MACOSX)
206 // UDPSocketPrivate_Broadcast is disabled for OSX because it requires
207 // root permissions on OSX 10.7+.
208 TEST_F(UDPSocketTest
, DISABLED_Broadcast
) {
209 #elif defined(OS_ANDROID)
210 // It is also disabled for Android because it is extremely flaky.
211 // The first call to SendToSocket returns -109 (Address not reachable)
212 // in some unpredictable cases. crbug.com/139144.
213 TEST_F(UDPSocketTest
, DISABLED_Broadcast
) {
215 TEST_F(UDPSocketTest
, Broadcast
) {
217 const int kPort
= 9999;
218 std::string
first_message("first message"), second_message("second message");
220 IPEndPoint broadcast_address
;
221 CreateUDPAddress("255.255.255.255", kPort
, &broadcast_address
);
222 IPEndPoint listen_address
;
223 CreateUDPAddress("0.0.0.0", kPort
, &listen_address
);
225 CapturingNetLog server1_log
, server2_log
;
226 scoped_ptr
<UDPServerSocket
> server1(
227 new UDPServerSocket(&server1_log
, NetLog::Source()));
228 scoped_ptr
<UDPServerSocket
> server2(
229 new UDPServerSocket(&server2_log
, NetLog::Source()));
230 server1
->AllowAddressReuse();
231 server1
->AllowBroadcast();
232 server2
->AllowAddressReuse();
233 server2
->AllowBroadcast();
235 int rv
= server1
->Listen(listen_address
);
237 rv
= server2
->Listen(listen_address
);
240 rv
= SendToSocket(server1
.get(), first_message
, broadcast_address
);
241 ASSERT_EQ(static_cast<int>(first_message
.size()), rv
);
242 std::string str
= RecvFromSocket(server1
.get());
243 ASSERT_EQ(first_message
, str
);
244 str
= RecvFromSocket(server2
.get());
245 ASSERT_EQ(first_message
, str
);
247 rv
= SendToSocket(server2
.get(), second_message
, broadcast_address
);
248 ASSERT_EQ(static_cast<int>(second_message
.size()), rv
);
249 str
= RecvFromSocket(server1
.get());
250 ASSERT_EQ(second_message
, str
);
251 str
= RecvFromSocket(server2
.get());
252 ASSERT_EQ(second_message
, str
);
255 // In this test, we verify that random binding logic works, which attempts
256 // to bind to a random port and returns if succeeds, otherwise retries for
257 // |kBindRetries| number of times.
259 // To generate the scenario, we first create |kBindRetries| number of
260 // UDPClientSockets with default binding policy and connect to the same
261 // peer and save the used port numbers. Then we get rid of the last
262 // socket, making sure that the local port it was bound to is available.
263 // Finally, we create a socket with random binding policy, passing it a
264 // test PRNG that would serve used port numbers in the array, one after
265 // another. At the end, we make sure that the test socket was bound to the
266 // port that became available after deleting the last socket with default
269 // We do not test the randomness of bound ports, but that we are using
270 // passed in PRNG correctly, thus, it's the duty of PRNG to produce strong
272 static const int kBindRetries
= 10;
276 explicit TestPrng(const std::deque
<int>& numbers
) : numbers_(numbers
) {}
277 int GetNext(int /* min */, int /* max */) {
278 DCHECK(!numbers_
.empty());
279 int rv
= numbers_
.front();
280 numbers_
.pop_front();
284 std::deque
<int> numbers_
;
286 DISALLOW_COPY_AND_ASSIGN(TestPrng
);
289 #if defined(OS_ANDROID)
290 // Disabled on Android for lack of 192.168.1.13. crbug.com/161245
291 TEST_F(UDPSocketTest
, DISABLED_ConnectRandomBind
) {
293 TEST_F(UDPSocketTest
, ConnectRandomBind
) {
295 std::vector
<UDPClientSocket
*> sockets
;
296 IPEndPoint peer_address
;
297 CreateUDPAddress("192.168.1.13", 53, &peer_address
);
299 // Create and connect sockets and save port numbers.
300 std::deque
<int> used_ports
;
301 for (int i
= 0; i
< kBindRetries
; ++i
) {
302 UDPClientSocket
* socket
=
303 new UDPClientSocket(DatagramSocket::DEFAULT_BIND
,
307 sockets
.push_back(socket
);
308 EXPECT_EQ(OK
, socket
->Connect(peer_address
));
310 IPEndPoint client_address
;
311 EXPECT_EQ(OK
, socket
->GetLocalAddress(&client_address
));
312 used_ports
.push_back(client_address
.port());
315 // Free the last socket, its local port is still in |used_ports|.
316 delete sockets
.back();
319 TestPrng
test_prng(used_ports
);
320 RandIntCallback rand_int_cb
=
321 base::Bind(&TestPrng::GetNext
, base::Unretained(&test_prng
));
323 // Create a socket with random binding policy and connect.
324 scoped_ptr
<UDPClientSocket
> test_socket(
325 new UDPClientSocket(DatagramSocket::RANDOM_BIND
,
329 EXPECT_EQ(OK
, test_socket
->Connect(peer_address
));
331 // Make sure that the last port number in the |used_ports| was used.
332 IPEndPoint client_address
;
333 EXPECT_EQ(OK
, test_socket
->GetLocalAddress(&client_address
));
334 EXPECT_EQ(used_ports
.back(), client_address
.port());
336 STLDeleteElements(&sockets
);
339 // Return a privileged port (under 1024) so binding will fail.
340 int PrivilegedRand(int min
, int max
) {
341 // Chosen by fair dice roll. Guaranteed to be random.
345 TEST_F(UDPSocketTest
, ConnectFail
) {
346 IPEndPoint peer_address
;
347 CreateUDPAddress("0.0.0.0", 53, &peer_address
);
349 scoped_ptr
<UDPSocket
> socket(
350 new UDPSocket(DatagramSocket::RANDOM_BIND
,
351 base::Bind(&PrivilegedRand
),
354 int rv
= socket
->Connect(peer_address
);
355 // Connect should have failed since we couldn't bind to that port,
357 // Make sure that UDPSocket actually closed the socket.
358 EXPECT_FALSE(socket
->is_connected());
361 // In this test, we verify that connect() on a socket will have the effect
362 // of filtering reads on this socket only to data read from the destination
365 // The purpose of this test is that some documentation indicates that connect
366 // binds the client's sends to send to a particular server endpoint, but does
367 // not bind the client's reads to only be from that endpoint, and that we need
368 // to always use recvfrom() to disambiguate.
369 TEST_F(UDPSocketTest
, VerifyConnectBindsAddr
) {
370 const int kPort1
= 9999;
371 const int kPort2
= 10000;
372 std::string
simple_message("hello world!");
373 std::string
foreign_message("BAD MESSAGE TO GET!!");
375 // Setup the first server to listen.
376 IPEndPoint bind_address
;
377 CreateUDPAddress("127.0.0.1", kPort1
, &bind_address
);
378 UDPServerSocket
server1(NULL
, NetLog::Source());
379 server1
.AllowAddressReuse();
380 int rv
= server1
.Listen(bind_address
);
383 // Setup the second server to listen.
384 CreateUDPAddress("127.0.0.1", kPort2
, &bind_address
);
385 UDPServerSocket
server2(NULL
, NetLog::Source());
386 server2
.AllowAddressReuse();
387 rv
= server2
.Listen(bind_address
);
390 // Setup the client, connected to server 1.
391 IPEndPoint server_address
;
392 CreateUDPAddress("127.0.0.1", kPort1
, &server_address
);
393 UDPClientSocket
client(DatagramSocket::DEFAULT_BIND
,
397 rv
= client
.Connect(server_address
);
400 // Client sends to server1.
401 rv
= WriteSocket(&client
, simple_message
);
402 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(rv
));
404 // Server1 waits for message.
405 std::string str
= RecvFromSocket(&server1
);
406 DCHECK(simple_message
== str
);
408 // Get the client's address.
409 IPEndPoint client_address
;
410 rv
= client
.GetLocalAddress(&client_address
);
413 // Server2 sends reply.
414 rv
= SendToSocket(&server2
, foreign_message
,
416 EXPECT_EQ(foreign_message
.length(), static_cast<size_t>(rv
));
418 // Server1 sends reply.
419 rv
= SendToSocket(&server1
, simple_message
,
421 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(rv
));
423 // Client waits for response.
424 str
= ReadSocket(&client
);
425 DCHECK(simple_message
== str
);
428 TEST_F(UDPSocketTest
, ClientGetLocalPeerAddresses
) {
430 std::string remote_address
;
431 std::string local_address
;
434 { "127.0.00.1", "127.0.0.1", false },
435 { "::1", "::1", true },
436 #if !defined(OS_ANDROID)
437 // Addresses below are disabled on Android. See crbug.com/161248
438 { "192.168.1.1", "127.0.0.1", false },
439 { "2001:db8:0::42", "::1", true },
442 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); i
++) {
443 SCOPED_TRACE(std::string("Connecting from ") + tests
[i
].local_address
+
444 std::string(" to ") + tests
[i
].remote_address
);
446 IPAddressNumber ip_number
;
447 ParseIPLiteralToNumber(tests
[i
].remote_address
, &ip_number
);
448 IPEndPoint
remote_address(ip_number
, 80);
449 ParseIPLiteralToNumber(tests
[i
].local_address
, &ip_number
);
450 IPEndPoint
local_address(ip_number
, 80);
452 UDPClientSocket
client(DatagramSocket::DEFAULT_BIND
,
456 int rv
= client
.Connect(remote_address
);
457 if (tests
[i
].may_fail
&& rv
== ERR_ADDRESS_UNREACHABLE
) {
458 // Connect() may return ERR_ADDRESS_UNREACHABLE for IPv6
459 // addresses if IPv6 is not configured.
463 EXPECT_LE(ERR_IO_PENDING
, rv
);
465 IPEndPoint fetched_local_address
;
466 rv
= client
.GetLocalAddress(&fetched_local_address
);
469 // TODO(mbelshe): figure out how to verify the IP and port.
470 // The port is dynamically generated by the udp stack.
471 // The IP is the real IP of the client, not necessarily
473 //EXPECT_EQ(local_address.address(), fetched_local_address.address());
475 IPEndPoint fetched_remote_address
;
476 rv
= client
.GetPeerAddress(&fetched_remote_address
);
479 EXPECT_EQ(remote_address
, fetched_remote_address
);
483 TEST_F(UDPSocketTest
, ServerGetLocalAddress
) {
484 IPEndPoint bind_address
;
485 CreateUDPAddress("127.0.0.1", 0, &bind_address
);
486 UDPServerSocket
server(NULL
, NetLog::Source());
487 int rv
= server
.Listen(bind_address
);
490 IPEndPoint local_address
;
491 rv
= server
.GetLocalAddress(&local_address
);
494 // Verify that port was allocated.
495 EXPECT_GT(local_address
.port(), 0);
496 EXPECT_EQ(local_address
.address(), bind_address
.address());
499 TEST_F(UDPSocketTest
, ServerGetPeerAddress
) {
500 IPEndPoint bind_address
;
501 CreateUDPAddress("127.0.0.1", 0, &bind_address
);
502 UDPServerSocket
server(NULL
, NetLog::Source());
503 int rv
= server
.Listen(bind_address
);
506 IPEndPoint peer_address
;
507 rv
= server
.GetPeerAddress(&peer_address
);
508 EXPECT_EQ(rv
, ERR_SOCKET_NOT_CONNECTED
);
511 // Close the socket while read is pending.
512 TEST_F(UDPSocketTest
, CloseWithPendingRead
) {
513 IPEndPoint bind_address
;
514 CreateUDPAddress("127.0.0.1", 0, &bind_address
);
515 UDPServerSocket
server(NULL
, NetLog::Source());
516 int rv
= server
.Listen(bind_address
);
519 TestCompletionCallback callback
;
521 rv
= server
.RecvFrom(buffer_
.get(), kMaxRead
, &from
, callback
.callback());
522 EXPECT_EQ(rv
, ERR_IO_PENDING
);
526 EXPECT_FALSE(callback
.have_result());
529 #if defined(OS_ANDROID)
530 // Some Android devices do not support multicast socket.
531 // The ones supporting multicast need WifiManager.MulitcastLock to enable it.
532 // http://goo.gl/jjAk9
533 #define MAYBE_JoinMulticastGroup DISABLED_JoinMulticastGroup
535 #define MAYBE_JoinMulticastGroup JoinMulticastGroup
536 #endif // defined(OS_ANDROID)
538 TEST_F(UDPSocketTest
, MAYBE_JoinMulticastGroup
) {
539 const int kPort
= 9999;
540 const char* const kGroup
= "237.132.100.17";
542 IPEndPoint bind_address
;
543 CreateUDPAddress("0.0.0.0", kPort
, &bind_address
);
544 IPAddressNumber group_ip
;
545 EXPECT_TRUE(ParseIPLiteralToNumber(kGroup
, &group_ip
));
547 UDPSocket
socket(DatagramSocket::DEFAULT_BIND
,
551 EXPECT_EQ(OK
, socket
.Bind(bind_address
));
552 EXPECT_EQ(OK
, socket
.JoinGroup(group_ip
));
553 // Joining group multiple times.
554 EXPECT_NE(OK
, socket
.JoinGroup(group_ip
));
555 EXPECT_EQ(OK
, socket
.LeaveGroup(group_ip
));
556 // Leaving group multiple times.
557 EXPECT_NE(OK
, socket
.LeaveGroup(group_ip
));
562 TEST_F(UDPSocketTest
, MulticastOptions
) {
563 const int kPort
= 9999;
564 IPEndPoint bind_address
;
565 CreateUDPAddress("0.0.0.0", kPort
, &bind_address
);
567 UDPSocket
socket(DatagramSocket::DEFAULT_BIND
,
572 EXPECT_EQ(OK
, socket
.SetMulticastLoopbackMode(false));
573 EXPECT_EQ(OK
, socket
.SetMulticastLoopbackMode(true));
574 EXPECT_EQ(OK
, socket
.SetMulticastTimeToLive(0));
575 EXPECT_EQ(OK
, socket
.SetMulticastTimeToLive(3));
576 EXPECT_NE(OK
, socket
.SetMulticastTimeToLive(-1));
577 EXPECT_EQ(OK
, socket
.SetMulticastInterface(0));
579 EXPECT_EQ(OK
, socket
.Bind(bind_address
));
581 EXPECT_NE(OK
, socket
.SetMulticastLoopbackMode(false));
582 EXPECT_NE(OK
, socket
.SetMulticastTimeToLive(0));
583 EXPECT_NE(OK
, socket
.SetMulticastInterface(0));