Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/tests/lib/libc/net test harness for gethostbyname()/gethostb...
details: https://anonhg.NetBSD.org/src/rev/bbb27cc4c734
branches: trunk
changeset: 789360:bbb27cc4c734
user: christos <christos%NetBSD.org@localhost>
date: Fri Aug 16 15:29:45 2013 +0000
description:
test harness for gethostbyname()/gethostbyaddr() and their internal bits.
XXX[1]: How can we avoid using hard-coded hosts for DNS
XXX[2]: How do we test NIS?
diffstat:
tests/lib/libc/net/Makefile | 11 ++-
tests/lib/libc/net/h_hostent.c | 190 ++++++++++++++++++++++++++++++++++++++++
tests/lib/libc/net/hosts | 11 ++
tests/lib/libc/net/t_hostent.sh | 188 +++++++++++++++++++++++++++++++++++++++
4 files changed, 399 insertions(+), 1 deletions(-)
diffs (truncated from 438 to 300 lines):
diff -r dd740f7ef811 -r bbb27cc4c734 tests/lib/libc/net/Makefile
--- a/tests/lib/libc/net/Makefile Fri Aug 16 15:27:12 2013 +0000
+++ b/tests/lib/libc/net/Makefile Fri Aug 16 15:29:45 2013 +0000
@@ -1,10 +1,11 @@
-# $NetBSD: Makefile,v 1.7 2012/09/15 16:22:58 plunky Exp $
+# $NetBSD: Makefile,v 1.8 2013/08/16 15:29:45 christos Exp $
.include <bsd.own.mk>
MKMAN= no
TESTS_SUBDIRS+= getaddrinfo
+FILES+=hosts
TESTSDIR= ${TESTSBASE}/lib/libc/net
@@ -19,12 +20,20 @@
TESTS_SH+= t_nsdispatch
TESTS_SH+= t_protoent
TESTS_SH+= t_servent
+TESTS_SH+= t_hostent
BINDIR= ${TESTSDIR}
PROGS+= h_nsd_recurse
PROGS+= h_protoent
PROGS+= h_servent
+PROGS+= h_hostent
+CPPFLAGS.h_hostent.c += -I${NETBSDSRCDIR}/lib/libc/net
+
+# For easy debugging, without installing libc
+#.PATH.c:${NETBSDSRCDIR}/lib/libc/net
+#SRCS.h_hostent = h_hostent.c gethnamaddr.c
+#DBG=-g3
LDADD.h_nsd_recurse+= -lpthread
diff -r dd740f7ef811 -r bbb27cc4c734 tests/lib/libc/net/h_hostent.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/net/h_hostent.c Fri Aug 16 15:29:45 2013 +0000
@@ -0,0 +1,190 @@
+/* $NetBSD: h_hostent.c,v 1.1 2013/08/16 15:29:45 christos Exp $ */
+
+/*-
+ * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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/cdefs.h>
+__RCSID("$NetBSD: h_hostent.c,v 1.1 2013/08/16 15:29:45 christos Exp $");
+
+#include <stdio.h>
+#include <string.h>
+#include <nsswitch.h>
+#include <netdb.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <err.h>
+
+#include <netinet/in.h>
+#include <sys/types.h>
+#include <arpa/nameser.h>
+#include <arpa/inet.h>
+
+#include "hostent.h"
+
+static void
+phostent(const struct hostent *h)
+{
+ size_t i;
+ char buf[1024];
+ const int af = h->h_length == NS_INADDRSZ ? AF_INET : AF_INET6;
+
+ printf("name=%s, length=%d, addrtype=%d, aliases=[",
+ h->h_name, h->h_length, h->h_addrtype);
+
+ for (i = 0; h->h_aliases[i]; i++)
+ printf("%s%s", i == 0 ? "" : " ", h->h_aliases[i]);
+
+ printf("] addr_list=[");
+
+ for (i = 0; h->h_addr_list[i]; i++)
+ printf("%s%s", i == 0 ? "" : " ", inet_ntop(af,
+ h->h_addr_list[i], buf, (socklen_t)sizeof(buf)));
+
+ printf("]\n");
+}
+
+static void
+usage(void)
+{
+ (void)fprintf(stderr, "Usage: %s [-f <hostsfile>] "
+ "[-t <any|dns|nis|files>] "
+ "[-46a] <name|address>\n", getprogname());
+ exit(EXIT_FAILURE);
+}
+
+static void
+getby(int (*f)(void *, void *, va_list), struct getnamaddr *info, ...)
+{
+ va_list ap;
+ int e;
+
+ va_start(ap, info);
+ e = (*f)(info, NULL, ap);
+ va_end(ap);
+ switch (e) {
+ case NS_SUCCESS:
+ phostent(info->hp);
+ break;
+ default:
+ printf("error %d\n", e);
+ break;
+ }
+}
+
+static void
+geta(struct hostent *hp) {
+ if (hp == NULL)
+ printf("error %d\n", h_errno);
+ else
+ phostent(hp);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int (*f)(void *, void *, va_list) = NULL;
+ const char *type = "any";
+ int c, af, e, byaddr, len;
+ struct hostent hent;
+ struct getnamaddr info;
+ char buf[4096];
+
+ af = AF_INET;
+ byaddr = 0;
+ len = 0;
+ info.hp = &hent;
+ info.buf = buf;
+ info.buflen = sizeof(buf);
+ info.he = &e;
+
+ while ((c = getopt(argc, argv, "46af:t:")) != -1) {
+ switch (c) {
+ case '4':
+ af = AF_INET;
+ break;
+ case '6':
+ af = AF_INET6;
+ break;
+ case 'a':
+ byaddr++;
+ break;
+ case 't':
+ type = optarg;
+ break;
+ case 'f':
+ _hf_sethostsfile(optarg);
+ break;
+ default:
+ usage();
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc != 1)
+ usage();
+
+ switch (*type) {
+ case 'a':
+ break;
+ case 'd':
+ f = byaddr ? _dns_gethtbyaddr : _dns_gethtbyname;
+ break;
+#ifdef YP
+ case 'n':
+ f = byaddr ? _yp_gethtbyaddr : _yp_gethtbyname;
+ break;
+#endif
+ case 'f':
+ f = byaddr ? _hf_gethtbyaddr : _hf_gethtbyname;
+ break;
+ default:
+ errx(EXIT_FAILURE, "Unknown db type `%s'", type);
+ }
+
+ if (byaddr) {
+ struct in6_addr addr;
+ af = strchr(*argv, ':') ? AF_INET6 : AF_INET;
+ len = af == AF_INET ? NS_INADDRSZ : NS_IN6ADDRSZ;
+ if (inet_pton(af, *argv, &addr) == -1)
+ err(EXIT_FAILURE, "Can't parse `%s'", *argv);
+ if (*type == 'a')
+ geta(gethostbyaddr((const char *)&addr, len, af));
+ else
+ getby(f, &info, &addr, len, af);
+ } else {
+ if (*type == 'a')
+ geta(gethostbyname2(*argv, af));
+ else
+ getby(f, &info, *argv, len, af);
+ }
+
+ return 0;
+}
diff -r dd740f7ef811 -r bbb27cc4c734 tests/lib/libc/net/hosts
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/net/hosts Fri Aug 16 15:29:45 2013 +0000
@@ -0,0 +1,11 @@
+# $NetBSD: hosts,v 1.1 2013/08/16 15:29:45 christos Exp $
+#
+# Host Database
+# This file should contain the addresses and aliases
+# for local hosts that share this file.
+# It is used only for "ifconfig" and other operations
+# before the nameserver is started.
+#
+#
+::1 localhost localhost. localhost.localdomain.
+127.0.0.1 localhost localhost. localhost.localdomain.
diff -r dd740f7ef811 -r bbb27cc4c734 tests/lib/libc/net/t_hostent.sh
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/net/t_hostent.sh Fri Aug 16 15:29:45 2013 +0000
@@ -0,0 +1,188 @@
+# $NetBSD: t_hostent.sh,v 1.1 2013/08/16 15:29:45 christos Exp $
+#
+# Copyright (c) 2008 The NetBSD Foundation, Inc.
+# All rights reserved.
+#
+# 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+#
+
+n6="sixthavenue.astron.com"
+a6="2620:106:3000:100:2e0:81ff:fe2f:e5d6"
+ans6="name=$n6, length=16, addrtype=24, aliases=[] addr_list=[$a6]\n"
+
+n4="sixthavenue.astron.com"
+a4="208.77.212.99"
+ans4="name=$n4, length=4, addrtype=2, aliases=[] addr_list=[$a4]\n"
+
+l6="localhost"
+al6="::1"
+loc6="name=$l6, length=16, addrtype=24, aliases=[localhost. localhost.localdomain.] addr_list=[$al6]\n"
+
+l4="localhost"
+al4="127.0.0.1"
+loc4="name=$l4, length=4, addrtype=2, aliases=[localhost. localhost.localdomain.] addr_list=[$al4]\n"
+
+atf_test_case gethostbyname4
+gethostbyname4_head()
+{
+ atf_set "descr" "Checks gethostbyname2(3) for AF_INET (auto, as determined by nsswitch.conf(5)"
+}
+gethostbyname4_body()
+{
Home |
Main Index |
Thread Index |
Old Index