From a35cd8cdc2d43c8352011c5177af09adc2225c4e Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Tue, 13 Jan 2015 14:35:15 -0800 Subject: [PATCH] Test that ip6-localhost exists in etc/hosts. Addition change: make netdb_test pass on host. Bug: 18791191 Change-Id: I7a9e29aa559ff6557288b47323d8a436379201a2 --- tests/netdb_test.cpp | 83 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 18 deletions(-) diff --git a/tests/netdb_test.cpp b/tests/netdb_test.cpp index 5660bbdaa..a35f703fc 100644 --- a/tests/netdb_test.cpp +++ b/tests/netdb_test.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -79,16 +80,34 @@ TEST(netdb, getaddrinfo_service_lookup) { TEST(netdb, getaddrinfo_hints) { addrinfo hints; memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; + hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; addrinfo* ai = NULL; ASSERT_EQ(0, getaddrinfo( "localhost", "9999", &hints, &ai)); - ASSERT_EQ(AF_INET, ai->ai_family); - ASSERT_EQ(SOCK_STREAM, ai->ai_socktype); - ASSERT_EQ(IPPROTO_TCP, ai->ai_protocol); - ASSERT_TRUE(ai->ai_next == NULL); + ASSERT_TRUE(ai != NULL); + // In glibc, getaddrinfo() converts ::1 to 127.0.0.1 for localhost, + // so one or two addrinfo may be returned. + addrinfo* tai = ai; + while (tai != NULL) { + ASSERT_EQ(AF_INET, tai->ai_family); + ASSERT_EQ(SOCK_STREAM, tai->ai_socktype); + ASSERT_EQ(IPPROTO_TCP, tai->ai_protocol); + tai = tai->ai_next; + } + freeaddrinfo(ai); +} + +TEST(netdb, getaddrinfo_ip6_localhost) { + addrinfo* ai = NULL; + ASSERT_EQ(0, getaddrinfo("ip6-localhost", NULL, NULL, &ai)); + ASSERT_TRUE(ai != NULL); + ASSERT_GE(ai->ai_addrlen, static_cast(sizeof(sockaddr_in6))); + ASSERT_TRUE(ai->ai_addr != NULL); + sockaddr_in6 *addr = reinterpret_cast(ai->ai_addr); + ASSERT_EQ(addr->sin6_family, AF_INET6); + ASSERT_EQ(0, memcmp(&addr->sin6_addr, &in6addr_loopback, sizeof(in6_addr))); freeaddrinfo(ai); } @@ -121,8 +140,41 @@ TEST(netdb, getnameinfo_salen) { ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST)); } -void VerifyLocalhost(hostent *hent) { +TEST(netdb, getnameinfo_localhost) { + sockaddr_in addr; + char host[NI_MAXHOST]; + memset(&addr, 0, sizeof(sockaddr_in)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(0x7f000001); + ASSERT_EQ(0, getnameinfo(reinterpret_cast(&addr), sizeof(addr), + host, sizeof(host), NULL, 0, 0)); + ASSERT_STREQ(host, "localhost"); +} + +static void VerifyLocalhostName(const char* name) { + // Test possible localhost name and aliases, which depend on /etc/hosts or /system/etc/hosts. + ASSERT_TRUE(strcmp(name, "localhost") == 0 || + strcmp(name, "ip6-localhost") == 0 || + strcmp(name, "ip6-loopback") == 0) << name; +} + +TEST(netdb, getnameinfo_ip6_localhost) { + sockaddr_in6 addr; + char host[NI_MAXHOST]; + memset(&addr, 0, sizeof(sockaddr_in6)); + addr.sin6_family = AF_INET6; + addr.sin6_addr = in6addr_loopback; + ASSERT_EQ(0, getnameinfo(reinterpret_cast(&addr), sizeof(addr), + host, sizeof(host), NULL, 0, 0)); + VerifyLocalhostName(host); +} + +static void VerifyLocalhost(hostent *hent) { ASSERT_TRUE(hent != NULL); + VerifyLocalhostName(hent->h_name); + for (size_t i = 0; hent->h_aliases[i] != NULL; ++i) { + VerifyLocalhostName(hent->h_aliases[i]); + } ASSERT_EQ(hent->h_addrtype, AF_INET); ASSERT_EQ(hent->h_addr[0], 127); ASSERT_EQ(hent->h_addr[1], 0); @@ -185,21 +237,18 @@ TEST(netdb, gethostbyname2_r) { } TEST(netdb, gethostbyaddr) { - char addr[4]; - ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", addr)); - hostent *hp = gethostbyaddr(addr, sizeof(addr), AF_INET); + in_addr addr = { htonl(0x7f000001) }; + hostent *hp = gethostbyaddr(&addr, sizeof(addr), AF_INET); VerifyLocalhost(hp); } TEST(netdb, gethostbyaddr_r) { - char addr[4]; - ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", addr)); - + in_addr addr = { htonl(0x7f000001) }; hostent hent; hostent *hp; char buf[512]; int err; - int result = gethostbyaddr_r(addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err); + int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err); ASSERT_EQ(0, result); VerifyLocalhost(hp); @@ -209,7 +258,7 @@ TEST(netdb, gethostbyaddr_r) { hostent hent2; hostent *hp2; char buf2[512]; - result = gethostbyaddr_r(addr, sizeof(addr), AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err); + result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err); ASSERT_EQ(0, result); VerifyLocalhost(hp2); @@ -237,14 +286,12 @@ TEST(netdb, gethostbyname2_r_ERANGE) { } TEST(netdb, gethostbyaddr_r_ERANGE) { - char addr[4]; - ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", addr)); - + in_addr addr = { htonl(0x7f000001) }; hostent hent; hostent *hp; char buf[4]; // Use too small buffer. int err; - int result = gethostbyaddr_r(addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err); + int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err); ASSERT_EQ(ERANGE, result); ASSERT_EQ(NULL, hp); } -- 2.11.0