summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStef Walter <stef@thewalter.net>2013-07-17 09:51:32 +0200
committerStef Walter <stef@thewalter.net>2013-07-18 08:13:20 +0200
commit81a6e16539e5e4a27c55194ae095cc4a75d08ade (patch)
tree70e409e938e60c326f95d85ac059efb58f48286a
parenteb8f5859b1349f8147ba47a1da8032df192f2370 (diff)
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.c44
-rw-r--r--common/test.c57
-rw-r--r--common/test.h2
-rw-r--r--common/tests/test-path.c31
-rw-r--r--trust/tests/test-bundle.c4
-rw-r--r--trust/tests/test-cer.c4
-rw-r--r--trust/tests/test-module.c4
-rw-r--r--trust/tests/test-openssl.c4
-rw-r--r--trust/tests/test-save.c4
-rw-r--r--trust/tests/test-token.c9
-rw-r--r--trust/tests/test-trust.c6
11 files changed, 73 insertions, 96 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
diff --git a/trust/tests/test-bundle.c b/trust/tests/test-bundle.c
index 10f89d2..397787f 100644
--- a/trust/tests/test-bundle.c
+++ b/trust/tests/test-bundle.c
@@ -78,9 +78,7 @@ setup (void *unused)
p11_extract_info_init (&test.ex);
- test.directory = p11_path_expand ("$TEMP/test-extract.XXXXXX");
- if (!mkdtemp (test.directory))
- assert_not_reached ();
+ test.directory = p11_test_directory ("test-extract");
}
static void
diff --git a/trust/tests/test-cer.c b/trust/tests/test-cer.c
index fc4d007..846cabf 100644
--- a/trust/tests/test-cer.c
+++ b/trust/tests/test-cer.c
@@ -78,9 +78,7 @@ setup (void *unused)
p11_extract_info_init (&test.ex);
- test.directory = p11_path_expand ("$TEMP/test-extract.XXXXXX");
- if (!mkdtemp (test.directory))
- assert_fail ("mkdtemp() failed", test.directory);
+ test.directory = p11_test_directory ("test-extract");
}
static void
diff --git a/trust/tests/test-module.c b/trust/tests/test-module.c
index 80747da..5920076 100644
--- a/trust/tests/test-module.c
+++ b/trust/tests/test-module.c
@@ -138,9 +138,7 @@ setup_writable (void *unused)
rv = C_GetFunctionList (&test.module);
assert (rv == CKR_OK);
- test.directory = p11_path_expand ("$TEMP/test-module.XXXXXX");
- if (!mkdtemp (test.directory))
- assert_not_reached ();
+ test.directory = p11_test_directory ("test-module");
memset (&args, 0, sizeof (args));
if (asprintf (&arguments, "paths='%s'", test.directory) < 0)
diff --git a/trust/tests/test-openssl.c b/trust/tests/test-openssl.c
index 1396102..f31a41a 100644
--- a/trust/tests/test-openssl.c
+++ b/trust/tests/test-openssl.c
@@ -81,9 +81,7 @@ setup (void *unused)
p11_extract_info_init (&test.ex);
- test.directory = p11_path_expand ("$TEMP/test-extract.XXXXXX");
- if (!mkdtemp (test.directory))
- assert_not_reached ();
+ test.directory = p11_test_directory ("test-extract");
}
static void
diff --git a/trust/tests/test-save.c b/trust/tests/test-save.c
index eba5632..be16141 100644
--- a/trust/tests/test-save.c
+++ b/trust/tests/test-save.c
@@ -63,9 +63,7 @@ struct {
static void
setup (void *unused)
{
- test.directory = p11_path_expand ("$TEMP/test-extract.XXXXXX");
- if (!mkdtemp (test.directory))
- assert_fail ("mkdtemp() failed", strerror (errno));
+ test.directory = p11_test_directory ("test-extract");
}
static void
diff --git a/trust/tests/test-token.c b/trust/tests/test-token.c
index 6b998ca..3b7d701 100644
--- a/trust/tests/test-token.c
+++ b/trust/tests/test-token.c
@@ -76,10 +76,7 @@ setup (void *path)
static void
setup_temp (void *unused)
{
- test.directory = p11_path_expand ("$TEMP/test-module.XXXXXX");
- if (!mkdtemp (test.directory))
- assert_not_reached ();
-
+ test.directory = p11_test_directory ("test-module");
setup (test.directory);
}
@@ -268,9 +265,7 @@ test_writable_no_exist (void)
p11_token *token;
char *path;
- directory = p11_path_expand ("$TEMP/test-module.XXXXXX");
- if (!mkdtemp (directory))
- assert_not_reached ();
+ directory = p11_test_directory ("test-module");
path = p11_path_build (directory, "subdir", NULL);
assert (path != NULL);
diff --git a/trust/tests/test-trust.c b/trust/tests/test-trust.c
index 205a08a..8c69107 100644
--- a/trust/tests/test-trust.c
+++ b/trust/tests/test-trust.c
@@ -36,6 +36,8 @@
#include "attrs.h"
#include "debug.h"
+#include "message.h"
+#include "path.h"
#include "test.h"
#include "test-trust.h"
@@ -52,6 +54,10 @@
#include <string.h>
#include <unistd.h>
+#ifdef OS_UNIX
+#include <paths.h>
+#endif
+
void
test_check_object_msg (const char *file,
int line,