2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <gtest/gtest.h>
21 #include <arpa/inet.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
27 // https://code.google.com/p/android/issues/detail?id=13228
28 TEST(netdb, freeaddrinfo_NULL) {
32 TEST(netdb, getaddrinfo_NULL_host) {
33 // It's okay for the host argument to be NULL, as long as service isn't.
35 ASSERT_EQ(0, getaddrinfo(NULL, "smtp", NULL, &ai));
36 // (sockaddr_in::sin_port and sockaddr_in6::sin6_port overlap.)
37 ASSERT_EQ(25U, ntohs(reinterpret_cast<sockaddr_in*>(ai->ai_addr)->sin_port));
41 TEST(netdb, getaddrinfo_NULL_service) {
42 // It's okay for the service argument to be NULL, as long as host isn't.
44 ASSERT_EQ(0, getaddrinfo("localhost", NULL, NULL, &ai));
45 ASSERT_TRUE(ai != NULL);
49 TEST(netdb, getaddrinfo_NULL_hints) {
51 ASSERT_EQ(0, getaddrinfo("localhost", "9999", NULL, &ai));
55 for (addrinfo* p = ai; p != NULL; p = p->ai_next) {
56 ASSERT_TRUE(p->ai_family == AF_INET || p->ai_family == AF_INET6);
57 if (p->ai_socktype == SOCK_STREAM) {
58 ASSERT_EQ(IPPROTO_TCP, p->ai_protocol);
60 } else if (p->ai_socktype == SOCK_DGRAM) {
61 ASSERT_EQ(IPPROTO_UDP, p->ai_protocol);
71 TEST(netdb, getaddrinfo_service_lookup) {
73 ASSERT_EQ(0, getaddrinfo("localhost", "smtp", NULL, &ai));
74 ASSERT_EQ(SOCK_STREAM, ai->ai_socktype);
75 ASSERT_EQ(IPPROTO_TCP, ai->ai_protocol);
76 ASSERT_EQ(25, ntohs(reinterpret_cast<sockaddr_in*>(ai->ai_addr)->sin_port));
80 TEST(netdb, getaddrinfo_hints) {
82 memset(&hints, 0, sizeof(hints));
83 hints.ai_family = AF_INET;
84 hints.ai_socktype = SOCK_STREAM;
85 hints.ai_protocol = IPPROTO_TCP;
88 ASSERT_EQ(0, getaddrinfo( "localhost", "9999", &hints, &ai));
89 ASSERT_TRUE(ai != NULL);
90 // In glibc, getaddrinfo() converts ::1 to 127.0.0.1 for localhost,
91 // so one or two addrinfo may be returned.
94 ASSERT_EQ(AF_INET, tai->ai_family);
95 ASSERT_EQ(SOCK_STREAM, tai->ai_socktype);
96 ASSERT_EQ(IPPROTO_TCP, tai->ai_protocol);
102 TEST(netdb, getaddrinfo_ip6_localhost) {
104 ASSERT_EQ(0, getaddrinfo("ip6-localhost", NULL, NULL, &ai));
105 ASSERT_TRUE(ai != NULL);
106 ASSERT_GE(ai->ai_addrlen, static_cast<socklen_t>(sizeof(sockaddr_in6)));
107 ASSERT_TRUE(ai->ai_addr != NULL);
108 sockaddr_in6 *addr = reinterpret_cast<sockaddr_in6*>(ai->ai_addr);
109 ASSERT_EQ(addr->sin6_family, AF_INET6);
110 ASSERT_EQ(0, memcmp(&addr->sin6_addr, &in6addr_loopback, sizeof(in6_addr)));
114 TEST(netdb, getnameinfo_salen) {
116 memset(&ss, 0, sizeof(ss));
117 sockaddr* sa = reinterpret_cast<sockaddr*>(&ss);
120 ss.ss_family = AF_INET;
121 socklen_t too_much = sizeof(ss);
122 socklen_t just_right = sizeof(sockaddr_in);
123 socklen_t too_little = sizeof(sockaddr_in) - 1;
125 ASSERT_EQ(0, getnameinfo(sa, too_much, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
126 ASSERT_STREQ("0.0.0.0", tmp);
127 ASSERT_EQ(0, getnameinfo(sa, just_right, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
128 ASSERT_STREQ("0.0.0.0", tmp);
129 ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
131 ss.ss_family = AF_INET6;
132 just_right = sizeof(sockaddr_in6);
133 too_little = sizeof(sockaddr_in6) - 1;
134 too_much = just_right + 1;
136 ASSERT_EQ(0, getnameinfo(sa, too_much, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
137 ASSERT_STREQ("::", tmp);
138 ASSERT_EQ(0, getnameinfo(sa, just_right, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
139 ASSERT_STREQ("::", tmp);
140 ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
143 TEST(netdb, getnameinfo_localhost) {
145 char host[NI_MAXHOST];
146 memset(&addr, 0, sizeof(sockaddr_in));
147 addr.sin_family = AF_INET;
148 addr.sin_addr.s_addr = htonl(0x7f000001);
149 ASSERT_EQ(0, getnameinfo(reinterpret_cast<sockaddr*>(&addr), sizeof(addr),
150 host, sizeof(host), NULL, 0, 0));
151 ASSERT_STREQ(host, "localhost");
154 static void VerifyLocalhostName(const char* name) {
155 // Test possible localhost name and aliases, which depend on /etc/hosts or /system/etc/hosts.
156 ASSERT_TRUE(strcmp(name, "localhost") == 0 ||
157 strcmp(name, "ip6-localhost") == 0 ||
158 strcmp(name, "ip6-loopback") == 0) << name;
161 TEST(netdb, getnameinfo_ip6_localhost) {
163 char host[NI_MAXHOST];
164 memset(&addr, 0, sizeof(sockaddr_in6));
165 addr.sin6_family = AF_INET6;
166 addr.sin6_addr = in6addr_loopback;
167 ASSERT_EQ(0, getnameinfo(reinterpret_cast<sockaddr*>(&addr), sizeof(addr),
168 host, sizeof(host), NULL, 0, 0));
169 VerifyLocalhostName(host);
172 static void VerifyLocalhost(hostent *hent) {
173 ASSERT_TRUE(hent != NULL);
174 VerifyLocalhostName(hent->h_name);
175 for (size_t i = 0; hent->h_aliases[i] != NULL; ++i) {
176 VerifyLocalhostName(hent->h_aliases[i]);
178 ASSERT_EQ(hent->h_addrtype, AF_INET);
179 ASSERT_EQ(hent->h_addr[0], 127);
180 ASSERT_EQ(hent->h_addr[1], 0);
181 ASSERT_EQ(hent->h_addr[2], 0);
182 ASSERT_EQ(hent->h_addr[3], 1);
185 TEST(netdb, gethostbyname) {
186 hostent* hp = gethostbyname("localhost");
190 TEST(netdb, gethostbyname2) {
191 hostent* hp = gethostbyname2("localhost", AF_INET);
195 TEST(netdb, gethostbyname_r) {
200 int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err);
201 ASSERT_EQ(0, result);
204 // Change hp->h_addr to test reentrancy.
210 result = gethostbyname_r("localhost", &hent2, buf2, sizeof(buf2), &hp2, &err);
211 ASSERT_EQ(0, result);
212 VerifyLocalhost(hp2);
214 ASSERT_EQ(0, hp->h_addr[0]);
217 TEST(netdb, gethostbyname2_r) {
222 int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
223 ASSERT_EQ(0, result);
226 // Change hp->h_addr to test reentrancy.
232 result = gethostbyname2_r("localhost", AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err);
233 ASSERT_EQ(0, result);
234 VerifyLocalhost(hp2);
236 ASSERT_EQ(0, hp->h_addr[0]);
239 TEST(netdb, gethostbyaddr) {
240 in_addr addr = { htonl(0x7f000001) };
241 hostent *hp = gethostbyaddr(&addr, sizeof(addr), AF_INET);
245 TEST(netdb, gethostbyaddr_r) {
246 in_addr addr = { htonl(0x7f000001) };
251 int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
252 ASSERT_EQ(0, result);
255 // Change hp->h_addr to test reentrancy.
261 result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err);
262 ASSERT_EQ(0, result);
263 VerifyLocalhost(hp2);
265 ASSERT_EQ(0, hp->h_addr[0]);
268 TEST(netdb, gethostbyname_r_ERANGE) {
271 char buf[4]; // Use too small buffer.
273 int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err);
274 ASSERT_EQ(ERANGE, result);
278 TEST(netdb, gethostbyname2_r_ERANGE) {
281 char buf[4]; // Use too small buffer.
283 int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
284 ASSERT_EQ(ERANGE, result);
288 TEST(netdb, gethostbyaddr_r_ERANGE) {
289 in_addr addr = { htonl(0x7f000001) };
292 char buf[4]; // Use too small buffer.
294 int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
295 ASSERT_EQ(ERANGE, result);
299 TEST(netdb, getservbyname) {
300 // smtp is TCP-only, so we know we'll get 25/tcp back.
301 servent* s = getservbyname("smtp", NULL);
302 ASSERT_TRUE(s != NULL);
303 ASSERT_EQ(25, ntohs(s->s_port));
304 ASSERT_STREQ("tcp", s->s_proto);
306 // We get the same result by explicitly asking for tcp.
307 s = getservbyname("smtp", "tcp");
308 ASSERT_TRUE(s != NULL);
309 ASSERT_EQ(25, ntohs(s->s_port));
310 ASSERT_STREQ("tcp", s->s_proto);
312 // And we get a failure if we explicitly ask for udp.
313 s = getservbyname("smtp", "udp");
314 ASSERT_TRUE(s == NULL);
316 // But there are actually udp services.
317 s = getservbyname("echo", "udp");
318 ASSERT_TRUE(s != NULL);
319 ASSERT_EQ(7, ntohs(s->s_port));
320 ASSERT_STREQ("udp", s->s_proto);