diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/path.c | 44 | ||||
-rw-r--r-- | common/test.c | 57 | ||||
-rw-r--r-- | common/test.h | 2 | ||||
-rw-r--r-- | common/tests/test-path.c | 31 |
4 files changed, 60 insertions, 74 deletions
diff --git a/common/path.c b/common/path.c index 818befc..0ff1431 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 @@ -135,41 +134,6 @@ expand_homedir (const char *remainder) } } -static char * -expand_tempdir (const char *remainder) -{ - const char *env; - - if (remainder[0] == '\0') - remainder = NULL; - - 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 inline bool is_path_component_or_null (char ch) { @@ -189,14 +153,6 @@ p11_path_expand (const char *path) is_path_component_or_null (path[1])) { return expand_homedir (path + 1); - } else if (strncmp (path, "$HOME", 5) == 0 && - is_path_component_or_null (path[5])) { - return expand_homedir (path + 5); - - } else if (strncmp (path, "$TEMP", 5) == 0 && - is_path_component_or_null (path[5])) { - return expand_tempdir (path + 5); - } else { return strdup (path); } diff --git a/common/test.c b/common/test.c index c72cb7d..0f5c451 100644 --- a/common/test.c +++ b/common/test.c @@ -38,8 +38,10 @@ #include "test.h" #include "debug.h" +#include "path.h" #include <assert.h> +#include <errno.h> #include <setjmp.h> #include <stdarg.h> #include <stdio.h> @@ -274,3 +276,58 @@ p11_test_run (int argc, gl.number = 0; return ret; } + +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)) { + printf ("# couldn't lookup temp directory\n"); + errno = ENOTDIR; + return NULL; + } + + return p11_path_build (directory, name, NULL); + +#endif /* OS_WIN32 */ + } +} + +char * +p11_test_directory (const char *prefix) +{ + char *templ; + char *directory; + + if (asprintf (&templ, "%s.XXXXXX", prefix) < 0) + assert_not_reached (); + + directory = expand_tempdir (templ); + assert (directory != NULL); + + if (!mkdtemp (directory)) { + printf ("# couldn't create temp directory: %s: %s\n", + directory, strerror (errno)); + free (directory); + assert_not_reached (); + } + + free (templ); + return directory; +} diff --git a/common/test.h b/common/test.h index 1da3608..7354595 100644 --- a/common/test.h +++ b/common/test.h @@ -128,4 +128,6 @@ void p11_fixture (void (* setup) (void *), int p11_test_run (int argc, char **argv); +char * p11_test_directory (const char *prefix); + #endif /* P11_TEST_H_ */ diff --git a/common/tests/test-path.c b/common/tests/test-path.c index 1f85dbb..0077cd0 100644 --- a/common/tests/test-path.c +++ b/common/tests/test-path.c @@ -114,50 +114,21 @@ test_expand (void) #ifdef OS_UNIX putenv ("HOME=/home/blah"); check_equals_and_free ("/home/blah/my/path", - p11_path_expand ("$HOME/my/path")); - check_equals_and_free ("/home/blah/my/path", p11_path_expand ("~/my/path")); check_equals_and_free ("/home/blah", - p11_path_expand ("$HOME")); - check_equals_and_free ("/home/blah", p11_path_expand ("~")); - putenv ("TEMP=/tmpdir"); - check_equals_and_free ("/tmpdir/my/path", - p11_path_expand ("$TEMP/my/path")); - check_equals_and_free ("/tmpdir", - p11_path_expand ("$TEMP")); #else /* OS_WIN32 */ putenv ("HOME=C:\\Users\\blah"); check_equals_and_free ("C:\\Users\\blah\\path", - p11_path_expand ("$HOME/path")); - check_equals_and_free ("C:\\Users\\blah\\path", - p11_path_expand ("$HOME\\path")); - check_equals_and_free ("C:\\Users\\blah\\path", - p11_path_expand ("~/path")); + p11_path_expand ("~/my/path")); check_equals_and_free ("C:\\Users\\blah\\path", p11_path_expand ("~\\path")); - - putenv ("TEMP=C:\\Temp Directory"); - check_equals_and_free ("C:\\Temp Directory\\path", - p11_path_expand ("$TEMP/path")); - check_equals_and_free ("C:\\Temp Directory\\path", - p11_path_expand ("$TEMP\\path")); #endif putenv("HOME="); - path = p11_path_expand ("$HOME/this/is/my/path"); - assert (strstr (path, "this/is/my/path") != NULL); - free (path); - - putenv("HOME="); path = p11_path_expand ("~/this/is/my/path"); assert (strstr (path, "this/is/my/path") != NULL); free (path); - - putenv("TEMP="); - path = p11_path_expand ("$TEMP/this/is/my/path"); - assert (strstr (path, "this/is/my/path") != NULL); - free (path); } static void |