]> granicus.if.org Git - p11-kit/commitdiff
tools: Use $TMPDIR instead of $TEMP
authorStef Walter <stef@thewalter.net>
Wed, 17 Jul 2013 07:51:32 +0000 (09:51 +0200)
committerStef Walter <stef@thewalter.net>
Thu, 18 Jul 2013 06:13:20 +0000 (08:13 +0200)
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

common/path.c
common/test.c
common/test.h
common/tests/test-path.c
trust/tests/test-bundle.c
trust/tests/test-cer.c
trust/tests/test-module.c
trust/tests/test-openssl.c
trust/tests/test-save.c
trust/tests/test-token.c
trust/tests/test-trust.c

index 818befcca65a5e17ed3f8857848630ca5dd30aea..0ff143175cbb1b7f3ac6a754182927ad2beae124 100644 (file)
@@ -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);
        }
index c72cb7d893a00000d46db3a747f6384d46e7006c..0f5c4516b3b0ec13217434e633ec63a86a0e3397 100644 (file)
 
 #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;
+}
index 1da3608344ecd814a7fa749aa12236547ed393ce..735459517dd09794a5959a4a6da9034f4ea4efce 100644 (file)
@@ -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_ */
index 1f85dbbfa4a70b5fdd59db4600b33e55b3252ea9..0077cd05507c89336ddab8818fc7845c3b27c67f 100644 (file)
@@ -113,51 +113,22 @@ 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
index 10f89d2a86eab83649ff7ba98072daaba0473252..397787f3430a51b5fd0705aa2790a5057656c5ed 100644 (file)
@@ -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
index fc4d0079e60ce7e0dd93f29d5601bae66b3b7bfd..846cabfce051e05eb3a4629ac74b8bc059e727b7 100644 (file)
@@ -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
index 80747da0046378b39070933f52e5c419e9aeb496..59200761388635323b445c5d585cc80bd7db77bf 100644 (file)
@@ -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)
index 1396102473abfb9c6b53742ab21e92ed44fc5255..f31a41afef5a644363cfaa5500f3720435e1395c 100644 (file)
@@ -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
index eba5632668d1ac23a792aabc2296de0289eddbe6..be16141691e3324b2e68c03079b4b183e3609262 100644 (file)
@@ -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
index 6b998ca25cdedb08851c1b21fbfd9f18e6c6e9af..3b7d701177c1927147d088758f634209a76c8103 100644 (file)
@@ -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);
index 205a08a0f278164db74ee62ee66b88f28263d5ef..8c6910718e3680d6de60c0db16644344eb4bea51 100644 (file)
@@ -36,6 +36,8 @@
 
 #include "attrs.h"
 #include "debug.h"
+#include "message.h"
+#include "path.h"
 #include "test.h"
 
 #include "test-trust.h"
 #include <string.h>
 #include <unistd.h>
 
+#ifdef OS_UNIX
+#include <paths.h>
+#endif
+
 void
 test_check_object_msg (const char *file,
                        int line,