blob: f6e8a32d2e22d6053724f313d5c40cc92a01b4fb [file] [log] [blame]
Elliott Hughesd3b9d112013-02-13 08:22:07 -08001/*
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
Yabin Cui58d33a52014-12-16 17:03:44 -080017#include <netdb.h>
18
Elliott Hughesd3b9d112013-02-13 08:22:07 -080019#include <gtest/gtest.h>
20
Yabin Cui58d33a52014-12-16 17:03:44 -080021#include <arpa/inet.h>
Elliott Hughesd8213bb2013-02-13 09:49:33 -080022#include <netinet/in.h>
Colin Cross14d15072021-08-16 16:35:27 -070023#include <string.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070024#include <sys/cdefs.h>
Colin Cross14d15072021-08-16 16:35:27 -070025#include <sys/socket.h>
26#include <sys/types.h>
Elliott Hughesd3b9d112013-02-13 08:22:07 -080027
Elliott Hughesc62a4b52015-01-08 17:28:46 -080028// https://code.google.com/p/android/issues/detail?id=13228
29TEST(netdb, freeaddrinfo_NULL) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070030 freeaddrinfo(nullptr);
Elliott Hughesc62a4b52015-01-08 17:28:46 -080031}
32
Elliott Hughes32fea142014-11-16 10:14:54 -080033TEST(netdb, getaddrinfo_NULL_host) {
34 // It's okay for the host argument to be NULL, as long as service isn't.
Yi Kong32bc0fc2018-08-02 17:31:13 -070035 addrinfo* ai = nullptr;
36 ASSERT_EQ(0, getaddrinfo(nullptr, "smtp", nullptr, &ai));
Elliott Hughes32fea142014-11-16 10:14:54 -080037 // (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
42TEST(netdb, getaddrinfo_NULL_service) {
43 // It's okay for the service argument to be NULL, as long as host isn't.
Yi Kong32bc0fc2018-08-02 17:31:13 -070044 addrinfo* ai = nullptr;
45 ASSERT_EQ(0, getaddrinfo("localhost", nullptr, nullptr, &ai));
46 ASSERT_TRUE(ai != nullptr);
Elliott Hughes32fea142014-11-16 10:14:54 -080047 freeaddrinfo(ai);
48}
49
Elliott Hughesd3b9d112013-02-13 08:22:07 -080050TEST(netdb, getaddrinfo_NULL_hints) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070051 addrinfo* ai = nullptr;
52 ASSERT_EQ(0, getaddrinfo("localhost", "9999", nullptr, &ai));
Derek Xueba811122014-08-13 14:19:17 +010053
54 bool saw_tcp = false;
55 bool saw_udp = false;
Yi Kong32bc0fc2018-08-02 17:31:13 -070056 for (addrinfo* p = ai; p != nullptr; p = p->ai_next) {
Derek Xueba811122014-08-13 14:19:17 +010057 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
72TEST(netdb, getaddrinfo_service_lookup) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070073 addrinfo* ai = nullptr;
74 ASSERT_EQ(0, getaddrinfo("localhost", "smtp", nullptr, &ai));
Derek Xueba811122014-08-13 14:19:17 +010075 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
81TEST(netdb, getaddrinfo_hints) {
Christopher Ferris2a391882024-12-19 13:44:35 -080082 addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_STREAM, .ai_protocol = IPPROTO_TCP};
Derek Xueba811122014-08-13 14:19:17 +010083
Yi Kong32bc0fc2018-08-02 17:31:13 -070084 addrinfo* ai = nullptr;
Derek Xueba811122014-08-13 14:19:17 +010085 ASSERT_EQ(0, getaddrinfo( "localhost", "9999", &hints, &ai));
Yi Kong32bc0fc2018-08-02 17:31:13 -070086 ASSERT_TRUE(ai != nullptr);
Yabin Cuia35cd8c2015-01-13 14:35:15 -080087 // 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;
Yi Kong32bc0fc2018-08-02 17:31:13 -070090 while (tai != nullptr) {
Yabin Cuia35cd8c2015-01-13 14:35:15 -080091 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
99TEST(netdb, getaddrinfo_ip6_localhost) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700100 addrinfo* ai = nullptr;
101 ASSERT_EQ(0, getaddrinfo("ip6-localhost", nullptr, nullptr, &ai));
102 ASSERT_TRUE(ai != nullptr);
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800103 ASSERT_GE(ai->ai_addrlen, static_cast<socklen_t>(sizeof(sockaddr_in6)));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700104 ASSERT_TRUE(ai->ai_addr != nullptr);
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800105 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)));
Elliott Hughesd3b9d112013-02-13 08:22:07 -0800108 freeaddrinfo(ai);
109}
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800110
111TEST(netdb, getnameinfo_salen) {
Christopher Ferris2a391882024-12-19 13:44:35 -0800112 sockaddr_storage ss = {};
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800113 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
Yi Kong32bc0fc2018-08-02 17:31:13 -0700121 ASSERT_EQ(0, getnameinfo(sa, too_much, tmp, sizeof(tmp), nullptr, 0, NI_NUMERICHOST));
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800122 ASSERT_STREQ("0.0.0.0", tmp);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700123 ASSERT_EQ(0, getnameinfo(sa, just_right, tmp, sizeof(tmp), nullptr, 0, NI_NUMERICHOST));
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800124 ASSERT_STREQ("0.0.0.0", tmp);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700125 ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), nullptr, 0, NI_NUMERICHOST));
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800126
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
Yi Kong32bc0fc2018-08-02 17:31:13 -0700132 ASSERT_EQ(0, getnameinfo(sa, too_much, tmp, sizeof(tmp), nullptr, 0, NI_NUMERICHOST));
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800133 ASSERT_STREQ("::", tmp);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700134 ASSERT_EQ(0, getnameinfo(sa, just_right, tmp, sizeof(tmp), nullptr, 0, NI_NUMERICHOST));
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800135 ASSERT_STREQ("::", tmp);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700136 ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), nullptr, 0, NI_NUMERICHOST));
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800137}
Derek Xue4912fc72014-08-13 14:19:17 +0100138
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800139TEST(netdb, getnameinfo_localhost) {
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800140 char host[NI_MAXHOST];
Christopher Ferris2a391882024-12-19 13:44:35 -0800141 sockaddr_in addr = {.sin_family = AF_INET, .sin_addr.s_addr = htonl(0x7f000001)};
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800142 ASSERT_EQ(0, getnameinfo(reinterpret_cast<sockaddr*>(&addr), sizeof(addr),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700143 host, sizeof(host), nullptr, 0, 0));
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800144 ASSERT_STREQ(host, "localhost");
145}
146
147static 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
154TEST(netdb, getnameinfo_ip6_localhost) {
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800155 char host[NI_MAXHOST];
Christopher Ferris2a391882024-12-19 13:44:35 -0800156 sockaddr_in6 addr = {.sin6_family = AF_INET6, .sin6_addr = in6addr_loopback};
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800157 ASSERT_EQ(0, getnameinfo(reinterpret_cast<sockaddr*>(&addr), sizeof(addr),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700158 host, sizeof(host), nullptr, 0, 0));
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800159 VerifyLocalhostName(host);
160}
161
162static void VerifyLocalhost(hostent *hent) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700163 ASSERT_TRUE(hent != nullptr);
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800164 VerifyLocalhostName(hent->h_name);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700165 for (size_t i = 0; hent->h_aliases[i] != nullptr; ++i) {
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800166 VerifyLocalhostName(hent->h_aliases[i]);
167 }
Derek Xue4912fc72014-08-13 14:19:17 +0100168 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}
Derek Xueba811122014-08-13 14:19:17 +0100174
Yabin Cui58d33a52014-12-16 17:03:44 -0800175TEST(netdb, gethostbyname) {
176 hostent* hp = gethostbyname("localhost");
177 VerifyLocalhost(hp);
178}
179
180TEST(netdb, gethostbyname2) {
181 hostent* hp = gethostbyname2("localhost", AF_INET);
182 VerifyLocalhost(hp);
183}
184
185TEST(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
207TEST(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
229TEST(netdb, gethostbyaddr) {
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800230 in_addr addr = { htonl(0x7f000001) };
231 hostent *hp = gethostbyaddr(&addr, sizeof(addr), AF_INET);
Yabin Cui58d33a52014-12-16 17:03:44 -0800232 VerifyLocalhost(hp);
233}
234
235TEST(netdb, gethostbyaddr_r) {
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800236 in_addr addr = { htonl(0x7f000001) };
Yabin Cui58d33a52014-12-16 17:03:44 -0800237 hostent hent;
238 hostent *hp;
239 char buf[512];
240 int err;
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800241 int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
Yabin Cui58d33a52014-12-16 17:03:44 -0800242 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];
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800251 result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err);
Yabin Cui58d33a52014-12-16 17:03:44 -0800252 ASSERT_EQ(0, result);
253 VerifyLocalhost(hp2);
254
255 ASSERT_EQ(0, hp->h_addr[0]);
256}
257
Colin Cross4c5595c2021-08-16 15:51:59 -0700258#if defined(ANDROID_HOST_MUSL)
Colin Cross7da20342021-07-28 11:18:11 -0700259// 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
Yabin Cui58d33a52014-12-16 17:03:44 -0800265TEST(netdb, gethostbyname_r_ERANGE) {
266 hostent hent;
267 hostent *hp;
268 char buf[4]; // Use too small buffer.
Colin Cross7da20342021-07-28 11:18:11 -0700269 int err = 0;
Yabin Cui58d33a52014-12-16 17:03:44 -0800270 int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err);
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700271 EXPECT_EQ(NETDB_INTERNAL, err);
272 EXPECT_EQ(ERANGE, result);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700273 EXPECT_EQ(nullptr, hp);
Yabin Cui58d33a52014-12-16 17:03:44 -0800274}
275
276TEST(netdb, gethostbyname2_r_ERANGE) {
277 hostent hent;
278 hostent *hp;
279 char buf[4]; // Use too small buffer.
Colin Cross7da20342021-07-28 11:18:11 -0700280 int err = 0;
Yabin Cui58d33a52014-12-16 17:03:44 -0800281 int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700282 EXPECT_EQ(NETDB_INTERNAL, err);
283 EXPECT_EQ(ERANGE, result);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700284 EXPECT_EQ(nullptr, hp);
Yabin Cui58d33a52014-12-16 17:03:44 -0800285}
286
287TEST(netdb, gethostbyaddr_r_ERANGE) {
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800288 in_addr addr = { htonl(0x7f000001) };
Yabin Cui58d33a52014-12-16 17:03:44 -0800289 hostent hent;
290 hostent *hp;
291 char buf[4]; // Use too small buffer.
Colin Cross7da20342021-07-28 11:18:11 -0700292 int err = 0;
Yabin Cuia35cd8c2015-01-13 14:35:15 -0800293 int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700294 EXPECT_EQ(NETDB_INTERNAL, err);
295 EXPECT_EQ(ERANGE, result);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700296 EXPECT_EQ(nullptr, hp);
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700297}
298
299TEST(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);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700307 EXPECT_EQ(nullptr, hp);
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700308}
309
310TEST(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);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700318 EXPECT_EQ(nullptr, hp);
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700319}
320
321TEST(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);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700330 EXPECT_EQ(nullptr, hp);
Yabin Cui58d33a52014-12-16 17:03:44 -0800331}
332
Derek Xueba811122014-08-13 14:19:17 +0100333TEST(netdb, getservbyname) {
334 // smtp is TCP-only, so we know we'll get 25/tcp back.
Elliott Hughes50339182017-10-13 17:52:01 -0700335 servent* s = getservbyname("smtp", nullptr);
336 ASSERT_TRUE(s != nullptr);
337 ASSERT_STREQ("smtp", s->s_name);
Derek Xueba811122014-08-13 14:19:17 +0100338 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");
Elliott Hughes50339182017-10-13 17:52:01 -0700343 ASSERT_TRUE(s != nullptr);
344 ASSERT_STREQ("smtp", s->s_name);
Derek Xueba811122014-08-13 14:19:17 +0100345 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");
Elliott Hughes50339182017-10-13 17:52:01 -0700350 ASSERT_TRUE(s == nullptr);
Derek Xueba811122014-08-13 14:19:17 +0100351
352 // But there are actually udp services.
353 s = getservbyname("echo", "udp");
Elliott Hughes50339182017-10-13 17:52:01 -0700354 ASSERT_TRUE(s != nullptr);
355 ASSERT_STREQ("echo", s->s_name);
Derek Xueba811122014-08-13 14:19:17 +0100356 ASSERT_EQ(7, ntohs(s->s_port));
357 ASSERT_STREQ("udp", s->s_proto);
358}
Elliott Hughes50339182017-10-13 17:52:01 -0700359
360TEST(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
387TEST(netdb, endnetent_getnetent_setnetent) {
388 setnetent(0);
389 setnetent(1);
390 endnetent();
391 while (getnetent() != nullptr) {
392 }
393}
394
395TEST(netdb, getnetbyaddr) {
396 getnetbyaddr(0, 0);
397}
398
399TEST(netdb, getnetbyname) {
400 getnetbyname("x");
401}
402
403TEST(netdb, endprotoent_getprotoent_setprotoent) {
404 setprotoent(0);
405 setprotoent(1);
406 endprotoent();
407 while (getprotoent() != nullptr) {
408 }
409}
410
411TEST(netdb, getprotobyname) {
412 getprotobyname("tcp");
413}
414
415TEST(netdb, getprotobynumber) {
416 getprotobynumber(6);
417}
418
419TEST(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
430TEST(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
438TEST(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
446TEST(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
453TEST(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
460TEST(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
471TEST(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
478TEST(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}