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 #include <errno.h> | 4 #include <errno.h> |
5 #include <fcntl.h> | 5 #include <fcntl.h> |
6 #include <pthread.h> | 6 #include <pthread.h> |
7 #include <sys/stat.h> | 7 #include <sys/stat.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 EXPECT_LT(ki_socketpair(AF_INET, SOCK_STREAM, 0, sv), 0); | 261 EXPECT_LT(ki_socketpair(AF_INET, SOCK_STREAM, 0, sv), 0); |
262 EXPECT_EQ(errno, EPROTONOSUPPORT); | 262 EXPECT_EQ(errno, EPROTONOSUPPORT); |
263 EXPECT_LT(ki_socketpair(AF_INET6, SOCK_STREAM, 0, sv), 0); | 263 EXPECT_LT(ki_socketpair(AF_INET6, SOCK_STREAM, 0, sv), 0); |
264 EXPECT_EQ(errno, EPROTONOSUPPORT); | 264 EXPECT_EQ(errno, EPROTONOSUPPORT); |
265 } | 265 } |
266 | 266 |
267 // These utility functions are only used for newlib (glibc provides its own | 267 // These utility functions are only used for newlib (glibc provides its own |
268 // implementations of these functions). | 268 // implementations of these functions). |
269 #if !defined(__GLIBC__) | 269 #if !defined(__GLIBC__) |
270 | 270 |
| 271 TEST(SocketUtilityFunctions, Htonl) { |
| 272 uint32_t host_long = 0x44332211; |
| 273 uint32_t network_long = htonl(host_long); |
| 274 uint8_t network_bytes[4]; |
| 275 memcpy(network_bytes, &network_long, 4); |
| 276 EXPECT_EQ(network_bytes[0], 0x44); |
| 277 EXPECT_EQ(network_bytes[1], 0x33); |
| 278 EXPECT_EQ(network_bytes[2], 0x22); |
| 279 EXPECT_EQ(network_bytes[3], 0x11); |
| 280 } |
| 281 |
| 282 TEST(SocketUtilityFunctions, Htons) { |
| 283 uint16_t host_short = 0x2211; |
| 284 uint16_t network_short = htons(host_short); |
| 285 uint8_t network_bytes[2]; |
| 286 memcpy(network_bytes, &network_short, 2); |
| 287 EXPECT_EQ(network_bytes[0], 0x22); |
| 288 EXPECT_EQ(network_bytes[1], 0x11); |
| 289 } |
| 290 |
271 static struct in_addr generate_ipv4_addr(int tuple1, int tuple2, | 291 static struct in_addr generate_ipv4_addr(int tuple1, int tuple2, |
272 int tuple3, int tuple4) { | 292 int tuple3, int tuple4) { |
273 unsigned char addr[4]; | 293 unsigned char addr[4]; |
274 addr[0] = static_cast<unsigned char>(tuple1); | 294 addr[0] = static_cast<unsigned char>(tuple1); |
275 addr[1] = static_cast<unsigned char>(tuple2); | 295 addr[1] = static_cast<unsigned char>(tuple2); |
276 addr[2] = static_cast<unsigned char>(tuple3); | 296 addr[2] = static_cast<unsigned char>(tuple3); |
277 addr[3] = static_cast<unsigned char>(tuple4); | 297 addr[3] = static_cast<unsigned char>(tuple4); |
278 struct in_addr* real_addr = reinterpret_cast<struct in_addr*>(addr); | 298 struct in_addr real_addr; |
279 return *real_addr; | 299 memcpy(&real_addr, addr, 4); |
| 300 return real_addr; |
280 } | 301 } |
281 | 302 |
282 static struct in6_addr generate_ipv6_addr(int* tuples) { | 303 static struct in6_addr generate_ipv6_addr(int* tuples) { |
283 unsigned char addr[16]; | 304 unsigned char addr[16]; |
284 for (int i = 0; i < 8; i++) { | 305 for (int i = 0; i < 8; i++) { |
285 addr[2*i] = (tuples[i] >> 8) & 0xFF; | 306 addr[2*i] = (tuples[i] >> 8) & 0xFF; |
286 addr[2*i+1] = tuples[i] & 0xFF; | 307 addr[2*i+1] = tuples[i] & 0xFF; |
287 } | 308 } |
288 struct in6_addr* real_addr = reinterpret_cast<struct in6_addr*>(addr); | 309 struct in6_addr real_addr; |
289 return *real_addr; | 310 memcpy(&real_addr, addr, 16); |
| 311 return real_addr; |
290 } | 312 } |
291 | 313 |
292 TEST(SocketUtilityFunctions, Inet_ntoa) { | 314 TEST(SocketUtilityFunctions, Inet_ntoa) { |
293 char* stringified_addr = inet_ntoa(generate_ipv4_addr(0,0,0,0)); | 315 char* stringified_addr = inet_ntoa(generate_ipv4_addr(0,0,0,0)); |
294 ASSERT_TRUE(NULL != stringified_addr); | 316 ASSERT_TRUE(NULL != stringified_addr); |
295 EXPECT_STREQ("0.0.0.0", stringified_addr); | 317 EXPECT_STREQ("0.0.0.0", stringified_addr); |
296 | 318 |
297 stringified_addr = inet_ntoa(generate_ipv4_addr(127,0,0,1)); | 319 stringified_addr = inet_ntoa(generate_ipv4_addr(127,0,0,1)); |
298 ASSERT_TRUE(NULL != stringified_addr); | 320 ASSERT_TRUE(NULL != stringified_addr); |
299 EXPECT_STREQ("127.0.0.1", stringified_addr); | 321 EXPECT_STREQ("127.0.0.1", stringified_addr); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 | 387 |
366 EXPECT_TRUE(NULL == inet_ntop(AF_INET, &ipv4_addr, | 388 EXPECT_TRUE(NULL == inet_ntop(AF_INET, &ipv4_addr, |
367 addr_name, INET_ADDRSTRLEN - 1)); | 389 addr_name, INET_ADDRSTRLEN - 1)); |
368 EXPECT_EQ(errno, ENOSPC); | 390 EXPECT_EQ(errno, ENOSPC); |
369 | 391 |
370 EXPECT_TRUE(NULL == inet_ntop(AF_INET6, &ipv6_addr, | 392 EXPECT_TRUE(NULL == inet_ntop(AF_INET6, &ipv6_addr, |
371 addr_name, INET6_ADDRSTRLEN - 1)); | 393 addr_name, INET6_ADDRSTRLEN - 1)); |
372 EXPECT_EQ(errno, ENOSPC); | 394 EXPECT_EQ(errno, ENOSPC); |
373 } | 395 } |
374 | 396 |
| 397 TEST(SocketUtilityFunctions, Ntohs) { |
| 398 uint8_t network_bytes[2] = { 0x22, 0x11 }; |
| 399 uint16_t network_short; |
| 400 memcpy(&network_short, network_bytes, 2); |
| 401 uint16_t host_short = ntohs(network_short); |
| 402 EXPECT_EQ(host_short, 0x2211); |
| 403 } |
| 404 |
| 405 TEST(SocketUtilityFunctions, Ntohl) { |
| 406 uint8_t network_bytes[4] = { 0x44, 0x33, 0x22, 0x11 }; |
| 407 uint32_t network_long; |
| 408 memcpy(&network_long, network_bytes, 4); |
| 409 uint32_t host_long = ntohl(network_long); |
| 410 EXPECT_EQ(host_long, 0x44332211); |
| 411 } |
| 412 |
375 #endif // !defined(__GLIBC__) | 413 #endif // !defined(__GLIBC__) |
376 #endif // PROVIDES_SOCKETPAIR_API | 414 #endif // PROVIDES_SOCKETPAIR_API |
OLD | NEW |