PageRenderTime 35ms CodeModel.GetById 16ms app.highlight 16ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/magic-uri.c

https://bitbucket.org/edgimar/midori
C | 380 lines | 329 code | 36 blank | 15 comment | 12 complexity | 064b6b48d316a4ceba8904912f26f703 MD5 | raw file
Possible License(s): LGPL-2.1
  1/*
  2 Copyright (C) 2008-2009 Christian Dywan <christian@twotoasts.de>
  3 Copyright (C) 2009 Alexander Butenko <a.butenka@gmail.com>
  4
  5 This library is free software; you can redistribute it and/or
  6 modify it under the terms of the GNU Lesser General Public
  7 License as published by the Free Software Foundation; either
  8 version 2.1 of the License, or (at your option) any later version.
  9
 10 See the file COPYING for the full license text.
 11*/
 12
 13#include <midori/midori.h>
 14
 15#define SM "http://www.searchmash.com/search/"
 16#define HTTP_PREFIX "midori-unit-test-expected-http-prefix"
 17
 18static void
 19test_input (const gchar* input,
 20            const gchar* expected)
 21{
 22    static KatzeArray* search_engines = NULL;
 23    gchar* uri;
 24    gchar* real_expected = NULL;
 25
 26    if (G_UNLIKELY (!search_engines))
 27    {
 28        KatzeItem* item;
 29
 30        search_engines = katze_array_new (KATZE_TYPE_ITEM);
 31        item = g_object_new (KATZE_TYPE_ITEM,
 32                             "uri", SM "%s",
 33                             "token", "sm", NULL);
 34        katze_array_add_item (search_engines, item);
 35        g_object_unref (item);
 36        item = g_object_new (KATZE_TYPE_ITEM,
 37                             "uri", SM,
 38                             "token", "se", NULL);
 39        katze_array_add_item (search_engines, item);
 40        g_object_unref (item);
 41        item = g_object_new (KATZE_TYPE_ITEM,
 42                             "uri", "ddg.gg",
 43                             "token", "dd", NULL);
 44        katze_array_add_item (search_engines, item);
 45        g_object_unref (item);
 46        item = g_object_new (KATZE_TYPE_ITEM,
 47                             "uri", "google.com",
 48                             "token", "d", NULL);
 49        katze_array_add_item (search_engines, item);
 50        g_object_unref (item);
 51    }
 52
 53    uri = sokoke_magic_uri (input);
 54    if (!uri)
 55    {
 56        const gchar* keywords = NULL;
 57        const gchar* search_uri = NULL;
 58        KatzeItem* item;
 59
 60        /* Do we have a keyword and a string? */
 61        if ((item = katze_array_find_token (search_engines, input)))
 62        {
 63            keywords = strchr (input, ' ');
 64            if (keywords != NULL)
 65                keywords++;
 66            else
 67                keywords = "";
 68            search_uri = katze_item_get_uri (item);
 69        }
 70
 71        uri = search_uri ? midori_uri_for_search (search_uri, keywords) : NULL;
 72    }
 73
 74    if (!g_strcmp0 (expected, HTTP_PREFIX))
 75        real_expected = g_strconcat ("http://", input, NULL);
 76
 77    katze_assert_str_equal (input, uri, real_expected ? real_expected : expected);
 78    g_free (real_expected);
 79    g_free (uri);
 80}
 81
 82static void
 83magic_uri_uri (void)
 84{
 85    const gchar* uri;
 86    gchar* path;
 87
 88    test_input ("ftp://ftp.mozilla.org", "ftp://ftp.mozilla.org");
 89    test_input ("ftp://ftp.mozilla.org/pub", "ftp://ftp.mozilla.org/pub");
 90    test_input ("http://www.example.com", "http://www.example.com");
 91    test_input ("http://example.com", "http://example.com");
 92    test_input ("example.com", "http://example.com");
 93    test_input ("example.com", "http://example.com");
 94    test_input ("www.google..com", "http://www.google..com");
 95    test_input ("/home/user/midori.html", "file:///home/user/midori.html");
 96    test_input ("http://www.google.com/search?q=query test",
 97                "http://www.google.com/search?q=query test");
 98    if (sokoke_resolve_hostname ("localhost"))
 99    {
100        test_input ("localhost", "http://localhost");
101        test_input ("localhost:8000", "http://localhost:8000");
102        test_input ("localhost/rss", "http://localhost/rss");
103    }
104    test_input ("10.0.0.1", "http://10.0.0.1");
105    test_input ("192.168.1.1", "http://192.168.1.1");
106    test_input ("192.168.1.1:8000", "http://192.168.1.1:8000");
107    test_input ("file:///home/mark/foo/bar.html",
108                "file:///home/mark/foo/bar.html");
109    test_input ("foo:123@bar.baz", "http://foo:123@bar.baz");
110    /* test_input ("foo:f1o2o3@bar.baz", "http://f1o2o3:foo@bar.baz"); */
111    /* test_input ("foo:foo@bar.baz", "http://foo:foo@bar.baz"); */
112
113    test_input ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", HTTP_PREFIX);
114    test_input ("fe80:0:0:0:202:b3ff:fe1e:8329", HTTP_PREFIX);
115    test_input ("fe80::202:b3ff:fe1e:8329", HTTP_PREFIX);
116    test_input ("fe80::76e5:bff:fe04:38e0/64", HTTP_PREFIX);
117    test_input ("content::browser", NULL);
118    test_input ("std::copy", NULL);
119
120    uri = "http://bugs.launchpad.net/midori";
121    g_assert_cmpstr ("bugs.launchpad.net", ==, midori_uri_parse_hostname (uri, NULL));
122    uri = "https://bugs.launchpad.net/midori";
123    g_assert_cmpstr ("bugs.launchpad.net", ==, midori_uri_parse_hostname (uri, NULL));
124    g_assert_cmpstr ("bugs.launchpad.net", ==, midori_uri_parse_hostname (uri, &path));
125    g_assert_cmpstr ("/midori", ==, path);
126    uri = "http://??????.ru/users/kotyata";
127    g_assert_cmpstr ("??????.ru", ==, midori_uri_parse_hostname (uri, &path));
128    g_assert_cmpstr ("/users/kotyata", ==, path);
129    uri = "invalid:/uri.like/thing";
130    g_assert_cmpstr (NULL, ==, midori_uri_parse_hostname (uri, NULL));
131    uri = "invalid-uri.like:thing";
132    g_assert_cmpstr (NULL, ==, midori_uri_parse_hostname (uri, NULL));
133}
134
135static void
136magic_uri_idn (void)
137{
138    typedef struct
139    {
140        const gchar* before;
141        const gchar* after;
142    } URIItem;
143
144    static const URIItem items[] = {
145     { "http://www.m�nchhausen.at", "http://www.xn--mnchhausen-9db.at" },
146     { "http://www.??????.com/", "http://www.xn--mgbndb8il.com/" },
147     { "??????.com", "xn--80aildf0a.com" },
148     { "http://??????.jp", "http://xn--1lq68wkwbj6ugkpigi.jp" },
149     { "https://????",  "https://xn--u9jthzcs263c" },
150    { "http://en.wikipedia.org/wiki/K�lsch_language", NULL },
151    { "file:///home/mark/fr�hst�ck", NULL },
152    { "about:version", NULL },
153     };
154    guint i;
155
156    for (i = 0; i < G_N_ELEMENTS (items); i++)
157    {
158        gchar* result = midori_uri_to_ascii (items[i].before);
159        const gchar* after = items[i].after ? items[i].after : items[i].before;
160        katze_assert_str_equal (items[i].before, result, after);
161        g_free (result);
162    }
163
164    test_input ("??????.com", "http://??????.com");
165    test_input ("sm K�chenzubeh�r", SM "K�chenzubeh�r");
166    test_input ("sm ??????", SM "??????");
167}
168
169static void
170magic_uri_search (void)
171{
172    test_input ("sm midori", SM "midori");
173    test_input ("d midori browser", "google.com" "midori%20browser");
174    test_input ("dd midori browser", "ddg.gg" "midori%20browser");
175    test_input ("sm cats dogs", SM "cats%20dogs");
176    test_input ("se cats dogs", SM "cats%20dogs");
177    test_input ("dict midori", NULL);
178    test_input ("cats", NULL);
179    test_input ("cats dogs", NULL);
180    test_input ("gtk 2.0", NULL);
181    test_input ("gtk2.0", NULL);
182    test_input ("pcre++", NULL);
183    test_input ("sm pcre++", SM "pcre%2B%2B");
184    test_input ("5580", NULL);
185    test_input ("sm 5580", SM "5580");
186    test_input ("midori0.1.0", NULL);
187    test_input ("midori 0.1.0", NULL);
188    test_input ("search:cats", NULL);
189    test_input ("search:twotoasts.de", NULL);
190    test_input ("g cache:127.0.0.1", NULL);
191    test_input ("g cache:127.0.0.1/foo", NULL);
192    test_input ("g cache:twotoasts.de/foo", NULL);
193    test_input ("sm cache:127.0.0.1", SM "cache:127.0.0.1");
194    test_input ("sm cache:127.0.0.1/foo", SM "cache:127.0.0.1/foo");
195    test_input ("sm cache:twotoasts.de/foo", SM "cache:twotoasts.de/foo");
196    test_input ("de.po verbose", NULL);
197    test_input ("verbose de.po", NULL);
198    test_input ("g de.po verbose", NULL);
199    test_input ("sm de.po verbose", SM "de.po%20verbose");
200    test_input ("sm warning: configure /dev/net: virtual",
201                SM "warning:%20configure%20/dev/net:%20virtual");
202    test_input ("g \"ISO 9001:2000 certified\"", NULL);
203    test_input ("g conference \"April 2, 7:00 am\"", NULL);
204    test_input ("max@mustermann.de", NULL);
205    test_input ("g max@mustermann.de", NULL);
206    test_input ("g inurl:http://twotoasts.de bug", NULL);
207    test_input ("sm", SM);
208    /* test_input ("LT_PREREQ(2.2)", NULL); */
209}
210
211static void
212magic_uri_pseudo (void)
213{
214    test_input ("javascript:alert(1)", "javascript:alert(1)");
215    test_input ("mailto:christian@twotoasts.de", "mailto:christian@twotoasts.de");
216    test_input ("data:text/html;charset=utf-8,<title>Test</title>Test",
217                "data:text/html;charset=utf-8,<title>Test</title>Test");
218}
219
220static void
221magic_uri_performance (void)
222{
223    gsize i;
224
225    g_test_timer_start ();
226
227    for (i = 0; i < 1000; i++)
228    {
229        magic_uri_uri ();
230        magic_uri_idn ();
231        magic_uri_search ();
232        magic_uri_pseudo ();
233    }
234
235    g_print ("\nTime needed for URI tests: %f ", g_test_timer_elapsed ());
236}
237
238static void
239magic_uri_fingerprint (void)
240{
241    const gchar* uri;
242    uri = "http://midori-0.4.1.tar.bz2#!md5!33dde203cd71ae2b1d2adcc7f5739f65";
243    g_assert_cmpint (midori_uri_get_fingerprint (uri, NULL, NULL), ==, G_CHECKSUM_MD5);
244    uri = "http://midori-0.4.1.tar.bz2#!md5!33DDE203CD71AE2B1D2ADCC7F5739F65";
245    g_assert_cmpint (midori_uri_get_fingerprint (uri, NULL, NULL), ==, G_CHECKSUM_MD5);
246    uri = "http://midori-0.4.1.tar.bz2#!sha1!0c499459b1049feabf86dce89f49020139a9efd9";
247    g_assert_cmpint (midori_uri_get_fingerprint (uri, NULL, NULL), ==, G_CHECKSUM_SHA1);
248    uri = "http://midori-0.4.1.tar.bz2#!sha256!123456";
249    g_assert_cmpint (midori_uri_get_fingerprint (uri, NULL, NULL), ==, G_MAXINT);
250    uri = "http://midori-0.4.1.tar.bz2#abcdefg";
251    g_assert_cmpint (midori_uri_get_fingerprint (uri, NULL, NULL), ==, G_MAXINT);
252    uri = "http://midori-0.4.1.tar.bz2";
253    g_assert_cmpint (midori_uri_get_fingerprint (uri, NULL, NULL), ==, G_MAXINT);
254}
255
256static void
257magic_uri_ip (void)
258{
259    g_assert (midori_uri_is_ip_address ("192.168.1.1"));
260    g_assert (midori_uri_is_ip_address ("192.168.1.1:1234"));
261    g_assert (!midori_uri_is_ip_address ("0.168.1.1"));
262    g_assert (midori_uri_is_ip_address ("user@192.168.1.1"));
263    g_assert (midori_uri_is_ip_address ("user:password@192.168.1.1"));
264    g_assert (midori_uri_is_ip_address ("2001:0db8:85a3:0000:0000:8a2e:0370:7334"));
265    g_assert (midori_uri_is_ip_address ("fe80:0:0:0:202:b3ff:fe1e:8329"));
266    g_assert (midori_uri_is_ip_address ("fe80::202:b3ff:fe1e:8329"));
267    g_assert (midori_uri_is_ip_address ("fe80::76e5:bff:fe04:38e0/64"));
268}
269
270static void
271magic_uri_format (void)
272{
273    typedef struct
274    {
275        const gchar* before;
276        const gchar* after;
277    } URIItem;
278
279    static const URIItem items[] = {
280     { "http://www.csszengarden.com", NULL },
281     { "http://live.gnome.org/GTK+/3.0/Tasks", NULL },
282     { "http://www.johannk�nig.com/index.php?ausw=home", NULL },
283     { "http://digilife.bz/wiki/index.php?Python%E3%81%AE%E9%96%8B%E7%99%BA%E6%89%8B%E9%A0%86",
284       "http://digilife.bz/wiki/index.php?Python?????" },
285     { "http://die-welt.net/~evgeni/LenovoBatteryLinux/", NULL },
286     { "http://wiki.c3sl.ufpr.br/multiseat/index.php/Xephyr_Solution", NULL },
287     { "http://?��?�??????????m��??????�??.de/char.jpg", NULL },
288     { "http://www.?????.org/", "http://www.gnome.org/" },
289     };
290    guint i;
291
292    for (i = 0; i < G_N_ELEMENTS (items); i++)
293    {
294        gchar* result = midori_uri_format_for_display (items[i].before);
295        const gchar* after = items[i].after ? items[i].after : items[i].before;
296        katze_assert_str_equal (items[i].before, result, after);
297        g_free (result);
298    }
299}
300
301static void
302magic_uri_prefetch (void)
303{
304    g_assert (!sokoke_prefetch_uri (NULL, NULL, NULL, NULL));
305    g_assert (sokoke_prefetch_uri (NULL, "http://google.com", NULL, NULL));
306    g_assert (sokoke_prefetch_uri (NULL, "http://google.com", NULL, NULL));
307    g_assert (sokoke_prefetch_uri (NULL, "http://googlecom", NULL, NULL));
308    g_assert (sokoke_prefetch_uri (NULL, "http://1kino.com", NULL, NULL));
309    g_assert (sokoke_prefetch_uri (NULL, "http://", NULL, NULL));
310    g_assert (!sokoke_prefetch_uri (NULL, "http:/", NULL, NULL));
311    g_assert (!sokoke_prefetch_uri (NULL, "http", NULL, NULL));
312    g_assert (!sokoke_prefetch_uri (NULL, "ftp://ftphost.org", NULL, NULL));
313    g_assert (!sokoke_prefetch_uri (NULL, "http://10.0.0.1", NULL, NULL));
314    g_assert (!sokoke_prefetch_uri (NULL, "about:blank", NULL, NULL));
315    g_assert (!sokoke_prefetch_uri (NULL, "javascript: alert()", NULL, NULL));
316}
317
318static void
319magic_uri_commands (void)
320{
321    typedef struct
322    {
323        const gchar* command;
324        gboolean quote;
325        const gchar* expected;
326    } CommandItem;
327
328    static const CommandItem commands[] = {
329        { "midori", TRUE, NULL },
330        { "/usr/bin/midori", TRUE, NULL },
331        { "C:/Program Files/Midori/bin/midori.exe", TRUE, NULL },
332        { "C:/Programme/Midori/bin/midori.exe", TRUE, NULL },
333    };
334    static const CommandItem arguments[] = {
335        { "-a 'http://lunduke.com/?p=3606'", FALSE, NULL },
336        { "about:private", FALSE, NULL },
337    };
338    guint i, j;
339
340    for (i = 0; i < G_N_ELEMENTS (commands); i++)
341        for (j = 0; j < G_N_ELEMENTS (arguments); j++)
342        {
343            gchar* input = g_strconcat (commands[i].command, " ", arguments[j].command, NULL);
344            gchar* ce = commands[i].expected ? commands[i].expected
345             : g_strconcat ("'", commands[i].command, "'", NULL);
346            gchar* ae = arguments[j].expected ? arguments[j].expected
347              : (arguments[j].quote ? g_strconcat ("'", arguments[j].command, "'", NULL)
348              : g_strdup (arguments[j].command));
349            gchar* expected = g_strconcat (ce, " ", ae, NULL);
350            gchar* result = sokoke_prepare_command (commands[i].command,
351                commands[i].quote, arguments[j].command, arguments[j].quote);
352            katze_assert_str_equal (input, result, expected);
353            g_free (input);
354            g_free (ce);
355            g_free (ae);
356            g_free (expected);
357            g_free (result);
358        }
359}
360
361int
362main (int    argc,
363      char** argv)
364{
365    g_test_init (&argc, &argv, NULL);
366    midori_app_setup (&argc, &argv, NULL, NULL);
367
368    g_test_add_func ("/magic-uri/uri", magic_uri_uri);
369    g_test_add_func ("/magic-uri/idn", magic_uri_idn);
370    g_test_add_func ("/magic-uri/search", magic_uri_search);
371    g_test_add_func ("/magic-uri/pseudo", magic_uri_pseudo);
372    g_test_add_func ("/magic-uri/performance", magic_uri_performance);
373    g_test_add_func ("/magic-uri/fingerprint", magic_uri_fingerprint);
374    g_test_add_func ("/magic-uri/ip", magic_uri_ip);
375    g_test_add_func ("/magic-uri/format", magic_uri_format);
376    g_test_add_func ("/magic-uri/prefetch", magic_uri_prefetch);
377    g_test_add_func ("/magic-uri/commands", magic_uri_commands);
378
379    return g_test_run ();
380}