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_socket.h"
7 #include "net/udp/udp_client_socket.h"
8 #include "net/udp/udp_server_socket.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/location.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/run_loop.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/stl_util.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "net/base/io_buffer.h"
19 #include "net/base/ip_endpoint.h"
20 #include "net/base/net_errors.h"
21 #include "net/base/net_util.h"
22 #include "net/base/test_completion_callback.h"
23 #include "net/log/test_net_log.h"
24 #include "net/log/test_net_log_entry.h"
25 #include "net/log/test_net_log_util.h"
26 #include "net/test/net_test_suite.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "testing/platform_test.h"
34 class UDPSocketTest
: public PlatformTest
{
36 UDPSocketTest() : buffer_(new IOBufferWithSize(kMaxRead
)) {}
38 // Blocks until data is read from the socket.
39 std::string
RecvFromSocket(UDPServerSocket
* socket
) {
40 TestCompletionCallback callback
;
42 int rv
= socket
->RecvFrom(
43 buffer_
.get(), kMaxRead
, &recv_from_address_
, callback
.callback());
44 if (rv
== ERR_IO_PENDING
)
45 rv
= callback
.WaitForResult();
47 return std::string(); // error!
48 return std::string(buffer_
->data(), rv
);
51 // Loop until |msg| has been written to the socket or until an
53 // If |address| is specified, then it is used for the destination
54 // to send to. Otherwise, will send to the last socket this server
56 int SendToSocket(UDPServerSocket
* socket
, const std::string
& msg
) {
57 return SendToSocket(socket
, msg
, recv_from_address_
);
60 int SendToSocket(UDPServerSocket
* socket
,
62 const IPEndPoint
& address
) {
63 TestCompletionCallback callback
;
65 int length
= msg
.length();
66 scoped_refptr
<StringIOBuffer
> io_buffer(new StringIOBuffer(msg
));
67 scoped_refptr
<DrainableIOBuffer
> buffer(
68 new DrainableIOBuffer(io_buffer
.get(), length
));
71 while (buffer
->BytesRemaining()) {
72 int rv
= socket
->SendTo(
73 buffer
.get(), buffer
->BytesRemaining(), address
, callback
.callback());
74 if (rv
== ERR_IO_PENDING
)
75 rv
= callback
.WaitForResult();
77 return bytes_sent
> 0 ? bytes_sent
: rv
;
79 buffer
->DidConsume(rv
);
84 std::string
ReadSocket(UDPClientSocket
* socket
) {
85 TestCompletionCallback callback
;
87 int rv
= socket
->Read(buffer_
.get(), kMaxRead
, callback
.callback());
88 if (rv
== ERR_IO_PENDING
)
89 rv
= callback
.WaitForResult();
91 return std::string(); // error!
92 return std::string(buffer_
->data(), rv
);
95 // Loop until |msg| has been written to the socket or until an
97 int WriteSocket(UDPClientSocket
* socket
, const std::string
& msg
) {
98 TestCompletionCallback callback
;
100 int length
= msg
.length();
101 scoped_refptr
<StringIOBuffer
> io_buffer(new StringIOBuffer(msg
));
102 scoped_refptr
<DrainableIOBuffer
> buffer(
103 new DrainableIOBuffer(io_buffer
.get(), length
));
106 while (buffer
->BytesRemaining()) {
107 int rv
= socket
->Write(
108 buffer
.get(), buffer
->BytesRemaining(), callback
.callback());
109 if (rv
== ERR_IO_PENDING
)
110 rv
= callback
.WaitForResult();
112 return bytes_sent
> 0 ? bytes_sent
: rv
;
114 buffer
->DidConsume(rv
);
119 void WriteSocketIgnoreResult(UDPClientSocket
* socket
,
120 const std::string
& msg
) {
121 WriteSocket(socket
, msg
);
124 // Creates an address from ip address and port and writes it to |*address|.
125 void CreateUDPAddress(const std::string
& ip_str
,
127 IPEndPoint
* address
) {
128 IPAddressNumber ip_number
;
129 bool rv
= ParseIPLiteralToNumber(ip_str
, &ip_number
);
132 *address
= IPEndPoint(ip_number
, port
);
135 // Run unit test for a connection test.
136 // |use_nonblocking_io| is used to switch between overlapped and non-blocking
137 // IO on Windows. It has no effect in other ports.
138 void ConnectTest(bool use_nonblocking_io
);
141 static const int kMaxRead
= 1024;
142 scoped_refptr
<IOBufferWithSize
> buffer_
;
143 IPEndPoint recv_from_address_
;
146 void ReadCompleteCallback(int* result_out
, base::Closure callback
, int result
) {
147 *result_out
= result
;
151 void UDPSocketTest::ConnectTest(bool use_nonblocking_io
) {
152 const uint16 kPort
= 9999;
153 std::string
simple_message("hello world!");
155 // Setup the server to listen.
156 IPEndPoint bind_address
;
157 CreateUDPAddress("127.0.0.1", kPort
, &bind_address
);
158 TestNetLog server_log
;
159 scoped_ptr
<UDPServerSocket
> server(
160 new UDPServerSocket(&server_log
, NetLog::Source()));
162 if (use_nonblocking_io
)
163 server
->UseNonBlockingIO();
165 server
->AllowAddressReuse();
166 int rv
= server
->Listen(bind_address
);
170 IPEndPoint server_address
;
171 CreateUDPAddress("127.0.0.1", kPort
, &server_address
);
172 TestNetLog client_log
;
173 scoped_ptr
<UDPClientSocket
> client(
174 new UDPClientSocket(DatagramSocket::DEFAULT_BIND
, RandIntCallback(),
175 &client_log
, NetLog::Source()));
177 if (use_nonblocking_io
)
178 client
->UseNonBlockingIO();
181 rv
= client
->Connect(server_address
);
184 // Client sends to the server.
185 rv
= WriteSocket(client
.get(), simple_message
);
186 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(rv
));
188 // Server waits for message.
189 std::string str
= RecvFromSocket(server
.get());
190 DCHECK(simple_message
== str
);
192 // Server echoes reply.
193 rv
= SendToSocket(server
.get(), simple_message
);
194 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(rv
));
196 // Client waits for response.
197 str
= ReadSocket(client
.get());
198 DCHECK(simple_message
== str
);
200 // Test asynchronous read. Server waits for message.
201 base::RunLoop run_loop
;
203 rv
= server
->RecvFrom(
204 buffer_
.get(), kMaxRead
, &recv_from_address_
,
205 base::Bind(&ReadCompleteCallback
, &read_result
, run_loop
.QuitClosure()));
206 EXPECT_EQ(ERR_IO_PENDING
, rv
);
208 // Client sends to the server.
209 base::ThreadTaskRunnerHandle::Get()->PostTask(
211 base::Bind(&UDPSocketTest::WriteSocketIgnoreResult
,
212 base::Unretained(this), client
.get(), simple_message
));
214 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(read_result
));
215 EXPECT_EQ(simple_message
, std::string(buffer_
->data(), read_result
));
217 // Delete sockets so they log their final events.
221 // Check the server's log.
222 TestNetLogEntry::List server_entries
;
223 server_log
.GetEntries(&server_entries
);
224 EXPECT_EQ(5u, server_entries
.size());
226 LogContainsBeginEvent(server_entries
, 0, NetLog::TYPE_SOCKET_ALIVE
));
227 EXPECT_TRUE(LogContainsEvent(
228 server_entries
, 1, NetLog::TYPE_UDP_BYTES_RECEIVED
, NetLog::PHASE_NONE
));
229 EXPECT_TRUE(LogContainsEvent(server_entries
, 2, NetLog::TYPE_UDP_BYTES_SENT
,
230 NetLog::PHASE_NONE
));
231 EXPECT_TRUE(LogContainsEvent(
232 server_entries
, 3, NetLog::TYPE_UDP_BYTES_RECEIVED
, NetLog::PHASE_NONE
));
234 LogContainsEndEvent(server_entries
, 4, NetLog::TYPE_SOCKET_ALIVE
));
236 // Check the client's log.
237 TestNetLogEntry::List client_entries
;
238 client_log
.GetEntries(&client_entries
);
239 EXPECT_EQ(7u, client_entries
.size());
241 LogContainsBeginEvent(client_entries
, 0, NetLog::TYPE_SOCKET_ALIVE
));
243 LogContainsBeginEvent(client_entries
, 1, NetLog::TYPE_UDP_CONNECT
));
244 EXPECT_TRUE(LogContainsEndEvent(client_entries
, 2, NetLog::TYPE_UDP_CONNECT
));
245 EXPECT_TRUE(LogContainsEvent(client_entries
, 3, NetLog::TYPE_UDP_BYTES_SENT
,
246 NetLog::PHASE_NONE
));
247 EXPECT_TRUE(LogContainsEvent(
248 client_entries
, 4, NetLog::TYPE_UDP_BYTES_RECEIVED
, NetLog::PHASE_NONE
));
249 EXPECT_TRUE(LogContainsEvent(client_entries
, 5, NetLog::TYPE_UDP_BYTES_SENT
,
250 NetLog::PHASE_NONE
));
252 LogContainsEndEvent(client_entries
, 6, NetLog::TYPE_SOCKET_ALIVE
));
255 TEST_F(UDPSocketTest
, Connect
) {
256 // The variable |use_nonblocking_io| has no effect in non-Windows ports.
261 TEST_F(UDPSocketTest
, ConnectNonBlocking
) {
266 #if defined(OS_MACOSX)
267 // UDPSocketPrivate_Broadcast is disabled for OSX because it requires
268 // root permissions on OSX 10.7+.
269 TEST_F(UDPSocketTest
, DISABLED_Broadcast
) {
270 #elif defined(OS_ANDROID)
271 // Disabled for Android because devices attached to testbots don't have default
272 // network, so broadcasting to 255.255.255.255 returns error -109 (Address not
273 // reachable). crbug.com/139144.
274 TEST_F(UDPSocketTest
, DISABLED_Broadcast
) {
276 TEST_F(UDPSocketTest
, Broadcast
) {
278 const uint16 kPort
= 9999;
279 std::string
first_message("first message"), second_message("second message");
281 IPEndPoint broadcast_address
;
282 CreateUDPAddress("255.255.255.255", kPort
, &broadcast_address
);
283 IPEndPoint listen_address
;
284 CreateUDPAddress("0.0.0.0", kPort
, &listen_address
);
286 TestNetLog server1_log
, server2_log
;
287 scoped_ptr
<UDPServerSocket
> server1(
288 new UDPServerSocket(&server1_log
, NetLog::Source()));
289 scoped_ptr
<UDPServerSocket
> server2(
290 new UDPServerSocket(&server2_log
, NetLog::Source()));
291 server1
->AllowAddressReuse();
292 server1
->AllowBroadcast();
293 server2
->AllowAddressReuse();
294 server2
->AllowBroadcast();
296 int rv
= server1
->Listen(listen_address
);
298 rv
= server2
->Listen(listen_address
);
301 rv
= SendToSocket(server1
.get(), first_message
, broadcast_address
);
302 ASSERT_EQ(static_cast<int>(first_message
.size()), rv
);
303 std::string str
= RecvFromSocket(server1
.get());
304 ASSERT_EQ(first_message
, str
);
305 str
= RecvFromSocket(server2
.get());
306 ASSERT_EQ(first_message
, str
);
308 rv
= SendToSocket(server2
.get(), second_message
, broadcast_address
);
309 ASSERT_EQ(static_cast<int>(second_message
.size()), rv
);
310 str
= RecvFromSocket(server1
.get());
311 ASSERT_EQ(second_message
, str
);
312 str
= RecvFromSocket(server2
.get());
313 ASSERT_EQ(second_message
, str
);
316 // In this test, we verify that random binding logic works, which attempts
317 // to bind to a random port and returns if succeeds, otherwise retries for
318 // |kBindRetries| number of times.
320 // To generate the scenario, we first create |kBindRetries| number of
321 // UDPClientSockets with default binding policy and connect to the same
322 // peer and save the used port numbers. Then we get rid of the last
323 // socket, making sure that the local port it was bound to is available.
324 // Finally, we create a socket with random binding policy, passing it a
325 // test PRNG that would serve used port numbers in the array, one after
326 // another. At the end, we make sure that the test socket was bound to the
327 // port that became available after deleting the last socket with default
330 // We do not test the randomness of bound ports, but that we are using
331 // passed in PRNG correctly, thus, it's the duty of PRNG to produce strong
333 static const int kBindRetries
= 10;
337 explicit TestPrng(const std::deque
<int>& numbers
) : numbers_(numbers
) {}
338 int GetNext(int /* min */, int /* max */) {
339 DCHECK(!numbers_
.empty());
340 int rv
= numbers_
.front();
341 numbers_
.pop_front();
345 std::deque
<int> numbers_
;
347 DISALLOW_COPY_AND_ASSIGN(TestPrng
);
350 TEST_F(UDPSocketTest
, ConnectRandomBind
) {
351 std::vector
<UDPClientSocket
*> sockets
;
352 IPEndPoint peer_address
;
353 CreateUDPAddress("127.0.0.1", 53, &peer_address
);
355 // Create and connect sockets and save port numbers.
356 std::deque
<int> used_ports
;
357 for (int i
= 0; i
< kBindRetries
; ++i
) {
358 UDPClientSocket
* socket
=
359 new UDPClientSocket(DatagramSocket::DEFAULT_BIND
,
363 sockets
.push_back(socket
);
364 EXPECT_EQ(OK
, socket
->Connect(peer_address
));
366 IPEndPoint client_address
;
367 EXPECT_EQ(OK
, socket
->GetLocalAddress(&client_address
));
368 used_ports
.push_back(client_address
.port());
371 // Free the last socket, its local port is still in |used_ports|.
372 delete sockets
.back();
375 TestPrng
test_prng(used_ports
);
376 RandIntCallback rand_int_cb
=
377 base::Bind(&TestPrng::GetNext
, base::Unretained(&test_prng
));
379 // Create a socket with random binding policy and connect.
380 scoped_ptr
<UDPClientSocket
> test_socket(
381 new UDPClientSocket(DatagramSocket::RANDOM_BIND
,
385 EXPECT_EQ(OK
, test_socket
->Connect(peer_address
));
387 // Make sure that the last port number in the |used_ports| was used.
388 IPEndPoint client_address
;
389 EXPECT_EQ(OK
, test_socket
->GetLocalAddress(&client_address
));
390 EXPECT_EQ(used_ports
.back(), client_address
.port());
392 STLDeleteElements(&sockets
);
395 // Return a privileged port (under 1024) so binding will fail.
396 int PrivilegedRand(int min
, int max
) {
397 // Chosen by fair dice roll. Guaranteed to be random.
401 TEST_F(UDPSocketTest
, ConnectFail
) {
402 IPEndPoint peer_address
;
403 CreateUDPAddress("0.0.0.0", 53, &peer_address
);
405 scoped_ptr
<UDPSocket
> socket(
406 new UDPSocket(DatagramSocket::RANDOM_BIND
,
407 base::Bind(&PrivilegedRand
),
410 int rv
= socket
->Open(peer_address
.GetFamily());
412 rv
= socket
->Connect(peer_address
);
413 // Connect should have failed since we couldn't bind to that port,
415 // Make sure that UDPSocket actually closed the socket.
416 EXPECT_FALSE(socket
->is_connected());
419 // In this test, we verify that connect() on a socket will have the effect
420 // of filtering reads on this socket only to data read from the destination
423 // The purpose of this test is that some documentation indicates that connect
424 // binds the client's sends to send to a particular server endpoint, but does
425 // not bind the client's reads to only be from that endpoint, and that we need
426 // to always use recvfrom() to disambiguate.
427 TEST_F(UDPSocketTest
, VerifyConnectBindsAddr
) {
428 const uint16 kPort1
= 9999;
429 const uint16 kPort2
= 10000;
430 std::string
simple_message("hello world!");
431 std::string
foreign_message("BAD MESSAGE TO GET!!");
433 // Setup the first server to listen.
434 IPEndPoint bind_address
;
435 CreateUDPAddress("127.0.0.1", kPort1
, &bind_address
);
436 UDPServerSocket
server1(NULL
, NetLog::Source());
437 server1
.AllowAddressReuse();
438 int rv
= server1
.Listen(bind_address
);
441 // Setup the second server to listen.
442 CreateUDPAddress("127.0.0.1", kPort2
, &bind_address
);
443 UDPServerSocket
server2(NULL
, NetLog::Source());
444 server2
.AllowAddressReuse();
445 rv
= server2
.Listen(bind_address
);
448 // Setup the client, connected to server 1.
449 IPEndPoint server_address
;
450 CreateUDPAddress("127.0.0.1", kPort1
, &server_address
);
451 UDPClientSocket
client(DatagramSocket::DEFAULT_BIND
,
455 rv
= client
.Connect(server_address
);
458 // Client sends to server1.
459 rv
= WriteSocket(&client
, simple_message
);
460 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(rv
));
462 // Server1 waits for message.
463 std::string str
= RecvFromSocket(&server1
);
464 DCHECK(simple_message
== str
);
466 // Get the client's address.
467 IPEndPoint client_address
;
468 rv
= client
.GetLocalAddress(&client_address
);
471 // Server2 sends reply.
472 rv
= SendToSocket(&server2
, foreign_message
,
474 EXPECT_EQ(foreign_message
.length(), static_cast<size_t>(rv
));
476 // Server1 sends reply.
477 rv
= SendToSocket(&server1
, simple_message
,
479 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(rv
));
481 // Client waits for response.
482 str
= ReadSocket(&client
);
483 DCHECK(simple_message
== str
);
486 TEST_F(UDPSocketTest
, ClientGetLocalPeerAddresses
) {
488 std::string remote_address
;
489 std::string local_address
;
492 { "127.0.00.1", "127.0.0.1", false },
493 { "::1", "::1", true },
494 #if !defined(OS_ANDROID) && !defined(OS_IOS)
495 // Addresses below are disabled on Android. See crbug.com/161248
496 // They are also disabled on iOS. See https://crbug.com/523225
497 { "192.168.1.1", "127.0.0.1", false },
498 { "2001:db8:0::42", "::1", true },
501 for (size_t i
= 0; i
< arraysize(tests
); i
++) {
502 SCOPED_TRACE(std::string("Connecting from ") + tests
[i
].local_address
+
503 std::string(" to ") + tests
[i
].remote_address
);
505 IPAddressNumber ip_number
;
506 ParseIPLiteralToNumber(tests
[i
].remote_address
, &ip_number
);
507 IPEndPoint
remote_address(ip_number
, 80);
508 ParseIPLiteralToNumber(tests
[i
].local_address
, &ip_number
);
509 IPEndPoint
local_address(ip_number
, 80);
511 UDPClientSocket
client(DatagramSocket::DEFAULT_BIND
,
515 int rv
= client
.Connect(remote_address
);
516 if (tests
[i
].may_fail
&& rv
== ERR_ADDRESS_UNREACHABLE
) {
517 // Connect() may return ERR_ADDRESS_UNREACHABLE for IPv6
518 // addresses if IPv6 is not configured.
522 EXPECT_LE(ERR_IO_PENDING
, rv
);
524 IPEndPoint fetched_local_address
;
525 rv
= client
.GetLocalAddress(&fetched_local_address
);
528 // TODO(mbelshe): figure out how to verify the IP and port.
529 // The port is dynamically generated by the udp stack.
530 // The IP is the real IP of the client, not necessarily
532 //EXPECT_EQ(local_address.address(), fetched_local_address.address());
534 IPEndPoint fetched_remote_address
;
535 rv
= client
.GetPeerAddress(&fetched_remote_address
);
538 EXPECT_EQ(remote_address
, fetched_remote_address
);
542 TEST_F(UDPSocketTest
, ServerGetLocalAddress
) {
543 IPEndPoint bind_address
;
544 CreateUDPAddress("127.0.0.1", 0, &bind_address
);
545 UDPServerSocket
server(NULL
, NetLog::Source());
546 int rv
= server
.Listen(bind_address
);
549 IPEndPoint local_address
;
550 rv
= server
.GetLocalAddress(&local_address
);
553 // Verify that port was allocated.
554 EXPECT_GT(local_address
.port(), 0);
555 EXPECT_EQ(local_address
.address(), bind_address
.address());
558 TEST_F(UDPSocketTest
, ServerGetPeerAddress
) {
559 IPEndPoint bind_address
;
560 CreateUDPAddress("127.0.0.1", 0, &bind_address
);
561 UDPServerSocket
server(NULL
, NetLog::Source());
562 int rv
= server
.Listen(bind_address
);
565 IPEndPoint peer_address
;
566 rv
= server
.GetPeerAddress(&peer_address
);
567 EXPECT_EQ(rv
, ERR_SOCKET_NOT_CONNECTED
);
570 // Close the socket while read is pending.
571 TEST_F(UDPSocketTest
, CloseWithPendingRead
) {
572 IPEndPoint bind_address
;
573 CreateUDPAddress("127.0.0.1", 0, &bind_address
);
574 UDPServerSocket
server(NULL
, NetLog::Source());
575 int rv
= server
.Listen(bind_address
);
578 TestCompletionCallback callback
;
580 rv
= server
.RecvFrom(buffer_
.get(), kMaxRead
, &from
, callback
.callback());
581 EXPECT_EQ(rv
, ERR_IO_PENDING
);
585 EXPECT_FALSE(callback
.have_result());
588 #if defined(OS_ANDROID)
589 // Some Android devices do not support multicast socket.
590 // The ones supporting multicast need WifiManager.MulitcastLock to enable it.
591 // http://goo.gl/jjAk9
592 #define MAYBE_JoinMulticastGroup DISABLED_JoinMulticastGroup
594 #define MAYBE_JoinMulticastGroup JoinMulticastGroup
595 #endif // defined(OS_ANDROID)
597 TEST_F(UDPSocketTest
, MAYBE_JoinMulticastGroup
) {
598 const uint16 kPort
= 9999;
599 const char kGroup
[] = "237.132.100.17";
601 IPEndPoint bind_address
;
602 CreateUDPAddress("0.0.0.0", kPort
, &bind_address
);
603 IPAddressNumber group_ip
;
604 EXPECT_TRUE(ParseIPLiteralToNumber(kGroup
, &group_ip
));
606 UDPSocket
socket(DatagramSocket::DEFAULT_BIND
,
610 EXPECT_EQ(OK
, socket
.Open(bind_address
.GetFamily()));
611 EXPECT_EQ(OK
, socket
.Bind(bind_address
));
612 EXPECT_EQ(OK
, socket
.JoinGroup(group_ip
));
613 // Joining group multiple times.
614 EXPECT_NE(OK
, socket
.JoinGroup(group_ip
));
615 EXPECT_EQ(OK
, socket
.LeaveGroup(group_ip
));
616 // Leaving group multiple times.
617 EXPECT_NE(OK
, socket
.LeaveGroup(group_ip
));
622 TEST_F(UDPSocketTest
, MulticastOptions
) {
623 const uint16 kPort
= 9999;
624 IPEndPoint bind_address
;
625 CreateUDPAddress("0.0.0.0", kPort
, &bind_address
);
627 UDPSocket
socket(DatagramSocket::DEFAULT_BIND
,
632 EXPECT_EQ(OK
, socket
.SetMulticastLoopbackMode(false));
633 EXPECT_EQ(OK
, socket
.SetMulticastLoopbackMode(true));
634 EXPECT_EQ(OK
, socket
.SetMulticastTimeToLive(0));
635 EXPECT_EQ(OK
, socket
.SetMulticastTimeToLive(3));
636 EXPECT_NE(OK
, socket
.SetMulticastTimeToLive(-1));
637 EXPECT_EQ(OK
, socket
.SetMulticastInterface(0));
639 EXPECT_EQ(OK
, socket
.Open(bind_address
.GetFamily()));
640 EXPECT_EQ(OK
, socket
.Bind(bind_address
));
642 EXPECT_NE(OK
, socket
.SetMulticastLoopbackMode(false));
643 EXPECT_NE(OK
, socket
.SetMulticastTimeToLive(0));
644 EXPECT_NE(OK
, socket
.SetMulticastInterface(0));
649 // Checking that DSCP bits are set correctly is difficult,
650 // but let's check that the code doesn't crash at least.
651 TEST_F(UDPSocketTest
, SetDSCP
) {
652 // Setup the server to listen.
653 IPEndPoint bind_address
;
654 UDPSocket
client(DatagramSocket::DEFAULT_BIND
,
658 // We need a real IP, but we won't actually send anything to it.
659 CreateUDPAddress("8.8.8.8", 9999, &bind_address
);
660 int rv
= client
.Open(bind_address
.GetFamily());
663 rv
= client
.Connect(bind_address
);
665 // Let's try localhost then..
666 CreateUDPAddress("127.0.0.1", 9999, &bind_address
);
667 rv
= client
.Connect(bind_address
);
671 client
.SetDiffServCodePoint(DSCP_NO_CHANGE
);
672 client
.SetDiffServCodePoint(DSCP_AF41
);
673 client
.SetDiffServCodePoint(DSCP_DEFAULT
);
674 client
.SetDiffServCodePoint(DSCP_CS2
);
675 client
.SetDiffServCodePoint(DSCP_NO_CHANGE
);
676 client
.SetDiffServCodePoint(DSCP_DEFAULT
);
686 const HANDLE kFakeHandle
= (HANDLE
)19;
687 const QOS_FLOWID kFakeFlowId
= (QOS_FLOWID
)27;
689 BOOL WINAPI
FakeQOSCreateHandleFAIL(PQOS_VERSION version
, PHANDLE handle
) {
690 EXPECT_EQ(0, version
->MinorVersion
);
691 EXPECT_EQ(1, version
->MajorVersion
);
692 SetLastError(ERROR_OPEN_FAILED
);
696 BOOL WINAPI
FakeQOSCreateHandle(PQOS_VERSION version
, PHANDLE handle
) {
697 EXPECT_EQ(0, version
->MinorVersion
);
698 EXPECT_EQ(1, version
->MajorVersion
);
699 *handle
= kFakeHandle
;
703 BOOL WINAPI
FakeQOSCloseHandle(HANDLE handle
) {
704 EXPECT_EQ(kFakeHandle
, handle
);
708 QOS_TRAFFIC_TYPE g_expected_traffic_type
;
710 BOOL WINAPI
FakeQOSAddSocketToFlow(HANDLE handle
,
713 QOS_TRAFFIC_TYPE traffic_type
,
715 PQOS_FLOWID flow_id
) {
716 EXPECT_EQ(kFakeHandle
, handle
);
717 EXPECT_EQ(NULL
, addr
);
718 EXPECT_EQ(QOS_NON_ADAPTIVE_FLOW
, flags
);
719 EXPECT_EQ(0, *flow_id
);
720 *flow_id
= kFakeFlowId
;
724 BOOL WINAPI
FakeQOSRemoveSocketFromFlow(HANDLE handle
,
728 EXPECT_EQ(kFakeHandle
, handle
);
729 EXPECT_EQ(NULL
, socket
);
730 EXPECT_EQ(kFakeFlowId
, flowid
);
731 EXPECT_EQ(0, reserved
);
735 DWORD g_expected_dscp
;
737 BOOL WINAPI
FakeQOSSetFlow(HANDLE handle
,
743 LPOVERLAPPED overlapped
) {
744 EXPECT_EQ(kFakeHandle
, handle
);
745 EXPECT_EQ(QOSSetOutgoingDSCPValue
, op
);
746 EXPECT_EQ(sizeof(DWORD
), size
);
747 EXPECT_EQ(g_expected_dscp
, *reinterpret_cast<DWORD
*>(data
));
748 EXPECT_EQ(kFakeFlowId
, flow_id
);
749 EXPECT_EQ(0, reserved
);
750 EXPECT_EQ(NULL
, overlapped
);
756 // Mock out the Qwave functions and make sure they are
757 // called correctly. Must be in net namespace for friendship
759 TEST_F(UDPSocketTest
, SetDSCPFake
) {
760 // Setup the server to listen.
761 IPEndPoint bind_address
;
762 // We need a real IP, but we won't actually send anything to it.
763 CreateUDPAddress("8.8.8.8", 9999, &bind_address
);
764 UDPSocket
client(DatagramSocket::DEFAULT_BIND
,
768 int rv
= client
.SetDiffServCodePoint(DSCP_AF41
);
769 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED
, rv
);
771 rv
= client
.Open(bind_address
.GetFamily());
774 rv
= client
.Connect(bind_address
);
777 QwaveAPI
& qos(QwaveAPI::Get());
778 qos
.create_handle_func_
= FakeQOSCreateHandleFAIL
;
779 qos
.close_handle_func_
= FakeQOSCloseHandle
;
780 qos
.add_socket_to_flow_func_
= FakeQOSAddSocketToFlow
;
781 qos
.remove_socket_from_flow_func_
= FakeQOSRemoveSocketFromFlow
;
782 qos
.set_flow_func_
= FakeQOSSetFlow
;
783 qos
.qwave_supported_
= true;
785 EXPECT_EQ(OK
, client
.SetDiffServCodePoint(DSCP_NO_CHANGE
));
786 EXPECT_EQ(ERROR_NOT_SUPPORTED
, client
.SetDiffServCodePoint(DSCP_AF41
));
787 qos
.create_handle_func_
= FakeQOSCreateHandle
;
788 g_expected_dscp
= DSCP_AF41
;
789 g_expected_traffic_type
= QOSTrafficTypeAudioVideo
;
790 EXPECT_EQ(OK
, client
.SetDiffServCodePoint(DSCP_AF41
));
791 g_expected_dscp
= DSCP_DEFAULT
;
792 g_expected_traffic_type
= QOSTrafficTypeBestEffort
;
793 EXPECT_EQ(OK
, client
.SetDiffServCodePoint(DSCP_DEFAULT
));
794 g_expected_dscp
= DSCP_CS2
;
795 g_expected_traffic_type
= QOSTrafficTypeExcellentEffort
;
796 EXPECT_EQ(OK
, client
.SetDiffServCodePoint(DSCP_CS2
));
797 g_expected_dscp
= DSCP_CS3
;
798 g_expected_traffic_type
= QOSTrafficTypeExcellentEffort
;
799 EXPECT_EQ(OK
, client
.SetDiffServCodePoint(DSCP_NO_CHANGE
));
800 g_expected_dscp
= DSCP_DEFAULT
;
801 g_expected_traffic_type
= QOSTrafficTypeBestEffort
;
802 EXPECT_EQ(OK
, client
.SetDiffServCodePoint(DSCP_DEFAULT
));