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