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
, 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
, 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
, std::string msg
) {
120 WriteSocket(socket
, msg
);
123 // Creates an address from ip address and port and writes it to |*address|.
124 void CreateUDPAddress(std::string ip_str
, uint16 port
, IPEndPoint
* address
) {
125 IPAddressNumber ip_number
;
126 bool rv
= ParseIPLiteralToNumber(ip_str
, &ip_number
);
129 *address
= IPEndPoint(ip_number
, port
);
132 // Run unit test for a connection test.
133 // |use_nonblocking_io| is used to switch between overlapped and non-blocking
134 // IO on Windows. It has no effect in other ports.
135 void ConnectTest(bool use_nonblocking_io
);
138 static const int kMaxRead
= 1024;
139 scoped_refptr
<IOBufferWithSize
> buffer_
;
140 IPEndPoint recv_from_address_
;
143 void ReadCompleteCallback(int* result_out
, base::Closure callback
, int result
) {
144 *result_out
= result
;
148 void UDPSocketTest::ConnectTest(bool use_nonblocking_io
) {
149 const uint16 kPort
= 9999;
150 std::string
simple_message("hello world!");
152 // Setup the server to listen.
153 IPEndPoint bind_address
;
154 CreateUDPAddress("127.0.0.1", kPort
, &bind_address
);
155 TestNetLog server_log
;
156 scoped_ptr
<UDPServerSocket
> server(
157 new UDPServerSocket(&server_log
, NetLog::Source()));
159 if (use_nonblocking_io
)
160 server
->UseNonBlockingIO();
162 server
->AllowAddressReuse();
163 int rv
= server
->Listen(bind_address
);
167 IPEndPoint server_address
;
168 CreateUDPAddress("127.0.0.1", kPort
, &server_address
);
169 TestNetLog client_log
;
170 scoped_ptr
<UDPClientSocket
> client(
171 new UDPClientSocket(DatagramSocket::DEFAULT_BIND
, RandIntCallback(),
172 &client_log
, NetLog::Source()));
174 if (use_nonblocking_io
)
175 client
->UseNonBlockingIO();
178 rv
= client
->Connect(server_address
);
181 // Client sends to the server.
182 rv
= WriteSocket(client
.get(), simple_message
);
183 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(rv
));
185 // Server waits for message.
186 std::string str
= RecvFromSocket(server
.get());
187 DCHECK(simple_message
== str
);
189 // Server echoes reply.
190 rv
= SendToSocket(server
.get(), simple_message
);
191 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(rv
));
193 // Client waits for response.
194 str
= ReadSocket(client
.get());
195 DCHECK(simple_message
== str
);
197 // Test asynchronous read. Server waits for message.
198 base::RunLoop run_loop
;
200 rv
= server
->RecvFrom(
201 buffer_
.get(), kMaxRead
, &recv_from_address_
,
202 base::Bind(&ReadCompleteCallback
, &read_result
, run_loop
.QuitClosure()));
203 EXPECT_EQ(ERR_IO_PENDING
, rv
);
205 // Client sends to the server.
206 base::ThreadTaskRunnerHandle::Get()->PostTask(
208 base::Bind(&UDPSocketTest::WriteSocketIgnoreResult
,
209 base::Unretained(this), client
.get(), simple_message
));
211 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(read_result
));
212 EXPECT_EQ(simple_message
, std::string(buffer_
->data(), read_result
));
214 // Delete sockets so they log their final events.
218 // Check the server's log.
219 TestNetLogEntry::List server_entries
;
220 server_log
.GetEntries(&server_entries
);
221 EXPECT_EQ(5u, server_entries
.size());
223 LogContainsBeginEvent(server_entries
, 0, NetLog::TYPE_SOCKET_ALIVE
));
224 EXPECT_TRUE(LogContainsEvent(
225 server_entries
, 1, NetLog::TYPE_UDP_BYTES_RECEIVED
, NetLog::PHASE_NONE
));
226 EXPECT_TRUE(LogContainsEvent(server_entries
, 2, NetLog::TYPE_UDP_BYTES_SENT
,
227 NetLog::PHASE_NONE
));
228 EXPECT_TRUE(LogContainsEvent(
229 server_entries
, 3, NetLog::TYPE_UDP_BYTES_RECEIVED
, NetLog::PHASE_NONE
));
231 LogContainsEndEvent(server_entries
, 4, NetLog::TYPE_SOCKET_ALIVE
));
233 // Check the client's log.
234 TestNetLogEntry::List client_entries
;
235 client_log
.GetEntries(&client_entries
);
236 EXPECT_EQ(7u, client_entries
.size());
238 LogContainsBeginEvent(client_entries
, 0, NetLog::TYPE_SOCKET_ALIVE
));
240 LogContainsBeginEvent(client_entries
, 1, NetLog::TYPE_UDP_CONNECT
));
241 EXPECT_TRUE(LogContainsEndEvent(client_entries
, 2, NetLog::TYPE_UDP_CONNECT
));
242 EXPECT_TRUE(LogContainsEvent(client_entries
, 3, NetLog::TYPE_UDP_BYTES_SENT
,
243 NetLog::PHASE_NONE
));
244 EXPECT_TRUE(LogContainsEvent(
245 client_entries
, 4, NetLog::TYPE_UDP_BYTES_RECEIVED
, NetLog::PHASE_NONE
));
246 EXPECT_TRUE(LogContainsEvent(client_entries
, 5, NetLog::TYPE_UDP_BYTES_SENT
,
247 NetLog::PHASE_NONE
));
249 LogContainsEndEvent(client_entries
, 6, NetLog::TYPE_SOCKET_ALIVE
));
252 TEST_F(UDPSocketTest
, Connect
) {
253 // The variable |use_nonblocking_io| has no effect in non-Windows ports.
258 TEST_F(UDPSocketTest
, ConnectNonBlocking
) {
263 #if defined(OS_MACOSX)
264 // UDPSocketPrivate_Broadcast is disabled for OSX because it requires
265 // root permissions on OSX 10.7+.
266 TEST_F(UDPSocketTest
, DISABLED_Broadcast
) {
267 #elif defined(OS_ANDROID)
268 // Disabled for Android because devices attached to testbots don't have default
269 // network, so broadcasting to 255.255.255.255 returns error -109 (Address not
270 // reachable). crbug.com/139144.
271 TEST_F(UDPSocketTest
, DISABLED_Broadcast
) {
273 TEST_F(UDPSocketTest
, Broadcast
) {
275 const uint16 kPort
= 9999;
276 std::string
first_message("first message"), second_message("second message");
278 IPEndPoint broadcast_address
;
279 CreateUDPAddress("255.255.255.255", kPort
, &broadcast_address
);
280 IPEndPoint listen_address
;
281 CreateUDPAddress("0.0.0.0", kPort
, &listen_address
);
283 TestNetLog server1_log
, server2_log
;
284 scoped_ptr
<UDPServerSocket
> server1(
285 new UDPServerSocket(&server1_log
, NetLog::Source()));
286 scoped_ptr
<UDPServerSocket
> server2(
287 new UDPServerSocket(&server2_log
, NetLog::Source()));
288 server1
->AllowAddressReuse();
289 server1
->AllowBroadcast();
290 server2
->AllowAddressReuse();
291 server2
->AllowBroadcast();
293 int rv
= server1
->Listen(listen_address
);
295 rv
= server2
->Listen(listen_address
);
298 rv
= SendToSocket(server1
.get(), first_message
, broadcast_address
);
299 ASSERT_EQ(static_cast<int>(first_message
.size()), rv
);
300 std::string str
= RecvFromSocket(server1
.get());
301 ASSERT_EQ(first_message
, str
);
302 str
= RecvFromSocket(server2
.get());
303 ASSERT_EQ(first_message
, str
);
305 rv
= SendToSocket(server2
.get(), second_message
, broadcast_address
);
306 ASSERT_EQ(static_cast<int>(second_message
.size()), rv
);
307 str
= RecvFromSocket(server1
.get());
308 ASSERT_EQ(second_message
, str
);
309 str
= RecvFromSocket(server2
.get());
310 ASSERT_EQ(second_message
, str
);
313 // In this test, we verify that random binding logic works, which attempts
314 // to bind to a random port and returns if succeeds, otherwise retries for
315 // |kBindRetries| number of times.
317 // To generate the scenario, we first create |kBindRetries| number of
318 // UDPClientSockets with default binding policy and connect to the same
319 // peer and save the used port numbers. Then we get rid of the last
320 // socket, making sure that the local port it was bound to is available.
321 // Finally, we create a socket with random binding policy, passing it a
322 // test PRNG that would serve used port numbers in the array, one after
323 // another. At the end, we make sure that the test socket was bound to the
324 // port that became available after deleting the last socket with default
327 // We do not test the randomness of bound ports, but that we are using
328 // passed in PRNG correctly, thus, it's the duty of PRNG to produce strong
330 static const int kBindRetries
= 10;
334 explicit TestPrng(const std::deque
<int>& numbers
) : numbers_(numbers
) {}
335 int GetNext(int /* min */, int /* max */) {
336 DCHECK(!numbers_
.empty());
337 int rv
= numbers_
.front();
338 numbers_
.pop_front();
342 std::deque
<int> numbers_
;
344 DISALLOW_COPY_AND_ASSIGN(TestPrng
);
347 TEST_F(UDPSocketTest
, ConnectRandomBind
) {
348 std::vector
<UDPClientSocket
*> sockets
;
349 IPEndPoint peer_address
;
350 CreateUDPAddress("127.0.0.1", 53, &peer_address
);
352 // Create and connect sockets and save port numbers.
353 std::deque
<int> used_ports
;
354 for (int i
= 0; i
< kBindRetries
; ++i
) {
355 UDPClientSocket
* socket
=
356 new UDPClientSocket(DatagramSocket::DEFAULT_BIND
,
360 sockets
.push_back(socket
);
361 EXPECT_EQ(OK
, socket
->Connect(peer_address
));
363 IPEndPoint client_address
;
364 EXPECT_EQ(OK
, socket
->GetLocalAddress(&client_address
));
365 used_ports
.push_back(client_address
.port());
368 // Free the last socket, its local port is still in |used_ports|.
369 delete sockets
.back();
372 TestPrng
test_prng(used_ports
);
373 RandIntCallback rand_int_cb
=
374 base::Bind(&TestPrng::GetNext
, base::Unretained(&test_prng
));
376 // Create a socket with random binding policy and connect.
377 scoped_ptr
<UDPClientSocket
> test_socket(
378 new UDPClientSocket(DatagramSocket::RANDOM_BIND
,
382 EXPECT_EQ(OK
, test_socket
->Connect(peer_address
));
384 // Make sure that the last port number in the |used_ports| was used.
385 IPEndPoint client_address
;
386 EXPECT_EQ(OK
, test_socket
->GetLocalAddress(&client_address
));
387 EXPECT_EQ(used_ports
.back(), client_address
.port());
389 STLDeleteElements(&sockets
);
392 // Return a privileged port (under 1024) so binding will fail.
393 int PrivilegedRand(int min
, int max
) {
394 // Chosen by fair dice roll. Guaranteed to be random.
398 TEST_F(UDPSocketTest
, ConnectFail
) {
399 IPEndPoint peer_address
;
400 CreateUDPAddress("0.0.0.0", 53, &peer_address
);
402 scoped_ptr
<UDPSocket
> socket(
403 new UDPSocket(DatagramSocket::RANDOM_BIND
,
404 base::Bind(&PrivilegedRand
),
407 int rv
= socket
->Open(peer_address
.GetFamily());
409 rv
= socket
->Connect(peer_address
);
410 // Connect should have failed since we couldn't bind to that port,
412 // Make sure that UDPSocket actually closed the socket.
413 EXPECT_FALSE(socket
->is_connected());
416 // In this test, we verify that connect() on a socket will have the effect
417 // of filtering reads on this socket only to data read from the destination
420 // The purpose of this test is that some documentation indicates that connect
421 // binds the client's sends to send to a particular server endpoint, but does
422 // not bind the client's reads to only be from that endpoint, and that we need
423 // to always use recvfrom() to disambiguate.
424 TEST_F(UDPSocketTest
, VerifyConnectBindsAddr
) {
425 const uint16 kPort1
= 9999;
426 const uint16 kPort2
= 10000;
427 std::string
simple_message("hello world!");
428 std::string
foreign_message("BAD MESSAGE TO GET!!");
430 // Setup the first server to listen.
431 IPEndPoint bind_address
;
432 CreateUDPAddress("127.0.0.1", kPort1
, &bind_address
);
433 UDPServerSocket
server1(NULL
, NetLog::Source());
434 server1
.AllowAddressReuse();
435 int rv
= server1
.Listen(bind_address
);
438 // Setup the second server to listen.
439 CreateUDPAddress("127.0.0.1", kPort2
, &bind_address
);
440 UDPServerSocket
server2(NULL
, NetLog::Source());
441 server2
.AllowAddressReuse();
442 rv
= server2
.Listen(bind_address
);
445 // Setup the client, connected to server 1.
446 IPEndPoint server_address
;
447 CreateUDPAddress("127.0.0.1", kPort1
, &server_address
);
448 UDPClientSocket
client(DatagramSocket::DEFAULT_BIND
,
452 rv
= client
.Connect(server_address
);
455 // Client sends to server1.
456 rv
= WriteSocket(&client
, simple_message
);
457 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(rv
));
459 // Server1 waits for message.
460 std::string str
= RecvFromSocket(&server1
);
461 DCHECK(simple_message
== str
);
463 // Get the client's address.
464 IPEndPoint client_address
;
465 rv
= client
.GetLocalAddress(&client_address
);
468 // Server2 sends reply.
469 rv
= SendToSocket(&server2
, foreign_message
,
471 EXPECT_EQ(foreign_message
.length(), static_cast<size_t>(rv
));
473 // Server1 sends reply.
474 rv
= SendToSocket(&server1
, simple_message
,
476 EXPECT_EQ(simple_message
.length(), static_cast<size_t>(rv
));
478 // Client waits for response.
479 str
= ReadSocket(&client
);
480 DCHECK(simple_message
== str
);
483 TEST_F(UDPSocketTest
, ClientGetLocalPeerAddresses
) {
485 std::string remote_address
;
486 std::string local_address
;
489 { "127.0.00.1", "127.0.0.1", false },
490 { "::1", "::1", true },
491 #if !defined(OS_ANDROID)
492 // Addresses below are disabled on Android. See crbug.com/161248
493 { "192.168.1.1", "127.0.0.1", false },
494 { "2001:db8:0::42", "::1", true },
497 for (size_t i
= 0; i
< arraysize(tests
); i
++) {
498 SCOPED_TRACE(std::string("Connecting from ") + tests
[i
].local_address
+
499 std::string(" to ") + tests
[i
].remote_address
);
501 IPAddressNumber ip_number
;
502 ParseIPLiteralToNumber(tests
[i
].remote_address
, &ip_number
);
503 IPEndPoint
remote_address(ip_number
, 80);
504 ParseIPLiteralToNumber(tests
[i
].local_address
, &ip_number
);
505 IPEndPoint
local_address(ip_number
, 80);
507 UDPClientSocket
client(DatagramSocket::DEFAULT_BIND
,
511 int rv
= client
.Connect(remote_address
);
512 if (tests
[i
].may_fail
&& rv
== ERR_ADDRESS_UNREACHABLE
) {
513 // Connect() may return ERR_ADDRESS_UNREACHABLE for IPv6
514 // addresses if IPv6 is not configured.
518 EXPECT_LE(ERR_IO_PENDING
, rv
);
520 IPEndPoint fetched_local_address
;
521 rv
= client
.GetLocalAddress(&fetched_local_address
);
524 // TODO(mbelshe): figure out how to verify the IP and port.
525 // The port is dynamically generated by the udp stack.
526 // The IP is the real IP of the client, not necessarily
528 //EXPECT_EQ(local_address.address(), fetched_local_address.address());
530 IPEndPoint fetched_remote_address
;
531 rv
= client
.GetPeerAddress(&fetched_remote_address
);
534 EXPECT_EQ(remote_address
, fetched_remote_address
);
538 TEST_F(UDPSocketTest
, ServerGetLocalAddress
) {
539 IPEndPoint bind_address
;
540 CreateUDPAddress("127.0.0.1", 0, &bind_address
);
541 UDPServerSocket
server(NULL
, NetLog::Source());
542 int rv
= server
.Listen(bind_address
);
545 IPEndPoint local_address
;
546 rv
= server
.GetLocalAddress(&local_address
);
549 // Verify that port was allocated.
550 EXPECT_GT(local_address
.port(), 0);
551 EXPECT_EQ(local_address
.address(), bind_address
.address());
554 TEST_F(UDPSocketTest
, ServerGetPeerAddress
) {
555 IPEndPoint bind_address
;
556 CreateUDPAddress("127.0.0.1", 0, &bind_address
);
557 UDPServerSocket
server(NULL
, NetLog::Source());
558 int rv
= server
.Listen(bind_address
);
561 IPEndPoint peer_address
;
562 rv
= server
.GetPeerAddress(&peer_address
);
563 EXPECT_EQ(rv
, ERR_SOCKET_NOT_CONNECTED
);
566 // Close the socket while read is pending.
567 TEST_F(UDPSocketTest
, CloseWithPendingRead
) {
568 IPEndPoint bind_address
;
569 CreateUDPAddress("127.0.0.1", 0, &bind_address
);
570 UDPServerSocket
server(NULL
, NetLog::Source());
571 int rv
= server
.Listen(bind_address
);
574 TestCompletionCallback callback
;
576 rv
= server
.RecvFrom(buffer_
.get(), kMaxRead
, &from
, callback
.callback());
577 EXPECT_EQ(rv
, ERR_IO_PENDING
);
581 EXPECT_FALSE(callback
.have_result());
584 #if defined(OS_ANDROID)
585 // Some Android devices do not support multicast socket.
586 // The ones supporting multicast need WifiManager.MulitcastLock to enable it.
587 // http://goo.gl/jjAk9
588 #define MAYBE_JoinMulticastGroup DISABLED_JoinMulticastGroup
590 #define MAYBE_JoinMulticastGroup JoinMulticastGroup
591 #endif // defined(OS_ANDROID)
593 TEST_F(UDPSocketTest
, MAYBE_JoinMulticastGroup
) {
594 const uint16 kPort
= 9999;
595 const char kGroup
[] = "237.132.100.17";
597 IPEndPoint bind_address
;
598 CreateUDPAddress("0.0.0.0", kPort
, &bind_address
);
599 IPAddressNumber group_ip
;
600 EXPECT_TRUE(ParseIPLiteralToNumber(kGroup
, &group_ip
));
602 UDPSocket
socket(DatagramSocket::DEFAULT_BIND
,
606 EXPECT_EQ(OK
, socket
.Open(bind_address
.GetFamily()));
607 EXPECT_EQ(OK
, socket
.Bind(bind_address
));
608 EXPECT_EQ(OK
, socket
.JoinGroup(group_ip
));
609 // Joining group multiple times.
610 EXPECT_NE(OK
, socket
.JoinGroup(group_ip
));
611 EXPECT_EQ(OK
, socket
.LeaveGroup(group_ip
));
612 // Leaving group multiple times.
613 EXPECT_NE(OK
, socket
.LeaveGroup(group_ip
));
618 TEST_F(UDPSocketTest
, MulticastOptions
) {
619 const uint16 kPort
= 9999;
620 IPEndPoint bind_address
;
621 CreateUDPAddress("0.0.0.0", kPort
, &bind_address
);
623 UDPSocket
socket(DatagramSocket::DEFAULT_BIND
,
628 EXPECT_EQ(OK
, socket
.SetMulticastLoopbackMode(false));
629 EXPECT_EQ(OK
, socket
.SetMulticastLoopbackMode(true));
630 EXPECT_EQ(OK
, socket
.SetMulticastTimeToLive(0));
631 EXPECT_EQ(OK
, socket
.SetMulticastTimeToLive(3));
632 EXPECT_NE(OK
, socket
.SetMulticastTimeToLive(-1));
633 EXPECT_EQ(OK
, socket
.SetMulticastInterface(0));
635 EXPECT_EQ(OK
, socket
.Open(bind_address
.GetFamily()));
636 EXPECT_EQ(OK
, socket
.Bind(bind_address
));
638 EXPECT_NE(OK
, socket
.SetMulticastLoopbackMode(false));
639 EXPECT_NE(OK
, socket
.SetMulticastTimeToLive(0));
640 EXPECT_NE(OK
, socket
.SetMulticastInterface(0));
645 // Checking that DSCP bits are set correctly is difficult,
646 // but let's check that the code doesn't crash at least.
647 TEST_F(UDPSocketTest
, SetDSCP
) {
648 // Setup the server to listen.
649 IPEndPoint bind_address
;
650 UDPSocket
client(DatagramSocket::DEFAULT_BIND
,
654 // We need a real IP, but we won't actually send anything to it.
655 CreateUDPAddress("8.8.8.8", 9999, &bind_address
);
656 int rv
= client
.Open(bind_address
.GetFamily());
659 rv
= client
.Connect(bind_address
);
661 // Let's try localhost then..
662 CreateUDPAddress("127.0.0.1", 9999, &bind_address
);
663 rv
= client
.Connect(bind_address
);
667 client
.SetDiffServCodePoint(DSCP_NO_CHANGE
);
668 client
.SetDiffServCodePoint(DSCP_AF41
);
669 client
.SetDiffServCodePoint(DSCP_DEFAULT
);
670 client
.SetDiffServCodePoint(DSCP_CS2
);
671 client
.SetDiffServCodePoint(DSCP_NO_CHANGE
);
672 client
.SetDiffServCodePoint(DSCP_DEFAULT
);
682 const HANDLE kFakeHandle
= (HANDLE
)19;
683 const QOS_FLOWID kFakeFlowId
= (QOS_FLOWID
)27;
685 BOOL WINAPI
FakeQOSCreateHandleFAIL(PQOS_VERSION version
, PHANDLE handle
) {
686 EXPECT_EQ(0, version
->MinorVersion
);
687 EXPECT_EQ(1, version
->MajorVersion
);
688 SetLastError(ERROR_OPEN_FAILED
);
692 BOOL WINAPI
FakeQOSCreateHandle(PQOS_VERSION version
, PHANDLE handle
) {
693 EXPECT_EQ(0, version
->MinorVersion
);
694 EXPECT_EQ(1, version
->MajorVersion
);
695 *handle
= kFakeHandle
;
699 BOOL WINAPI
FakeQOSCloseHandle(HANDLE handle
) {
700 EXPECT_EQ(kFakeHandle
, handle
);
704 QOS_TRAFFIC_TYPE g_expected_traffic_type
;
706 BOOL WINAPI
FakeQOSAddSocketToFlow(HANDLE handle
,
709 QOS_TRAFFIC_TYPE traffic_type
,
711 PQOS_FLOWID flow_id
) {
712 EXPECT_EQ(kFakeHandle
, handle
);
713 EXPECT_EQ(NULL
, addr
);
714 EXPECT_EQ(QOS_NON_ADAPTIVE_FLOW
, flags
);
715 EXPECT_EQ(0, *flow_id
);
716 *flow_id
= kFakeFlowId
;
720 BOOL WINAPI
FakeQOSRemoveSocketFromFlow(HANDLE handle
,
724 EXPECT_EQ(kFakeHandle
, handle
);
725 EXPECT_EQ(NULL
, socket
);
726 EXPECT_EQ(kFakeFlowId
, flowid
);
727 EXPECT_EQ(0, reserved
);
731 DWORD g_expected_dscp
;
733 BOOL WINAPI
FakeQOSSetFlow(HANDLE handle
,
739 LPOVERLAPPED overlapped
) {
740 EXPECT_EQ(kFakeHandle
, handle
);
741 EXPECT_EQ(QOSSetOutgoingDSCPValue
, op
);
742 EXPECT_EQ(sizeof(DWORD
), size
);
743 EXPECT_EQ(g_expected_dscp
, *reinterpret_cast<DWORD
*>(data
));
744 EXPECT_EQ(kFakeFlowId
, flow_id
);
745 EXPECT_EQ(0, reserved
);
746 EXPECT_EQ(NULL
, overlapped
);
752 // Mock out the Qwave functions and make sure they are
753 // called correctly. Must be in net namespace for friendship
755 TEST_F(UDPSocketTest
, SetDSCPFake
) {
756 // Setup the server to listen.
757 IPEndPoint bind_address
;
758 // We need a real IP, but we won't actually send anything to it.
759 CreateUDPAddress("8.8.8.8", 9999, &bind_address
);
760 UDPSocket
client(DatagramSocket::DEFAULT_BIND
,
764 int rv
= client
.SetDiffServCodePoint(DSCP_AF41
);
765 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED
, rv
);
767 rv
= client
.Open(bind_address
.GetFamily());
770 rv
= client
.Connect(bind_address
);
773 QwaveAPI
& qos(QwaveAPI::Get());
774 qos
.create_handle_func_
= FakeQOSCreateHandleFAIL
;
775 qos
.close_handle_func_
= FakeQOSCloseHandle
;
776 qos
.add_socket_to_flow_func_
= FakeQOSAddSocketToFlow
;
777 qos
.remove_socket_from_flow_func_
= FakeQOSRemoveSocketFromFlow
;
778 qos
.set_flow_func_
= FakeQOSSetFlow
;
779 qos
.qwave_supported_
= true;
781 EXPECT_EQ(OK
, client
.SetDiffServCodePoint(DSCP_NO_CHANGE
));
782 EXPECT_EQ(ERROR_NOT_SUPPORTED
, client
.SetDiffServCodePoint(DSCP_AF41
));
783 qos
.create_handle_func_
= FakeQOSCreateHandle
;
784 g_expected_dscp
= DSCP_AF41
;
785 g_expected_traffic_type
= QOSTrafficTypeAudioVideo
;
786 EXPECT_EQ(OK
, client
.SetDiffServCodePoint(DSCP_AF41
));
787 g_expected_dscp
= DSCP_DEFAULT
;
788 g_expected_traffic_type
= QOSTrafficTypeBestEffort
;
789 EXPECT_EQ(OK
, client
.SetDiffServCodePoint(DSCP_DEFAULT
));
790 g_expected_dscp
= DSCP_CS2
;
791 g_expected_traffic_type
= QOSTrafficTypeExcellentEffort
;
792 EXPECT_EQ(OK
, client
.SetDiffServCodePoint(DSCP_CS2
));
793 g_expected_dscp
= DSCP_CS3
;
794 g_expected_traffic_type
= QOSTrafficTypeExcellentEffort
;
795 EXPECT_EQ(OK
, client
.SetDiffServCodePoint(DSCP_NO_CHANGE
));
796 g_expected_dscp
= DSCP_DEFAULT
;
797 g_expected_traffic_type
= QOSTrafficTypeBestEffort
;
798 EXPECT_EQ(OK
, client
.SetDiffServCodePoint(DSCP_DEFAULT
));