Path: blob/main/lib/libc/tests/resolv/resolv_test.c
39530 views
/* $NetBSD: resolv.c,v 1.6 2004/05/23 16:59:11 christos Exp $ */12/*-3* Copyright (c) 2004 The NetBSD Foundation, Inc.4* All rights reserved.5*6* This code is derived from software contributed to The NetBSD Foundation7* by Christos Zoulas.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS19* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED20* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS22* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28* POSSIBILITY OF SUCH DAMAGE.29*/30#include <sys/cdefs.h>31__RCSID("$NetBSD: resolv.c,v 1.6 2004/05/23 16:59:11 christos Exp $");3233#include <sys/types.h>34#include <sys/socket.h>35#include <assert.h>36#include <errno.h>37#include <pthread.h>38#include <stdio.h>39#include <stdatomic.h>40#include <netdb.h>41#include <stdlib.h>42#include <unistd.h>43#include <string.h>44#include <stringlist.h>4546#include <atf-c.h>4748#define NTHREADS 1049#define NHOSTS 10050#define WS " \t\n\r"5152enum method {53METHOD_GETADDRINFO,54METHOD_GETHOSTBY,55METHOD_GETIPNODEBY56};5758static StringList *hosts = NULL;59static _Atomic(int) *ask = NULL;60static _Atomic(int) *got = NULL;61static bool debug_output = 0;6263static void load(const char *);64static void resolvone(long, int, enum method);65static void *resolvloop(void *);66static pthread_t run(int, enum method, long);6768#define DBG(...) do { \69if (debug_output) \70dprintf(STDOUT_FILENO, __VA_ARGS__); \71} while (0)7273static void74load(const char *fname)75{76FILE *fp;77size_t linecap;78char *line;7980fp = fopen(fname, "r");81ATF_REQUIRE(fp != NULL);82line = NULL;83linecap = 0;84while (getline(&line, &linecap, fp) >= 0) {85char *ptr;8687for (ptr = strtok(line, WS); ptr; ptr = strtok(NULL, WS)) {88if (ptr[0] == '#')89break;90sl_add(hosts, strdup(ptr));91}92}93free(line);9495(void)fclose(fp);96}9798static int99resolv_getaddrinfo(long threadnum, char *host, int port, const char **errstr)100{101char portstr[6], hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];102struct addrinfo hints, *res;103int error;104105snprintf(portstr, sizeof(portstr), "%d", port);106memset(&hints, 0, sizeof(hints));107hints.ai_family = AF_UNSPEC;108hints.ai_flags = AI_PASSIVE;109hints.ai_socktype = SOCK_STREAM;110error = getaddrinfo(host, portstr, &hints, &res);111if (error == 0) {112DBG("T%ld: host %s ok\n", threadnum, host);113memset(hbuf, 0, sizeof(hbuf));114memset(pbuf, 0, sizeof(pbuf));115getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),116pbuf, sizeof(pbuf), 0);117DBG("T%ld: reverse %s %s\n", threadnum, hbuf, pbuf);118freeaddrinfo(res);119} else {120*errstr = gai_strerror(error);121DBG("T%ld: host %s not found: %s\n", threadnum, host, *errstr);122}123return error;124}125126static int127resolv_gethostby(long threadnum, char *host, const char **errstr)128{129char buf[1024];130struct hostent *hp, *hp2;131132hp = gethostbyname(host);133if (hp) {134DBG("T%ld: host %s ok\n", threadnum, host);135memcpy(buf, hp->h_addr, hp->h_length);136hp2 = gethostbyaddr(buf, hp->h_length, hp->h_addrtype);137if (hp2) {138DBG("T%ld: reverse %s\n", threadnum, hp2->h_name);139}140} else {141*errstr = hstrerror(h_errno);142DBG("T%ld: host %s not found: %s\n", threadnum, host, *errstr);143}144return hp ? 0 : h_errno;145}146147static int148resolv_getipnodeby(long threadnum, char *host, const char **errstr)149{150char buf[1024];151struct hostent *hp, *hp2;152int error = 0;153154hp = getipnodebyname(host, AF_INET, 0, &error);155if (hp) {156DBG("T%ld: host %s ok\n", threadnum, host);157memcpy(buf, hp->h_addr, hp->h_length);158hp2 = getipnodebyaddr(buf, hp->h_length, hp->h_addrtype,159&error);160if (hp2) {161DBG("T%ld: reverse %s\n", threadnum, hp2->h_name);162freehostent(hp2);163}164freehostent(hp);165} else {166*errstr = hstrerror(error);167DBG("T%ld: host %s not found: %s\n", threadnum, host, *errstr);168}169return hp ? 0 : error;170}171172static void173resolvone(long threadnum, int n, enum method method)174{175const char* errstr = NULL;176size_t i = (random() & 0x0fffffff) % hosts->sl_cur;177char *host = hosts->sl_str[i];178int error;179180DBG("T%ld: %d resolving %s %zd\n", threadnum, n, host, i);181switch (method) {182case METHOD_GETADDRINFO:183error = resolv_getaddrinfo(threadnum, host, i, &errstr);184break;185case METHOD_GETHOSTBY:186error = resolv_gethostby(threadnum, host, &errstr);187break;188case METHOD_GETIPNODEBY:189error = resolv_getipnodeby(threadnum, host, &errstr);190break;191default:192/* UNREACHABLE */193/* XXX Needs an __assert_unreachable() for userland. */194assert(0 && "Unreachable segment reached");195abort();196break;197}198atomic_fetch_add_explicit(&ask[i], 1, memory_order_relaxed);199if (error == 0)200atomic_fetch_add_explicit(&got[i], 1, memory_order_relaxed);201else if (got[i] != 0)202fprintf(stderr,203"T%ld ERROR after previous success for %s: %d (%s)\n",204threadnum, hosts->sl_str[i], error, errstr);205}206207struct resolvloop_args {208int nhosts;209enum method method;210long threadnum;211};212213static void *214resolvloop(void *p)215{216struct resolvloop_args *args = p;217int nhosts = args->nhosts;218219if (nhosts == 0) {220free(args);221return NULL;222}223224do {225resolvone(args->threadnum, nhosts, args->method);226} while (--nhosts);227free(args);228return (void *)(uintptr_t)nhosts;229}230231static pthread_t232run(int nhosts, enum method method, long i)233{234pthread_t t;235int rc;236struct resolvloop_args *args;237238/* Created thread is responsible for free(). */239args = malloc(sizeof(*args));240ATF_REQUIRE(args != NULL);241242args->nhosts = nhosts;243args->method = method;244args->threadnum = i + 1;245rc = pthread_create(&t, NULL, resolvloop, args);246ATF_REQUIRE_MSG(rc == 0, "pthread_create failed: %s", strerror(rc));247return t;248}249250static int251run_tests(const char *hostlist_file, enum method method)252{253size_t nthreads = NTHREADS;254pthread_t *threads;255size_t nhosts = NHOSTS;256size_t i;257int c;258hosts = sl_init();259260srandom(1234);261debug_output = getenv("DEBUG_OUTPUT") != NULL;262263load(hostlist_file);264265ATF_REQUIRE_MSG(0 < hosts->sl_cur, "0 hosts in %s", hostlist_file);266267ask = calloc(hosts->sl_cur, sizeof(int));268ATF_REQUIRE(ask != NULL);269270got = calloc(hosts->sl_cur, sizeof(int));271ATF_REQUIRE(got != NULL);272273threads = calloc(nthreads, sizeof(pthread_t));274ATF_REQUIRE(threads != NULL);275276for (i = 0; i < nthreads; i++) {277threads[i] = run(nhosts, method, i);278}279/* Wait for all threads to join and check that they checked all hosts */280for (i = 0; i < nthreads; i++) {281size_t remaining;282283remaining = (uintptr_t)pthread_join(threads[i], NULL);284ATF_CHECK_EQ_MSG(0, remaining,285"Thread %zd still had %zd hosts to check!", i, remaining);286}287288c = 0;289for (i = 0; i < hosts->sl_cur; i++) {290if (got[i] != 0) {291ATF_CHECK_EQ_MSG(ask[i], got[i],292"Error: host %s ask %d got %d", hosts->sl_str[i],293ask[i], got[i]);294c += ask[i] != got[i];295}296}297free(threads);298free(ask);299free(got);300sl_free(hosts, 1);301return c;302}303304#define HOSTLIST_FILE "mach"305306#define RUN_TESTS(tc, method) \307do { \308char *_hostlist_file; \309ATF_REQUIRE(0 < asprintf(&_hostlist_file, "%s/%s", \310atf_tc_get_config_var(tc, "srcdir"), HOSTLIST_FILE)); \311ATF_REQUIRE(run_tests(_hostlist_file, method) == 0); \312} while(0)313314ATF_TC(getaddrinfo_test);315ATF_TC_HEAD(getaddrinfo_test, tc) {316atf_tc_set_md_var(tc, "timeout", "1200");317}318ATF_TC_BODY(getaddrinfo_test, tc)319{320321RUN_TESTS(tc, METHOD_GETADDRINFO);322}323324ATF_TC(gethostby_test);325ATF_TC_HEAD(gethostby_test, tc) {326atf_tc_set_md_var(tc, "timeout", "1200");327}328ATF_TC_BODY(gethostby_test, tc)329{330331RUN_TESTS(tc, METHOD_GETHOSTBY);332}333334ATF_TC(getipnodeby_test);335ATF_TC_HEAD(getipnodeby_test, tc) {336337atf_tc_set_md_var(tc, "timeout", "1200");338}339ATF_TC_BODY(getipnodeby_test, tc)340{341342RUN_TESTS(tc, METHOD_GETIPNODEBY);343}344345ATF_TP_ADD_TCS(tp)346{347348ATF_TP_ADD_TC(tp, getaddrinfo_test);349ATF_TP_ADD_TC(tp, gethostby_test);350ATF_TP_ADD_TC(tp, getipnodeby_test);351352return (atf_no_error());353}354355356