diff --git a/tests/common/common.h b/tests/common/common.h index 921b3665a4..797343a28b 100644 --- a/tests/common/common.h +++ b/tests/common/common.h @@ -82,6 +82,7 @@ extern int afstest_IsLoopbackNetworkDefault(void); extern int afstest_SkipTestsIfLoopbackNetIsDefault(void); extern void afstest_SkipTestsIfBadHostname(void); extern void afstest_SkipTestsIfServerRunning(char *name); +extern afs_uint32 afstest_MyHostAddr(void); /* misc.c */ extern char *afstest_GetProgname(char **argv); diff --git a/tests/common/config.c b/tests/common/config.c index 9841639359..3603b0035a 100644 --- a/tests/common/config.c +++ b/tests/common/config.c @@ -63,10 +63,10 @@ char * afstest_BuildTestConfig(void) { char *dir = NULL; FILE *file; - struct hostent *host; - char hostname[255]; struct in_addr iaddr; + memset(&iaddr, 0, sizeof(iaddr)); + dir = afstest_mkdtemp(); if (dir == NULL) { goto fail; @@ -75,13 +75,7 @@ afstest_BuildTestConfig(void) { /* Work out which IP address to use in our CellServDB. We figure this out * according to the IP address which ubik is most likely to pick for one of * our db servers */ - - gethostname(hostname, sizeof(hostname)); - host = gethostbyname(hostname); - if (!host) - return NULL; - - memcpy(&iaddr, host->h_addr, 4); + iaddr.s_addr = afstest_MyHostAddr(); file = openConfigFile(dir, "CellServDB"); fprintf(file, ">example.org # An example cell\n"); diff --git a/tests/common/network.c b/tests/common/network.c index c664505c19..37af0fe12c 100644 --- a/tests/common/network.c +++ b/tests/common/network.c @@ -9,6 +9,30 @@ #include #include "common.h" +static afs_uint32 +gethostaddr(void) +{ + char hostname[256]; + struct hostent *host; + static afs_uint32 addr = 0; + + if (addr != 0) { + return addr; + } + + memset(hostname, 0, sizeof(hostname)); + + gethostname(hostname, sizeof(hostname)); + host = gethostbyname(hostname); + if (host == NULL) { + diag("gethostbyname(%s) error: %d", hostname, h_errno); + return 0; + } + + memcpy(&addr, host->h_addr, sizeof(addr)); + return addr; +} + /*! * Check if the current machine's hostname resolves to the loopback * network. @@ -16,16 +40,10 @@ int afstest_IsLoopbackNetworkDefault(void) { - char hostname[MAXHOSTCHARS]; - afs_uint32 addr; - struct hostent *host; - - gethostname(hostname, sizeof(hostname)); - host = gethostbyname(hostname); - if (!host) { - skip_all("Can't resolve hostname %s\n", hostname); + afs_uint32 addr = gethostaddr(); + if (addr == 0) { + skip_all("Can't resolve hostname"); } - memcpy(&addr, host->h_addr, sizeof(addr)); return(rx_IsLoopbackAddr(ntohl(addr))); } @@ -53,13 +71,8 @@ afstest_SkipTestsIfLoopbackNetIsDefault(void) void afstest_SkipTestsIfBadHostname(void) { - char hostname[MAXHOSTCHARS]; - struct hostent *host; - - gethostname(hostname, sizeof(hostname)); - host = gethostbyname(hostname); - if (!host) - skip_all("Can't resolve hostname %s\n", hostname); + if (gethostaddr() == 0) + skip_all("Can't resolve hostname"); } /*! @@ -102,3 +115,18 @@ afstest_SkipTestsIfServerRunning(char *name) } close(sock); } + +/*! + * Get the IP address of the local machine, for use with mock CellServDBs, etc. + * + * @return ipv4 address in net-byte order + */ +afs_uint32 +afstest_MyHostAddr(void) +{ + afs_uint32 addr = gethostaddr(); + if (addr == 0) { + bail("cannot resolve hostname"); + } + return addr; +}