[Spice-commits] 6 commits - src/spice-uri.c tests/Makefile.am tests/uri.c
Pavel Grunt
pgrunt at kemper.freedesktop.org
Thu Jun 2 08:36:34 UTC 2016
src/spice-uri.c | 76 +++++++++++++++++++++-------
tests/Makefile.am | 2
tests/uri.c | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 202 insertions(+), 19 deletions(-)
New commits:
commit b542dfa2d58cd352dc596318a1e50f074865ef5e
Author: Pavel Grunt <pgrunt at redhat.com>
Date: Wed Jun 1 10:04:49 2016 +0200
spice-uri: Add ipv6 support
Just basic support - http://user:password@[host]:port
Resolves: rhbz#1335239
diff --git a/src/spice-uri.c b/src/spice-uri.c
index 83ebe79..0376cd8 100644
--- a/src/spice-uri.c
+++ b/src/spice-uri.c
@@ -161,8 +161,26 @@ gboolean spice_uri_parse(SpiceURI *self, const gchar *_uri, GError **error)
uri = next;
}
- /* max 2 parts, host:port */
- uriv = g_strsplit(uri, ":", 2);
+ if (*uri == '[') { /* ipv6 address */
+ uriv = g_strsplit(uri + 1, "]", 2);
+ if (uriv[1] == NULL) {
+ g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
+ "Missing ']' in ipv6 uri");
+ goto end;
+ }
+ if (*uriv[1] == ':') {
+ uri_port = uriv[1] + 1;
+ } else if (strlen(uriv[1]) > 0) { /* invalid string after the hostname */
+ g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
+ "Invalid uri address");
+ goto end;
+ }
+ } else {
+ /* max 2 parts, host:port */
+ uriv = g_strsplit(uri, ":", 2);
+ if (uriv[0] != NULL)
+ uri_port = uriv[1];
+ }
if (uriv[0] == NULL || strlen(uriv[0]) == 0) {
g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
@@ -171,8 +189,6 @@ gboolean spice_uri_parse(SpiceURI *self, const gchar *_uri, GError **error)
}
spice_uri_set_hostname(self, uriv[0]);
- if (uriv[0] != NULL)
- uri_port = uriv[1];
if (uri_port != NULL) {
gchar *endptr;
diff --git a/tests/uri.c b/tests/uri.c
index 80b00f4..ea8b794 100644
--- a/tests/uri.c
+++ b/tests/uri.c
@@ -99,12 +99,45 @@ static void test_spice_uri_ipv4_good(void)
test_spice_uri_good(valid_test_cases, G_N_ELEMENTS(valid_test_cases));
}
+static void test_spice_uri_ipv6_bad(void)
+{
+ const struct test_case invalid_test_cases[] = {
+ {"http://[]:80", "http", NULL, 80, NULL, NULL, "Invalid hostname in uri address"},
+ {"http://[::1", "http", NULL, 3128, NULL, NULL, "Missing ']' in ipv6 uri"},
+ {"http://[host]1234", "http", "host", 3128, NULL, NULL, "Invalid uri address"},
+ {"http://[host]foo/", "http", "host", 3128, NULL, NULL, "Invalid uri address"},
+ {"http://[::1]:port", "http", "::1", 3128, NULL, NULL, "Invalid uri port: port"},
+ {"http://[::127.0.0.1]:", "http", "::127.0.0.1", 3128, NULL, NULL, "Missing uri port"},
+ {"http://[::127.0.0.1]:-42", "http", "::127.0.0.1", 3128, NULL, NULL, "Port out of range"},
+ {"[3ffe:2a00:100:7031::1]:42000000", "http", "3ffe:2a00:100:7031::1", 3128, NULL, NULL, "Port out of range"},
+ {"scheme://[3ffe::192.168.1.1]:3128", "http", "3ffe::192.168.1.1", 3128, NULL, NULL,
+ "Invalid uri scheme for proxy: scheme"},
+ };
+
+ test_spice_uri_bad(invalid_test_cases, G_N_ELEMENTS(invalid_test_cases));
+}
+
+static void test_spice_uri_ipv6_good(void)
+{
+ const struct test_case valid_test_cases[] = {
+ {"http://user:password@[host]:80/", "http", "host", 80, "user", "password", NULL},
+ {"http://user@[1080:0:0:0:8:800:200C:4171]:100", "http", "1080:0:0:0:8:800:200C:4171", 100,
+ "user", NULL, NULL},
+ {"https://[1080::8:800:200C:417A]", "https", "1080::8:800:200C:417A", 3129, NULL, NULL, NULL},
+ {"[3ffe:2a00:100:7031::1]", "http", "3ffe:2a00:100:7031::1", 3128, NULL, NULL, NULL},
+ };
+
+ test_spice_uri_good(valid_test_cases, G_N_ELEMENTS(valid_test_cases));
+}
+
int main(int argc, char* argv[])
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/spice_uri/ipv4/bad-uri", test_spice_uri_ipv4_bad);
g_test_add_func("/spice_uri/ipv4/good-uri", test_spice_uri_ipv4_good);
+ g_test_add_func("/spice_uri/ipv6/bad-uri", test_spice_uri_ipv6_bad);
+ g_test_add_func("/spice_uri/ipv6/good-uri", test_spice_uri_ipv6_good);
return g_test_run();
}
commit 8dcb4129acde2aed353cd66e28678408e7d1257c
Author: Pavel Grunt <pgrunt at redhat.com>
Date: Wed Jun 1 10:04:48 2016 +0200
spice-uri: Validate uri scheme
Related: rhbz#1335239
Acked-by: Victor Toso <victortoso at redhat.com>
diff --git a/src/spice-uri.c b/src/spice-uri.c
index 7eec6e5..83ebe79 100644
--- a/src/spice-uri.c
+++ b/src/spice-uri.c
@@ -109,7 +109,9 @@ static void spice_uri_reset(SpiceURI *self)
G_GNUC_INTERNAL
gboolean spice_uri_parse(SpiceURI *self, const gchar *_uri, GError **error)
{
- gchar *dup, *uri;
+ gchar *dup, *uri, **uriv = NULL;
+ const gchar *uri_port = NULL;
+ char *uri_scheme = NULL;
gboolean success = FALSE;
size_t len;
@@ -122,17 +124,21 @@ gboolean spice_uri_parse(SpiceURI *self, const gchar *_uri, GError **error)
uri = dup = g_strdup(_uri);
/* FIXME: use GUri when it is ready... only support http atm */
/* the code is voluntarily not parsing thoroughly the uri */
- if (g_ascii_strncasecmp("http://", uri, 7) == 0) {
- uri += 7;
+ uri_scheme = g_uri_parse_scheme(uri);
+ if (uri_scheme == NULL) {
spice_uri_set_scheme(self, "http");
+ } else {
+ spice_uri_set_scheme(self, uri_scheme);
+ uri += strlen(uri_scheme) + 3; /* scheme + "://" */
+ }
+ if (g_ascii_strcasecmp(spice_uri_get_scheme(self), "http") == 0) {
spice_uri_set_port(self, 3128);
- } else if (g_ascii_strncasecmp("https://", uri, 8) == 0) {
- uri += 8;
- spice_uri_set_scheme(self, "https");
+ } else if (g_ascii_strcasecmp(spice_uri_get_scheme(self), "https") == 0) {
spice_uri_set_port(self, 3129);
} else {
- spice_uri_set_scheme(self, "http");
- spice_uri_set_port(self, 3128);
+ g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
+ "Invalid uri scheme for proxy: %s", spice_uri_get_scheme(self));
+ goto end;
}
/* remove trailing slash */
len = strlen(uri);
@@ -156,8 +162,7 @@ gboolean spice_uri_parse(SpiceURI *self, const gchar *_uri, GError **error)
}
/* max 2 parts, host:port */
- gchar **uriv = g_strsplit(uri, ":", 2);
- const gchar *uri_port = NULL;
+ uriv = g_strsplit(uri, ":", 2);
if (uriv[0] == NULL || strlen(uriv[0]) == 0) {
g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
@@ -190,6 +195,7 @@ gboolean spice_uri_parse(SpiceURI *self, const gchar *_uri, GError **error)
success = TRUE;
end:
+ free(uri_scheme);
g_free(dup);
g_strfreev(uriv);
return success;
diff --git a/tests/uri.c b/tests/uri.c
index 34569ec..80b00f4 100644
--- a/tests/uri.c
+++ b/tests/uri.c
@@ -79,6 +79,8 @@ static void test_spice_uri_ipv4_bad(void)
{"http://127.0.0.1:-80", "http", "127.0.0.1", 3128, NULL, NULL, "Port out of range"},
{"http://127.0.0.1:4294967396", "http", "127.0.0.1", 3128, NULL, NULL, "Port out of range"},
{"http://127.0.0.1:12345678901234", "http", "127.0.0.1", 3128, NULL, NULL, "Port out of range"},
+ {"scheme://192.168.1.1:3128", "http", "127.0.0.1", 3128, NULL, NULL,
+ "Invalid uri scheme for proxy: scheme"},
};
test_spice_uri_bad(invalid_test_cases, G_N_ELEMENTS(invalid_test_cases));
commit eacbe261d48979f72585225107d00bba76623e8e
Author: Pavel Grunt <pgrunt at redhat.com>
Date: Wed Jun 1 10:04:47 2016 +0200
spice-uri: Check if port is in allowed range
Use g_ascii_strtoll because it helps to detect overflow.
Related: rhbz#1335239
Acked-by: Victor Toso <victortoso at redhat.com>
diff --git a/src/spice-uri.c b/src/spice-uri.c
index ea25aaa..7eec6e5 100644
--- a/src/spice-uri.c
+++ b/src/spice-uri.c
@@ -170,8 +170,8 @@ gboolean spice_uri_parse(SpiceURI *self, const gchar *_uri, GError **error)
uri_port = uriv[1];
if (uri_port != NULL) {
- char *endptr;
- guint port = strtoul(uri_port, &endptr, 10);
+ gchar *endptr;
+ gint64 port = g_ascii_strtoll(uri_port, &endptr, 10);
if (*endptr != '\0') {
g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
"Invalid uri port: %s", uri_port);
@@ -180,6 +180,10 @@ gboolean spice_uri_parse(SpiceURI *self, const gchar *_uri, GError **error)
g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED, "Missing uri port");
goto end;
}
+ if (port <= 0 || port > 65535) {
+ g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED, "Port out of range");
+ goto end;
+ }
spice_uri_set_port(self, port);
}
diff --git a/tests/uri.c b/tests/uri.c
index ee3d061..34569ec 100644
--- a/tests/uri.c
+++ b/tests/uri.c
@@ -76,6 +76,9 @@ static void test_spice_uri_ipv4_bad(void)
{"http://127.0.0.1:port", "http", "127.0.0.1", 3128, NULL, NULL,
"Invalid uri port: port"},
{"http://127.0.0.1:", "http", "127.0.0.1", 3128, NULL, NULL, "Missing uri port"},
+ {"http://127.0.0.1:-80", "http", "127.0.0.1", 3128, NULL, NULL, "Port out of range"},
+ {"http://127.0.0.1:4294967396", "http", "127.0.0.1", 3128, NULL, NULL, "Port out of range"},
+ {"http://127.0.0.1:12345678901234", "http", "127.0.0.1", 3128, NULL, NULL, "Port out of range"},
};
test_spice_uri_bad(invalid_test_cases, G_N_ELEMENTS(invalid_test_cases));
commit 85051b06c1f04b206c00bb7f674a9a29bfb19594
Author: Pavel Grunt <pgrunt at redhat.com>
Date: Wed Jun 1 10:04:46 2016 +0200
spice-uri: Do not allow empty port string
Related: rhbz#1335239
Acked-by: Victor Toso <victortoso at redhat.com>
diff --git a/src/spice-uri.c b/src/spice-uri.c
index 9f793de..ea25aaa 100644
--- a/src/spice-uri.c
+++ b/src/spice-uri.c
@@ -176,6 +176,9 @@ gboolean spice_uri_parse(SpiceURI *self, const gchar *_uri, GError **error)
g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
"Invalid uri port: %s", uri_port);
goto end;
+ } else if (endptr == uri_port) {
+ g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED, "Missing uri port");
+ goto end;
}
spice_uri_set_port(self, port);
}
diff --git a/tests/uri.c b/tests/uri.c
index d57f072..ee3d061 100644
--- a/tests/uri.c
+++ b/tests/uri.c
@@ -75,6 +75,7 @@ static void test_spice_uri_ipv4_bad(void)
{"http://", "http", NULL, 3128, NULL, NULL, "Invalid hostname in uri address"},
{"http://127.0.0.1:port", "http", "127.0.0.1", 3128, NULL, NULL,
"Invalid uri port: port"},
+ {"http://127.0.0.1:", "http", "127.0.0.1", 3128, NULL, NULL, "Missing uri port"},
};
test_spice_uri_bad(invalid_test_cases, G_N_ELEMENTS(invalid_test_cases));
commit 024eccefba506f57fbff837c6d75864c51ed2add
Author: Pavel Grunt <pgrunt at redhat.com>
Date: Wed Jun 1 10:04:45 2016 +0200
spice-uri: Reset SpiceURI before parsing
Avoid using old values after parsing a new uri.
Related: rhbz#1335239
diff --git a/src/spice-uri.c b/src/spice-uri.c
index 3bdb502..9f793de 100644
--- a/src/spice-uri.c
+++ b/src/spice-uri.c
@@ -97,6 +97,15 @@ SpiceURI* spice_uri_new(void)
return self;
}
+static void spice_uri_reset(SpiceURI *self)
+{
+ g_clear_pointer(&self->scheme, g_free);
+ g_clear_pointer(&self->hostname, g_free);
+ g_clear_pointer(&self->user, g_free);
+ g_clear_pointer(&self->password, g_free);
+ self->port = 0;
+}
+
G_GNUC_INTERNAL
gboolean spice_uri_parse(SpiceURI *self, const gchar *_uri, GError **error)
{
@@ -105,6 +114,9 @@ gboolean spice_uri_parse(SpiceURI *self, const gchar *_uri, GError **error)
size_t len;
g_return_val_if_fail(self != NULL, FALSE);
+
+ spice_uri_reset(self);
+
g_return_val_if_fail(_uri != NULL, FALSE);
uri = dup = g_strdup(_uri);
@@ -333,10 +345,7 @@ static void spice_uri_finalize(GObject* obj)
SpiceURI *self;
self = G_TYPE_CHECK_INSTANCE_CAST(obj, SPICE_TYPE_URI, SpiceURI);
- g_free(self->scheme);
- g_free(self->hostname);
- g_free(self->user);
- g_free(self->password);
+ spice_uri_reset(self);
G_OBJECT_CLASS (spice_uri_parent_class)->finalize (obj);
}
diff --git a/tests/uri.c b/tests/uri.c
index b68f159..d57f072 100644
--- a/tests/uri.c
+++ b/tests/uri.c
@@ -87,6 +87,7 @@ static void test_spice_uri_ipv4_good(void)
{"https://127.0.0.1", "https", "127.0.0.1", 3129, NULL, NULL, NULL},
{"127.0.0.1", "http", "127.0.0.1", 3128, NULL, NULL, NULL},
{"http://user:password@host:80", "http", "host", 80, "user", "password", NULL},
+ {"https://host:42", "https", "host", 42, NULL, NULL, NULL}, /* tests resetting of username & password */
};
test_spice_uri_good(valid_test_cases, G_N_ELEMENTS(valid_test_cases));
commit dab190d8d2cb93f85f1858135354dd4e93ff48f8
Author: Pavel Grunt <pgrunt at redhat.com>
Date: Wed Jun 1 10:04:44 2016 +0200
tests: Add test for SpiceURI
Related: rhbz#1335239
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d56ff1b..1a8b768 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
@@ -36,6 +37,7 @@ util_SOURCES = util.c
coroutine_SOURCES = coroutine.c
session_SOURCES = session.c
pipe_SOURCES = pipe.c
+test_spice_uri_SOURCES = 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/uri.c b/tests/uri.c
new file mode 100644
index 0000000..b68f159
--- /dev/null
+++ b/tests/uri.c
@@ -0,0 +1,103 @@
+/* -*- 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;
+ gchar *error_msg;
+};
+
+static void test_spice_uri_bad(const struct test_case invalid_test_cases[], const guint cases_cnt)
+{
+ guint i;
+
+ SpiceURI *uri = spice_uri_new();
+ g_assert_nonnull(uri);
+
+ for (i = 0; i < cases_cnt; i++) {
+ GError *error = NULL;
+ g_assert_false(spice_uri_parse(uri, invalid_test_cases[i].uri, &error));
+ g_assert_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED);
+ g_assert_cmpstr(error->message, ==, invalid_test_cases[i].error_msg);
+ g_error_free(error);
+ }
+
+ g_object_unref(uri);
+}
+
+static void test_spice_uri_good(const struct test_case valid_test_cases[], const guint cases_cnt)
+{
+ guint i;
+
+ SpiceURI *uri = spice_uri_new();
+ g_assert_nonnull(uri);
+
+ for (i = 0; i < cases_cnt; i++) {
+ GError *error = NULL;
+ g_assert_true(spice_uri_parse(uri, valid_test_cases[i].uri, &error));
+ 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_assert_no_error(error);
+ }
+
+ g_object_unref(uri);
+}
+
+static void test_spice_uri_ipv4_bad(void)
+{
+ const struct test_case invalid_test_cases[] = {
+ {"http://:80", "http", NULL, 80, NULL, NULL, "Invalid hostname in uri address"},
+ {"http://", "http", NULL, 3128, NULL, NULL, "Invalid hostname in uri address"},
+ {"http://127.0.0.1:port", "http", "127.0.0.1", 3128, NULL, NULL,
+ "Invalid uri port: port"},
+ };
+
+ test_spice_uri_bad(invalid_test_cases, G_N_ELEMENTS(invalid_test_cases));
+}
+
+static void test_spice_uri_ipv4_good(void)
+{
+ const struct test_case valid_test_cases[] = {
+ {"http://127.0.0.1/", "http", "127.0.0.1", 3128, NULL, NULL, NULL},
+ {"https://127.0.0.1", "https", "127.0.0.1", 3129, NULL, NULL, NULL},
+ {"127.0.0.1", "http", "127.0.0.1", 3128, NULL, NULL, NULL},
+ {"http://user:password@host:80", "http", "host", 80, "user", "password", NULL},
+ };
+
+ test_spice_uri_good(valid_test_cases, G_N_ELEMENTS(valid_test_cases));
+}
+
+int main(int argc, char* argv[])
+{
+ g_test_init(&argc, &argv, NULL);
+
+ g_test_add_func("/spice_uri/ipv4/bad-uri", test_spice_uri_ipv4_bad);
+ g_test_add_func("/spice_uri/ipv4/good-uri", test_spice_uri_ipv4_good);
+
+ return g_test_run();
+}
More information about the Spice-commits
mailing list