#include <string.h>
#ifdef OS_UNIX
-#include <paths.h>
#include <pwd.h>
#include <unistd.h>
#endif
}
}
-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)
{
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);
}
#include "test.h"
#include "debug.h"
+#include "path.h"
#include <assert.h>
+#include <errno.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
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;
+}
int p11_test_run (int argc,
char **argv);
+char * p11_test_directory (const char *prefix);
+
#endif /* P11_TEST_H_ */
#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
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
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
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)
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
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
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);
}
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);
#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,