[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 3d2fd54 | 2014-08-12 03:07:08 | [diff] [blame] | 5 | #include "net/udp/udp_socket.h" |
| 6 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 7 | #include "net/udp/udp_client_socket.h" |
| 8 | #include "net/udp/udp_server_socket.h" |
| 9 | |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 10 | #include "base/bind.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 11 | #include "base/location.h" |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 12 | #include "base/macros.h" |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 14 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 15 | #include "base/single_thread_task_runner.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 16 | #include "base/stl_util.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 17 | #include "base/thread_task_runner_handle.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 18 | #include "net/base/io_buffer.h" |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 19 | #include "net/base/ip_endpoint.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 20 | #include "net/base/net_errors.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 21 | #include "net/base/test_completion_callback.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 22 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 23 | #include "net/log/test_net_log_entry.h" |
| 24 | #include "net/log/test_net_log_util.h" |
[email protected] | 8efa4ba4 | 2013-03-18 21:14:14 | [diff] [blame] | 25 | #include "net/test/net_test_suite.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 26 | #include "testing/gtest/include/gtest/gtest.h" |
| 27 | #include "testing/platform_test.h" |
| 28 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 29 | #if defined(OS_IOS) |
| 30 | #include <TargetConditionals.h> |
| 31 | #endif |
| 32 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 33 | namespace net { |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | class UDPSocketTest : public PlatformTest { |
| 38 | public: |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 39 | UDPSocketTest() : buffer_(new IOBufferWithSize(kMaxRead)) {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 40 | |
| 41 | // Blocks until data is read from the socket. |
| 42 | std::string RecvFromSocket(UDPServerSocket* socket) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 43 | TestCompletionCallback callback; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 44 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 45 | int rv = socket->RecvFrom( |
| 46 | buffer_.get(), kMaxRead, &recv_from_address_, callback.callback()); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 47 | if (rv == ERR_IO_PENDING) |
| 48 | rv = callback.WaitForResult(); |
| 49 | if (rv < 0) |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 50 | return std::string(); // error! |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 51 | return std::string(buffer_->data(), rv); |
| 52 | } |
| 53 | |
| 54 | // Loop until |msg| has been written to the socket or until an |
| 55 | // error occurs. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 56 | // If |address| is specified, then it is used for the destination |
| 57 | // to send to. Otherwise, will send to the last socket this server |
| 58 | // received from. |
ki.stfu | 144dce1 | 2015-09-22 01:07:57 | [diff] [blame] | 59 | int SendToSocket(UDPServerSocket* socket, const std::string& msg) { |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 60 | return SendToSocket(socket, msg, recv_from_address_); |
| 61 | } |
| 62 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 63 | int SendToSocket(UDPServerSocket* socket, |
| 64 | std::string msg, |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 65 | const IPEndPoint& address) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 66 | TestCompletionCallback callback; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 67 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 68 | int length = msg.length(); |
| 69 | scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); |
| 70 | scoped_refptr<DrainableIOBuffer> buffer( |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 71 | new DrainableIOBuffer(io_buffer.get(), length)); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 72 | |
| 73 | int bytes_sent = 0; |
| 74 | while (buffer->BytesRemaining()) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 75 | int rv = socket->SendTo( |
| 76 | buffer.get(), buffer->BytesRemaining(), address, callback.callback()); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 77 | if (rv == ERR_IO_PENDING) |
| 78 | rv = callback.WaitForResult(); |
| 79 | if (rv <= 0) |
| 80 | return bytes_sent > 0 ? bytes_sent : rv; |
| 81 | bytes_sent += rv; |
| 82 | buffer->DidConsume(rv); |
| 83 | } |
| 84 | return bytes_sent; |
| 85 | } |
| 86 | |
| 87 | std::string ReadSocket(UDPClientSocket* socket) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 88 | TestCompletionCallback callback; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 89 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 90 | int rv = socket->Read(buffer_.get(), kMaxRead, callback.callback()); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 91 | if (rv == ERR_IO_PENDING) |
| 92 | rv = callback.WaitForResult(); |
| 93 | if (rv < 0) |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 94 | return std::string(); // error! |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 95 | return std::string(buffer_->data(), rv); |
| 96 | } |
| 97 | |
| 98 | // Loop until |msg| has been written to the socket or until an |
| 99 | // error occurs. |
ki.stfu | 144dce1 | 2015-09-22 01:07:57 | [diff] [blame] | 100 | int WriteSocket(UDPClientSocket* socket, const std::string& msg) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 101 | TestCompletionCallback callback; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 102 | |
| 103 | int length = msg.length(); |
| 104 | scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); |
| 105 | scoped_refptr<DrainableIOBuffer> buffer( |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 106 | new DrainableIOBuffer(io_buffer.get(), length)); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 107 | |
| 108 | int bytes_sent = 0; |
| 109 | while (buffer->BytesRemaining()) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 110 | int rv = socket->Write( |
| 111 | buffer.get(), buffer->BytesRemaining(), callback.callback()); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 112 | if (rv == ERR_IO_PENDING) |
| 113 | rv = callback.WaitForResult(); |
| 114 | if (rv <= 0) |
| 115 | return bytes_sent > 0 ? bytes_sent : rv; |
| 116 | bytes_sent += rv; |
| 117 | buffer->DidConsume(rv); |
| 118 | } |
| 119 | return bytes_sent; |
| 120 | } |
| 121 | |
ki.stfu | 144dce1 | 2015-09-22 01:07:57 | [diff] [blame] | 122 | void WriteSocketIgnoreResult(UDPClientSocket* socket, |
| 123 | const std::string& msg) { |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 124 | WriteSocket(socket, msg); |
| 125 | } |
| 126 | |
| 127 | // Creates an address from ip address and port and writes it to |*address|. |
ki.stfu | 144dce1 | 2015-09-22 01:07:57 | [diff] [blame] | 128 | void CreateUDPAddress(const std::string& ip_str, |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 129 | uint16_t port, |
ki.stfu | 144dce1 | 2015-09-22 01:07:57 | [diff] [blame] | 130 | IPEndPoint* address) { |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 131 | IPAddressNumber ip_number; |
| 132 | bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); |
| 133 | if (!rv) |
| 134 | return; |
| 135 | *address = IPEndPoint(ip_number, port); |
| 136 | } |
| 137 | |
| 138 | // Run unit test for a connection test. |
| 139 | // |use_nonblocking_io| is used to switch between overlapped and non-blocking |
| 140 | // IO on Windows. It has no effect in other ports. |
| 141 | void ConnectTest(bool use_nonblocking_io); |
| 142 | |
[email protected] | 7b6afd0 | 2011-03-12 00:03:12 | [diff] [blame] | 143 | protected: |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 144 | static const int kMaxRead = 1024; |
| 145 | scoped_refptr<IOBufferWithSize> buffer_; |
[email protected] | 9d1e2ba0 | 2011-03-09 19:42:33 | [diff] [blame] | 146 | IPEndPoint recv_from_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 147 | }; |
| 148 | |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 149 | void ReadCompleteCallback(int* result_out, base::Closure callback, int result) { |
| 150 | *result_out = result; |
| 151 | callback.Run(); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 152 | } |
| 153 | |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 154 | void UDPSocketTest::ConnectTest(bool use_nonblocking_io) { |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 155 | const uint16_t kPort = 9999; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 156 | std::string simple_message("hello world!"); |
| 157 | |
| 158 | // Setup the server to listen. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 159 | IPEndPoint bind_address; |
[email protected] | 2cb8310 | 2012-09-18 19:13:01 | [diff] [blame] | 160 | CreateUDPAddress("127.0.0.1", kPort, &bind_address); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 161 | TestNetLog server_log; |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 162 | scoped_ptr<UDPServerSocket> server( |
| 163 | new UDPServerSocket(&server_log, NetLog::Source())); |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 164 | #if defined(OS_WIN) |
| 165 | if (use_nonblocking_io) |
| 166 | server->UseNonBlockingIO(); |
| 167 | #endif |
[email protected] | 2cb8310 | 2012-09-18 19:13:01 | [diff] [blame] | 168 | server->AllowAddressReuse(); |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 169 | int rv = server->Listen(bind_address); |
[email protected] | d72a14f | 2012-09-11 16:27:11 | [diff] [blame] | 170 | ASSERT_EQ(OK, rv); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 171 | |
| 172 | // Setup the client. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 173 | IPEndPoint server_address; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 174 | CreateUDPAddress("127.0.0.1", kPort, &server_address); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 175 | TestNetLog client_log; |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 176 | scoped_ptr<UDPClientSocket> client( |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 177 | new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
| 178 | &client_log, NetLog::Source())); |
| 179 | #if defined(OS_WIN) |
| 180 | if (use_nonblocking_io) |
| 181 | client->UseNonBlockingIO(); |
| 182 | #endif |
| 183 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 184 | rv = client->Connect(server_address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 185 | EXPECT_EQ(OK, rv); |
| 186 | |
| 187 | // Client sends to the server. |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 188 | rv = WriteSocket(client.get(), simple_message); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 189 | EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
| 190 | |
| 191 | // Server waits for message. |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 192 | std::string str = RecvFromSocket(server.get()); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 193 | DCHECK(simple_message == str); |
| 194 | |
| 195 | // Server echoes reply. |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 196 | rv = SendToSocket(server.get(), simple_message); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 197 | EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
| 198 | |
| 199 | // Client waits for response. |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 200 | str = ReadSocket(client.get()); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 201 | DCHECK(simple_message == str); |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 202 | |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 203 | // Test asynchronous read. Server waits for message. |
| 204 | base::RunLoop run_loop; |
| 205 | int read_result = 0; |
| 206 | rv = server->RecvFrom( |
| 207 | buffer_.get(), kMaxRead, &recv_from_address_, |
| 208 | base::Bind(&ReadCompleteCallback, &read_result, run_loop.QuitClosure())); |
| 209 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 210 | |
| 211 | // Client sends to the server. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 212 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 213 | FROM_HERE, |
| 214 | base::Bind(&UDPSocketTest::WriteSocketIgnoreResult, |
| 215 | base::Unretained(this), client.get(), simple_message)); |
| 216 | run_loop.Run(); |
| 217 | EXPECT_EQ(simple_message.length(), static_cast<size_t>(read_result)); |
| 218 | EXPECT_EQ(simple_message, std::string(buffer_->data(), read_result)); |
| 219 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 220 | // Delete sockets so they log their final events. |
| 221 | server.reset(); |
| 222 | client.reset(); |
| 223 | |
| 224 | // Check the server's log. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 225 | TestNetLogEntry::List server_entries; |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 226 | server_log.GetEntries(&server_entries); |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 227 | EXPECT_EQ(5u, server_entries.size()); |
| 228 | EXPECT_TRUE( |
| 229 | LogContainsBeginEvent(server_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 230 | EXPECT_TRUE(LogContainsEvent( |
| 231 | server_entries, 1, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 232 | EXPECT_TRUE(LogContainsEvent(server_entries, 2, NetLog::TYPE_UDP_BYTES_SENT, |
| 233 | NetLog::PHASE_NONE)); |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 234 | EXPECT_TRUE(LogContainsEvent( |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 235 | server_entries, 3, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); |
| 236 | EXPECT_TRUE( |
| 237 | LogContainsEndEvent(server_entries, 4, NetLog::TYPE_SOCKET_ALIVE)); |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 238 | |
| 239 | // Check the client's log. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 240 | TestNetLogEntry::List client_entries; |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 241 | client_log.GetEntries(&client_entries); |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 242 | EXPECT_EQ(7u, client_entries.size()); |
| 243 | EXPECT_TRUE( |
| 244 | LogContainsBeginEvent(client_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); |
| 245 | EXPECT_TRUE( |
| 246 | LogContainsBeginEvent(client_entries, 1, NetLog::TYPE_UDP_CONNECT)); |
| 247 | EXPECT_TRUE(LogContainsEndEvent(client_entries, 2, NetLog::TYPE_UDP_CONNECT)); |
| 248 | EXPECT_TRUE(LogContainsEvent(client_entries, 3, NetLog::TYPE_UDP_BYTES_SENT, |
| 249 | NetLog::PHASE_NONE)); |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 250 | EXPECT_TRUE(LogContainsEvent( |
| 251 | client_entries, 4, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 252 | EXPECT_TRUE(LogContainsEvent(client_entries, 5, NetLog::TYPE_UDP_BYTES_SENT, |
| 253 | NetLog::PHASE_NONE)); |
| 254 | EXPECT_TRUE( |
| 255 | LogContainsEndEvent(client_entries, 6, NetLog::TYPE_SOCKET_ALIVE)); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 256 | } |
| 257 | |
hclam | 5a5ee68 | 2015-02-05 04:18:22 | [diff] [blame] | 258 | TEST_F(UDPSocketTest, Connect) { |
| 259 | // The variable |use_nonblocking_io| has no effect in non-Windows ports. |
| 260 | ConnectTest(false); |
| 261 | } |
| 262 | |
| 263 | #if defined(OS_WIN) |
| 264 | TEST_F(UDPSocketTest, ConnectNonBlocking) { |
| 265 | ConnectTest(true); |
| 266 | } |
| 267 | #endif |
| 268 | |
[email protected] | 86c8e512 | 2012-11-15 20:34:43 | [diff] [blame] | 269 | #if defined(OS_MACOSX) |
[email protected] | 2cb8310 | 2012-09-18 19:13:01 | [diff] [blame] | 270 | // UDPSocketPrivate_Broadcast is disabled for OSX because it requires |
| 271 | // root permissions on OSX 10.7+. |
[email protected] | 86c8e512 | 2012-11-15 20:34:43 | [diff] [blame] | 272 | TEST_F(UDPSocketTest, DISABLED_Broadcast) { |
| 273 | #elif defined(OS_ANDROID) |
mef | 39df8ac | 2015-06-16 17:19:41 | [diff] [blame] | 274 | // Disabled for Android because devices attached to testbots don't have default |
| 275 | // network, so broadcasting to 255.255.255.255 returns error -109 (Address not |
| 276 | // reachable). crbug.com/139144. |
[email protected] | 2cb8310 | 2012-09-18 19:13:01 | [diff] [blame] | 277 | TEST_F(UDPSocketTest, DISABLED_Broadcast) { |
| 278 | #else |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 279 | TEST_F(UDPSocketTest, Broadcast) { |
[email protected] | 2cb8310 | 2012-09-18 19:13:01 | [diff] [blame] | 280 | #endif |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 281 | const uint16_t kPort = 9999; |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 282 | std::string first_message("first message"), second_message("second message"); |
| 283 | |
| 284 | IPEndPoint broadcast_address; |
| 285 | CreateUDPAddress("255.255.255.255", kPort, &broadcast_address); |
| 286 | IPEndPoint listen_address; |
| 287 | CreateUDPAddress("0.0.0.0", kPort, &listen_address); |
| 288 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 289 | TestNetLog server1_log, server2_log; |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 290 | scoped_ptr<UDPServerSocket> server1( |
| 291 | new UDPServerSocket(&server1_log, NetLog::Source())); |
| 292 | scoped_ptr<UDPServerSocket> server2( |
| 293 | new UDPServerSocket(&server2_log, NetLog::Source())); |
| 294 | server1->AllowAddressReuse(); |
| 295 | server1->AllowBroadcast(); |
| 296 | server2->AllowAddressReuse(); |
| 297 | server2->AllowBroadcast(); |
| 298 | |
| 299 | int rv = server1->Listen(listen_address); |
| 300 | EXPECT_EQ(OK, rv); |
| 301 | rv = server2->Listen(listen_address); |
| 302 | EXPECT_EQ(OK, rv); |
| 303 | |
| 304 | rv = SendToSocket(server1.get(), first_message, broadcast_address); |
[email protected] | 86c8e512 | 2012-11-15 20:34:43 | [diff] [blame] | 305 | ASSERT_EQ(static_cast<int>(first_message.size()), rv); |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 306 | std::string str = RecvFromSocket(server1.get()); |
| 307 | ASSERT_EQ(first_message, str); |
| 308 | str = RecvFromSocket(server2.get()); |
| 309 | ASSERT_EQ(first_message, str); |
| 310 | |
| 311 | rv = SendToSocket(server2.get(), second_message, broadcast_address); |
[email protected] | 86c8e512 | 2012-11-15 20:34:43 | [diff] [blame] | 312 | ASSERT_EQ(static_cast<int>(second_message.size()), rv); |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 313 | str = RecvFromSocket(server1.get()); |
| 314 | ASSERT_EQ(second_message, str); |
| 315 | str = RecvFromSocket(server2.get()); |
| 316 | ASSERT_EQ(second_message, str); |
| 317 | } |
| 318 | |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 319 | // In this test, we verify that random binding logic works, which attempts |
| 320 | // to bind to a random port and returns if succeeds, otherwise retries for |
| 321 | // |kBindRetries| number of times. |
| 322 | |
| 323 | // To generate the scenario, we first create |kBindRetries| number of |
| 324 | // UDPClientSockets with default binding policy and connect to the same |
| 325 | // peer and save the used port numbers. Then we get rid of the last |
| 326 | // socket, making sure that the local port it was bound to is available. |
| 327 | // Finally, we create a socket with random binding policy, passing it a |
| 328 | // test PRNG that would serve used port numbers in the array, one after |
| 329 | // another. At the end, we make sure that the test socket was bound to the |
| 330 | // port that became available after deleting the last socket with default |
| 331 | // binding policy. |
| 332 | |
| 333 | // We do not test the randomness of bound ports, but that we are using |
| 334 | // passed in PRNG correctly, thus, it's the duty of PRNG to produce strong |
| 335 | // random numbers. |
| 336 | static const int kBindRetries = 10; |
| 337 | |
| 338 | class TestPrng { |
| 339 | public: |
| 340 | explicit TestPrng(const std::deque<int>& numbers) : numbers_(numbers) {} |
| 341 | int GetNext(int /* min */, int /* max */) { |
| 342 | DCHECK(!numbers_.empty()); |
| 343 | int rv = numbers_.front(); |
| 344 | numbers_.pop_front(); |
| 345 | return rv; |
| 346 | } |
| 347 | private: |
| 348 | std::deque<int> numbers_; |
| 349 | |
| 350 | DISALLOW_COPY_AND_ASSIGN(TestPrng); |
| 351 | }; |
| 352 | |
| 353 | TEST_F(UDPSocketTest, ConnectRandomBind) { |
| 354 | std::vector<UDPClientSocket*> sockets; |
| 355 | IPEndPoint peer_address; |
mef | 39df8ac | 2015-06-16 17:19:41 | [diff] [blame] | 356 | CreateUDPAddress("127.0.0.1", 53, &peer_address); |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 357 | |
| 358 | // Create and connect sockets and save port numbers. |
| 359 | std::deque<int> used_ports; |
| 360 | for (int i = 0; i < kBindRetries; ++i) { |
| 361 | UDPClientSocket* socket = |
| 362 | new UDPClientSocket(DatagramSocket::DEFAULT_BIND, |
| 363 | RandIntCallback(), |
| 364 | NULL, |
| 365 | NetLog::Source()); |
| 366 | sockets.push_back(socket); |
| 367 | EXPECT_EQ(OK, socket->Connect(peer_address)); |
| 368 | |
| 369 | IPEndPoint client_address; |
| 370 | EXPECT_EQ(OK, socket->GetLocalAddress(&client_address)); |
| 371 | used_ports.push_back(client_address.port()); |
| 372 | } |
| 373 | |
| 374 | // Free the last socket, its local port is still in |used_ports|. |
| 375 | delete sockets.back(); |
| 376 | sockets.pop_back(); |
| 377 | |
| 378 | TestPrng test_prng(used_ports); |
| 379 | RandIntCallback rand_int_cb = |
| 380 | base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); |
| 381 | |
| 382 | // Create a socket with random binding policy and connect. |
| 383 | scoped_ptr<UDPClientSocket> test_socket( |
| 384 | new UDPClientSocket(DatagramSocket::RANDOM_BIND, |
| 385 | rand_int_cb, |
| 386 | NULL, |
| 387 | NetLog::Source())); |
| 388 | EXPECT_EQ(OK, test_socket->Connect(peer_address)); |
| 389 | |
| 390 | // Make sure that the last port number in the |used_ports| was used. |
| 391 | IPEndPoint client_address; |
| 392 | EXPECT_EQ(OK, test_socket->GetLocalAddress(&client_address)); |
| 393 | EXPECT_EQ(used_ports.back(), client_address.port()); |
| 394 | |
| 395 | STLDeleteElements(&sockets); |
| 396 | } |
| 397 | |
[email protected] | 391de68 | 2013-02-22 22:44:44 | [diff] [blame] | 398 | // Return a privileged port (under 1024) so binding will fail. |
| 399 | int PrivilegedRand(int min, int max) { |
| 400 | // Chosen by fair dice roll. Guaranteed to be random. |
| 401 | return 4; |
| 402 | } |
| 403 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 404 | #if defined(OS_IOS) && !TARGET_IPHONE_SIMULATOR |
| 405 | // TODO(droger): On iOS this test fails on device (but passes on simulator). |
| 406 | // See http://crbug.com/227760. |
| 407 | #define MAYBE_ConnectFail DISABLED_ConnectFail |
| 408 | #else |
| 409 | #define MAYBE_ConnectFail ConnectFail |
| 410 | #endif |
| 411 | TEST_F(UDPSocketTest, MAYBE_ConnectFail) { |
[email protected] | 391de68 | 2013-02-22 22:44:44 | [diff] [blame] | 412 | IPEndPoint peer_address; |
| 413 | CreateUDPAddress("0.0.0.0", 53, &peer_address); |
| 414 | |
| 415 | scoped_ptr<UDPSocket> socket( |
| 416 | new UDPSocket(DatagramSocket::RANDOM_BIND, |
| 417 | base::Bind(&PrivilegedRand), |
| 418 | NULL, |
| 419 | NetLog::Source())); |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 420 | int rv = socket->Open(peer_address.GetFamily()); |
| 421 | EXPECT_EQ(OK, rv); |
| 422 | rv = socket->Connect(peer_address); |
[email protected] | 391de68 | 2013-02-22 22:44:44 | [diff] [blame] | 423 | // Connect should have failed since we couldn't bind to that port, |
| 424 | EXPECT_NE(OK, rv); |
| 425 | // Make sure that UDPSocket actually closed the socket. |
| 426 | EXPECT_FALSE(socket->is_connected()); |
| 427 | } |
| 428 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 429 | // In this test, we verify that connect() on a socket will have the effect |
| 430 | // of filtering reads on this socket only to data read from the destination |
| 431 | // we connected to. |
| 432 | // |
| 433 | // The purpose of this test is that some documentation indicates that connect |
| 434 | // binds the client's sends to send to a particular server endpoint, but does |
| 435 | // not bind the client's reads to only be from that endpoint, and that we need |
| 436 | // to always use recvfrom() to disambiguate. |
| 437 | TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 438 | const uint16_t kPort1 = 9999; |
| 439 | const uint16_t kPort2 = 10000; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 440 | std::string simple_message("hello world!"); |
| 441 | std::string foreign_message("BAD MESSAGE TO GET!!"); |
| 442 | |
| 443 | // Setup the first server to listen. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 444 | IPEndPoint bind_address; |
[email protected] | 2cb8310 | 2012-09-18 19:13:01 | [diff] [blame] | 445 | CreateUDPAddress("127.0.0.1", kPort1, &bind_address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 446 | UDPServerSocket server1(NULL, NetLog::Source()); |
[email protected] | 2cb8310 | 2012-09-18 19:13:01 | [diff] [blame] | 447 | server1.AllowAddressReuse(); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 448 | int rv = server1.Listen(bind_address); |
[email protected] | d72a14f | 2012-09-11 16:27:11 | [diff] [blame] | 449 | ASSERT_EQ(OK, rv); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 450 | |
| 451 | // Setup the second server to listen. |
[email protected] | 2cb8310 | 2012-09-18 19:13:01 | [diff] [blame] | 452 | CreateUDPAddress("127.0.0.1", kPort2, &bind_address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 453 | UDPServerSocket server2(NULL, NetLog::Source()); |
[email protected] | 2cb8310 | 2012-09-18 19:13:01 | [diff] [blame] | 454 | server2.AllowAddressReuse(); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 455 | rv = server2.Listen(bind_address); |
[email protected] | d72a14f | 2012-09-11 16:27:11 | [diff] [blame] | 456 | ASSERT_EQ(OK, rv); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 457 | |
| 458 | // Setup the client, connected to server 1. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 459 | IPEndPoint server_address; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 460 | CreateUDPAddress("127.0.0.1", kPort1, &server_address); |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 461 | UDPClientSocket client(DatagramSocket::DEFAULT_BIND, |
| 462 | RandIntCallback(), |
| 463 | NULL, |
| 464 | NetLog::Source()); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 465 | rv = client.Connect(server_address); |
| 466 | EXPECT_EQ(OK, rv); |
| 467 | |
| 468 | // Client sends to server1. |
| 469 | rv = WriteSocket(&client, simple_message); |
| 470 | EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
| 471 | |
| 472 | // Server1 waits for message. |
| 473 | std::string str = RecvFromSocket(&server1); |
| 474 | DCHECK(simple_message == str); |
| 475 | |
| 476 | // Get the client's address. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 477 | IPEndPoint client_address; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 478 | rv = client.GetLocalAddress(&client_address); |
| 479 | EXPECT_EQ(OK, rv); |
| 480 | |
| 481 | // Server2 sends reply. |
| 482 | rv = SendToSocket(&server2, foreign_message, |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 483 | client_address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 484 | EXPECT_EQ(foreign_message.length(), static_cast<size_t>(rv)); |
| 485 | |
| 486 | // Server1 sends reply. |
| 487 | rv = SendToSocket(&server1, simple_message, |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 488 | client_address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 489 | EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
| 490 | |
| 491 | // Client waits for response. |
| 492 | str = ReadSocket(&client); |
| 493 | DCHECK(simple_message == str); |
| 494 | } |
| 495 | |
ellyjones | 74165fa | 2015-08-27 16:28:05 | [diff] [blame] | 496 | TEST_F(UDPSocketTest, ClientGetLocalPeerAddresses) { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 497 | struct TestData { |
| 498 | std::string remote_address; |
| 499 | std::string local_address; |
[email protected] | 7b6afd0 | 2011-03-12 00:03:12 | [diff] [blame] | 500 | bool may_fail; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 501 | } tests[] = { |
| 502 | { "127.0.00.1", "127.0.0.1", false }, |
[email protected] | 75aa495 | 2011-03-11 21:38:55 | [diff] [blame] | 503 | { "::1", "::1", true }, |
ellyjones | 74165fa | 2015-08-27 16:28:05 | [diff] [blame] | 504 | #if !defined(OS_ANDROID) && !defined(OS_IOS) |
[email protected] | 86c8e512 | 2012-11-15 20:34:43 | [diff] [blame] | 505 | // Addresses below are disabled on Android. See crbug.com/161248 |
ellyjones | 74165fa | 2015-08-27 16:28:05 | [diff] [blame] | 506 | // They are also disabled on iOS. See https://crbug.com/523225 |
[email protected] | 86c8e512 | 2012-11-15 20:34:43 | [diff] [blame] | 507 | { "192.168.1.1", "127.0.0.1", false }, |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 508 | { "2001:db8:0::42", "::1", true }, |
[email protected] | 86c8e512 | 2012-11-15 20:34:43 | [diff] [blame] | 509 | #endif |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 510 | }; |
viettrungluu | e4a8b88 | 2014-10-16 06:17:38 | [diff] [blame] | 511 | for (size_t i = 0; i < arraysize(tests); i++) { |
[email protected] | 7b6afd0 | 2011-03-12 00:03:12 | [diff] [blame] | 512 | SCOPED_TRACE(std::string("Connecting from ") + tests[i].local_address + |
| 513 | std::string(" to ") + tests[i].remote_address); |
| 514 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 515 | IPAddressNumber ip_number; |
| 516 | ParseIPLiteralToNumber(tests[i].remote_address, &ip_number); |
| 517 | IPEndPoint remote_address(ip_number, 80); |
| 518 | ParseIPLiteralToNumber(tests[i].local_address, &ip_number); |
| 519 | IPEndPoint local_address(ip_number, 80); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 520 | |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 521 | UDPClientSocket client(DatagramSocket::DEFAULT_BIND, |
| 522 | RandIntCallback(), |
| 523 | NULL, |
| 524 | NetLog::Source()); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 525 | int rv = client.Connect(remote_address); |
[email protected] | 7b6afd0 | 2011-03-12 00:03:12 | [diff] [blame] | 526 | if (tests[i].may_fail && rv == ERR_ADDRESS_UNREACHABLE) { |
| 527 | // Connect() may return ERR_ADDRESS_UNREACHABLE for IPv6 |
| 528 | // addresses if IPv6 is not configured. |
| 529 | continue; |
| 530 | } |
| 531 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 532 | EXPECT_LE(ERR_IO_PENDING, rv); |
| 533 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 534 | IPEndPoint fetched_local_address; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 535 | rv = client.GetLocalAddress(&fetched_local_address); |
| 536 | EXPECT_EQ(OK, rv); |
| 537 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 538 | // TODO(mbelshe): figure out how to verify the IP and port. |
| 539 | // The port is dynamically generated by the udp stack. |
| 540 | // The IP is the real IP of the client, not necessarily |
| 541 | // loopback. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 542 | //EXPECT_EQ(local_address.address(), fetched_local_address.address()); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 543 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 544 | IPEndPoint fetched_remote_address; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 545 | rv = client.GetPeerAddress(&fetched_remote_address); |
| 546 | EXPECT_EQ(OK, rv); |
| 547 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 548 | EXPECT_EQ(remote_address, fetched_remote_address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
| 552 | TEST_F(UDPSocketTest, ServerGetLocalAddress) { |
[email protected] | 7b6afd0 | 2011-03-12 00:03:12 | [diff] [blame] | 553 | IPEndPoint bind_address; |
| 554 | CreateUDPAddress("127.0.0.1", 0, &bind_address); |
| 555 | UDPServerSocket server(NULL, NetLog::Source()); |
| 556 | int rv = server.Listen(bind_address); |
| 557 | EXPECT_EQ(OK, rv); |
| 558 | |
| 559 | IPEndPoint local_address; |
| 560 | rv = server.GetLocalAddress(&local_address); |
| 561 | EXPECT_EQ(rv, 0); |
| 562 | |
| 563 | // Verify that port was allocated. |
[email protected] | c10d57a1 | 2011-04-05 21:36:24 | [diff] [blame] | 564 | EXPECT_GT(local_address.port(), 0); |
[email protected] | 7b6afd0 | 2011-03-12 00:03:12 | [diff] [blame] | 565 | EXPECT_EQ(local_address.address(), bind_address.address()); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | TEST_F(UDPSocketTest, ServerGetPeerAddress) { |
[email protected] | 7b6afd0 | 2011-03-12 00:03:12 | [diff] [blame] | 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); |
| 574 | |
| 575 | IPEndPoint peer_address; |
| 576 | rv = server.GetPeerAddress(&peer_address); |
| 577 | EXPECT_EQ(rv, ERR_SOCKET_NOT_CONNECTED); |
| 578 | } |
| 579 | |
| 580 | // Close the socket while read is pending. |
| 581 | TEST_F(UDPSocketTest, CloseWithPendingRead) { |
| 582 | IPEndPoint bind_address; |
| 583 | CreateUDPAddress("127.0.0.1", 0, &bind_address); |
| 584 | UDPServerSocket server(NULL, NetLog::Source()); |
| 585 | int rv = server.Listen(bind_address); |
| 586 | EXPECT_EQ(OK, rv); |
| 587 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 588 | TestCompletionCallback callback; |
[email protected] | 7b6afd0 | 2011-03-12 00:03:12 | [diff] [blame] | 589 | IPEndPoint from; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 590 | rv = server.RecvFrom(buffer_.get(), kMaxRead, &from, callback.callback()); |
[email protected] | 7b6afd0 | 2011-03-12 00:03:12 | [diff] [blame] | 591 | EXPECT_EQ(rv, ERR_IO_PENDING); |
| 592 | |
| 593 | server.Close(); |
| 594 | |
[email protected] | a647bcf | 2011-03-14 18:33:31 | [diff] [blame] | 595 | EXPECT_FALSE(callback.have_result()); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 596 | } |
| 597 | |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 598 | #if defined(OS_ANDROID) |
| 599 | // Some Android devices do not support multicast socket. |
| 600 | // The ones supporting multicast need WifiManager.MulitcastLock to enable it. |
| 601 | // http://goo.gl/jjAk9 |
| 602 | #define MAYBE_JoinMulticastGroup DISABLED_JoinMulticastGroup |
| 603 | #else |
| 604 | #define MAYBE_JoinMulticastGroup JoinMulticastGroup |
| 605 | #endif // defined(OS_ANDROID) |
| 606 | |
| 607 | TEST_F(UDPSocketTest, MAYBE_JoinMulticastGroup) { |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 608 | const uint16_t kPort = 9999; |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 609 | const char kGroup[] = "237.132.100.17"; |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 610 | |
| 611 | IPEndPoint bind_address; |
| 612 | CreateUDPAddress("0.0.0.0", kPort, &bind_address); |
| 613 | IPAddressNumber group_ip; |
| 614 | EXPECT_TRUE(ParseIPLiteralToNumber(kGroup, &group_ip)); |
| 615 | |
| 616 | UDPSocket socket(DatagramSocket::DEFAULT_BIND, |
| 617 | RandIntCallback(), |
| 618 | NULL, |
| 619 | NetLog::Source()); |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 620 | EXPECT_EQ(OK, socket.Open(bind_address.GetFamily())); |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 621 | EXPECT_EQ(OK, socket.Bind(bind_address)); |
| 622 | EXPECT_EQ(OK, socket.JoinGroup(group_ip)); |
| 623 | // Joining group multiple times. |
| 624 | EXPECT_NE(OK, socket.JoinGroup(group_ip)); |
| 625 | EXPECT_EQ(OK, socket.LeaveGroup(group_ip)); |
| 626 | // Leaving group multiple times. |
| 627 | EXPECT_NE(OK, socket.LeaveGroup(group_ip)); |
| 628 | |
| 629 | socket.Close(); |
| 630 | } |
| 631 | |
| 632 | TEST_F(UDPSocketTest, MulticastOptions) { |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 633 | const uint16_t kPort = 9999; |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 634 | IPEndPoint bind_address; |
| 635 | CreateUDPAddress("0.0.0.0", kPort, &bind_address); |
| 636 | |
| 637 | UDPSocket socket(DatagramSocket::DEFAULT_BIND, |
| 638 | RandIntCallback(), |
| 639 | NULL, |
| 640 | NetLog::Source()); |
| 641 | // Before binding. |
| 642 | EXPECT_EQ(OK, socket.SetMulticastLoopbackMode(false)); |
| 643 | EXPECT_EQ(OK, socket.SetMulticastLoopbackMode(true)); |
| 644 | EXPECT_EQ(OK, socket.SetMulticastTimeToLive(0)); |
| 645 | EXPECT_EQ(OK, socket.SetMulticastTimeToLive(3)); |
| 646 | EXPECT_NE(OK, socket.SetMulticastTimeToLive(-1)); |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 647 | EXPECT_EQ(OK, socket.SetMulticastInterface(0)); |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 648 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 649 | EXPECT_EQ(OK, socket.Open(bind_address.GetFamily())); |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 650 | EXPECT_EQ(OK, socket.Bind(bind_address)); |
| 651 | |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 652 | EXPECT_NE(OK, socket.SetMulticastLoopbackMode(false)); |
| 653 | EXPECT_NE(OK, socket.SetMulticastTimeToLive(0)); |
| 654 | EXPECT_NE(OK, socket.SetMulticastInterface(0)); |
| 655 | |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 656 | socket.Close(); |
| 657 | } |
| 658 | |
[email protected] | 3d2fd54 | 2014-08-12 03:07:08 | [diff] [blame] | 659 | // Checking that DSCP bits are set correctly is difficult, |
| 660 | // but let's check that the code doesn't crash at least. |
| 661 | TEST_F(UDPSocketTest, SetDSCP) { |
| 662 | // Setup the server to listen. |
| 663 | IPEndPoint bind_address; |
| 664 | UDPSocket client(DatagramSocket::DEFAULT_BIND, |
| 665 | RandIntCallback(), |
| 666 | NULL, |
| 667 | NetLog::Source()); |
| 668 | // We need a real IP, but we won't actually send anything to it. |
| 669 | CreateUDPAddress("8.8.8.8", 9999, &bind_address); |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 670 | int rv = client.Open(bind_address.GetFamily()); |
| 671 | EXPECT_EQ(OK, rv); |
| 672 | |
| 673 | rv = client.Connect(bind_address); |
[email protected] | 3d2fd54 | 2014-08-12 03:07:08 | [diff] [blame] | 674 | if (rv != OK) { |
| 675 | // Let's try localhost then.. |
| 676 | CreateUDPAddress("127.0.0.1", 9999, &bind_address); |
| 677 | rv = client.Connect(bind_address); |
| 678 | } |
| 679 | EXPECT_EQ(OK, rv); |
| 680 | |
| 681 | client.SetDiffServCodePoint(DSCP_NO_CHANGE); |
| 682 | client.SetDiffServCodePoint(DSCP_AF41); |
| 683 | client.SetDiffServCodePoint(DSCP_DEFAULT); |
| 684 | client.SetDiffServCodePoint(DSCP_CS2); |
| 685 | client.SetDiffServCodePoint(DSCP_NO_CHANGE); |
| 686 | client.SetDiffServCodePoint(DSCP_DEFAULT); |
| 687 | client.Close(); |
| 688 | } |
| 689 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 690 | } // namespace |
| 691 | |
[email protected] | 3d2fd54 | 2014-08-12 03:07:08 | [diff] [blame] | 692 | #if defined(OS_WIN) |
| 693 | |
| 694 | namespace { |
| 695 | |
| 696 | const HANDLE kFakeHandle = (HANDLE)19; |
| 697 | const QOS_FLOWID kFakeFlowId = (QOS_FLOWID)27; |
| 698 | |
| 699 | BOOL WINAPI FakeQOSCreateHandleFAIL(PQOS_VERSION version, PHANDLE handle) { |
| 700 | EXPECT_EQ(0, version->MinorVersion); |
| 701 | EXPECT_EQ(1, version->MajorVersion); |
| 702 | SetLastError(ERROR_OPEN_FAILED); |
| 703 | return false; |
| 704 | } |
| 705 | |
| 706 | BOOL WINAPI FakeQOSCreateHandle(PQOS_VERSION version, PHANDLE handle) { |
| 707 | EXPECT_EQ(0, version->MinorVersion); |
| 708 | EXPECT_EQ(1, version->MajorVersion); |
| 709 | *handle = kFakeHandle; |
| 710 | return true; |
| 711 | } |
| 712 | |
| 713 | BOOL WINAPI FakeQOSCloseHandle(HANDLE handle) { |
| 714 | EXPECT_EQ(kFakeHandle, handle); |
| 715 | return true; |
| 716 | } |
| 717 | |
| 718 | QOS_TRAFFIC_TYPE g_expected_traffic_type; |
| 719 | |
| 720 | BOOL WINAPI FakeQOSAddSocketToFlow(HANDLE handle, |
| 721 | SOCKET socket, |
| 722 | PSOCKADDR addr, |
| 723 | QOS_TRAFFIC_TYPE traffic_type, |
| 724 | DWORD flags, |
| 725 | PQOS_FLOWID flow_id) { |
| 726 | EXPECT_EQ(kFakeHandle, handle); |
| 727 | EXPECT_EQ(NULL, addr); |
thakis | 6ef917b | 2015-12-10 19:29:57 | [diff] [blame] | 728 | EXPECT_EQ(static_cast<DWORD>(QOS_NON_ADAPTIVE_FLOW), flags); |
| 729 | EXPECT_EQ(0u, *flow_id); |
[email protected] | 3d2fd54 | 2014-08-12 03:07:08 | [diff] [blame] | 730 | *flow_id = kFakeFlowId; |
| 731 | return true; |
| 732 | } |
| 733 | |
| 734 | BOOL WINAPI FakeQOSRemoveSocketFromFlow(HANDLE handle, |
| 735 | SOCKET socket, |
| 736 | QOS_FLOWID flowid, |
| 737 | DWORD reserved) { |
| 738 | EXPECT_EQ(kFakeHandle, handle); |
thakis | 6ef917b | 2015-12-10 19:29:57 | [diff] [blame] | 739 | EXPECT_EQ(0u, socket); |
[email protected] | 3d2fd54 | 2014-08-12 03:07:08 | [diff] [blame] | 740 | EXPECT_EQ(kFakeFlowId, flowid); |
thakis | 6ef917b | 2015-12-10 19:29:57 | [diff] [blame] | 741 | EXPECT_EQ(0u, reserved); |
[email protected] | 3d2fd54 | 2014-08-12 03:07:08 | [diff] [blame] | 742 | return true; |
| 743 | } |
| 744 | |
| 745 | DWORD g_expected_dscp; |
| 746 | |
| 747 | BOOL WINAPI FakeQOSSetFlow(HANDLE handle, |
| 748 | QOS_FLOWID flow_id, |
| 749 | QOS_SET_FLOW op, |
| 750 | ULONG size, |
| 751 | PVOID data, |
| 752 | DWORD reserved, |
| 753 | LPOVERLAPPED overlapped) { |
| 754 | EXPECT_EQ(kFakeHandle, handle); |
| 755 | EXPECT_EQ(QOSSetOutgoingDSCPValue, op); |
| 756 | EXPECT_EQ(sizeof(DWORD), size); |
| 757 | EXPECT_EQ(g_expected_dscp, *reinterpret_cast<DWORD*>(data)); |
| 758 | EXPECT_EQ(kFakeFlowId, flow_id); |
thakis | 6ef917b | 2015-12-10 19:29:57 | [diff] [blame] | 759 | EXPECT_EQ(0u, reserved); |
[email protected] | 3d2fd54 | 2014-08-12 03:07:08 | [diff] [blame] | 760 | EXPECT_EQ(NULL, overlapped); |
| 761 | return true; |
| 762 | } |
| 763 | |
| 764 | } // namespace |
| 765 | |
| 766 | // Mock out the Qwave functions and make sure they are |
| 767 | // called correctly. Must be in net namespace for friendship |
| 768 | // reasons. |
| 769 | TEST_F(UDPSocketTest, SetDSCPFake) { |
| 770 | // Setup the server to listen. |
| 771 | IPEndPoint bind_address; |
| 772 | // We need a real IP, but we won't actually send anything to it. |
| 773 | CreateUDPAddress("8.8.8.8", 9999, &bind_address); |
| 774 | UDPSocket client(DatagramSocket::DEFAULT_BIND, |
| 775 | RandIntCallback(), |
| 776 | NULL, |
| 777 | NetLog::Source()); |
| 778 | int rv = client.SetDiffServCodePoint(DSCP_AF41); |
| 779 | EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, rv); |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 780 | |
| 781 | rv = client.Open(bind_address.GetFamily()); |
| 782 | EXPECT_EQ(OK, rv); |
| 783 | |
[email protected] | 3d2fd54 | 2014-08-12 03:07:08 | [diff] [blame] | 784 | rv = client.Connect(bind_address); |
| 785 | EXPECT_EQ(OK, rv); |
| 786 | |
| 787 | QwaveAPI& qos(QwaveAPI::Get()); |
| 788 | qos.create_handle_func_ = FakeQOSCreateHandleFAIL; |
| 789 | qos.close_handle_func_ = FakeQOSCloseHandle; |
| 790 | qos.add_socket_to_flow_func_ = FakeQOSAddSocketToFlow; |
| 791 | qos.remove_socket_from_flow_func_ = FakeQOSRemoveSocketFromFlow; |
| 792 | qos.set_flow_func_ = FakeQOSSetFlow; |
| 793 | qos.qwave_supported_ = true; |
| 794 | |
| 795 | EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); |
| 796 | EXPECT_EQ(ERROR_NOT_SUPPORTED, client.SetDiffServCodePoint(DSCP_AF41)); |
| 797 | qos.create_handle_func_ = FakeQOSCreateHandle; |
| 798 | g_expected_dscp = DSCP_AF41; |
| 799 | g_expected_traffic_type = QOSTrafficTypeAudioVideo; |
| 800 | EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_AF41)); |
| 801 | g_expected_dscp = DSCP_DEFAULT; |
| 802 | g_expected_traffic_type = QOSTrafficTypeBestEffort; |
| 803 | EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); |
| 804 | g_expected_dscp = DSCP_CS2; |
| 805 | g_expected_traffic_type = QOSTrafficTypeExcellentEffort; |
| 806 | EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_CS2)); |
| 807 | g_expected_dscp = DSCP_CS3; |
| 808 | g_expected_traffic_type = QOSTrafficTypeExcellentEffort; |
| 809 | EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); |
| 810 | g_expected_dscp = DSCP_DEFAULT; |
| 811 | g_expected_traffic_type = QOSTrafficTypeBestEffort; |
| 812 | EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); |
| 813 | client.Close(); |
| 814 | } |
| 815 | #endif |
| 816 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 817 | } // namespace net |