[gnome-keyring/trust-store] [xdg-store] Add tests for better test coverage.



commit 49fa69251cf65c48d7f355e09f2cd2ac4a27d791
Author: Stef Walter <stefw collabora co uk>
Date:   Wed Nov 24 20:57:00 2010 +0000

    [xdg-store] Add tests for better test coverage.
    
    Test fiddling with files in the directory and seeing how
    the module reacts.

 pkcs11/xdg-store/gkm-xdg-module.c        |   21 +++-
 pkcs11/xdg-store/tests/Makefile.am       |    4 +-
 pkcs11/xdg-store/tests/test-xdg-module.c |  166 +++++++++++++++++++++++++++++-
 3 files changed, 184 insertions(+), 7 deletions(-)
---
diff --git a/pkcs11/xdg-store/gkm-xdg-module.c b/pkcs11/xdg-store/gkm-xdg-module.c
index 5d7fe6c..e173a5a 100644
--- a/pkcs11/xdg-store/gkm-xdg-module.c
+++ b/pkcs11/xdg-store/gkm-xdg-module.c
@@ -140,6 +140,13 @@ add_object_to_module (GkmXdgModule *self, GkmObject *object, const gchar *filena
 }
 
 static void
+remove_object_from_module (GkmXdgModule *self, GkmObject *object, const gchar *filename)
+{
+	g_assert (g_hash_table_lookup (self->objects_by_path, filename) == object);
+	g_hash_table_remove (self->objects_by_path, filename);
+}
+
+static void
 file_load (GkmFileTracker *tracker, const gchar *path, GkmXdgModule *self)
 {
 	GkmObject *object;
@@ -195,8 +202,10 @@ file_load (GkmFileTracker *tracker, const gchar *path, GkmXdgModule *self)
 
 	} else {
 		g_message ("failed to load file in user store: %s", path);
-		if (!added)
+		if (!added) {
 			gkm_object_expose (object, FALSE);
+			remove_object_from_module (self, object, path);
+		}
 	}
 
 	g_object_unref (object);
@@ -205,9 +214,14 @@ file_load (GkmFileTracker *tracker, const gchar *path, GkmXdgModule *self)
 static void
 file_remove (GkmFileTracker *tracker, const gchar *path, GkmXdgModule *self)
 {
+	GkmObject *object;
+
 	g_return_if_fail (path);
 	g_return_if_fail (GKM_IS_XDG_MODULE (self));
-	g_hash_table_remove (self->objects_by_path, path);
+
+	object = g_hash_table_lookup (self->objects_by_path, path);
+	if (object != NULL)
+		remove_object_from_module (self, object, path);
 }
 
 static gchar*
@@ -424,7 +438,8 @@ gkm_xdg_module_constructor (GType type, guint n_props, GObjectConstructParam *pr
 static void
 gkm_xdg_module_init (GkmXdgModule *self)
 {
-	self->objects_by_path = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
+	self->objects_by_path = g_hash_table_new_full (g_str_hash, g_str_equal,
+	                                               g_free, gkm_util_dispose_unref);
 
 	/* Our default token info, updated as module runs */
 	memcpy (&self->token_info, &user_module_token_info, sizeof (CK_TOKEN_INFO));
diff --git a/pkcs11/xdg-store/tests/Makefile.am b/pkcs11/xdg-store/tests/Makefile.am
index 7cf37d0..a2864c3 100644
--- a/pkcs11/xdg-store/tests/Makefile.am
+++ b/pkcs11/xdg-store/tests/Makefile.am
@@ -4,8 +4,8 @@ TESTING_SOURCES = \
 
 # Test files should be listed in order they need to run
 TESTING_FILES = \
-	test-xdg-trust.c \
-	test-xdg-module.c
+	test-xdg-module.c \
+	test-xdg-trust.c
 
 TESTING_LIBS =  \
 	$(top_builddir)/pkcs11/xdg-store/libgkm-xdg-store.la \
diff --git a/pkcs11/xdg-store/tests/test-xdg-module.c b/pkcs11/xdg-store/tests/test-xdg-module.c
index 4cc3335..8419759 100644
--- a/pkcs11/xdg-store/tests/test-xdg-module.c
+++ b/pkcs11/xdg-store/tests/test-xdg-module.c
@@ -26,8 +26,12 @@
 #include "test-xdg-module.h"
 #include "gkm-xdg-store.h"
 
+#include "gkm/gkm-session.h"
 #include "gkm/gkm-module.h"
 
+#include <errno.h>
+#include <sys/times.h>
+
 #include <string.h>
 
 static GMutex *mutex = NULL;
@@ -59,13 +63,54 @@ copy_scratch_file (const gchar *basename)
 static void
 empty_scratch_file (const gchar *basename)
 {
+	GError *err = NULL;
 	gchar *filename;
+
 	filename = testing_scratch_filename (basename);
-	if (!g_file_set_contents (filename, "", 0, NULL))
-		g_return_if_reached ();
+	if (!g_file_set_contents (filename, "", 0, &err))
+		g_assert_no_error (err);
+
+	g_free (filename);
+}
+
+static void
+touch_scratch_file (const gchar *basename, gint future)
+{
+	GError *err = NULL;
+	gchar *filename;
+	struct timeval tv;
+
+	filename = testing_scratch_filename (basename);
+
+	gettimeofday (&tv, NULL);
+	tv.tv_sec += future;
+
+	if (utimes (filename, &tv) < 0) {
+		err = g_error_new_literal (G_FILE_ERROR, g_file_error_from_errno (errno),
+		                           g_strerror (errno));
+		g_assert_no_error (err);
+	}
+
+	g_free (filename);
+}
+
+static void
+remove_scratch_file (const gchar *basename)
+{
+	GError *err = NULL;
+	gchar *filename;
+
+	filename = testing_scratch_filename (basename);
+	if (g_unlink (filename) < 0) {
+		err = g_error_new_literal (G_FILE_ERROR, g_file_error_from_errno (errno),
+		                           g_strerror (errno));
+		g_assert_no_error (err);
+	}
+
 	g_free (filename);
 }
 
+
 GkmModule*
 test_xdg_module_initialize_and_enter (void)
 {
@@ -85,6 +130,7 @@ test_xdg_module_initialize_and_enter (void)
 	copy_scratch_file ("test-refer-1.trust");
 	empty_scratch_file ("invalid-without-ext");
 	empty_scratch_file ("test-file.unknown");
+	empty_scratch_file ("test-invalid.trust");
 
 	funcs = gkm_xdg_store_get_functions ();
 	rv = (funcs->C_Initialize) (&args);
@@ -152,8 +198,124 @@ test_xdg_module_open_session (gboolean writable)
 	return session;
 }
 
+/* --------------------------------------------------------------------------------------
+ * MODULE TESTS
+ */
+
+static GkmModule *module = NULL;
+static GkmSession *session = NULL;
+
 TESTING_EXTERNAL(xdg_module)
 {
 	CK_FUNCTION_LIST_PTR funcs = gkm_xdg_store_get_functions ();
 	testing_test_p11_module (funcs, "p11-tests.conf");
 }
+
+TESTING_SETUP(xdg_module_setup)
+{
+	CK_RV rv;
+
+	module = test_xdg_module_initialize_and_enter ();
+	session = test_xdg_module_open_session (TRUE);
+
+	rv = gkm_module_C_Login (module, gkm_session_get_handle (session), CKU_USER, NULL, 0);
+	g_assert (rv == CKR_OK);
+}
+
+TESTING_TEARDOWN(xdg_module_teardown)
+{
+	test_xdg_module_leave_and_finalize ();
+	module = NULL;
+	session = NULL;
+}
+
+TESTING_TEST (xdg_module_find_twice_is_same)
+{
+	CK_OBJECT_HANDLE objects[256];
+	CK_ULONG n_objects;
+	CK_ULONG n_check;
+	CK_RV rv;
+
+	rv = gkm_session_C_FindObjectsInit (session, NULL, 0);
+	g_assert (rv == CKR_OK);
+	rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_objects);
+	g_assert (rv == CKR_OK);
+	rv = gkm_session_C_FindObjectsFinal (session);
+	g_assert (rv == CKR_OK);
+
+	gkm_assert_cmpulong (n_objects, >, 0);
+
+	/* Update the time on the file */
+	touch_scratch_file ("test-refer-1.trust", 1);
+
+	rv = gkm_session_C_FindObjectsInit (session, NULL, 0);
+	g_assert (rv == CKR_OK);
+	rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_check);
+	g_assert (rv == CKR_OK);
+	rv = gkm_session_C_FindObjectsFinal (session);
+	g_assert (rv == CKR_OK);
+
+	/* Should have same objects after reload */
+	gkm_assert_cmpulong (n_check, ==, n_objects);
+}
+
+TESTING_TEST (xdg_module_file_becomes_invalid)
+{
+	CK_OBJECT_HANDLE objects[256];
+	CK_ULONG n_objects;
+	CK_ULONG n_check;
+	CK_RV rv;
+
+	rv = gkm_session_C_FindObjectsInit (session, NULL, 0);
+	g_assert (rv == CKR_OK);
+	rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_objects);
+	g_assert (rv == CKR_OK);
+	rv = gkm_session_C_FindObjectsFinal (session);
+	g_assert (rv == CKR_OK);
+
+	gkm_assert_cmpulong (n_objects, >, 0);
+
+	/* Overwrite the file with empty */
+	empty_scratch_file ("test-refer-1.trust");
+	touch_scratch_file ("test-refer-1.trust", 2);
+
+	rv = gkm_session_C_FindObjectsInit (session, NULL, 0);
+	g_assert (rv == CKR_OK);
+	rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_check);
+	g_assert (rv == CKR_OK);
+	rv = gkm_session_C_FindObjectsFinal (session);
+	g_assert (rv == CKR_OK);
+
+	/* Should have less objects */
+	gkm_assert_cmpulong (n_check, <, n_objects);
+}
+
+TESTING_TEST (xdg_module_file_remove)
+{
+	CK_OBJECT_HANDLE objects[256];
+	CK_ULONG n_objects;
+	CK_ULONG n_check;
+	CK_RV rv;
+
+	rv = gkm_session_C_FindObjectsInit (session, NULL, 0);
+	g_assert (rv == CKR_OK);
+	rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_objects);
+	g_assert (rv == CKR_OK);
+	rv = gkm_session_C_FindObjectsFinal (session);
+	g_assert (rv == CKR_OK);
+
+	gkm_assert_cmpulong (n_objects, >, 0);
+
+	/* This file goes away */
+	remove_scratch_file ("test-refer-1.trust");
+
+	rv = gkm_session_C_FindObjectsInit (session, NULL, 0);
+	g_assert (rv == CKR_OK);
+	rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_check);
+	g_assert (rv == CKR_OK);
+	rv = gkm_session_C_FindObjectsFinal (session);
+	g_assert (rv == CKR_OK);
+
+	/* Should have less objects */
+	gkm_assert_cmpulong (n_check, <, n_objects);
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]