Path: blob/main/lib/libc/tests/net/getaddrinfo/getaddrinfo.c
39562 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2025 Gleb Smirnoff <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <dlfcn.h>28#include <errno.h>29#include <netdb.h>30#include <stdlib.h>31#include <string.h>32#include <resolv.h>3334#include <atf-c.h>3536static const char goodname[] = "www.freebsd.org";37static const char goodname_dot[] = "www.freebsd.org.";38static const char badname[] = "does-not-exist.freebsd.org";39static const char badname_dot[] = "does-not-exist.freebsd.org.";40static const char ipv6onlyname[] = "beefy15.nyi.freebsd.org";41static const char ipv6onlyname_dot[] = "beefy15.nyi.freebsd.org.";42static const char ipv4onlyname[] = "ipv4only.arpa";43static const char ipv4onlyname_dot[] = "ipv4only.arpa.";44/*45* We need an IP address that doesn't exist, but not reported with ICMP46* unreachable by the nearest router. Let's try TEST-NET-3.47*/48static char badresolvconf[] = "nameserver 203.0.113.1";49static char badresolvconf2[] = "nameserver 203.0.113.1\n"50"nameserver 203.0.113.2";51static char *resconf = NULL;52FILE *53fopen(const char * restrict path, const char * restrict mode)54{55static FILE *(*orig)(const char *, const char *);5657if (orig == NULL && (orig = dlsym(RTLD_NEXT, "fopen")) == NULL)58atf_libc_error(ENOENT, "dlsym(fopen): %s", dlerror());59if (resconf != NULL && strcmp(path, _PATH_RESCONF) == 0)60return (fmemopen(resconf, strlen(resconf), mode));61else62return (orig(path, mode));63}6465static int send_error = 0;66ssize_t67send(int s, const void *msg, size_t len, int flags)68{69static ssize_t (*orig)(int, const void *, size_t, int);7071if (orig == NULL && (orig = dlsym(RTLD_NEXT, "send")) == NULL)72atf_libc_error(ENOENT, "dlsym(send): %s", dlerror());73if (send_error != 0) {74errno = send_error;75return (-1);76} else {77return (orig(s, msg, len, flags));78}79}8081ATF_TC(basic);82ATF_TC_HEAD(basic, tc)83{84atf_tc_set_md_var(tc, "require.config", "allow_network_access");85}8687ATF_TC_BODY(basic, tc)88{89static const struct addrinfo hints = {90.ai_family = AF_UNSPEC,91.ai_flags = AI_CANONNAME,92};93struct addrinfo *res;94int rv;9596rv = getaddrinfo(goodname, NULL, &hints, &res);97ATF_REQUIRE_MSG(rv == 0,98"Expected 0, got %d (%s)", rv, gai_strerror(rv));99freeaddrinfo(res);100101rv = getaddrinfo(goodname_dot, NULL, &hints, &res);102ATF_REQUIRE_MSG(rv == 0,103"Expected 0, got %d (%s)", rv, gai_strerror(rv));104freeaddrinfo(res);105106rv = getaddrinfo(badname, NULL, &hints, &res);107ATF_REQUIRE_MSG(rv == EAI_NONAME,108"Expected %d (EAI_NONAME), got %d (%s)",109EAI_NONAME, rv, gai_strerror(rv));110111rv = getaddrinfo(badname_dot, NULL, &hints, &res);112ATF_REQUIRE_MSG(rv == EAI_NONAME,113"Expected %d (EAI_NONAME), got %d (%s)",114EAI_NONAME, rv, gai_strerror(rv));115}116117ATF_TC_WITHOUT_HEAD(timeout);118ATF_TC_BODY(timeout, tc)119{120static const struct addrinfo hints = {121.ai_family = AF_UNSPEC,122.ai_flags = AI_CANONNAME,123};124struct addrinfo *res;125int rv;126127resconf = badresolvconf;128rv = getaddrinfo(goodname, NULL, &hints, &res);129ATF_REQUIRE_MSG(rv == EAI_AGAIN,130"Expected %d (EAI_AGAIN), got %d (%s)",131EAI_AGAIN, rv, gai_strerror(rv));132133rv = getaddrinfo(goodname_dot, NULL, &hints, &res);134ATF_REQUIRE_MSG(rv == EAI_AGAIN,135"Expected %d (EAI_AGAIN), got %d (%s)",136EAI_AGAIN, rv, gai_strerror(rv));137}138139ATF_TC_WITHOUT_HEAD(timeout_specific);140ATF_TC_BODY(timeout_specific, tc)141{142static const struct addrinfo hints = {143.ai_family = AF_INET,144.ai_socktype = SOCK_STREAM,145.ai_flags = AI_CANONNAME,146};147struct addrinfo *res;148int rv;149150resconf = badresolvconf;151rv = getaddrinfo(goodname, "666", &hints, &res);152ATF_REQUIRE_MSG(rv == EAI_AGAIN,153"Expected %d (EAI_AGAIN), got %d (%s)",154EAI_AGAIN, rv, gai_strerror(rv));155156rv = getaddrinfo(goodname_dot, "666", &hints, &res);157ATF_REQUIRE_MSG(rv == EAI_AGAIN,158"Expected %d (EAI_AGAIN), got %d (%s)",159EAI_AGAIN, rv, gai_strerror(rv));160}161162ATF_TC_WITHOUT_HEAD(timeout2);163ATF_TC_BODY(timeout2, tc)164{165static const struct addrinfo hints = {166.ai_family = AF_UNSPEC,167.ai_flags = AI_CANONNAME,168};169struct addrinfo *res;170int rv;171172resconf = badresolvconf2;173rv = getaddrinfo(goodname, NULL, &hints, &res);174ATF_REQUIRE_MSG(rv == EAI_AGAIN,175"Expected %d (EAI_AGAIN), got %d (%s)",176EAI_AGAIN, rv, gai_strerror(rv));177178rv = getaddrinfo(goodname_dot, NULL, &hints, &res);179ATF_REQUIRE_MSG(rv == EAI_AGAIN,180"Expected %d (EAI_AGAIN), got %d (%s)",181EAI_AGAIN, rv, gai_strerror(rv));182}183184/*185* Emulate interface/network down.186*/187ATF_TC_WITHOUT_HEAD(netdown);188ATF_TC_BODY(netdown, tc)189{190static const struct addrinfo hints = {191.ai_family = AF_UNSPEC,192.ai_flags = AI_CANONNAME,193};194struct addrinfo *res;195int rv;196197send_error = ENETDOWN;198rv = getaddrinfo(goodname, NULL, &hints, &res);199ATF_REQUIRE_MSG(rv == EAI_AGAIN,200"Expected %d (EAI_AGAIN), got %d (%s)",201EAI_AGAIN, rv, gai_strerror(rv));202203rv = getaddrinfo(goodname_dot, NULL, &hints, &res);204ATF_REQUIRE_MSG(rv == EAI_AGAIN,205"Expected %d (EAI_AGAIN), got %d (%s)",206EAI_AGAIN, rv, gai_strerror(rv));207}208209/*210* See https://reviews.freebsd.org/D37139.211*/212ATF_TC(nofamily);213ATF_TC_HEAD(nofamily, tc)214{215atf_tc_set_md_var(tc, "require.config", "allow_network_access");216}217ATF_TC_BODY(nofamily, tc)218{219static const struct addrinfo hints4 = {220.ai_family = AF_INET,221.ai_flags = AI_CANONNAME,222}, hints6 = {223.ai_family = AF_INET6,224.ai_flags = AI_CANONNAME,225};226struct addrinfo *res;227int rv;228229rv = getaddrinfo(ipv6onlyname, NULL, &hints4, &res);230ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,231"Expected %d (EAI_ADDRFAMILY), got %d (%s)",232EAI_ADDRFAMILY, rv, gai_strerror(rv));233234rv = getaddrinfo(ipv6onlyname_dot, NULL, &hints4, &res);235ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,236"Expected %d (EAI_ADDRFAMILY), got %d (%s)",237EAI_ADDRFAMILY, rv, gai_strerror(rv));238239rv = getaddrinfo(ipv4onlyname, NULL, &hints6, &res);240ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,241"Expected %d (EAI_ADDRFAMILY), got %d (%s)",242EAI_ADDRFAMILY, rv, gai_strerror(rv));243244rv = getaddrinfo(ipv4onlyname_dot, NULL, &hints6, &res);245ATF_REQUIRE_MSG(rv == EAI_ADDRFAMILY,246"Expected %d (EAI_ADDRFAMILY), got %d (%s)",247EAI_ADDRFAMILY, rv, gai_strerror(rv));248249rv = getaddrinfo(badname, NULL, &hints4, &res);250ATF_REQUIRE_MSG(rv == EAI_NONAME,251"Expected %d (EAI_NONAME), got %d (%s)",252EAI_NONAME, rv, gai_strerror(rv));253254rv = getaddrinfo(badname_dot, NULL, &hints6, &res);255ATF_REQUIRE_MSG(rv == EAI_NONAME,256"Expected %d (EAI_NONAME), got %d (%s)",257EAI_NONAME, rv, gai_strerror(rv));258}259260ATF_TP_ADD_TCS(tp)261{262ATF_TP_ADD_TC(tp, basic);263ATF_TP_ADD_TC(tp, timeout);264ATF_TP_ADD_TC(tp, timeout_specific);265ATF_TP_ADD_TC(tp, timeout2);266ATF_TP_ADD_TC(tp, netdown);267ATF_TP_ADD_TC(tp, nofamily);268269return (atf_no_error());270}271272273