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 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 | 388 |
389 EXPECT_TRUE(NULL == inet_ntop(AF_INET, &ipv4_addr, | 389 EXPECT_TRUE(NULL == inet_ntop(AF_INET, &ipv4_addr, |
390 addr_name, INET_ADDRSTRLEN - 1)); | 390 addr_name, INET_ADDRSTRLEN - 1)); |
391 EXPECT_EQ(errno, ENOSPC); | 391 EXPECT_EQ(errno, ENOSPC); |
392 | 392 |
393 EXPECT_TRUE(NULL == inet_ntop(AF_INET6, &ipv6_addr, | 393 EXPECT_TRUE(NULL == inet_ntop(AF_INET6, &ipv6_addr, |
394 addr_name, INET6_ADDRSTRLEN - 1)); | 394 addr_name, INET6_ADDRSTRLEN - 1)); |
395 EXPECT_EQ(errno, ENOSPC); | 395 EXPECT_EQ(errno, ENOSPC); |
396 } | 396 } |
397 | 397 |
| 398 TEST(SocketUtilityFunctions, Inet_ptoa) { |
| 399 struct { |
| 400 int family; |
| 401 const char* input; |
| 402 const char* output; |
| 403 } tests[] = { |
| 404 { AF_INET, "127.127.12.0", NULL }, |
| 405 { AF_INET, "0.0.0.0", NULL }, |
| 406 |
| 407 { AF_INET6, "0:0:0:0:0:0:0:0", NULL }, |
| 408 { AF_INET6, "1234:5678:9abc:def0:1234:5678:9abc:def0", NULL }, |
| 409 { AF_INET6, "1:2:3:4:5:6:7:8", NULL }, |
| 410 { AF_INET6, "a:b:c:d:e:f:1:2", NULL }, |
| 411 { AF_INET6, "A:B:C:D:E:F:1:2", "a:b:c:d:e:f:1:2" }, |
| 412 { AF_INET6, "::", "0:0:0:0:0:0:0:0" }, |
| 413 { AF_INET6, "::12", "0:0:0:0:0:0:0:12" }, |
| 414 { AF_INET6, "::1:2:3", "0:0:0:0:0:1:2:3" }, |
| 415 { AF_INET6, "12::", "12:0:0:0:0:0:0:0" }, |
| 416 { AF_INET6, "1:2::", "1:2:0:0:0:0:0:0" }, |
| 417 { AF_INET6, "::12:0:0:0:0:0:0:0", "12:0:0:0:0:0:0:0" }, |
| 418 { AF_INET6, "1:2:3::4:5", "1:2:3:0:0:0:4:5" }, |
| 419 { AF_INET6, "::1.1.1.1", "0:0:0:0:0:0:101:101" }, |
| 420 { AF_INET6, "::ffff:1.1.1.1", "0:0:0:0:0:ffff:101:101" }, |
| 421 { AF_INET6, "ffff::1.1.1.1", "ffff:0:0:0:0:0:101:101" }, |
| 422 { AF_INET6, "::1.1.1.1", "0:0:0:0:0:0:101:101" }, |
| 423 }; |
| 424 |
| 425 for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) { |
| 426 uint8_t addr[16]; |
| 427 EXPECT_TRUE(inet_pton(tests[i].family, tests[i].input, addr)); |
| 428 const char* expected = tests[i].output ? tests[i].output : tests[i].input; |
| 429 char out_buffer[256]; |
| 430 EXPECT_TRUE( |
| 431 inet_ntop(tests[i].family, addr, out_buffer, sizeof(out_buffer))); |
| 432 EXPECT_EQ(std::string(expected), std::string(out_buffer)); |
| 433 } |
| 434 } |
| 435 |
| 436 TEST(SocketUtilityFunctions, Inet_ptoa_failure) { |
| 437 uint8_t addr[16]; |
| 438 EXPECT_EQ(0, inet_pton(AF_INET, "127.127.12.24312", &addr)); |
| 439 EXPECT_EQ(0, inet_pton(AF_INET, "127.127.12.24 ", &addr)); |
| 440 EXPECT_EQ(0, inet_pton(AF_INET, "127.127.12.0.1", &addr)); |
| 441 EXPECT_EQ(0, inet_pton(AF_INET, "127.127.12. 0", &addr)); |
| 442 EXPECT_EQ(0, inet_pton(AF_INET, " 127.127.12.0", &addr)); |
| 443 EXPECT_EQ(0, inet_pton(AF_INET, "127.127.12.0.", &addr)); |
| 444 EXPECT_EQ(0, inet_pton(AF_INET, ".127.127.12.0", &addr)); |
| 445 EXPECT_EQ(0, inet_pton(AF_INET, "127.127.12.0x0", &addr)); |
| 446 |
| 447 EXPECT_EQ(0, inet_pton(AF_INET6, ":::", &addr)); |
| 448 EXPECT_EQ(0, inet_pton(AF_INET6, "0:::0", &addr)); |
| 449 EXPECT_EQ(0, inet_pton(AF_INET6, "0::0:0::1", &addr)); |
| 450 EXPECT_EQ(0, inet_pton(AF_INET6, "0:0:0:0:0:0:1: 2", &addr)); |
| 451 EXPECT_EQ(0, inet_pton(AF_INET6, " 0:0:0:0:0:0:1:2", &addr)); |
| 452 EXPECT_EQ(0, inet_pton(AF_INET6, "0:0:0:0:0:0:1:2 ", &addr)); |
| 453 EXPECT_EQ(0, inet_pton(AF_INET6, ":0:0:0:0:0:0:1:2", &addr)); |
| 454 EXPECT_EQ(0, inet_pton(AF_INET6, "0:0:0:0:0:0:1:2:4", &addr)); |
| 455 EXPECT_EQ(0, inet_pton(AF_INET6, "0:0:0:0:0:0:1:0.0.0.0", &addr)); |
| 456 EXPECT_EQ(0, inet_pton(AF_INET6, "::0.0.0.0:1", &addr)); |
| 457 EXPECT_EQ(0, inet_pton(AF_INET6, "::0.0.0.0.0", &addr)); |
| 458 } |
| 459 |
398 TEST(SocketUtilityFunctions, Ntohs) { | 460 TEST(SocketUtilityFunctions, Ntohs) { |
399 uint8_t network_bytes[2] = { 0x22, 0x11 }; | 461 uint8_t network_bytes[2] = { 0x22, 0x11 }; |
400 uint16_t network_short; | 462 uint16_t network_short; |
401 memcpy(&network_short, network_bytes, 2); | 463 memcpy(&network_short, network_bytes, 2); |
402 uint16_t host_short = ntohs(network_short); | 464 uint16_t host_short = ntohs(network_short); |
403 EXPECT_EQ(host_short, 0x2211); | 465 EXPECT_EQ(host_short, 0x2211); |
404 } | 466 } |
405 | 467 |
406 TEST(SocketUtilityFunctions, Ntohl) { | 468 TEST(SocketUtilityFunctions, Ntohl) { |
407 uint8_t network_bytes[4] = { 0x44, 0x33, 0x22, 0x11 }; | 469 uint8_t network_bytes[4] = { 0x44, 0x33, 0x22, 0x11 }; |
408 uint32_t network_long; | 470 uint32_t network_long; |
409 memcpy(&network_long, network_bytes, 4); | 471 memcpy(&network_long, network_bytes, 4); |
410 uint32_t host_long = ntohl(network_long); | 472 uint32_t host_long = ntohl(network_long); |
411 EXPECT_EQ(host_long, 0x44332211); | 473 EXPECT_EQ(host_long, 0x44332211); |
412 } | 474 } |
413 | 475 |
414 #endif // !defined(__GLIBC__) | 476 #endif // !defined(__GLIBC__) |
415 #endif // PROVIDES_SOCKETPAIR_API | 477 #endif // PROVIDES_SOCKETPAIR_API |
OLD | NEW |