Chromium Code Reviews| 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. | |
|
Sam Clegg
2013/11/18 17:10:42
Maybe check getlocalname() before and after the se
binji
2013/11/18 20:54:18
Done.
| |
| 256 //EXPECT_EQ(0, Bind(sock1_, LOCAL_HOST, PORT1)); | |
| 257 EXPECT_EQ(0, Bind(sock2_, LOCAL_HOST, PORT2)); | |
| 258 | |
| 259 sockaddr_in addr; | |
| 260 socklen_t addrlen = sizeof(addr); | |
| 261 IP4ToSockAddr(LOCAL_HOST, PORT2, &addr); | |
| 262 | |
| 263 int len1 = | |
| 264 sendto(sock1_, outbuf, sizeof(outbuf), 0, (sockaddr*) &addr, addrlen); | |
| 265 EXPECT_EQ(sizeof(outbuf), len1); | |
| 266 | |
| 267 // Ensure the buffers are different | |
| 268 EXPECT_NE(0, memcmp(outbuf, inbuf, sizeof(outbuf))); | |
| 269 memset(&addr, 0, sizeof(addr)); | |
| 270 | |
| 271 // Try to receive the previously sent packet | |
| 272 int len2 = | |
| 273 recvfrom(sock2_, inbuf, sizeof(inbuf), 0, (sockaddr*) &addr, &addrlen); | |
| 274 EXPECT_EQ(sizeof(outbuf), len2); | |
| 275 EXPECT_EQ(sizeof(sockaddr_in), addrlen); | |
| 276 EXPECT_EQ(LOCAL_HOST, htonl(addr.sin_addr.s_addr)); | |
| 277 EXPECT_NE(0, htons(addr.sin_port)); | |
| 278 | |
| 279 // Now they should be the same | |
| 280 EXPECT_EQ(0, memcmp(outbuf, inbuf, sizeof(outbuf))); | |
| 281 } | |
| 282 | |
| 247 const size_t kQueueSize = 65536 * 8; | 283 const size_t kQueueSize = 65536 * 8; |
| 248 TEST_F(SocketTestUDP, FullFifo) { | 284 TEST_F(SocketTestUDP, FullFifo) { |
| 249 char outbuf[16 * 1024]; | 285 char outbuf[16 * 1024]; |
| 250 | 286 |
| 251 ASSERT_EQ(0, Bind(sock1_, LOCAL_HOST, PORT1)); | 287 ASSERT_EQ(0, Bind(sock1_, LOCAL_HOST, PORT1)); |
| 252 ASSERT_EQ(0, Bind(sock2_, LOCAL_HOST, PORT2)); | 288 ASSERT_EQ(0, Bind(sock2_, LOCAL_HOST, PORT2)); |
| 253 | 289 |
| 254 sockaddr_in addr; | 290 sockaddr_in addr; |
| 255 socklen_t addrlen = sizeof(addr); | 291 socklen_t addrlen = sizeof(addr); |
| 256 IP4ToSockAddr(LOCAL_HOST, PORT2, &addr); | 292 IP4ToSockAddr(LOCAL_HOST, PORT2, &addr); |
| (...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 725 while (remainder > 0) { | 761 while (remainder > 0) { |
| 726 int rtn = recv(new_sock, buffer, remainder, 0); | 762 int rtn = recv(new_sock, buffer, remainder, 0); |
| 727 ASSERT_GT(rtn, 0); | 763 ASSERT_GT(rtn, 0); |
| 728 remainder -= rtn; | 764 remainder -= rtn; |
| 729 } | 765 } |
| 730 | 766 |
| 731 ASSERT_EQ(0, close(new_sock)); | 767 ASSERT_EQ(0, close(new_sock)); |
| 732 } | 768 } |
| 733 | 769 |
| 734 #endif // PROVIDES_SOCKET_API | 770 #endif // PROVIDES_SOCKET_API |
| OLD | NEW |