[gnome-keyring/trust-store] [gcr] Complete tests of gcr_trust_xxx functionality.



commit 07699cc140ad40ada9c3810e25e45208be5510d7
Author: Stef Walter <stefw collabora co uk>
Date:   Mon Nov 29 22:24:59 2010 +0000

    [gcr] Complete tests of gcr_trust_xxx functionality.

 gck/gck-mock.c         |   16 ++++++++
 gck/gck-mock.h         |   10 +++++
 gcr/gcr-trust.c        |   39 ++++++++++---------
 gcr/gcr-trust.h        |   12 +++---
 gcr/tests/test-trust.c |   98 ++++++++++++++++++++++++++++++++++++------------
 5 files changed, 126 insertions(+), 49 deletions(-)
---
diff --git a/gck/gck-mock.c b/gck/gck-mock.c
index bc4a839..4a64590 100644
--- a/gck/gck-mock.c
+++ b/gck/gck-mock.c
@@ -850,6 +850,14 @@ gck_mock_C_CreateObject (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
 }
 
 CK_RV
+gck_mock_fail_C_CreateObject (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
+                              CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phObject)
+{
+	/* Always fails */
+	return CKR_FUNCTION_FAILED;
+}
+
+CK_RV
 gck_mock_unsupported_C_CopyObject (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
                                    CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
                                    CK_OBJECT_HANDLE_PTR phNewObject)
@@ -1018,6 +1026,14 @@ gck_mock_C_FindObjectsInit (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTempla
 }
 
 CK_RV
+gck_mock_fail_C_FindObjects (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject,
+                             CK_ULONG ulMaxObjectCount, CK_ULONG_PTR pulObjectCount)
+{
+	/* Always fails */
+	return CKR_FUNCTION_FAILED;
+}
+
+CK_RV
 gck_mock_C_FindObjects (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject,
                         CK_ULONG ulMaxObjectCount, CK_ULONG_PTR pulObjectCount)
 {
diff --git a/gck/gck-mock.h b/gck/gck-mock.h
index b92c33c..fcdf2fb 100644
--- a/gck/gck-mock.h
+++ b/gck/gck-mock.h
@@ -129,6 +129,11 @@ CK_RV               gck_mock_C_CreateObject                        (CK_SESSION_H
                                                                     CK_ULONG ulCount,
                                                                     CK_OBJECT_HANDLE_PTR phObject);
 
+CK_RV               gck_mock_fail_C_CreateObject                   (CK_SESSION_HANDLE hSession,
+                                                                    CK_ATTRIBUTE_PTR pTemplate,
+                                                                    CK_ULONG ulCount,
+                                                                    CK_OBJECT_HANDLE_PTR phObject);
+
 CK_RV               gck_mock_unsupported_C_CopyObject              (CK_SESSION_HANDLE hSession,
                                                                     CK_OBJECT_HANDLE hObject,
                                                                     CK_ATTRIBUTE_PTR pTemplate,
@@ -161,6 +166,11 @@ CK_RV               gck_mock_C_FindObjects                         (CK_SESSION_H
                                                                     CK_ULONG ulMaxObjectCount,
                                                                     CK_ULONG_PTR pulObjectCount);
 
+CK_RV               gck_mock_fail_C_FindObjects                    (CK_SESSION_HANDLE hSession,
+                                                                    CK_OBJECT_HANDLE_PTR phObject,
+                                                                    CK_ULONG ulMaxObjectCount,
+                                                                    CK_ULONG_PTR pulObjectCount);
+
 CK_RV               gck_mock_C_FindObjectsFinal                    (CK_SESSION_HANDLE hSession);
 
 CK_RV               gck_mock_C_EncryptInit                         (CK_SESSION_HANDLE hSession,
diff --git a/gcr/gcr-trust.c b/gcr/gcr-trust.c
index f6320d7..809636b 100644
--- a/gcr/gcr-trust.c
+++ b/gcr/gcr-trust.c
@@ -107,7 +107,7 @@ prepare_trust_attrs (GcrCertificate *cert, CK_ASSERTION_TYPE type)
  */
 
 static GckEnumerator*
-prepare_is_certificate_exception (GcrCertificate *cert, const gchar *purpose, const gchar *remote)
+prepare_is_certificate_exception (GcrCertificate *cert, const gchar *purpose, const gchar *peer)
 {
 	GckAttributes *attrs;
 	GckEnumerator *en;
@@ -119,7 +119,7 @@ prepare_is_certificate_exception (GcrCertificate *cert, const gchar *purpose, co
 	g_return_val_if_fail (attrs, NULL);
 
 	gck_attributes_add_string (attrs, CKA_G_PURPOSE, purpose);
-	gck_attributes_add_string (attrs, CKA_G_PEER, remote);
+	gck_attributes_add_string (attrs, CKA_G_PEER, peer);
 
 	/*
 	 * TODO: We need to be able to sort the modules by preference
@@ -156,16 +156,16 @@ perform_is_certificate_exception (GckEnumerator *en, GCancellable *cancel, GErro
 
 gboolean
 gcr_trust_is_certificate_exception (GcrCertificate *cert, const gchar *purpose,
-                                    const gchar *remote, GCancellable *cancel, GError **error)
+                                    const gchar *peer, GCancellable *cancel, GError **error)
 {
 	GckEnumerator *en;
 	gboolean ret;
 
 	g_return_val_if_fail (GCR_IS_CERTIFICATE (cert), FALSE);
 	g_return_val_if_fail (purpose, FALSE);
-	g_return_val_if_fail (remote, FALSE);
+	g_return_val_if_fail (peer, FALSE);
 
-	en = prepare_is_certificate_exception (cert, purpose, remote);
+	en = prepare_is_certificate_exception (cert, purpose, peer);
 	g_return_val_if_fail (en, FALSE);
 
 	ret = perform_is_certificate_exception (en, cancel, error);
@@ -190,13 +190,13 @@ thread_is_certificate_exception (GSimpleAsyncResult *res, GObject *object, GCanc
 
 void
 gcr_trust_is_certificate_exception_async (GcrCertificate *cert, const gchar *purpose,
-                                          const gchar *remote, GCancellable *cancel,
+                                          const gchar *peer, GCancellable *cancel,
                                           GAsyncReadyCallback callback, gpointer user_data)
 {
 	GSimpleAsyncResult *async;
 	GckEnumerator *en;
 
-	en = prepare_is_certificate_exception (cert, purpose, remote);
+	en = prepare_is_certificate_exception (cert, purpose, peer);
 	g_return_if_fail (en);
 
 	async = g_simple_async_result_new (G_OBJECT (en), callback, user_data,
@@ -231,7 +231,7 @@ gcr_trust_is_certificate_exception_finish (GAsyncResult *res, GError **error)
  */
 
 static GckEnumerator*
-prepare_add_certificate_exception (GcrCertificate *cert, const gchar *purpose, const gchar *remote)
+prepare_add_certificate_exception (GcrCertificate *cert, const gchar *purpose, const gchar *peer)
 {
 	GckAttributes *attrs;
 	GckEnumerator *en;
@@ -243,7 +243,8 @@ prepare_add_certificate_exception (GcrCertificate *cert, const gchar *purpose, c
 	g_return_val_if_fail (attrs, NULL);
 
 	gck_attributes_add_string (attrs, CKA_G_PURPOSE, purpose);
-	gck_attributes_add_string (attrs, CKA_G_PEER, remote);
+	gck_attributes_add_string (attrs, CKA_G_PEER, peer);
+	gck_attributes_add_boolean (attrs, CKA_TOKEN, TRUE);
 
 	/*
 	 * TODO: We need to be able to sort the modules by preference
@@ -317,13 +318,13 @@ perform_add_certificate_exception (GckEnumerator *en, GCancellable *cancel, GErr
 }
 
 gboolean
-gcr_trust_add_certificate_exception (GcrCertificate *cert, const gchar *purpose, const gchar *remote,
+gcr_trust_add_certificate_exception (GcrCertificate *cert, const gchar *purpose, const gchar *peer,
                                      GCancellable *cancel, GError **error)
 {
 	GckEnumerator *en;
 	gboolean ret;
 
-	en = prepare_add_certificate_exception (cert, purpose, remote);
+	en = prepare_add_certificate_exception (cert, purpose, peer);
 	g_return_val_if_fail (en, FALSE);
 
 	ret = perform_add_certificate_exception (en, cancel, error);
@@ -348,13 +349,13 @@ thread_add_certificate_exception (GSimpleAsyncResult *res, GObject *object, GCan
 
 void
 gcr_trust_add_certificate_exception_async (GcrCertificate *cert, const gchar *purpose,
-                                           const gchar *remote, GCancellable *cancel,
+                                           const gchar *peer, GCancellable *cancel,
                                            GAsyncReadyCallback callback, gpointer user_data)
 {
 	GSimpleAsyncResult *async;
 	GckEnumerator *en;
 
-	en = prepare_add_certificate_exception (cert, purpose, remote);
+	en = prepare_add_certificate_exception (cert, purpose, peer);
 	g_return_if_fail (en);
 
 	async = g_simple_async_result_new (G_OBJECT (en), callback, user_data,
@@ -388,7 +389,7 @@ gcr_trust_add_certificate_exception_finish (GAsyncResult *res, GError **error)
 
 static GckEnumerator*
 prepare_remove_certificate_exception (GcrCertificate *cert, const gchar *purpose,
-                                      const gchar *remote)
+                                      const gchar *peer)
 {
 	GckAttributes *attrs;
 	GckEnumerator *en;
@@ -400,7 +401,7 @@ prepare_remove_certificate_exception (GcrCertificate *cert, const gchar *purpose
 	g_return_val_if_fail (attrs, NULL);
 
 	gck_attributes_add_string (attrs, CKA_G_PURPOSE, purpose);
-	gck_attributes_add_string (attrs, CKA_G_PEER, remote);
+	gck_attributes_add_string (attrs, CKA_G_PEER, peer);
 
 	/*
 	 * TODO: We need to be able to sort the modules by preference
@@ -445,13 +446,13 @@ perform_remove_certificate_exception (GckEnumerator *en, GCancellable *cancel, G
 }
 
 gboolean
-gcr_trust_remove_certificate_exception (GcrCertificate *cert, const gchar *purpose, const gchar *remote,
+gcr_trust_remove_certificate_exception (GcrCertificate *cert, const gchar *purpose, const gchar *peer,
                                         GCancellable *cancel, GError **error)
 {
 	GckEnumerator *en;
 	gboolean ret;
 
-	en = prepare_remove_certificate_exception (cert, purpose, remote);
+	en = prepare_remove_certificate_exception (cert, purpose, peer);
 	g_return_val_if_fail (en, FALSE);
 
 	ret = perform_remove_certificate_exception (en, cancel, error);
@@ -476,13 +477,13 @@ thread_remove_certificate_exception (GSimpleAsyncResult *res, GObject *object, G
 
 void
 gcr_trust_remove_certificate_exception_async (GcrCertificate *cert, const gchar *purpose,
-                                              const gchar *remote, GCancellable *cancel,
+                                              const gchar *peer, GCancellable *cancel,
                                               GAsyncReadyCallback callback, gpointer user_data)
 {
 	GSimpleAsyncResult *async;
 	GckEnumerator *en;
 
-	en = prepare_remove_certificate_exception (cert, purpose, remote);
+	en = prepare_remove_certificate_exception (cert, purpose, peer);
 	g_return_if_fail (en);
 
 	async = g_simple_async_result_new (G_OBJECT (en), callback, user_data,
diff --git a/gcr/gcr-trust.h b/gcr/gcr-trust.h
index 74238db..bce4d27 100644
--- a/gcr/gcr-trust.h
+++ b/gcr/gcr-trust.h
@@ -33,13 +33,13 @@ G_BEGIN_DECLS
 
 gboolean       gcr_trust_is_certificate_exception              (GcrCertificate *cert,
                                                                 const gchar *purpose,
-                                                                const gchar *remote,
+                                                                const gchar *peer,
                                                                 GCancellable *cancel,
                                                                 GError **error);
 
 void           gcr_trust_is_certificate_exception_async        (GcrCertificate *cert,
                                                                 const gchar *purpose,
-                                                                const gchar *remote,
+                                                                const gchar *peer,
                                                                 GCancellable *cancel,
                                                                 GAsyncReadyCallback callback,
                                                                 gpointer user_data);
@@ -49,13 +49,13 @@ gboolean       gcr_trust_is_certificate_exception_finish       (GAsyncResult *re
 
 gboolean       gcr_trust_add_certificate_exception             (GcrCertificate *cert,
                                                                 const gchar *purpose,
-                                                                const gchar *remote,
+                                                                const gchar *peer,
                                                                 GCancellable *cancel,
                                                                 GError **error);
 
 void           gcr_trust_add_certificate_exception_async       (GcrCertificate *cert,
                                                                 const gchar *purpose,
-                                                                const gchar *remote,
+                                                                const gchar *peer,
                                                                 GCancellable *cancel,
                                                                 GAsyncReadyCallback callback,
                                                                 gpointer user_data);
@@ -65,13 +65,13 @@ gboolean       gcr_trust_add_certificate_exception_finish      (GAsyncResult *re
 
 gboolean       gcr_trust_remove_certificate_exception          (GcrCertificate *cert,
                                                                 const gchar *purpose,
-                                                                const gchar *remote,
+                                                                const gchar *peer,
                                                                 GCancellable *cancel,
                                                                 GError **error);
 
 void           gcr_trust_remove_certificate_exception_async    (GcrCertificate *cert,
                                                                 const gchar *purpose,
-                                                                const gchar *remote,
+                                                                const gchar *peer,
                                                                 GCancellable *cancel,
                                                                 GAsyncReadyCallback callback,
                                                                 gpointer user_data);
diff --git a/gcr/tests/test-trust.c b/gcr/tests/test-trust.c
index 1e9f4c5..2facf64 100644
--- a/gcr/tests/test-trust.c
+++ b/gcr/tests/test-trust.c
@@ -34,12 +34,13 @@
 
 #include <glib.h>
 
-static CK_FUNCTION_LIST_PTR funcs;
+static CK_FUNCTION_LIST funcs;
 static GList *modules = NULL;
 static GcrCertificate *certificate = NULL;
 
 TESTING_SETUP (trust_setup)
 {
+	CK_FUNCTION_LIST_PTR f;
 	GckModule *module;
 	guchar *contents;
 	gsize len;
@@ -50,15 +51,16 @@ TESTING_SETUP (trust_setup)
 
 	certificate = gcr_simple_certificate_new (contents, len);
 
-	rv = gck_mock_C_GetFunctionList (&funcs);
+	rv = gck_mock_C_GetFunctionList (&f);
 	gck_assert_cmprv (rv, ==, CKR_OK);
+	memcpy (&funcs, f, sizeof (funcs));
 
 	/* Open a session */
-	rv = (funcs->C_Initialize) (NULL);
+	rv = (funcs.C_Initialize) (NULL);
 	gck_assert_cmprv (rv, ==, CKR_OK);
 
 	g_assert (!modules);
-	module = gck_module_new (funcs, 0);
+	module = gck_module_new (&funcs, 0);
 	modules = g_list_prepend (modules, module);
 
 	_gcr_set_test_pkcs11_modules (modules);
@@ -72,8 +74,7 @@ TESTING_TEARDOWN (trust_setup)
 	g_object_unref (certificate);
 	certificate = NULL;
 
-	g_assert (funcs);
-	rv = (funcs->C_Finalize) (NULL);
+	rv = (funcs.C_Finalize) (NULL);
 	gck_assert_cmprv (rv, ==, CKR_OK);
 
 	gck_list_unref_free (modules);
@@ -86,7 +87,7 @@ TESTING_TEST (trust_is_exception_none)
 	gboolean trust;
 
 	trust = gcr_trust_is_certificate_exception (certificate, GCR_PURPOSE_EMAIL, "host", NULL, &error);
-	g_assert_cmpint (trust, ==, TRUE);
+	g_assert_cmpint (trust, ==, FALSE);
 	g_assert (error == NULL);
 }
 
@@ -97,7 +98,7 @@ TESTING_TEST (trust_add_and_is_exception)
 	gboolean ret;
 
 	trust = gcr_trust_is_certificate_exception (certificate, GCR_PURPOSE_EMAIL, "host", NULL, &error);
-	g_assert_cmpint (trust, ==, TRUE);
+	g_assert_cmpint (trust, ==, FALSE);
 	g_assert (error == NULL);
 
 	ret = gcr_trust_add_certificate_exception (certificate, GCR_PURPOSE_EMAIL, "host", NULL, &error);
@@ -109,6 +110,19 @@ TESTING_TEST (trust_add_and_is_exception)
 	g_assert (error == NULL);
 }
 
+TESTING_TEST (trust_add_certificate_exception_fail)
+{
+	GError *error = NULL;
+	gboolean ret;
+
+	/* Make this function fail */
+	funcs.C_CreateObject = gck_mock_fail_C_CreateObject;
+
+	ret = gcr_trust_add_certificate_exception (certificate, GCR_PURPOSE_CLIENT_AUTH, NULL, NULL, &error);
+	g_assert (ret == FALSE);
+	g_assert_error (error, GCK_ERROR, CKR_FUNCTION_FAILED);
+}
+
 TESTING_TEST (trust_add_and_remov_exception)
 {
 	GError *error = NULL;
@@ -176,6 +190,50 @@ TESTING_TEST (trust_add_and_is_exception_async)
 	result = NULL;
 }
 
+TESTING_TEST (trust_add_and_remov_exception_async)
+{
+	GAsyncResult *result = NULL;
+	GError *error = NULL;
+	gboolean trust;
+	gboolean ret;
+
+	gcr_trust_add_certificate_exception_async (certificate, GCR_PURPOSE_EMAIL, "host", NULL, fetch_async_result, &result);
+	testing_wait_until (500);
+	g_assert (result);
+	ret = gcr_trust_add_certificate_exception_finish (result, &error);
+	g_assert (ret == TRUE);
+	g_assert (error == NULL);
+	g_object_unref (result);
+	result = NULL;
+
+	gcr_trust_is_certificate_exception_async (certificate, GCR_PURPOSE_EMAIL, "host", NULL, fetch_async_result, &result);
+	testing_wait_until (500);
+	g_assert (result);
+	trust = gcr_trust_is_certificate_exception_finish (result, &error);
+	g_assert (trust == TRUE);
+	g_assert (error == NULL);
+	g_object_unref (result);
+	result = NULL;
+
+	gcr_trust_remove_certificate_exception_async (certificate, GCR_PURPOSE_EMAIL, "host", NULL, fetch_async_result, &result);
+	testing_wait_until (500);
+	g_assert (result);
+	ret = gcr_trust_remove_certificate_exception_finish (result, &error);
+	g_assert (ret == TRUE);
+	g_assert (error == NULL);
+	g_object_unref (result);
+	result = NULL;
+
+	gcr_trust_is_certificate_exception_async (certificate, GCR_PURPOSE_EMAIL, "host", NULL, fetch_async_result, &result);
+	testing_wait_until (500);
+	g_assert (result);
+	trust = gcr_trust_is_certificate_exception_finish (result, &error);
+	g_assert (trust == FALSE);
+	g_assert (error == NULL);
+	g_object_unref (result);
+	result = NULL;
+}
+
 TESTING_TEST (trust_is_certificate_anchor_not)
 {
 	GError *error = NULL;
@@ -190,26 +248,18 @@ TESTING_TEST (trust_is_certificate_anchor_yes)
 {
 	GError *error = NULL;
 	GckAttributes *attrs;
-	gpointer data;
-	gsize n_data;
+	gconstpointer der;
+	gsize n_der;
 	gboolean ret;
 
 	/* Create a certificate root trust */
 	attrs = gck_attributes_new ();
-	data = gcr_certificate_get_issuer_raw (certificate, &n_data);
-	g_assert (data && n_data);
-	gck_attributes_add_data (attrs, CKA_ISSUER, data, n_data);
-	g_free (data);
-	data = gcr_certificate_get_serial_number (certificate, &n_data);
-	g_assert (data && n_data);
-	gck_attributes_add_data (attrs, CKA_SERIAL_NUMBER, data, n_data);
-	g_free (data);
-	data = gcr_certificate_get_fingerprint (certificate, G_CHECKSUM_SHA1, &n_data);
-	g_assert (data);
-	gck_attributes_add_data (attrs, CKA_CERT_SHA1_HASH, data, n_data);
-	g_free (data);
-	gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_NETSCAPE_TRUST);
-	gck_attributes_add_ulong (attrs, CKA_TRUST_CLIENT_AUTH, CKT_NETSCAPE_TRUSTED_DELEGATOR);
+	der = gcr_certificate_get_der_data (certificate, &n_der);
+	gck_attributes_add_data (attrs, CKA_G_CERTIFICATE_VALUE, der, n_der);
+	gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_G_TRUST_ASSERTION);
+	gck_attributes_add_boolean (attrs, CKA_TOKEN, TRUE);
+	gck_attributes_add_string (attrs, CKA_G_PURPOSE, GCR_PURPOSE_CLIENT_AUTH);
+	gck_attributes_add_ulong (attrs, CKA_G_ASSERTION_TYPE, CKT_G_CERTIFICATE_TRUST_ANCHOR);
 	gck_mock_module_take_object (attrs);
 
 	ret = gcr_trust_is_certificate_anchor (certificate, GCR_PURPOSE_CLIENT_AUTH, NULL, &error);



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