Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F142768469
D49410.id152406.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
7 KB
Referenced Files
None
Subscribers
None
D49410.id152406.diff
View Options
diff --git a/lib/libc/tests/net/getaddrinfo/Makefile b/lib/libc/tests/net/getaddrinfo/Makefile
--- a/lib/libc/tests/net/getaddrinfo/Makefile
+++ b/lib/libc/tests/net/getaddrinfo/Makefile
@@ -7,6 +7,7 @@
BINDIR= ${TESTSDIR}
NETBSD_ATF_TESTS_SH= getaddrinfo_test
+ATF_TESTS_C= getaddrinfo2
PROGS= h_gai
diff --git a/lib/libc/tests/net/getaddrinfo/getaddrinfo2.c b/lib/libc/tests/net/getaddrinfo/getaddrinfo2.c
new file mode 100644
--- /dev/null
+++ b/lib/libc/tests/net/getaddrinfo/getaddrinfo2.c
@@ -0,0 +1,242 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 Gleb Smirnoff <glebius@FreeBSD.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <dlfcn.h>
+#include <errno.h>
+#include <netdb.h>
+#include <stdlib.h>
+#include <string.h>
+#include <resolv.h>
+
+#include <atf-c.h>
+
+static char goodname[] = "www.freebsd.org.";
+static char badname[] = "does-not-exist.freebsd.org.";
+static char ipv6onlyname[] = "beefy15.nyi.freebsd.org.";
+/*
+ * We need an IP address that doesn't exist, but not reported with ICMP
+ * unreachable by the nearest router. Let's try TEST-NET-3.
+ */
+static char badresolvconf[] = "nameserver 203.0.113.1";
+static char badresolvconf2[] = "nameserver 203.0.113.1\n"
+ "nameserver 203.0.113.2";
+static char *resconf = NULL;
+static size_t resconflen;
+FILE *
+fopen(const char * restrict path, const char * restrict mode)
+{
+ static FILE *(*orig)(const char *, const char *);
+
+ if (orig == NULL)
+ orig = dlsym(RTLD_NEXT, "fopen");
+ if (resconf != NULL && strcmp(path, _PATH_RESCONF) == 0) {
+ return (fmemopen(resconf, resconflen, mode));
+ } else
+ return (orig(path, mode));
+}
+
+static int send_error = 0;
+ssize_t
+send(int s, const void *msg, size_t len, int flags)
+{
+ static ssize_t (*orig)(int, const void *, size_t, int);
+
+ if (orig == NULL)
+ orig = dlsym(RTLD_NEXT, "send");
+ if (send_error != 0) {
+ errno = send_error;
+ return (-1);
+ } else
+ return (orig(s, msg, len, flags));
+}
+
+ATF_TC_WITHOUT_HEAD(basic);
+ATF_TC_BODY(basic, tc)
+{
+ struct addrinfo hints = {
+ .ai_family = AF_UNSPEC,
+ .ai_flags = AI_CANONNAME,
+ }, *res;
+ int rv;
+
+ rv = getaddrinfo(goodname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == 0,
+ "Expected 0, got %d (%s)", rv, gai_strerror(rv));
+ freeaddrinfo(res);
+
+ *strrchr(goodname, '.') = '\0';
+ rv = getaddrinfo(goodname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == 0,
+ "Expected 0, got %d (%s)", rv, gai_strerror(rv));
+ freeaddrinfo(res);
+
+ rv = getaddrinfo(badname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_NONAME,
+ "Expected EAI_NONAME, got %d (%s)", rv, gai_strerror(rv));
+
+ *strrchr(badname, '.') = '\0';
+ rv = getaddrinfo(badname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_NONAME,
+ "Expected EAI_NONAME, got %d (%s)", rv, gai_strerror(rv));
+}
+
+ATF_TC_WITHOUT_HEAD(timeout);
+ATF_TC_BODY(timeout, tc)
+{
+ struct addrinfo hints = {
+ .ai_family = AF_UNSPEC,
+ .ai_flags = AI_CANONNAME,
+ }, *res;
+ int rv;
+
+ resconf = badresolvconf;
+ resconflen = sizeof(badresolvconf);
+ rv = getaddrinfo(goodname, NULL, &hints, &res);
+ /*
+ * XXXGL: EAI_ADDRFAMILY is most likely a regression from 144361386696.
+ * Error code on timeout used to be EAI_NONAME for many years and IMHO
+ * this is not correct either. Should be EAI_AGAIN.
+ */
+ ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,
+ "Expected EAI_ADDRFAMILY, got %d (%s)", rv, gai_strerror(rv));
+
+ *strrchr(goodname, '.') = '\0';
+ rv = getaddrinfo(goodname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,
+ "Expected EAI_ADDRFAMILY, got %d (%s)", rv, gai_strerror(rv));
+}
+
+ATF_TC_WITHOUT_HEAD(timeout_specific);
+ATF_TC_BODY(timeout_specific, tc)
+{
+ struct addrinfo hints = {
+ .ai_family = AF_INET,
+ .ai_socktype = SOCK_STREAM,
+ .ai_flags = AI_CANONNAME,
+ }, *res;
+ int rv;
+
+ resconf = badresolvconf;
+ resconflen = sizeof(badresolvconf);
+ rv = getaddrinfo(goodname, "666", &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,
+ "Expected EAI_ADDRFAMILY, got %d (%s)", rv, gai_strerror(rv));
+
+ *strrchr(goodname, '.') = '\0';
+ rv = getaddrinfo(goodname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,
+ "Expected EAI_ADDRFAMILY, got %d (%s)", rv, gai_strerror(rv));
+}
+
+ATF_TC_WITHOUT_HEAD(timeout2);
+ATF_TC_BODY(timeout2, tc)
+{
+ struct addrinfo hints = {
+ .ai_family = AF_UNSPEC,
+ .ai_flags = AI_CANONNAME,
+ }, *res;
+ int rv;
+
+ resconf = badresolvconf2;
+ resconflen = sizeof(badresolvconf2);
+ rv = getaddrinfo(goodname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,
+ "Expected EAI_ADDRFAMILY, got %d (%s)", rv, gai_strerror(rv));
+
+ *strrchr(goodname, '.') = '\0';
+ rv = getaddrinfo(goodname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,
+ "Expected EAI_ADDRFAMILY, got %d (%s)", rv, gai_strerror(rv));
+}
+
+/*
+ * Emulate interface/network down.
+ */
+ATF_TC_WITHOUT_HEAD(netdown);
+ATF_TC_BODY(netdown, tc)
+{
+ struct addrinfo hints = {
+ .ai_family = AF_UNSPEC,
+ .ai_flags = AI_CANONNAME,
+ }, *res;
+ int rv;
+
+ send_error = ENETDOWN;
+ rv = getaddrinfo(goodname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,
+ "Expected EAI_ADDRFAMILY, got %d (%s)", rv, gai_strerror(rv));
+
+ *strrchr(goodname, '.') = '\0';
+ rv = getaddrinfo(goodname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_NONAME,
+ "Expected EAI_NONAME, got %d (%s)", rv, gai_strerror(rv));
+}
+
+/*
+ * See https://reviews.freebsd.org/D37139.
+ */
+ATF_TC_WITHOUT_HEAD(nofamily);
+ATF_TC_BODY(nofamily, tc)
+{
+ struct addrinfo hints = {
+ .ai_family = AF_INET,
+ .ai_flags = AI_CANONNAME,
+ }, *res;
+ int rv;
+
+ rv = getaddrinfo(ipv6onlyname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,
+ "Expected EAI_ADDRFAMILY, got %d (%s)", rv, gai_strerror(rv));
+
+ *strrchr(ipv6onlyname, '.') = '\0';
+ rv = getaddrinfo(ipv6onlyname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,
+ "Expected EAI_ADDRFAMILY, got %d (%s)", rv, gai_strerror(rv));
+
+ rv = getaddrinfo(badname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_NONAME,
+ "Expected EAI_NONAME, got %d (%s)", rv, gai_strerror(rv));
+
+ *strrchr(badname, '.') = '\0';
+ rv = getaddrinfo(badname, NULL, &hints, &res);
+ ATF_REQUIRE_MSG(rv == EAI_NONAME,
+ "Expected EAI_NONAME, got %d (%s)", rv, gai_strerror(rv));
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, basic);
+ ATF_TP_ADD_TC(tp, timeout);
+ ATF_TP_ADD_TC(tp, timeout_specific);
+ ATF_TP_ADD_TC(tp, timeout2);
+ ATF_TP_ADD_TC(tp, netdown);
+ ATF_TP_ADD_TC(tp, nofamily);
+
+ return (atf_no_error());
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Jan 24, 8:40 AM (12 h, 17 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
27895310
Default Alt Text
D49410.id152406.diff (7 KB)
Attached To
Mode
D49410: libc/tests: add getaddrinfo test
Attached
Detach File
Event Timeline
Log In to Comment