From 40d6644264db8a581800a524176cece34e90a1e1 Mon Sep 17 00:00:00 2001 From: Andrew Deason Date: Thu, 2 Jul 2020 19:16:40 -0500 Subject: [PATCH] tests: Introduce afstest_*_path() Currently, several of our tests contain logic to locate files (via srcdir or objdir), based on the C_TAP_SOURCE/BUILD environment variables. This logic is duplicated in several places, so consolidate the code into a couple of new functions: afstest_src_path and afstest_obj_path. Update all callers to use these new functions. Change-Id: I67a5e5d7f8fd7a1edb55a45e52d877ac41f9a2ea Reviewed-on: https://gerrit.openafs.org/14319 Reviewed-by: Michael Meffie Reviewed-by: Cheyenne Wills Reviewed-by: Benjamin Kaduk Tested-by: BuildBot --- tests/auth/keys-t.c | 9 +---- tests/cmd/Makefile.in | 2 +- tests/cmd/command-t.c | 18 +++------- tests/common/Makefile.in | 2 +- tests/common/common.h | 5 +++ tests/common/files.c | 76 ++++++++++++++++++++++++++++++++++++++++ tests/common/servers.c | 9 ++--- tests/volser/vos-t.c | 13 ++----- 8 files changed, 94 insertions(+), 40 deletions(-) create mode 100644 tests/common/files.c diff --git a/tests/auth/keys-t.c b/tests/auth/keys-t.c index 2f5ee17008..50f3ab5e84 100644 --- a/tests/auth/keys-t.c +++ b/tests/auth/keys-t.c @@ -126,14 +126,7 @@ int main(int argc, char **argv) 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 */ - if (getenv("C_TAP_SOURCE") == NULL) { - keyfilesrc = strdup("KeyFile"); - } else { - if (asprintf(&keyfilesrc, "%s/auth/KeyFile", getenv("C_TAP_SOURCE")) == -1) - goto out; - } + keyfilesrc = afstest_src_path("tests/auth/KeyFile"); /* First, copy in a known keyfile */ code = copy(keyfilesrc, keyfile); diff --git a/tests/cmd/Makefile.in b/tests/cmd/Makefile.in index a6752c1e48..ede656e342 100644 --- a/tests/cmd/Makefile.in +++ b/tests/cmd/Makefile.in @@ -5,7 +5,7 @@ abs_top_builddir=@abs_top_builddir@ include @TOP_OBJDIR@/src/config/Makefile.config include @TOP_OBJDIR@/src/config/Makefile.pthread -MODULE_CFLAGS = -I$(TOP_OBJDIR) +MODULE_CFLAGS = -I$(TOP_OBJDIR) -I$(srcdir)/../common/ LIBS = $(abs_top_builddir)/tests/common/libafstest_common.la \ $(abs_top_builddir)/src/cmd/liboafs_cmd.la \ diff --git a/tests/cmd/command-t.c b/tests/cmd/command-t.c index 7b285f921e..4252648f7e 100644 --- a/tests/cmd/command-t.c +++ b/tests/cmd/command-t.c @@ -34,6 +34,7 @@ #include #include +#include "common.h" enum cmdOptions { copt_flag = 0, @@ -361,19 +362,10 @@ main(int argc, char **argv) cmd_FreeArgv(tv); /* Now, try adding a configuration file into the mix */ - if (getenv("C_TAP_SOURCE") == NULL) - path = strdup("test1.conf"); - else { - if (asprintf(&path, "%s/cmd/test1.conf", getenv("C_TAP_SOURCE")) < 0) - path = NULL; - } - if (path != NULL) { - cmd_SetCommandName("test"); - code = cmd_OpenConfigFile(path); - is_int(0, code, "cmd_OpenConfigFile succeeds"); - } else { - skip("no memory to build config file path"); - } + path = afstest_src_path("tests/cmd/test1.conf"); + cmd_SetCommandName("test"); + code = cmd_OpenConfigFile(path); + is_int(0, code, "cmd_OpenConfigFile succeeds"); code = cmd_ParseLine("-first 1", tv, &tc, 100); is_int(0, code, "cmd_ParseLine succeeds"); diff --git a/tests/common/Makefile.in b/tests/common/Makefile.in index 5866977985..2c2894d25b 100644 --- a/tests/common/Makefile.in +++ b/tests/common/Makefile.in @@ -6,7 +6,7 @@ include @TOP_OBJDIR@/src/config/Makefile.libtool MODULE_CFLAGS=-I$(TOP_OBJDIR) -LT_objs = config.lo misc.lo network.lo rxkad.lo servers.lo ubik.lo +LT_objs = config.lo files.lo misc.lo network.lo rxkad.lo servers.lo ubik.lo LT_libs = $(LIB_rfc3961) $(LIB_roken) LT_deps = $(top_builddir)/tests/tap/libafstest_tap.la \ $(top_builddir)/src/util/liboafs_util.la diff --git a/tests/common/common.h b/tests/common/common.h index 6b5a153edf..5baddfc3d8 100644 --- a/tests/common/common.h +++ b/tests/common/common.h @@ -30,6 +30,11 @@ extern char *afstest_mkdtemp(char *template); struct afsconf_dir; extern int afstest_AddDESKeyFile(struct afsconf_dir *dir); +/* files.c */ + +extern char *afstest_src_path(char *path); +extern char *afstest_obj_path(char *path); + /* rxkad.c */ extern struct rx_securityClass diff --git a/tests/common/files.c b/tests/common/files.c new file mode 100644 index 0000000000..6c13eddb38 --- /dev/null +++ b/tests/common/files.c @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2020 Sine Nomine Associates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/*! + * Common file-related functions for test programs + */ + +#include +#include +#include + +#include +#include + +#include + +#include "common.h" + +static char * +path_from_tdir(char *env_var, char *filename) +{ + char *tdir; + + /* C_TAP_SOURCE/C_TAP_BUILD in the env points to 'tests/' in the + * srcdir/objdir. */ + + tdir = getenv(env_var); + if (tdir == NULL) { + /* + * If C_TAP_SOURCE/C_TAP_BUILD isn't set, we assume we're running from + * the same cwd as one of the test programs (e.g. 'tests/foo/'). So to + * get to 'tests/', just go up one level. + */ + tdir = ".."; + } + + /* + * The given 'filename' is specified relative to the top srcdir/objdir. + * Since 'tdir' points to 'tests/', go up one level before following + * 'filename'. + */ + return afstest_asprintf("%s/../%s", tdir, filename); +} + +char * +afstest_src_path(char *path) +{ + return path_from_tdir("C_TAP_SOURCE", path); +} + +char * +afstest_obj_path(char *path) +{ + return path_from_tdir("C_TAP_BUILD", path); +} diff --git a/tests/common/servers.c b/tests/common/servers.c index c20368f94e..ddff2e7b42 100644 --- a/tests/common/servers.c +++ b/tests/common/servers.c @@ -31,15 +31,10 @@ afstest_StartVLServer(char *dirname, pid_t *serverPid) exit(1); /* Argggggghhhhh */ } else if (pid == 0) { - char *binPath, *logPath, *dbPath, *build; + char *binPath, *logPath, *dbPath; /* Child */ - build = getenv("C_TAP_BUILD"); - - if (build == NULL) - build = ".."; - - binPath = afstest_asprintf("%s/../src/tvlserver/vlserver", build); + binPath = afstest_obj_path("src/tvlserver/vlserver"); logPath = afstest_asprintf("%s/VLLog", dirname); dbPath = afstest_asprintf("%s/vldb", dirname); diff --git a/tests/volser/vos-t.c b/tests/volser/vos-t.c index 05ec8fd75a..bfe3ae8db9 100644 --- a/tests/volser/vos-t.c +++ b/tests/volser/vos-t.c @@ -63,21 +63,14 @@ TestListAddrs(struct ubik_client *client, char *dirname) } pid = fork(); if (pid == 0) { - char *build, *binPath; + char *vos; dup2(outpipe[1], STDOUT_FILENO); /* Redirect stdout into pipe */ close(outpipe[0]); close(outpipe[1]); - build = getenv("C_TAP_BUILD"); - if (build == NULL) - build = ".."; - - if (asprintf(&binPath, "%s/../src/volser/vos", build) < 0) { - fprintf(stderr, "Out of memory building vos arguments\n"); - exit(1); - } - execl(binPath, "vos", + vos = afstest_obj_path("src/volser/vos"); + execl(vos, "vos", "listaddrs", "-config", dirname, "-noauth", "-noresolve", NULL); exit(1); }