mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-26 14:06:15 +01:00
gnetworkaddress: fix parsing of URIs with IPv6 literal and port
Addresses like "http://[::1]:8080/" were being parsed as though they did not include a port. Fix that, and add some more parsing tests.
This commit is contained in:
parent
635196b1ff
commit
e1f92431c1
@ -615,6 +615,9 @@ _g_uri_parse_authority (const char *uri,
|
|||||||
if (host)
|
if (host)
|
||||||
*host = g_uri_unescape_segment (start, p - 1, NULL);
|
*host = g_uri_unescape_segment (start, p - 1, NULL);
|
||||||
|
|
||||||
|
if (c == ']')
|
||||||
|
c = *p++;
|
||||||
|
|
||||||
if (c == ':')
|
if (c == ':')
|
||||||
{
|
{
|
||||||
/* Decode pot:
|
/* Decode pot:
|
||||||
|
@ -22,64 +22,64 @@ test_basic (void)
|
|||||||
g_object_unref (address);
|
g_object_unref (address);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
typedef struct {
|
||||||
test_parse_uri (void)
|
|
||||||
{
|
|
||||||
GNetworkAddress *address;
|
|
||||||
GError *error = NULL;
|
|
||||||
|
|
||||||
address = (GNetworkAddress*)g_network_address_parse_uri ("http://www.gnome.org:2020/start", 8080, &error);
|
|
||||||
g_assert_no_error (error);
|
|
||||||
g_assert_cmpstr (g_network_address_get_scheme (address), ==, "http");
|
|
||||||
g_assert_cmpstr (g_network_address_get_hostname (address), ==, "www.gnome.org");
|
|
||||||
g_assert_cmpint (g_network_address_get_port (address), ==, 2020);
|
|
||||||
g_object_unref (address);
|
|
||||||
|
|
||||||
address = (GNetworkAddress*)g_network_address_parse_uri ("ftp://joe~:(*)%46@ftp.gnome.org:2020/start", 8080, &error);
|
|
||||||
g_assert_no_error (error);
|
|
||||||
g_assert_cmpstr (g_network_address_get_scheme (address), ==, "ftp");
|
|
||||||
g_assert_cmpstr (g_network_address_get_hostname (address), ==, "ftp.gnome.org");
|
|
||||||
g_assert_cmpint (g_network_address_get_port (address), ==, 2020);
|
|
||||||
g_object_unref (address);
|
|
||||||
|
|
||||||
address = (GNetworkAddress*)g_network_address_parse_uri ("ftp://[fec0::abcd]/start", 8080, &error);
|
|
||||||
g_assert_no_error (error);
|
|
||||||
g_assert_cmpstr (g_network_address_get_scheme (address), ==, "ftp");
|
|
||||||
g_assert_cmpstr (g_network_address_get_hostname (address), ==, "fec0::abcd");
|
|
||||||
g_assert_cmpint (g_network_address_get_port (address), ==, 8080);
|
|
||||||
g_object_unref (address);
|
|
||||||
|
|
||||||
address = (GNetworkAddress*)g_network_address_parse_uri ("ftp://joe%x-@ftp.gnome.org:2020/start", 8080, &error);
|
|
||||||
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
|
|
||||||
g_assert (address == NULL);
|
|
||||||
g_error_free (error);
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct _ParseTest ParseTest;
|
|
||||||
|
|
||||||
struct _ParseTest
|
|
||||||
{
|
|
||||||
const gchar *input;
|
const gchar *input;
|
||||||
|
const gchar *scheme;
|
||||||
const gchar *hostname;
|
const gchar *hostname;
|
||||||
guint16 port;
|
guint16 port;
|
||||||
gint error_code;
|
gint error_code;
|
||||||
};
|
} ParseTest;
|
||||||
|
|
||||||
static ParseTest tests[] =
|
static ParseTest uri_tests[] = {
|
||||||
{
|
{ "http://www.gnome.org:2020/start", "http", "www.gnome.org", 2020, -1 },
|
||||||
{ "www.gnome.org", "www.gnome.org", 1234, -1 },
|
{ "ftp://joe~:(*)%46@ftp.gnome.org:2020/start", "ftp", "ftp.gnome.org", 2020, -1 },
|
||||||
{ "www.gnome.org:8080", "www.gnome.org", 8080, -1 },
|
{ "ftp://[fec0::abcd]/start", "ftp", "fec0::abcd", 8080, -1 },
|
||||||
{ "[2001:db8::1]", "2001:db8::1", 1234, -1 },
|
{ "ftp://[fec0::abcd]:999/start", "ftp", "fec0::abcd", 999, -1 },
|
||||||
{ "[2001:db8::1]:888", "2001:db8::1", 888, -1 },
|
{ "ftp://joe%x-@ftp.gnome.org:2020/start", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT }
|
||||||
{ "[hostname", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
|
|
||||||
{ "[hostnam]e", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
|
|
||||||
{ "hostname:", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
|
|
||||||
{ "hostname:-1", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
|
|
||||||
{ "hostname:9999999", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_parse (gconstpointer d)
|
test_parse_uri (gconstpointer d)
|
||||||
|
{
|
||||||
|
const ParseTest *test = d;
|
||||||
|
GNetworkAddress *address;
|
||||||
|
GError *error;
|
||||||
|
|
||||||
|
error = NULL;
|
||||||
|
address = (GNetworkAddress*)g_network_address_parse_uri (test->input, 8080, &error);
|
||||||
|
|
||||||
|
if (address)
|
||||||
|
{
|
||||||
|
g_assert_cmpstr (g_network_address_get_scheme (address), ==, test->scheme);
|
||||||
|
g_assert_cmpstr (g_network_address_get_hostname (address), ==, test->hostname);
|
||||||
|
g_assert_cmpint (g_network_address_get_port (address), ==, test->port);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
g_assert_error (error, G_IO_ERROR, test->error_code);
|
||||||
|
|
||||||
|
if (address)
|
||||||
|
g_object_unref (address);
|
||||||
|
if (error)
|
||||||
|
g_error_free (error);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ParseTest host_tests[] =
|
||||||
|
{
|
||||||
|
{ "www.gnome.org", NULL, "www.gnome.org", 1234, -1 },
|
||||||
|
{ "www.gnome.org:8080", NULL, "www.gnome.org", 8080, -1 },
|
||||||
|
{ "[2001:db8::1]", NULL, "2001:db8::1", 1234, -1 },
|
||||||
|
{ "[2001:db8::1]:888", NULL, "2001:db8::1", 888, -1 },
|
||||||
|
{ "[2001:db8::1%em1]", NULL, "2001:db8::1%em1", 1234, -1 },
|
||||||
|
{ "[hostname", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
|
||||||
|
{ "[hostnam]e", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
|
||||||
|
{ "hostname:", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
|
||||||
|
{ "hostname:-1", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
|
||||||
|
{ "hostname:9999999", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT }
|
||||||
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_parse_host (gconstpointer d)
|
||||||
{
|
{
|
||||||
const ParseTest *test = d;
|
const ParseTest *test = d;
|
||||||
GNetworkAddress *address;
|
GNetworkAddress *address;
|
||||||
@ -90,6 +90,7 @@ test_parse (gconstpointer d)
|
|||||||
|
|
||||||
if (address)
|
if (address)
|
||||||
{
|
{
|
||||||
|
g_assert_null (g_network_address_get_scheme (address));
|
||||||
g_assert_cmpstr (g_network_address_get_hostname (address), ==, test->hostname);
|
g_assert_cmpstr (g_network_address_get_hostname (address), ==, test->hostname);
|
||||||
g_assert_cmpint (g_network_address_get_port (address), ==, test->port);
|
g_assert_cmpint (g_network_address_get_port (address), ==, test->port);
|
||||||
g_assert_no_error (error);
|
g_assert_no_error (error);
|
||||||
@ -114,12 +115,18 @@ main (int argc, char *argv[])
|
|||||||
g_test_init (&argc, &argv, NULL);
|
g_test_init (&argc, &argv, NULL);
|
||||||
|
|
||||||
g_test_add_func ("/network-address/basic", test_basic);
|
g_test_add_func ("/network-address/basic", test_basic);
|
||||||
g_test_add_func ("/network-address/parse/uri", test_parse_uri);
|
|
||||||
|
|
||||||
for (i = 0; i < G_N_ELEMENTS (tests); i++)
|
for (i = 0; i < G_N_ELEMENTS (host_tests); i++)
|
||||||
{
|
{
|
||||||
path = g_strdup_printf ("/network-address/parse/%d", i);
|
path = g_strdup_printf ("/network-address/parse-host/%d", i);
|
||||||
g_test_add_data_func (path, &tests[i], test_parse);
|
g_test_add_data_func (path, &host_tests[i], test_parse_host);
|
||||||
|
g_free (path);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < G_N_ELEMENTS (uri_tests); i++)
|
||||||
|
{
|
||||||
|
path = g_strdup_printf ("/network-address/parse-uri/%d", i);
|
||||||
|
g_test_add_data_func (path, &uri_tests[i], test_parse_uri);
|
||||||
g_free (path);
|
g_free (path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user