From: Simon Wilkinson Date: Thu, 24 Feb 2011 13:52:40 +0000 (+0000) Subject: tests/auth: Refactor common code X-Git-Tag: openafs-devel-1_7_1~892 X-Git-Url: https://git.openafs.org/?p=openafs.git;a=commitdiff_plain;h=1bc528d008fe11c43da6f6eef4210561bab33cbc tests/auth: Refactor common code Pull the common code for creating a configuration directory out of the keys and superuser tests into a single file. This both cleans up the existing tests, and makes it easier to add new ones. Change-Id: I08058117e08da3a3baf750b3b14ef6780f942206 Reviewed-on: http://gerrit.openafs.org/4049 Tested-by: BuildBot Reviewed-by: Derrick Brashear Tested-by: Derrick Brashear --- diff --git a/tests/auth/Makefile.in b/tests/auth/Makefile.in index 7414829..31d2a59 100644 --- a/tests/auth/Makefile.in +++ b/tests/auth/Makefile.in @@ -17,12 +17,13 @@ MODULE_LIBS = ../tap/libtap.a \ $(LIB_rfc3961) $(LIB_roken) -lafsutil\ $(XLIBS) -superuser-t: superuser-t.o test.cs.o test.ss.o test.xdr.o - $(AFS_LDRULE) superuser-t.o test.cs.o test.ss.o test.xdr.o \ +superuser-t: superuser-t.o common.o test.cs.o test.ss.o test.xdr.o + $(AFS_LDRULE) superuser-t.o common.o \ + test.cs.o test.ss.o test.xdr.o \ $(MODULE_LIBS) -keys-t: keys-t.o - $(AFS_LDRULE) keys-t.o $(MODULE_LIBS) +keys-t: keys-t.o common.o + $(AFS_LDRULE) keys-t.o common.o $(MODULE_LIBS) writekeyfile: writekeyfile.o $(AFS_LDRULE) writekeyfile.o $(MODULE_LIBS) @@ -39,7 +40,7 @@ test.xdr.c: test.xg test.h: test.xg $(RXGEN) -A -x -h -o $@ $(srcdir)/test.xg -superuser-t.o: test.h +superuser-t.o: test.h common.h clean: rm -f *.o *.cs.c *.ss.c *.xdr.c test.h \ diff --git a/tests/auth/common.c b/tests/auth/common.c new file mode 100644 index 0000000..fb95955 --- /dev/null +++ b/tests/auth/common.c @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2010 Your File System Inc. 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 functions for building a configuration directory + */ + +#include +#include +#include + +#include + +#include "common.h" + +static FILE * +openConfigFile(char *dirname, char *filename) { + char *path = NULL; + FILE *file; + + if (asprintf(&path, "%s/%s", dirname, filename) == -1) + return NULL; + + file = fopen(path, "w"); + free(path); + return file; +} + +static void +unlinkConfigFile(char *dirname, char *filename) { + char *path; + + if (asprintf(&path, "%s/%s", dirname, filename) != -1) { + unlink(path); + free(path); + } +} + +/*! + * Build a test configuration directory, containing a CellServDB and ThisCell + * file for the "example.org" cell + * + * @return + * The path to the configuration directory. This should be freed by the caller + * using free() + * + */ + +char * +buildTestConfig(void) { + char *dir = NULL; + FILE *file; + + if (asprintf(&dir, "%s/afs_XXXXXX", gettmpdir()) == -1) + goto fail; + + if (mkdtemp(dir) == NULL) + goto fail; + + /* Create a CellServDB */ + file = openConfigFile(dir, "CellServDB"); + fprintf(file, ">example.org # An example cell\n"); + fprintf(file, "127.0.0.1 #test.example.org\n"); + fclose(file); + + /* Create a ThisCell file */ + file = openConfigFile(dir, "ThisCell"); + fprintf(file, "example.org"); + fclose(file); + + return dir; + +fail: + if (dir) + free(dir); + return NULL; +} + +/*! + * Delete at test configuration directory + */ + +void +unlinkTestConfig(char *dir) +{ + unlinkConfigFile(dir, "KeyFile"); + unlinkConfigFile(dir, "CellServDB"); + unlinkConfigFile(dir, "ThisCell"); + unlinkConfigFile(dir, "UserList"); + rmdir(dir); +} diff --git a/tests/auth/common.h b/tests/auth/common.h new file mode 100644 index 0000000..afe0b67 --- /dev/null +++ b/tests/auth/common.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2010 Your File System Inc. 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. + */ + +extern char *buildTestConfig(void); +extern void unlinkTestConfig(char *); diff --git a/tests/auth/keys-t.c b/tests/auth/keys-t.c index 49281dc..d02d655 100644 --- a/tests/auth/keys-t.c +++ b/tests/auth/keys-t.c @@ -38,6 +38,9 @@ #include +#include "test.h" +#include "common.h" + static int copy(char *inFile, char *outFile) { @@ -88,7 +91,6 @@ keyMatches(struct afsconf_typedKey *typedKey, memcmp(keyMaterial, buffer->val, buffer->len) == 0); } - int main(int argc, char **argv) { struct afsconf_dir *dir; @@ -97,10 +99,8 @@ int main(int argc, char **argv) struct rx_opaque *keyMaterial; struct afsconf_typedKey *typedKey; struct afsconf_typedKeyList *typedKeyList; - - char buffer[1024]; - char *dirEnd; - FILE *file; + char *dirname; + char *keyfile; afs_int32 kvno; int code; int i; @@ -108,33 +108,19 @@ int main(int argc, char **argv) plan(122); /* Create a temporary afs configuration directory */ - snprintf(buffer, sizeof(buffer), "%s/afs_XXXXXX", gettmpdir()); - mkdtemp(buffer); - dirEnd = buffer + strlen(buffer); - - /* Create a CellServDB file */ - strcpy(dirEnd, "/CellServDB"); - file = fopen(buffer, "w"); - fprintf(file, ">example.org # An example cell\n"); - fprintf(file, "127.0.0.1 #test.example.org\n"); - fclose(file); - - /* Create a ThisCell file */ - strcpy(dirEnd, "/ThisCell"); - file = fopen(buffer, "w"); - fprintf(file, "example.org\n"); - fclose(file); - - /* Firstly, copy in a known keyfile. */ - strcpy(dirEnd, "/KeyFile"); - code = copy("KeyFile", buffer); - if (code) + + dirname = buildTestConfig(); + + if (asprintf(&keyfile, "%s/KeyFile", dirname) == -1) goto out; - *dirEnd='\0'; + /* First, copy in a known keyfile */ + code = copy("KeyFile", keyfile); + if (code) + goto out; /* Start with a blank configuration directory */ - dir = afsconf_Open(strdup(buffer)); + dir = afsconf_Open(dirname); ok(dir != NULL, "Sucessfully re-opened config directory"); if (dir == NULL) goto out; @@ -238,8 +224,7 @@ int main(int argc, char **argv) * still have the same KeyFile */ afsconf_Close(dir); - *dirEnd='\0'; - dir = afsconf_Open(strdup(buffer)); + dir = afsconf_Open(dirname); ok(dir != NULL, "Sucessfully re-opened config directory"); if (dir == NULL) goto out; @@ -336,14 +321,12 @@ int main(int argc, char **argv) is_int(AFSCONF_NOTFOUND, code, " ... and is really gone"); /* Unlink the KeyFile */ - strcpy(dirEnd, "/KeyFile"); - unlink(buffer); + unlink(keyfile); /* Force a rebuild of the directory structure, just in case */ afsconf_Close(dir); - *dirEnd='\0'; - dir = afsconf_Open(strdup(buffer)); + dir = afsconf_Open(dirname); ok(dir != NULL, "Sucessfully re-opened config directory"); if (dir == NULL) goto out; @@ -518,8 +501,7 @@ int main(int argc, char **argv) */ afsconf_Close(dir); - *dirEnd='\0'; - dir = afsconf_Open(strdup(buffer)); + dir = afsconf_Open(dirname); ok(dir != NULL, "Sucessfully re-opened config directory"); if (dir == NULL) goto out; @@ -542,16 +524,7 @@ int main(int argc, char **argv) afsconf_PutTypedKeyList(&typedKeyList); out: - strcpy(dirEnd, "/KeyFile"); - unlink(buffer); - strcpy(dirEnd, "/CellServDB"); - unlink(buffer); - strcpy(dirEnd, "/ThisCell"); - unlink(buffer); - strcpy(dirEnd, "/UserList"); - unlink(buffer); - *dirEnd='\0'; - rmdir(buffer); + unlinkTestConfig(dirname); return 0; } diff --git a/tests/auth/superuser-t.c b/tests/auth/superuser-t.c index 339d190..bc46fab 100644 --- a/tests/auth/superuser-t.c +++ b/tests/auth/superuser-t.c @@ -44,6 +44,7 @@ #include #include "test.h" +#include "common.h" #define TEST_PORT 1234 @@ -423,11 +424,9 @@ startServer(char *configPath) int main(int argc, char **argv) { struct afsconf_dir *dir; - char buffer[1024]; + char *dirname; int serverPid, clientPid, waited, stat; char keymaterial[]="\x19\x17\xff\xe6\xbb\x77\x2e\xfc"; - char *dirEnd; - FILE *file; int code; /* Start the client and the server if requested */ @@ -448,26 +447,9 @@ int main(int argc, char **argv) /* Otherwise, do the basic configuration, then start the client and * server */ - snprintf(buffer, sizeof(buffer), "%s/afs_XXXXXX", gettmpdir()); - mkdtemp(buffer); - dirEnd = buffer + strlen(buffer); - - /* Create a CellServDB file */ - strcpy(dirEnd, "/CellServDB"); - file = fopen(buffer, "w"); - fprintf(file, ">example.org # An example cell\n"); - fprintf(file, "127.0.0.1 #test.example.org\n"); - fclose(file); - - /* Create a ThisCell file */ - strcpy(dirEnd, "/ThisCell"); - file = fopen(buffer, "w"); - fprintf(file, "example.org\n"); - fclose(file); - - *dirEnd='\0'; - /* Start with a blank configuration directory */ - dir = afsconf_Open(strdup(buffer)); + dirname = buildTestConfig(); + + dir = afsconf_Open(dirname); if (dir == NULL) { fprintf(stderr, "Unable to configure directory.\n"); exit(1); @@ -482,12 +464,12 @@ int main(int argc, char **argv) exit(1); } - printf("Config directory is %s\n", buffer); + printf("Config directory is %s\n", dirname); serverPid = fork(); if (serverPid == -1) { /* Bang */ } else if (serverPid == 0) { - execl(argv[0], argv[0], "-server", buffer, NULL); + execl(argv[0], argv[0], "-server", dirname, NULL); exit(1); } clientPid = fork(); @@ -496,7 +478,7 @@ int main(int argc, char **argv) waitpid(serverPid, &stat, 0); exit(1); } else if (clientPid == 0) { - execl(argv[0], argv[0], "-client", buffer, NULL); + execl(argv[0], argv[0], "-client", dirname, NULL); } do { @@ -512,16 +494,7 @@ int main(int argc, char **argv) /* Client and server are both done, so cleanup after everything */ - strcpy(dirEnd, "/KeyFile"); - unlink(buffer); - strcpy(dirEnd, "/CellServDB"); - unlink(buffer); - strcpy(dirEnd, "/ThisCell"); - unlink(buffer); - strcpy(dirEnd, "/UserList"); - unlink(buffer); - *dirEnd='\0'; - rmdir(buffer); + /* unlinkTestConfig(dirname); */ return 0; }