OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/udp/udp_client_socket.h" | 5 #include "net/udp/udp_client_socket.h" |
6 #include "net/udp/udp_server_socket.h" | 6 #include "net/udp/udp_server_socket.h" |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 if (rv == ERR_IO_PENDING) | 100 if (rv == ERR_IO_PENDING) |
101 rv = callback.WaitForResult(); | 101 rv = callback.WaitForResult(); |
102 if (rv <= 0) | 102 if (rv <= 0) |
103 return bytes_sent > 0 ? bytes_sent : rv; | 103 return bytes_sent > 0 ? bytes_sent : rv; |
104 bytes_sent += rv; | 104 bytes_sent += rv; |
105 buffer->DidConsume(rv); | 105 buffer->DidConsume(rv); |
106 } | 106 } |
107 return bytes_sent; | 107 return bytes_sent; |
108 } | 108 } |
109 | 109 |
110 private: | 110 protected: |
111 static const int kMaxRead = 1024; | 111 static const int kMaxRead = 1024; |
112 scoped_refptr<IOBufferWithSize> buffer_; | 112 scoped_refptr<IOBufferWithSize> buffer_; |
113 IPEndPoint recv_from_address_; | 113 IPEndPoint recv_from_address_; |
114 }; | 114 }; |
115 | 115 |
116 // Creates and address from an ip/port and returns it in |address|. | 116 // Creates and address from an ip/port and returns it in |address|. |
117 void CreateUDPAddress(std::string ip_str, int port, IPEndPoint* address) { | 117 void CreateUDPAddress(std::string ip_str, int port, IPEndPoint* address) { |
118 IPAddressNumber ip_number; | 118 IPAddressNumber ip_number; |
119 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); | 119 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); |
120 if (!rv) | 120 if (!rv) |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 | 216 |
217 // Client waits for response. | 217 // Client waits for response. |
218 str = ReadSocket(&client); | 218 str = ReadSocket(&client); |
219 DCHECK(simple_message == str); | 219 DCHECK(simple_message == str); |
220 } | 220 } |
221 | 221 |
222 TEST_F(UDPSocketTest, ClientGetLocalPeerAddresses) { | 222 TEST_F(UDPSocketTest, ClientGetLocalPeerAddresses) { |
223 struct TestData { | 223 struct TestData { |
224 std::string remote_address; | 224 std::string remote_address; |
225 std::string local_address; | 225 std::string local_address; |
226 bool is_ipv6; | 226 bool may_fail; |
227 } tests[] = { | 227 } tests[] = { |
228 { "127.0.00.1", "127.0.0.1", false }, | 228 { "127.0.00.1", "127.0.0.1", false }, |
229 { "192.168.1.1", "127.0.0.1", false }, | 229 { "192.168.1.1", "127.0.0.1", false }, |
230 { "::1", "::1", true }, | 230 { "::1", "::1", false }, |
231 { "2001:db8:0::42", "::1", true }, | 231 { "2001:db8:0::42", "::1", true }, |
232 }; | 232 }; |
233 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) { | 233 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) { |
| 234 SCOPED_TRACE(std::string("Connecting from ") + tests[i].local_address + |
| 235 std::string(" to ") + tests[i].remote_address); |
| 236 |
234 net::IPAddressNumber ip_number; | 237 net::IPAddressNumber ip_number; |
235 net::ParseIPLiteralToNumber(tests[i].remote_address, &ip_number); | 238 net::ParseIPLiteralToNumber(tests[i].remote_address, &ip_number); |
236 net::IPEndPoint remote_address(ip_number, 80); | 239 net::IPEndPoint remote_address(ip_number, 80); |
237 net::ParseIPLiteralToNumber(tests[i].local_address, &ip_number); | 240 net::ParseIPLiteralToNumber(tests[i].local_address, &ip_number); |
238 net::IPEndPoint local_address(ip_number, 80); | 241 net::IPEndPoint local_address(ip_number, 80); |
239 | 242 |
240 UDPClientSocket client(NULL, NetLog::Source()); | 243 UDPClientSocket client(NULL, NetLog::Source()); |
241 int rv = client.Connect(remote_address); | 244 int rv = client.Connect(remote_address); |
| 245 if (tests[i].may_fail && rv == ERR_ADDRESS_UNREACHABLE) { |
| 246 // Connect() may return ERR_ADDRESS_UNREACHABLE for IPv6 |
| 247 // addresses if IPv6 is not configured. |
| 248 continue; |
| 249 } |
| 250 |
242 EXPECT_LE(ERR_IO_PENDING, rv); | 251 EXPECT_LE(ERR_IO_PENDING, rv); |
243 | 252 |
244 IPEndPoint fetched_local_address; | 253 IPEndPoint fetched_local_address; |
245 rv = client.GetLocalAddress(&fetched_local_address); | 254 rv = client.GetLocalAddress(&fetched_local_address); |
246 EXPECT_EQ(OK, rv); | 255 EXPECT_EQ(OK, rv); |
247 | 256 |
248 // TODO(mbelshe): figure out how to verify the IP and port. | 257 // TODO(mbelshe): figure out how to verify the IP and port. |
249 // The port is dynamically generated by the udp stack. | 258 // The port is dynamically generated by the udp stack. |
250 // The IP is the real IP of the client, not necessarily | 259 // The IP is the real IP of the client, not necessarily |
251 // loopback. | 260 // loopback. |
252 //EXPECT_EQ(local_address.address(), fetched_local_address.address()); | 261 //EXPECT_EQ(local_address.address(), fetched_local_address.address()); |
253 | 262 |
254 IPEndPoint fetched_remote_address; | 263 IPEndPoint fetched_remote_address; |
255 rv = client.GetPeerAddress(&fetched_remote_address); | 264 rv = client.GetPeerAddress(&fetched_remote_address); |
256 EXPECT_EQ(OK, rv); | 265 EXPECT_EQ(OK, rv); |
257 | 266 |
258 EXPECT_EQ(remote_address, fetched_remote_address); | 267 EXPECT_EQ(remote_address, fetched_remote_address); |
259 } | 268 } |
260 } | 269 } |
261 | 270 |
262 TEST_F(UDPSocketTest, ServerGetLocalAddress) { | 271 TEST_F(UDPSocketTest, ServerGetLocalAddress) { |
263 // TODO(mbelshe): implement me | 272 IPEndPoint bind_address; |
| 273 CreateUDPAddress("127.0.0.1", 0, &bind_address); |
| 274 UDPServerSocket server(NULL, NetLog::Source()); |
| 275 int rv = server.Listen(bind_address); |
| 276 EXPECT_EQ(OK, rv); |
| 277 |
| 278 IPEndPoint local_address; |
| 279 rv = server.GetLocalAddress(&local_address); |
| 280 EXPECT_EQ(rv, 0); |
| 281 |
| 282 // Verify that port was allocated. |
| 283 EXPECT_GE(local_address.port(), 0); |
| 284 EXPECT_EQ(local_address.address(), bind_address.address()); |
264 } | 285 } |
265 | 286 |
266 TEST_F(UDPSocketTest, ServerGetPeerAddress) { | 287 TEST_F(UDPSocketTest, ServerGetPeerAddress) { |
267 // TODO(mbelshe): implement me | 288 IPEndPoint bind_address; |
| 289 CreateUDPAddress("127.0.0.1", 0, &bind_address); |
| 290 UDPServerSocket server(NULL, NetLog::Source()); |
| 291 int rv = server.Listen(bind_address); |
| 292 EXPECT_EQ(OK, rv); |
| 293 |
| 294 IPEndPoint peer_address; |
| 295 rv = server.GetPeerAddress(&peer_address); |
| 296 EXPECT_EQ(rv, ERR_SOCKET_NOT_CONNECTED); |
| 297 } |
| 298 |
| 299 // Close the socket while read is pending. |
| 300 TEST_F(UDPSocketTest, CloseWithPendingRead) { |
| 301 IPEndPoint bind_address; |
| 302 CreateUDPAddress("127.0.0.1", 0, &bind_address); |
| 303 UDPServerSocket server(NULL, NetLog::Source()); |
| 304 int rv = server.Listen(bind_address); |
| 305 EXPECT_EQ(OK, rv); |
| 306 |
| 307 TestCompletionCallback callback; |
| 308 IPEndPoint from; |
| 309 rv = server.RecvFrom(buffer_, kMaxRead, &from, &callback); |
| 310 EXPECT_EQ(rv, ERR_IO_PENDING); |
| 311 |
| 312 server.Close(); |
| 313 |
| 314 EXPECT_TRUE(callback.have_result()); |
| 315 EXPECT_EQ(callback.GetResult(rv), ERR_ABORTED); |
268 } | 316 } |
269 | 317 |
270 } // namespace | 318 } // namespace |
271 | 319 |
272 } // namespace net | 320 } // namespace net |
273 | |
274 int main(int argc, char** argv) { | |
275 // Record histograms, so we can get histograms data in tests. | |
276 base::StatisticsRecorder recorder; | |
277 NetTestSuite test_suite(argc, argv); | |
278 | |
279 return test_suite.Run(); | |
280 } | |
OLD | NEW |