[gnome-keyring] Implement some testing of creating credentials.



commit f6e4268ef31f1d8f70e9963dfa436a8ef685d7af
Author: Stef Walter <stef memberwebs com>
Date:   Tue Jun 8 20:38:10 2010 +0000

    Implement some testing of creating credentials.

 pkcs11/gkm/gkm-test.c                            |   35 +++++-
 pkcs11/gkm/gkm-test.h                            |   19 +++-
 pkcs11/wrap-layer/tests/Makefile.am              |    1 +
 pkcs11/wrap-layer/tests/test-create-credential.c |  153 ++++++++++++++++++++++
 pkcs11/wrap-layer/tests/test-login-specific.c    |    4 +-
 5 files changed, 208 insertions(+), 4 deletions(-)
---
diff --git a/pkcs11/gkm/gkm-test.c b/pkcs11/gkm/gkm-test.c
index 11a2761..0897650 100644
--- a/pkcs11/gkm/gkm-test.c
+++ b/pkcs11/gkm/gkm-test.c
@@ -26,13 +26,14 @@
 #include "gkm-util.h"
 
 #include "pkcs11/pkcs11.h"
+#include "pkcs11/pkcs11i.h"
 
 #include <glib.h>
 
 #include <string.h>
 
 void
-gkm_assertion_message_cmprv (const gchar *domain, const gchar *file, int line,
+gkm_assertion_message_cmprv (const gchar *domain, const gchar *file, gint line,
                              const gchar *func, const gchar *expr,
                              CK_RV arg1, const gchar *cmp, CK_RV arg2)
 {
@@ -46,12 +47,25 @@ gkm_assertion_message_cmprv (const gchar *domain, const gchar *file, int line,
 	g_free (s);
 }
 
+void
+gkm_assertion_message_cmpulong (const gchar *domain, const gchar *file, gint line,
+                                const gchar *func, const gchar *expr,
+                                CK_ULONG arg1, const gchar *cmp, CK_ULONG arg2)
+{
+	char *s = NULL;
+	s = g_strdup_printf ("assertion failed (%s): (0x%08llx %s 0x%08llx)", expr,
+	                     (long long unsigned)arg1, cmp, (long long unsigned)arg2);
+	g_assertion_message (domain, file, line, func, s);
+	g_free (s);
+}
+
 /* -------------------------------------------------------------------------------
  * TEST MODULE
  */
 
 static gboolean initialized = FALSE;
 static gchar *the_pin = NULL;
+static gulong n_the_pin = 0;
 
 static gboolean logged_in = FALSE;
 static CK_USER_TYPE user_type = 0;
@@ -144,6 +158,7 @@ gkm_test_C_Initialize (CK_VOID_PTR pInitArgs)
 	}
 
 	the_pin = g_strdup ("booo");
+	n_the_pin = strlen (the_pin);
 	the_sessions = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, free_session);
 	the_objects = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)gkm_template_free);
 
@@ -426,6 +441,7 @@ gkm_test_C_InitToken (CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen
 
 	g_free (the_pin);
 	the_pin = g_strndup ((gchar*)pPin, ulPinLen);
+	n_the_pin = ulPinLen;
 	return CKR_OK;
 }
 
@@ -529,6 +545,7 @@ gkm_test_C_InitPIN (CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin,
 
 	g_free (the_pin);
 	the_pin = g_strndup ((gchar*)pPin, ulPinLen);
+	n_the_pin = ulPinLen;
 	return CKR_OK;
 }
 
@@ -550,6 +567,7 @@ gkm_test_C_SetPIN (CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin,
 
 	g_free (the_pin);
 	the_pin = g_strndup ((gchar*)pNewPin, ulNewLen);
+	n_the_pin = ulNewLen;
 	return CKR_OK;
 }
 
@@ -627,6 +645,9 @@ gkm_test_C_CreateObject (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
 	GArray *attrs;
 	Session *session;
 	gboolean token, priv;
+	CK_OBJECT_CLASS klass;
+	CK_OBJECT_HANDLE object;
+	CK_ATTRIBUTE_PTR attr;
 
 	g_assert (phObject != NULL);
 
@@ -644,6 +665,18 @@ gkm_test_C_CreateObject (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
 		}
 	}
 
+	/* In order to create a credential we must check CK_VALUE */
+	if (gkm_template_find_ulong (attrs, CKA_CLASS, &klass) && klass == CKO_G_CREDENTIAL) {
+		if (gkm_template_find_ulong (attrs, CKA_G_OBJECT, &object)) {
+			attr = gkm_template_find (attrs, CKA_VALUE);
+			if (!attr || attr->ulValueLen != n_the_pin ||
+			    memcmp (attr->pValue, the_pin, attr->ulValueLen) != 0) {
+				gkm_template_free (attrs);
+				return CKR_PIN_INCORRECT;
+			}
+		}
+	}
+
 	*phObject = ++unique_identifier;
 	if (gkm_template_find_boolean (attrs, CKA_TOKEN, &token) && token)
 		g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phObject), attrs);
diff --git a/pkcs11/gkm/gkm-test.h b/pkcs11/gkm/gkm-test.h
index f908056..acc1a62 100644
--- a/pkcs11/gkm/gkm-test.h
+++ b/pkcs11/gkm/gkm-test.h
@@ -22,6 +22,7 @@
 #include <glib.h>
 
 #include "pkcs11/pkcs11.h"
+#include "pkcs11/pkcs11i.h"
 
 #ifndef TESTMODULE_H_
 #define TESTMODULE_H_
@@ -30,7 +31,14 @@
 		do { CK_RV __v1 = (v1), __v2 = (v2); \
 			if (__v1 cmp __v2) ; else \
 				gkm_assertion_message_cmprv (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
-				                           #v1 " " #cmp " " #v2, __v1, #cmp, __v2); \
+				                             #v1 " " #cmp " " #v2, __v1, #cmp, __v2); \
+		} while (0)
+
+#define         gkm_assert_cmpulong(v1, cmp, v2) \
+		do { CK_RV __v1 = (v1), __v2 = (v2); \
+			if (__v1 cmp __v2) ; else \
+				gkm_assertion_message_cmpulong (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+				                                #v1 " " #cmp " " #v2, __v1, #cmp, __v2); \
 		} while (0)
 
 void            gkm_assertion_message_cmprv        (const gchar *domain,
@@ -42,6 +50,15 @@ void            gkm_assertion_message_cmprv        (const gchar *domain,
                                                     const gchar *cmp,
                                                     CK_RV arg2);
 
+void            gkm_assertion_message_cmpulong     (const gchar *domain,
+                                                    const gchar *file,
+                                                    gint line,
+                                                    const gchar *func,
+                                                    const gchar *expr,
+                                                    CK_ULONG arg1,
+                                                    const gchar *cmp,
+                                                    CK_ULONG arg2);
+
 CK_RV           gkm_test_C_Initialize              (CK_VOID_PTR pInitArgs);
 
 CK_RV           gkm_test_C_Finalize                (CK_VOID_PTR pReserved);
diff --git a/pkcs11/wrap-layer/tests/Makefile.am b/pkcs11/wrap-layer/tests/Makefile.am
index 621384a..ae531a6 100644
--- a/pkcs11/wrap-layer/tests/Makefile.am
+++ b/pkcs11/wrap-layer/tests/Makefile.am
@@ -1,5 +1,6 @@
 
 TESTING_FILES = \
+	test-create-credential.c \
 	test-login-user.c \
 	test-login-specific.c
 
diff --git a/pkcs11/wrap-layer/tests/test-create-credential.c b/pkcs11/wrap-layer/tests/test-create-credential.c
new file mode 100644
index 0000000..a84097f
--- /dev/null
+++ b/pkcs11/wrap-layer/tests/test-create-credential.c
@@ -0,0 +1,153 @@
+/*
+ * gnome-keyring
+ *
+ * Copyright (C) 2010 Stefan Walter
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include "test-suite.h"
+
+#include "gkm/gkm-test.h"
+
+#include "wrap-layer/gkm-wrap-layer.h"
+
+#include "ui/gku-prompt.h"
+
+static CK_FUNCTION_LIST test_functions;
+static CK_FUNCTION_LIST_PTR module = NULL;
+static CK_SESSION_HANDLE session = 0;
+static CK_OBJECT_HANDLE object = 0;
+
+DEFINE_SETUP (create_credential)
+{
+	CK_FUNCTION_LIST_PTR funcs;
+	CK_SLOT_ID slot_id;
+	CK_ULONG n_slots = 1;
+	CK_ULONG count;
+	CK_RV rv;
+
+	CK_BBOOL always = TRUE;
+	CK_ATTRIBUTE attrs[] = {
+		{ CKA_ALWAYS_AUTHENTICATE, &always, sizeof (always) }
+	};
+
+	/* Always start off with test functions */
+	rv = gkm_test_C_GetFunctionList (&funcs);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+	memcpy (&test_functions, funcs, sizeof (test_functions));
+
+	gkm_wrap_layer_reset_modules ();
+	gkm_wrap_layer_add_module (&test_functions);
+	module = gkm_wrap_layer_get_functions ();
+
+	gku_prompt_dummy_prepare_response ();
+
+	/* Open a session */
+	rv = (module->C_Initialize) (NULL);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+
+	rv = (module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+
+	rv = (module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &session);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+
+	/* Find the always authenticate object */
+	rv = (module->C_FindObjectsInit) (session, attrs, 1);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+
+	rv = (module->C_FindObjects) (session, &object, 1, &count);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+	gkm_assert_cmpulong (count, ==, 1);
+	gkm_assert_cmpulong (object, !=, 0);
+
+	rv = (module->C_FindObjectsFinal) (session);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+}
+
+DEFINE_TEARDOWN (create_credential)
+{
+	CK_RV rv;
+
+	object = 0;
+
+	rv = (module->C_CloseSession) (session);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+	session = 0;
+
+	rv = (module->C_Finalize) (NULL);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+	module = NULL;
+}
+
+DEFINE_TEST (create_credential_ok_password)
+{
+	CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
+	CK_ATTRIBUTE attrs[] = {
+		{ CKA_CLASS, &klass, sizeof (klass) },
+		{ CKA_G_OBJECT, &object, sizeof (object) },
+		{ CKA_VALUE, NULL, 0 }
+	};
+
+	CK_OBJECT_HANDLE cred = 0;
+	CK_RV rv;
+
+	gku_prompt_dummy_queue_ok_password ("booo");
+
+	rv = (module->C_CreateObject) (session, attrs, G_N_ELEMENTS (attrs), &cred);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+	gkm_assert_cmpulong (cred, !=, 0);
+}
+
+DEFINE_TEST (create_credential_bad_password_then_cancel)
+{
+	CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
+	CK_ATTRIBUTE attrs[] = {
+		{ CKA_CLASS, &klass, sizeof (klass) },
+		{ CKA_G_OBJECT, &object, sizeof (object) },
+		{ CKA_VALUE, NULL, 0 }
+	};
+
+	CK_OBJECT_HANDLE cred = 0;
+	CK_RV rv;
+
+	gku_prompt_dummy_queue_ok_password ("bad password");
+	gku_prompt_dummy_queue_no ();
+
+	rv = (module->C_CreateObject) (session, attrs, G_N_ELEMENTS (attrs), &cred);
+	gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
+}
+
+DEFINE_TEST (create_credentiaol_cancel_immediately)
+{
+	CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
+	CK_ATTRIBUTE attrs[] = {
+		{ CKA_CLASS, &klass, sizeof (klass) },
+		{ CKA_G_OBJECT, &object, sizeof (object) },
+		{ CKA_VALUE, NULL, 0 }
+	};
+
+	CK_OBJECT_HANDLE cred = 0;
+	CK_RV rv;
+
+	gku_prompt_dummy_queue_no ();
+
+	rv = (module->C_CreateObject) (session, attrs, G_N_ELEMENTS (attrs), &cred);
+	gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
+}
diff --git a/pkcs11/wrap-layer/tests/test-login-specific.c b/pkcs11/wrap-layer/tests/test-login-specific.c
index ce97e38..c436d07 100644
--- a/pkcs11/wrap-layer/tests/test-login-specific.c
+++ b/pkcs11/wrap-layer/tests/test-login-specific.c
@@ -76,8 +76,8 @@ DEFINE_SETUP (login_specific)
 
 	rv = (module->C_FindObjects) (session, &key, 1, &count);
 	gkm_assert_cmprv (rv, ==, CKR_OK);
-	g_assert (count == 1);
-	g_assert (key != 0);
+	gkm_assert_cmpulong (count, ==, 1);
+	gkm_assert_cmpulong (key, !=, 0);
 
 	rv = (module->C_FindObjectsFinal) (session);
 	gkm_assert_cmprv (rv, ==, CKR_OK);



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