url_util_unittest.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "url/url_util.h"
  5. #include <stddef.h>
  6. #include "base/strings/string_piece.h"
  7. #include "build/build_config.h"
  8. #include "testing/gtest/include/gtest/gtest-message.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #include "url/third_party/mozilla/url_parse.h"
  12. #include "url/url_canon.h"
  13. #include "url/url_canon_stdstring.h"
  14. #include "url/url_test_utils.h"
  15. namespace url {
  16. class URLUtilTest : public testing::Test {
  17. public:
  18. URLUtilTest() = default;
  19. URLUtilTest(const URLUtilTest&) = delete;
  20. URLUtilTest& operator=(const URLUtilTest&) = delete;
  21. ~URLUtilTest() override = default;
  22. private:
  23. ScopedSchemeRegistryForTests scoped_registry_;
  24. };
  25. TEST_F(URLUtilTest, FindAndCompareScheme) {
  26. Component found_scheme;
  27. // Simple case where the scheme is found and matches.
  28. const char kStr1[] = "http://www.com/";
  29. EXPECT_TRUE(FindAndCompareScheme(
  30. kStr1, static_cast<int>(strlen(kStr1)), "http", NULL));
  31. EXPECT_TRUE(FindAndCompareScheme(
  32. kStr1, static_cast<int>(strlen(kStr1)), "http", &found_scheme));
  33. EXPECT_TRUE(found_scheme == Component(0, 4));
  34. // A case where the scheme is found and doesn't match.
  35. EXPECT_FALSE(FindAndCompareScheme(
  36. kStr1, static_cast<int>(strlen(kStr1)), "https", &found_scheme));
  37. EXPECT_TRUE(found_scheme == Component(0, 4));
  38. // A case where there is no scheme.
  39. const char kStr2[] = "httpfoobar";
  40. EXPECT_FALSE(FindAndCompareScheme(
  41. kStr2, static_cast<int>(strlen(kStr2)), "http", &found_scheme));
  42. EXPECT_TRUE(found_scheme == Component());
  43. // When there is an empty scheme, it should match the empty scheme.
  44. const char kStr3[] = ":foo.com/";
  45. EXPECT_TRUE(FindAndCompareScheme(
  46. kStr3, static_cast<int>(strlen(kStr3)), "", &found_scheme));
  47. EXPECT_TRUE(found_scheme == Component(0, 0));
  48. // But when there is no scheme, it should fail.
  49. EXPECT_FALSE(FindAndCompareScheme("", 0, "", &found_scheme));
  50. EXPECT_TRUE(found_scheme == Component());
  51. // When there is a whitespace char in scheme, it should canonicalize the URL
  52. // before comparison.
  53. const char whtspc_str[] = " \r\n\tjav\ra\nscri\tpt:alert(1)";
  54. EXPECT_TRUE(FindAndCompareScheme(whtspc_str,
  55. static_cast<int>(strlen(whtspc_str)),
  56. "javascript", &found_scheme));
  57. EXPECT_TRUE(found_scheme == Component(1, 10));
  58. // Control characters should be stripped out on the ends, and kept in the
  59. // middle.
  60. const char ctrl_str[] = "\02jav\02scr\03ipt:alert(1)";
  61. EXPECT_FALSE(FindAndCompareScheme(ctrl_str,
  62. static_cast<int>(strlen(ctrl_str)),
  63. "javascript", &found_scheme));
  64. EXPECT_TRUE(found_scheme == Component(1, 11));
  65. }
  66. TEST_F(URLUtilTest, IsStandard) {
  67. const char kHTTPScheme[] = "http";
  68. EXPECT_TRUE(IsStandard(kHTTPScheme, Component(0, strlen(kHTTPScheme))));
  69. const char kFooScheme[] = "foo";
  70. EXPECT_FALSE(IsStandard(kFooScheme, Component(0, strlen(kFooScheme))));
  71. }
  72. TEST_F(URLUtilTest, IsReferrerScheme) {
  73. const char kHTTPScheme[] = "http";
  74. EXPECT_TRUE(IsReferrerScheme(kHTTPScheme, Component(0, strlen(kHTTPScheme))));
  75. const char kFooScheme[] = "foo";
  76. EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
  77. }
  78. TEST_F(URLUtilTest, AddReferrerScheme) {
  79. static const char kFooScheme[] = "foo";
  80. EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
  81. url::ScopedSchemeRegistryForTests scoped_registry;
  82. AddReferrerScheme(kFooScheme, url::SCHEME_WITH_HOST);
  83. EXPECT_TRUE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
  84. }
  85. TEST_F(URLUtilTest, ShutdownCleansUpSchemes) {
  86. static const char kFooScheme[] = "foo";
  87. EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
  88. {
  89. url::ScopedSchemeRegistryForTests scoped_registry;
  90. AddReferrerScheme(kFooScheme, url::SCHEME_WITH_HOST);
  91. EXPECT_TRUE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
  92. }
  93. EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
  94. }
  95. TEST_F(URLUtilTest, GetStandardSchemeType) {
  96. url::SchemeType scheme_type;
  97. const char kHTTPScheme[] = "http";
  98. scheme_type = url::SCHEME_WITHOUT_AUTHORITY;
  99. EXPECT_TRUE(GetStandardSchemeType(kHTTPScheme,
  100. Component(0, strlen(kHTTPScheme)),
  101. &scheme_type));
  102. EXPECT_EQ(url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, scheme_type);
  103. const char kFilesystemScheme[] = "filesystem";
  104. scheme_type = url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
  105. EXPECT_TRUE(GetStandardSchemeType(kFilesystemScheme,
  106. Component(0, strlen(kFilesystemScheme)),
  107. &scheme_type));
  108. EXPECT_EQ(url::SCHEME_WITHOUT_AUTHORITY, scheme_type);
  109. const char kFooScheme[] = "foo";
  110. scheme_type = url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
  111. EXPECT_FALSE(GetStandardSchemeType(kFooScheme,
  112. Component(0, strlen(kFooScheme)),
  113. &scheme_type));
  114. }
  115. TEST_F(URLUtilTest, GetStandardSchemes) {
  116. std::vector<std::string> expected = {
  117. kHttpsScheme, kHttpScheme, kFileScheme,
  118. kFtpScheme, kWssScheme, kWsScheme,
  119. kFileSystemScheme, kQuicTransportScheme, "foo",
  120. };
  121. AddStandardScheme("foo", url::SCHEME_WITHOUT_AUTHORITY);
  122. EXPECT_EQ(expected, GetStandardSchemes());
  123. }
  124. TEST_F(URLUtilTest, ReplaceComponents) {
  125. Parsed parsed;
  126. RawCanonOutputT<char> output;
  127. Parsed new_parsed;
  128. // Check that the following calls do not cause crash
  129. Replacements<char> replacements;
  130. replacements.SetRef("test", Component(0, 4));
  131. ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
  132. ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
  133. replacements.ClearRef();
  134. replacements.SetHost("test", Component(0, 4));
  135. ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
  136. ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
  137. replacements.ClearHost();
  138. ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
  139. ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
  140. ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
  141. ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
  142. }
  143. static std::string CheckReplaceScheme(const char* base_url,
  144. const char* scheme) {
  145. // Make sure the input is canonicalized.
  146. RawCanonOutput<32> original;
  147. Parsed original_parsed;
  148. Canonicalize(base_url, strlen(base_url), true, NULL, &original,
  149. &original_parsed);
  150. Replacements<char> replacements;
  151. replacements.SetScheme(scheme, Component(0, strlen(scheme)));
  152. std::string output_string;
  153. StdStringCanonOutput output(&output_string);
  154. Parsed output_parsed;
  155. ReplaceComponents(original.data(), original.length(), original_parsed,
  156. replacements, NULL, &output, &output_parsed);
  157. output.Complete();
  158. return output_string;
  159. }
  160. TEST_F(URLUtilTest, ReplaceScheme) {
  161. EXPECT_EQ("https://google.com/",
  162. CheckReplaceScheme("http://google.com/", "https"));
  163. EXPECT_EQ("file://google.com/",
  164. CheckReplaceScheme("http://google.com/", "file"));
  165. EXPECT_EQ("http://home/Build",
  166. CheckReplaceScheme("file:///Home/Build", "http"));
  167. EXPECT_EQ("javascript:foo",
  168. CheckReplaceScheme("about:foo", "javascript"));
  169. EXPECT_EQ("://google.com/",
  170. CheckReplaceScheme("http://google.com/", ""));
  171. EXPECT_EQ("http://google.com/",
  172. CheckReplaceScheme("about:google.com", "http"));
  173. EXPECT_EQ("http:", CheckReplaceScheme("", "http"));
  174. #ifdef WIN32
  175. // Magic Windows drive letter behavior when converting to a file URL.
  176. EXPECT_EQ("file:///E:/foo/",
  177. CheckReplaceScheme("http://localhost/e:foo/", "file"));
  178. #endif
  179. // This will probably change to "about://google.com/" when we fix
  180. // http://crbug.com/160 which should also be an acceptable result.
  181. EXPECT_EQ("about://google.com/",
  182. CheckReplaceScheme("http://google.com/", "about"));
  183. EXPECT_EQ("http://example.com/%20hello%20#%20world",
  184. CheckReplaceScheme("myscheme:example.com/ hello # world ", "http"));
  185. }
  186. TEST_F(URLUtilTest, DecodeURLEscapeSequences) {
  187. struct DecodeCase {
  188. const char* input;
  189. const char* output;
  190. } decode_cases[] = {
  191. {"hello, world", "hello, world"},
  192. {"%01%02%03%04%05%06%07%08%09%0a%0B%0C%0D%0e%0f/",
  193. "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0B\x0C\x0D\x0e\x0f/"},
  194. {"%10%11%12%13%14%15%16%17%18%19%1a%1B%1C%1D%1e%1f/",
  195. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1B\x1C\x1D\x1e\x1f/"},
  196. {"%20%21%22%23%24%25%26%27%28%29%2a%2B%2C%2D%2e%2f/",
  197. " !\"#$%&'()*+,-.//"},
  198. {"%30%31%32%33%34%35%36%37%38%39%3a%3B%3C%3D%3e%3f/",
  199. "0123456789:;<=>?/"},
  200. {"%40%41%42%43%44%45%46%47%48%49%4a%4B%4C%4D%4e%4f/",
  201. "@ABCDEFGHIJKLMNO/"},
  202. {"%50%51%52%53%54%55%56%57%58%59%5a%5B%5C%5D%5e%5f/",
  203. "PQRSTUVWXYZ[\\]^_/"},
  204. {"%60%61%62%63%64%65%66%67%68%69%6a%6B%6C%6D%6e%6f/",
  205. "`abcdefghijklmno/"},
  206. {"%70%71%72%73%74%75%76%77%78%79%7a%7B%7C%7D%7e%7f/",
  207. "pqrstuvwxyz{|}~\x7f/"},
  208. {"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd"},
  209. };
  210. for (size_t i = 0; i < std::size(decode_cases); i++) {
  211. const char* input = decode_cases[i].input;
  212. RawCanonOutputT<char16_t> output;
  213. DecodeURLEscapeSequences(input, strlen(input),
  214. DecodeURLMode::kUTF8OrIsomorphic, &output);
  215. EXPECT_EQ(decode_cases[i].output, base::UTF16ToUTF8(std::u16string(
  216. output.data(), output.length())));
  217. RawCanonOutputT<char16_t> output_utf8;
  218. DecodeURLEscapeSequences(input, strlen(input), DecodeURLMode::kUTF8,
  219. &output_utf8);
  220. EXPECT_EQ(decode_cases[i].output,
  221. base::UTF16ToUTF8(
  222. std::u16string(output_utf8.data(), output_utf8.length())));
  223. }
  224. // Our decode should decode %00
  225. const char zero_input[] = "%00";
  226. RawCanonOutputT<char16_t> zero_output;
  227. DecodeURLEscapeSequences(zero_input, strlen(zero_input), DecodeURLMode::kUTF8,
  228. &zero_output);
  229. EXPECT_NE("%00", base::UTF16ToUTF8(std::u16string(zero_output.data(),
  230. zero_output.length())));
  231. // Test the error behavior for invalid UTF-8.
  232. struct Utf8DecodeCase {
  233. const char* input;
  234. std::vector<char16_t> expected_iso;
  235. std::vector<char16_t> expected_utf8;
  236. } utf8_decode_cases[] = {
  237. // %e5%a5%bd is a valid UTF-8 sequence. U+597D
  238. {"%e4%a0%e5%a5%bd",
  239. {0x00e4, 0x00a0, 0x00e5, 0x00a5, 0x00bd, 0},
  240. {0xfffd, 0x597d, 0}},
  241. {"%e5%a5%bd%e4%a0",
  242. {0x00e5, 0x00a5, 0x00bd, 0x00e4, 0x00a0, 0},
  243. {0x597d, 0xfffd, 0}},
  244. {"%e4%a0%e5%bd",
  245. {0x00e4, 0x00a0, 0x00e5, 0x00bd, 0},
  246. {0xfffd, 0xfffd, 0}},
  247. };
  248. for (const auto& test : utf8_decode_cases) {
  249. const char* input = test.input;
  250. RawCanonOutputT<char16_t> output_iso;
  251. DecodeURLEscapeSequences(input, strlen(input),
  252. DecodeURLMode::kUTF8OrIsomorphic, &output_iso);
  253. EXPECT_EQ(std::u16string(test.expected_iso.data()),
  254. std::u16string(output_iso.data(), output_iso.length()));
  255. RawCanonOutputT<char16_t> output_utf8;
  256. DecodeURLEscapeSequences(input, strlen(input), DecodeURLMode::kUTF8,
  257. &output_utf8);
  258. EXPECT_EQ(std::u16string(test.expected_utf8.data()),
  259. std::u16string(output_utf8.data(), output_utf8.length()));
  260. }
  261. }
  262. TEST_F(URLUtilTest, TestEncodeURIComponent) {
  263. struct EncodeCase {
  264. const char* input;
  265. const char* output;
  266. } encode_cases[] = {
  267. {"hello, world", "hello%2C%20world"},
  268. {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
  269. "%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"},
  270. {"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
  271. "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"},
  272. {" !\"#$%&'()*+,-./",
  273. "%20!%22%23%24%25%26%27()*%2B%2C-.%2F"},
  274. {"0123456789:;<=>?",
  275. "0123456789%3A%3B%3C%3D%3E%3F"},
  276. {"@ABCDEFGHIJKLMNO",
  277. "%40ABCDEFGHIJKLMNO"},
  278. {"PQRSTUVWXYZ[\\]^_",
  279. "PQRSTUVWXYZ%5B%5C%5D%5E_"},
  280. {"`abcdefghijklmno",
  281. "%60abcdefghijklmno"},
  282. {"pqrstuvwxyz{|}~\x7f",
  283. "pqrstuvwxyz%7B%7C%7D~%7F"},
  284. };
  285. for (size_t i = 0; i < std::size(encode_cases); i++) {
  286. const char* input = encode_cases[i].input;
  287. RawCanonOutputT<char> buffer;
  288. EncodeURIComponent(input, strlen(input), &buffer);
  289. std::string output(buffer.data(), buffer.length());
  290. EXPECT_EQ(encode_cases[i].output, output);
  291. }
  292. }
  293. TEST_F(URLUtilTest, TestResolveRelativeWithNonStandardBase) {
  294. // This tests non-standard (in the sense that IsStandard() == false)
  295. // hierarchical schemes.
  296. struct ResolveRelativeCase {
  297. const char* base;
  298. const char* rel;
  299. bool is_valid;
  300. const char* out;
  301. } resolve_non_standard_cases[] = {
  302. // Resolving a relative path against a non-hierarchical URL should fail.
  303. {"scheme:opaque_data", "/path", false, ""},
  304. // Resolving a relative path against a non-standard authority-based base
  305. // URL doesn't alter the authority section.
  306. {"scheme://Authority/", "../path", true, "scheme://Authority/path"},
  307. // A non-standard hierarchical base is resolved with path URL
  308. // canonicalization rules.
  309. {"data:/Blah:Blah/", "file.html", true, "data:/Blah:Blah/file.html"},
  310. {"data:/Path/../part/part2", "file.html", true,
  311. "data:/Path/../part/file.html"},
  312. {"data://text/html,payload", "//user:pass@host:33////payload22", true,
  313. "data://user:pass@host:33////payload22"},
  314. // Path URL canonicalization rules also apply to non-standard authority-
  315. // based URLs.
  316. {"custom://Authority/", "file.html", true,
  317. "custom://Authority/file.html"},
  318. {"custom://Authority/", "other://Auth/", true, "other://Auth/"},
  319. {"custom://Authority/", "../../file.html", true,
  320. "custom://Authority/file.html"},
  321. {"custom://Authority/path/", "file.html", true,
  322. "custom://Authority/path/file.html"},
  323. {"custom://Authority:NoCanon/path/", "file.html", true,
  324. "custom://Authority:NoCanon/path/file.html"},
  325. // It's still possible to get an invalid path URL.
  326. {"custom://Invalid:!#Auth/", "file.html", false, ""},
  327. // A path with an authority section gets canonicalized under standard URL
  328. // rules, even though the base was non-standard.
  329. {"content://content.Provider/", "//other.Provider", true,
  330. "content://other.provider/"},
  331. // Resolving an absolute URL doesn't cause canonicalization of the
  332. // result.
  333. {"about:blank", "custom://Authority", true, "custom://Authority"},
  334. // Fragment URLs can be resolved against a non-standard base.
  335. {"scheme://Authority/path", "#fragment", true,
  336. "scheme://Authority/path#fragment"},
  337. {"scheme://Authority/", "#fragment", true,
  338. "scheme://Authority/#fragment"},
  339. // Resolving should fail if the base URL is authority-based but is
  340. // missing a path component (the '/' at the end).
  341. {"scheme://Authority", "path", false, ""},
  342. // Test resolving a fragment (only) against any kind of base-URL.
  343. {"about:blank", "#id42", true, "about:blank#id42"},
  344. {"about:blank", " #id42", true, "about:blank#id42"},
  345. {"about:blank#oldfrag", "#newfrag", true, "about:blank#newfrag"},
  346. // A surprising side effect of allowing fragments to resolve against
  347. // any URL scheme is we might break javascript: URLs by doing so...
  348. {"javascript:alert('foo#bar')", "#badfrag", true,
  349. "javascript:alert('foo#badfrag"},
  350. // In this case, the backslashes will not be canonicalized because it's a
  351. // non-standard URL, but they will be treated as a path separators,
  352. // giving the base URL here a path of "\".
  353. //
  354. // The result here is somewhat arbitrary. One could argue it should be
  355. // either "aaa://a\" or "aaa://a/" since the path is being replaced with
  356. // the "current directory". But in the context of resolving on data URLs,
  357. // adding the requested dot doesn't seem wrong either.
  358. {"aaa://a\\", "aaa:.", true, "aaa://a\\."}};
  359. for (size_t i = 0; i < std::size(resolve_non_standard_cases); i++) {
  360. const ResolveRelativeCase& test_data = resolve_non_standard_cases[i];
  361. Parsed base_parsed;
  362. ParsePathURL(test_data.base, strlen(test_data.base), false, &base_parsed);
  363. std::string resolved;
  364. StdStringCanonOutput output(&resolved);
  365. Parsed resolved_parsed;
  366. bool valid = ResolveRelative(test_data.base, strlen(test_data.base),
  367. base_parsed, test_data.rel,
  368. strlen(test_data.rel), NULL, &output,
  369. &resolved_parsed);
  370. output.Complete();
  371. EXPECT_EQ(test_data.is_valid, valid) << i;
  372. if (test_data.is_valid && valid)
  373. EXPECT_EQ(test_data.out, resolved) << i;
  374. }
  375. }
  376. TEST_F(URLUtilTest, TestNoRefComponent) {
  377. // The hash-mark must be ignored when mailto: scheme is parsed,
  378. // even if the URL has a base and relative part.
  379. const char* base = "mailto://to/";
  380. const char* rel = "any#body";
  381. Parsed base_parsed;
  382. ParsePathURL(base, strlen(base), false, &base_parsed);
  383. std::string resolved;
  384. StdStringCanonOutput output(&resolved);
  385. Parsed resolved_parsed;
  386. bool valid = ResolveRelative(base, strlen(base),
  387. base_parsed, rel,
  388. strlen(rel), NULL, &output,
  389. &resolved_parsed);
  390. EXPECT_TRUE(valid);
  391. EXPECT_FALSE(resolved_parsed.ref.is_valid());
  392. }
  393. TEST_F(URLUtilTest, PotentiallyDanglingMarkup) {
  394. struct ResolveRelativeCase {
  395. const char* base;
  396. const char* rel;
  397. bool potentially_dangling_markup;
  398. const char* out;
  399. } cases[] = {
  400. {"https://example.com/", "/path<", false, "https://example.com/path%3C"},
  401. {"https://example.com/", "\n/path<", true, "https://example.com/path%3C"},
  402. {"https://example.com/", "\r/path<", true, "https://example.com/path%3C"},
  403. {"https://example.com/", "\t/path<", true, "https://example.com/path%3C"},
  404. {"https://example.com/", "/pa\nth<", true, "https://example.com/path%3C"},
  405. {"https://example.com/", "/pa\rth<", true, "https://example.com/path%3C"},
  406. {"https://example.com/", "/pa\tth<", true, "https://example.com/path%3C"},
  407. {"https://example.com/", "/path\n<", true, "https://example.com/path%3C"},
  408. {"https://example.com/", "/path\r<", true, "https://example.com/path%3C"},
  409. {"https://example.com/", "/path\r<", true, "https://example.com/path%3C"},
  410. {"https://example.com/", "\n/<path", true, "https://example.com/%3Cpath"},
  411. {"https://example.com/", "\r/<path", true, "https://example.com/%3Cpath"},
  412. {"https://example.com/", "\t/<path", true, "https://example.com/%3Cpath"},
  413. {"https://example.com/", "/<pa\nth", true, "https://example.com/%3Cpath"},
  414. {"https://example.com/", "/<pa\rth", true, "https://example.com/%3Cpath"},
  415. {"https://example.com/", "/<pa\tth", true, "https://example.com/%3Cpath"},
  416. {"https://example.com/", "/<path\n", true, "https://example.com/%3Cpath"},
  417. {"https://example.com/", "/<path\r", true, "https://example.com/%3Cpath"},
  418. {"https://example.com/", "/<path\r", true, "https://example.com/%3Cpath"},
  419. };
  420. for (const auto& test : cases) {
  421. SCOPED_TRACE(::testing::Message() << test.base << ", " << test.rel);
  422. Parsed base_parsed;
  423. ParseStandardURL(test.base, strlen(test.base), &base_parsed);
  424. std::string resolved;
  425. StdStringCanonOutput output(&resolved);
  426. Parsed resolved_parsed;
  427. bool valid =
  428. ResolveRelative(test.base, strlen(test.base), base_parsed, test.rel,
  429. strlen(test.rel), NULL, &output, &resolved_parsed);
  430. ASSERT_TRUE(valid);
  431. output.Complete();
  432. EXPECT_EQ(test.potentially_dangling_markup,
  433. resolved_parsed.potentially_dangling_markup);
  434. EXPECT_EQ(test.out, resolved);
  435. }
  436. }
  437. TEST_F(URLUtilTest, PotentiallyDanglingMarkupAfterReplacement) {
  438. // Parse a URL with potentially dangling markup.
  439. Parsed original_parsed;
  440. RawCanonOutput<32> original;
  441. const char* url = "htt\nps://example.com/<path";
  442. Canonicalize(url, strlen(url), false, nullptr, &original, &original_parsed);
  443. ASSERT_TRUE(original_parsed.potentially_dangling_markup);
  444. // Perform a replacement, and validate that the potentially_dangling_markup
  445. // flag carried over to the new Parsed object.
  446. Replacements<char> replacements;
  447. replacements.ClearRef();
  448. Parsed replaced_parsed;
  449. RawCanonOutput<32> replaced;
  450. ReplaceComponents(original.data(), original.length(), original_parsed,
  451. replacements, nullptr, &replaced, &replaced_parsed);
  452. EXPECT_TRUE(replaced_parsed.potentially_dangling_markup);
  453. }
  454. TEST_F(URLUtilTest, PotentiallyDanglingMarkupAfterSchemeOnlyReplacement) {
  455. // Parse a URL with potentially dangling markup.
  456. Parsed original_parsed;
  457. RawCanonOutput<32> original;
  458. const char* url = "http://example.com/\n/<path";
  459. Canonicalize(url, strlen(url), false, nullptr, &original, &original_parsed);
  460. ASSERT_TRUE(original_parsed.potentially_dangling_markup);
  461. // Perform a replacement, and validate that the potentially_dangling_markup
  462. // flag carried over to the new Parsed object.
  463. Replacements<char> replacements;
  464. const char* new_scheme = "https";
  465. replacements.SetScheme(new_scheme, Component(0, strlen(new_scheme)));
  466. Parsed replaced_parsed;
  467. RawCanonOutput<32> replaced;
  468. ReplaceComponents(original.data(), original.length(), original_parsed,
  469. replacements, nullptr, &replaced, &replaced_parsed);
  470. EXPECT_TRUE(replaced_parsed.potentially_dangling_markup);
  471. }
  472. TEST_F(URLUtilTest, TestDomainIs) {
  473. const struct {
  474. const char* canonicalized_host;
  475. const char* lower_ascii_domain;
  476. bool expected_domain_is;
  477. } kTestCases[] = {
  478. {"google.com", "google.com", true},
  479. {"www.google.com", "google.com", true}, // Subdomain is ignored.
  480. {"www.google.com.cn", "google.com", false}, // Different TLD.
  481. {"www.google.comm", "google.com", false},
  482. {"www.iamnotgoogle.com", "google.com", false}, // Different hostname.
  483. {"www.google.com", "Google.com", false}, // The input is not lower-cased.
  484. // If the host ends with a dot, it matches domains with or without a dot.
  485. {"www.google.com.", "google.com", true},
  486. {"www.google.com.", "google.com.", true},
  487. {"www.google.com.", ".com", true},
  488. {"www.google.com.", ".com.", true},
  489. // But, if the host doesn't end with a dot and the input domain does, then
  490. // it's considered to not match.
  491. {"www.google.com", "google.com.", false},
  492. // If the host ends with two dots, it doesn't match.
  493. {"www.google.com..", "google.com", false},
  494. // Empty parameters.
  495. {"www.google.com", "", false},
  496. {"", "www.google.com", false},
  497. {"", "", false},
  498. };
  499. for (const auto& test_case : kTestCases) {
  500. SCOPED_TRACE(testing::Message() << "(host, domain): ("
  501. << test_case.canonicalized_host << ", "
  502. << test_case.lower_ascii_domain << ")");
  503. EXPECT_EQ(
  504. test_case.expected_domain_is,
  505. DomainIs(test_case.canonicalized_host, test_case.lower_ascii_domain));
  506. }
  507. }
  508. namespace {
  509. absl::optional<std::string> CanonicalizeSpec(base::StringPiece spec,
  510. bool trim_path_end) {
  511. std::string canonicalized;
  512. StdStringCanonOutput output(&canonicalized);
  513. Parsed parsed;
  514. if (!Canonicalize(spec.data(), spec.size(), trim_path_end,
  515. /*charset_converter=*/nullptr, &output, &parsed)) {
  516. return {};
  517. }
  518. output.Complete(); // Must be called before string is used.
  519. return canonicalized;
  520. }
  521. } // namespace
  522. #if BUILDFLAG(IS_WIN)
  523. // Regression test for https://crbug.com/1252658.
  524. TEST_F(URLUtilTest, TestCanonicalizeWindowsPathWithLeadingNUL) {
  525. auto PrefixWithNUL = [](std::string&& s) -> std::string { return '\0' + s; };
  526. EXPECT_EQ(CanonicalizeSpec(PrefixWithNUL("w:"), /*trim_path_end=*/false),
  527. absl::make_optional("file:///W:"));
  528. EXPECT_EQ(CanonicalizeSpec(PrefixWithNUL("\\\\server\\share"),
  529. /*trim_path_end=*/false),
  530. absl::make_optional("file://server/share"));
  531. }
  532. #endif
  533. TEST_F(URLUtilTest, TestCanonicalizeIdempotencyWithLeadingControlCharacters) {
  534. std::string spec = "_w:";
  535. // Loop over all C0 control characters and the space character.
  536. for (char c = '\0'; c <= ' '; c++) {
  537. SCOPED_TRACE(testing::Message() << "c: " << c);
  538. // Overwrite the first character of `spec`. Note that replacing the first
  539. // character with NUL will not change the length!
  540. spec[0] = c;
  541. for (bool trim_path_end : {false, true}) {
  542. SCOPED_TRACE(testing::Message() << "trim_path_end: " << trim_path_end);
  543. absl::optional<std::string> canonicalized =
  544. CanonicalizeSpec(spec, trim_path_end);
  545. ASSERT_TRUE(canonicalized);
  546. EXPECT_EQ(canonicalized, CanonicalizeSpec(*canonicalized, trim_path_end));
  547. }
  548. }
  549. }
  550. } // namespace url