[gnome-keyring] gcr: More testing for secret gnupg keys.



commit b5fd76a82785ee4d5d056541b3425a217a548e5e
Author: Stef Walter <stefw collabora co uk>
Date:   Thu May 12 11:18:07 2011 +0200

    gcr: More testing for secret gnupg keys.
    
     * Create an example keyring
     * Change signature of _gcr_gnupg_key_new so it also accepts
       the secret part.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=648019

 gcr/gcr-gnupg-collection.c        |    2 +-
 gcr/gcr-gnupg-key.c               |   50 +++++++++++++++++++++++++++-----
 gcr/gcr-gnupg-key.h               |    3 +-
 gcr/tests/test-gnupg-key.c        |   56 +++++++++++++++++++++++++++++++++++--
 testing/gnupg-example/pubring.gpg |  Bin 0 -> 21969 bytes
 testing/gnupg-example/random_seed |  Bin 0 -> 600 bytes
 testing/gnupg-example/secring.gpg |  Bin 0 -> 4398 bytes
 testing/gnupg-example/trustdb.gpg |  Bin 0 -> 1400 bytes
 8 files changed, 98 insertions(+), 13 deletions(-)
---
diff --git a/gcr/gcr-gnupg-collection.c b/gcr/gcr-gnupg-collection.c
index ffdb097..3f82e24 100644
--- a/gcr/gcr-gnupg-collection.c
+++ b/gcr/gcr-gnupg-collection.c
@@ -251,7 +251,7 @@ process_dataset_as_public_key (GcrGnupgCollectionLoad *load, GPtrArray *dataset,
 
 	/* Add a new key */
 	} else {
-		key = _gcr_gnupg_key_new (dataset);
+		key = _gcr_gnupg_key_new (dataset, NULL);
 		g_hash_table_insert (load->collection->pv->items, g_strdup (keyid), key);
 		gcr_collection_emit_added (GCR_COLLECTION (load->collection), G_OBJECT (key));
 	}
diff --git a/gcr/gcr-gnupg-key.c b/gcr/gcr-gnupg-key.c
index 0bf2c15..2f078e2 100644
--- a/gcr/gcr-gnupg-key.c
+++ b/gcr/gcr-gnupg-key.c
@@ -200,18 +200,23 @@ _gcr_gnupg_key_class_init (GcrGnupgKeyClass *klass)
 
 /**
  * _gcr_gnupg_key_new:
- * @dataset: array of GcrColons*
+ * @pubset: array of GcrColons* representing public part of key
+ * @secset: optional array of GcrColons* representing secret part of key.
  *
- * Create a new GcrGnupgKey for the colons data passed.
+ * Create a new GcrGnupgKey for the colons data passed. If the secret part
+ * of the key is set, then this represents a secret key.
  *
  * Returns: A newly allocated key, which should be released with
  *     g_object_unref().
  */
 GcrGnupgKey*
-_gcr_gnupg_key_new (GPtrArray *dataset)
+_gcr_gnupg_key_new (GPtrArray *pubset, GPtrArray *secset)
 {
-	g_return_val_if_fail (dataset, NULL);
-	return g_object_new (GCR_TYPE_GNUPG_KEY, "public-dataset", dataset, NULL);
+	g_return_val_if_fail (pubset, NULL);
+	return g_object_new (GCR_TYPE_GNUPG_KEY,
+	                     "public-dataset", pubset,
+	                     "secret-dataset", secset,
+	                     NULL);
 }
 
 /**
@@ -240,10 +245,25 @@ void
 _gcr_gnupg_key_set_public_dataset (GcrGnupgKey *self, GPtrArray *dataset)
 {
 	GObject *obj;
+	const gchar *old_keyid;
+	const gchar *new_keyid;
 
 	g_return_if_fail (GCR_IS_GNUPG_KEY (self));
 	g_return_if_fail (dataset);
 
+	/* Check that it matches previous */
+	if (self->pv->public_dataset) {
+		old_keyid = _gcr_gnupg_key_get_keyid_for_colons (self->pv->public_dataset);
+		new_keyid = _gcr_gnupg_key_get_keyid_for_colons (dataset);
+
+		if (g_strcmp0 (old_keyid, new_keyid) != 0) {
+			g_warning ("it is an error to change a gnupg key so that the "
+			           "fingerprint is no longer the same: %s != %s",
+			           old_keyid, new_keyid);
+			return;
+		}
+	}
+
 	g_ptr_array_ref (dataset);
 	if (self->pv->public_dataset)
 		g_ptr_array_unref (self->pv->public_dataset);
@@ -254,7 +274,6 @@ _gcr_gnupg_key_set_public_dataset (GcrGnupgKey *self, GPtrArray *dataset)
 	g_object_notify (obj, "public-dataset");
 	g_object_notify (obj, "label");
 	g_object_notify (obj, "markup");
-	g_object_notify (obj, "keyid");
 	g_object_thaw_notify (obj);
 }
 
@@ -284,11 +303,26 @@ void
 _gcr_gnupg_key_set_secret_dataset (GcrGnupgKey *self, GPtrArray *dataset)
 {
 	GObject *obj;
+	const gchar *pub_keyid;
+	const gchar *sec_keyid;
 
 	g_return_if_fail (GCR_IS_GNUPG_KEY (self));
-	g_return_if_fail (dataset);
 
-	g_ptr_array_ref (dataset);
+	/* Check that it matches public key */
+	if (self->pv->public_dataset && dataset) {
+		pub_keyid = _gcr_gnupg_key_get_keyid_for_colons (self->pv->public_dataset);
+		sec_keyid = _gcr_gnupg_key_get_keyid_for_colons (dataset);
+
+		if (g_strcmp0 (pub_keyid, sec_keyid) != 0) {
+			g_warning ("it is an error to create a gnupg key so that the "
+			           "fingerprint of thet pub and sec parts are not the same: %s != %s",
+			           pub_keyid, sec_keyid);
+			return;
+		}
+	}
+
+	if (dataset)
+		g_ptr_array_ref (dataset);
 	if (self->pv->secret_dataset)
 		g_ptr_array_unref (self->pv->secret_dataset);
 	self->pv->secret_dataset = dataset;
diff --git a/gcr/gcr-gnupg-key.h b/gcr/gcr-gnupg-key.h
index 1fe45e1..e6d0235 100644
--- a/gcr/gcr-gnupg-key.h
+++ b/gcr/gcr-gnupg-key.h
@@ -61,7 +61,8 @@ GType               _gcr_gnupg_key_get_type                      (void);
 
 const GcrColumn*    _gcr_gnupg_key_get_columns                   (void);
 
-GcrGnupgKey*        _gcr_gnupg_key_new                           (GPtrArray *dataset);
+GcrGnupgKey*        _gcr_gnupg_key_new                           (GPtrArray *pubset,
+                                                                  GPtrArray *secset);
 
 GPtrArray*          _gcr_gnupg_key_get_public_dataset            (GcrGnupgKey *self);
 
diff --git a/gcr/tests/test-gnupg-key.c b/gcr/tests/test-gnupg-key.c
index 5f1efac..dfb19a3 100644
--- a/gcr/tests/test-gnupg-key.c
+++ b/gcr/tests/test-gnupg-key.c
@@ -34,6 +34,8 @@
 
 typedef struct {
 	GPtrArray *dataset;
+	GPtrArray *pubset;
+	GPtrArray *secset;
 	GcrGnupgKey *key;
 } Test;
 
@@ -51,9 +53,23 @@ setup (Test *test, gconstpointer unused)
 	g_ptr_array_add (dataset, _gcr_colons_parse ("fpr:::::::::CF8BCC4B18DE08FCD8A1615906AD222CADF6A6E1:\n", -1));
 	g_ptr_array_add (dataset, _gcr_colons_parse ("sub:r:1536:20:5CE086B5B5A18FF4:899817788:1025961788:::::esc:\n", -1));
 	g_ptr_array_add (dataset, _gcr_colons_parse ("fpr:::::::::AB059359A3B81F410FCFF97F5CE086B5B5A18FF4:", -1));
-
-	test->key = _gcr_gnupg_key_new (dataset);
 	test->dataset = dataset;
+
+	test->key = _gcr_gnupg_key_new (dataset, NULL);
+
+	dataset = g_ptr_array_new_with_free_func (_gcr_colons_free);
+	g_ptr_array_add (dataset, _gcr_colons_parse ("pub:u:2048:1:4842D952AFC000FD:1305189489:::u:::scESC:", -1));
+	g_ptr_array_add (dataset, _gcr_colons_parse ("uid:u::::1305189849::D0A8FA7B15DC4BE3F8F03A49C372F2718C78AFC0::Dr. Strangelove <lovingbomb example com>:", -1));
+	g_ptr_array_add (dataset, _gcr_colons_parse ("uid:u::::1305189489::D449F1605254754B0BBFA424FC34E50609103BBB::Test Number 1 (unlimited) <test-number-1 example com>:", -1));
+	g_ptr_array_add (dataset, _gcr_colons_parse ("sub:u:2048:1:4852132BBED15014:1305189489::::::e:", -1));
+	test->pubset = dataset;
+
+	dataset = g_ptr_array_new_with_free_func (_gcr_colons_free);
+	g_ptr_array_add (dataset, _gcr_colons_parse ("sec::2048:1:4842D952AFC000FD:1305189489::::::::::", -1));
+	g_ptr_array_add (dataset, _gcr_colons_parse ("uid:::::::D449F1605254754B0BBFA424FC34E50609103BBB::Test Number 1 (unlimited) <test-number-1 example com>:", -1));
+	g_ptr_array_add (dataset, _gcr_colons_parse ("uid:::::::D0A8FA7B15DC4BE3F8F03A49C372F2718C78AFC0::Dr. Strangelove <lovingbomb example com>:", -1));
+	g_ptr_array_add (dataset, _gcr_colons_parse ("ssb::2048:1:4852132BBED15014:1305189489::::::::::", -1));
+	test->secset = dataset;
 }
 
 static void
@@ -61,6 +77,8 @@ teardown (Test *test, gconstpointer unused)
 {
 	g_object_unref (test->key);
 	g_ptr_array_unref (test->dataset);
+	g_ptr_array_unref (test->pubset);
+	g_ptr_array_unref (test->secset);
 }
 
 static void
@@ -101,13 +119,27 @@ test_dataset (Test *test, gconstpointer unused)
 {
 	GPtrArray *dataset;
 
-	g_object_get (test->key, "dataset", &dataset, NULL);
+	g_object_get (test->key, "public-dataset", &dataset, NULL);
 	g_assert (dataset == test->dataset);
 
+	_gcr_gnupg_key_set_public_dataset (test->key, dataset);
+	g_assert (dataset == _gcr_gnupg_key_get_public_dataset (test->key));
+
 	g_ptr_array_unref (dataset);
 }
 
 static void
+test_keyid (Test *test, gconstpointer unused)
+{
+	gchar *keyid;
+
+	g_object_get (test->key, "keyid", &keyid, NULL);
+	g_assert_cmpstr (keyid, ==, "621CC013");
+
+	g_free (keyid);
+}
+
+static void
 test_keyid_for_colons (Test *test, gconstpointer unused)
 {
 	const gchar *keyid;
@@ -116,6 +148,22 @@ test_keyid_for_colons (Test *test, gconstpointer unused)
 	g_assert_cmpstr (keyid, ==, "6C7EE1B8621CC013");
 }
 
+static void
+test_with_secret (Test *test, gconstpointer unused)
+{
+	GcrGnupgKey *key;
+	GPtrArray *secset;
+
+	key = _gcr_gnupg_key_new (test->pubset, test->secset);
+	g_assert (GCR_IS_GNUPG_KEY (key));
+
+	g_object_get (key, "secret-dataset", &secset, NULL);
+	g_assert (secset == _gcr_gnupg_key_get_secret_dataset (key));
+	g_object_set (key, "secret-dataset", secset, NULL);
+
+	g_object_unref (key);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -126,7 +174,9 @@ main (int argc, char **argv)
 	g_test_add ("/gcr/gnupg-key/description", Test, NULL, setup, test_description, teardown);
 	g_test_add ("/gcr/gnupg-key/markup", Test, NULL, setup, test_markup, teardown);
 	g_test_add ("/gcr/gnupg-key/dataset", Test, NULL, setup, test_dataset, teardown);
+	g_test_add ("/gcr/gnupg-key/keyid", Test, NULL, setup, test_keyid, teardown);
 	g_test_add ("/gcr/gnupg-key/keyid_for_colons", Test, NULL, setup, test_keyid_for_colons, teardown);
+	g_test_add ("/gcr/gnupg-key/with_secret", Test, NULL, setup, test_with_secret, teardown);
 
 	return g_test_run ();
 }
diff --git a/testing/gnupg-example/pubring.gpg b/testing/gnupg-example/pubring.gpg
new file mode 100644
index 0000000..10b1372
Binary files /dev/null and b/testing/gnupg-example/pubring.gpg differ
diff --git a/testing/gnupg-example/random_seed b/testing/gnupg-example/random_seed
new file mode 100644
index 0000000..94b1acf
Binary files /dev/null and b/testing/gnupg-example/random_seed differ
diff --git a/testing/gnupg-example/secring.gpg b/testing/gnupg-example/secring.gpg
new file mode 100644
index 0000000..4a21e26
Binary files /dev/null and b/testing/gnupg-example/secring.gpg differ
diff --git a/testing/gnupg-example/trustdb.gpg b/testing/gnupg-example/trustdb.gpg
new file mode 100644
index 0000000..c0ba085
Binary files /dev/null and b/testing/gnupg-example/trustdb.gpg differ



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