• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #include <netdb.h>
18 
19 #include <gtest/gtest.h>
20 
21 #include <arpa/inet.h>
22 #include <netinet/in.h>
23 #include <string.h>
24 #include <sys/cdefs.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
27 
28 // https://code.google.com/p/android/issues/detail?id=13228
TEST(netdb,freeaddrinfo_NULL)29 TEST(netdb, freeaddrinfo_NULL) {
30   freeaddrinfo(nullptr);
31 }
32 
TEST(netdb,getaddrinfo_NULL_host)33 TEST(netdb, getaddrinfo_NULL_host) {
34   // It's okay for the host argument to be NULL, as long as service isn't.
35   addrinfo* ai = nullptr;
36   ASSERT_EQ(0, getaddrinfo(nullptr, "smtp", nullptr, &ai));
37   // (sockaddr_in::sin_port and sockaddr_in6::sin6_port overlap.)
38   ASSERT_EQ(25U, ntohs(reinterpret_cast<sockaddr_in*>(ai->ai_addr)->sin_port));
39   freeaddrinfo(ai);
40 }
41 
TEST(netdb,getaddrinfo_NULL_service)42 TEST(netdb, getaddrinfo_NULL_service) {
43   // It's okay for the service argument to be NULL, as long as host isn't.
44   addrinfo* ai = nullptr;
45   ASSERT_EQ(0, getaddrinfo("localhost", nullptr, nullptr, &ai));
46   ASSERT_TRUE(ai != nullptr);
47   freeaddrinfo(ai);
48 }
49 
TEST(netdb,getaddrinfo_NULL_hints)50 TEST(netdb, getaddrinfo_NULL_hints) {
51   addrinfo* ai = nullptr;
52   ASSERT_EQ(0, getaddrinfo("localhost", "9999", nullptr, &ai));
53 
54   bool saw_tcp = false;
55   bool saw_udp = false;
56   for (addrinfo* p = ai; p != nullptr; p = p->ai_next) {
57     ASSERT_TRUE(p->ai_family == AF_INET || p->ai_family == AF_INET6);
58     if (p->ai_socktype == SOCK_STREAM) {
59       ASSERT_EQ(IPPROTO_TCP, p->ai_protocol);
60       saw_tcp = true;
61     } else if (p->ai_socktype == SOCK_DGRAM) {
62       ASSERT_EQ(IPPROTO_UDP, p->ai_protocol);
63       saw_udp = true;
64     }
65   }
66   ASSERT_TRUE(saw_tcp);
67   ASSERT_TRUE(saw_udp);
68 
69   freeaddrinfo(ai);
70 }
71 
TEST(netdb,getaddrinfo_service_lookup)72 TEST(netdb, getaddrinfo_service_lookup) {
73   addrinfo* ai = nullptr;
74   ASSERT_EQ(0, getaddrinfo("localhost", "smtp", nullptr, &ai));
75   ASSERT_EQ(SOCK_STREAM, ai->ai_socktype);
76   ASSERT_EQ(IPPROTO_TCP, ai->ai_protocol);
77   ASSERT_EQ(25, ntohs(reinterpret_cast<sockaddr_in*>(ai->ai_addr)->sin_port));
78   freeaddrinfo(ai);
79 }
80 
TEST(netdb,getaddrinfo_hints)81 TEST(netdb, getaddrinfo_hints) {
82   addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_STREAM, .ai_protocol = IPPROTO_TCP};
83 
84   addrinfo* ai = nullptr;
85   ASSERT_EQ(0, getaddrinfo( "localhost", "9999", &hints, &ai));
86   ASSERT_TRUE(ai != nullptr);
87   // In glibc, getaddrinfo() converts ::1 to 127.0.0.1 for localhost,
88   // so one or two addrinfo may be returned.
89   addrinfo* tai = ai;
90   while (tai != nullptr) {
91     ASSERT_EQ(AF_INET, tai->ai_family);
92     ASSERT_EQ(SOCK_STREAM, tai->ai_socktype);
93     ASSERT_EQ(IPPROTO_TCP, tai->ai_protocol);
94     tai = tai->ai_next;
95   }
96   freeaddrinfo(ai);
97 }
98 
TEST(netdb,getaddrinfo_ip6_localhost)99 TEST(netdb, getaddrinfo_ip6_localhost) {
100   addrinfo* ai = nullptr;
101   ASSERT_EQ(0, getaddrinfo("ip6-localhost", nullptr, nullptr, &ai));
102   ASSERT_TRUE(ai != nullptr);
103   ASSERT_GE(ai->ai_addrlen, static_cast<socklen_t>(sizeof(sockaddr_in6)));
104   ASSERT_TRUE(ai->ai_addr != nullptr);
105   sockaddr_in6 *addr = reinterpret_cast<sockaddr_in6*>(ai->ai_addr);
106   ASSERT_EQ(addr->sin6_family, AF_INET6);
107   ASSERT_EQ(0, memcmp(&addr->sin6_addr, &in6addr_loopback, sizeof(in6_addr)));
108   freeaddrinfo(ai);
109 }
110 
TEST(netdb,getnameinfo_salen)111 TEST(netdb, getnameinfo_salen) {
112   sockaddr_storage ss = {};
113   sockaddr* sa = reinterpret_cast<sockaddr*>(&ss);
114   char tmp[16];
115 
116   ss.ss_family = AF_INET;
117   socklen_t too_much = sizeof(ss);
118   socklen_t just_right = sizeof(sockaddr_in);
119   socklen_t too_little = sizeof(sockaddr_in) - 1;
120 
121   ASSERT_EQ(0, getnameinfo(sa, too_much, tmp, sizeof(tmp), nullptr, 0, NI_NUMERICHOST));
122   ASSERT_STREQ("0.0.0.0", tmp);
123   ASSERT_EQ(0, getnameinfo(sa, just_right, tmp, sizeof(tmp), nullptr, 0, NI_NUMERICHOST));
124   ASSERT_STREQ("0.0.0.0", tmp);
125   ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), nullptr, 0, NI_NUMERICHOST));
126 
127   ss.ss_family = AF_INET6;
128   just_right = sizeof(sockaddr_in6);
129   too_little = sizeof(sockaddr_in6) - 1;
130   too_much = just_right + 1;
131 
132   ASSERT_EQ(0, getnameinfo(sa, too_much, tmp, sizeof(tmp), nullptr, 0, NI_NUMERICHOST));
133   ASSERT_STREQ("::", tmp);
134   ASSERT_EQ(0, getnameinfo(sa, just_right, tmp, sizeof(tmp), nullptr, 0, NI_NUMERICHOST));
135   ASSERT_STREQ("::", tmp);
136   ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), nullptr, 0, NI_NUMERICHOST));
137 }
138 
TEST(netdb,getnameinfo_localhost)139 TEST(netdb, getnameinfo_localhost) {
140   char host[NI_MAXHOST];
141   sockaddr_in addr = {.sin_family = AF_INET, .sin_addr.s_addr = htonl(0x7f000001)};
142   ASSERT_EQ(0, getnameinfo(reinterpret_cast<sockaddr*>(&addr), sizeof(addr),
143                            host, sizeof(host), nullptr, 0, 0));
144   ASSERT_STREQ(host, "localhost");
145 }
146 
VerifyLocalhostName(const char * name)147 static void VerifyLocalhostName(const char* name) {
148   // Test possible localhost name and aliases, which depend on /etc/hosts or /system/etc/hosts.
149   ASSERT_TRUE(strcmp(name, "localhost") == 0 ||
150               strcmp(name, "ip6-localhost") == 0 ||
151               strcmp(name, "ip6-loopback") == 0) << name;
152 }
153 
TEST(netdb,getnameinfo_ip6_localhost)154 TEST(netdb, getnameinfo_ip6_localhost) {
155   char host[NI_MAXHOST];
156   sockaddr_in6 addr = {.sin6_family = AF_INET6, .sin6_addr = in6addr_loopback};
157   ASSERT_EQ(0, getnameinfo(reinterpret_cast<sockaddr*>(&addr), sizeof(addr),
158                            host, sizeof(host), nullptr, 0, 0));
159   VerifyLocalhostName(host);
160 }
161 
VerifyLocalhost(hostent * hent)162 static void VerifyLocalhost(hostent *hent) {
163   ASSERT_TRUE(hent != nullptr);
164   VerifyLocalhostName(hent->h_name);
165   for (size_t i = 0; hent->h_aliases[i] != nullptr; ++i) {
166     VerifyLocalhostName(hent->h_aliases[i]);
167   }
168   ASSERT_EQ(hent->h_addrtype, AF_INET);
169   ASSERT_EQ(hent->h_addr[0], 127);
170   ASSERT_EQ(hent->h_addr[1], 0);
171   ASSERT_EQ(hent->h_addr[2], 0);
172   ASSERT_EQ(hent->h_addr[3], 1);
173 }
174 
TEST(netdb,gethostbyname)175 TEST(netdb, gethostbyname) {
176   hostent* hp = gethostbyname("localhost");
177   VerifyLocalhost(hp);
178 }
179 
TEST(netdb,gethostbyname2)180 TEST(netdb, gethostbyname2) {
181   hostent* hp = gethostbyname2("localhost", AF_INET);
182   VerifyLocalhost(hp);
183 }
184 
TEST(netdb,gethostbyname_r)185 TEST(netdb, gethostbyname_r) {
186   hostent hent;
187   hostent *hp;
188   char buf[512];
189   int err;
190   int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err);
191   ASSERT_EQ(0, result);
192   VerifyLocalhost(hp);
193 
194   // Change hp->h_addr to test reentrancy.
195   hp->h_addr[0] = 0;
196 
197   hostent hent2;
198   hostent *hp2;
199   char buf2[512];
200   result = gethostbyname_r("localhost", &hent2, buf2, sizeof(buf2), &hp2, &err);
201   ASSERT_EQ(0, result);
202   VerifyLocalhost(hp2);
203 
204   ASSERT_EQ(0, hp->h_addr[0]);
205 }
206 
TEST(netdb,gethostbyname2_r)207 TEST(netdb, gethostbyname2_r) {
208   hostent hent;
209   hostent *hp;
210   char buf[512];
211   int err;
212   int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
213   ASSERT_EQ(0, result);
214   VerifyLocalhost(hp);
215 
216   // Change hp->h_addr to test reentrancy.
217   hp->h_addr[0] = 0;
218 
219   hostent hent2;
220   hostent *hp2;
221   char buf2[512];
222   result = gethostbyname2_r("localhost", AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err);
223   ASSERT_EQ(0, result);
224   VerifyLocalhost(hp2);
225 
226   ASSERT_EQ(0, hp->h_addr[0]);
227 }
228 
TEST(netdb,gethostbyaddr)229 TEST(netdb, gethostbyaddr) {
230   in_addr addr = { htonl(0x7f000001) };
231   hostent *hp = gethostbyaddr(&addr, sizeof(addr), AF_INET);
232   VerifyLocalhost(hp);
233 }
234 
TEST(netdb,gethostbyaddr_r)235 TEST(netdb, gethostbyaddr_r) {
236   in_addr addr = { htonl(0x7f000001) };
237   hostent hent;
238   hostent *hp;
239   char buf[512];
240   int err;
241   int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
242   ASSERT_EQ(0, result);
243   VerifyLocalhost(hp);
244 
245   // Change hp->h_addr to test reentrancy.
246   hp->h_addr[0] = 0;
247 
248   hostent hent2;
249   hostent *hp2;
250   char buf2[512];
251   result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err);
252   ASSERT_EQ(0, result);
253   VerifyLocalhost(hp2);
254 
255   ASSERT_EQ(0, hp->h_addr[0]);
256 }
257 
258 #if defined(ANDROID_HOST_MUSL)
259 // musl doesn't define NETDB_INTERNAL.  It also never sets *err to -1, but
260 // since gethostbyname_r is a glibc extension, the difference in behavior
261 // between musl and  glibc should probably be considered a bug in musl.
262 #define NETDB_INTERNAL -1
263 #endif
264 
TEST(netdb,gethostbyname_r_ERANGE)265 TEST(netdb, gethostbyname_r_ERANGE) {
266   hostent hent;
267   hostent *hp;
268   char buf[4]; // Use too small buffer.
269   int err = 0;
270   int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err);
271   EXPECT_EQ(NETDB_INTERNAL, err);
272   EXPECT_EQ(ERANGE, result);
273   EXPECT_EQ(nullptr, hp);
274 }
275 
TEST(netdb,gethostbyname2_r_ERANGE)276 TEST(netdb, gethostbyname2_r_ERANGE) {
277   hostent hent;
278   hostent *hp;
279   char buf[4]; // Use too small buffer.
280   int err = 0;
281   int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
282   EXPECT_EQ(NETDB_INTERNAL, err);
283   EXPECT_EQ(ERANGE, result);
284   EXPECT_EQ(nullptr, hp);
285 }
286 
TEST(netdb,gethostbyaddr_r_ERANGE)287 TEST(netdb, gethostbyaddr_r_ERANGE) {
288   in_addr addr = { htonl(0x7f000001) };
289   hostent hent;
290   hostent *hp;
291   char buf[4]; // Use too small buffer.
292   int err = 0;
293   int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
294   EXPECT_EQ(NETDB_INTERNAL, err);
295   EXPECT_EQ(ERANGE, result);
296   EXPECT_EQ(nullptr, hp);
297 }
298 
TEST(netdb,gethostbyname_r_HOST_NOT_FOUND)299 TEST(netdb, gethostbyname_r_HOST_NOT_FOUND) {
300   hostent hent;
301   hostent *hp;
302   char buf[BUFSIZ];
303   int err;
304   int result = gethostbyname_r("does.not.exist.google.com", &hent, buf, sizeof(buf), &hp, &err);
305   EXPECT_EQ(HOST_NOT_FOUND, err);
306   EXPECT_EQ(0, result);
307   EXPECT_EQ(nullptr, hp);
308 }
309 
TEST(netdb,gethostbyname2_r_HOST_NOT_FOUND)310 TEST(netdb, gethostbyname2_r_HOST_NOT_FOUND) {
311   hostent hent;
312   hostent *hp;
313   char buf[BUFSIZ];
314   int err;
315   int result = gethostbyname2_r("does.not.exist.google.com", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
316   EXPECT_EQ(HOST_NOT_FOUND, err);
317   EXPECT_EQ(0, result);
318   EXPECT_EQ(nullptr, hp);
319 }
320 
TEST(netdb,gethostbyaddr_r_HOST_NOT_FOUND)321 TEST(netdb, gethostbyaddr_r_HOST_NOT_FOUND) {
322   in_addr addr = { htonl(0xffffffff) };
323   hostent hent;
324   hostent *hp;
325   char buf[BUFSIZ];
326   int err;
327   int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
328   EXPECT_EQ(HOST_NOT_FOUND, err);
329   EXPECT_EQ(0, result);
330   EXPECT_EQ(nullptr, hp);
331 }
332 
TEST(netdb,getservbyname)333 TEST(netdb, getservbyname) {
334   // smtp is TCP-only, so we know we'll get 25/tcp back.
335   servent* s = getservbyname("smtp", nullptr);
336   ASSERT_TRUE(s != nullptr);
337   ASSERT_STREQ("smtp", s->s_name);
338   ASSERT_EQ(25, ntohs(s->s_port));
339   ASSERT_STREQ("tcp", s->s_proto);
340 
341   // We get the same result by explicitly asking for tcp.
342   s = getservbyname("smtp", "tcp");
343   ASSERT_TRUE(s != nullptr);
344   ASSERT_STREQ("smtp", s->s_name);
345   ASSERT_EQ(25, ntohs(s->s_port));
346   ASSERT_STREQ("tcp", s->s_proto);
347 
348   // And we get a failure if we explicitly ask for udp.
349   s = getservbyname("smtp", "udp");
350   ASSERT_TRUE(s == nullptr);
351 
352   // But there are actually udp services.
353   s = getservbyname("echo", "udp");
354   ASSERT_TRUE(s != nullptr);
355   ASSERT_STREQ("echo", s->s_name);
356   ASSERT_EQ(7, ntohs(s->s_port));
357   ASSERT_STREQ("udp", s->s_proto);
358 }
359 
TEST(netdb,getservbyport)360 TEST(netdb, getservbyport) {
361   // smtp is TCP-only, so we know we'll get 25/tcp back.
362   servent* s = getservbyport(htons(25), nullptr);
363   ASSERT_TRUE(s != nullptr);
364   ASSERT_STREQ("smtp", s->s_name);
365   ASSERT_EQ(25, ntohs(s->s_port));
366   ASSERT_STREQ("tcp", s->s_proto);
367 
368   // We get the same result by explicitly asking for tcp.
369   s = getservbyport(htons(25), "tcp");
370   ASSERT_TRUE(s != nullptr);
371   ASSERT_STREQ("smtp", s->s_name);
372   ASSERT_EQ(25, ntohs(s->s_port));
373   ASSERT_STREQ("tcp", s->s_proto);
374 
375   // And we get a failure if we explicitly ask for udp.
376   s = getservbyport(htons(25), "udp");
377   ASSERT_TRUE(s == nullptr);
378 
379   // But there are actually udp services.
380   s = getservbyport(htons(7), "udp");
381   ASSERT_TRUE(s != nullptr);
382   ASSERT_STREQ("echo", s->s_name);
383   ASSERT_EQ(7, ntohs(s->s_port));
384   ASSERT_STREQ("udp", s->s_proto);
385 }
386 
TEST(netdb,endnetent_getnetent_setnetent)387 TEST(netdb, endnetent_getnetent_setnetent) {
388   setnetent(0);
389   setnetent(1);
390   endnetent();
391   while (getnetent() != nullptr) {
392   }
393 }
394 
TEST(netdb,getnetbyaddr)395 TEST(netdb, getnetbyaddr) {
396   getnetbyaddr(0, 0);
397 }
398 
TEST(netdb,getnetbyname)399 TEST(netdb, getnetbyname) {
400   getnetbyname("x");
401 }
402 
TEST(netdb,endprotoent_getprotoent_setprotoent)403 TEST(netdb, endprotoent_getprotoent_setprotoent) {
404   setprotoent(0);
405   setprotoent(1);
406   endprotoent();
407   while (getprotoent() != nullptr) {
408   }
409 }
410 
TEST(netdb,getprotobyname)411 TEST(netdb, getprotobyname) {
412   getprotobyname("tcp");
413 }
414 
TEST(netdb,getprotobynumber)415 TEST(netdb, getprotobynumber) {
416   getprotobynumber(6);
417 }
418 
TEST(netdb,endservent_getservent_setservent)419 TEST(netdb, endservent_getservent_setservent) {
420   setservent(0);
421   setservent(1);
422   endservent();
423   size_t service_count = 0;
424   while (getservent() != nullptr) {
425     ++service_count;
426   }
427   ASSERT_GT(service_count, 0U);
428 }
429 
TEST(netdb,getservbyname_getservent_conflicts)430 TEST(netdb, getservbyname_getservent_conflicts) {
431   // Calling getservbyname shouldn't affect getservent's iteration order.
432   endservent();
433   while (getservent() != nullptr) {
434     ASSERT_TRUE(getservbyname("smtp", "tcp") != nullptr);
435   }
436 }
437 
TEST(netdb,getservbyport_getservent_conflicts)438 TEST(netdb, getservbyport_getservent_conflicts) {
439   // Calling getservbyport shouldn't affect getservent's iteration order.
440   endservent();
441   while (getservent() != nullptr) {
442     ASSERT_TRUE(getservbyport(htons(25), "tcp") != nullptr);
443   }
444 }
445 
TEST(netdb,endservent_resets)446 TEST(netdb, endservent_resets) {
447   endservent();
448   std::string first_service(getservent()->s_name);
449   endservent();
450   ASSERT_EQ(first_service, std::string(getservent()->s_name));
451 }
452 
TEST(netdb,setservent_resets)453 TEST(netdb, setservent_resets) {
454   endservent();
455   std::string first_service(getservent()->s_name);
456   setservent(0);
457   ASSERT_EQ(first_service, std::string(getservent()->s_name));
458 }
459 
TEST(netdb,endhostent_gethostent_sethostent)460 TEST(netdb, endhostent_gethostent_sethostent) {
461   sethostent(0);
462   sethostent(1);
463   endhostent();
464   size_t host_count = 0;
465   while (gethostent() != nullptr) {
466     ++host_count;
467   }
468   ASSERT_GT(host_count, 0U);
469 }
470 
TEST(netdb,endhostent_resets)471 TEST(netdb, endhostent_resets) {
472   endhostent();
473   std::string first_host(gethostent()->h_name);
474   endhostent();
475   ASSERT_EQ(first_host, std::string(gethostent()->h_name));
476 }
477 
TEST(netdb,sethostent_resets)478 TEST(netdb, sethostent_resets) {
479   endhostent();
480   std::string first_host(gethostent()->h_name);
481   sethostent(0);
482   ASSERT_EQ(first_host, std::string(gethostent()->h_name));
483 }
484