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 <mmeffie@sinenomine.net>
Reviewed-by: Cheyenne Wills <cwills@sinenomine.net>
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>
Tested-by: BuildBot <buildbot@rampaginggeek.com>
This commit is contained in:
Andrew Deason 2021-04-14 16:09:37 -05:00 committed by Benjamin Kaduk
parent 405001be72
commit d16a0f8d16
6 changed files with 36 additions and 17 deletions

View File

@ -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 */

View File

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

View File

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

View File

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

View File

@ -29,6 +29,7 @@
#include <afsconfig.h>
#include <afs/param.h>
#include <roken.h>
#include <tests/tap/basic.h>
#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;
}

View File

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