From 0a528a52f5da5e225567f3b9deab9f7d08022f9f Mon Sep 17 00:00:00 2001 From: Marc Dionne Date: Sun, 17 Feb 2013 13:29:38 -0500 Subject: [PATCH] tests: Improve failure mode for unresolvable hostname In the case of a host where gethostbyname is unable to resolve the hostname, afstest_BuildTestConfig() may return NULL which can cause several tests to crash. Add a common function to look out for this condition and use it where appropriate. When it occurs, the current module is skipped and the user gets an error message that indicates the configuration problem. Change-Id: I7216876eb2424368f415e5759e2b95009ad055b2 Reviewed-on: http://gerrit.openafs.org/9120 Reviewed-by: Derrick Brashear Tested-by: BuildBot Reviewed-by: Jeffrey Altman --- tests/auth/Makefile.in | 16 ++++++++-------- tests/auth/authcon-t.c | 2 ++ tests/auth/keys-t.c | 2 ++ tests/auth/realms-t.c | 2 ++ tests/auth/superuser-t.c | 2 ++ tests/common/common.h | 1 + tests/common/config.c | 1 + tests/common/network.c | 19 +++++++++++++++++++ tests/volser/vos-t.c | 6 ++++-- 9 files changed, 41 insertions(+), 10 deletions(-) diff --git a/tests/auth/Makefile.in b/tests/auth/Makefile.in index 4cdb639..ed4ed27 100644 --- a/tests/auth/Makefile.in +++ b/tests/auth/Makefile.in @@ -15,22 +15,22 @@ MODULE_LIBS = ../tap/libtap.a \ $(LIB_rfc3961) $(LIB_roken) \ $(XLIBS) -authcon-t: authcon-t.o ../common/config.o - $(LT_LDRULE_static) authcon-t.o ../common/config.o \ +authcon-t: authcon-t.o ../common/config.o ../common/network.o + $(LT_LDRULE_static) authcon-t.o ../common/config.o ../common/network.o \ $(MODULE_LIBS) superuser-t: superuser-t.o ../common/config.o ../common/rxkad.o \ - test.cs.o test.ss.o test.xdr.o + test.cs.o test.ss.o test.xdr.o ../common/network.o $(LT_LDRULE_static) superuser-t.o ../common/config.o \ ../common/rxkad.o ../common/servers.o \ - test.cs.o test.ss.o test.xdr.o \ + test.cs.o test.ss.o test.xdr.o ../common/network.o \ $(MODULE_LIBS) -keys-t: keys-t.o ../common/config.o - $(LT_LDRULE_static) keys-t.o ../common/config.o $(MODULE_LIBS) +keys-t: keys-t.o ../common/config.o ../common/network.o + $(LT_LDRULE_static) keys-t.o ../common/config.o ../common/network.o $(MODULE_LIBS) -realms-t: realms-t.o ../common/config.o - $(LT_LDRULE_static) realms-t.o ../common/config.o $(MODULE_LIBS) +realms-t: realms-t.o ../common/config.o ../common/network.o + $(LT_LDRULE_static) realms-t.o ../common/config.o ../common/network.o $(MODULE_LIBS) writekeyfile: writekeyfile.o $(LT_LDRULE_static) writekeyfile.o $(MODULE_LIBS) diff --git a/tests/auth/authcon-t.c b/tests/auth/authcon-t.c index 46cf3c9..e029774 100644 --- a/tests/auth/authcon-t.c +++ b/tests/auth/authcon-t.c @@ -51,6 +51,8 @@ main(int argc, char **argv) struct afsconf_typedKey *key; int code = 0; + afstest_SkipTestsIfBadHostname(); + plan(9); dirname = afstest_BuildTestConfig(); diff --git a/tests/auth/keys-t.c b/tests/auth/keys-t.c index 2f08e08..5c483c5 100644 --- a/tests/auth/keys-t.c +++ b/tests/auth/keys-t.c @@ -106,6 +106,8 @@ int main(int argc, char **argv) int code; int i; + afstest_SkipTestsIfBadHostname(); + plan(134); /* Create a temporary afs configuration directory */ diff --git a/tests/auth/realms-t.c b/tests/auth/realms-t.c index cdd15da..04fac4e 100644 --- a/tests/auth/realms-t.c +++ b/tests/auth/realms-t.c @@ -359,6 +359,8 @@ test_update_config_files(void) int main(int argc, char **argv) { + afstest_SkipTestsIfBadHostname(); + plan(113); test_edges(); diff --git a/tests/auth/superuser-t.c b/tests/auth/superuser-t.c index 03f67d9..bdba611 100644 --- a/tests/auth/superuser-t.c +++ b/tests/auth/superuser-t.c @@ -373,6 +373,8 @@ int main(int argc, char **argv) int code; int ret = 0; + afstest_SkipTestsIfBadHostname(); + /* Start the client and the server if requested */ if (argc == 3 ) { diff --git a/tests/common/common.h b/tests/common/common.h index cf19692..c9f7349 100644 --- a/tests/common/common.h +++ b/tests/common/common.h @@ -54,3 +54,4 @@ extern int afstest_GetUbikClient(struct afsconf_dir *dir, char *service, /* network.c */ extern int afstest_IsLoopbackNetworkDefault(void); extern int afstest_SkipTestsIfLoopbackNetIsDefault(void); +extern void afstest_SkipTestsIfBadHostname(void); diff --git a/tests/common/config.c b/tests/common/config.c index 4783753..faefc00 100644 --- a/tests/common/config.c +++ b/tests/common/config.c @@ -35,6 +35,7 @@ #include +#include #include "common.h" static FILE * diff --git a/tests/common/network.c b/tests/common/network.c index 5611003..474fc61 100644 --- a/tests/common/network.c +++ b/tests/common/network.c @@ -22,6 +22,9 @@ afstest_IsLoopbackNetworkDefault(void) gethostname(hostname, sizeof(hostname)); host = gethostbyname(hostname); + if (!host) { + skip_all("Can't resolve hostname %s\n", hostname); + } memcpy(&addr, host->h_addr, sizeof(addr)); return(rx_IsLoopbackAddr(ntohl(addr))); @@ -42,3 +45,19 @@ afstest_SkipTestsIfLoopbackNetIsDefault(void) } return retval; } + +/*! + * Skips all TAP tests if the current machine's hostname can't be resolved + * to any IP address. + */ +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); +} diff --git a/tests/volser/vos-t.c b/tests/volser/vos-t.c index e3c499c..3919c0b 100644 --- a/tests/volser/vos-t.c +++ b/tests/volser/vos-t.c @@ -95,11 +95,13 @@ main(int argc, char **argv) struct ubik_client *ubikClient = NULL; int ret = 0; - plan(6); - + /* Skip all tests if the current hostname can't be resolved */ + afstest_SkipTestsIfBadHostname(); /* Skip all tests if the current hostname is on the loopback network */ afstest_SkipTestsIfLoopbackNetIsDefault(); + plan(6); + code = rx_Init(0); dirname = afstest_BuildTestConfig(); -- 1.9.4