Fix for Geolocation webTCT failures
[platform/framework/web/chromium-efl.git] / url / url_util_unittest.cc
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.
4
5 #include "url/url_util.h"
6
7 #include <stddef.h>
8
9 #include <string_view>
10
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"
19
20 namespace url {
21
22 class URLUtilTest : public testing::Test {
23  public:
24   URLUtilTest() = default;
25
26   URLUtilTest(const URLUtilTest&) = delete;
27   URLUtilTest& operator=(const URLUtilTest&) = delete;
28
29   ~URLUtilTest() override = default;
30
31  private:
32   ScopedSchemeRegistryForTests scoped_registry_;
33 };
34
35 TEST_F(URLUtilTest, FindAndCompareScheme) {
36   Component found_scheme;
37
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)),
41                                    "http", nullptr));
42   EXPECT_TRUE(FindAndCompareScheme(
43       kStr1, static_cast<int>(strlen(kStr1)), "http", &found_scheme));
44   EXPECT_TRUE(found_scheme == Component(0, 4));
45
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));
50
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());
56
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));
62
63   // But when there is no scheme, it should fail.
64   EXPECT_FALSE(FindAndCompareScheme("", 0, "", &found_scheme));
65   EXPECT_TRUE(found_scheme == Component());
66
67   // When there is a whitespace char in scheme, it should canonicalize the URL
68   // before comparison.
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));
74
75   // Control characters should be stripped out on the ends, and kept in the
76   // middle.
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));
82 }
83
84 TEST_F(URLUtilTest, IsStandard) {
85   const char kHTTPScheme[] = "http";
86   EXPECT_TRUE(IsStandard(kHTTPScheme, Component(0, strlen(kHTTPScheme))));
87
88   const char kFooScheme[] = "foo";
89   EXPECT_FALSE(IsStandard(kFooScheme, Component(0, strlen(kFooScheme))));
90 }
91
92 TEST_F(URLUtilTest, IsReferrerScheme) {
93   const char kHTTPScheme[] = "http";
94   EXPECT_TRUE(IsReferrerScheme(kHTTPScheme, Component(0, strlen(kHTTPScheme))));
95
96   const char kFooScheme[] = "foo";
97   EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
98 }
99
100 TEST_F(URLUtilTest, AddReferrerScheme) {
101   static const char kFooScheme[] = "foo";
102   EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
103
104   url::ScopedSchemeRegistryForTests scoped_registry;
105   AddReferrerScheme(kFooScheme, url::SCHEME_WITH_HOST);
106   EXPECT_TRUE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
107 }
108
109 TEST_F(URLUtilTest, ShutdownCleansUpSchemes) {
110   static const char kFooScheme[] = "foo";
111   EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
112
113   {
114     url::ScopedSchemeRegistryForTests scoped_registry;
115     AddReferrerScheme(kFooScheme, url::SCHEME_WITH_HOST);
116     EXPECT_TRUE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
117   }
118
119   EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
120 }
121
122 TEST_F(URLUtilTest, GetStandardSchemeType) {
123   url::SchemeType scheme_type;
124
125   const char kHTTPScheme[] = "http";
126   scheme_type = url::SCHEME_WITHOUT_AUTHORITY;
127   EXPECT_TRUE(GetStandardSchemeType(kHTTPScheme,
128                                     Component(0, strlen(kHTTPScheme)),
129                                     &scheme_type));
130   EXPECT_EQ(url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, scheme_type);
131
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)),
136                                     &scheme_type));
137   EXPECT_EQ(url::SCHEME_WITHOUT_AUTHORITY, scheme_type);
138
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)),
143                                      &scheme_type));
144 }
145
146 TEST_F(URLUtilTest, GetStandardSchemes) {
147   std::vector<std::string> expected = {
148       kHttpsScheme, kHttpScheme, kFileScheme,       kFtpScheme,
149       kWssScheme,   kWsScheme,   kFileSystemScheme, "foo",
150   };
151   AddStandardScheme("foo", url::SCHEME_WITHOUT_AUTHORITY);
152   EXPECT_EQ(expected, GetStandardSchemes());
153 }
154
155 TEST_F(URLUtilTest, ReplaceComponents) {
156   Parsed parsed;
157   RawCanonOutputT<char> output;
158   Parsed new_parsed;
159
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,
164                     &new_parsed);
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,
169                     &new_parsed);
170   ReplaceComponents("", 0, parsed, replacements, nullptr, &output, &new_parsed);
171
172   replacements.ClearHost();
173   ReplaceComponents(nullptr, 0, parsed, replacements, nullptr, &output,
174                     &new_parsed);
175   ReplaceComponents("", 0, parsed, replacements, nullptr, &output, &new_parsed);
176   ReplaceComponents(nullptr, 0, parsed, replacements, nullptr, &output,
177                     &new_parsed);
178   ReplaceComponents("", 0, parsed, replacements, nullptr, &output, &new_parsed);
179 }
180
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,
187                &original_parsed);
188
189   Replacements<char> replacements;
190   replacements.SetScheme(scheme, Component(0, strlen(scheme)));
191
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);
197
198   output.Complete();
199   return output_string;
200 }
201
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"));
216
217 #ifdef WIN32
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"));
221 #endif
222
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"));
227
228   EXPECT_EQ("http://example.com/%20hello%20#%20world",
229             CheckReplaceScheme("myscheme:example.com/ hello # world ", "http"));
230 }
231
232 TEST_F(URLUtilTest, DecodeURLEscapeSequences) {
233   struct DecodeCase {
234     const char* input;
235     const char* output;
236   } decode_cases[] = {
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"},
262   };
263
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())));
270
271     RawCanonOutputT<char16_t> output_utf8;
272     DecodeURLEscapeSequences(decode_case.input, DecodeURLMode::kUTF8,
273                              &output_utf8);
274     EXPECT_EQ(decode_case.output,
275               base::UTF16ToUTF8(
276                   std::u16string(output_utf8.data(), output_utf8.length())));
277   }
278
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())));
285
286   // Test the error behavior for invalid UTF-8.
287   struct Utf8DecodeCase {
288     const char* input;
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
293       {"%e4%a0%e5%a5%bd",
294        {0x00e4, 0x00a0, 0x00e5, 0x00a5, 0x00bd, 0},
295        {0xfffd, 0x597d, 0}},
296       {"%e5%a5%bd%e4%a0",
297        {0x00e5, 0x00a5, 0x00bd, 0x00e4, 0x00a0, 0},
298        {0x597d, 0xfffd, 0}},
299       {"%e4%a0%e5%bd",
300        {0x00e4, 0x00a0, 0x00e5, 0x00bd, 0},
301        {0xfffd, 0xfffd, 0}},
302   };
303
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()));
310
311     RawCanonOutputT<char16_t> output_utf8;
312     DecodeURLEscapeSequences(utf8_decode_case.input, DecodeURLMode::kUTF8,
313                              &output_utf8);
314     EXPECT_EQ(std::u16string(utf8_decode_case.expected_utf8.data()),
315               std::u16string(output_utf8.data(), output_utf8.length()));
316   }
317 }
318
319 TEST_F(URLUtilTest, TestEncodeURIComponent) {
320   struct EncodeCase {
321     const char* input;
322     const char* output;
323   } encode_cases[] = {
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"},
331     {"0123456789:;<=>?",
332      "0123456789%3A%3B%3C%3D%3E%3F"},
333     {"@ABCDEFGHIJKLMNO",
334      "%40ABCDEFGHIJKLMNO"},
335     {"PQRSTUVWXYZ[\\]^_",
336      "PQRSTUVWXYZ%5B%5C%5D%5E_"},
337     {"`abcdefghijklmno",
338      "%60abcdefghijklmno"},
339     {"pqrstuvwxyz{|}~\x7f",
340      "pqrstuvwxyz%7B%7C%7D~%7F"},
341   };
342
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);
348   }
349 }
350
351 TEST_F(URLUtilTest, TestResolveRelativeWithNonStandardBase) {
352   // This tests non-standard (in the sense that IsStandard() == false)
353   // hierarchical schemes.
354   struct ResolveRelativeCase {
355     const char* base;
356     const char* rel;
357     bool is_valid;
358     const char* out;
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-
373       // based URLs.
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/"},
389
390       // Resolving an absolute URL doesn't cause canonicalization of the
391       // result.
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 "\".
413       //
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\\."}};
419
420   for (const auto& test : resolve_non_standard_cases) {
421     SCOPED_TRACE(testing::Message()
422                  << "base: " << test.base << ", rel: " << test.rel);
423
424     Parsed base_parsed;
425     ParsePathURL(test.base, strlen(test.base), false, &base_parsed);
426
427     std::string resolved;
428     StdStringCanonOutput output(&resolved);
429     Parsed resolved_parsed;
430     bool valid =
431         ResolveRelative(test.base, strlen(test.base), base_parsed, test.rel,
432                         strlen(test.rel), nullptr, &output, &resolved_parsed);
433     output.Complete();
434
435     EXPECT_EQ(test.is_valid, valid);
436     if (test.is_valid && valid) {
437       EXPECT_EQ(test.out, resolved);
438     }
439   }
440 }
441
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";
447
448   Parsed base_parsed;
449   ParsePathURL(base, strlen(base), false, &base_parsed);
450
451   std::string resolved;
452   StdStringCanonOutput output(&resolved);
453   Parsed resolved_parsed;
454
455   bool valid = ResolveRelative(base, strlen(base), base_parsed, rel,
456                                strlen(rel), nullptr, &output, &resolved_parsed);
457   EXPECT_TRUE(valid);
458   EXPECT_FALSE(resolved_parsed.ref.is_valid());
459 }
460
461 TEST_F(URLUtilTest, PotentiallyDanglingMarkup) {
462   struct ResolveRelativeCase {
463     const char* base;
464     const char* rel;
465     bool potentially_dangling_markup;
466     const char* out;
467   } cases[] = {
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"},
487   };
488
489   for (const auto& test : cases) {
490     SCOPED_TRACE(::testing::Message() << test.base << ", " << test.rel);
491     Parsed base_parsed;
492     ParseStandardURL(test.base, strlen(test.base), &base_parsed);
493
494     std::string resolved;
495     StdStringCanonOutput output(&resolved);
496     Parsed resolved_parsed;
497     bool valid =
498         ResolveRelative(test.base, strlen(test.base), base_parsed, test.rel,
499                         strlen(test.rel), nullptr, &output, &resolved_parsed);
500     ASSERT_TRUE(valid);
501     output.Complete();
502
503     EXPECT_EQ(test.potentially_dangling_markup,
504               resolved_parsed.potentially_dangling_markup);
505     EXPECT_EQ(test.out, resolved);
506   }
507 }
508
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);
516
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);
526 }
527
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);
535
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);
546 }
547
548 TEST_F(URLUtilTest, TestDomainIs) {
549   const struct {
550     const char* canonicalized_host;
551     const char* lower_ascii_domain;
552     bool expected_domain_is;
553   } kTestCases[] = {
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.
560
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},
566
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},
570
571       // If the host ends with two dots, it doesn't match.
572       {"www.google.com..", "google.com", false},
573
574       // Empty parameters.
575       {"www.google.com", "", false},
576       {"", "www.google.com", false},
577       {"", "", false},
578   };
579
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 << ")");
584
585     EXPECT_EQ(
586         test_case.expected_domain_is,
587         DomainIs(test_case.canonicalized_host, test_case.lower_ascii_domain));
588   }
589 }
590
591 namespace {
592 absl::optional<std::string> CanonicalizeSpec(std::string_view spec,
593                                              bool trim_path_end) {
594   std::string canonicalized;
595   StdStringCanonOutput output(&canonicalized);
596   Parsed parsed;
597   if (!Canonicalize(spec.data(), spec.size(), trim_path_end,
598                     /*charset_converter=*/nullptr, &output, &parsed)) {
599     return {};
600   }
601   output.Complete();  // Must be called before string is used.
602   return canonicalized;
603 }
604 }  // namespace
605
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"));
615 }
616 #endif
617
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);
623
624     // Overwrite the first character of `spec`. Note that replacing the first
625     // character with NUL will not change the length!
626     spec[0] = c;
627
628     for (bool trim_path_end : {false, true}) {
629       SCOPED_TRACE(testing::Message() << "trim_path_end: " << trim_path_end);
630
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));
635     }
636   }
637 }
638
639 TEST_F(URLUtilTest, TestHasInvalidURLEscapeSequences) {
640   struct TestCase {
641     const char* input;
642     bool is_invalid;
643   } cases[] = {
644       // Edge cases.
645       {"", false},
646       {"%", true},
647
648       // Single regular chars with no escaping are valid.
649       {"a", false},
650       {"g", false},
651       {"A", false},
652       {"G", false},
653       {":", false},
654       {"]", false},
655       {"\x00", false},      // ASCII 'NUL' char
656       {"\x01", false},      // ASCII 'SOH' char
657       {"\xC2\xA3", false},  // UTF-8 encoded '£'.
658
659       // Longer strings without escaping are valid.
660       {"Hello world", false},
661       {"Here: [%25] <-- a percent-encoded percent character.", false},
662
663       // Valid %-escaped sequences ('%' followed by two hex digits).
664       {"%00", false},
665       {"%20", false},
666       {"%02", false},
667       {"%ff", false},
668       {"%FF", false},
669       {"%0a", false},
670       {"%0A", false},
671       {"abc%FF", false},
672       {"%FFabc", false},
673       {"abc%FFabc", false},
674       {"hello %FF world", false},
675       {"%20hello%20world", false},
676       {"%25", false},
677       {"%25%25", false},
678       {"%250", false},
679       {"%259", false},
680       {"%25A", false},
681       {"%25F", false},
682       {"%0a:", false},
683
684       // '%' followed by a single character is never a valid sequence.
685       {"%%", true},
686       {"%2", true},
687       {"%a", true},
688       {"%A", true},
689       {"%g", true},
690       {"%G", true},
691       {"%:", true},
692       {"%[", true},
693       {"%F", true},
694       {"%\xC2\xA3", true},  //% followed by UTF-8 encoded '£'.
695
696       // String ends on a potential escape sequence but without two hex-digits
697       // is invalid.
698       {"abc%", true},
699       {"abc%%", true},
700       {"abc%%%", true},
701       {"abc%a", true},
702
703       // One hex and one non-hex digit is invalid.
704       {"%a:", true},
705       {"%:a", true},
706       {"%::", true},
707       {"%ag", true},
708       {"%ga", true},
709       {"%-1", true},
710       {"%1-", true},
711       {"%0\xC2\xA3", true},  // %0£.
712   };
713
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 << "'";
719   }
720 }
721
722 }  // namespace url