Merge "Test that ip6-localhost exists in etc/hosts."
diff --git a/tests/netdb_test.cpp b/tests/netdb_test.cpp
index 5660bbd..a35f703 100644
--- a/tests/netdb_test.cpp
+++ b/tests/netdb_test.cpp
@@ -19,6 +19,7 @@
#include <gtest/gtest.h>
#include <arpa/inet.h>
+#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -79,16 +80,34 @@
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<socklen_t>(sizeof(sockaddr_in6)));
+ ASSERT_TRUE(ai->ai_addr != NULL);
+ sockaddr_in6 *addr = reinterpret_cast<sockaddr_in6*>(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 @@
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<sockaddr*>(&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<sockaddr*>(&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, 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 @@
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, 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);
}