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