/tests/magic-uri.c

https://bitbucket.org/edgimar/midori · C · 380 lines · 329 code · 36 blank · 15 comment · 12 complexity · 064b6b48d316a4ceba8904912f26f703 MD5 · raw file

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