From d16a0f8d166c91122d75e6f648c9c7577cc22ee3 Mon Sep 17 00:00:00 2001 From: Andrew Deason Date: Wed, 14 Apr 2021 16:09:37 -0500 Subject: [PATCH] tests: Introduce afstest_asprintf Add a thin wrapper around asprintf, called afstest_asprintf (and afstest_vasprintf), which does its own error checking. This just helps makes tests a little less cluttered when needing to construct strings. Adapt all asprintf callers in 'tests' to use the wrapper. Change-Id: I6c4ae5b72af827e2c4c66ecfc57f152855b1d401 Reviewed-on: https://gerrit.openafs.org/14620 Reviewed-by: Michael Meffie Reviewed-by: Cheyenne Wills Reviewed-by: Benjamin Kaduk Tested-by: BuildBot --- tests/auth/keys-t.c | 3 +-- tests/auth/writekeyfile.c | 2 +- tests/common/common.h | 3 +++ tests/common/config.c | 13 +++++-------- tests/common/misc.c | 22 ++++++++++++++++++++++ tests/common/servers.c | 10 ++++------ 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/tests/auth/keys-t.c b/tests/auth/keys-t.c index f0556ce..2f5ee17 100644 --- a/tests/auth/keys-t.c +++ b/tests/auth/keys-t.c @@ -124,8 +124,7 @@ int main(int argc, char **argv) dirname = afstest_BuildTestConfig(); - if (asprintf(&keyfile, "%s/KeyFile", dirname) == -1) - goto out; + keyfile = afstest_asprintf("%s/KeyFile", dirname); /* Work out the path to our KeyFile. If the test harness hasn't set * the C_TAP_SOURCE environment variable, then assume it is in our CWD */ diff --git a/tests/auth/writekeyfile.c b/tests/auth/writekeyfile.c index 4a07e61..50eb399 100644 --- a/tests/auth/writekeyfile.c +++ b/tests/auth/writekeyfile.c @@ -43,7 +43,7 @@ main(int argc, char **argv) afsconf_Close(dir); /* Copy out the resulting keyfile into our homedirectory */ - opr_Verify(asprintf(&keyfile, "%s/KeyFile", dirname) > 0); + keyfile = afstest_asprintf("%s/KeyFile", dirname); in = open(keyfile, O_RDONLY); out = open("KeyFile", O_WRONLY | O_CREAT, 0644); diff --git a/tests/common/common.h b/tests/common/common.h index 88bd745..6b5a153 100644 --- a/tests/common/common.h +++ b/tests/common/common.h @@ -60,3 +60,6 @@ extern void afstest_SkipTestsIfServerRunning(char *name); /* misc.c */ extern char *afstest_GetProgname(char **argv); +extern char *afstest_vasprintf(const char *fmt, va_list ap); +extern char *afstest_asprintf(const char *fmt, ...) + AFS_ATTRIBUTE_FORMAT(__printf__, 1, 2); diff --git a/tests/common/config.c b/tests/common/config.c index debd71e..a678fe4 100644 --- a/tests/common/config.c +++ b/tests/common/config.c @@ -43,8 +43,7 @@ openConfigFile(char *dirname, char *filename) { char *path = NULL; FILE *file; - if (asprintf(&path, "%s/%s", dirname, filename) == -1) - return NULL; + path = afstest_asprintf("%s/%s", dirname, filename); file = fopen(path, "w"); free(path); @@ -55,10 +54,9 @@ static void unlinkConfigFile(char *dirname, char *filename) { char *path; - if (asprintf(&path, "%s/%s", dirname, filename) != -1) { - unlink(path); - free(path); - } + path = afstest_asprintf("%s/%s", dirname, filename); + unlink(path); + free(path); } /*! @@ -103,8 +101,7 @@ afstest_BuildTestConfig(void) { char hostname[255]; struct in_addr iaddr; - if (asprintf(&dir, "%s/afs_XXXXXX", gettmpdir()) == -1) - goto fail; + dir = afstest_asprintf("%s/afs_XXXXXX", gettmpdir()); if (afstest_mkdtemp(dir) == NULL) goto fail; diff --git a/tests/common/misc.c b/tests/common/misc.c index b2703d0..84f181b 100644 --- a/tests/common/misc.c +++ b/tests/common/misc.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "common.h" @@ -46,3 +47,24 @@ afstest_GetProgname(char **argv) } return argv[0]; } + +char * +afstest_vasprintf(const char *fmt, va_list ap) +{ + char *str; + if (vasprintf(&str, fmt, ap) < 0) { + sysbail("vasprintf"); + } + return str; +} + +char * +afstest_asprintf(const char *fmt, ...) +{ + char *str; + va_list ap; + va_start(ap, fmt); + str = afstest_vasprintf(fmt, ap); + va_end(ap); + return str; +} diff --git a/tests/common/servers.c b/tests/common/servers.c index 27bec51..c20368f 100644 --- a/tests/common/servers.c +++ b/tests/common/servers.c @@ -39,12 +39,10 @@ afstest_StartVLServer(char *dirname, pid_t *serverPid) if (build == NULL) build = ".."; - if (asprintf(&binPath, "%s/../src/tvlserver/vlserver", build) < 0 || - asprintf(&logPath, "%s/VLLog", dirname) < 0 || - asprintf(&dbPath, "%s/vldb", dirname) < 0) { - fprintf(stderr, "Out of memory building vlserver arguments\n"); - exit(1); - } + binPath = afstest_asprintf("%s/../src/tvlserver/vlserver", build); + logPath = afstest_asprintf("%s/VLLog", dirname); + dbPath = afstest_asprintf("%s/vldb", dirname); + execl(binPath, "vlserver", "-logfile", logPath, "-database", dbPath, "-config", dirname, NULL); fprintf(stderr, "Running %s failed\n", binPath); -- 1.9.4