Tweak is_ip_address and add IPv6 test cases

Stop wrongly mistaking foo::bar for IPv6.
This commit is contained in:
Christian Dywan 2012-02-29 23:17:02 +01:00
parent 7e61fce9ca
commit 7361a80e24
3 changed files with 27 additions and 4 deletions

View file

@ -125,8 +125,17 @@ namespace Midori {
FIXME: Schemes are not handled FIXME: Schemes are not handled
hostname_is_ip_address () is not used because hostname_is_ip_address () is not used because
we'd have to separate the path from the URI first. */ we'd have to separate the path from the URI first. */
return uri != null && uri[0].isdigit () if (uri == null)
&& (uri.chr (4, '.') != null || uri.chr (4, ':') != null); return false;
/* IPv4 */
if (uri[0].isdigit () && (uri.chr (4, '.') != null))
return true;
/* IPv6 */
if (uri[0].isalnum () && uri[1].isalnum ()
&& uri[2].isalnum () && uri[3].isalnum () && uri[4] == ':'
&& (uri[5] == ':' || uri[5].isalnum ()))
return true;
return false;
} }
public static bool is_valid (string? uri) { public static bool is_valid (string? uri) {
return uri != null return uri != null

View file

@ -684,7 +684,7 @@ sokoke_magic_uri (const gchar* uri)
search = NULL; search = NULL;
if (!strchr (uri, ' ') && if (!strchr (uri, ' ') &&
((search = strchr (uri, ':')) || (search = strchr (uri, '@'))) && ((search = strchr (uri, ':')) || (search = strchr (uri, '@'))) &&
search[0] && !g_ascii_isalpha (search[1])) search[0] && g_ascii_isdigit (search[1]))
return g_strconcat ("http://", uri, NULL); return g_strconcat ("http://", uri, NULL);
if ((!strcmp (uri, "localhost") || strchr (uri, '/')) if ((!strcmp (uri, "localhost") || strchr (uri, '/'))
&& sokoke_resolve_hostname (uri)) && sokoke_resolve_hostname (uri))

View file

@ -13,6 +13,7 @@
#include <midori/midori.h> #include <midori/midori.h>
#define SM "http://www.searchmash.com/search/" #define SM "http://www.searchmash.com/search/"
#define HTTP_PREFIX "midori-unit-test-expected-http-prefix"
static void static void
test_input (const gchar* input, test_input (const gchar* input,
@ -20,6 +21,7 @@ test_input (const gchar* input,
{ {
static KatzeArray* search_engines = NULL; static KatzeArray* search_engines = NULL;
gchar* uri; gchar* uri;
gchar* real_expected = NULL;
if (G_UNLIKELY (!search_engines)) if (G_UNLIKELY (!search_engines))
{ {
@ -58,7 +60,12 @@ test_input (const gchar* input,
uri = search_uri ? midori_uri_for_search (search_uri, keywords) : NULL; uri = search_uri ? midori_uri_for_search (search_uri, keywords) : NULL;
} }
katze_assert_str_equal (input, uri, expected);
if (!g_strcmp0 (expected, HTTP_PREFIX))
real_expected = g_strconcat ("http://", input, NULL);
katze_assert_str_equal (input, uri, real_expected ? real_expected : expected);
g_free (real_expected);
g_free (uri); g_free (uri);
} }
@ -93,6 +100,13 @@ magic_uri_uri (void)
/* test_input ("foo:f1o2o3@bar.baz", "http://f1o2o3:foo@bar.baz"); */ /* test_input ("foo:f1o2o3@bar.baz", "http://f1o2o3:foo@bar.baz"); */
/* test_input ("foo:foo@bar.baz", "http://foo:foo@bar.baz"); */ /* test_input ("foo:foo@bar.baz", "http://foo:foo@bar.baz"); */
test_input ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", HTTP_PREFIX);
test_input ("fe80:0:0:0:202:b3ff:fe1e:8329", HTTP_PREFIX);
test_input ("fe80::202:b3ff:fe1e:8329", HTTP_PREFIX);
test_input ("fe80::76e5:bff:fe04:38e0/64", HTTP_PREFIX);
test_input ("content::browser", NULL);
test_input ("std::copy", NULL);
uri = "http://bugs.launchpad.net/midori"; uri = "http://bugs.launchpad.net/midori";
g_assert_cmpstr ("bugs.launchpad.net", ==, midori_uri_parse_hostname (uri, NULL)); g_assert_cmpstr ("bugs.launchpad.net", ==, midori_uri_parse_hostname (uri, NULL));
uri = "https://bugs.launchpad.net/midori"; uri = "https://bugs.launchpad.net/midori";