[Spice-commits] 2 commits - gtk/spice-session.c tests/Makefile.am tests/session.c

Victor Toso de Carvalho victortoso at kemper.freedesktop.org
Fri Feb 27 08:00:29 PST 2015


 gtk/spice-session.c |   20 ++++++++++++--
 tests/Makefile.am   |    2 +
 tests/session.c     |   74 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 2 deletions(-)

New commits:
commit ca59cb30c1af0d89efa21570237b8adbe965e8b4
Author: Victor Toso <victortoso at redhat.com>
Date:   Fri Feb 27 16:24:34 2015 +0100

    tests: add spice-session test
    
    Checking if URIs are being parsed and generated correctly.

diff --git a/tests/Makefile.am b/tests/Makefile.am
index b236b12..57b06db 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -3,6 +3,7 @@ NULL =
 noinst_PROGRAMS =				\
 	coroutine				\
 	util					\
+	session					\
 	$(NULL)
 
 TESTS = $(noinst_PROGRAMS)
@@ -20,5 +21,6 @@ LDADD =							\
 
 util_SOURCES = util.c
 coroutine_SOURCES = coroutine.c
+session_SOURCES = session.c
 
 -include $(top_srcdir)/git.mk
diff --git a/tests/session.c b/tests/session.c
new file mode 100644
index 0000000..feee3db
--- /dev/null
+++ b/tests/session.c
@@ -0,0 +1,74 @@
+#include <glib.h>
+
+#include "spice-session.h"
+
+static void test_session_uri(void)
+{
+    SpiceSession *s;
+    gint i;
+
+    struct {
+        gchar *port;
+        gchar *tls_port;
+        gchar *uri_input;
+        gchar *uri_output;
+    } tests[] = {
+        /* Arguments with empty value */
+        { "5900", NULL,
+          "spice://localhost?port=5900&tls-port=",
+          "spice://localhost?port=5900&" },
+        { "5910", NULL,
+          "spice://localhost?tls-port=&port=5910",
+          "spice://localhost?port=5910&" },
+        { NULL, "5920",
+          "spice://localhost?tls-port=5920&port=",
+          "spice://localhost?tls-port=5920" },
+        { NULL, "5930",
+          "spice://localhost?port=&tls-port=5930",
+          "spice://localhost?tls-port=5930" },
+    };
+
+    /* Set URI and check URI, port and tls_port */
+    for (i = 0; i < G_N_ELEMENTS(tests); i++) {
+        gchar *uri, *port, *tls_port;
+
+        s = spice_session_new();
+        g_object_set(s, "uri", tests[i].uri_input, NULL);
+        g_object_get(s,
+                     "uri", &uri,
+                     "port", &port,
+                     "tls-port", &tls_port,
+                      NULL);
+        g_assert_cmpstr(tests[i].uri_output, ==, uri);
+        g_assert_cmpstr(tests[i].port, ==, port);
+        g_assert_cmpstr(tests[i].tls_port, ==, tls_port);
+        g_clear_pointer(&uri, g_free);
+        g_clear_pointer(&port, g_free);
+        g_clear_pointer(&tls_port, g_free);
+        g_object_unref(s);
+    }
+
+    /* Set port and tls_port, check URI */
+    for (i = 0; i < G_N_ELEMENTS(tests); i++) {
+        gchar *uri;
+
+        s = spice_session_new();
+        g_object_set(s,
+                     "port", tests[i].port,
+                     "tls-port", tests[i].tls_port,
+                      NULL);
+        g_object_get(s, "uri", &uri, NULL);
+        g_assert_cmpstr(tests[i].uri_output, ==, uri);
+        g_clear_pointer(&uri, g_free);
+        g_object_unref(s);
+    }
+}
+
+int main(int argc, char* argv[])
+{
+    g_test_init(&argc, &argv, NULL);
+
+    g_test_add_func("/session/uri", test_session_uri);
+
+    return g_test_run();
+}
commit 2ff11ef4d2e20f0dba4950173c2c0be84deb5297
Author: Victor Toso <victortoso at redhat.com>
Date:   Thu Feb 26 15:55:42 2015 +0100

    session: accept argument in URI without value
    
    The examples below should be considered valid URIs:
    
    e.g: spice://localhost?port=5900&tls-port=
    e.g: spice://localhost?tls-port=&port=5900
    
    This patch deals with arguments with empty value;

diff --git a/gtk/spice-session.c b/gtk/spice-session.c
index 82ea55f..607224b 100644
--- a/gtk/spice-session.c
+++ b/gtk/spice-session.c
@@ -470,10 +470,26 @@ static int spice_parse_uri(SpiceSession *session, const char *original_uri)
         gchar **target_key;
 
         int len;
-        if (sscanf(query, "%31[-a-zA-Z0-9]=%127[^;&]%n", key, value, &len) != 2) {
-            g_warning("Failed to parse URI query '%s'", query);
+        if (sscanf(query, "%31[-a-zA-Z0-9]=%n", key, &len) != 1) {
+            spice_warning("Failed to parse key in URI '%s'", query);
             goto fail;
         }
+
+        query += len;
+        if (*query == '\0') {
+            spice_warning ("key '%s' without value", key);
+            break;
+        } else if (*query == ';' || *query == '&') {
+            /* another argument */
+            query++;
+            continue;
+        }
+
+        if (sscanf(query, "%127[^;&]%n", value, &len) != 1) {
+            spice_warning("Failed to parse value of key '%s' in URI '%s'", key, query);
+            goto fail;
+        }
+
         query += len;
         if (*query)
             query++;


More information about the Spice-commits mailing list