gnetworkaddress: fix parsing of URIs with IPv6 literal and port
authorDan Winship <danw@gnome.org>
Mon, 19 Aug 2013 16:09:59 +0000 (12:09 -0400)
committerDan Winship <danw@gnome.org>
Mon, 19 Aug 2013 16:15:36 +0000 (12:15 -0400)
Addresses like "http://[::1]:8080/" were being parsed as though they
did not include a port. Fix that, and add some more parsing tests.

gio/gnetworkaddress.c
gio/tests/network-address.c

index 4f6c623..394a21a 100644 (file)
@@ -615,6 +615,9 @@ _g_uri_parse_authority (const char  *uri,
   if (host)
     *host = g_uri_unescape_segment (start, p - 1, NULL);
 
+  if (c == ']')
+    c = *p++;
+
   if (c == ':')
     {
       /* Decode pot:
index 7a419c2..173e659 100644 (file)
@@ -22,64 +22,64 @@ test_basic (void)
   g_object_unref (address);
 }
 
+typedef struct {
+  const gchar *input;
+  const gchar *scheme;
+  const gchar *hostname;
+  guint16 port;
+  gint error_code;
+} ParseTest;
+
+static ParseTest uri_tests[] = {
+  { "http://www.gnome.org:2020/start", "http", "www.gnome.org", 2020, -1 },
+  { "ftp://joe~:(*)%46@ftp.gnome.org:2020/start", "ftp", "ftp.gnome.org", 2020, -1 },
+  { "ftp://[fec0::abcd]/start", "ftp", "fec0::abcd", 8080, -1 },
+  { "ftp://[fec0::abcd]:999/start", "ftp", "fec0::abcd", 999, -1 },
+  { "ftp://joe%x-@ftp.gnome.org:2020/start", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT }
+};
+
 static void
-test_parse_uri (void)
+test_parse_uri (gconstpointer d)
 {
+  const ParseTest *test = d;
   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);
+  GError *error;
 
-  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);
+  error = NULL;
+  address = (GNetworkAddress*)g_network_address_parse_uri (test->input, 8080, &error);
 
-  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);
+  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);
 
-  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);
+  if (address)
+    g_object_unref (address);
+  if (error)
+    g_error_free (error);
 }
 
-typedef struct _ParseTest ParseTest;
-
-struct _ParseTest
+static ParseTest host_tests[] =
 {
-  const gchar *input;
-  const gchar *hostname;
-  guint16 port;
-  gint error_code;
-};
-
-static ParseTest tests[] =
-{
-  { "www.gnome.org", "www.gnome.org", 1234, -1 },
-  { "www.gnome.org:8080", "www.gnome.org", 8080, -1 },
-  { "[2001:db8::1]", "2001:db8::1", 1234, -1 },
-  { "[2001:db8::1]:888", "2001:db8::1", 888, -1 },
-  { "[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 }
+  { "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 (gconstpointer d)
+test_parse_host (gconstpointer d)
 {
   const ParseTest *test = d;
   GNetworkAddress *address;
@@ -90,6 +90,7 @@ test_parse (gconstpointer d)
 
   if (address)
     {
+      g_assert_null (g_network_address_get_scheme (address));
       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);
@@ -114,12 +115,18 @@ main (int argc, char *argv[])
   g_test_init (&argc, &argv, NULL);
 
   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-host/%d", i);
+      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/%d", i);
-      g_test_add_data_func (path, &tests[i], test_parse);
+      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);
     }