[gnome-keyring] egg: Add methods for creating scratch directory in tests
- From: Stefan Walter <stefw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-keyring] egg: Add methods for creating scratch directory in tests
- Date: Wed, 27 Jun 2012 09:42:17 +0000 (UTC)
commit 09f5a69c00f5ef3e4928b6751ae8ebc86c3bfe2b
Author: Stef Walter <stefw gnome org>
Date: Wed Jun 27 10:58:40 2012 +0200
egg: Add methods for creating scratch directory in tests
* Add egg_tests_create_scratch_directory() and
egg_tests_remove_scratch_directory() methods, and use
them in tests.
egg/Makefile.am | 1 +
egg/egg-testing.c | 79 +++++++++++++++++++++++
egg/egg-testing.h | 7 ++-
pkcs11/gnome2-store/tests/test-gnome2-storage.c | 56 ++--------------
pkcs11/gnome2-store/tests/test-import.c | 22 +------
pkcs11/secret-store/tests/mock-secret-module.c | 31 ++--------
pkcs11/xdg-store/tests/mock-xdg-module.c | 32 ++-------
7 files changed, 107 insertions(+), 121 deletions(-)
---
diff --git a/egg/Makefile.am b/egg/Makefile.am
index f024ae0..2e8d133 100644
--- a/egg/Makefile.am
+++ b/egg/Makefile.am
@@ -131,6 +131,7 @@ libegg_hex_la_LIBS = \
$(GLIB_LIBS)
libegg_test_la_SOURCES = \
+ egg-mkdtemp.c egg-mkdtemp.h \
egg-testing.c egg-testing.h
libegg_test_la_CFLAGS = \
diff --git a/egg/egg-testing.c b/egg/egg-testing.c
index 041d6f4..8c0f55a 100644
--- a/egg/egg-testing.c
+++ b/egg/egg-testing.c
@@ -23,8 +23,11 @@
#include "config.h"
+#include "egg-mkdtemp.h"
#include "egg-testing.h"
+#include <glib/gstdio.h>
+
#include <errno.h>
#include <unistd.h>
@@ -151,3 +154,79 @@ egg_tests_run_in_thread_with_loop (void)
return GPOINTER_TO_INT (ret);
}
+
+static void
+copy_scratch_file (const gchar *filename,
+ const gchar *directory)
+{
+ GError *error = NULL;
+ gchar *basename;
+ gchar *contents;
+ gchar *destination;
+ gsize length;
+
+ g_assert (directory);
+
+ g_file_get_contents (filename, &contents, &length, &error);
+ g_assert_no_error (error);
+
+ basename = g_path_get_basename (filename);
+ destination = g_build_filename (directory, basename, NULL);
+ g_free (basename);
+
+ g_file_set_contents (destination, contents, length, &error);
+ g_assert_no_error (error);
+ g_free (destination);
+ g_free (contents);
+}
+
+gchar *
+egg_tests_create_scratch_directory (const gchar *file_to_copy,
+ ...)
+{
+ gchar *basename;
+ gchar *directory;
+ va_list va;
+
+ basename = g_path_get_basename (g_get_prgname ());
+ directory = g_strdup_printf ("/tmp/scratch-%s.XXXXXX", basename);
+ g_free (basename);
+
+ if (!egg_mkdtemp (directory))
+ g_assert_not_reached ();
+
+ va_start (va, file_to_copy);
+
+ while (file_to_copy != NULL) {
+ copy_scratch_file (file_to_copy, directory);
+ file_to_copy = va_arg (va, const gchar *);
+ }
+
+ va_end (va);
+
+ return directory;
+}
+
+void
+egg_tests_remove_scratch_directory (const gchar *directory)
+{
+ GDir *dir;
+ GError *error = NULL;
+ const gchar *name;
+ gchar *filename;
+
+ dir = g_dir_open (directory, 0, &error);
+ g_assert_no_error (error);
+
+ while ((name = g_dir_read_name (dir)) != NULL) {
+ filename = g_build_filename (directory, name, NULL);
+ if (g_unlink (filename) < 0)
+ g_assert_not_reached ();
+ g_free (filename);
+ }
+
+ g_dir_close (dir);
+
+ if (g_rmdir (directory) < 0)
+ g_assert_not_reached ();
+}
diff --git a/egg/egg-testing.h b/egg/egg-testing.h
index 5b4c911..d92dbc4 100644
--- a/egg/egg-testing.h
+++ b/egg/egg-testing.h
@@ -56,4 +56,9 @@ gboolean egg_test_wait_until (int timeout);
gint egg_tests_run_in_thread_with_loop (void);
-#endif /* EGG_DH_H_ */
+gchar * egg_tests_create_scratch_directory (const gchar *file_to_copy,
+ ...) G_GNUC_NULL_TERMINATED;
+
+void egg_tests_remove_scratch_directory (const gchar *directory);
+
+#endif /* EGG_TESTS_H_ */
diff --git a/pkcs11/gnome2-store/tests/test-gnome2-storage.c b/pkcs11/gnome2-store/tests/test-gnome2-storage.c
index 10d6d30..a18f904 100644
--- a/pkcs11/gnome2-store/tests/test-gnome2-storage.c
+++ b/pkcs11/gnome2-store/tests/test-gnome2-storage.c
@@ -33,7 +33,7 @@
#include "gkm/gkm-test.h"
#include "egg/egg-libgcrypt.h"
-#include "egg/egg-mkdtemp.h"
+#include "egg/egg-testing.h"
#include <glib/gstdio.h>
@@ -56,38 +56,13 @@ typedef struct {
#define MSEC(x) ((x) * 1000)
static void
-copy_scratch_file (Test *test,
- const gchar *name)
-{
- GError *error = NULL;
- gchar *filename;
- gchar *contents;
- gsize length;
-
- g_assert (test->directory);
-
- filename = g_build_filename (SRCDIR, "files", name, NULL);
- g_file_get_contents (filename, &contents, &length, &error);
- g_assert_no_error (error);
- g_free (filename);
-
- filename = g_build_filename (test->directory, name, NULL);
- g_file_set_contents (filename, contents, length, &error);
- g_assert_no_error (error);
- g_free (filename);
-}
-
-static void
setup_directory (Test *test,
gconstpointer unused)
{
- test->directory = g_strdup ("/tmp/gkd-test.XXXXXX");
- if (!egg_mkdtemp (test->directory))
- g_assert_not_reached ();
-
- /* Copy in a valid set of storage data */
- copy_scratch_file (test, "Thawte_Personal_Premium_CA.cer");
- copy_scratch_file (test, "user.keystore");
+ test->directory = egg_tests_create_scratch_directory (
+ SRCDIR "/files/Thawte_Personal_Premium_CA.cer",
+ SRCDIR "/files/user.keystore",
+ NULL);
}
static void
@@ -163,26 +138,7 @@ static void
teardown_directory (Test *test,
gconstpointer unused)
{
- GDir *dir;
- GError *error = NULL;
- const gchar *name;
- gchar *filename;
-
- dir = g_dir_open (test->directory, 0, &error);
- g_assert_no_error (error);
-
- while ((name = g_dir_read_name (dir)) != NULL) {
- filename = g_build_filename (test->directory, name, NULL);
- if (g_unlink (filename) < 0)
- g_assert_not_reached ();
- g_free (filename);
- }
-
- g_dir_close (dir);
-
- if (g_rmdir (test->directory) < 0)
- g_assert_not_reached ();
-
+ egg_tests_remove_scratch_directory (test->directory);
g_free (test->directory);
}
diff --git a/pkcs11/gnome2-store/tests/test-import.c b/pkcs11/gnome2-store/tests/test-import.c
index ac6a9df..56b71bb 100644
--- a/pkcs11/gnome2-store/tests/test-import.c
+++ b/pkcs11/gnome2-store/tests/test-import.c
@@ -23,7 +23,7 @@
#include "config.h"
-#include "egg/egg-mkdtemp.h"
+#include "egg/egg-testing.h"
#include "gkm/gkm-module.h"
#include "gkm/gkm-test.h"
@@ -57,9 +57,7 @@ setup (Test *test,
GList *modules;
CK_RV rv;
- test->directory = g_strdup ("/tmp/gkd-test.XXXXXX");
- if (!egg_mkdtemp (test->directory))
- g_assert_not_reached ();
+ test->directory = egg_tests_create_scratch_directory (NULL, NULL);
memset (&args, 0, sizeof (args));
args.flags = CKF_OS_LOCKING_OK;
@@ -93,10 +91,6 @@ static void
teardown (Test *test,
gconstpointer unused)
{
- GError *error = NULL;
- const gchar *name;
- gchar *filename;
- GDir *dir;
CK_RV rv;
g_list_free_full (test->importers, g_object_unref);
@@ -107,17 +101,7 @@ teardown (Test *test,
gkm_assert_cmprv (rv, ==, CKR_OK);
/* Cleanup the directory */
- dir = g_dir_open (test->directory, 0, &error);
- g_assert_no_error (error);
- while ((name = g_dir_read_name (dir)) != NULL) {
- filename = g_build_filename (test->directory, name, NULL);
- if (g_unlink (filename) < 0)
- g_assert_not_reached ();
- g_free (filename);
- }
- g_dir_close (dir);
- if (g_rmdir (test->directory) < 0)
- g_assert_not_reached ();
+ egg_tests_remove_scratch_directory (test->directory);
g_free (test->directory);
}
diff --git a/pkcs11/secret-store/tests/mock-secret-module.c b/pkcs11/secret-store/tests/mock-secret-module.c
index 7022e69..9b10fee 100644
--- a/pkcs11/secret-store/tests/mock-secret-module.c
+++ b/pkcs11/secret-store/tests/mock-secret-module.c
@@ -35,8 +35,8 @@
#include "secret-store/gkm-secret-object.h"
#include "secret-store/gkm-secret-store.h"
-#include "egg/egg-mkdtemp.h"
#include "egg/egg-secure-memory.h"
+#include "egg/egg-testing.h"
#include <glib.h>
@@ -50,26 +50,6 @@ static gchar *directory = NULL;
GkmModule* _gkm_secret_store_get_module_for_testing (void);
GMutex* _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *module);
-static void
-copy_file_to_directory (const gchar *from, const gchar *directory)
-{
- gchar *filename;
- gchar *basename;
- gchar *data;
- gsize n_data;
-
- if (!g_file_get_contents (from, &data, &n_data, NULL))
- g_error ("couldn't read: %s", from);
-
- basename = g_path_get_basename (from);
- filename = g_build_filename (directory, basename, NULL);
- if (!g_file_set_contents (filename, data, n_data, NULL))
- g_error ("couldn't write: %s", filename);
- g_free (filename);
- g_free (basename);
- g_free (data);
-}
-
GkmModule*
test_secret_module_initialize_and_enter (void)
{
@@ -79,7 +59,10 @@ test_secret_module_initialize_and_enter (void)
gchar *string;
CK_RV rv;
- directory = egg_mkdtemp (g_strdup ("/tmp/mock-secret-XXXXXX"));
+ directory = egg_tests_create_scratch_directory (
+ SRCDIR "/files/encrypted.keyring",
+ SRCDIR "/files/plain.keyring",
+ NULL);
/* Setup test directory to work in */
memset (&args, 0, sizeof (args));
@@ -87,10 +70,6 @@ test_secret_module_initialize_and_enter (void)
args.pReserved = string;
args.flags = CKF_OS_LOCKING_OK;
- /* Copy files from test-data to scratch */
- copy_file_to_directory (SRCDIR "/files/encrypted.keyring", directory);
- copy_file_to_directory (SRCDIR "/files/plain.keyring", directory);
-
funcs = gkm_secret_store_get_functions ();
rv = (funcs->C_Initialize) (&args);
g_return_val_if_fail (rv == CKR_OK, NULL);
diff --git a/pkcs11/xdg-store/tests/mock-xdg-module.c b/pkcs11/xdg-store/tests/mock-xdg-module.c
index a3b8cb9..6f19a2a 100644
--- a/pkcs11/xdg-store/tests/mock-xdg-module.c
+++ b/pkcs11/xdg-store/tests/mock-xdg-module.c
@@ -28,8 +28,8 @@
#include "xdg-store/gkm-xdg-store.h"
-#include "egg/egg-mkdtemp.h"
#include "egg/egg-secure-memory.h"
+#include "egg/egg-testing.h"
#include "gkm/gkm-session.h"
#include "gkm/gkm-module.h"
@@ -49,26 +49,6 @@ static gchar *directory = NULL;
GkmModule* _gkm_xdg_store_get_module_for_testing (void);
GMutex* _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *module);
-static void
-copy_file_to_directory (const gchar *from, const gchar *directory)
-{
- gchar *filename;
- gchar *basename;
- gchar *data;
- gsize n_data;
-
- if (!g_file_get_contents (from, &data, &n_data, NULL))
- g_error ("couldn't read: %s", from);
-
- basename = g_path_get_basename (from);
- filename = g_build_filename (directory, basename, NULL);
- if (!g_file_set_contents (filename, data, n_data, NULL))
- g_error ("couldn't write: %s", filename);
- g_free (filename);
- g_free (basename);
- g_free (data);
-}
-
void
mock_xdg_module_remove_file (const gchar *name)
{
@@ -129,7 +109,10 @@ mock_xdg_module_initialize_and_enter (void)
gchar *string;
CK_RV rv;
- directory = egg_mkdtemp (g_strdup ("/tmp/mock-secret-XXXXXX"));
+ directory = egg_tests_create_scratch_directory (
+ SRCDIR "/files/test-refer-1.trust",
+ SRCDIR "/files/test-certificate-1.cer",
+ NULL);
/* Setup test directory to work in */
memset (&args, 0, sizeof (args));
@@ -138,9 +121,7 @@ mock_xdg_module_initialize_and_enter (void)
args.flags = CKF_OS_LOCKING_OK;
/* Copy files from test-data to scratch */
- copy_file_to_directory (SRCDIR "/files/test-refer-1.trust", directory);
- copy_file_to_directory (SRCDIR "/files/test-certificate-1.cer", directory);
- mock_xdg_module_empty_file ("invalid-without-ext");
+ mock_xdg_module_empty_file ("invalid-without-ext");
mock_xdg_module_empty_file ("test-file.unknown");
mock_xdg_module_empty_file ("test-invalid.trust");
@@ -171,6 +152,7 @@ mock_xdg_module_leave_and_finalize (void)
rv = (funcs->C_Finalize) (NULL);
g_return_if_fail (rv == CKR_OK);
+ egg_tests_remove_scratch_directory (directory);
g_free (directory);
directory = NULL;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]