[seahorse/wip/hkp-test: 2/3] pgp: Add a test for HKP




commit 9307900eb944ab0adfb9b590cc710eca61da00ef
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Fri Mar 5 21:45:41 2021 +0100

    pgp: Add a test for HKP
    
    With the advent of HKPS support, some of the code in `SeahorseHkpSource`
    got changed quite a lot. With no proper unit tests, it's hard to
    guarantee we didn't break anything. With help of the previous commit, we
    can now finally start writing tests per backend, so let's do that.
    
    The test actually uncovered some issues with the current HKP
    implementetion as well, notably that the first key is ignored. Fix those
    while we're at it.

 pgp/meson.build           |  24 ++++++++
 pgp/seahorse-hkp-source.c |  46 +++++++--------
 pgp/seahorse-hkp-source.h |   2 +
 pgp/test-hkp-source.c     | 145 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 192 insertions(+), 25 deletions(-)
---
diff --git a/pgp/meson.build b/pgp/meson.build
index 0b7b41bb..752b2dfe 100644
--- a/pgp/meson.build
+++ b/pgp/meson.build
@@ -95,3 +95,27 @@ xloadimage = executable('xloadimage',
   install: true,
   install_dir: libexecbindir,
 )
+
+
+# Tests
+test_names = [
+]
+
+if get_option('hkp-support')
+  test_names += 'hkp-source'
+endif
+
+foreach _test : test_names
+  test_bin = executable(_test,
+    files('test-@0@.c'.format(_test)),
+    dependencies: [
+      pgp_dep,
+      pgp_dependencies,
+    ],
+    include_directories: include_directories('..'),
+  )
+
+  test(_test, test_bin,
+    suite: 'pgp',
+  )
+endforeach
diff --git a/pgp/seahorse-hkp-source.c b/pgp/seahorse-hkp-source.c
index 1ccf5fb5..5cfbc9f2 100644
--- a/pgp/seahorse-hkp-source.c
+++ b/pgp/seahorse-hkp-source.c
@@ -220,14 +220,15 @@ parse_hkp_flags (char *flags)
 }
 
 /**
-* response: The HKP server response to parse
-*
-* Extracts the key data from the HKP server response
-*
-* Returns A GList of keys
-**/
-static GList*
-parse_hkp_index (const char *response)
+ * parse_hkp_index:
+ * response: The HKP server response to parse
+ *
+ * Extracts the key data from the HKP server response
+ *
+ * Returns: (transfer full): The parsed list of keys
+ */
+GList *
+seahorse_hkp_parse_lookup_response (const char *response)
 {
     /*
      * Use The OpenPGP HTTP Keyserver Protocol (HKP) to search and get keys
@@ -236,7 +237,6 @@ parse_hkp_index (const char *response)
     g_auto(GStrv) lines = NULL;
     SeahorsePgpKey *key = NULL;
     GList *keys = NULL;
-    SeahorseFlags flags;
     guint key_total = 0, key_count = 0;
 
     lines = g_strsplit (response, "\n", 0);
@@ -244,17 +244,18 @@ parse_hkp_index (const char *response)
         char *line = *l;
         g_auto(GStrv) columns = NULL;
 
-        g_debug ("%s", line);
-
-        if (strlen(line) == 0) {
+        if (!*line) {
           g_debug ("HKP Parser: skip empty line");
           continue;
         }
 
+        g_debug ("%s", line);
+
         /* split the line using hkp delimiter */
-        columns = g_strsplit_set(line, ":", 7);
+        columns = g_strsplit_set (line, ":", 7);
 
         /* info header */
+        /* info:<version>:<count> */
         if (g_ascii_strncasecmp (columns[0], "info", 4) == 0) {
             if (!columns[1] && !columns[2]){
                 g_debug("HKP Parse: Invalid info line: %s", line);
@@ -272,17 +273,10 @@ parse_hkp_index (const char *response)
             long created = 0, expired = 0;
             g_autoptr(GDateTime) created_date = NULL;
             g_autoptr(GDateTime) expired_date = NULL;
+            SeahorseFlags flags;
 
             key_count++;
 
-            /* reset previous key */
-            if (key) {
-                g_debug ("HKP Parse: previous key found");
-                seahorse_pgp_key_realize (SEAHORSE_PGP_KEY (key));
-                keys = g_list_prepend (keys, key);
-                key = NULL;
-            }
-
             if (!columns[0] || !columns[1] || !columns[2] || !columns[3] || !columns[4]) {
                 g_message ("Invalid key line from server: %s", line);
                 continue;
@@ -327,9 +321,8 @@ parse_hkp_index (const char *response)
 
             /* set flags (optional) */
             flags = SEAHORSE_FLAG_EXPORTABLE;
-            if (columns[6]){
-                flags |= parse_hkp_flags(columns[6]);
-            }
+            if (columns[6])
+                flags |= parse_hkp_flags (columns[6]);
 
             /* create key */
             g_debug("HKP Parse: found new key");
@@ -351,6 +344,9 @@ parse_hkp_index (const char *response)
                 seahorse_pgp_subkey_set_algorithm (subkey, algo);
             seahorse_pgp_key_add_subkey (key, subkey);
 
+            /* Now add it to the list */
+            keys = g_list_prepend (keys, key);
+
         /* A UID for the key */
         } else if (g_ascii_strncasecmp (columns[0], "uid", 3) == 0) {
             g_autoptr (SeahorsePgpUid) uid = NULL;
@@ -557,7 +553,7 @@ on_search_message_complete (SoupSession *session,
         return;
     }
 
-    keys = parse_hkp_index (message->response_body->data);
+    keys = seahorse_hkp_parse_lookup_response (message->response_body->data);
     for (l = keys; l; l = g_list_next (l)) {
         g_object_set (l->data, "place", closure->source, NULL);
         gcr_simple_collection_add (closure->results, l->data);
diff --git a/pgp/seahorse-hkp-source.h b/pgp/seahorse-hkp-source.h
index fbb4e01e..e5f66a46 100644
--- a/pgp/seahorse-hkp-source.h
+++ b/pgp/seahorse-hkp-source.h
@@ -41,6 +41,8 @@ SeahorseHKPSource*    seahorse_hkp_source_new      (const char *uri);
 
 gboolean              seahorse_hkp_is_valid_uri    (const char *uri);
 
+GList *               seahorse_hkp_parse_lookup_response  (const char *response);
+
 
 #define HKP_ERROR_DOMAIN (seahorse_hkp_error_quark())
 GQuark            seahorse_hkp_error_quark       (void);
diff --git a/pgp/test-hkp-source.c b/pgp/test-hkp-source.c
new file mode 100644
index 00000000..10f76cbb
--- /dev/null
+++ b/pgp/test-hkp-source.c
@@ -0,0 +1,145 @@
+/*
+ * Seahorse
+ *
+ * Copyright (C) 2021 Niels De Graef
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include "seahorse-hkp-source.h"
+#include "seahorse-pgp-key.h"
+#include "seahorse-pgp-uid.h"
+
+#include <glib.h>
+
+static void
+test_hkp_lookup_response_simple_no_uid (void)
+{
+    g_autolist(SeahorsePgpKey) keys = NULL;
+    SeahorsePgpKey *key;
+    g_autoptr(GDateTime) created = NULL;
+    GListModel *uids;
+
+    keys = seahorse_hkp_parse_lookup_response (
+        "info:1:1\n"
+        "pub:0123456789ABCDEF0123456789ABCDEF01234567:1:4096:712627200::\n"
+    );
+
+    g_assert_nonnull (keys);
+    g_assert_nonnull (keys->data);
+    g_assert_cmpuint (g_list_length (keys), ==, 1);
+
+    key = keys->data;
+
+    g_assert_cmpstr (seahorse_pgp_key_get_fingerprint (key), ==,
+                     "0123 4567 89AB CDEF 0123 4567 89AB CDEF 0123 4567");
+    g_assert_cmpstr (seahorse_pgp_key_get_algo (key), ==, "RSA");
+    g_assert_cmpuint (seahorse_pgp_key_get_length (key), ==, 4096);
+
+    /* Happy birthday emails always welcome */
+    created = g_date_time_new_utc (1992, 8, 1, 0, 0, 0);
+    g_assert_true (g_date_time_equal (seahorse_pgp_key_get_created (key),
+                                      created));
+
+    g_assert_null (seahorse_pgp_key_get_expires (key));
+
+    uids = seahorse_pgp_key_get_uids (key);
+    g_assert_nonnull (uids);
+    g_assert_cmpuint (g_list_model_get_n_items (uids), ==, 0);
+}
+
+static void
+test_hkp_lookup_response_simple (void)
+{
+    g_autolist(SeahorsePgpKey) keys = NULL;
+    SeahorsePgpKey *key;
+    g_autoptr(GDateTime) created = NULL;
+    GListModel *uids;
+    g_autoptr(SeahorsePgpUid) uid = NULL;
+
+    keys = seahorse_hkp_parse_lookup_response (
+        "info:1:1\n"
+        "pub:0123456789ABCDEF0123456789ABCDEF01234567:1:4096:712627200::\n"
+        "uid:Niels De Graef <nielsdegraef gmail com>:::\n"
+    );
+
+    g_assert_nonnull (keys);
+    g_assert_nonnull (keys->data);
+    g_assert_cmpuint (g_list_length (keys), ==, 1);
+
+    key = keys->data;
+
+    g_assert_cmpstr (seahorse_pgp_key_get_fingerprint (key), ==,
+                     "0123 4567 89AB CDEF 0123 4567 89AB CDEF 0123 4567");
+    g_assert_cmpstr (seahorse_pgp_key_get_algo (key), ==, "RSA");
+    g_assert_cmpuint (seahorse_pgp_key_get_length (key), ==, 4096);
+
+    /* Happy birthday emails always welcome */
+    created = g_date_time_new_utc (1992, 8, 1, 0, 0, 0);
+    g_assert_true (g_date_time_equal (seahorse_pgp_key_get_created (key),
+                                      created));
+
+    g_assert_null (seahorse_pgp_key_get_expires (key));
+
+    /* UID */
+    uids = seahorse_pgp_key_get_uids (key);
+    g_assert_nonnull (uids);
+    g_assert_cmpuint (g_list_model_get_n_items (uids), ==, 1);
+
+    uid = g_list_model_get_item (uids, 0);
+    g_assert_nonnull (uid);
+    g_assert_cmpstr (seahorse_pgp_uid_get_name (uid), ==, "Niels De Graef");
+    g_assert_cmpstr (seahorse_pgp_uid_get_email (uid), ==, "nielsdegraef gmail com");
+    g_assert_cmpstr (seahorse_pgp_uid_get_comment (uid), ==, "");
+}
+
+static void
+test_hkp_lookup_response_empty (void)
+{
+    g_autolist(SeahorsePgpKey) keys = NULL;
+
+    keys = seahorse_hkp_parse_lookup_response ("info:1:0\n");
+
+    g_assert_null (keys);
+    g_assert_cmpuint (g_list_length (keys), ==, 0);
+}
+
+static void
+test_hkp_is_valid_uri (void)
+{
+    g_assert_true (seahorse_hkp_is_valid_uri ("hkp://keys.openpgp.org"));
+    g_assert_true (seahorse_hkp_is_valid_uri ("hkps://keys.openpgp.org"));
+
+    /* Invalid URL */
+    g_assert_false (seahorse_hkp_is_valid_uri ("test"));
+
+    /* Missing scheme */
+    g_assert_false (seahorse_hkp_is_valid_uri ("keys.openpgp.org"));
+
+    /* Wrong scheme */
+    g_assert_false (seahorse_hkp_is_valid_uri ("ldap://keys.openpgp.org";));
+}
+
+int
+main (int argc, char **argv)
+{
+    g_test_init (&argc, &argv, NULL);
+
+    g_test_add_func ("/hkp/valid-uri", test_hkp_is_valid_uri);
+    g_test_add_func ("/hkp/lookup-response-empty", test_hkp_lookup_response_empty);
+    g_test_add_func ("/hkp/lookup-response-simple", test_hkp_lookup_response_simple);
+    g_test_add_func ("/hkp/lookup-response-simple-no-uid", test_hkp_lookup_response_simple_no_uid);
+
+    return g_test_run ();
+}


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