diff options
author | Stef Walter <stef@thewalter.net> | 2013-07-17 09:51:32 +0200 |
---|---|---|
committer | Stef Walter <stef@thewalter.net> | 2013-07-18 09:34:14 +0200 |
commit | e75007c5dfa15b92359b91c5c141680475c0192b (patch) | |
tree | 0d7d75ba4e22eb96bc62442fc543548d58cf064d | |
parent | b1a94ad1ccc1af1ec3d54d47ffb86b84885b066e (diff) | |
download | p11-kit-e75007c5dfa15b92359b91c5c141680475c0192b.tar.gz |
tools: Use $TMPDIR instead of $TEMP
TMPDIR is a more standard environment variable for locating the
temp directory on Unix. In addition since this is only used in
tests, remove the code from the generic p11_path_expand() func.
In general remove the possibility for forks to put $HOME or $TEMP
environment variables in configured paths. This was possible
due to code in p11_path_expand() but not something we supported.
https://bugzilla.redhat.com/show_bug.cgi?id=985017
-rw-r--r-- | common/path.c | 68 | ||||
-rw-r--r-- | common/tests/test-path.c | 29 | ||||
-rw-r--r-- | tools/tests/Makefile.am | 2 | ||||
-rw-r--r-- | tools/tests/test-openssl.c | 4 | ||||
-rw-r--r-- | tools/tests/test-pem.c | 4 | ||||
-rw-r--r-- | tools/tests/test-save.c | 4 | ||||
-rw-r--r-- | tools/tests/test-x509.c | 4 | ||||
-rw-r--r-- | tools/tests/test.c | 58 | ||||
-rw-r--r-- | tools/tests/test.h | 2 |
9 files changed, 85 insertions, 90 deletions
diff --git a/common/path.c b/common/path.c index bba2c23..f03b156 100644 --- a/common/path.c +++ b/common/path.c @@ -49,7 +49,6 @@ #include <string.h> #ifdef OS_UNIX -#include <paths.h> #include <pwd.h> #include <unistd.h> #endif @@ -92,11 +91,26 @@ p11_path_base (const char *path) return strndup (beg, end - beg); } +static inline bool +is_path_component_or_null (char ch) +{ + return (ch == '\0' || ch == '/' +#ifdef OS_WIN32 + || ch == '\\' +#endif + ); +} + static char * expand_homedir (const char *remainder) { const char *env; + while (remainder[0] && is_path_component_or_null (remainder[0])) + remainder++; + if (remainder[0] == '\0') + remainder = NULL; + env = getenv ("HOME"); if (env && env[0]) { return p11_path_build (env, remainder, NULL); @@ -132,48 +146,6 @@ expand_homedir (const char *remainder) } } -static char * -expand_tempdir (const char *remainder) -{ - const char *env; - - env = getenv ("TEMP"); - if (env && env[0]) { - return p11_path_build (env, remainder, NULL); - - } else { -#ifdef OS_UNIX -#ifdef _PATH_TMP - return p11_path_build (_PATH_TMP, remainder, NULL); -#else - return p11_path_build ("/tmp", remainder, NULL); -#endif - -#else /* OS_WIN32 */ - char directory[MAX_PATH + 1]; - - if (!GetTempPathA (MAX_PATH + 1, directory)) { - p11_message ("couldn't lookup temp directory"); - errno = ENOTDIR; - return NULL; - } - - return p11_path_build (directory, remainder, NULL); - -#endif /* OS_WIN32 */ - } -} - -static bool -is_path_component_or_null (char ch) -{ - return (ch == '0' || ch == '/' -#ifdef OS_WIN32 - || ch == '\\' -#endif - ); -} - char * p11_path_expand (const char *path) { @@ -181,15 +153,7 @@ p11_path_expand (const char *path) if (strncmp (path, "~", 1) == 0 && is_path_component_or_null (path[1])) { - return expand_homedir (path + 2); - - } else if (strncmp (path, "$HOME", 5) == 0 && - is_path_component_or_null (path[5])) { - return expand_homedir (path + 6); - - } else if (strncmp (path, "$TEMP", 5) == 0 && - is_path_component_or_null (path[5])) { - return expand_tempdir (path + 6); + return expand_homedir (path + 1); } else { return strdup (path); diff --git a/common/tests/test-path.c b/common/tests/test-path.c index 8263d1f..bb9f03b 100644 --- a/common/tests/test-path.c +++ b/common/tests/test-path.c @@ -125,44 +125,23 @@ test_expand (CuTest *tc) #ifdef OS_UNIX putenv ("HOME=/home/blah"); check_equals_and_free (tc, "/home/blah/my/path", - p11_path_expand ("$HOME/my/path")); - check_equals_and_free (tc, "/home/blah/my/path", p11_path_expand ("~/my/path")); - putenv ("TEMP=/tmpdir"); - check_equals_and_free (tc, "/tmpdir/my/path", - p11_path_expand ("$TEMP/my/path")); + check_equals_and_free (tc, "/home/blah", + p11_path_expand ("~")); + check_equals_and_free (tc, "/home/blah", + p11_path_expand ("~///")); #else /* OS_WIN32 */ putenv ("HOME=C:\\Users\\blah"); check_equals_and_free (tc, "C:\\Users\\blah\\path", - p11_path_expand ("$HOME/path")); - check_equals_and_free (tc, "C:\\Users\\blah\\path", - p11_path_expand ("$HOME\\path")); - check_equals_and_free (tc, "C:\\Users\\blah\\path", p11_path_expand ("~/path")); check_equals_and_free (tc, "C:\\Users\\blah\\path", p11_path_expand ("~\\path")); - - putenv ("TEMP=C:\\Temp Directory"); - check_equals_and_free (tc, "C:\\Temp Directory\\path", - p11_path_expand ("$TEMP/path")); - check_equals_and_free (tc, "C:\\Temp Directory\\path", - p11_path_expand ("$TEMP\\path")); #endif putenv("HOME="); - path = p11_path_expand ("$HOME/this/is/my/path"); - CuAssertTrue (tc, strstr (path, "this/is/my/path") != NULL); - free (path); - - putenv("HOME="); path = p11_path_expand ("~/this/is/my/path"); CuAssertTrue (tc, strstr (path, "this/is/my/path") != NULL); free (path); - - putenv("TEMP="); - path = p11_path_expand ("$TEMP/this/is/my/path"); - CuAssertTrue (tc, strstr (path, "this/is/my/path") != NULL); - free (path); } static void diff --git a/tools/tests/Makefile.am b/tools/tests/Makefile.am index ad06446..6229cfb 100644 --- a/tools/tests/Makefile.am +++ b/tools/tests/Makefile.am @@ -21,11 +21,11 @@ INCLUDES = \ $(NULL) LDADD = \ + $(builddir)/libtestcommon.la \ $(top_builddir)/p11-kit/libp11-kit.la \ $(top_builddir)/common/libp11-data.la \ $(top_builddir)/common/libp11-mock.la \ $(top_builddir)/common/libp11-common.la \ - $(builddir)/libtestcommon.la \ $(LIBTASN1_LIBS) \ $(LTLIBINTL) \ $(CUTEST_LIBS) \ diff --git a/tools/tests/test-openssl.c b/tools/tests/test-openssl.c index 215e0da..45ed29a 100644 --- a/tools/tests/test-openssl.c +++ b/tools/tests/test-openssl.c @@ -79,9 +79,7 @@ setup (CuTest *tc) p11_extract_info_init (&test.ex); - test.directory = p11_path_expand ("$TEMP/test-extract.XXXXXX"); - if (!mkdtemp (test.directory)) - assert_not_reached (); + test.directory = test_temp_directory ("test-extract.XXXXXX"); } static void diff --git a/tools/tests/test-pem.c b/tools/tests/test-pem.c index dc1cb08..680886a 100644 --- a/tools/tests/test-pem.c +++ b/tools/tests/test-pem.c @@ -76,9 +76,7 @@ setup (CuTest *tc) p11_extract_info_init (&test.ex); - test.directory = p11_path_expand ("$TEMP/test-extract.XXXXXX"); - if (!mkdtemp (test.directory)) - assert_not_reached (); + test.directory = test_temp_directory ("test-extract.XXXXXX"); } static void diff --git a/tools/tests/test-save.c b/tools/tests/test-save.c index b739c21..f26d577 100644 --- a/tools/tests/test-save.c +++ b/tools/tests/test-save.c @@ -63,9 +63,7 @@ struct { static void setup (CuTest *tc) { - test.directory = p11_path_expand ("$TEMP/test-extract.XXXXXX"); - if (!mkdtemp (test.directory)) - CuFail (tc, "mkdtemp() failed"); + test.directory = test_temp_directory ("test-extract.XXXXXX"); } static void diff --git a/tools/tests/test-x509.c b/tools/tests/test-x509.c index e952e53..2a9f5f2 100644 --- a/tools/tests/test-x509.c +++ b/tools/tests/test-x509.c @@ -76,9 +76,7 @@ setup (CuTest *tc) p11_extract_info_init (&test.ex); - test.directory = p11_path_expand ("$TEMP/test-extract.XXXXXX"); - if (!mkdtemp (test.directory)) - CuFail (tc, "mkdtemp() failed"); + test.directory = test_temp_directory ("test-extract.XXXXXX"); } static void diff --git a/tools/tests/test.c b/tools/tests/test.c index 4ba2162..589e7e9 100644 --- a/tools/tests/test.c +++ b/tools/tests/test.c @@ -36,17 +36,24 @@ #include "CuTest.h" #include "debug.h" +#include "message.h" +#include "path.h" #include "test.h" #include <sys/stat.h> #include <assert.h> #include <dirent.h> +#include <errno.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <unistd.h> +#ifdef OS_UNIX +#include <paths.h> +#endif + static char * read_file (CuTest *tc, const char *file, @@ -207,3 +214,54 @@ test_check_directory_msg (CuTest *tc, p11_dict_free (files); } + +static char * +expand_tempdir (const char *name) +{ + const char *env; + + env = getenv ("TMPDIR"); + if (env && env[0]) { + return p11_path_build (env, name, NULL); + + } else { +#ifdef OS_UNIX +#ifdef _PATH_TMP + return p11_path_build (_PATH_TMP, name, NULL); +#else + return p11_path_build ("/tmp", name, NULL); +#endif + +#else /* OS_WIN32 */ + char directory[MAX_PATH + 1]; + + if (!GetTempPathA (MAX_PATH + 1, directory)) { + p11_message ("couldn't lookup temp directory"); + errno = ENOTDIR; + return NULL; + } + + return p11_path_build (directory, name, NULL); + +#endif /* OS_WIN32 */ + } +} + +char * +test_temp_directory (const char *templ) +{ + char *directory; + + directory = expand_tempdir (templ); + if (directory == NULL) + return NULL; + + if (!mkdtemp (directory)) { + p11_message ("couldn't create temp directory: %s: %s", + directory, strerror (errno)); + free (directory); + assert (0 && "not reached"); + } + + return directory; +} diff --git a/tools/tests/test.h b/tools/tests/test.h index de2bdc1..487ef0d 100644 --- a/tools/tests/test.h +++ b/tools/tests/test.h @@ -257,4 +257,6 @@ void test_check_directory_msg (CuTest *tc, (test_check_directory_msg (tc, __FILE__, __LINE__, directory, \ test_check_directory_files files)) +char * test_temp_directory (const char *prefix); + #endif /* TEST_COMMON_H_ */ |