gnome-keyring r1471 - in trunk: . gp11 gp11/tests



Author: nnielsen
Date: Tue Jan 20 20:38:29 2009
New Revision: 1471
URL: http://svn.gnome.org/viewvc/gnome-keyring?rev=1471&view=rev

Log:
	* gp11/gp11.h:
	* gp11/gp11-module.c:
	* gp11/gp11-object.c:
	* gp11/gp11-slot.c:
	* gp11/tests/unit-test-gp11-module.c:
	* gp11/tests/unit-test-gp11-object.c:
	* gp11/tests/unit-test-gp11-slot.c: Add hash and equals functions for 
	PKCS#11 modules, slots, and objects. Allows them to be used in hash tables
	and searches.


Modified:
   trunk/ChangeLog
   trunk/gp11/gp11-module.c
   trunk/gp11/gp11-object.c
   trunk/gp11/gp11-slot.c
   trunk/gp11/gp11.h
   trunk/gp11/tests/unit-test-gp11-module.c
   trunk/gp11/tests/unit-test-gp11-object.c
   trunk/gp11/tests/unit-test-gp11-slot.c

Modified: trunk/gp11/gp11-module.c
==============================================================================
--- trunk/gp11/gp11-module.c	(original)
+++ trunk/gp11/gp11-module.c	Tue Jan 20 20:38:29 2009
@@ -684,6 +684,55 @@
 }
 
 /**
+ * gp11_module_equal:
+ * @module1: A pointer to the first GP11Module
+ * @module2: A pointer to the second GP11Module
+ * 
+ * Checks equality of two modules. Two GP11Module objects can point to the same 
+ * underlying PKCS#11 module.
+ * 
+ * Return value: TRUE if module1 and module2 are equal. FALSE if either is not a GP11Module.
+ **/
+gboolean
+gp11_module_equal (gconstpointer module1, gconstpointer module2)
+{
+	GP11ModuleData *data1, *data2;
+
+	if (module1 == module2)
+		return TRUE;
+	if (!GP11_IS_MODULE (module1) || !GP11_IS_MODULE (module2))
+		return FALSE;
+	
+	data1 = GP11_MODULE_GET_DATA (module1);
+	data2 = GP11_MODULE_GET_DATA (module2);
+	
+	return data1->funcs == data2->funcs;
+}
+
+/**
+ * gp11_module_hash:
+ * @module: A pointer to a GP11Module
+ * 
+ * Create a hash value for the GP11Module. 
+ * 
+ * This function is intended for easily hashing a GP11Module to add to 
+ * a GHashTable or similar data structure.
+ * 
+ * Return value: An integer that can be used as a hash value, or 0 if invalid.
+ **/
+guint
+gp11_module_hash (gconstpointer module)
+{
+	GP11ModuleData *data;
+	
+	g_return_val_if_fail (GP11_IS_MODULE (module), 0);
+
+	data = GP11_MODULE_GET_DATA (module);
+	
+	return g_direct_hash (data->funcs);
+}
+
+/**
  * gp11_module_get_info:
  * @self: The module to get info for.
  * 

Modified: trunk/gp11/gp11-object.c
==============================================================================
--- trunk/gp11/gp11-object.c	(original)
+++ trunk/gp11/gp11-object.c	Tue Jan 20 20:38:29 2009
@@ -310,6 +310,58 @@
 }
 
 /**
+ * gp11_object_equal:
+ * @object1: A pointer to the first GP11Object
+ * @object2: A pointer to the second GP11Object
+ * 
+ * Checks equality of two objects. Two GP11Object objects can point to the same 
+ * underlying PKCS#11 object.
+ * 
+ * Return value: TRUE if object1 and object2 are equal. FALSE if either is not a GP11Object.
+ **/
+gboolean
+gp11_object_equal (gconstpointer object1, gconstpointer object2)
+{
+	GP11ObjectData *data1, *data2;
+
+	if (object1 == object2)
+		return TRUE;
+	if (!GP11_IS_OBJECT (object1) || !GP11_IS_OBJECT (object2))
+		return FALSE;
+	
+	data1 = GP11_OBJECT_GET_DATA (object1);
+	data2 = GP11_OBJECT_GET_DATA (object2);
+	
+	return data1->handle == data2->handle && 
+	       gp11_slot_equal (data1->slot, data2->slot);
+}
+
+/**
+ * gp11_object_hash:
+ * @object: A pointer to a GP11Object
+ * 
+ * Create a hash value for the GP11Object. 
+ * 
+ * This function is intended for easily hashing a GP11Object to add to 
+ * a GHashTable or similar data structure.
+ * 
+ * Return value: An integer that can be used as a hash value, or 0 if invalid.
+ **/
+guint
+gp11_object_hash (gconstpointer object)
+{
+	GP11ObjectData *data;
+	
+	g_return_val_if_fail (GP11_IS_OBJECT (object), 0);
+
+	data = GP11_OBJECT_GET_DATA (object);
+	
+	return _gp11_ulong_hash (&data->handle) ^
+	       gp11_slot_hash (data->slot);
+}
+
+
+/**
  * gp11_object_get_handle:
  * @self: The object.
  * 

Modified: trunk/gp11/gp11-slot.c
==============================================================================
--- trunk/gp11/gp11-slot.c	(original)
+++ trunk/gp11/gp11-slot.c	Tue Jan 20 20:38:29 2009
@@ -295,6 +295,57 @@
 }
 
 /**
+ * gp11_slot_equal:
+ * @slot1: A pointer to the first GP11Slot
+ * @slot2: A pointer to the second GP11Slot
+ * 
+ * Checks equality of two slots. Two GP11Slot objects can point to the same 
+ * underlying PKCS#11 slot.
+ * 
+ * Return value: TRUE if slot1 and slot2 are equal. FALSE if either is not a GP11Slot.
+ **/
+gboolean
+gp11_slot_equal (gconstpointer slot1, gconstpointer slot2)
+{
+	GP11SlotData *data1, *data2;
+
+	if (slot1 == slot2)
+		return TRUE;
+	if (!GP11_IS_SLOT (slot1) || !GP11_IS_SLOT (slot2))
+		return FALSE;
+	
+	data1 = GP11_SLOT_GET_DATA (slot1);
+	data2 = GP11_SLOT_GET_DATA (slot2);
+	
+	return data1->handle == data2->handle && 
+	       gp11_module_equal (data1->module, data2->module);
+}
+
+/**
+ * gp11_slot_hash:
+ * @slot: A pointer to a GP11Slot
+ * 
+ * Create a hash value for the GP11Slot. 
+ * 
+ * This function is intended for easily hashing a GP11Slot to add to 
+ * a GHashTable or similar data structure.
+ * 
+ * Return value: An integer that can be used as a hash value, or 0 if invalid.
+ **/
+guint
+gp11_slot_hash (gconstpointer slot)
+{
+	GP11SlotData *data;
+	
+	g_return_val_if_fail (GP11_IS_SLOT (slot), 0);
+
+	data = GP11_SLOT_GET_DATA (slot);
+	
+	return _gp11_ulong_hash (&data->handle) ^
+	       gp11_module_hash (data->module);
+}
+
+/**
  * gp11_slot_get_handle:
  * @self: The slot to get the handle of.
  * 

Modified: trunk/gp11/gp11.h
==============================================================================
--- trunk/gp11/gp11.h	(original)
+++ trunk/gp11/gp11.h	Tue Jan 20 20:38:29 2009
@@ -288,6 +288,11 @@
                                                                gpointer reserved,
                                                                GError **err);
 
+gboolean              gp11_module_equal                       (gconstpointer module1,
+                                                               gconstpointer module2);
+
+guint                 gp11_module_hash                        (gconstpointer module);
+
 const gchar*          gp11_module_get_path                    (GP11Module *self);
 
 CK_FUNCTION_LIST_PTR  gp11_module_get_functions               (GP11Module *self);
@@ -428,6 +433,11 @@
 
 GType               gp11_slot_get_type                      (void) G_GNUC_CONST;
 
+gboolean            gp11_slot_equal                         (gconstpointer slot1,
+                                                             gconstpointer slot2);
+
+guint               gp11_slot_hash                          (gconstpointer slot);
+
 GP11Module*         gp11_slot_get_module                    (GP11Slot *self);
 
 CK_SLOT_ID          gp11_slot_get_handle                    (GP11Slot *self);
@@ -1265,6 +1275,11 @@
                                                              CK_OBJECT_HANDLE_PTR handles,
                                                              CK_ULONG n_handles);
 
+gboolean            gp11_object_equal                       (gconstpointer slot1,
+                                                             gconstpointer slot2);
+
+guint               gp11_object_hash                        (gconstpointer slot);
+
 GP11Module*         gp11_object_get_module                  (GP11Object *self);
 
 GP11Slot*           gp11_object_get_slot                    (GP11Object *self);

Modified: trunk/gp11/tests/unit-test-gp11-module.c
==============================================================================
--- trunk/gp11/tests/unit-test-gp11-module.c	(original)
+++ trunk/gp11/tests/unit-test-gp11-module.c	Tue Jan 20 20:38:29 2009
@@ -33,7 +33,29 @@
 	/* Shouldn't be able to load any file successfully */ 
 	invalid = gp11_module_initialize ("/usr/lib/libm.so", NULL, &err);
 	FAIL_RES (invalid, err);
+}
 
+DEFINE_TEST(module_equals_hash)
+{
+	GP11Module *other;
+	GObject *obj;
+	guint hash;
+	
+	hash = gp11_module_hash (module);
+	g_assert (hash != 0);
+	
+	g_assert (gp11_module_equal (module, module));
+	
+	other = gp11_module_new (gp11_module_get_functions (module));
+	obj = g_object_new (G_TYPE_OBJECT, NULL);
+	
+	g_assert (gp11_module_equal (module, other));
+	
+	/* TODO: Could do with another test for inequality */
+	g_assert (!gp11_module_equal (module, obj));
+	
+	g_object_unref (other);
+	g_object_unref (obj);
 }
 
 DEFINE_TEST(module_props)

Modified: trunk/gp11/tests/unit-test-gp11-object.c
==============================================================================
--- trunk/gp11/tests/unit-test-gp11-object.c	(original)
+++ trunk/gp11/tests/unit-test-gp11-object.c	Tue Jan 20 20:38:29 2009
@@ -58,6 +58,37 @@
 	g_assert (handle == 2);
 }
 
+DEFINE_TEST(object_equals_hash)
+{
+	GP11Slot *other_slot;
+	GP11Object *other_object;
+	GObject *obj;
+	guint hash;
+	
+	hash = gp11_object_hash (object);
+	g_assert (hash != 0);
+	
+	g_assert (gp11_object_equal (object, object));
+	
+	other_slot = g_object_new (GP11_TYPE_SLOT, "module", module, "handle", 5895, NULL);
+	other_object = gp11_object_from_handle (other_slot, gp11_object_get_handle (object));
+	g_assert (!gp11_object_equal (object, other_object));
+	g_object_unref (other_slot);
+	g_object_unref (other_object);
+	
+	obj = g_object_new (G_TYPE_OBJECT, NULL);
+	g_assert (!gp11_object_equal (object, obj));
+	g_object_unref (obj);
+
+	other_object = gp11_object_from_handle (slot, 383838);
+	g_assert (!gp11_object_equal (object, other_object));
+	g_object_unref (other_object);
+	
+	other_object = gp11_object_from_handle (slot, gp11_object_get_handle (object));
+	g_assert (gp11_object_equal (object, other_object));
+	g_object_unref (other_object);
+}
+
 static void 
 fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data)
 {

Modified: trunk/gp11/tests/unit-test-gp11-slot.c
==============================================================================
--- trunk/gp11/tests/unit-test-gp11-slot.c	(original)
+++ trunk/gp11/tests/unit-test-gp11-slot.c	Tue Jan 20 20:38:29 2009
@@ -99,6 +99,33 @@
 	g_object_unref (mod);
 }
 
+DEFINE_TEST(slot_equals_hash)
+{
+	GP11Module *other_mod;
+	GP11Slot *other_slot;
+	GObject *obj;
+	guint hash;
+	
+	hash = gp11_slot_hash (slot);
+	g_assert (hash != 0);
+	
+	g_assert (gp11_slot_equal (slot, slot));
+	
+	other_mod = gp11_module_new (gp11_module_get_functions (module));
+	other_slot = g_object_new (GP11_TYPE_SLOT, "module", other_mod, "handle", gp11_slot_get_handle (slot), NULL);
+	g_assert (gp11_slot_equal (slot, other_slot));
+	g_object_unref (other_mod);
+	g_object_unref (other_slot);
+	
+	obj = g_object_new (G_TYPE_OBJECT, NULL);
+	g_assert (!gp11_slot_equal (slot, obj));
+	g_object_unref (obj);
+
+	other_slot = g_object_new (GP11_TYPE_SLOT, "module", module, "handle", 8909, NULL);
+	g_assert (!gp11_slot_equal (slot, obj));
+	g_object_unref (other_slot);
+}
+
 DEFINE_TEST(slot_mechanisms)
 {
 	GP11Mechanisms *mechs;



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