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