[gnome-keyring/gck-work] [gck] Objects keep reference to sessions not slots.



commit 822991de8a7428845b8afdc6736c95ded298beba
Author: Stef Walter <stef memberwebs com>
Date:   Fri Jul 30 09:21:28 2010 +0200

    [gck] Objects keep reference to sessions not slots.
    
     * This better models the way that PKCS#11 actually works.

 gck/gck-module.c            |    7 +-
 gck/gck-object.c            |  335 +++++++++----------------------------------
 gck/gck-session.c           |   31 ++---
 gck/gck.h                   |   10 +-
 gck/tests/gck-test-module.c |   31 ++---
 gck/tests/gck-test.h        |    9 ++
 gck/tests/test-gck-crypto.c |    2 -
 gck/tests/test-gck-object.c |   77 ++--------
 8 files changed, 122 insertions(+), 380 deletions(-)
---
diff --git a/gck/gck-module.c b/gck/gck-module.c
index 096a044..d194384 100644
--- a/gck/gck-module.c
+++ b/gck/gck-module.c
@@ -412,6 +412,7 @@ _gck_module_fire_authenticate_object (GckModule *self, GckObject *object,
                                       gchar *label, gchar **password)
 {
 	GckTokenInfo *info;
+	GckSession *session;
 	GckSlot *slot;
 	gboolean ret;
 
@@ -419,7 +420,10 @@ _gck_module_fire_authenticate_object (GckModule *self, GckObject *object,
 	g_assert (GCK_IS_OBJECT (object));
 	g_assert (password);
 
-	slot = gck_object_get_slot (object);
+	session = gck_object_get_session (object);
+	slot = gck_session_get_slot (session);
+	g_object_unref (session);
+
 	info = gck_slot_get_token_info (slot);
 	g_object_unref (slot);
 
@@ -1196,7 +1200,6 @@ gck_module_enumerate_objects_full (GckModule *self, GckAttributes *attrs,
 		}
 
 		for (o = objects; !stop && o; o = g_list_next (o)) {
-			gck_object_set_session (o->data, session);
 			if (!(func)(o->data, user_data)) {
 				stop = TRUE;
 				break;
diff --git a/gck/gck-object.c b/gck/gck-object.c
index bc3e8ad..c1e2128 100644
--- a/gck/gck-object.c
+++ b/gck/gck-object.c
@@ -52,21 +52,18 @@
 enum {
 	PROP_0,
 	PROP_MODULE,
-	PROP_SLOT,
-	PROP_HANDLE,
-	PROP_SESSION
+	PROP_SESSION,
+	PROP_HANDLE
 };
 
 typedef struct _GckObjectData {
 	GckModule *module;
-	GckSlot *slot;
+	GckSession *session;
 	CK_OBJECT_HANDLE handle;
 } GckObjectData;
 
 typedef struct _GckObjectPrivate {
 	GckObjectData data;
-	GStaticMutex mutex;
-	GckSession *session;
 } GckObjectPrivate;
 
 #define GCK_OBJECT_GET_DATA(o) \
@@ -75,90 +72,13 @@ typedef struct _GckObjectPrivate {
 G_DEFINE_TYPE (GckObject, gck_object, G_TYPE_OBJECT);
 
 /* ----------------------------------------------------------------------------
- * INTERNAL
- */
-
-static void
-run_call_with_session (GckCall *call, GckSession *session)
-{
-	g_assert (GCK_IS_CALL (call));
-	g_assert (GCK_IS_SESSION (session));
-
-	/* Hold onto this session for the length of the call */
-	g_object_set_data_full (G_OBJECT (call), "call-opened-session",
-	                        g_object_ref (session), g_object_unref);
-
-	_gck_call_async_object (call, session);
-	_gck_call_async_go (call);
-}
-
-static void
-opened_session (GObject *obj, GAsyncResult *result, gpointer user_data)
-{
-	GckSession *session;
-	GError *err = NULL;
-	GckCall *call;
-
-	g_assert (GCK_IS_CALL (user_data));
-	call = GCK_CALL (user_data);
-
-	session = gck_slot_open_session_finish (GCK_SLOT (obj), result, &err);
-
-	/* Transtfer the error to the outer call and finish */
-	if (!session) {
-		_gck_call_async_short (user_data, err->code);
-		g_error_free (err);
-		return;
-	}
-
-	run_call_with_session (GCK_CALL (user_data), session);
-	g_object_unref (session);
-}
-
-static void
-require_session_async (GckObject *self, GckCall *call,
-                       gulong flags, GCancellable *cancellable)
-{
-	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
-	GckSession *session;
-
-	g_assert (GCK_IS_OBJECT (self));
-
-	session = gck_object_get_session (self);
-	if (session) {
-		run_call_with_session (call, session);
-		g_object_unref (session);
-	} else {
-		gck_slot_open_session_async (data->slot, flags, NULL, NULL,
-		                              cancellable, opened_session, call);
-	}
-
-}
-
-static GckSession*
-require_session_sync (GckObject *self, gulong flags, GError **err)
-{
-	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
-	GckSession *session;
-
-	g_assert (GCK_IS_OBJECT (self));
-
-	session = gck_object_get_session (self);
-	if (session)
-		return session;
-
-	return gck_slot_open_session (data->slot, flags, err);
-}
-
-/* ----------------------------------------------------------------------------
  * OBJECT
  */
 
 static void
 gck_object_init (GckObject *self)
 {
-	GckObjectPrivate *pv = (G_TYPE_INSTANCE_GET_PRIVATE(self, GCK_TYPE_OBJECT, GckObjectPrivate));
-	g_static_mutex_init (&pv->mutex);
+
 }
 
 static void
@@ -171,9 +91,6 @@ gck_object_get_property (GObject *obj, guint prop_id, GValue *value,
 	case PROP_MODULE:
 		g_value_take_object (value, gck_object_get_module (self));
 		break;
-	case PROP_SLOT:
-		g_value_take_object (value, gck_object_get_slot (self));
-		break;
 	case PROP_SESSION:
 		g_value_take_object (value, gck_object_get_session (self));
 		break;
@@ -188,7 +105,6 @@ gck_object_set_property (GObject *obj, guint prop_id, const GValue *value,
                           GParamSpec *pspec)
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (obj);
-	GckObject *self = GCK_OBJECT (obj);
 
 	/* The sets to data below are only allowed during construction */
 
@@ -199,14 +115,11 @@ gck_object_set_property (GObject *obj, guint prop_id, const GValue *value,
 		g_return_if_fail (data->module);
 		g_object_ref (data->module);
 		break;
-	case PROP_SLOT:
-		g_return_if_fail (!data->slot);
-		data->slot = g_value_get_object (value);
-		g_return_if_fail (data->slot);
-		g_object_ref (data->slot);
-		break;
 	case PROP_SESSION:
-		gck_object_set_session (self, g_value_get_object (value));
+		g_return_if_fail (!data->session);
+		data->session = g_value_get_object (value);
+		g_return_if_fail (data->session);
+		g_object_ref (data->session);
 		break;
 	case PROP_HANDLE:
 		g_return_if_fail (!data->handle);
@@ -218,25 +131,18 @@ gck_object_set_property (GObject *obj, guint prop_id, const GValue *value,
 static void
 gck_object_finalize (GObject *obj)
 {
-	GckObjectPrivate *pv = (G_TYPE_INSTANCE_GET_PRIVATE(obj, GCK_TYPE_OBJECT, GckObjectPrivate));
 	GckObjectData *data = GCK_OBJECT_GET_DATA (obj);
 
-	if (data->slot)
-		g_object_unref (data->slot);
-	data->slot = NULL;
+	if (data->session)
+		g_object_unref (data->session);
+	data->session = NULL;
 
 	if (data->module)
 		g_object_unref (data->module);
 	data->module = NULL;
 
-	if (pv->session)
-		g_object_unref (pv->session);
-	pv->session = NULL;
-
 	data->handle = 0;
 
-	g_static_mutex_free (&pv->mutex);
-
 	G_OBJECT_CLASS (gck_object_parent_class)->finalize (obj);
 }
 
@@ -261,18 +167,6 @@ gck_object_class_init (GckObjectClass *klass)
 		                     GCK_TYPE_MODULE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
 	/**
-	 * GckObject:slot:
-	 *
-	 * The GckSlot that this object belongs to.
-	 *
-	 * If this is a token object then it will be stored on the token in this slot.
-	 * If this is a session object, then it belongs to a session opened on this slot.
-	 */
-	g_object_class_install_property (gobject_class, PROP_SLOT,
-		g_param_spec_object ("slot", "slot", "PKCS11 Slot",
-		                     GCK_TYPE_SLOT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
-
-	/**
 	 * GckObject:handle:
 	 *
 	 * The raw PKCS11 handle for this object.
@@ -292,7 +186,7 @@ gck_object_class_init (GckObjectClass *klass)
 	 */
 	g_object_class_install_property (gobject_class, PROP_SESSION,
 		g_param_spec_object ("session", "session", "PKCS11 Session to make calls on",
-		                     GCK_TYPE_SESSION, G_PARAM_READWRITE));
+		                     GCK_TYPE_SESSION, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
 	g_type_class_add_private (klass, sizeof (GckObjectPrivate));
 }
@@ -303,7 +197,7 @@ gck_object_class_init (GckObjectClass *klass)
 
 /**
  * gck_object_from_handle:
- * @slot: The slot on which this object is present.
+ * @session: The session through which this object is accessed or created.
  * @handle: The raw handle of the object.
  *
  * Initialize a GckObject from a raw PKCS#11 handle. Normally you would use
@@ -312,15 +206,15 @@ gck_object_class_init (GckObjectClass *klass)
  * Return value: The new GckObject. You should use g_object_unref() when done with this object.
  **/
 GckObject*
-gck_object_from_handle (GckSlot *slot, CK_OBJECT_HANDLE handle)
+gck_object_from_handle (GckSession *session, CK_OBJECT_HANDLE handle)
 {
 	GckModule *module = NULL;
 	GckObject *object;
 
-	g_return_val_if_fail (GCK_IS_SLOT (slot), NULL);
+	g_return_val_if_fail (GCK_IS_SESSION (session), NULL);
 
-	module = gck_slot_get_module (slot);
-	object = g_object_new (GCK_TYPE_OBJECT, "module", module, "handle", handle, "slot", slot, NULL);
+	module = gck_session_get_module (session);
+	object = g_object_new (GCK_TYPE_OBJECT, "module", module, "handle", handle, "session", session, NULL);
 	g_object_unref (module);
 
 	return object;
@@ -339,16 +233,16 @@ gck_object_from_handle (GckSlot *slot, CK_OBJECT_HANDLE handle)
  * this list.
  **/
 GList*
-gck_objects_from_handle_array (GckSlot *slot, CK_OBJECT_HANDLE_PTR handles, CK_ULONG n_handles)
+gck_objects_from_handle_array (GckSession *session, CK_OBJECT_HANDLE_PTR handles, CK_ULONG n_handles)
 {
 	GList *results = NULL;
 	CK_ULONG i;
 
-	g_return_val_if_fail (GCK_IS_SLOT (slot), NULL);
+	g_return_val_if_fail (GCK_IS_SESSION (session), NULL);
 	g_return_val_if_fail (handles || !n_handles, NULL);
 
 	for (i = 0; i < n_handles; ++i)
-		results = g_list_prepend (results, gck_object_from_handle (slot, handles[i]));
+		results = g_list_prepend (results, gck_object_from_handle (session, handles[i]));
 	return g_list_reverse (results);
 }
 
@@ -366,6 +260,8 @@ gboolean
 gck_object_equal (gconstpointer object1, gconstpointer object2)
 {
 	GckObjectData *data1, *data2;
+	GckSlot *slot1, *slot2;
+	gboolean ret;
 
 	if (object1 == object2)
 		return TRUE;
@@ -375,8 +271,16 @@ gck_object_equal (gconstpointer object1, gconstpointer object2)
 	data1 = GCK_OBJECT_GET_DATA (object1);
 	data2 = GCK_OBJECT_GET_DATA (object2);
 
-	return data1->handle == data2->handle &&
-	       gck_slot_equal (data1->slot, data2->slot);
+	slot1 = gck_session_get_slot (data1->session);
+	slot2 = gck_session_get_slot (data2->session);
+
+	ret = data1->handle == data2->handle &&
+	      gck_slot_equal (slot1, slot2);
+
+	g_object_unref (slot1);
+	g_object_unref (slot2);
+
+	return ret;
 }
 
 /**
@@ -394,13 +298,20 @@ guint
 gck_object_hash (gconstpointer object)
 {
 	GckObjectData *data;
+	GckSlot *slot;
+	guint hash;
 
 	g_return_val_if_fail (GCK_IS_OBJECT (object), 0);
 
 	data = GCK_OBJECT_GET_DATA (object);
+	slot = gck_session_get_slot (data->session);
+
+	hash = _gck_ulong_hash (&data->handle) ^
+	       gck_slot_hash (slot);
+
+	g_object_unref (slot);
 
-	return _gck_ulong_hash (&data->handle) ^
-	       gck_slot_hash (data->slot);
+	return hash;
 }
 
 
@@ -437,22 +348,6 @@ gck_object_get_module (GckObject *self)
 	return g_object_ref (data->module);
 }
 
-/**
- * gck_object_get_slot:
- * @self: The object.
- *
- * Get the PKCS#11 slot to which this object belongs.
- *
- * Return value: The slot, which should be unreffed after use.
- **/
-GckSlot*
-gck_object_get_slot (GckObject *self)
-{
-	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
-	g_return_val_if_fail (GCK_IS_OBJECT (self), NULL);
-	g_return_val_if_fail (GCK_IS_SLOT (data->slot), NULL);
-	return g_object_ref (data->slot);
-}
 
 /**
  * gck_object_get_session:
@@ -470,55 +365,10 @@ gck_object_get_slot (GckObject *self)
 GckSession*
 gck_object_get_session (GckObject *self)
 {
-	GckObjectPrivate *pv = (G_TYPE_INSTANCE_GET_PRIVATE (self, GCK_TYPE_OBJECT, GckObjectPrivate));
-	GckSession *session;
-
+	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	g_return_val_if_fail (GCK_IS_OBJECT (self), NULL);
-
-	g_static_mutex_lock (&pv->mutex);
-
-	{
-		session = pv->session ? g_object_ref (pv->session) : NULL;
-	}
-
-	g_static_mutex_unlock (&pv->mutex);
-
-	return session;
-}
-
-/**
- * gck_object_set_session:
- * @self: The object
- * @session: The assigned session
- *
- * Set the PKCS#11 session assigned to make calls on when operating
- * on this object.
- *
- * It isn't always necessary to assign a session to an object.
- * By default an object will open and close sessions appropriate for
- * its calls.
- *
- * If you assign a read-only session, then calls on this object
- * that modify the state of the object will probably fail.
- **/
-void
-gck_object_set_session (GckObject *self, GckSession *session)
-{
-	GckObjectPrivate *pv = (G_TYPE_INSTANCE_GET_PRIVATE (self, GCK_TYPE_OBJECT, GckObjectPrivate));
-
-	g_return_if_fail (GCK_IS_OBJECT (self));
-
-	g_static_mutex_lock (&pv->mutex);
-
-	{
-		if (session)
-			g_object_ref (session);
-		if (pv->session)
-			g_object_unref (pv->session);
-		pv->session = session;
-	}
-
-	g_static_mutex_unlock (&pv->mutex);
+	g_return_val_if_fail (GCK_IS_SESSION (data->session), NULL);
+	return g_object_ref (data->session);
 }
 
 /* --------------------------------------------------------------------------------------
@@ -571,20 +421,13 @@ gck_object_destroy_full (GckObject *self, GCancellable *cancellable, GError **er
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	Destroy args = { GCK_ARGUMENTS_INIT, 0 };
-	GckSession *session;
-	gboolean ret = FALSE;
 
 	g_return_val_if_fail (GCK_IS_OBJECT (self), FALSE);
-	g_return_val_if_fail (GCK_IS_SLOT (data->slot), FALSE);
+	g_return_val_if_fail (GCK_IS_SESSION (data->session), FALSE);
 	g_return_val_if_fail (!err || !*err, FALSE);
 
 	args.object = data->handle;
-
-	session = require_session_sync (self, CKF_RW_SESSION, err);
-	if (session)
-		ret = _gck_call_sync (session, perform_destroy, NULL, &args, cancellable, err);
-	g_object_unref (session);
-	return ret;
+	return _gck_call_sync (data->session, perform_destroy, NULL, &args, cancellable, err);
 }
 
 /**
@@ -603,16 +446,14 @@ gck_object_destroy_async (GckObject *self, GCancellable *cancellable,
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	Destroy* args;
-	GckCall *call;
 
 	g_return_if_fail (GCK_IS_OBJECT (self));
-	g_return_if_fail (GCK_IS_SLOT (data->slot));
+	g_return_if_fail (GCK_IS_SESSION (data->session));
 
-	args = _gck_call_async_prep (data->slot, self, perform_destroy, NULL, sizeof (*args), NULL);
+	args = _gck_call_async_prep (data->session, self, perform_destroy, NULL, sizeof (*args), NULL);
 	args->object = data->handle;
 
-	call = _gck_call_async_ready (args, cancellable, callback, user_data);
-	require_session_async (self, call, CKF_RW_SESSION, cancellable);
+	_gck_call_async_ready_go (args, cancellable, callback, user_data);
 }
 
 /**
@@ -730,11 +571,10 @@ gck_object_set (GckObject *self, GError **err, ...)
  **/
 gboolean
 gck_object_set_full (GckObject *self, GckAttributes *attrs,
-                      GCancellable *cancellable, GError **err)
+                     GCancellable *cancellable, GError **err)
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	SetAttributes args;
-	GckSession *session;
 	gboolean ret = FALSE;
 
 	g_return_val_if_fail (GCK_IS_OBJECT (self), FALSE);
@@ -747,12 +587,9 @@ gck_object_set_full (GckObject *self, GckAttributes *attrs,
 	args.attrs = attrs;
 	args.object = data->handle;
 
-	session = require_session_sync (self, CKF_RW_SESSION, err);
-	if (session)
-		ret = _gck_call_sync (session, perform_set_attributes, NULL, &args, cancellable, err);
+	ret = _gck_call_sync (data->session, perform_set_attributes, NULL, &args, cancellable, err);
 
 	_gck_attributes_unlock (attrs);
-	g_object_unref (session);
 	return ret;
 }
 
@@ -773,20 +610,18 @@ gck_object_set_async (GckObject *self, GckAttributes *attrs, GCancellable *cance
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	SetAttributes *args;
-	GckCall *call;
 
 	g_return_if_fail (GCK_IS_OBJECT (self));
 	g_return_if_fail (attrs);
 
-	args = _gck_call_async_prep (data->slot, self, perform_set_attributes,
-	                              NULL, sizeof (*args), free_set_attributes);
+	args = _gck_call_async_prep (data->session, self, perform_set_attributes,
+	                             NULL, sizeof (*args), free_set_attributes);
 
 	_gck_attributes_lock (attrs);
 	args->attrs = gck_attributes_ref (attrs);
 	args->object = data->handle;
 
-	call = _gck_call_async_ready (args, cancellable, callback, user_data);
-	require_session_async (self, call, CKF_RW_SESSION, cancellable);
+	_gck_call_async_ready_go (args, cancellable, callback, user_data);
 }
 
 /**
@@ -949,26 +784,20 @@ gck_object_get_full (GckObject *self, GckAttributes *attrs,
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	GetAttributes args;
-	GckSession *session;
 	gboolean ret;
 
 	g_return_val_if_fail (GCK_IS_OBJECT (self), NULL);
 	g_return_val_if_fail (attrs, NULL);
 	g_return_val_if_fail (!err || !*err, NULL);
 
-	session = require_session_sync (self, 0, err);
-	if (!session)
-		return NULL;
-
 	_gck_attributes_lock (attrs);
 
 	memset (&args, 0, sizeof (args));
 	args.attrs = attrs;
 	args.object = data->handle;
 
-	ret = _gck_call_sync (session, perform_get_attributes, NULL, &args, cancellable, err);
+	ret = _gck_call_sync (data->session, perform_get_attributes, NULL, &args, cancellable, err);
 	_gck_attributes_unlock (attrs);
-	g_object_unref (session);
 
 	return ret ? attrs : NULL;
 }
@@ -994,20 +823,18 @@ gck_object_get_async (GckObject *self, GckAttributes *attrs, GCancellable *cance
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	GetAttributes *args;
-	GckCall *call;
 
 	g_return_if_fail (GCK_IS_OBJECT (self));
 	g_return_if_fail (attrs);
 
-	args = _gck_call_async_prep (data->slot, self, perform_get_attributes,
-	                              NULL, sizeof (*args), free_get_attributes);
+	args = _gck_call_async_prep (data->session, self, perform_get_attributes,
+	                             NULL, sizeof (*args), free_get_attributes);
 
 	_gck_attributes_lock (attrs);
 	args->attrs = gck_attributes_ref (attrs);
 	args->object = data->handle;
 
-	call = _gck_call_async_ready (args, cancellable, callback, user_data);
-	require_session_async (self, call, 0, cancellable);
+	_gck_call_async_ready_go (args, cancellable, callback, user_data);
 }
 
 /**
@@ -1145,7 +972,6 @@ gck_object_get_data_full (GckObject *self, gulong attr_type, GckAllocator alloca
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	GetAttributeData args;
-	GckSession *session;
 	gboolean ret;
 
 	g_return_val_if_fail (GCK_IS_OBJECT (self), NULL);
@@ -1155,17 +981,12 @@ gck_object_get_data_full (GckObject *self, gulong attr_type, GckAllocator alloca
 	if (!allocator)
 		allocator = g_realloc;
 
-	session = require_session_sync (self, 0, err);
-	if (!session)
-		return NULL;
-
 	memset (&args, 0, sizeof (args));
 	args.allocator = allocator;
 	args.object = data->handle;
 	args.type = attr_type;
 
-	ret = _gck_call_sync (session, perform_get_attribute_data, NULL, &args, cancellable, err);
-	g_object_unref (session);
+	ret = _gck_call_sync (data->session, perform_get_attribute_data, NULL, &args, cancellable, err);
 
 	/* Free any value if failed */
 	if (!ret) {
@@ -1198,22 +1019,20 @@ gck_object_get_data_async (GckObject *self, gulong attr_type, GckAllocator alloc
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	GetAttributeData *args;
-	GckCall *call;
 
 	g_return_if_fail (GCK_IS_OBJECT (self));
 
 	if (!allocator)
 		allocator = g_realloc;
 
-	args = _gck_call_async_prep (data->slot, self, perform_get_attribute_data,
-	                              NULL, sizeof (*args), free_get_attribute_data);
+	args = _gck_call_async_prep (data->session, self, perform_get_attribute_data,
+	                             NULL, sizeof (*args), free_get_attribute_data);
 
 	args->allocator = allocator;
 	args->object = data->handle;
 	args->type = attr_type;
 
-	call = _gck_call_async_ready (args, cancellable, callback, user_data);
-	require_session_async (self, call, 0, cancellable);
+	_gck_call_async_ready_go (args, cancellable, callback, user_data);
 }
 
 /**
@@ -1332,7 +1151,6 @@ gck_object_set_template_full (GckObject *self, gulong attr_type, GckAttributes *
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	set_template_args args;
-	GckSession *session;
 	gboolean ret = FALSE;
 
 	g_return_val_if_fail (GCK_IS_OBJECT (self), FALSE);
@@ -1346,12 +1164,9 @@ gck_object_set_template_full (GckObject *self, gulong attr_type, GckAttributes *
 	args.type = attr_type;
 	args.object = data->handle;
 
-	session = require_session_sync (self, CKF_RW_SESSION, err);
-	if (session)
-		ret = _gck_call_sync (session, perform_set_template, NULL, &args, cancellable, err);
+	ret = _gck_call_sync (data->session, perform_set_template, NULL, &args, cancellable, err);
 
 	_gck_attributes_unlock (attrs);
-	g_object_unref (session);
 	return ret;
 }
 
@@ -1376,21 +1191,19 @@ gck_object_set_template_async (GckObject *self, gulong attr_type, GckAttributes
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	set_template_args *args;
-	GckCall *call;
 
 	g_return_if_fail (GCK_IS_OBJECT (self));
 	g_return_if_fail (attrs);
 
-	args = _gck_call_async_prep (data->slot, self, perform_set_template,
-	                              NULL, sizeof (*args), free_set_template);
+	args = _gck_call_async_prep (data->session, self, perform_set_template,
+	                             NULL, sizeof (*args), free_set_template);
 
 	_gck_attributes_lock (attrs);
 	args->attrs = gck_attributes_ref (attrs);
 	args->type = attr_type;
 	args->object = data->handle;
 
-	call = _gck_call_async_ready (args, cancellable, callback, user_data);
-	require_session_async (self, call, CKF_RW_SESSION, cancellable);
+	_gck_call_async_ready_go (args, cancellable, callback, user_data);
 }
 
 /**
@@ -1523,22 +1336,16 @@ gck_object_get_template_full (GckObject *self, gulong attr_type,
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	get_template_args args;
-	GckSession *session;
 	gboolean ret;
 
 	g_return_val_if_fail (GCK_IS_OBJECT (self), NULL);
 	g_return_val_if_fail (!err || !*err, NULL);
 
-	session = require_session_sync (self, 0, err);
-	if (!session)
-		return NULL;
-
 	memset (&args, 0, sizeof (args));
 	args.object = data->handle;
 	args.type = attr_type;
 
-	ret = _gck_call_sync (session, perform_get_template, NULL, &args, cancellable, err);
-	g_object_unref (session);
+	ret = _gck_call_sync (data->session, perform_get_template, NULL, &args, cancellable, err);
 
 	_gck_attributes_unlock (args.attrs);
 
@@ -1571,18 +1378,16 @@ gck_object_get_template_async (GckObject *self, gulong attr_type,
 {
 	GckObjectData *data = GCK_OBJECT_GET_DATA (self);
 	get_template_args *args;
-	GckCall *call;
 
 	g_return_if_fail (GCK_IS_OBJECT (self));
 
-	args = _gck_call_async_prep (data->slot, self, perform_get_template,
-	                              NULL, sizeof (*args), free_get_template);
+	args = _gck_call_async_prep (data->session, self, perform_get_template,
+	                             NULL, sizeof (*args), free_get_template);
 
 	args->object = data->handle;
 	args->type = attr_type;
 
-	call = _gck_call_async_ready (args, cancellable, callback, user_data);
-	require_session_async (self, call, 0, cancellable);
+	_gck_call_async_ready_go (args, cancellable, callback, user_data);
 }
 
 /**
diff --git a/gck/gck-session.c b/gck/gck-session.c
index dc34f2f..064a52d 100644
--- a/gck/gck-session.c
+++ b/gck/gck-session.c
@@ -995,7 +995,6 @@ GckObject*
 gck_session_create_object_full (GckSession *self, GckAttributes *attrs,
                                  GCancellable *cancellable, GError **err)
 {
-	GckSessionData *data = GCK_SESSION_GET_DATA (self);
 	CreateObject args = { GCK_ARGUMENTS_INIT, attrs, 0 };
 	gboolean ret;
 
@@ -1009,7 +1008,7 @@ gck_session_create_object_full (GckSession *self, GckAttributes *attrs,
 	if (!ret)
 		return NULL;
 
-	return gck_object_from_handle (data->slot, args.object);
+	return gck_object_from_handle (self, args.object);
 }
 
 /**
@@ -1052,7 +1051,6 @@ gck_session_create_object_async (GckSession *self, GckAttributes *attrs,
 GckObject*
 gck_session_create_object_finish (GckSession *self, GAsyncResult *result, GError **err)
 {
-	GckSessionData *data = GCK_SESSION_GET_DATA (self);
 	CreateObject *args;
 
 	args = _gck_call_arguments (result, CreateObject);
@@ -1060,7 +1058,7 @@ gck_session_create_object_finish (GckSession *self, GAsyncResult *result, GError
 
 	if (!_gck_call_basic_finish (result, err))
 		return NULL;
-	return gck_object_from_handle (data->slot, args->object);
+	return gck_object_from_handle (self, args->object);
 }
 
 
@@ -1142,12 +1140,11 @@ static GList*
 objlist_from_handles (GckSession *self, CK_OBJECT_HANDLE_PTR objects,
                       CK_ULONG n_objects)
 {
-	GckSessionData *data = GCK_SESSION_GET_DATA (self);
 	GList *results = NULL;
 
 	while (n_objects > 0) {
 		results = g_list_prepend (results,
-		                gck_object_from_handle (data->slot, objects[--n_objects]));
+		                gck_object_from_handle (self, objects[--n_objects]));
 	}
 
 	return g_list_reverse (results);
@@ -1340,7 +1337,6 @@ gck_session_generate_key_pair_full (GckSession *self, GckMechanism *mechanism,
                                      GckObject **public_key, GckObject **private_key,
                                      GCancellable *cancellable, GError **err)
 {
-	GckSessionData *data = GCK_SESSION_GET_DATA (self);
 	GenerateKeyPair args = { GCK_ARGUMENTS_INIT, mechanism, public_attrs, private_attrs, 0, 0 };
 	gboolean ret;
 
@@ -1362,8 +1358,8 @@ gck_session_generate_key_pair_full (GckSession *self, GckMechanism *mechanism,
 	if (!ret)
 		return FALSE;
 
-	*public_key = gck_object_from_handle (data->slot, args.public_key);
-	*private_key = gck_object_from_handle (data->slot, args.private_key);
+	*public_key = gck_object_from_handle (self, args.public_key);
+	*private_key = gck_object_from_handle (self, args.private_key);
 	return TRUE;
 }
 
@@ -1421,7 +1417,6 @@ gck_session_generate_key_pair_finish (GckSession *self, GAsyncResult *result,
                                        GckObject **public_key, GckObject **private_key,
                                        GError **err)
 {
-	GckSessionData *data = GCK_SESSION_GET_DATA (self);
 	GenerateKeyPair *args;
 
 	g_return_val_if_fail (GCK_IS_SESSION (self), FALSE);
@@ -1436,8 +1431,8 @@ gck_session_generate_key_pair_finish (GckSession *self, GAsyncResult *result,
 	if (!_gck_call_basic_finish (result, err))
 		return FALSE;
 
-	*public_key = gck_object_from_handle (data->slot, args->public_key);
-	*private_key = gck_object_from_handle (data->slot, args->private_key);
+	*public_key = gck_object_from_handle (self, args->public_key);
+	*private_key = gck_object_from_handle (self, args->private_key);
 	return TRUE;
 }
 
@@ -1733,7 +1728,6 @@ gck_session_unwrap_key_full (GckSession *self, GckObject *wrapper, GckMechanism
                               gconstpointer input, gsize n_input, GckAttributes *attrs,
                               GCancellable *cancellable, GError **err)
 {
-	GckSessionData *data = GCK_SESSION_GET_DATA (self);
 	UnwrapKey args = { GCK_ARGUMENTS_INIT, mechanism, attrs, 0, input, n_input, 0 };
 	gboolean ret;
 
@@ -1752,7 +1746,7 @@ gck_session_unwrap_key_full (GckSession *self, GckObject *wrapper, GckMechanism
 	if (!ret)
 		return NULL;
 
-	return gck_object_from_handle (data->slot, args.unwrapped);
+	return gck_object_from_handle (self, args.unwrapped);
 }
 
 /**
@@ -1808,7 +1802,6 @@ gck_session_unwrap_key_async (GckSession *self, GckObject *wrapper, GckMechanism
 GckObject*
 gck_session_unwrap_key_finish (GckSession *self, GAsyncResult *result, GError **err)
 {
-	GckSessionData *data = GCK_SESSION_GET_DATA (self);
 	UnwrapKey *args;
 
 	g_return_val_if_fail (GCK_IS_SESSION (self), NULL);
@@ -1818,7 +1811,7 @@ gck_session_unwrap_key_finish (GckSession *self, GAsyncResult *result, GError **
 
 	if (!_gck_call_basic_finish (result, err))
 		return NULL;
-	return gck_object_from_handle (data->slot, args->unwrapped);
+	return gck_object_from_handle (self, args->unwrapped);
 }
 
 /* -----------------------------------------------------------------------------
@@ -1927,7 +1920,6 @@ GckObject*
 gck_session_derive_key_full (GckSession *self, GckObject *base, GckMechanism *mechanism,
                               GckAttributes *attrs, GCancellable *cancellable, GError **err)
 {
-	GckSessionData *data = GCK_SESSION_GET_DATA (self);
 	DeriveKey args = { GCK_ARGUMENTS_INIT, mechanism, attrs, 0, 0 };
 	gboolean ret;
 
@@ -1946,7 +1938,7 @@ gck_session_derive_key_full (GckSession *self, GckObject *base, GckMechanism *me
 	if (!ret)
 		return NULL;
 
-	return gck_object_from_handle (data->slot, args.derived);
+	return gck_object_from_handle (self, args.derived);
 }
 
 /**
@@ -1997,7 +1989,6 @@ gck_session_derive_key_async (GckSession *self, GckObject *base, GckMechanism *m
 GckObject*
 gck_session_derive_key_finish (GckSession *self, GAsyncResult *result, GError **err)
 {
-	GckSessionData *data = GCK_SESSION_GET_DATA (self);
 	DeriveKey *args;
 
 	g_return_val_if_fail (GCK_IS_SESSION (self), NULL);
@@ -2008,7 +1999,7 @@ gck_session_derive_key_finish (GckSession *self, GAsyncResult *result, GError **
 	if (!_gck_call_basic_finish (result, err))
 		return NULL;
 
-	return gck_object_from_handle (data->slot, args->derived);
+	return gck_object_from_handle (self, args->derived);
 }
 
 /* --------------------------------------------------------------------------------------------------
diff --git a/gck/gck.h b/gck/gck.h
index 5e74b95..dc7a39c 100644
--- a/gck/gck.h
+++ b/gck/gck.h
@@ -1108,10 +1108,10 @@ struct _GckObjectClass {
 
 GType               gck_object_get_type                     (void) G_GNUC_CONST;
 
-GckObject*          gck_object_from_handle                  (GckSlot *slot,
+GckObject*          gck_object_from_handle                  (GckSession *session,
                                                              CK_OBJECT_HANDLE handle);
 
-GList*              gck_objects_from_handle_array           (GckSlot *slot,
+GList*              gck_objects_from_handle_array           (GckSession *session,
                                                              CK_OBJECT_HANDLE_PTR handles,
                                                              CK_ULONG n_handles);
 
@@ -1122,16 +1122,10 @@ guint               gck_object_hash                         (gconstpointer objec
 
 GckModule*          gck_object_get_module                   (GckObject *self);
 
-GckSlot*            gck_object_get_slot                     (GckObject *self);
-
 CK_OBJECT_HANDLE    gck_object_get_handle                   (GckObject *self);
 
 GckSession*         gck_object_get_session                  (GckObject *self);
 
-void                gck_object_set_session                  (GckObject *self,
-                                                             GckSession *session);
-
-
 #ifdef UNIMPLEMENTED
 
 GckObject*          gck_object_copy                         (GckObject *self,
diff --git a/gck/tests/gck-test-module.c b/gck/tests/gck-test-module.c
index e6ad4df..4fb8e60 100644
--- a/gck/tests/gck-test-module.c
+++ b/gck/tests/gck-test-module.c
@@ -231,15 +231,6 @@ test_C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list)
 	return C_GetFunctionList (list);
 }
 
-#define TEST_SLOT_ONE  52
-#define TEST_SLOT_TWO  134
-
-/*
- * Two slots
- *  ONE: token present
- *  TWO: token not present
- */
-
 static CK_RV
 test_C_GetSlotList (CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount)
 {
@@ -261,9 +252,9 @@ test_C_GetSlotList (CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PT
 	}
 
 	*pulCount = count;
-	pSlotList[0] = TEST_SLOT_ONE;
+	pSlotList[0] = GCK_TEST_SLOT_ONE;
 	if (!tokenPresent)
-		pSlotList[1] = TEST_SLOT_TWO;
+		pSlotList[1] = GCK_TEST_SLOT_TWO;
 
 	return CKR_OK;
 }
@@ -289,10 +280,10 @@ test_C_GetSlotInfo (CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo)
 {
 	g_assert (pInfo != NULL && "Invalid pInfo");
 
-	if (slotID == TEST_SLOT_ONE) {
+	if (slotID == GCK_TEST_SLOT_ONE) {
 		memcpy (pInfo, &TEST_INFO_ONE, sizeof (*pInfo));
 		return CKR_OK;
-	} else if (slotID == TEST_SLOT_TWO) {
+	} else if (slotID == GCK_TEST_SLOT_TWO) {
 		memcpy (pInfo, &TEST_INFO_TWO, sizeof (*pInfo));
 		return CKR_OK;
 	} else {
@@ -327,10 +318,10 @@ test_C_GetTokenInfo (CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo)
 {
 	g_assert (pInfo != NULL && "Invalid pInfo");
 
-	if (slotID == TEST_SLOT_ONE) {
+	if (slotID == GCK_TEST_SLOT_ONE) {
 		memcpy (pInfo, &TEST_TOKEN_ONE, sizeof (*pInfo));
 		return CKR_OK;
-	} else if (slotID == TEST_SLOT_TWO) {
+	} else if (slotID == GCK_TEST_SLOT_TWO) {
 		return CKR_TOKEN_NOT_PRESENT;
 	} else {
 		g_assert_not_reached (); /* "Invalid slot id" */
@@ -348,7 +339,7 @@ static CK_RV
 test_C_GetMechanismList (CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList,
                          CK_ULONG_PTR pulCount)
 {
-	g_assert (slotID == TEST_SLOT_ONE && "Invalid slotID");
+	g_assert (slotID == GCK_TEST_SLOT_ONE && "Invalid slotID");
 	g_assert (pulCount != NULL && "Invalid pulCount");
 
 	/* Application only wants to know the number of slots. */
@@ -379,7 +370,7 @@ static CK_RV
 test_C_GetMechanismInfo (CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
                          CK_MECHANISM_INFO_PTR pInfo)
 {
-	g_assert (slotID == TEST_SLOT_ONE && "Invalid slotID");
+	g_assert (slotID == GCK_TEST_SLOT_ONE && "Invalid slotID");
 	g_assert (pInfo != NULL && "Invalid pInfo");
 
 	if (type == CKM_CAPITALIZE) {
@@ -398,7 +389,7 @@ static CK_RV
 test_C_InitToken (CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen,
                   CK_UTF8CHAR_PTR pLabel)
 {
-	g_assert (slotID == TEST_SLOT_ONE && "Invalid slotID");
+	g_assert (slotID == GCK_TEST_SLOT_ONE && "Invalid slotID");
 	g_assert (pPin != NULL && "Invalid pPin");
 	g_assert (strlen ("TEST PIN") && "Invalid ulPinLen");
 	g_assert (strncmp ((gchar*)pPin, "TEST PIN", ulPinLen) == 0 && "Invalid pPin string");
@@ -423,7 +414,7 @@ test_C_OpenSession (CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication,
 {
 	Session *sess;
 
-	g_assert (slotID == TEST_SLOT_ONE && "Invalid slotID");
+	g_assert ((slotID == GCK_TEST_SLOT_ONE || slotID == GCK_TEST_SLOT_TWO) && "Invalid slotID");
 	g_assert (pApplication == NULL && "pApplication should be null");
 	g_assert (Notify == NULL && "Notify should be null");
 	g_assert (phSession != NULL && "Invalid phSession");
@@ -459,7 +450,7 @@ test_C_CloseSession (CK_SESSION_HANDLE hSession)
 static CK_RV
 test_C_CloseAllSessions (CK_SLOT_ID slotID)
 {
-	g_assert (slotID == TEST_SLOT_ONE && "Invalid slotID");
+	g_assert (slotID == GCK_TEST_SLOT_ONE && "Invalid slotID");
 
 	g_hash_table_remove_all (the_sessions);
 	return CKR_OK;
diff --git a/gck/tests/gck-test.h b/gck/tests/gck-test.h
index 078c962..4373cd4 100644
--- a/gck/tests/gck-test.h
+++ b/gck/tests/gck-test.h
@@ -43,4 +43,13 @@
 #define CKM_WRAP          (CKM_VENDOR_DEFINED | 4)
 #define CKM_DERIVE        (CKM_VENDOR_DEFINED | 5)
 
+/*
+ * Two slots
+ *  ONE: token present
+ *  TWO: token not present
+ */
+
+#define GCK_TEST_SLOT_ONE  52
+#define GCK_TEST_SLOT_TWO  134
+
 #endif /* GCK_TEST_H_ */
diff --git a/gck/tests/test-gck-crypto.c b/gck/tests/test-gck-crypto.c
index f03b7e7..54bb168 100644
--- a/gck/tests/test-gck-crypto.c
+++ b/gck/tests/test-gck-crypto.c
@@ -59,7 +59,6 @@ find_key (GckSession *session, CK_ATTRIBUTE_TYPE method, CK_MECHANISM_TYPE mech)
 	g_assert (objects);
 
 	for (l = objects; l; l = g_list_next (l)) {
-		gck_object_set_session (l->data, session);
 		if (mech) {
 			mechs = gck_object_get_data (l->data, CKA_ALLOWED_MECHANISMS, &n_mechs, NULL);
 			g_assert (mechs);
@@ -98,7 +97,6 @@ check_key_with_value (GckSession *session, GckObject *key, CK_OBJECT_CLASS klass
 	GckAttribute *attr;
 	gulong check;
 
-	gck_object_set_session (key, session);
 	attrs = gck_object_get (key, NULL, CKA_CLASS, CKA_VALUE, GCK_INVALID);
 	g_assert (attrs);
 
diff --git a/gck/tests/test-gck-object.c b/gck/tests/test-gck-object.c
index 2a63049..b552529 100644
--- a/gck/tests/test-gck-object.c
+++ b/gck/tests/test-gck-object.c
@@ -33,7 +33,7 @@ DEFINE_SETUP(prep_object)
 	SUCCESS_RES(session, err);
 
 	/* Our module always exports a token object with this */
-	object = gck_object_from_handle (slot, 2);
+	object = gck_object_from_handle (session, 2);
 	g_assert (object != NULL);
 }
 
@@ -47,12 +47,12 @@ DEFINE_TEARDOWN(prep_object)
 
 DEFINE_TEST(object_props)
 {
-	GckSlot *sl;
+	GckSession *sess;
 	GckModule *mod;
 	CK_OBJECT_HANDLE handle;
-	g_object_get (object, "slot", &sl, "module", &mod, "handle", &handle, NULL);
-	g_assert (slot == sl);
-	g_object_unref (sl);
+	g_object_get (object, "session", &sess, "module", &mod, "handle", &handle, NULL);
+	g_assert (session == sess);
+	g_object_unref (sess);
 	g_assert (module == mod);
 	g_object_unref (mod);
 	g_assert (handle == 2);
@@ -61,8 +61,10 @@ DEFINE_TEST(object_props)
 DEFINE_TEST(object_equals_hash)
 {
 	GckSlot *other_slot;
+	GckSession *other_session;
 	GckObject *other_object;
 	GObject *obj;
+	GError *err = NULL;
 	guint hash;
 
 	hash = gck_object_hash (object);
@@ -70,21 +72,24 @@ DEFINE_TEST(object_equals_hash)
 
 	g_assert (gck_object_equal (object, object));
 
-	other_slot = g_object_new (GCK_TYPE_SLOT, "module", module, "handle", 5895, NULL);
-	other_object = gck_object_from_handle (other_slot, gck_object_get_handle (object));
+	other_slot = g_object_new (GCK_TYPE_SLOT, "module", module, "handle", GCK_TEST_SLOT_TWO, NULL);
+	other_session = gck_slot_open_session (other_slot, 0, &err);
+	SUCCESS_RES (other_session, err);
+	other_object = gck_object_from_handle (other_session, gck_object_get_handle (object));
 	g_assert (!gck_object_equal (object, other_object));
 	g_object_unref (other_slot);
+	g_object_unref (other_session);
 	g_object_unref (other_object);
 
 	obj = g_object_new (G_TYPE_OBJECT, NULL);
 	g_assert (!gck_object_equal (object, obj));
 	g_object_unref (obj);
 
-	other_object = gck_object_from_handle (slot, 383838);
+	other_object = gck_object_from_handle (session, 383838);
 	g_assert (!gck_object_equal (object, other_object));
 	g_object_unref (other_object);
 
-	other_object = gck_object_from_handle (slot, gck_object_get_handle (object));
+	other_object = gck_object_from_handle (session, gck_object_get_handle (object));
 	g_assert (gck_object_equal (object, other_object));
 	g_object_unref (other_object);
 }
@@ -407,57 +412,3 @@ DEFINE_TEST(find_objects)
 	g_assert (objects == NULL);
 	gck_list_unref_free (objects);
 }
-
-DEFINE_TEST(explicit_sessions)
-{
-	GckSession *sess;
-	GAsyncResult *result = NULL;
-	CK_OBJECT_CLASS_PTR klass;
-	GError *err = NULL;
-	gsize n_data;
-
-	/* Set an explicit session */
-	gck_object_set_session (object, session);
-	sess = gck_object_get_session (object);
-	g_assert (sess == session);
-	g_object_unref (sess);
-	g_object_get (object, "session", &sess, NULL);
-	g_assert (sess == session);
-	g_object_unref (sess);
-
-	/* Simple */
-	klass = gck_object_get_data (object, CKA_CLASS, &n_data, &err);
-	SUCCESS_RES (klass, err);
-	if (klass != NULL) {
-		g_assert (n_data == sizeof (CK_OBJECT_CLASS));
-		g_assert (*klass == CKO_DATA);
-		g_free (klass);
-	}
-
-	/* Async */
-	gck_object_get_data_async (object, CKA_CLASS, NULL, NULL, fetch_async_result, &result);
-	testing_wait_until (500);
-	g_assert (result != NULL);
-
-	klass = gck_object_get_data_finish (object, result, &n_data, &err);
-	g_object_unref (result);
-	SUCCESS_RES (klass, err);
-	if (klass != NULL) {
-		g_assert (n_data == sizeof (CK_OBJECT_CLASS));
-		g_assert (*klass == CKO_DATA);
-		g_free (klass);
-	}
-
-	/* Set it to null and make sure taht works */
-	gck_object_set_session (object, NULL);
-	g_assert (gck_object_get_session (object) == NULL);
-	g_object_get (object, "session", &sess, NULL);
-	g_assert (sess == NULL);
-
-	/* Test property settor */
-	g_object_set (object, "session", session, NULL);
-	sess = gck_object_get_session (object);
-	g_assert (sess == session);
-	gck_object_set_session (object, NULL);
-	g_object_unref (sess);
-}



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