Skip to content

Commit

Permalink
Fix test suite issues affecting Travis images
Browse files Browse the repository at this point in the history
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.

(cherry picked from commit c2497d4)
  • Loading branch information
greghudson committed Dec 9, 2019
1 parent 6c672cf commit f8c72bc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 97 deletions.
14 changes: 1 addition & 13 deletions src/appl/simple/server/sim_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand All @@ -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");
Expand Down
19 changes: 10 additions & 9 deletions src/kadmin/testing/scripts/qualname.plin
Original file line number Diff line number Diff line change
@@ -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";

5 changes: 3 additions & 2 deletions src/tests/gssapi/t_ccselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@
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)
Expand Down
2 changes: 1 addition & 1 deletion src/tests/resolve/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
98 changes: 26 additions & 72 deletions src/tests/resolve/resolve.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,47 +42,22 @@

/* This program tests the resolve library and sees if it is broken... */

#include "autoconf.h"
#include <stdio.h>

#if STDC_HEADERS
#include <string.h>
#else
#ifndef HAVE_STRCHR
#define strchr index
#endif
char *strchr();
#endif

#include "k5-platform.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <netinet/in.h>
#include <netdb.h>

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) {
Expand All @@ -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");
Expand All @@ -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;
}

0 comments on commit f8c72bc

Please sign in to comment.