tests: Introduce afstest_MyHostAddr

Several places resolve the local hostname into an IP address.
Consolidate these into a single function (gethostaddr), and add the
function afstest_MyHostAddr, which caches the IP and bails if we can't
resolve our hostname.

Change-Id: I7f71cd136796e4395c639eed8dd8eb19a7b9beec
Reviewed-on: https://gerrit.openafs.org/14802
Reviewed-by: Cheyenne Wills <cwills@sinenomine.net>
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>
This commit is contained in:
Andrew Deason 2021-04-24 16:08:38 -05:00 committed by Benjamin Kaduk
parent f29ca90311
commit c3df91f303
3 changed files with 48 additions and 25 deletions

View File

@ -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);

View File

@ -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");

View File

@ -9,6 +9,30 @@
#include <tests/tap/basic.h>
#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;
}