TEST(netdb, getaddrinfo_NULL_host) { // It's okay for the host argument to be NULL, as long as service isn't.
addrinfo* ai = nullptr;
ASSERT_EQ(0, getaddrinfo(nullptr, "smtp", nullptr, &ai)); // (sockaddr_in::sin_port and sockaddr_in6::sin6_port overlap.)
ASSERT_EQ(25U, ntohs(reinterpret_cast<sockaddr_in*>(ai->ai_addr)->sin_port));
freeaddrinfo(ai);
}
TEST(netdb, getaddrinfo_NULL_service) { // It's okay for the service argument to be NULL, as long as host isn't.
addrinfo* ai = nullptr;
ASSERT_EQ(0, getaddrinfo("localhost", nullptr, nullptr, &ai));
ASSERT_TRUE(ai != nullptr);
freeaddrinfo(ai);
}
addrinfo* ai = nullptr;
ASSERT_EQ(0, getaddrinfo( "localhost", "9999", &hints, &ai));
ASSERT_TRUE(ai != nullptr); // In glibc, getaddrinfo() converts ::1 to 127.0.0.1 for localhost, // so one or two addrinfo may be returned.
addrinfo* tai = ai; while (tai != nullptr) {
ASSERT_EQ(AF_INET, tai->ai_family);
ASSERT_EQ(SOCK_STREAM, tai->ai_socktype);
ASSERT_EQ(IPPROTO_TCP, tai->ai_protocol);
tai = tai->ai_next;
}
freeaddrinfo(ai);
}
staticvoid VerifyLocalhostName(constchar* name) { // Test possible localhost name and aliases, which depend on /etc/hosts or /system/etc/hosts.
ASSERT_TRUE(strcmp(name, "localhost") == 0 ||
strcmp(name, "ip6-localhost") == 0 ||
strcmp(name, "ip6-loopback") == 0) << name;
}
#ifdefined(ANDROID_HOST_MUSL) // musl doesn't define NETDB_INTERNAL. It also never sets *err to -1, but // since gethostbyname_r is a glibc extension, the difference in behavior // between musl and glibc should probably be considered a bug in musl. #define NETDB_INTERNAL -1 #endif
TEST(netdb, gethostbyname_r_ERANGE) {
hostent hent;
hostent *hp; char buf[4]; // Use too small buffer. int err = 0; int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err);
EXPECT_EQ(NETDB_INTERNAL, err);
EXPECT_EQ(ERANGE, result);
EXPECT_EQ(nullptr, hp);
}
TEST(netdb, gethostbyname2_r_ERANGE) {
hostent hent;
hostent *hp; char buf[4]; // Use too small buffer. int err = 0; int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
EXPECT_EQ(NETDB_INTERNAL, err);
EXPECT_EQ(ERANGE, result);
EXPECT_EQ(nullptr, hp);
}
TEST(netdb, gethostbyaddr_r_ERANGE) {
in_addr addr = { htonl(0x7f000001) };
hostent hent;
hostent *hp; char buf[4]; // Use too small buffer. int err = 0; int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
EXPECT_EQ(NETDB_INTERNAL, err);
EXPECT_EQ(ERANGE, result);
EXPECT_EQ(nullptr, hp);
}
TEST(netdb, gethostbyname_r_HOST_NOT_FOUND) {
hostent hent;
hostent *hp; char buf[BUFSIZ]; int err; int result = gethostbyname_r("does.not.exist.google.com", &hent, buf, sizeof(buf), &hp, &err);
EXPECT_EQ(HOST_NOT_FOUND, err);
EXPECT_EQ(0, result);
EXPECT_EQ(nullptr, hp);
}
TEST(netdb, gethostbyname2_r_HOST_NOT_FOUND) {
hostent hent;
hostent *hp; char buf[BUFSIZ]; int err; int result = gethostbyname2_r("does.not.exist.google.com", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
EXPECT_EQ(HOST_NOT_FOUND, err);
EXPECT_EQ(0, result);
EXPECT_EQ(nullptr, hp);
}
TEST(netdb, getservbyname) { // smtp is TCP-only, so we know we'll get 25/tcp back.
servent* s = getservbyname("smtp", nullptr);
ASSERT_TRUE(s != nullptr);
ASSERT_STREQ("smtp", s->s_name);
ASSERT_EQ(25, ntohs(s->s_port));
ASSERT_STREQ("tcp", s->s_proto);
// We get the same result by explicitly asking for tcp.
s = getservbyname("smtp", "tcp");
ASSERT_TRUE(s != nullptr);
ASSERT_STREQ("smtp", s->s_name);
ASSERT_EQ(25, ntohs(s->s_port));
ASSERT_STREQ("tcp", s->s_proto);
// And we get a failure if we explicitly ask for udp.
s = getservbyname("smtp", "udp");
ASSERT_TRUE(s == nullptr);
// But there are actually udp services.
s = getservbyname("echo", "udp");
ASSERT_TRUE(s != nullptr);
ASSERT_STREQ("echo", s->s_name);
ASSERT_EQ(7, ntohs(s->s_port));
ASSERT_STREQ("udp", s->s_proto);
}
TEST(netdb, getservbyport) { // smtp is TCP-only, so we know we'll get 25/tcp back.
servent* s = getservbyport(htons(25), nullptr);
ASSERT_TRUE(s != nullptr);
ASSERT_STREQ("smtp", s->s_name);
ASSERT_EQ(25, ntohs(s->s_port));
ASSERT_STREQ("tcp", s->s_proto);
// We get the same result by explicitly asking for tcp.
s = getservbyport(htons(25), "tcp");
ASSERT_TRUE(s != nullptr);
ASSERT_STREQ("smtp", s->s_name);
ASSERT_EQ(25, ntohs(s->s_port));
ASSERT_STREQ("tcp", s->s_proto);
// And we get a failure if we explicitly ask for udp.
s = getservbyport(htons(25), "udp");
ASSERT_TRUE(s == nullptr);
// But there are actually udp services.
s = getservbyport(htons(7), "udp");
ASSERT_TRUE(s != nullptr);
ASSERT_STREQ("echo", s->s_name);
ASSERT_EQ(7, ntohs(s->s_port));
ASSERT_STREQ("udp", s->s_proto);
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.13Bemerkung:
(vorverarbeitet am 2026-06-28)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.