From 9e7378611290b0f24f7e6491d4fc76a2399cee62 Mon Sep 17 00:00:00 2001 From: otto <> Date: Mon, 25 Oct 2004 15:10:36 +0000 Subject: basic regression test for some netdb functions --- src/regress/lib/libc/Makefile | 4 +- src/regress/lib/libc/netdb/Makefile | 5 + src/regress/lib/libc/netdb/netdb.c | 182 ++++++++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 src/regress/lib/libc/netdb/Makefile create mode 100644 src/regress/lib/libc/netdb/netdb.c diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile index 6dfe1cfe28..34d418e428 100644 --- a/src/regress/lib/libc/Makefile +++ b/src/regress/lib/libc/Makefile @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile,v 1.17 2004/08/03 20:39:28 otto Exp $ +# $OpenBSD: Makefile,v 1.18 2004/10/25 15:10:36 otto Exp $ SUBDIR+= _setjmp alloca atexit db getaddrinfo getopt_long hsearch longjmp malloc -SUBDIR+= popen regex setjmp setjmp-signal sigreturn sigsetjmp +SUBDIR+= netdb popen regex setjmp setjmp-signal sigreturn sigsetjmp SUBDIR+= sprintf strerror strtonum time .if (${MACHINE_ARCH} != "vax") diff --git a/src/regress/lib/libc/netdb/Makefile b/src/regress/lib/libc/netdb/Makefile new file mode 100644 index 0000000000..8254607227 --- /dev/null +++ b/src/regress/lib/libc/netdb/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2004/10/25 15:10:36 otto Exp $ + +PROG= netdb + +.include diff --git a/src/regress/lib/libc/netdb/netdb.c b/src/regress/lib/libc/netdb/netdb.c new file mode 100644 index 0000000000..0082c1bd1b --- /dev/null +++ b/src/regress/lib/libc/netdb/netdb.c @@ -0,0 +1,182 @@ +/* $OpenBSD: netdb.c,v 1.1 2004/10/25 15:10:36 otto Exp $ */ + +/* + * Public domain, 2004, Otto Moerbeek + */ + +#include +#include +#include + +int ret = 0; + +void +checkp(int n, struct protoent *p, int proto, const char *name, ...) +{ + va_list va; + char *a; + int i; + + if (p == NULL) { + warnx("%d proto struct is NULL", n); + ret = 1; + return; + } + if (p->p_proto != proto) { + warnx("%d proto num mismatch %d %d", n, p->p_proto, proto); + ret = 1; + } + if (strcmp(p->p_name, name) != 0) { + warnx("%d proto name mismatch %s %s", n, p->p_name, name); + ret = 1; + } + va_start(va, name); + a = va_arg(va, char *); + i = 0; + while (a != NULL) { + if (strcmp(p->p_aliases[i], a) != 0) { + warnx("%d proto alias mismatch %s %s", n, + p->p_aliases[i], a); + ret = 1; + } + i++; + a = va_arg(va, char *); + } + if (p->p_aliases[i] != NULL) { + warnx("%d proto alias list does not end with NULL", n); + ret = 1; + } + va_end(va); +} + + +void +checks(int n, struct servent *s, int port, const char *proto, + const char *name, ...) +{ + va_list va; + char *a; + int i; + + if (s == NULL) { + warnx("%d serv struct is NULL", n); + ret = 1; + return; + } + if (s->s_port != ntohs(port)) { + warnx("%d port num mismatch %d %d", n, s->s_port, port); + ret = 1; + } + if (strcmp(s->s_name, name) != 0) { + warnx("%d serv name mismatch %s %s", n, s->s_name, name); + ret = 1; + } + if (strcmp(s->s_proto, proto) != 0) { + warnx("%d serv proto mismatch %s %s", n, s->s_proto, proto); + ret = 1; + } + va_start(va, name); + a = va_arg(va, char *); + i = 0; + while (a != NULL) { + if (strcmp(s->s_aliases[i], a) != 0) { + warnx("%d serv alias mismatch %s %s", n, + s->s_aliases[i], a); + ret = 1; + } + i++; + a = va_arg(va, char *); + } + if (s->s_aliases[i] != NULL) { + warnx("%d serv alias list does not end with NULL", n); + ret = 1; + } + va_end(va); +} + +int +main() +{ + struct protoent *p; + struct protoent protoent; + struct protoent_data protoent_data; + struct servent *s; + struct servent servent; + struct servent_data servent_data; + int r; + + p = getprotobynumber(35); + checkp(1, p, 35, "idpr", "IDPR", (char *)NULL); + + p = getprotobyname("igp"); + checkp(2, p, 9, "igp", "IGP", (char *) NULL); + + p = getprotobyname("RDP"); + checkp(3, p, 27, "rdp", "RDP", (char *) NULL); + + p = getprotobyname("vrrp"); + checkp(4, p, 112, "carp", "CARP", "vrrp", (char *) NULL); + + p = getprotobyname("nonexistent"); + if (p != NULL) + err(1, "nonexistent proto found"); + + memset(&protoent_data, 0, sizeof(protoent_data)); + r = getprotobynumber_r(35, &protoent, &protoent_data); + if (r != 0) + err(1, "proto not found"); + + checkp(5, &protoent, 35, "idpr", "IDPR", (char *)NULL); + + r = getprotobyname_r("vrrp", &protoent, &protoent_data); + if (r != 0) + err(1, "proto not found"); + checkp(6, &protoent, 112, "carp", "CARP", "vrrp", (char *) NULL); + + r = getprotobyname_r("nonexistent", &protoent, &protoent_data); + if (r != -1) + err(1, "nonexistent proto found"); + + r = getprotobyname_r("", &protoent, &protoent_data); + if (r != -1) + err(1, "nonexistent proto found"); + + + r = getprotobynumber_r(256, &protoent, &protoent_data); + if (r != -1) + err(1, "nonexistent proto found"); + + s = getservbyname("zip", NULL); + checks(7, s, 6, "ddp", "zip", (char *)NULL); + + s = getservbyname("nicname", "tcp"); + checks(8, s, 43, "tcp", "whois", "nicname", (char *)NULL); + + s = getservbyport(htons(42), "tcp"); + checks(9, s, 42, "tcp", "nameserver", "name", (char *)NULL); + + memset(&servent_data, 0, sizeof(servent_data)); + r = getservbyname_r("zip", "ddp", &servent, &servent_data); + if (r != 0) + err(1, "servent not found"); + checks(10, &servent, 6, "ddp", "zip", (char *)NULL); + + r = getservbyport_r(htons(520), NULL, &servent, &servent_data); + if (r != 0) + err(1, "servent not found"); + checks(11, &servent, 520, "udp", "route", "router", "routed", (char *)NULL); + + r = getservbyname_r("nonexistent", NULL, &servent, &servent_data); + if (r != -1) + err(1, "nonexiststent servent found"); + + r = getservbyport_r(htons(50000), NULL, &servent, &servent_data); + if (r != -1) + err(1, "nonexistent servent found"); + + r = getservbyport_r(htons(520), "tcp", &servent, &servent_data); + if (r != -1) + err(1, "nonexistent servent found"); + + return ret; +} -- cgit v1.2.3-55-g6feb