[Spice-devel] [PATCH spice-gtk v2 1/5] tests: Add test for SpiceUri

Pavel Grunt pgrunt at redhat.com
Tue May 17 17:30:45 UTC 2016


Related: rhbz#1335239
---
 tests/Makefile.am      |  2 ++
 tests/test-spice-uri.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)
 create mode 100644 tests/test-spice-uri.c

diff --git a/tests/Makefile.am b/tests/Makefile.am
index c1d95c1..fb97138 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -4,6 +4,7 @@ noinst_PROGRAMS =
 TESTS = coroutine				\
 	util					\
 	session					\
+	test-spice-uri				\
 	$(NULL)
 
 if WITH_PHODAV
@@ -35,6 +36,7 @@ util_SOURCES = util.c
 coroutine_SOURCES = coroutine.c
 session_SOURCES = session.c
 pipe_SOURCES = pipe.c
+test_spice_uri_SOURCES = test-spice-uri.c
 usb_acl_helper_SOURCES = usb-acl-helper.c
 usb_acl_helper_CFLAGS = -DTESTDIR=\"$(abs_builddir)\"
 mock_acl_helper_SOURCES = mock-acl-helper.c
diff --git a/tests/test-spice-uri.c b/tests/test-spice-uri.c
new file mode 100644
index 0000000..e8cfcc9
--- /dev/null
+++ b/tests/test-spice-uri.c
@@ -0,0 +1,73 @@
+/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+   Copyright (C) 2016 Red Hat, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+#include <glib.h>
+#include <spice-client.h>
+#include "spice-uri-priv.h"
+
+struct test_case {
+    gchar *uri;
+    gchar *scheme;
+    gchar *hostname;
+    guint port;
+    gchar *user;
+    gchar *password;
+};
+
+static void test_spice_uri_ipv4(void)
+{
+    const struct test_case invalid_test_cases[] = {
+        {"http://:80", "http", NULL, 80, NULL, NULL}, /* missing hostname */
+        {"http://", "http", NULL, 3128, NULL, NULL},
+        {"http://127.0.0.1:port", "http", "127.0.0.1", 3128, NULL, NULL}, /* invalid port */
+    };
+    const struct test_case valid_test_cases[] = {
+        {"http://127.0.0.1/", "http", "127.0.0.1", 3128, NULL, NULL},
+        {"https://127.0.0.1", "https", "127.0.0.1", 3129, NULL, NULL},
+        {"127.0.0.1", "http", "127.0.0.1", 3128, NULL, NULL},
+        {"http://user:password@host:80", "http", "host", 80, "user", "password"},
+    };
+
+    guint i;
+
+    SpiceURI *uri = spice_uri_new();
+    g_assert_nonnull(uri);
+
+    for (i = 0; i < G_N_ELEMENTS(invalid_test_cases); i++) {
+        g_assert_false(spice_uri_parse(uri, invalid_test_cases[i].uri, NULL));
+    }
+
+    for (i = 0; i < G_N_ELEMENTS(valid_test_cases); i++) {
+        g_assert_true(spice_uri_parse(uri, valid_test_cases[i].uri, NULL));
+        g_assert_cmpstr(spice_uri_get_scheme(uri), ==, valid_test_cases[i].scheme);
+        g_assert_cmpstr(spice_uri_get_hostname(uri), ==, valid_test_cases[i].hostname);
+        g_assert_cmpstr(spice_uri_get_user(uri), ==, valid_test_cases[i].user);
+        g_assert_cmpstr(spice_uri_get_password(uri), ==, valid_test_cases[i].password);
+        g_assert_cmpuint(spice_uri_get_port(uri), ==, valid_test_cases[i].port);
+    }
+
+    g_object_unref(uri);
+}
+
+int main(int argc, char* argv[])
+{
+    g_test_init(&argc, &argv, NULL);
+
+    g_test_add_func("/spice_uri/ipv4", test_spice_uri_ipv4);
+
+    return g_test_run();
+}
-- 
2.8.2



More information about the Spice-devel mailing list