[gnome-keyring/dbus-api] [gck] Add gck_secret_equal(), change so NULL secret == ""
- From: Stefan Walter <stefw src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnome-keyring/dbus-api] [gck] Add gck_secret_equal(), change so NULL secret == ""
- Date: Sat, 15 Aug 2009 14:41:13 +0000 (UTC)
commit 00eef882286633fd49acc3986e7ca8cafad255e1
Author: Stef Walter <stef memberwebs com>
Date: Sat Aug 15 13:40:47 2009 +0000
[gck] Add gck_secret_equal(), change so NULL secret == ""
Change semantics of secret comparing so that a NULL secret is equal to
a zero length secret. Also add gck_secret_equal() function.
pkcs11/gck/gck-secret.c | 23 ++++++++++++++++++++++-
pkcs11/gck/gck-secret.h | 3 +++
pkcs11/gck/tests/unit-test-secret.c | 20 ++++++++++++++++++--
3 files changed, 43 insertions(+), 3 deletions(-)
---
diff --git a/pkcs11/gck/gck-secret.c b/pkcs11/gck/gck-secret.c
index cc3f091..a8a2cb6 100644
--- a/pkcs11/gck/gck-secret.c
+++ b/pkcs11/gck/gck-secret.c
@@ -135,6 +135,16 @@ gck_secret_get_password (GckSecret *self, gsize *n_data)
}
gboolean
+gck_secret_equal (GckSecret *self, GckSecret *other)
+{
+ g_return_val_if_fail (GCK_IS_SECRET (self), FALSE);
+ g_return_val_if_fail (GCK_IS_SECRET (other), FALSE);
+ if (self == other)
+ return TRUE;
+ return gck_secret_equals (self, other->memory, other->n_memory);
+}
+
+gboolean
gck_secret_equals (GckSecret *self, const guchar* pin, gssize n_pin)
{
g_return_val_if_fail (GCK_IS_SECRET (self), FALSE);
@@ -145,12 +155,23 @@ gck_secret_equals (GckSecret *self, const guchar* pin, gssize n_pin)
if (n_pin == -1 && pin != NULL)
n_pin = strlen ((const gchar*)pin);
-
+
+ /* The same length */
if (n_pin != self->n_memory)
return FALSE;
+
+ /* Two null passwords */
if (!pin && !self->memory)
return TRUE;
+
+ /* For our purposes a null password equals an empty password */
+ if (n_pin == 0)
+ return TRUE;
+
+ /* One null, one not null */
if (!pin || !self->memory)
return FALSE;
+
+ /* Compare actual memory */
return memcmp (pin, self->memory, n_pin) == 0;
}
diff --git a/pkcs11/gck/gck-secret.h b/pkcs11/gck/gck-secret.h
index 6101a33..f1d7380 100644
--- a/pkcs11/gck/gck-secret.h
+++ b/pkcs11/gck/gck-secret.h
@@ -57,6 +57,9 @@ const guchar* gck_secret_get (GckSecret *self,
const gchar* gck_secret_get_password (GckSecret *self,
gsize *n_pin);
+gboolean gck_secret_equal (GckSecret *self,
+ GckSecret *other);
+
gboolean gck_secret_equals (GckSecret *self,
const guchar *data,
gssize n_data);
diff --git a/pkcs11/gck/tests/unit-test-secret.c b/pkcs11/gck/tests/unit-test-secret.c
index 8e3a69c..7401ec2 100644
--- a/pkcs11/gck/tests/unit-test-secret.c
+++ b/pkcs11/gck/tests/unit-test-secret.c
@@ -134,7 +134,7 @@ DEFINE_TEST(test_null)
g_assert (!gck_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pino", 9));
g_assert (gck_secret_equals (secret, NULL, 0));
g_assert (!gck_secret_equals (secret, (CK_UTF8CHAR_PTR)"null-terminated", -1));
- g_assert (!gck_secret_equals (secret, (CK_UTF8CHAR_PTR)"", 0));
+ g_assert (gck_secret_equals (secret, (CK_UTF8CHAR_PTR)"", 0));
g_object_unref (secret);
}
@@ -154,9 +154,25 @@ DEFINE_TEST(test_empty)
g_assert (!gck_secret_equals (secret, (CK_UTF8CHAR_PTR)"null-terminated", strlen ("null-terminated")));
g_assert (!gck_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pino", 9));
- g_assert (!gck_secret_equals (secret, NULL, 0));
+ g_assert (gck_secret_equals (secret, NULL, 0));
g_assert (gck_secret_equals (secret, (CK_UTF8CHAR_PTR)"", -1));
g_assert (gck_secret_equals (secret, (CK_UTF8CHAR_PTR)"", 0));
g_object_unref (secret);
}
+
+DEFINE_TEST(test_equal)
+{
+ GckSecret *one;
+ GckSecret *two;
+
+ one = two = gck_secret_new ((CK_UTF8CHAR_PTR)"funny", 5);
+ g_assert (gck_secret_equal (one, two));
+
+ two = gck_secret_new_from_password ("funny");
+ g_assert (gck_secret_equal (one, two));
+
+ g_object_unref (one);
+ one = gck_secret_new_from_password ("other");
+ g_assert (!gck_secret_equal (one, two));
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]