OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <arpa/inet.h> | 5 #include <arpa/inet.h> |
6 #include <errno.h> | 6 #include <errno.h> |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <netinet/in.h> | 8 #include <netinet/in.h> |
9 #include <pthread.h> | 9 #include <pthread.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 int len2 = | 237 int len2 = |
238 recvfrom(sock2_, inbuf, sizeof(inbuf), 0, (sockaddr*) &addr, &addrlen); | 238 recvfrom(sock2_, inbuf, sizeof(inbuf), 0, (sockaddr*) &addr, &addrlen); |
239 EXPECT_EQ(sizeof(outbuf), len2); | 239 EXPECT_EQ(sizeof(outbuf), len2); |
240 EXPECT_EQ(sizeof(sockaddr_in), addrlen); | 240 EXPECT_EQ(sizeof(sockaddr_in), addrlen); |
241 EXPECT_EQ(PORT1, htons(addr.sin_port)); | 241 EXPECT_EQ(PORT1, htons(addr.sin_port)); |
242 | 242 |
243 // Now they should be the same | 243 // Now they should be the same |
244 EXPECT_EQ(0, memcmp(outbuf, inbuf, sizeof(outbuf))); | 244 EXPECT_EQ(0, memcmp(outbuf, inbuf, sizeof(outbuf))); |
245 } | 245 } |
246 | 246 |
| 247 TEST_F(SocketTestUDP, SendRcvUnbound) { |
| 248 char outbuf[256]; |
| 249 char inbuf[512]; |
| 250 |
| 251 memset(outbuf, 1, sizeof(outbuf)); |
| 252 memset(inbuf, 0, sizeof(inbuf)); |
| 253 |
| 254 // Don't bind sock1_, this will automatically bind sock1_ to a random port |
| 255 // at the time of the first send. |
| 256 EXPECT_EQ(0, Bind(sock2_, LOCAL_HOST, PORT2)); |
| 257 |
| 258 sockaddr_in addr; |
| 259 sockaddr_in addr2; |
| 260 socklen_t addrlen = sizeof(addr2); |
| 261 IP4ToSockAddr(LOCAL_HOST, PORT2, &addr2); |
| 262 |
| 263 // The first send hasn't occurred, so the socket is not yet bound. |
| 264 socklen_t out_addrlen = sizeof(addr); |
| 265 ASSERT_EQ(0, getsockname(sock1_, (sockaddr*)&addr, &out_addrlen)); |
| 266 EXPECT_EQ(addrlen, out_addrlen); |
| 267 EXPECT_EQ(0, htonl(addr.sin_addr.s_addr)); |
| 268 EXPECT_EQ(0, htons(addr.sin_port)); |
| 269 |
| 270 int len1 = |
| 271 sendto(sock1_, outbuf, sizeof(outbuf), 0, (sockaddr*) &addr2, addrlen); |
| 272 EXPECT_EQ(sizeof(outbuf), len1); |
| 273 |
| 274 // After the first send, the socket should be bound; the port is set, but |
| 275 // the address is still 0. |
| 276 ASSERT_EQ(0, getsockname(sock1_, (sockaddr*)&addr, &out_addrlen)); |
| 277 EXPECT_EQ(addrlen, out_addrlen); |
| 278 EXPECT_EQ(0, htonl(addr.sin_addr.s_addr)); |
| 279 EXPECT_NE(0, htons(addr.sin_port)); |
| 280 |
| 281 // Ensure the buffers are different |
| 282 EXPECT_NE(0, memcmp(outbuf, inbuf, sizeof(outbuf))); |
| 283 |
| 284 // Try to receive the previously sent packet |
| 285 int len2 = |
| 286 recvfrom(sock2_, inbuf, sizeof(inbuf), 0, (sockaddr*) &addr, &addrlen); |
| 287 EXPECT_EQ(sizeof(outbuf), len2); |
| 288 EXPECT_EQ(sizeof(sockaddr_in), addrlen); |
| 289 EXPECT_EQ(LOCAL_HOST, htonl(addr.sin_addr.s_addr)); |
| 290 EXPECT_NE(0, htons(addr.sin_port)); |
| 291 |
| 292 // Now they should be the same |
| 293 EXPECT_EQ(0, memcmp(outbuf, inbuf, sizeof(outbuf))); |
| 294 } |
| 295 |
247 const size_t kQueueSize = 65536 * 8; | 296 const size_t kQueueSize = 65536 * 8; |
248 TEST_F(SocketTestUDP, FullFifo) { | 297 TEST_F(SocketTestUDP, FullFifo) { |
249 char outbuf[16 * 1024]; | 298 char outbuf[16 * 1024]; |
250 | 299 |
251 ASSERT_EQ(0, Bind(sock1_, LOCAL_HOST, PORT1)); | 300 ASSERT_EQ(0, Bind(sock1_, LOCAL_HOST, PORT1)); |
252 ASSERT_EQ(0, Bind(sock2_, LOCAL_HOST, PORT2)); | 301 ASSERT_EQ(0, Bind(sock2_, LOCAL_HOST, PORT2)); |
253 | 302 |
254 sockaddr_in addr; | 303 sockaddr_in addr; |
255 socklen_t addrlen = sizeof(addr); | 304 socklen_t addrlen = sizeof(addr); |
256 IP4ToSockAddr(LOCAL_HOST, PORT2, &addr); | 305 IP4ToSockAddr(LOCAL_HOST, PORT2, &addr); |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
725 while (remainder > 0) { | 774 while (remainder > 0) { |
726 int rtn = recv(new_sock, buffer, remainder, 0); | 775 int rtn = recv(new_sock, buffer, remainder, 0); |
727 ASSERT_GT(rtn, 0); | 776 ASSERT_GT(rtn, 0); |
728 remainder -= rtn; | 777 remainder -= rtn; |
729 } | 778 } |
730 | 779 |
731 ASSERT_EQ(0, close(new_sock)); | 780 ASSERT_EQ(0, close(new_sock)); |
732 } | 781 } |
733 | 782 |
734 #endif // PROVIDES_SOCKET_API | 783 #endif // PROVIDES_SOCKET_API |
OLD | NEW |