From: Greg Hudson Date: Fri, 26 Jul 2019 22:33:28 +0000 (-0400) Subject: Fix test suite issues affecting Travis images X-Git-Tag: krb5-1.18-beta1~91 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F956%2Fhead;p=thirdparty%2Fkrb5.git Fix test suite issues affecting Travis images In the utilities used by the dejagnu test suites, use getaddrinfo()/getnameinfo() instead of gethostbyname()/gethostbyaddr(), as the results can vary when the local hostname appears in multiple lines in /etc/hosts. In t_ccselect.py, don't cause an error if the canonicalized local hostname is "localhost". The tests will continue to run in this case, as long as we don't try to create duplicate principals. In sim_server.c, bind to the wildcard address instead of the resolved local hostname, to resolve a mysterious problem observed in Travis where the second of three sim_client send() operations fails with ECONNREFUSED. --- diff --git a/src/appl/simple/server/sim_server.c b/src/appl/simple/server/sim_server.c index f09489c35d..ed383a00bf 100644 --- a/src/appl/simple/server/sim_server.c +++ b/src/appl/simple/server/sim_server.c @@ -69,10 +69,8 @@ main(int argc, char *argv[]) int flags = 0; /* for recvfrom() */ int on = 1; struct servent *serv; - struct hostent *host; struct sockaddr_in s_sock; /* server's address */ struct sockaddr_in c_sock; /* client's address */ - char full_hname[MAXHOSTNAMELEN]; char *cp; extern char * optarg; int ch; @@ -133,6 +131,7 @@ main(int argc, char *argv[]) /* Set up server address */ memset(&s_sock, 0, sizeof(s_sock)); s_sock.sin_family = AF_INET; + s_sock.sin_addr.s_addr = INADDR_ANY; if (port == 0) { /* Look up service */ @@ -145,17 +144,6 @@ main(int argc, char *argv[]) s_sock.sin_port = htons(port); } - if (gethostname(full_hname, sizeof(full_hname)) < 0) { - perror("gethostname"); - exit(1); - } - - if ((host = gethostbyname(full_hname)) == (struct hostent *)0) { - fprintf(stderr, "%s: host unknown\n", full_hname); - exit(1); - } - memcpy(&s_sock.sin_addr, host->h_addr, sizeof(s_sock.sin_addr)); - /* Open socket */ if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("opening datagram socket"); diff --git a/src/kadmin/testing/scripts/qualname.plin b/src/kadmin/testing/scripts/qualname.plin index b712d89738..ccc57e3a73 100755 --- a/src/kadmin/testing/scripts/qualname.plin +++ b/src/kadmin/testing/scripts/qualname.plin @@ -1,19 +1,20 @@ -#!/afs/athena/contrib/perl/p +#!/usr/bin/perl +use Socket qw(:addrinfo); +use strict; +my $hostname; if ($#ARGV == -1) { chop($hostname = `hostname`); } else { $hostname = $ARGV[0]; } -if (! (($name,$type,$addr) = (gethostbyname($hostname))[0,2,4])) { - print STDERR "No such host: $hostname\n"; - exit(1); -} -if (! ($qualname = (gethostbyaddr($addr,$type))[0])) { - $qualname = $name; -} +my ($gaerr, @addrs) = getaddrinfo($hostname, "", {flags => AI_CANONNAME}); +die "No such host: $hostname ($gaerr)" if $gaerr; +my ($canonname, $addr) = ($addrs[0]->{canonname}, $addrs[0]->{addr}); + +my ($gnerr, $name, $servicename) = getnameinfo($addr, NI_NAMEREQD); +my $qualname = $gnerr ? $name : $name; $qualname =~ tr/A-Z/a-z/; # lowercase our name for keytab use. print "$qualname\n"; - diff --git a/src/tests/gssapi/t_ccselect.py b/src/tests/gssapi/t_ccselect.py index 9ca66554fb..423a00af2e 100755 --- a/src/tests/gssapi/t_ccselect.py +++ b/src/tests/gssapi/t_ccselect.py @@ -76,8 +76,9 @@ r1.addprinc(bob, password('bob')) r2.addprinc(zaphod, password('zaphod')) # Create host principals and keytabs for fallback realm tests. -r1.addprinc('host/localhost') -r2.addprinc('host/localhost') +if hostname != 'localhost': + r1.addprinc('host/localhost') + r2.addprinc('host/localhost') r1.addprinc('host/' + foo) r2.addprinc('host/' + foo2) r1.addprinc('host/' + foobar) diff --git a/src/tests/resolve/Makefile.in b/src/tests/resolve/Makefile.in index e6248f814b..1f59540896 100644 --- a/src/tests/resolve/Makefile.in +++ b/src/tests/resolve/Makefile.in @@ -8,7 +8,7 @@ SRCS=$(srcdir)/resolve.c $(srcdir)/addrinfo-test.c \ all: resolve addrinfo-test fake-addrinfo-test resolve: resolve.o - $(CC_LINK) -o $@ resolve.o $(LIBS) + $(CC_LINK) -o $@ resolve.o $(SUPPORT_LIB) $(LIBS) addrinfo-test: addrinfo-test.o $(CC_LINK) -o $@ addrinfo-test.o $(SUPPORT_LIB) $(LIBS) diff --git a/src/tests/resolve/resolve.c b/src/tests/resolve/resolve.c index 7339d21bd9..ea0239113a 100644 --- a/src/tests/resolve/resolve.c +++ b/src/tests/resolve/resolve.c @@ -42,47 +42,22 @@ /* This program tests the resolve library and sees if it is broken... */ -#include "autoconf.h" -#include - -#if STDC_HEADERS -#include -#else -#ifndef HAVE_STRCHR -#define strchr index -#endif -char *strchr(); -#endif - +#include "k5-platform.h" +#include +#include +#include +#include #ifdef HAVE_SYS_PARAM_H #include #endif -#ifdef HAVE_SYS_SOCKET_H -#include -#endif - -#ifdef HAVE_STDLIB_H -#include -#endif - -#ifdef HAVE_UNISTD_H -#include -#endif - -#include -#include - int -main(argc, argv) - int argc; - char **argv; +main(int argc, char **argv) { - char myname[MAXHOSTNAMELEN+1]; - char *ptr, *fqdn; - struct in_addr addrcopy; - struct hostent *host; - int quiet = 0; + struct addrinfo *ai = NULL, hint; + char myname[MAXHOSTNAMELEN + 1], namebuf[NI_MAXHOST], abuf[256]; + const char *addrstr; + int err, quiet = 0; argc--; argv++; while (argc) { @@ -95,7 +70,7 @@ main(argc, argv) } if (argc >= 1) { - strncpy(myname, *argv, MAXHOSTNAMELEN); + strlcpy(myname, *argv, sizeof(myname)); } else { if(gethostname(myname, MAXHOSTNAMELEN)) { perror("gethostname failure"); @@ -109,53 +84,32 @@ main(argc, argv) if (!quiet) printf("Hostname: %s\n", myname); - - /* Set the hosts db to close each time - effectively rewinding file */ - sethostent(0); - - if((host = gethostbyname (myname)) == NULL) { + memset(&hint, 0, sizeof(hint)); + hint.ai_flags = AI_CANONNAME; + err = getaddrinfo(myname, 0, &hint, &ai); + if (err) { fprintf(stderr, "Could not look up address for hostname '%s' - fatal\n", myname); exit(2); } - fqdn = strdup(host->h_name); - if (fqdn == NULL) { - perror("strdup"); - exit(2); + if (!quiet) { + addrstr = inet_ntop(ai->ai_family, ai->ai_addr, abuf, sizeof(abuf)); + if (addrstr != NULL) + printf("Host address: %s\n", addrstr); } - ptr = host->h_addr_list[0]; -#define UC(a) (((int)a)&0xff) - if (!quiet) - printf("Host address: %d.%d.%d.%d\n", - UC(ptr[0]), UC(ptr[1]), UC(ptr[2]), UC(ptr[3])); - - memcpy(&addrcopy.s_addr, ptr, 4); - - /* Convert back to full name */ - if ((host = gethostbyaddr(&addrcopy.s_addr, 4, AF_INET)) == NULL) { - if (!quiet) - fprintf(stderr, "Error looking up IP address\n"); - } else { - free(fqdn); - fqdn = strdup(host->h_name); - if (fqdn == NULL) { - perror("strdup"); - exit (2); - } - } + err = getnameinfo(ai->ai_addr, ai->ai_addrlen, namebuf, sizeof(namebuf), + NULL, 0, NI_NAMEREQD); + if (err && !quiet) + fprintf(stderr, "Error looking up IP address\n"); - if (quiet) - printf("%s\n", fqdn); - else - printf("FQDN: %s\n", fqdn); + printf("%s%s\n", quiet ? "" : "FQDN: ", err ? ai->ai_canonname : namebuf); if (!quiet) printf("Resolve library appears to have passed the test\n"); - /* All ok */ - exit(0); - + freeaddrinfo(ai); + return 0; }