[gnome-keyring] Implement login prompting tests as automatic test.



commit c712ccada0f5524e2bda7a16f3e754e14519bdd0
Author: Stef Walter <stef memberwebs com>
Date:   Tue Jun 8 14:58:44 2010 +0000

    Implement login prompting tests as automatic test.

 pkcs11/gkm/gkm-test.c                       |    1 +
 pkcs11/wrap-layer/gkm-wrap-layer.c          |   15 ++++++
 pkcs11/wrap-layer/gkm-wrap-layer.h          |    2 +
 pkcs11/wrap-layer/tests/Makefile.am         |   28 ++++--------
 pkcs11/wrap-layer/tests/test-prompt-login.c |   63 ++++++++++++--------------
 ui/gku-prompt.c                             |   24 +++++++++-
 ui/gku-prompt.h                             |    8 +++-
 7 files changed, 85 insertions(+), 56 deletions(-)
---
diff --git a/pkcs11/gkm/gkm-test.c b/pkcs11/gkm/gkm-test.c
index 820cda3..37c477b 100644
--- a/pkcs11/gkm/gkm-test.c
+++ b/pkcs11/gkm/gkm-test.c
@@ -216,6 +216,7 @@ gkm_test_C_Finalize (CK_VOID_PTR pReserved)
 	g_return_val_if_fail (initialized == TRUE, CKR_CRYPTOKI_NOT_INITIALIZED);
 
 	initialized = FALSE;
+	logged_in = FALSE;
 	g_hash_table_destroy (the_objects);
 	the_objects = NULL;
 
diff --git a/pkcs11/wrap-layer/gkm-wrap-layer.c b/pkcs11/wrap-layer/gkm-wrap-layer.c
index 5e71bf3..337625e 100644
--- a/pkcs11/wrap-layer/gkm-wrap-layer.c
+++ b/pkcs11/wrap-layer/gkm-wrap-layer.c
@@ -1393,12 +1393,27 @@ gkm_wrap_layer_get_functions (void)
 }
 
 void
+gkm_wrap_layer_reset_modules (void)
+{
+	G_LOCK (wrap_layer);
+
+		g_assert (!wrap_mappings);
+		g_assert (!wrap_sessions);
+		g_list_free (wrap_modules);
+		wrap_modules = NULL;
+
+	G_UNLOCK (wrap_layer);
+}
+
+void
 gkm_wrap_layer_add_module (CK_FUNCTION_LIST_PTR funcs)
 {
 	g_assert (funcs);
 
 	G_LOCK (wrap_layer);
 
+		g_assert (!wrap_mappings);
+		g_assert (!wrap_sessions);
 		wrap_modules = g_list_append (wrap_modules, funcs);
 
 	G_UNLOCK (wrap_layer);
diff --git a/pkcs11/wrap-layer/gkm-wrap-layer.h b/pkcs11/wrap-layer/gkm-wrap-layer.h
index f12b61b..a8d73c6 100644
--- a/pkcs11/wrap-layer/gkm-wrap-layer.h
+++ b/pkcs11/wrap-layer/gkm-wrap-layer.h
@@ -26,6 +26,8 @@
 
 CK_FUNCTION_LIST_PTR    gkm_wrap_layer_get_functions     (void);
 
+void                    gkm_wrap_layer_reset_modules     (void);
+
 void                    gkm_wrap_layer_add_module        (CK_FUNCTION_LIST_PTR funcs);
 
 #endif /* __GKM_WRAP_LAYER_H__ */
diff --git a/pkcs11/wrap-layer/tests/Makefile.am b/pkcs11/wrap-layer/tests/Makefile.am
index 308bdbf..9fcf2b4 100644
--- a/pkcs11/wrap-layer/tests/Makefile.am
+++ b/pkcs11/wrap-layer/tests/Makefile.am
@@ -1,24 +1,16 @@
 
-INCLUDES = -I. \
-	-I$(top_srcdir) \
-	-I$(top_builddir) \
-	-I$(top_srcdir)/pkcs11 \
-	$(GLIB_CFLAGS) \
-	$(P11_TESTS_CFLAGS)
+UNIT_AUTO = \
+	test-prompt-login.c
 
-LIBS = \
-	$(GLIB_LIBS) \
-	$(GTHREAD_LIBS)
+UNIT_PROMPT =
 
-noinst_PROGRAMS = \
-	test-create-credential
-
-test_create_credential_SOURCES = \
-	test-create-credential.c
-
-test_create_credential_LDADD = \
+UNIT_LIBS = \
 	$(top_builddir)/pkcs11/wrap-layer/libgkm-wrap-layer.la \
 	$(top_builddir)/pkcs11/gkm/libgkm.la \
 	$(top_builddir)/ui/libgku-prompt.la \
-	$(top_builddir)/egg/libegg.la \
-	$(DAEMON_LIBS)
\ No newline at end of file
+	$(top_builddir)/egg/libegg.la
+
+include $(top_srcdir)/tests/gtest.make
+
+EXTRA_DIST = \
+	test-data
\ No newline at end of file
diff --git a/pkcs11/wrap-layer/tests/test-prompt-login.c b/pkcs11/wrap-layer/tests/test-prompt-login.c
index 2cf235a..ee2ff97 100644
--- a/pkcs11/wrap-layer/tests/test-prompt-login.c
+++ b/pkcs11/wrap-layer/tests/test-prompt-login.c
@@ -21,7 +21,8 @@
 
 #include "config.h"
 
-#include "egg/egg-libgcrypt.h"
+#include "run-auto-test.h"
+
 #include "egg/egg-secure-memory.h"
 
 #include "gkm/gkm-test.h"
@@ -35,7 +36,7 @@
 
 #include <glib-object.h>
 
-CK_FUNCTION_LIST create_credential_functions = {
+CK_FUNCTION_LIST prompt_login_functions = {
 	{ 2, 11 },	/* version */
 	gkm_test_C_Initialize,
 	gkm_test_C_Finalize,
@@ -107,20 +108,17 @@ CK_FUNCTION_LIST create_credential_functions = {
 	gkm_test_C_WaitForSlotEvent
 };
 
-EGG_SECURE_GLIB_DEFINITIONS()
+static CK_FUNCTION_LIST_PTR module = NULL;
+static CK_SESSION_HANDLE session = 0;
 
-static gpointer
-test (gpointer loop)
+DEFINE_SETUP (module)
 {
-	CK_FUNCTION_LIST_PTR module;
-	CK_SESSION_HANDLE session;
 	CK_SLOT_ID slot_id;
 	CK_ULONG n_slots = 1;
 	CK_RV rv;
 
-	gku_prompt_queue_dummy_ok_password ("booo");
-
-	gkm_wrap_layer_add_module (&create_credential_functions);
+	gkm_wrap_layer_reset_modules ();
+	gkm_wrap_layer_add_module (&prompt_login_functions);
 	module = gkm_wrap_layer_get_functions ();
 
 	/* Open a session */
@@ -132,43 +130,40 @@ test (gpointer loop)
 
 	rv = (module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &session);
 	gkm_assert_cmprv (rv, ==, CKR_OK);
+}
 
-	rv = (module->C_Login) (session, CKU_USER, NULL, 0);
-	if (rv == CKR_PIN_INCORRECT) {
-		g_message ("pin incorrect");
-	} else {
-		gkm_assert_cmprv (rv, ==, CKR_OK);
-		g_message ("pin correct");
-	}
+DEFINE_TEARDOWN (module)
+{
+	CK_RV rv;
 
 	rv = (module->C_CloseSession) (session);
 	gkm_assert_cmprv (rv, ==, CKR_OK);
+	session = 0;
 
 	rv = (module->C_Finalize) (NULL);
 	gkm_assert_cmprv (rv, ==, CKR_OK);
-
-	g_main_loop_quit (loop);
-	return NULL;
+	module = NULL;
 }
 
-int
-main(void)
+DEFINE_TEST (login_prompt_ok)
 {
-	GThread *thread;
-	GMainLoop *loop;
+	CK_RV rv;
 
-	g_type_init ();
-	g_thread_init (NULL);
-	egg_libgcrypt_initialize ();
+	gku_prompt_dummy_prepare_response ();
+	gku_prompt_dummy_queue_ok_password ("booo");
 
-	loop = g_main_loop_new (NULL, FALSE);
-	thread = g_thread_create (test, loop, TRUE, NULL);
-	g_assert (thread);
+	rv = (module->C_Login) (session, CKU_USER, NULL, 0);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+}
 
-	g_main_loop_run (loop);
+DEFINE_TEST (login_prompt_cancel)
+{
+	CK_RV rv;
 
-	g_thread_join (thread);
-	g_main_loop_unref (loop);
+	gku_prompt_dummy_prepare_response ();
+	gku_prompt_dummy_queue_ok_password ("bad password");
+	gku_prompt_dummy_queue_no ();
 
-	return 0;
+	rv = (module->C_Login) (session, CKU_USER, NULL, 0);
+	gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
 }
diff --git a/ui/gku-prompt.c b/ui/gku-prompt.c
index 7dfcf0b..ce473cf 100644
--- a/ui/gku-prompt.c
+++ b/ui/gku-prompt.c
@@ -29,6 +29,7 @@
 #include "egg/egg-dh.h"
 #include "egg/egg-error.h"
 #include "egg/egg-hex.h"
+#include "egg/egg-libgcrypt.h"
 #include "egg/egg-secure-memory.h"
 #include "egg/egg-spawn.h"
 
@@ -623,6 +624,8 @@ gku_prompt_class_init (GkuPromptClass *klass)
 	                                   G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GkuPromptClass, responded),
 	                                   g_signal_accumulator_true_handled, NULL, gku_prompt_marshal_BOOLEAN__VOID,
 	                                   G_TYPE_BOOLEAN, 0);
+
+	egg_libgcrypt_initialize ();
 }
 
 /* -----------------------------------------------------------------------------
@@ -1159,6 +1162,16 @@ gku_prompt_request_attention_sync (const gchar *window_id, GkuPromptAttentionFun
 
 #ifdef WITH_TESTS
 
+void
+gku_prompt_dummy_prepare_response (void)
+{
+	g_static_mutex_lock (&attention_mutex);
+		dummy_responses = TRUE;
+		while (!g_queue_is_empty (&queued_responses))
+			g_free (g_queue_pop_head (&queued_responses));
+	g_static_mutex_unlock (&attention_mutex);
+}
+
 static void
 queue_dummy_response (gchar *response)
 {
@@ -1170,14 +1183,14 @@ queue_dummy_response (gchar *response)
 }
 
 void
-gku_prompt_queue_dummy_response (const gchar *response)
+gku_prompt_dummy_queue_response (const gchar *response)
 {
 	g_return_if_fail (response);
 	queue_dummy_response (g_strdup (response));
 }
 
 void
-gku_prompt_queue_dummy_ok_password (const gchar *password)
+gku_prompt_dummy_queue_ok_password (const gchar *password)
 {
 	const static gchar *RESPONSE = "[password]\nparameter=\nvalue=%s\n[prompt]\nresponse=ok\n";
 	gchar *value;
@@ -1188,4 +1201,11 @@ gku_prompt_queue_dummy_ok_password (const gchar *password)
 	g_free (value);
 }
 
+void
+gku_prompt_dummy_queue_no (void)
+{
+	const static gchar *RESPONSE = "[prompt]\nresponse=no\n";
+	gku_prompt_dummy_queue_response (RESPONSE);
+}
+
 #endif
diff --git a/ui/gku-prompt.h b/ui/gku-prompt.h
index 2d31459..075a209 100644
--- a/ui/gku-prompt.h
+++ b/ui/gku-prompt.h
@@ -141,9 +141,13 @@ void                gku_prompt_request_attention_sync     (const gchar *window_i
 
 #ifdef WITH_TESTS
 
-void                gku_prompt_queue_dummy_response        (const gchar *response);
+void                gku_prompt_dummy_prepare_response      (void);
 
-void                gku_prompt_queue_dummy_ok_password     (const gchar *password);
+void                gku_prompt_dummy_queue_response        (const gchar *response);
+
+void                gku_prompt_dummy_queue_ok_password     (const gchar *password);
+
+void                gku_prompt_dummy_queue_no              (void);
 
 #endif
 



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