Cleanup setting of 'sysroot' in common.gypi
[chromium-blink-merge.git] / net / udp / udp_socket_unittest.cc
blob57a8f5b79ad6e772f326645f463bb25b7e717c06
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"
30 namespace net {
32 namespace {
34 class UDPSocketTest : public PlatformTest {
35 public:
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();
46 if (rv < 0)
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
52 // error occurs.
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
55 // received from.
56 int SendToSocket(UDPServerSocket* socket, std::string msg) {
57 return SendToSocket(socket, msg, recv_from_address_);
60 int SendToSocket(UDPServerSocket* socket,
61 std::string msg,
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));
70 int bytes_sent = 0;
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();
76 if (rv <= 0)
77 return bytes_sent > 0 ? bytes_sent : rv;
78 bytes_sent += rv;
79 buffer->DidConsume(rv);
81 return bytes_sent;
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();
90 if (rv < 0)
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
96 // error occurs.
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));
105 int bytes_sent = 0;
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();
111 if (rv <= 0)
112 return bytes_sent > 0 ? bytes_sent : rv;
113 bytes_sent += rv;
114 buffer->DidConsume(rv);
116 return bytes_sent;
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);
127 if (!rv)
128 return;
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);
137 protected:
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;
145 callback.Run();
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()));
158 #if defined(OS_WIN)
159 if (use_nonblocking_io)
160 server->UseNonBlockingIO();
161 #endif
162 server->AllowAddressReuse();
163 int rv = server->Listen(bind_address);
164 ASSERT_EQ(OK, rv);
166 // Setup the client.
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()));
173 #if defined(OS_WIN)
174 if (use_nonblocking_io)
175 client->UseNonBlockingIO();
176 #endif
178 rv = client->Connect(server_address);
179 EXPECT_EQ(OK, rv);
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;
199 int read_result = 0;
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(
207 FROM_HERE,
208 base::Bind(&UDPSocketTest::WriteSocketIgnoreResult,
209 base::Unretained(this), client.get(), simple_message));
210 run_loop.Run();
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.
215 server.reset();
216 client.reset();
218 // Check the server's log.
219 TestNetLogEntry::List server_entries;
220 server_log.GetEntries(&server_entries);
221 EXPECT_EQ(5u, server_entries.size());
222 EXPECT_TRUE(
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));
230 EXPECT_TRUE(
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());
237 EXPECT_TRUE(
238 LogContainsBeginEvent(client_entries, 0, NetLog::TYPE_SOCKET_ALIVE));
239 EXPECT_TRUE(
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));
248 EXPECT_TRUE(
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.
254 ConnectTest(false);
257 #if defined(OS_WIN)
258 TEST_F(UDPSocketTest, ConnectNonBlocking) {
259 ConnectTest(true);
261 #endif
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) {
272 #else
273 TEST_F(UDPSocketTest, Broadcast) {
274 #endif
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);
294 EXPECT_EQ(OK, rv);
295 rv = server2->Listen(listen_address);
296 EXPECT_EQ(OK, rv);
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
325 // binding policy.
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
329 // random numbers.
330 static const int kBindRetries = 10;
332 class TestPrng {
333 public:
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();
339 return rv;
341 private:
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,
357 RandIntCallback(),
358 NULL,
359 NetLog::Source());
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();
370 sockets.pop_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,
379 rand_int_cb,
380 NULL,
381 NetLog::Source()));
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.
395 return 4;
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),
405 NULL,
406 NetLog::Source()));
407 int rv = socket->Open(peer_address.GetFamily());
408 EXPECT_EQ(OK, rv);
409 rv = socket->Connect(peer_address);
410 // Connect should have failed since we couldn't bind to that port,
411 EXPECT_NE(OK, rv);
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
418 // we connected to.
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);
436 ASSERT_EQ(OK, rv);
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);
443 ASSERT_EQ(OK, rv);
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,
449 RandIntCallback(),
450 NULL,
451 NetLog::Source());
452 rv = client.Connect(server_address);
453 EXPECT_EQ(OK, rv);
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);
466 EXPECT_EQ(OK, rv);
468 // Server2 sends reply.
469 rv = SendToSocket(&server2, foreign_message,
470 client_address);
471 EXPECT_EQ(foreign_message.length(), static_cast<size_t>(rv));
473 // Server1 sends reply.
474 rv = SendToSocket(&server1, simple_message,
475 client_address);
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) {
484 struct TestData {
485 std::string remote_address;
486 std::string local_address;
487 bool may_fail;
488 } tests[] = {
489 { "127.0.00.1", "127.0.0.1", false },
490 { "::1", "::1", true },
491 #if !defined(OS_ANDROID) && !defined(OS_IOS)
492 // Addresses below are disabled on Android. See crbug.com/161248
493 // They are also disabled on iOS. See https://crbug.com/523225
494 { "192.168.1.1", "127.0.0.1", false },
495 { "2001:db8:0::42", "::1", true },
496 #endif
498 for (size_t i = 0; i < arraysize(tests); i++) {
499 SCOPED_TRACE(std::string("Connecting from ") + tests[i].local_address +
500 std::string(" to ") + tests[i].remote_address);
502 IPAddressNumber ip_number;
503 ParseIPLiteralToNumber(tests[i].remote_address, &ip_number);
504 IPEndPoint remote_address(ip_number, 80);
505 ParseIPLiteralToNumber(tests[i].local_address, &ip_number);
506 IPEndPoint local_address(ip_number, 80);
508 UDPClientSocket client(DatagramSocket::DEFAULT_BIND,
509 RandIntCallback(),
510 NULL,
511 NetLog::Source());
512 int rv = client.Connect(remote_address);
513 if (tests[i].may_fail && rv == ERR_ADDRESS_UNREACHABLE) {
514 // Connect() may return ERR_ADDRESS_UNREACHABLE for IPv6
515 // addresses if IPv6 is not configured.
516 continue;
519 EXPECT_LE(ERR_IO_PENDING, rv);
521 IPEndPoint fetched_local_address;
522 rv = client.GetLocalAddress(&fetched_local_address);
523 EXPECT_EQ(OK, rv);
525 // TODO(mbelshe): figure out how to verify the IP and port.
526 // The port is dynamically generated by the udp stack.
527 // The IP is the real IP of the client, not necessarily
528 // loopback.
529 //EXPECT_EQ(local_address.address(), fetched_local_address.address());
531 IPEndPoint fetched_remote_address;
532 rv = client.GetPeerAddress(&fetched_remote_address);
533 EXPECT_EQ(OK, rv);
535 EXPECT_EQ(remote_address, fetched_remote_address);
539 TEST_F(UDPSocketTest, ServerGetLocalAddress) {
540 IPEndPoint bind_address;
541 CreateUDPAddress("127.0.0.1", 0, &bind_address);
542 UDPServerSocket server(NULL, NetLog::Source());
543 int rv = server.Listen(bind_address);
544 EXPECT_EQ(OK, rv);
546 IPEndPoint local_address;
547 rv = server.GetLocalAddress(&local_address);
548 EXPECT_EQ(rv, 0);
550 // Verify that port was allocated.
551 EXPECT_GT(local_address.port(), 0);
552 EXPECT_EQ(local_address.address(), bind_address.address());
555 TEST_F(UDPSocketTest, ServerGetPeerAddress) {
556 IPEndPoint bind_address;
557 CreateUDPAddress("127.0.0.1", 0, &bind_address);
558 UDPServerSocket server(NULL, NetLog::Source());
559 int rv = server.Listen(bind_address);
560 EXPECT_EQ(OK, rv);
562 IPEndPoint peer_address;
563 rv = server.GetPeerAddress(&peer_address);
564 EXPECT_EQ(rv, ERR_SOCKET_NOT_CONNECTED);
567 // Close the socket while read is pending.
568 TEST_F(UDPSocketTest, CloseWithPendingRead) {
569 IPEndPoint bind_address;
570 CreateUDPAddress("127.0.0.1", 0, &bind_address);
571 UDPServerSocket server(NULL, NetLog::Source());
572 int rv = server.Listen(bind_address);
573 EXPECT_EQ(OK, rv);
575 TestCompletionCallback callback;
576 IPEndPoint from;
577 rv = server.RecvFrom(buffer_.get(), kMaxRead, &from, callback.callback());
578 EXPECT_EQ(rv, ERR_IO_PENDING);
580 server.Close();
582 EXPECT_FALSE(callback.have_result());
585 #if defined(OS_ANDROID)
586 // Some Android devices do not support multicast socket.
587 // The ones supporting multicast need WifiManager.MulitcastLock to enable it.
588 // http://goo.gl/jjAk9
589 #define MAYBE_JoinMulticastGroup DISABLED_JoinMulticastGroup
590 #else
591 #define MAYBE_JoinMulticastGroup JoinMulticastGroup
592 #endif // defined(OS_ANDROID)
594 TEST_F(UDPSocketTest, MAYBE_JoinMulticastGroup) {
595 const uint16 kPort = 9999;
596 const char kGroup[] = "237.132.100.17";
598 IPEndPoint bind_address;
599 CreateUDPAddress("0.0.0.0", kPort, &bind_address);
600 IPAddressNumber group_ip;
601 EXPECT_TRUE(ParseIPLiteralToNumber(kGroup, &group_ip));
603 UDPSocket socket(DatagramSocket::DEFAULT_BIND,
604 RandIntCallback(),
605 NULL,
606 NetLog::Source());
607 EXPECT_EQ(OK, socket.Open(bind_address.GetFamily()));
608 EXPECT_EQ(OK, socket.Bind(bind_address));
609 EXPECT_EQ(OK, socket.JoinGroup(group_ip));
610 // Joining group multiple times.
611 EXPECT_NE(OK, socket.JoinGroup(group_ip));
612 EXPECT_EQ(OK, socket.LeaveGroup(group_ip));
613 // Leaving group multiple times.
614 EXPECT_NE(OK, socket.LeaveGroup(group_ip));
616 socket.Close();
619 TEST_F(UDPSocketTest, MulticastOptions) {
620 const uint16 kPort = 9999;
621 IPEndPoint bind_address;
622 CreateUDPAddress("0.0.0.0", kPort, &bind_address);
624 UDPSocket socket(DatagramSocket::DEFAULT_BIND,
625 RandIntCallback(),
626 NULL,
627 NetLog::Source());
628 // Before binding.
629 EXPECT_EQ(OK, socket.SetMulticastLoopbackMode(false));
630 EXPECT_EQ(OK, socket.SetMulticastLoopbackMode(true));
631 EXPECT_EQ(OK, socket.SetMulticastTimeToLive(0));
632 EXPECT_EQ(OK, socket.SetMulticastTimeToLive(3));
633 EXPECT_NE(OK, socket.SetMulticastTimeToLive(-1));
634 EXPECT_EQ(OK, socket.SetMulticastInterface(0));
636 EXPECT_EQ(OK, socket.Open(bind_address.GetFamily()));
637 EXPECT_EQ(OK, socket.Bind(bind_address));
639 EXPECT_NE(OK, socket.SetMulticastLoopbackMode(false));
640 EXPECT_NE(OK, socket.SetMulticastTimeToLive(0));
641 EXPECT_NE(OK, socket.SetMulticastInterface(0));
643 socket.Close();
646 // Checking that DSCP bits are set correctly is difficult,
647 // but let's check that the code doesn't crash at least.
648 TEST_F(UDPSocketTest, SetDSCP) {
649 // Setup the server to listen.
650 IPEndPoint bind_address;
651 UDPSocket client(DatagramSocket::DEFAULT_BIND,
652 RandIntCallback(),
653 NULL,
654 NetLog::Source());
655 // We need a real IP, but we won't actually send anything to it.
656 CreateUDPAddress("8.8.8.8", 9999, &bind_address);
657 int rv = client.Open(bind_address.GetFamily());
658 EXPECT_EQ(OK, rv);
660 rv = client.Connect(bind_address);
661 if (rv != OK) {
662 // Let's try localhost then..
663 CreateUDPAddress("127.0.0.1", 9999, &bind_address);
664 rv = client.Connect(bind_address);
666 EXPECT_EQ(OK, rv);
668 client.SetDiffServCodePoint(DSCP_NO_CHANGE);
669 client.SetDiffServCodePoint(DSCP_AF41);
670 client.SetDiffServCodePoint(DSCP_DEFAULT);
671 client.SetDiffServCodePoint(DSCP_CS2);
672 client.SetDiffServCodePoint(DSCP_NO_CHANGE);
673 client.SetDiffServCodePoint(DSCP_DEFAULT);
674 client.Close();
677 } // namespace
679 #if defined(OS_WIN)
681 namespace {
683 const HANDLE kFakeHandle = (HANDLE)19;
684 const QOS_FLOWID kFakeFlowId = (QOS_FLOWID)27;
686 BOOL WINAPI FakeQOSCreateHandleFAIL(PQOS_VERSION version, PHANDLE handle) {
687 EXPECT_EQ(0, version->MinorVersion);
688 EXPECT_EQ(1, version->MajorVersion);
689 SetLastError(ERROR_OPEN_FAILED);
690 return false;
693 BOOL WINAPI FakeQOSCreateHandle(PQOS_VERSION version, PHANDLE handle) {
694 EXPECT_EQ(0, version->MinorVersion);
695 EXPECT_EQ(1, version->MajorVersion);
696 *handle = kFakeHandle;
697 return true;
700 BOOL WINAPI FakeQOSCloseHandle(HANDLE handle) {
701 EXPECT_EQ(kFakeHandle, handle);
702 return true;
705 QOS_TRAFFIC_TYPE g_expected_traffic_type;
707 BOOL WINAPI FakeQOSAddSocketToFlow(HANDLE handle,
708 SOCKET socket,
709 PSOCKADDR addr,
710 QOS_TRAFFIC_TYPE traffic_type,
711 DWORD flags,
712 PQOS_FLOWID flow_id) {
713 EXPECT_EQ(kFakeHandle, handle);
714 EXPECT_EQ(NULL, addr);
715 EXPECT_EQ(QOS_NON_ADAPTIVE_FLOW, flags);
716 EXPECT_EQ(0, *flow_id);
717 *flow_id = kFakeFlowId;
718 return true;
721 BOOL WINAPI FakeQOSRemoveSocketFromFlow(HANDLE handle,
722 SOCKET socket,
723 QOS_FLOWID flowid,
724 DWORD reserved) {
725 EXPECT_EQ(kFakeHandle, handle);
726 EXPECT_EQ(NULL, socket);
727 EXPECT_EQ(kFakeFlowId, flowid);
728 EXPECT_EQ(0, reserved);
729 return true;
732 DWORD g_expected_dscp;
734 BOOL WINAPI FakeQOSSetFlow(HANDLE handle,
735 QOS_FLOWID flow_id,
736 QOS_SET_FLOW op,
737 ULONG size,
738 PVOID data,
739 DWORD reserved,
740 LPOVERLAPPED overlapped) {
741 EXPECT_EQ(kFakeHandle, handle);
742 EXPECT_EQ(QOSSetOutgoingDSCPValue, op);
743 EXPECT_EQ(sizeof(DWORD), size);
744 EXPECT_EQ(g_expected_dscp, *reinterpret_cast<DWORD*>(data));
745 EXPECT_EQ(kFakeFlowId, flow_id);
746 EXPECT_EQ(0, reserved);
747 EXPECT_EQ(NULL, overlapped);
748 return true;
751 } // namespace
753 // Mock out the Qwave functions and make sure they are
754 // called correctly. Must be in net namespace for friendship
755 // reasons.
756 TEST_F(UDPSocketTest, SetDSCPFake) {
757 // Setup the server to listen.
758 IPEndPoint bind_address;
759 // We need a real IP, but we won't actually send anything to it.
760 CreateUDPAddress("8.8.8.8", 9999, &bind_address);
761 UDPSocket client(DatagramSocket::DEFAULT_BIND,
762 RandIntCallback(),
763 NULL,
764 NetLog::Source());
765 int rv = client.SetDiffServCodePoint(DSCP_AF41);
766 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, rv);
768 rv = client.Open(bind_address.GetFamily());
769 EXPECT_EQ(OK, rv);
771 rv = client.Connect(bind_address);
772 EXPECT_EQ(OK, rv);
774 QwaveAPI& qos(QwaveAPI::Get());
775 qos.create_handle_func_ = FakeQOSCreateHandleFAIL;
776 qos.close_handle_func_ = FakeQOSCloseHandle;
777 qos.add_socket_to_flow_func_ = FakeQOSAddSocketToFlow;
778 qos.remove_socket_from_flow_func_ = FakeQOSRemoveSocketFromFlow;
779 qos.set_flow_func_ = FakeQOSSetFlow;
780 qos.qwave_supported_ = true;
782 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE));
783 EXPECT_EQ(ERROR_NOT_SUPPORTED, client.SetDiffServCodePoint(DSCP_AF41));
784 qos.create_handle_func_ = FakeQOSCreateHandle;
785 g_expected_dscp = DSCP_AF41;
786 g_expected_traffic_type = QOSTrafficTypeAudioVideo;
787 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_AF41));
788 g_expected_dscp = DSCP_DEFAULT;
789 g_expected_traffic_type = QOSTrafficTypeBestEffort;
790 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT));
791 g_expected_dscp = DSCP_CS2;
792 g_expected_traffic_type = QOSTrafficTypeExcellentEffort;
793 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_CS2));
794 g_expected_dscp = DSCP_CS3;
795 g_expected_traffic_type = QOSTrafficTypeExcellentEffort;
796 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE));
797 g_expected_dscp = DSCP_DEFAULT;
798 g_expected_traffic_type = QOSTrafficTypeBestEffort;
799 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT));
800 client.Close();
802 #endif
804 } // namespace net