1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "url/url_util.h"
11 #include "build/build_config.h"
12 #include "testing/gtest/include/gtest/gtest-message.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/abseil-cpp/absl/types/optional.h"
15 #include "url/third_party/mozilla/url_parse.h"
16 #include "url/url_canon.h"
17 #include "url/url_canon_stdstring.h"
18 #include "url/url_test_utils.h"
22 class URLUtilTest : public testing::Test {
24 URLUtilTest() = default;
26 URLUtilTest(const URLUtilTest&) = delete;
27 URLUtilTest& operator=(const URLUtilTest&) = delete;
29 ~URLUtilTest() override = default;
32 ScopedSchemeRegistryForTests scoped_registry_;
35 TEST_F(URLUtilTest, FindAndCompareScheme) {
36 Component found_scheme;
38 // Simple case where the scheme is found and matches.
39 const char kStr1[] = "http://www.com/";
40 EXPECT_TRUE(FindAndCompareScheme(kStr1, static_cast<int>(strlen(kStr1)),
42 EXPECT_TRUE(FindAndCompareScheme(
43 kStr1, static_cast<int>(strlen(kStr1)), "http", &found_scheme));
44 EXPECT_TRUE(found_scheme == Component(0, 4));
46 // A case where the scheme is found and doesn't match.
47 EXPECT_FALSE(FindAndCompareScheme(
48 kStr1, static_cast<int>(strlen(kStr1)), "https", &found_scheme));
49 EXPECT_TRUE(found_scheme == Component(0, 4));
51 // A case where there is no scheme.
52 const char kStr2[] = "httpfoobar";
53 EXPECT_FALSE(FindAndCompareScheme(
54 kStr2, static_cast<int>(strlen(kStr2)), "http", &found_scheme));
55 EXPECT_TRUE(found_scheme == Component());
57 // When there is an empty scheme, it should match the empty scheme.
58 const char kStr3[] = ":foo.com/";
59 EXPECT_TRUE(FindAndCompareScheme(
60 kStr3, static_cast<int>(strlen(kStr3)), "", &found_scheme));
61 EXPECT_TRUE(found_scheme == Component(0, 0));
63 // But when there is no scheme, it should fail.
64 EXPECT_FALSE(FindAndCompareScheme("", 0, "", &found_scheme));
65 EXPECT_TRUE(found_scheme == Component());
67 // When there is a whitespace char in scheme, it should canonicalize the URL
69 const char whtspc_str[] = " \r\n\tjav\ra\nscri\tpt:alert(1)";
70 EXPECT_TRUE(FindAndCompareScheme(whtspc_str,
71 static_cast<int>(strlen(whtspc_str)),
72 "javascript", &found_scheme));
73 EXPECT_TRUE(found_scheme == Component(1, 10));
75 // Control characters should be stripped out on the ends, and kept in the
77 const char ctrl_str[] = "\02jav\02scr\03ipt:alert(1)";
78 EXPECT_FALSE(FindAndCompareScheme(ctrl_str,
79 static_cast<int>(strlen(ctrl_str)),
80 "javascript", &found_scheme));
81 EXPECT_TRUE(found_scheme == Component(1, 11));
84 TEST_F(URLUtilTest, IsStandard) {
85 const char kHTTPScheme[] = "http";
86 EXPECT_TRUE(IsStandard(kHTTPScheme, Component(0, strlen(kHTTPScheme))));
88 const char kFooScheme[] = "foo";
89 EXPECT_FALSE(IsStandard(kFooScheme, Component(0, strlen(kFooScheme))));
92 TEST_F(URLUtilTest, IsReferrerScheme) {
93 const char kHTTPScheme[] = "http";
94 EXPECT_TRUE(IsReferrerScheme(kHTTPScheme, Component(0, strlen(kHTTPScheme))));
96 const char kFooScheme[] = "foo";
97 EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
100 TEST_F(URLUtilTest, AddReferrerScheme) {
101 static const char kFooScheme[] = "foo";
102 EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
104 url::ScopedSchemeRegistryForTests scoped_registry;
105 AddReferrerScheme(kFooScheme, url::SCHEME_WITH_HOST);
106 EXPECT_TRUE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
109 TEST_F(URLUtilTest, ShutdownCleansUpSchemes) {
110 static const char kFooScheme[] = "foo";
111 EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
114 url::ScopedSchemeRegistryForTests scoped_registry;
115 AddReferrerScheme(kFooScheme, url::SCHEME_WITH_HOST);
116 EXPECT_TRUE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
119 EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
122 TEST_F(URLUtilTest, GetStandardSchemeType) {
123 url::SchemeType scheme_type;
125 const char kHTTPScheme[] = "http";
126 scheme_type = url::SCHEME_WITHOUT_AUTHORITY;
127 EXPECT_TRUE(GetStandardSchemeType(kHTTPScheme,
128 Component(0, strlen(kHTTPScheme)),
130 EXPECT_EQ(url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, scheme_type);
132 const char kFilesystemScheme[] = "filesystem";
133 scheme_type = url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
134 EXPECT_TRUE(GetStandardSchemeType(kFilesystemScheme,
135 Component(0, strlen(kFilesystemScheme)),
137 EXPECT_EQ(url::SCHEME_WITHOUT_AUTHORITY, scheme_type);
139 const char kFooScheme[] = "foo";
140 scheme_type = url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
141 EXPECT_FALSE(GetStandardSchemeType(kFooScheme,
142 Component(0, strlen(kFooScheme)),
146 TEST_F(URLUtilTest, GetStandardSchemes) {
147 std::vector<std::string> expected = {
148 kHttpsScheme, kHttpScheme, kFileScheme, kFtpScheme,
149 kWssScheme, kWsScheme, kFileSystemScheme, "foo",
151 AddStandardScheme("foo", url::SCHEME_WITHOUT_AUTHORITY);
152 EXPECT_EQ(expected, GetStandardSchemes());
155 TEST_F(URLUtilTest, ReplaceComponents) {
157 RawCanonOutputT<char> output;
160 // Check that the following calls do not cause crash
161 Replacements<char> replacements;
162 replacements.SetRef("test", Component(0, 4));
163 ReplaceComponents(nullptr, 0, parsed, replacements, nullptr, &output,
165 ReplaceComponents("", 0, parsed, replacements, nullptr, &output, &new_parsed);
166 replacements.ClearRef();
167 replacements.SetHost("test", Component(0, 4));
168 ReplaceComponents(nullptr, 0, parsed, replacements, nullptr, &output,
170 ReplaceComponents("", 0, parsed, replacements, nullptr, &output, &new_parsed);
172 replacements.ClearHost();
173 ReplaceComponents(nullptr, 0, parsed, replacements, nullptr, &output,
175 ReplaceComponents("", 0, parsed, replacements, nullptr, &output, &new_parsed);
176 ReplaceComponents(nullptr, 0, parsed, replacements, nullptr, &output,
178 ReplaceComponents("", 0, parsed, replacements, nullptr, &output, &new_parsed);
181 static std::string CheckReplaceScheme(const char* base_url,
182 const char* scheme) {
183 // Make sure the input is canonicalized.
184 RawCanonOutput<32> original;
185 Parsed original_parsed;
186 Canonicalize(base_url, strlen(base_url), true, nullptr, &original,
189 Replacements<char> replacements;
190 replacements.SetScheme(scheme, Component(0, strlen(scheme)));
192 std::string output_string;
193 StdStringCanonOutput output(&output_string);
194 Parsed output_parsed;
195 ReplaceComponents(original.data(), original.length(), original_parsed,
196 replacements, nullptr, &output, &output_parsed);
199 return output_string;
202 TEST_F(URLUtilTest, ReplaceScheme) {
203 EXPECT_EQ("https://google.com/",
204 CheckReplaceScheme("http://google.com/", "https"));
205 EXPECT_EQ("file://google.com/",
206 CheckReplaceScheme("http://google.com/", "file"));
207 EXPECT_EQ("http://home/Build",
208 CheckReplaceScheme("file:///Home/Build", "http"));
209 EXPECT_EQ("javascript:foo",
210 CheckReplaceScheme("about:foo", "javascript"));
211 EXPECT_EQ("://google.com/",
212 CheckReplaceScheme("http://google.com/", ""));
213 EXPECT_EQ("http://google.com/",
214 CheckReplaceScheme("about:google.com", "http"));
215 EXPECT_EQ("http:", CheckReplaceScheme("", "http"));
218 // Magic Windows drive letter behavior when converting to a file URL.
219 EXPECT_EQ("file:///E:/foo/",
220 CheckReplaceScheme("http://localhost/e:foo/", "file"));
223 // This will probably change to "about://google.com/" when we fix
224 // http://crbug.com/160 which should also be an acceptable result.
225 EXPECT_EQ("about://google.com/",
226 CheckReplaceScheme("http://google.com/", "about"));
228 EXPECT_EQ("http://example.com/%20hello%20#%20world",
229 CheckReplaceScheme("myscheme:example.com/ hello # world ", "http"));
232 TEST_F(URLUtilTest, DecodeURLEscapeSequences) {
237 {"hello, world", "hello, world"},
238 {"%01%02%03%04%05%06%07%08%09%0a%0B%0C%0D%0e%0f/",
239 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0B\x0C\x0D\x0e\x0f/"},
240 {"%10%11%12%13%14%15%16%17%18%19%1a%1B%1C%1D%1e%1f/",
241 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1B\x1C\x1D\x1e\x1f/"},
242 {"%20%21%22%23%24%25%26%27%28%29%2a%2B%2C%2D%2e%2f/",
243 " !\"#$%&'()*+,-.//"},
244 {"%30%31%32%33%34%35%36%37%38%39%3a%3B%3C%3D%3e%3f/",
245 "0123456789:;<=>?/"},
246 {"%40%41%42%43%44%45%46%47%48%49%4a%4B%4C%4D%4e%4f/",
247 "@ABCDEFGHIJKLMNO/"},
248 {"%50%51%52%53%54%55%56%57%58%59%5a%5B%5C%5D%5e%5f/",
249 "PQRSTUVWXYZ[\\]^_/"},
250 {"%60%61%62%63%64%65%66%67%68%69%6a%6B%6C%6D%6e%6f/",
251 "`abcdefghijklmno/"},
252 {"%70%71%72%73%74%75%76%77%78%79%7a%7B%7C%7D%7e%7f/",
253 "pqrstuvwxyz{|}~\x7f/"},
254 {"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd"},
255 // U+FFFF (Noncharacter) should not be replaced with U+FFFD (Replacement
256 // Character) (http://crbug.com/1416021)
257 {"%ef%bf%bf", "\xef\xbf\xbf"},
258 // U+FDD0 (Noncharacter)
259 {"%ef%b7%90", "\xef\xb7\x90"},
260 // U+FFFD (Replacement Character)
261 {"%ef%bf%bd", "\xef\xbf\xbd"},
264 for (const auto& decode_case : decode_cases) {
265 RawCanonOutputT<char16_t> output;
266 DecodeURLEscapeSequences(decode_case.input,
267 DecodeURLMode::kUTF8OrIsomorphic, &output);
268 EXPECT_EQ(decode_case.output, base::UTF16ToUTF8(std::u16string(
269 output.data(), output.length())));
271 RawCanonOutputT<char16_t> output_utf8;
272 DecodeURLEscapeSequences(decode_case.input, DecodeURLMode::kUTF8,
274 EXPECT_EQ(decode_case.output,
276 std::u16string(output_utf8.data(), output_utf8.length())));
279 // Our decode should decode %00
280 const char zero_input[] = "%00";
281 RawCanonOutputT<char16_t> zero_output;
282 DecodeURLEscapeSequences(zero_input, DecodeURLMode::kUTF8, &zero_output);
283 EXPECT_NE("%00", base::UTF16ToUTF8(std::u16string(zero_output.data(),
284 zero_output.length())));
286 // Test the error behavior for invalid UTF-8.
287 struct Utf8DecodeCase {
289 std::vector<char16_t> expected_iso;
290 std::vector<char16_t> expected_utf8;
291 } utf8_decode_cases[] = {
292 // %e5%a5%bd is a valid UTF-8 sequence. U+597D
294 {0x00e4, 0x00a0, 0x00e5, 0x00a5, 0x00bd, 0},
295 {0xfffd, 0x597d, 0}},
297 {0x00e5, 0x00a5, 0x00bd, 0x00e4, 0x00a0, 0},
298 {0x597d, 0xfffd, 0}},
300 {0x00e4, 0x00a0, 0x00e5, 0x00bd, 0},
301 {0xfffd, 0xfffd, 0}},
304 for (const auto& utf8_decode_case : utf8_decode_cases) {
305 RawCanonOutputT<char16_t> output_iso;
306 DecodeURLEscapeSequences(utf8_decode_case.input,
307 DecodeURLMode::kUTF8OrIsomorphic, &output_iso);
308 EXPECT_EQ(std::u16string(utf8_decode_case.expected_iso.data()),
309 std::u16string(output_iso.data(), output_iso.length()));
311 RawCanonOutputT<char16_t> output_utf8;
312 DecodeURLEscapeSequences(utf8_decode_case.input, DecodeURLMode::kUTF8,
314 EXPECT_EQ(std::u16string(utf8_decode_case.expected_utf8.data()),
315 std::u16string(output_utf8.data(), output_utf8.length()));
319 TEST_F(URLUtilTest, TestEncodeURIComponent) {
324 {"hello, world", "hello%2C%20world"},
325 {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
326 "%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"},
327 {"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
328 "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"},
329 {" !\"#$%&'()*+,-./",
330 "%20!%22%23%24%25%26%27()*%2B%2C-.%2F"},
332 "0123456789%3A%3B%3C%3D%3E%3F"},
334 "%40ABCDEFGHIJKLMNO"},
335 {"PQRSTUVWXYZ[\\]^_",
336 "PQRSTUVWXYZ%5B%5C%5D%5E_"},
338 "%60abcdefghijklmno"},
339 {"pqrstuvwxyz{|}~\x7f",
340 "pqrstuvwxyz%7B%7C%7D~%7F"},
343 for (const auto& encode_case : encode_cases) {
344 RawCanonOutputT<char> buffer;
345 EncodeURIComponent(encode_case.input, &buffer);
346 std::string output(buffer.data(), buffer.length());
347 EXPECT_EQ(encode_case.output, output);
351 TEST_F(URLUtilTest, TestResolveRelativeWithNonStandardBase) {
352 // This tests non-standard (in the sense that IsStandard() == false)
353 // hierarchical schemes.
354 struct ResolveRelativeCase {
359 } resolve_non_standard_cases[] = {
360 // Resolving a relative path against a non-hierarchical URL should fail.
361 {"scheme:opaque_data", "/path", false, ""},
362 // Resolving a relative path against a non-standard authority-based base
363 // URL doesn't alter the authority section.
364 {"scheme://Authority/", "../path", true, "scheme://Authority/path"},
365 // A non-standard hierarchical base is resolved with path URL
366 // canonicalization rules.
367 {"data:/Blah:Blah/", "file.html", true, "data:/Blah:Blah/file.html"},
368 {"data:/Path/../part/part2", "file.html", true,
369 "data:/Path/../part/file.html"},
370 {"data://text/html,payload", "//user:pass@host:33////payload22", true,
371 "data://user:pass@host:33////payload22"},
372 // Path URL canonicalization rules also apply to non-standard authority-
374 {"custom://Authority/", "file.html", true,
375 "custom://Authority/file.html"},
376 {"custom://Authority/", "other://Auth/", true, "other://Auth/"},
377 {"custom://Authority/", "../../file.html", true,
378 "custom://Authority/file.html"},
379 {"custom://Authority/path/", "file.html", true,
380 "custom://Authority/path/file.html"},
381 {"custom://Authority:NoCanon/path/", "file.html", true,
382 "custom://Authority:NoCanon/path/file.html"},
383 // It's still possible to get an invalid path URL.
384 {"custom://Invalid:!#Auth/", "file.html", false, ""},
385 // A path with an authority section gets canonicalized under standard URL
386 // rules, even though the base was non-standard.
387 {"content://content.Provider/", "//other.Provider", true,
388 "content://other.provider/"},
390 // Resolving an absolute URL doesn't cause canonicalization of the
392 {"about:blank", "custom://Authority", true, "custom://Authority"},
393 // Fragment URLs can be resolved against a non-standard base.
394 {"scheme://Authority/path", "#fragment", true,
395 "scheme://Authority/path#fragment"},
396 {"scheme://Authority/", "#fragment", true,
397 "scheme://Authority/#fragment"},
398 // Resolving should fail if the base URL is authority-based but is
399 // missing a path component (the '/' at the end).
400 {"scheme://Authority", "path", false, ""},
401 // Test resolving a fragment (only) against any kind of base-URL.
402 {"about:blank", "#id42", true, "about:blank#id42"},
403 {"about:blank", " #id42", true, "about:blank#id42"},
404 {"about:blank#oldfrag", "#newfrag", true, "about:blank#newfrag"},
405 {"about:blank", " #id:42", true, "about:blank#id:42"},
406 // A surprising side effect of allowing fragments to resolve against
407 // any URL scheme is we might break javascript: URLs by doing so...
408 {"javascript:alert('foo#bar')", "#badfrag", true,
409 "javascript:alert('foo#badfrag"},
410 // In this case, the backslashes will not be canonicalized because it's a
411 // non-standard URL, but they will be treated as a path separators,
412 // giving the base URL here a path of "\".
414 // The result here is somewhat arbitrary. One could argue it should be
415 // either "aaa://a\" or "aaa://a/" since the path is being replaced with
416 // the "current directory". But in the context of resolving on data URLs,
417 // adding the requested dot doesn't seem wrong either.
418 {"aaa://a\\", "aaa:.", true, "aaa://a\\."}};
420 for (const auto& test : resolve_non_standard_cases) {
421 SCOPED_TRACE(testing::Message()
422 << "base: " << test.base << ", rel: " << test.rel);
425 ParsePathURL(test.base, strlen(test.base), false, &base_parsed);
427 std::string resolved;
428 StdStringCanonOutput output(&resolved);
429 Parsed resolved_parsed;
431 ResolveRelative(test.base, strlen(test.base), base_parsed, test.rel,
432 strlen(test.rel), nullptr, &output, &resolved_parsed);
435 EXPECT_EQ(test.is_valid, valid);
436 if (test.is_valid && valid) {
437 EXPECT_EQ(test.out, resolved);
442 TEST_F(URLUtilTest, TestNoRefComponent) {
443 // The hash-mark must be ignored when mailto: scheme is parsed,
444 // even if the URL has a base and relative part.
445 const char* base = "mailto://to/";
446 const char* rel = "any#body";
449 ParsePathURL(base, strlen(base), false, &base_parsed);
451 std::string resolved;
452 StdStringCanonOutput output(&resolved);
453 Parsed resolved_parsed;
455 bool valid = ResolveRelative(base, strlen(base), base_parsed, rel,
456 strlen(rel), nullptr, &output, &resolved_parsed);
458 EXPECT_FALSE(resolved_parsed.ref.is_valid());
461 TEST_F(URLUtilTest, PotentiallyDanglingMarkup) {
462 struct ResolveRelativeCase {
465 bool potentially_dangling_markup;
468 {"https://example.com/", "/path<", false, "https://example.com/path%3C"},
469 {"https://example.com/", "\n/path<", true, "https://example.com/path%3C"},
470 {"https://example.com/", "\r/path<", true, "https://example.com/path%3C"},
471 {"https://example.com/", "\t/path<", true, "https://example.com/path%3C"},
472 {"https://example.com/", "/pa\nth<", true, "https://example.com/path%3C"},
473 {"https://example.com/", "/pa\rth<", true, "https://example.com/path%3C"},
474 {"https://example.com/", "/pa\tth<", true, "https://example.com/path%3C"},
475 {"https://example.com/", "/path\n<", true, "https://example.com/path%3C"},
476 {"https://example.com/", "/path\r<", true, "https://example.com/path%3C"},
477 {"https://example.com/", "/path\r<", true, "https://example.com/path%3C"},
478 {"https://example.com/", "\n/<path", true, "https://example.com/%3Cpath"},
479 {"https://example.com/", "\r/<path", true, "https://example.com/%3Cpath"},
480 {"https://example.com/", "\t/<path", true, "https://example.com/%3Cpath"},
481 {"https://example.com/", "/<pa\nth", true, "https://example.com/%3Cpath"},
482 {"https://example.com/", "/<pa\rth", true, "https://example.com/%3Cpath"},
483 {"https://example.com/", "/<pa\tth", true, "https://example.com/%3Cpath"},
484 {"https://example.com/", "/<path\n", true, "https://example.com/%3Cpath"},
485 {"https://example.com/", "/<path\r", true, "https://example.com/%3Cpath"},
486 {"https://example.com/", "/<path\r", true, "https://example.com/%3Cpath"},
489 for (const auto& test : cases) {
490 SCOPED_TRACE(::testing::Message() << test.base << ", " << test.rel);
492 ParseStandardURL(test.base, strlen(test.base), &base_parsed);
494 std::string resolved;
495 StdStringCanonOutput output(&resolved);
496 Parsed resolved_parsed;
498 ResolveRelative(test.base, strlen(test.base), base_parsed, test.rel,
499 strlen(test.rel), nullptr, &output, &resolved_parsed);
503 EXPECT_EQ(test.potentially_dangling_markup,
504 resolved_parsed.potentially_dangling_markup);
505 EXPECT_EQ(test.out, resolved);
509 TEST_F(URLUtilTest, PotentiallyDanglingMarkupAfterReplacement) {
510 // Parse a URL with potentially dangling markup.
511 Parsed original_parsed;
512 RawCanonOutput<32> original;
513 const char* url = "htt\nps://example.com/<path";
514 Canonicalize(url, strlen(url), false, nullptr, &original, &original_parsed);
515 ASSERT_TRUE(original_parsed.potentially_dangling_markup);
517 // Perform a replacement, and validate that the potentially_dangling_markup
518 // flag carried over to the new Parsed object.
519 Replacements<char> replacements;
520 replacements.ClearRef();
521 Parsed replaced_parsed;
522 RawCanonOutput<32> replaced;
523 ReplaceComponents(original.data(), original.length(), original_parsed,
524 replacements, nullptr, &replaced, &replaced_parsed);
525 EXPECT_TRUE(replaced_parsed.potentially_dangling_markup);
528 TEST_F(URLUtilTest, PotentiallyDanglingMarkupAfterSchemeOnlyReplacement) {
529 // Parse a URL with potentially dangling markup.
530 Parsed original_parsed;
531 RawCanonOutput<32> original;
532 const char* url = "http://example.com/\n/<path";
533 Canonicalize(url, strlen(url), false, nullptr, &original, &original_parsed);
534 ASSERT_TRUE(original_parsed.potentially_dangling_markup);
536 // Perform a replacement, and validate that the potentially_dangling_markup
537 // flag carried over to the new Parsed object.
538 Replacements<char> replacements;
539 const char* new_scheme = "https";
540 replacements.SetScheme(new_scheme, Component(0, strlen(new_scheme)));
541 Parsed replaced_parsed;
542 RawCanonOutput<32> replaced;
543 ReplaceComponents(original.data(), original.length(), original_parsed,
544 replacements, nullptr, &replaced, &replaced_parsed);
545 EXPECT_TRUE(replaced_parsed.potentially_dangling_markup);
548 TEST_F(URLUtilTest, TestDomainIs) {
550 const char* canonicalized_host;
551 const char* lower_ascii_domain;
552 bool expected_domain_is;
554 {"google.com", "google.com", true},
555 {"www.google.com", "google.com", true}, // Subdomain is ignored.
556 {"www.google.com.cn", "google.com", false}, // Different TLD.
557 {"www.google.comm", "google.com", false},
558 {"www.iamnotgoogle.com", "google.com", false}, // Different hostname.
559 {"www.google.com", "Google.com", false}, // The input is not lower-cased.
561 // If the host ends with a dot, it matches domains with or without a dot.
562 {"www.google.com.", "google.com", true},
563 {"www.google.com.", "google.com.", true},
564 {"www.google.com.", ".com", true},
565 {"www.google.com.", ".com.", true},
567 // But, if the host doesn't end with a dot and the input domain does, then
568 // it's considered to not match.
569 {"www.google.com", "google.com.", false},
571 // If the host ends with two dots, it doesn't match.
572 {"www.google.com..", "google.com", false},
575 {"www.google.com", "", false},
576 {"", "www.google.com", false},
580 for (const auto& test_case : kTestCases) {
581 SCOPED_TRACE(testing::Message() << "(host, domain): ("
582 << test_case.canonicalized_host << ", "
583 << test_case.lower_ascii_domain << ")");
586 test_case.expected_domain_is,
587 DomainIs(test_case.canonicalized_host, test_case.lower_ascii_domain));
592 absl::optional<std::string> CanonicalizeSpec(std::string_view spec,
593 bool trim_path_end) {
594 std::string canonicalized;
595 StdStringCanonOutput output(&canonicalized);
597 if (!Canonicalize(spec.data(), spec.size(), trim_path_end,
598 /*charset_converter=*/nullptr, &output, &parsed)) {
601 output.Complete(); // Must be called before string is used.
602 return canonicalized;
606 #if BUILDFLAG(IS_WIN)
607 // Regression test for https://crbug.com/1252658.
608 TEST_F(URLUtilTest, TestCanonicalizeWindowsPathWithLeadingNUL) {
609 auto PrefixWithNUL = [](std::string&& s) -> std::string { return '\0' + s; };
610 EXPECT_EQ(CanonicalizeSpec(PrefixWithNUL("w:"), /*trim_path_end=*/false),
611 absl::make_optional("file:///W:"));
612 EXPECT_EQ(CanonicalizeSpec(PrefixWithNUL("\\\\server\\share"),
613 /*trim_path_end=*/false),
614 absl::make_optional("file://server/share"));
618 TEST_F(URLUtilTest, TestCanonicalizeIdempotencyWithLeadingControlCharacters) {
619 std::string spec = "_w:";
620 // Loop over all C0 control characters and the space character.
621 for (char c = '\0'; c <= ' '; c++) {
622 SCOPED_TRACE(testing::Message() << "c: " << c);
624 // Overwrite the first character of `spec`. Note that replacing the first
625 // character with NUL will not change the length!
628 for (bool trim_path_end : {false, true}) {
629 SCOPED_TRACE(testing::Message() << "trim_path_end: " << trim_path_end);
631 absl::optional<std::string> canonicalized =
632 CanonicalizeSpec(spec, trim_path_end);
633 ASSERT_TRUE(canonicalized);
634 EXPECT_EQ(canonicalized, CanonicalizeSpec(*canonicalized, trim_path_end));
639 TEST_F(URLUtilTest, TestHasInvalidURLEscapeSequences) {
648 // Single regular chars with no escaping are valid.
655 {"\x00", false}, // ASCII 'NUL' char
656 {"\x01", false}, // ASCII 'SOH' char
657 {"\xC2\xA3", false}, // UTF-8 encoded '£'.
659 // Longer strings without escaping are valid.
660 {"Hello world", false},
661 {"Here: [%25] <-- a percent-encoded percent character.", false},
663 // Valid %-escaped sequences ('%' followed by two hex digits).
673 {"abc%FFabc", false},
674 {"hello %FF world", false},
675 {"%20hello%20world", false},
684 // '%' followed by a single character is never a valid sequence.
694 {"%\xC2\xA3", true}, //% followed by UTF-8 encoded '£'.
696 // String ends on a potential escape sequence but without two hex-digits
703 // One hex and one non-hex digit is invalid.
711 {"%0\xC2\xA3", true}, // %0£.
714 for (TestCase test_case : cases) {
715 const char* input = test_case.input;
716 bool result = HasInvalidURLEscapeSequences(input);
717 EXPECT_EQ(test_case.is_invalid, result)
718 << "Invalid result for '" << input << "'";