[gnome-keyring/dbus-api] [gck] More tests for GckSecret, and complete missing functions.
- From: Stefan Walter <stefw src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnome-keyring/dbus-api] [gck] More tests for GckSecret, and complete missing functions.
- Date: Mon, 10 Aug 2009 01:02:30 +0000 (UTC)
commit 7e4290a2cd393d158610389d2ff5396dc851e66f
Author: Stef Walter <stef memberwebs com>
Date: Sun Aug 9 17:30:56 2009 +0000
[gck] More tests for GckSecret, and complete missing functions.
pkcs11/gck/gck-secret.c | 50 ++++++++++++++++++++--------------
pkcs11/gck/tests/unit-test-secret.c | 19 +++++++++++++
2 files changed, 48 insertions(+), 21 deletions(-)
---
diff --git a/pkcs11/gck/gck-secret.c b/pkcs11/gck/gck-secret.c
index cf68764..cc3f091 100644
--- a/pkcs11/gck/gck-secret.c
+++ b/pkcs11/gck/gck-secret.c
@@ -29,8 +29,8 @@
struct _GckSecret {
GObject parent;
- gchar *data;
- gsize n_data;
+ guchar *memory;
+ gsize n_memory;
};
G_DEFINE_TYPE (GckSecret, gck_secret, G_TYPE_OBJECT);
@@ -50,9 +50,7 @@ gck_secret_dispose (GObject *obj)
{
GckSecret *self = GCK_SECRET (obj);
- egg_secure_strfree (self->data);
- self->data = NULL;
- self->n_data = 0;
+ egg_secure_clear (self->memory, self->n_memory);
G_OBJECT_CLASS (gck_secret_parent_class)->dispose (obj);
}
@@ -61,9 +59,10 @@ static void
gck_secret_finalize (GObject *obj)
{
GckSecret *self = GCK_SECRET (obj);
-
- g_assert (!self->data);
- g_assert (!self->n_data);
+
+ egg_secure_free (self->memory);
+ self->memory = NULL;
+ self->n_memory = 0;
G_OBJECT_CLASS (gck_secret_parent_class)->finalize (obj);
}
@@ -87,16 +86,16 @@ gck_secret_new (const guchar *data, gssize n_data)
if (data) {
if (n_data == -1) {
- secret->data = egg_secure_strdup ((const gchar*)data);
- secret->n_data = strlen (secret->data);
+ secret->memory = (guchar*)egg_secure_strdup ((const gchar*)data);
+ secret->n_memory = strlen ((const gchar*)data);
} else {
- secret->data = egg_secure_alloc (n_data + 1);
- memcpy (secret->data, data, n_data);
- secret->n_data = n_data;
+ secret->memory = egg_secure_alloc (n_data + 1);
+ memcpy (secret->memory, data, n_data);
+ secret->n_memory = n_data;
}
} else {
- secret->data = NULL;
- secret->n_data = 0;
+ secret->memory = NULL;
+ secret->n_memory = 0;
}
return secret;
@@ -117,13 +116,22 @@ gck_secret_new_from_password (const gchar *password)
return gck_secret_new ((const guchar*)password, -1);
}
+const guchar*
+gck_secret_get (GckSecret *self, gsize *n_data)
+{
+ g_return_val_if_fail (GCK_IS_SECRET (self), NULL);
+ g_return_val_if_fail (n_data, NULL);
+ *n_data = self->n_memory;
+ return self->memory;
+}
+
const gchar*
gck_secret_get_password (GckSecret *self, gsize *n_data)
{
g_return_val_if_fail (GCK_IS_SECRET (self), NULL);
g_return_val_if_fail (n_data, NULL);
- *n_data = self->n_data;
- return self->data;
+ *n_data = self->n_memory;
+ return (gchar*)self->memory;
}
gboolean
@@ -138,11 +146,11 @@ gck_secret_equals (GckSecret *self, const guchar* pin, gssize n_pin)
if (n_pin == -1 && pin != NULL)
n_pin = strlen ((const gchar*)pin);
- if (n_pin != self->n_data)
+ if (n_pin != self->n_memory)
return FALSE;
- if (!pin && !self->data)
+ if (!pin && !self->memory)
return TRUE;
- if (!pin || !self->data)
+ if (!pin || !self->memory)
return FALSE;
- return memcmp (pin, self->data, n_pin) == 0;
+ return memcmp (pin, self->memory, n_pin) == 0;
}
diff --git a/pkcs11/gck/tests/unit-test-secret.c b/pkcs11/gck/tests/unit-test-secret.c
index be558e3..8e3a69c 100644
--- a/pkcs11/gck/tests/unit-test-secret.c
+++ b/pkcs11/gck/tests/unit-test-secret.c
@@ -98,6 +98,25 @@ DEFINE_TEST(test_null_terminated)
g_object_unref (secret);
}
+DEFINE_TEST(test_always_has_null)
+{
+ GckSecret *secret;
+ const guchar *memory;
+ gsize n_memory;
+
+ /* A 4 byte 'binary' secret */
+ secret = gck_secret_new ((guchar*)"barn", 4);
+ g_assert (GCK_IS_SECRET (secret));
+
+ memory = gck_secret_get (secret, &n_memory);
+ g_assert_cmpuint (n_memory, ==, 4);
+
+ /* But it should be null-terminated anyway */
+ g_assert (memory[4] == 0);
+
+ g_object_unref (secret);
+}
+
DEFINE_TEST(test_null)
{
GckSecret *secret;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]