diff options
author | Yabin Cui <yabinc@google.com> | 2014-12-16 17:03:44 -0800 |
---|---|---|
committer | Yabin Cui <yabinc@google.com> | 2014-12-17 16:19:27 -0800 |
commit | 58d33a51f336d6823ef1ec915949a5884699ff5f (patch) | |
tree | 6a57ae4b11a8338089aa56a7879a78be936a17ee /tests/netdb_test.cpp | |
parent | 16ea2790a84b1e65dc1e485c3ec494de787cc490 (diff) |
Sync with upstream for gethnamaddr.c.
Bug: 18566967
Change-Id: I37e7410226b49eec67614e20b2c1d5e3e47817a5
Diffstat (limited to 'tests/netdb_test.cpp')
-rw-r--r-- | tests/netdb_test.cpp | 126 |
1 files changed, 123 insertions, 3 deletions
diff --git a/tests/netdb_test.cpp b/tests/netdb_test.cpp index 0cebe4e8e..ab5b48755 100644 --- a/tests/netdb_test.cpp +++ b/tests/netdb_test.cpp @@ -14,11 +14,13 @@ * limitations under the License. */ +#include <netdb.h> + #include <gtest/gtest.h> +#include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> -#include <netdb.h> #include <netinet/in.h> TEST(netdb, getaddrinfo_NULL_host) { @@ -114,8 +116,7 @@ TEST(netdb, getnameinfo_salen) { ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST)); } -TEST(netdb, gethostbyname) { - hostent* hent = gethostbyname("localhost"); +void VerifyLocalhost(hostent *hent) { ASSERT_TRUE(hent != NULL); ASSERT_EQ(hent->h_addrtype, AF_INET); ASSERT_EQ(hent->h_addr[0], 127); @@ -124,6 +125,125 @@ TEST(netdb, gethostbyname) { ASSERT_EQ(hent->h_addr[3], 1); } +TEST(netdb, gethostbyname) { + hostent* hp = gethostbyname("localhost"); + VerifyLocalhost(hp); +} + +TEST(netdb, gethostbyname2) { + hostent* hp = gethostbyname2("localhost", AF_INET); + VerifyLocalhost(hp); +} + +TEST(netdb, gethostbyname_r) { + hostent hent; + hostent *hp; + char buf[512]; + int err; + int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err); + ASSERT_EQ(0, result); + VerifyLocalhost(hp); + + // Change hp->h_addr to test reentrancy. + hp->h_addr[0] = 0; + + hostent hent2; + hostent *hp2; + char buf2[512]; + result = gethostbyname_r("localhost", &hent2, buf2, sizeof(buf2), &hp2, &err); + ASSERT_EQ(0, result); + VerifyLocalhost(hp2); + + ASSERT_EQ(0, hp->h_addr[0]); +} + +TEST(netdb, gethostbyname2_r) { + hostent hent; + hostent *hp; + char buf[512]; + int err; + int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err); + ASSERT_EQ(0, result); + VerifyLocalhost(hp); + + // Change hp->h_addr to test reentrancy. + hp->h_addr[0] = 0; + + hostent hent2; + hostent *hp2; + char buf2[512]; + result = gethostbyname2_r("localhost", AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err); + ASSERT_EQ(0, result); + VerifyLocalhost(hp2); + + ASSERT_EQ(0, hp->h_addr[0]); +} + +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); + VerifyLocalhost(hp); +} + +TEST(netdb, gethostbyaddr_r) { + char addr[4]; + ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", addr)); + + hostent hent; + hostent *hp; + char buf[512]; + int err; + int result = gethostbyaddr_r(addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err); + ASSERT_EQ(0, result); + VerifyLocalhost(hp); + + // Change hp->h_addr to test reentrancy. + hp->h_addr[0] = 0; + + hostent hent2; + hostent *hp2; + char buf2[512]; + result = gethostbyaddr_r(addr, sizeof(addr), AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err); + ASSERT_EQ(0, result); + VerifyLocalhost(hp2); + + ASSERT_EQ(0, hp->h_addr[0]); +} + +TEST(netdb, gethostbyname_r_ERANGE) { + hostent hent; + hostent *hp; + char buf[4]; // Use too small buffer. + int err; + int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err); + ASSERT_EQ(ERANGE, result); + ASSERT_EQ(NULL, hp); +} + +TEST(netdb, gethostbyname2_r_ERANGE) { + hostent hent; + hostent *hp; + char buf[4]; // Use too small buffer. + int err; + int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err); + ASSERT_EQ(ERANGE, result); + ASSERT_EQ(NULL, hp); +} + +TEST(netdb, gethostbyaddr_r_ERANGE) { + char addr[4]; + ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", addr)); + + 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); + ASSERT_EQ(ERANGE, result); + ASSERT_EQ(NULL, hp); +} + TEST(netdb, getservbyname) { // smtp is TCP-only, so we know we'll get 25/tcp back. servent* s = getservbyname("smtp", NULL); |