url_parse_unittest.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <stddef.h>
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. #include "url/third_party/mozilla/url_parse.h"
  7. // Interesting IE file:isms...
  8. //
  9. // file:/foo/bar file:///foo/bar
  10. // The result here seems totally invalid!?!? This isn't UNC.
  11. //
  12. // file:/
  13. // file:// or any other number of slashes
  14. // IE6 doesn't do anything at all if you click on this link. No error:
  15. // nothing. IE6's history system seems to always color this link, so I'm
  16. // guessing that it maps internally to the empty URL.
  17. //
  18. // C:\ file:///C:/
  19. // / file:///C:/
  20. // /foo file:///C:/foo
  21. // Interestingly, IE treats "/" as an alias for "c:\", which makes sense,
  22. // but is weird to think about on Windows.
  23. //
  24. // file:foo/ file:foo/ (invalid?!?!?)
  25. // file:/foo/ file:///foo/ (invalid?!?!?)
  26. // file://foo/ file://foo/ (UNC to server "foo")
  27. // file:///foo/ file:///foo/ (invalid)
  28. // file:////foo/ file://foo/ (UNC to server "foo")
  29. // Any more than four slashes is also treated as UNC.
  30. //
  31. // file:C:/ file://C:/
  32. // file:/C:/ file://C:/
  33. // The number of slashes after "file:" don't matter if the thing following
  34. // it looks like an absolute drive path. Also, slashes and backslashes are
  35. // equally valid here.
  36. namespace url {
  37. namespace {
  38. // Used for regular URL parse cases.
  39. struct URLParseCase {
  40. const char* input;
  41. const char* scheme;
  42. const char* username;
  43. const char* password;
  44. const char* host;
  45. int port;
  46. const char* path;
  47. const char* query;
  48. const char* ref;
  49. };
  50. // Simpler version of URLParseCase for testing path URLs.
  51. struct PathURLParseCase {
  52. const char* input;
  53. const char* scheme;
  54. const char* path;
  55. };
  56. // Simpler version of URLParseCase for testing mailto URLs.
  57. struct MailtoURLParseCase {
  58. const char* input;
  59. const char* scheme;
  60. const char* path;
  61. const char* query;
  62. };
  63. // More complicated version of URLParseCase for testing filesystem URLs.
  64. struct FileSystemURLParseCase {
  65. const char* input;
  66. const char* inner_scheme;
  67. const char* inner_username;
  68. const char* inner_password;
  69. const char* inner_host;
  70. int inner_port;
  71. const char* inner_path;
  72. const char* path;
  73. const char* query;
  74. const char* ref;
  75. };
  76. bool ComponentMatches(const char* input,
  77. const char* reference,
  78. const Component& component) {
  79. // If the component is nonexistent (length == -1), it should begin at 0.
  80. EXPECT_TRUE(component.len >= 0 || component.len == -1);
  81. // Begin should be valid.
  82. EXPECT_LE(0, component.begin);
  83. // A NULL reference means the component should be nonexistent.
  84. if (!reference)
  85. return component.len == -1;
  86. if (component.len < 0)
  87. return false; // Reference is not NULL but we don't have anything
  88. if (strlen(reference) != static_cast<size_t>(component.len))
  89. return false; // Lengths don't match
  90. // Now check the actual characters.
  91. return strncmp(reference, &input[component.begin], component.len) == 0;
  92. }
  93. void ExpectInvalidComponent(const Component& component) {
  94. EXPECT_EQ(0, component.begin);
  95. EXPECT_EQ(-1, component.len);
  96. }
  97. // Parsed ----------------------------------------------------------------------
  98. TEST(URLParser, Length) {
  99. const char* length_cases[] = {
  100. // One with everything in it.
  101. "http://user:pass@host:99/foo?bar#baz",
  102. // One with nothing in it.
  103. "",
  104. // Working backwards, let's start taking off stuff from the full one.
  105. "http://user:pass@host:99/foo?bar#",
  106. "http://user:pass@host:99/foo?bar",
  107. "http://user:pass@host:99/foo?",
  108. "http://user:pass@host:99/foo",
  109. "http://user:pass@host:99/",
  110. "http://user:pass@host:99",
  111. "http://user:pass@host:",
  112. "http://user:pass@host",
  113. "http://host",
  114. "http://user@",
  115. "http:",
  116. };
  117. for (size_t i = 0; i < std::size(length_cases); i++) {
  118. int true_length = static_cast<int>(strlen(length_cases[i]));
  119. Parsed parsed;
  120. ParseStandardURL(length_cases[i], true_length, &parsed);
  121. EXPECT_EQ(true_length, parsed.Length());
  122. }
  123. }
  124. TEST(URLParser, CountCharactersBefore) {
  125. struct CountCase {
  126. const char* url;
  127. Parsed::ComponentType component;
  128. bool include_delimiter;
  129. int expected_count;
  130. } count_cases[] = {
  131. // Test each possibility in the case where all components are present.
  132. // 0 1 2
  133. // 0123456789012345678901
  134. {"http://u:p@h:8/p?q#r", Parsed::SCHEME, true, 0},
  135. {"http://u:p@h:8/p?q#r", Parsed::SCHEME, false, 0},
  136. {"http://u:p@h:8/p?q#r", Parsed::USERNAME, true, 7},
  137. {"http://u:p@h:8/p?q#r", Parsed::USERNAME, false, 7},
  138. {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, true, 9},
  139. {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, false, 9},
  140. {"http://u:p@h:8/p?q#r", Parsed::HOST, true, 11},
  141. {"http://u:p@h:8/p?q#r", Parsed::HOST, false, 11},
  142. {"http://u:p@h:8/p?q#r", Parsed::PORT, true, 12},
  143. {"http://u:p@h:8/p?q#r", Parsed::PORT, false, 13},
  144. {"http://u:p@h:8/p?q#r", Parsed::PATH, false, 14},
  145. {"http://u:p@h:8/p?q#r", Parsed::PATH, true, 14},
  146. {"http://u:p@h:8/p?q#r", Parsed::QUERY, true, 16},
  147. {"http://u:p@h:8/p?q#r", Parsed::QUERY, false, 17},
  148. {"http://u:p@h:8/p?q#r", Parsed::REF, true, 18},
  149. {"http://u:p@h:8/p?q#r", Parsed::REF, false, 19},
  150. // Now test when the requested component is missing.
  151. {"http://u:p@h:8/p?", Parsed::REF, true, 17},
  152. {"http://u:p@h:8/p?q", Parsed::REF, true, 18},
  153. {"http://u:p@h:8/p#r", Parsed::QUERY, true, 16},
  154. {"http://u:p@h:8#r", Parsed::PATH, true, 14},
  155. {"http://u:p@h/", Parsed::PORT, true, 12},
  156. {"http://u:p@/", Parsed::HOST, true, 11},
  157. // This case is a little weird. It will report that the password would
  158. // start where the host begins. This is arguably correct, although you
  159. // could also argue that it should start at the '@' sign. Doing it
  160. // starting with the '@' sign is actually harder, so we don't bother.
  161. {"http://u@h/", Parsed::PASSWORD, true, 9},
  162. {"http://h/", Parsed::USERNAME, true, 7},
  163. {"http:", Parsed::USERNAME, true, 5},
  164. {"", Parsed::SCHEME, true, 0},
  165. // Make sure a random component still works when there's nothing there.
  166. {"", Parsed::REF, true, 0},
  167. // File URLs are special with no host, so we test those.
  168. {"file:///c:/foo", Parsed::USERNAME, true, 7},
  169. {"file:///c:/foo", Parsed::PASSWORD, true, 7},
  170. {"file:///c:/foo", Parsed::HOST, true, 7},
  171. {"file:///c:/foo", Parsed::PATH, true, 7},
  172. };
  173. for (size_t i = 0; i < std::size(count_cases); i++) {
  174. int length = static_cast<int>(strlen(count_cases[i].url));
  175. // Simple test to distinguish file and standard URLs.
  176. Parsed parsed;
  177. if (length > 0 && count_cases[i].url[0] == 'f')
  178. ParseFileURL(count_cases[i].url, length, &parsed);
  179. else
  180. ParseStandardURL(count_cases[i].url, length, &parsed);
  181. int chars_before = parsed.CountCharactersBefore(
  182. count_cases[i].component, count_cases[i].include_delimiter);
  183. EXPECT_EQ(count_cases[i].expected_count, chars_before);
  184. }
  185. }
  186. // Standard --------------------------------------------------------------------
  187. // Input Scheme Usrname Passwd Host Port Path Query Ref
  188. // ------------------------------------ ------- ------- ---------- ------------ --- ---------- ------------ -----
  189. static URLParseCase cases[] = {
  190. // Regular URL with all the parts
  191. {"http://user:pass@foo:21/bar;par?b#c", "http", "user", "pass", "foo", 21, "/bar;par","b", "c"},
  192. // Known schemes should lean towards authority identification
  193. {"http:foo.com", "http", NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
  194. // Spaces!
  195. {"\t :foo.com \n", "", NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
  196. {" foo.com ", NULL, NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
  197. {"a:\t foo.com", "a", NULL, NULL, "\t foo.com", -1, NULL, NULL, NULL},
  198. {"http://f:21/ b ? d # e ", "http", NULL, NULL, "f", 21, "/ b ", " d ", " e"},
  199. // Invalid port numbers should be identified and turned into -2, empty port
  200. // numbers should be -1. Spaces aren't allowed in port numbers
  201. {"http://f:/c", "http", NULL, NULL, "f", -1, "/c", NULL, NULL},
  202. {"http://f:0/c", "http", NULL, NULL, "f", 0, "/c", NULL, NULL},
  203. {"http://f:00000000000000/c", "http", NULL, NULL, "f", 0, "/c", NULL, NULL},
  204. {"http://f:00000000000000000000080/c", "http", NULL, NULL, "f", 80, "/c", NULL, NULL},
  205. {"http://f:b/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
  206. {"http://f: /c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
  207. {"http://f:\n/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
  208. {"http://f:fifty-two/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
  209. {"http://f:999999/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
  210. {"http://f: 21 / b ? d # e ", "http", NULL, NULL, "f", -2, "/ b ", " d ", " e"},
  211. // Creative URLs missing key elements
  212. {"", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
  213. {" \t", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
  214. {":foo.com/", "", NULL, NULL, "foo.com", -1, "/", NULL, NULL},
  215. {":foo.com\\", "", NULL, NULL, "foo.com", -1, "\\", NULL, NULL},
  216. {":", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
  217. {":a", "", NULL, NULL, "a", -1, NULL, NULL, NULL},
  218. {":/", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
  219. {":\\", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
  220. {":#", "", NULL, NULL, NULL, -1, NULL, NULL, ""},
  221. {"#", NULL, NULL, NULL, NULL, -1, NULL, NULL, ""},
  222. {"#/", NULL, NULL, NULL, NULL, -1, NULL, NULL, "/"},
  223. {"#\\", NULL, NULL, NULL, NULL, -1, NULL, NULL, "\\"},
  224. {"#;?", NULL, NULL, NULL, NULL, -1, NULL, NULL, ";?"},
  225. {"?", NULL, NULL, NULL, NULL, -1, NULL, "", NULL},
  226. {"/", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
  227. {":23", "", NULL, NULL, "23", -1, NULL, NULL, NULL},
  228. {"/:23", "/", NULL, NULL, "23", -1, NULL, NULL, NULL},
  229. {"//", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
  230. {"::", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
  231. {"::23", "", NULL, NULL, NULL, 23, NULL, NULL, NULL},
  232. {"foo://", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
  233. // Username/passwords and things that look like them
  234. {"http://a:b@c:29/d", "http", "a", "b", "c", 29, "/d", NULL, NULL},
  235. {"http::@c:29", "http", "", "", "c", 29, NULL, NULL, NULL},
  236. // ... "]" in the password field isn't allowed, but we tolerate it here...
  237. {"http://&a:foo(b]c@d:2/", "http", "&a", "foo(b]c", "d", 2, "/", NULL, NULL},
  238. {"http://::@c@d:2", "http", "", ":@c", "d", 2, NULL, NULL, NULL},
  239. {"http://foo.com:b@d/", "http", "foo.com", "b", "d", -1, "/", NULL, NULL},
  240. {"http://foo.com/\\@", "http", NULL, NULL, "foo.com", -1, "/\\@", NULL, NULL},
  241. {"http:\\\\foo.com\\", "http", NULL, NULL, "foo.com", -1, "\\", NULL, NULL},
  242. {"http:\\\\a\\b:c\\d@foo.com\\", "http", NULL, NULL, "a", -1, "\\b:c\\d@foo.com\\", NULL, NULL},
  243. // Tolerate different numbers of slashes.
  244. {"foo:/", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
  245. {"foo:/bar.com/", "foo", NULL, NULL, "bar.com", -1, "/", NULL, NULL},
  246. {"foo://///////", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
  247. {"foo://///////bar.com/", "foo", NULL, NULL, "bar.com", -1, "/", NULL, NULL},
  248. {"foo:////://///", "foo", NULL, NULL, NULL, -1, "/////", NULL, NULL},
  249. // Raw file paths on Windows aren't handled by the parser.
  250. {"c:/foo", "c", NULL, NULL, "foo", -1, NULL, NULL, NULL},
  251. {"//foo/bar", NULL, NULL, NULL, "foo", -1, "/bar", NULL, NULL},
  252. // Use the first question mark for the query and the ref.
  253. {"http://foo/path;a??e#f#g", "http", NULL, NULL, "foo", -1, "/path;a", "?e", "f#g"},
  254. {"http://foo/abcd?efgh?ijkl", "http", NULL, NULL, "foo", -1, "/abcd", "efgh?ijkl", NULL},
  255. {"http://foo/abcd#foo?bar", "http", NULL, NULL, "foo", -1, "/abcd", NULL, "foo?bar"},
  256. // IPv6, check also interesting uses of colons.
  257. {"[61:24:74]:98", "[61", NULL, NULL, "24:74]", 98, NULL, NULL, NULL},
  258. {"http://[61:27]:98", "http", NULL, NULL, "[61:27]", 98, NULL, NULL, NULL},
  259. {"http:[61:27]/:foo", "http", NULL, NULL, "[61:27]", -1, "/:foo", NULL, NULL},
  260. {"http://[1::2]:3:4", "http", NULL, NULL, "[1::2]:3", 4, NULL, NULL, NULL},
  261. // Partially-complete IPv6 literals, and related cases.
  262. {"http://2001::1", "http", NULL, NULL, "2001:", 1, NULL, NULL, NULL},
  263. {"http://[2001::1", "http", NULL, NULL, "[2001::1", -1, NULL, NULL, NULL},
  264. {"http://2001::1]", "http", NULL, NULL, "2001::1]", -1, NULL, NULL, NULL},
  265. {"http://2001::1]:80", "http", NULL, NULL, "2001::1]", 80, NULL, NULL, NULL},
  266. {"http://[2001::1]", "http", NULL, NULL, "[2001::1]", -1, NULL, NULL, NULL},
  267. {"http://[2001::1]:80", "http", NULL, NULL, "[2001::1]", 80, NULL, NULL, NULL},
  268. {"http://[[::]]", "http", NULL, NULL, "[[::]]", -1, NULL, NULL, NULL},
  269. };
  270. TEST(URLParser, Standard) {
  271. // Declared outside for loop to try to catch cases in init() where we forget
  272. // to reset something that is reset by the constructor.
  273. Parsed parsed;
  274. for (size_t i = 0; i < std::size(cases); i++) {
  275. const char* url = cases[i].input;
  276. ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
  277. int port = ParsePort(url, parsed.port);
  278. EXPECT_TRUE(ComponentMatches(url, cases[i].scheme, parsed.scheme));
  279. EXPECT_TRUE(ComponentMatches(url, cases[i].username, parsed.username));
  280. EXPECT_TRUE(ComponentMatches(url, cases[i].password, parsed.password));
  281. EXPECT_TRUE(ComponentMatches(url, cases[i].host, parsed.host));
  282. EXPECT_EQ(cases[i].port, port);
  283. EXPECT_TRUE(ComponentMatches(url, cases[i].path, parsed.path));
  284. EXPECT_TRUE(ComponentMatches(url, cases[i].query, parsed.query));
  285. EXPECT_TRUE(ComponentMatches(url, cases[i].ref, parsed.ref));
  286. }
  287. }
  288. // PathURL --------------------------------------------------------------------
  289. // Various incarnations of path URLs.
  290. static PathURLParseCase path_cases[] = {
  291. {"", NULL, NULL},
  292. {":", "", NULL},
  293. {":/", "", "/"},
  294. {"/", NULL, "/"},
  295. {" This is \\interesting// \t", NULL, "This is \\interesting// \t"},
  296. {"about:", "about", NULL},
  297. {"about:blank", "about", "blank"},
  298. {" about: blank ", "about", " blank "},
  299. {"javascript :alert(\"He:/l\\l#o?foo\"); ", "javascript ", "alert(\"He:/l\\l#o?foo\"); "},
  300. };
  301. TEST(URLParser, PathURL) {
  302. // Declared outside for loop to try to catch cases in init() where we forget
  303. // to reset something that is reset by the constructor.
  304. Parsed parsed;
  305. for (size_t i = 0; i < std::size(path_cases); i++) {
  306. const char* url = path_cases[i].input;
  307. ParsePathURL(url, static_cast<int>(strlen(url)), false, &parsed);
  308. EXPECT_TRUE(ComponentMatches(url, path_cases[i].scheme, parsed.scheme))
  309. << i;
  310. EXPECT_TRUE(ComponentMatches(url, path_cases[i].path, parsed.GetContent()))
  311. << i;
  312. // The remaining components are never used for path URLs.
  313. ExpectInvalidComponent(parsed.username);
  314. ExpectInvalidComponent(parsed.password);
  315. ExpectInvalidComponent(parsed.host);
  316. ExpectInvalidComponent(parsed.port);
  317. }
  318. }
  319. // Various incarnations of file URLs.
  320. static URLParseCase file_cases[] = {
  321. #ifdef WIN32
  322. {"file:server", "file", NULL, NULL, "server", -1, NULL, NULL, NULL},
  323. {" file: server \t", "file", NULL, NULL, " server",-1, NULL, NULL, NULL},
  324. {"FiLe:c|", "FiLe", NULL, NULL, NULL, -1, "c|", NULL, NULL},
  325. {"FILE:/\\\\/server/file", "FILE", NULL, NULL, "server", -1, "/file", NULL, NULL},
  326. {"file://server/", "file", NULL, NULL, "server", -1, "/", NULL, NULL},
  327. {"file://localhost/c:/", "file", NULL, NULL, "localhost", -1, "/c:/", NULL, NULL},
  328. {"file://127.0.0.1/c|\\", "file", NULL, NULL, "127.0.0.1", -1, "/c|\\", NULL, NULL},
  329. {"file:/", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
  330. {"file:", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
  331. // If there is a Windows drive letter, treat any number of slashes as the
  332. // path part.
  333. {"file:c:\\fo\\b", "file", NULL, NULL, NULL, -1, "c:\\fo\\b", NULL, NULL},
  334. {"file:/c:\\foo/bar", "file", NULL, NULL, NULL, -1, "/c:\\foo/bar",NULL, NULL},
  335. {"file://c:/f\\b", "file", NULL, NULL, NULL, -1, "/c:/f\\b", NULL, NULL},
  336. {"file:///C:/foo", "file", NULL, NULL, NULL, -1, "/C:/foo", NULL, NULL},
  337. {"file://///\\/\\/c:\\f\\b", "file", NULL, NULL, NULL, -1, "/c:\\f\\b", NULL, NULL},
  338. // If there is not a drive letter, we should treat is as UNC EXCEPT for
  339. // three slashes, which we treat as a Unix style path.
  340. {"file:server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
  341. {"file:/server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
  342. {"file://server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
  343. {"file:///server/file", "file", NULL, NULL, NULL, -1, "/server/file",NULL, NULL},
  344. {"file://\\server/file", "file", NULL, NULL, NULL, -1, "\\server/file",NULL, NULL},
  345. {"file:////server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
  346. // Queries and refs are valid for file URLs as well.
  347. {"file:///C:/foo.html?#", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "", ""},
  348. {"file:///C:/foo.html?query=yes#ref", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "query=yes", "ref"},
  349. #else // WIN32
  350. // No slashes.
  351. {"file:", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
  352. {"file:path", "file", NULL, NULL, NULL, -1, "path", NULL, NULL},
  353. {"file:path/", "file", NULL, NULL, NULL, -1, "path/", NULL, NULL},
  354. {"file:path/f.txt", "file", NULL, NULL, NULL, -1, "path/f.txt", NULL, NULL},
  355. // One slash.
  356. {"file:/", "file", NULL, NULL, NULL, -1, "/", NULL, NULL},
  357. {"file:/path", "file", NULL, NULL, NULL, -1, "/path", NULL, NULL},
  358. {"file:/path/", "file", NULL, NULL, NULL, -1, "/path/", NULL, NULL},
  359. {"file:/path/f.txt", "file", NULL, NULL, NULL, -1, "/path/f.txt", NULL, NULL},
  360. // Two slashes.
  361. {"file://", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
  362. {"file://server", "file", NULL, NULL, "server", -1, NULL, NULL, NULL},
  363. {"file://server/", "file", NULL, NULL, "server", -1, "/", NULL, NULL},
  364. {"file://server/f.txt", "file", NULL, NULL, "server", -1, "/f.txt", NULL, NULL},
  365. // Three slashes.
  366. {"file:///", "file", NULL, NULL, NULL, -1, "/", NULL, NULL},
  367. {"file:///path", "file", NULL, NULL, NULL, -1, "/path", NULL, NULL},
  368. {"file:///path/", "file", NULL, NULL, NULL, -1, "/path/", NULL, NULL},
  369. {"file:///path/f.txt", "file", NULL, NULL, NULL, -1, "/path/f.txt", NULL, NULL},
  370. // More than three slashes.
  371. {"file:////", "file", NULL, NULL, NULL, -1, "/", NULL, NULL},
  372. {"file:////path", "file", NULL, NULL, NULL, -1, "/path", NULL, NULL},
  373. {"file:////path/", "file", NULL, NULL, NULL, -1, "/path/", NULL, NULL},
  374. {"file:////path/f.txt", "file", NULL, NULL, NULL, -1, "/path/f.txt", NULL, NULL},
  375. // Schemeless URLs
  376. {"path/f.txt", NULL, NULL, NULL, NULL, -1, "path/f.txt", NULL, NULL},
  377. {"path:80/f.txt", "path", NULL, NULL, NULL, -1, "80/f.txt", NULL, NULL},
  378. {"path/f.txt:80", "path/f.txt",NULL, NULL, NULL, -1, "80", NULL, NULL}, // Wrong.
  379. {"/path/f.txt", NULL, NULL, NULL, NULL, -1, "/path/f.txt", NULL, NULL},
  380. {"/path:80/f.txt", NULL, NULL, NULL, NULL, -1, "/path:80/f.txt",NULL, NULL},
  381. {"/path/f.txt:80", NULL, NULL, NULL, NULL, -1, "/path/f.txt:80",NULL, NULL},
  382. {"//server/f.txt", NULL, NULL, NULL, "server", -1, "/f.txt", NULL, NULL},
  383. {"//server:80/f.txt", NULL, NULL, NULL, "server:80",-1, "/f.txt", NULL, NULL},
  384. {"//server/f.txt:80", NULL, NULL, NULL, "server", -1, "/f.txt:80", NULL, NULL},
  385. {"///path/f.txt", NULL, NULL, NULL, NULL, -1, "/path/f.txt", NULL, NULL},
  386. {"///path:80/f.txt", NULL, NULL, NULL, NULL, -1, "/path:80/f.txt",NULL, NULL},
  387. {"///path/f.txt:80", NULL, NULL, NULL, NULL, -1, "/path/f.txt:80",NULL, NULL},
  388. {"////path/f.txt", NULL, NULL, NULL, NULL, -1, "/path/f.txt", NULL, NULL},
  389. {"////path:80/f.txt", NULL, NULL, NULL, NULL, -1, "/path:80/f.txt",NULL, NULL},
  390. {"////path/f.txt:80", NULL, NULL, NULL, NULL, -1, "/path/f.txt:80",NULL, NULL},
  391. // Queries and refs are valid for file URLs as well.
  392. {"file:///foo.html?#", "file", NULL, NULL, NULL, -1, "/foo.html", "", ""},
  393. {"file:///foo.html?q=y#ref", "file", NULL, NULL, NULL, -1, "/foo.html", "q=y", "ref"},
  394. #endif // WIN32
  395. };
  396. TEST(URLParser, ParseFileURL) {
  397. // Declared outside for loop to try to catch cases in init() where we forget
  398. // to reset something that is reset by the construtor.
  399. Parsed parsed;
  400. for (size_t i = 0; i < std::size(file_cases); i++) {
  401. const char* url = file_cases[i].input;
  402. ParseFileURL(url, static_cast<int>(strlen(url)), &parsed);
  403. int port = ParsePort(url, parsed.port);
  404. EXPECT_TRUE(ComponentMatches(url, file_cases[i].scheme, parsed.scheme))
  405. << " for case #" << i << " [" << url << "] "
  406. << parsed.scheme.begin << ", " << parsed.scheme.len;
  407. EXPECT_TRUE(ComponentMatches(url, file_cases[i].username, parsed.username))
  408. << " for case #" << i << " [" << url << "] "
  409. << parsed.username.begin << ", " << parsed.username.len;
  410. EXPECT_TRUE(ComponentMatches(url, file_cases[i].password, parsed.password))
  411. << " for case #" << i << " [" << url << "] "
  412. << parsed.password.begin << ", " << parsed.password.len;
  413. EXPECT_TRUE(ComponentMatches(url, file_cases[i].host, parsed.host))
  414. << " for case #" << i << " [" << url << "] "
  415. << parsed.host.begin << ", " << parsed.host.len;
  416. EXPECT_EQ(file_cases[i].port, port)
  417. << " for case #" << i << " [ " << url << "] " << port;
  418. EXPECT_TRUE(ComponentMatches(url, file_cases[i].path, parsed.path))
  419. << " for case #" << i << " [" << url << "] "
  420. << parsed.path.begin << ", " << parsed.path.len;
  421. EXPECT_TRUE(ComponentMatches(url, file_cases[i].query, parsed.query))
  422. << " for case #" << i << " [" << url << "] "
  423. << parsed.query.begin << ", " << parsed.query.len;
  424. EXPECT_TRUE(ComponentMatches(url, file_cases[i].ref, parsed.ref))
  425. << " for case #" << i << " [ "<< url << "] "
  426. << parsed.query.begin << ", " << parsed.scheme.len;
  427. }
  428. }
  429. TEST(URLParser, ExtractFileName) {
  430. struct FileCase {
  431. const char* input;
  432. const char* expected;
  433. } extract_cases[] = {
  434. {"http://www.google.com", nullptr},
  435. {"http://www.google.com/", ""},
  436. {"http://www.google.com/search", "search"},
  437. {"http://www.google.com/search/", ""},
  438. {"http://www.google.com/foo/bar.html?baz=22", "bar.html"},
  439. {"http://www.google.com/foo/bar.html#ref", "bar.html"},
  440. {"http://www.google.com/search/;param", ""},
  441. {"http://www.google.com/foo/bar.html;param#ref", "bar.html"},
  442. {"http://www.google.com/foo/bar.html;foo;param#ref", "bar.html"},
  443. {"http://www.google.com/foo/bar.html?query#ref", "bar.html"},
  444. {"http://www.google.com/foo;/bar.html", "bar.html"},
  445. {"http://www.google.com/foo;/", ""},
  446. {"http://www.google.com/foo;", "foo"},
  447. {"http://www.google.com/;", ""},
  448. {"http://www.google.com/foo;bar;html", "foo"},
  449. };
  450. for (size_t i = 0; i < std::size(extract_cases); i++) {
  451. const char* url = extract_cases[i].input;
  452. int len = static_cast<int>(strlen(url));
  453. Parsed parsed;
  454. ParseStandardURL(url, len, &parsed);
  455. Component file_name;
  456. ExtractFileName(url, parsed.path, &file_name);
  457. EXPECT_TRUE(ComponentMatches(url, extract_cases[i].expected, file_name));
  458. }
  459. }
  460. // Returns true if the parameter with index |parameter| in the given URL's
  461. // query string. The expected key can be NULL to indicate no such key index
  462. // should exist. The parameter number is 1-based.
  463. static bool NthParameterIs(const char* url,
  464. int parameter,
  465. const char* expected_key,
  466. const char* expected_value) {
  467. Parsed parsed;
  468. ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
  469. Component query = parsed.query;
  470. for (int i = 1; i <= parameter; i++) {
  471. Component key, value;
  472. if (!ExtractQueryKeyValue(url, &query, &key, &value)) {
  473. if (parameter >= i && !expected_key)
  474. return true; // Expected nonexistent key, got one.
  475. return false; // Not enough keys.
  476. }
  477. if (i == parameter) {
  478. if (!expected_key)
  479. return false;
  480. if (strncmp(&url[key.begin], expected_key, key.len) != 0)
  481. return false;
  482. if (strncmp(&url[value.begin], expected_value, value.len) != 0)
  483. return false;
  484. return true;
  485. }
  486. }
  487. return expected_key == NULL; // We didn't find that many parameters.
  488. }
  489. TEST(URLParser, ExtractQueryKeyValue) {
  490. EXPECT_TRUE(NthParameterIs("http://www.google.com", 1, NULL, NULL));
  491. // Basic case.
  492. char a[] = "http://www.google.com?arg1=1&arg2=2&bar";
  493. EXPECT_TRUE(NthParameterIs(a, 1, "arg1", "1"));
  494. EXPECT_TRUE(NthParameterIs(a, 2, "arg2", "2"));
  495. EXPECT_TRUE(NthParameterIs(a, 3, "bar", ""));
  496. EXPECT_TRUE(NthParameterIs(a, 4, NULL, NULL));
  497. // Empty param at the end.
  498. char b[] = "http://www.google.com?foo=bar&";
  499. EXPECT_TRUE(NthParameterIs(b, 1, "foo", "bar"));
  500. EXPECT_TRUE(NthParameterIs(b, 2, NULL, NULL));
  501. // Empty param at the beginning.
  502. char c[] = "http://www.google.com?&foo=bar";
  503. EXPECT_TRUE(NthParameterIs(c, 1, "", ""));
  504. EXPECT_TRUE(NthParameterIs(c, 2, "foo", "bar"));
  505. EXPECT_TRUE(NthParameterIs(c, 3, NULL, NULL));
  506. // Empty key with value.
  507. char d[] = "http://www.google.com?=foo";
  508. EXPECT_TRUE(NthParameterIs(d, 1, "", "foo"));
  509. EXPECT_TRUE(NthParameterIs(d, 2, NULL, NULL));
  510. // Empty value with key.
  511. char e[] = "http://www.google.com?foo=";
  512. EXPECT_TRUE(NthParameterIs(e, 1, "foo", ""));
  513. EXPECT_TRUE(NthParameterIs(e, 2, NULL, NULL));
  514. // Empty key and values.
  515. char f[] = "http://www.google.com?&&==&=";
  516. EXPECT_TRUE(NthParameterIs(f, 1, "", ""));
  517. EXPECT_TRUE(NthParameterIs(f, 2, "", ""));
  518. EXPECT_TRUE(NthParameterIs(f, 3, "", "="));
  519. EXPECT_TRUE(NthParameterIs(f, 4, "", ""));
  520. EXPECT_TRUE(NthParameterIs(f, 5, NULL, NULL));
  521. }
  522. // MailtoURL --------------------------------------------------------------------
  523. static MailtoURLParseCase mailto_cases[] = {
  524. //|input |scheme |path |query
  525. {"mailto:foo@gmail.com", "mailto", "foo@gmail.com", NULL},
  526. {" mailto: to \t", "mailto", " to", NULL},
  527. {"mailto:addr1%2C%20addr2 ", "mailto", "addr1%2C%20addr2", NULL},
  528. {"Mailto:addr1, addr2 ", "Mailto", "addr1, addr2", NULL},
  529. {"mailto:addr1:addr2 ", "mailto", "addr1:addr2", NULL},
  530. {"mailto:?to=addr1,addr2", "mailto", NULL, "to=addr1,addr2"},
  531. {"mailto:?to=addr1%2C%20addr2", "mailto", NULL, "to=addr1%2C%20addr2"},
  532. {"mailto:addr1?to=addr2", "mailto", "addr1", "to=addr2"},
  533. {"mailto:?body=#foobar#", "mailto", NULL, "body=#foobar#",},
  534. {"mailto:#?body=#foobar#", "mailto", "#", "body=#foobar#"},
  535. };
  536. TEST(URLParser, MailtoUrl) {
  537. // Declared outside for loop to try to catch cases in init() where we forget
  538. // to reset something that is reset by the constructor.
  539. Parsed parsed;
  540. for (size_t i = 0; i < std::size(mailto_cases); ++i) {
  541. const char* url = mailto_cases[i].input;
  542. ParseMailtoURL(url, static_cast<int>(strlen(url)), &parsed);
  543. int port = ParsePort(url, parsed.port);
  544. EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].scheme, parsed.scheme));
  545. EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].path, parsed.path));
  546. EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].query, parsed.query));
  547. EXPECT_EQ(PORT_UNSPECIFIED, port);
  548. // The remaining components are never used for mailto URLs.
  549. ExpectInvalidComponent(parsed.username);
  550. ExpectInvalidComponent(parsed.password);
  551. ExpectInvalidComponent(parsed.port);
  552. ExpectInvalidComponent(parsed.ref);
  553. }
  554. }
  555. // Various incarnations of filesystem URLs.
  556. static FileSystemURLParseCase filesystem_cases[] = {
  557. // Regular URL with all the parts
  558. {"filesystem:http://user:pass@foo:21/temporary/bar;par?b#c", "http", "user", "pass", "foo", 21, "/temporary", "/bar;par", "b", "c"},
  559. {"filesystem:https://foo/persistent/bar;par/", "https", NULL, NULL, "foo", -1, "/persistent", "/bar;par/", NULL, NULL},
  560. {"filesystem:file:///persistent/bar;par/", "file", NULL, NULL, NULL, -1, "/persistent", "/bar;par/", NULL, NULL},
  561. {"filesystem:file:///persistent/bar;par/?query#ref", "file", NULL, NULL, NULL, -1, "/persistent", "/bar;par/", "query", "ref"},
  562. {"filesystem:file:///persistent", "file", NULL, NULL, NULL, -1, "/persistent", "", NULL, NULL},
  563. };
  564. TEST(URLParser, FileSystemURL) {
  565. // Declared outside for loop to try to catch cases in init() where we forget
  566. // to reset something that is reset by the constructor.
  567. Parsed parsed;
  568. for (size_t i = 0; i < std::size(filesystem_cases); i++) {
  569. const FileSystemURLParseCase* parsecase = &filesystem_cases[i];
  570. const char* url = parsecase->input;
  571. ParseFileSystemURL(url, static_cast<int>(strlen(url)), &parsed);
  572. EXPECT_TRUE(ComponentMatches(url, "filesystem", parsed.scheme));
  573. EXPECT_EQ(!parsecase->inner_scheme, !parsed.inner_parsed());
  574. // Only check the inner_parsed if there is one.
  575. if (parsed.inner_parsed()) {
  576. EXPECT_TRUE(ComponentMatches(url, parsecase->inner_scheme,
  577. parsed.inner_parsed()->scheme));
  578. EXPECT_TRUE(ComponentMatches(url, parsecase->inner_username,
  579. parsed.inner_parsed()->username));
  580. EXPECT_TRUE(ComponentMatches(url, parsecase->inner_password,
  581. parsed.inner_parsed()->password));
  582. EXPECT_TRUE(ComponentMatches(url, parsecase->inner_host,
  583. parsed.inner_parsed()->host));
  584. int port = ParsePort(url, parsed.inner_parsed()->port);
  585. EXPECT_EQ(parsecase->inner_port, port);
  586. // The remaining components are never used for filesystem URLs.
  587. ExpectInvalidComponent(parsed.inner_parsed()->query);
  588. ExpectInvalidComponent(parsed.inner_parsed()->ref);
  589. }
  590. EXPECT_TRUE(ComponentMatches(url, parsecase->path, parsed.path));
  591. EXPECT_TRUE(ComponentMatches(url, parsecase->query, parsed.query));
  592. EXPECT_TRUE(ComponentMatches(url, parsecase->ref, parsed.ref));
  593. // The remaining components are never used for filesystem URLs.
  594. ExpectInvalidComponent(parsed.username);
  595. ExpectInvalidComponent(parsed.password);
  596. ExpectInvalidComponent(parsed.host);
  597. ExpectInvalidComponent(parsed.port);
  598. }
  599. }
  600. } // namespace
  601. } // namespace url