url_formatter_unittest.cc 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. // Copyright 2015 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 "components/url_formatter/url_formatter.h"
  5. #include <stddef.h>
  6. #include <string.h>
  7. #include <vector>
  8. #include "base/logging.h"
  9. #include "base/strings/string_number_conversions.h"
  10. #include "base/strings/string_piece.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "build/build_config.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. #include "url/gurl.h"
  15. namespace url_formatter {
  16. namespace {
  17. using base::WideToUTF16;
  18. using base::ASCIIToUTF16;
  19. const size_t kNpos = std::u16string::npos;
  20. struct AdjustOffsetCase {
  21. size_t input_offset;
  22. size_t output_offset;
  23. };
  24. struct UrlTestData {
  25. const char* const description;
  26. const char* const input;
  27. FormatUrlTypes format_types;
  28. base::UnescapeRule::Type escape_rules;
  29. const wchar_t* output; // Use |wchar_t| to handle Unicode constants easily.
  30. size_t prefix_len;
  31. };
  32. // A pair of helpers for the FormatUrlWithOffsets() test.
  33. void VerboseExpect(size_t expected,
  34. size_t actual,
  35. const std::string& original_url,
  36. size_t position,
  37. const std::u16string& formatted_url) {
  38. EXPECT_EQ(expected, actual) << "Original URL: " << original_url
  39. << " (at char " << position << ")\nFormatted URL: " << formatted_url;
  40. }
  41. void CheckAdjustedOffsets(const std::string& url_string,
  42. FormatUrlTypes format_types,
  43. base::UnescapeRule::Type unescape_rules,
  44. const size_t* output_offsets) {
  45. GURL url(url_string);
  46. size_t url_length = url_string.length();
  47. std::vector<size_t> offsets;
  48. for (size_t i = 0; i <= url_length + 1; ++i)
  49. offsets.push_back(i);
  50. offsets.push_back(500000); // Something larger than any input length.
  51. offsets.push_back(std::string::npos);
  52. std::u16string formatted_url = FormatUrlWithOffsets(
  53. url, format_types, unescape_rules, nullptr, nullptr, &offsets);
  54. for (size_t i = 0; i < url_length; ++i)
  55. VerboseExpect(output_offsets[i], offsets[i], url_string, i, formatted_url);
  56. VerboseExpect(formatted_url.length(), offsets[url_length], url_string,
  57. url_length, formatted_url);
  58. VerboseExpect(std::u16string::npos, offsets[url_length + 1], url_string,
  59. 500000, formatted_url);
  60. VerboseExpect(std::u16string::npos, offsets[url_length + 2], url_string,
  61. std::string::npos, formatted_url);
  62. }
  63. } // namespace
  64. TEST(UrlFormatterTest, FormatUrl) {
  65. FormatUrlTypes default_format_type = kFormatUrlOmitUsernamePassword;
  66. // clang-format off
  67. const UrlTestData tests[] = {
  68. {"Empty URL", "", default_format_type, base::UnescapeRule::NORMAL, L"", 0},
  69. {"Simple URL", "http://www.google.com/", default_format_type,
  70. base::UnescapeRule::NORMAL, L"http://www.google.com/", 7},
  71. {"With a port number and a reference",
  72. "http://www.google.com:8080/#\xE3\x82\xB0", default_format_type,
  73. base::UnescapeRule::NORMAL, L"http://www.google.com:8080/#\x30B0", 7},
  74. // -------- IDN tests --------
  75. {"Japanese IDN with ja", "http://xn--l8jvb1ey91xtjb.jp",
  76. default_format_type, base::UnescapeRule::NORMAL,
  77. L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7},
  78. {"mailto: with Japanese IDN", "mailto:foo@xn--l8jvb1ey91xtjb.jp",
  79. default_format_type, base::UnescapeRule::NORMAL,
  80. // GURL doesn't assume an email address's domain part as a host name.
  81. L"mailto:foo@xn--l8jvb1ey91xtjb.jp", 7},
  82. {"file: with Japanese IDN", "file://xn--l8jvb1ey91xtjb.jp/config.sys",
  83. default_format_type, base::UnescapeRule::NORMAL,
  84. L"file://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 7},
  85. {"ftp: with Japanese IDN", "ftp://xn--l8jvb1ey91xtjb.jp/config.sys",
  86. default_format_type, base::UnescapeRule::NORMAL,
  87. L"ftp://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 6},
  88. // -------- omit_username_password flag tests --------
  89. {"With username and password, omit_username_password=false",
  90. "http://user:passwd@example.com/foo", kFormatUrlOmitNothing,
  91. base::UnescapeRule::NORMAL, L"http://user:passwd@example.com/foo", 19},
  92. {"With username and password, omit_username_password=true",
  93. "http://user:passwd@example.com/foo", default_format_type,
  94. base::UnescapeRule::NORMAL, L"http://example.com/foo", 7},
  95. {"With username and no password", "http://user@example.com/foo",
  96. default_format_type, base::UnescapeRule::NORMAL,
  97. L"http://example.com/foo", 7},
  98. {"Just '@' without username and password", "http://@example.com/foo",
  99. default_format_type, base::UnescapeRule::NORMAL,
  100. L"http://example.com/foo", 7},
  101. // GURL doesn't think local-part of an email address is username for URL.
  102. {"mailto:, omit_username_password=true", "mailto:foo@example.com",
  103. default_format_type, base::UnescapeRule::NORMAL,
  104. L"mailto:foo@example.com", 7},
  105. // -------- unescape flag tests --------
  106. {"Do not unescape",
  107. "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/"
  108. "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"
  109. "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB",
  110. default_format_type, base::UnescapeRule::NONE,
  111. // GURL parses %-encoded hostnames into Punycode.
  112. L"http://\x30B0\x30FC\x30B0\x30EB.jp/"
  113. L"%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"
  114. L"?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB",
  115. 7},
  116. {"Unescape normally",
  117. "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/"
  118. "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"
  119. "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB",
  120. default_format_type, base::UnescapeRule::NORMAL,
  121. L"http://\x30B0\x30FC\x30B0\x30EB.jp/\x30B0\x30FC\x30B0\x30EB"
  122. L"?q=\x30B0\x30FC\x30B0\x30EB",
  123. 7},
  124. {"Unescape normally with BiDi control character",
  125. "http://example.com/%E2%80%AEabc?q=%E2%80%8Fxy", default_format_type,
  126. base::UnescapeRule::NORMAL,
  127. L"http://example.com/%E2%80%AEabc?q=%E2%80%8Fxy", 7},
  128. {"Unescape normally including unescape spaces",
  129. "http://www.google.com/search?q=Hello%20World", default_format_type,
  130. base::UnescapeRule::SPACES, L"http://www.google.com/search?q=Hello World",
  131. 7},
  132. /*
  133. {"unescape=true with some special characters",
  134. "http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z",
  135. kFormatUrlOmitNothing, base::UnescapeRule::NORMAL,
  136. L"http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", 25},
  137. */
  138. // Disabled: the resultant URL becomes "...user%253A:%2540passwd...".
  139. // -------- omit http: --------
  140. {"omit http", "http://www.google.com/", kFormatUrlOmitHTTP,
  141. base::UnescapeRule::NORMAL, L"www.google.com/", 0},
  142. {"omit http on bare scheme", "http://", kFormatUrlOmitDefaults,
  143. base::UnescapeRule::NORMAL, L"", 0},
  144. {"omit http with user name", "http://user@example.com/foo",
  145. kFormatUrlOmitDefaults, base::UnescapeRule::NORMAL, L"example.com/foo",
  146. 0},
  147. {"omit http with https", "https://www.google.com/", kFormatUrlOmitHTTP,
  148. base::UnescapeRule::NORMAL, L"https://www.google.com/", 8},
  149. {"omit http starts with ftp.", "http://ftp.google.com/",
  150. kFormatUrlOmitHTTP, base::UnescapeRule::NORMAL, L"http://ftp.google.com/",
  151. 7},
  152. // -------- omit file: --------
  153. #if BUILDFLAG(IS_WIN)
  154. {"omit file on Windows", "file:///C:/Users/homedirname/folder/file.pdf/",
  155. kFormatUrlOmitFileScheme, base::UnescapeRule::NORMAL,
  156. L"C:/Users/homedirname/folder/file.pdf/", static_cast<size_t>(-1)},
  157. #else
  158. {"omit file", "file:///Users/homedirname/folder/file.pdf/",
  159. kFormatUrlOmitFileScheme, base::UnescapeRule::NORMAL,
  160. L"/Users/homedirname/folder/file.pdf/", 0},
  161. #endif
  162. // -------- omit mailto: --------
  163. { "omit mailto", "mailto:foo@bar.com",
  164. kFormatUrlOmitMailToScheme, base::UnescapeRule::NORMAL,
  165. L"foo@bar.com", 0 },
  166. // -------- omit trailing slash on bare hostname --------
  167. {"omit slash when it's the entire path", "http://www.google.com/",
  168. kFormatUrlOmitTrailingSlashOnBareHostname, base::UnescapeRule::NORMAL,
  169. L"http://www.google.com", 7},
  170. {"omit slash when there's a ref", "http://www.google.com/#ref",
  171. kFormatUrlOmitTrailingSlashOnBareHostname, base::UnescapeRule::NORMAL,
  172. L"http://www.google.com/#ref", 7},
  173. {"omit slash when there's a query", "http://www.google.com/?",
  174. kFormatUrlOmitTrailingSlashOnBareHostname, base::UnescapeRule::NORMAL,
  175. L"http://www.google.com/?", 7},
  176. {"omit slash when it's not the entire path", "http://www.google.com/foo",
  177. kFormatUrlOmitTrailingSlashOnBareHostname, base::UnescapeRule::NORMAL,
  178. L"http://www.google.com/foo", 7},
  179. {"omit slash for nonstandard URLs", "data:/",
  180. kFormatUrlOmitTrailingSlashOnBareHostname, base::UnescapeRule::NORMAL,
  181. L"data:/", 5},
  182. {"omit slash for file URLs", "file:///",
  183. kFormatUrlOmitTrailingSlashOnBareHostname, base::UnescapeRule::NORMAL,
  184. L"file:///", 7},
  185. // -------- view-source: --------
  186. {"view-source", "view-source:http://xn--qcka1pmc.jp/",
  187. default_format_type, base::UnescapeRule::NORMAL,
  188. L"view-source:http://\x30B0\x30FC\x30B0\x30EB.jp/", 19},
  189. {"view-source of view-source",
  190. "view-source:view-source:http://xn--qcka1pmc.jp/", default_format_type,
  191. base::UnescapeRule::NORMAL,
  192. L"view-source:view-source:http://xn--qcka1pmc.jp/", 12},
  193. // view-source should omit http and trailing slash where non-view-source
  194. // would.
  195. {"view-source omit http", "view-source:http://a.b/c",
  196. kFormatUrlOmitDefaults, base::UnescapeRule::NORMAL, L"view-source:a.b/c",
  197. 12},
  198. {"view-source omit http starts with ftp.", "view-source:http://ftp.b/c",
  199. kFormatUrlOmitDefaults, base::UnescapeRule::NORMAL,
  200. L"view-source:http://ftp.b/c", 19},
  201. {"view-source omit slash when it's the entire path",
  202. "view-source:http://a.b/", kFormatUrlOmitDefaults,
  203. base::UnescapeRule::NORMAL, L"view-source:a.b", 12},
  204. {"view-source never applies destructive elisions to its inner URL",
  205. "view-source:https://www.google.com/foo",
  206. kFormatUrlOmitDefaults | kFormatUrlOmitHTTPS |
  207. kFormatUrlOmitTrivialSubdomains | kFormatUrlTrimAfterHost,
  208. base::UnescapeRule::NORMAL, L"view-source:https://www.google.com/foo",
  209. 20},
  210. #if BUILDFLAG(IS_WIN)
  211. {"view-source should not omit file on Windows",
  212. "view-source:file:///C:/Users/homedirname/folder/file.pdf/",
  213. kFormatUrlOmitDefaults | kFormatUrlOmitFileScheme,
  214. base::UnescapeRule::NORMAL,
  215. L"view-source:file:///C:/Users/homedirname/folder/file.pdf/", 19},
  216. #else
  217. {"view-source should not omit file",
  218. "view-source:file:///Users/homedirname/folder/file.pdf/",
  219. kFormatUrlOmitDefaults | kFormatUrlOmitFileScheme,
  220. base::UnescapeRule::NORMAL,
  221. L"view-source:file:///Users/homedirname/folder/file.pdf/", 19},
  222. #endif
  223. // -------- omit https --------
  224. {"omit https", "https://www.google.com/", kFormatUrlOmitHTTPS,
  225. base::UnescapeRule::NORMAL, L"www.google.com/", 0},
  226. {"omit https but do not omit http", "http://www.google.com/",
  227. kFormatUrlOmitHTTPS, base::UnescapeRule::NORMAL,
  228. L"http://www.google.com/", 7},
  229. {"omit https, username, and password",
  230. "https://user:password@example.com/foo",
  231. kFormatUrlOmitDefaults | kFormatUrlOmitHTTPS, base::UnescapeRule::NORMAL,
  232. L"example.com/foo", 0},
  233. {"omit https, but preserve user name and password",
  234. "https://user:password@example.com/foo", kFormatUrlOmitHTTPS,
  235. base::UnescapeRule::NORMAL, L"user:password@example.com/foo", 14},
  236. {"omit https should not affect hosts starting with ftp.",
  237. "https://ftp.google.com/", kFormatUrlOmitHTTP | kFormatUrlOmitHTTPS,
  238. base::UnescapeRule::NORMAL, L"https://ftp.google.com/", 8},
  239. // -------- omit trivial subdomains --------
  240. {"omit trivial subdomains - trim leading www",
  241. "http://www.wikipedia.org/", kFormatUrlOmitTrivialSubdomains,
  242. base::UnescapeRule::NORMAL, L"http://wikipedia.org/", 7},
  243. {"omit trivial subdomains - don't trim leading m",
  244. "http://m.google.com/", kFormatUrlOmitTrivialSubdomains,
  245. base::UnescapeRule::NORMAL, L"http://m.google.com/", 7},
  246. {"omit trivial subdomains - don't trim www after a leading m",
  247. "http://m.www.google.com/", kFormatUrlOmitTrivialSubdomains,
  248. base::UnescapeRule::NORMAL, L"http://m.www.google.com/", 7},
  249. {"omit trivial subdomains - trim first www only",
  250. "http://www.www.www.wikipedia.org/", kFormatUrlOmitTrivialSubdomains,
  251. base::UnescapeRule::NORMAL, L"http://www.www.wikipedia.org/", 7},
  252. {"omit trivial subdomains - don't trim www from middle",
  253. "http://en.www.wikipedia.org/", kFormatUrlOmitTrivialSubdomains,
  254. base::UnescapeRule::NORMAL, L"http://en.www.wikipedia.org/", 7},
  255. {"omit trivial subdomains - don't do blind substring matches for www",
  256. "http://foowww.google.com/", kFormatUrlOmitTrivialSubdomains,
  257. base::UnescapeRule::NORMAL, L"http://foowww.google.com/", 7},
  258. {"omit trivial subdomains - don't crash on multiple delimiters",
  259. "http://www....foobar...google.com/", kFormatUrlOmitTrivialSubdomains,
  260. base::UnescapeRule::NORMAL, L"http://...foobar...google.com/", 7},
  261. {"omit trivial subdomains - sanity check for ordinary subdomains",
  262. "http://mail.yahoo.com/", kFormatUrlOmitTrivialSubdomains,
  263. base::UnescapeRule::NORMAL, L"http://mail.yahoo.com/", 7},
  264. {"omit trivial subdomains - sanity check for auth",
  265. "http://www:m@google.com/", kFormatUrlOmitTrivialSubdomains,
  266. base::UnescapeRule::NORMAL, L"http://www:m@google.com/", 13},
  267. {"omit trivial subdomains - sanity check for path",
  268. "http://google.com/www.m.foobar", kFormatUrlOmitTrivialSubdomains,
  269. base::UnescapeRule::NORMAL, L"http://google.com/www.m.foobar", 7},
  270. {"omit trivial subdomains - sanity check for IDN",
  271. "http://www.xn--cy2a840a.www.xn--cy2a840a.com",
  272. kFormatUrlOmitTrivialSubdomains, base::UnescapeRule::NORMAL,
  273. L"http://\x89c6\x9891.www.\x89c6\x9891.com/", 7},
  274. {"omit trivial subdomains but leave registry and domain alone - trivial",
  275. "http://google.com/", kFormatUrlOmitTrivialSubdomains,
  276. base::UnescapeRule::NORMAL, L"http://google.com/", 7},
  277. {"omit trivial subdomains but leave registry and domain alone - www",
  278. "http://www.com/", kFormatUrlOmitTrivialSubdomains,
  279. base::UnescapeRule::NORMAL, L"http://www.com/", 7},
  280. {"omit trivial subdomains but leave registry and domain alone - co.uk",
  281. "http://m.co.uk/", kFormatUrlOmitTrivialSubdomains,
  282. base::UnescapeRule::NORMAL, L"http://m.co.uk/", 7},
  283. {"omit trivial subdomains but leave eTLD (effective TLD) alone",
  284. "http://www.appspot.com/", kFormatUrlOmitTrivialSubdomains,
  285. base::UnescapeRule::NORMAL, L"http://www.appspot.com/", 7},
  286. {"omit trivial subdomains but leave intranet hostnames alone",
  287. "http://router/", kFormatUrlOmitTrivialSubdomains,
  288. base::UnescapeRule::NORMAL, L"http://router/", 7},
  289. {"omit trivial subdomains but leave alone if host itself is a registry",
  290. "http://co.uk/", kFormatUrlOmitTrivialSubdomains,
  291. base::UnescapeRule::NORMAL, L"http://co.uk/", 7},
  292. #if BUILDFLAG(IS_IOS)
  293. // -------- omit mobile prefix --------
  294. {"omit mobile prefix - trim leading m.",
  295. "http://m.wikipedia.org/", kFormatUrlOmitMobilePrefix,
  296. base::UnescapeRule::NORMAL, L"http://wikipedia.org/", 7},
  297. {"omit mobile prefix - trim leading m., but don't trim www",
  298. "http://m.www.google.com/", kFormatUrlOmitMobilePrefix,
  299. base::UnescapeRule::NORMAL, L"http://www.google.com/", 7},
  300. {"omit mobile prefix - trim first m. only",
  301. "http://m.m.m.wikipedia.org/", kFormatUrlOmitMobilePrefix,
  302. base::UnescapeRule::NORMAL, L"http://m.m.wikipedia.org/", 7},
  303. {"omit mobile prefix - don't trim m. from middle",
  304. "http://en.m.wikipedia.org/", kFormatUrlOmitMobilePrefix,
  305. base::UnescapeRule::NORMAL, L"http://en.m.wikipedia.org/", 7},
  306. {"omit mobile prefix - don't do blind substring matches for m.",
  307. "http://foom.google.com/", kFormatUrlOmitMobilePrefix,
  308. base::UnescapeRule::NORMAL, L"http://foom.google.com/", 7},
  309. {"omit mobile prefix - don't crash on multiple delimiters",
  310. "http://m....foobar...google.com/", kFormatUrlOmitMobilePrefix,
  311. base::UnescapeRule::NORMAL, L"http://...foobar...google.com/", 7},
  312. {"omit mobile prefix - sanity check for ordinary subdomains",
  313. "http://mail.yahoo.com/", kFormatUrlOmitMobilePrefix,
  314. base::UnescapeRule::NORMAL, L"http://mail.yahoo.com/", 7},
  315. {"omit mobile prefix - sanity check for path",
  316. "http://google.com/www.m.foobar", kFormatUrlOmitMobilePrefix,
  317. base::UnescapeRule::NORMAL, L"http://google.com/www.m.foobar", 7},
  318. // -------- omit mobile prefix and trivial subdomains --------
  319. {"omit mobile prefix and trivial subdomains - trim leading m. and www.",
  320. "http://m.www.wikipedia.org/", kFormatUrlOmitMobilePrefix | kFormatUrlOmitTrivialSubdomains,
  321. base::UnescapeRule::NORMAL, L"http://wikipedia.org/", 7},
  322. {"omit mobile prefix and trivial subdomains - trim leading m., "
  323. "but don't trim www", "http://m.wwwwikipedia.org/",
  324. kFormatUrlOmitMobilePrefix | kFormatUrlOmitTrivialSubdomains,
  325. base::UnescapeRule::NORMAL, L"http://wwwwikipedia.org/", 7},
  326. {"omit mobile prefix and trivial subdomains - trim leading www. and m.",
  327. "http://www.m.wikipedia.org/", kFormatUrlOmitMobilePrefix |
  328. kFormatUrlOmitTrivialSubdomains, base::UnescapeRule::NORMAL,
  329. L"http://wikipedia.org/", 7},
  330. #endif
  331. // -------- trim after host --------
  332. {"omit the trailing slash when ommitting the path", "http://google.com/",
  333. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  334. base::UnescapeRule::NORMAL, L"google.com", 0},
  335. {"omit the simple file path when ommitting the path",
  336. "http://google.com/foo",
  337. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  338. base::UnescapeRule::NORMAL, L"google.com", 0},
  339. {"omit the file and folder path when ommitting the path",
  340. "http://google.com/ab/cd",
  341. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  342. base::UnescapeRule::NORMAL, L"google.com", 0},
  343. {"omit everything after host with query only",
  344. "http://google.com/?foo=bar",
  345. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  346. base::UnescapeRule::NORMAL, L"google.com", 0},
  347. {"omit everything after host with ref only", "http://google.com/#foobar",
  348. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  349. base::UnescapeRule::NORMAL, L"google.com", 0},
  350. {"omit everything after host with path and query only",
  351. "http://google.com/foo?a=b",
  352. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  353. base::UnescapeRule::NORMAL, L"google.com", 0},
  354. {"omit everything after host with path and ref only",
  355. "http://google.com/foo#c",
  356. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  357. base::UnescapeRule::NORMAL, L"google.com", 0},
  358. {"omit everything after host with query and ref only",
  359. "http://google.com/?a=b#c",
  360. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  361. base::UnescapeRule::NORMAL, L"google.com", 0},
  362. {"omit everything after host with path, query and ref",
  363. "http://google.com/foo?a=b#c",
  364. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  365. base::UnescapeRule::NORMAL, L"google.com", 0},
  366. {"omit everything after host with repeated delimiters (sanity check)",
  367. "http://google.com////???####",
  368. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  369. base::UnescapeRule::NORMAL, L"google.com", 0},
  370. {"never trim file paths", "file:///Users/homedirname/folder/file.pdf/",
  371. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  372. base::UnescapeRule::NORMAL,
  373. L"file:///Users/homedirname/folder/file.pdf/", 7},
  374. };
  375. // clang-format on
  376. for (size_t i = 0; i < std::size(tests); ++i) {
  377. size_t prefix_len;
  378. std::u16string formatted =
  379. FormatUrl(GURL(tests[i].input), tests[i].format_types,
  380. tests[i].escape_rules, nullptr, &prefix_len, nullptr);
  381. EXPECT_EQ(WideToUTF16(tests[i].output), formatted) << tests[i].description;
  382. EXPECT_EQ(tests[i].prefix_len, prefix_len) << tests[i].description;
  383. }
  384. }
  385. TEST(UrlFormatterTest, FormatUrlParsed) {
  386. // No unescape case.
  387. url::Parsed parsed;
  388. std::u16string formatted =
  389. FormatUrl(GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/"
  390. "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"),
  391. kFormatUrlOmitNothing, base::UnescapeRule::NONE, &parsed,
  392. nullptr, nullptr);
  393. EXPECT_EQ(
  394. u"http://%E3%82%B0:%E3%83%BC@\x30B0\x30FC\x30B0\x30EB.jp:8080"
  395. u"/%E3%82%B0/?q=%E3%82%B0#%E3%82%B0",
  396. formatted);
  397. EXPECT_EQ(u"%E3%82%B0",
  398. formatted.substr(parsed.username.begin, parsed.username.len));
  399. EXPECT_EQ(u"%E3%83%BC",
  400. formatted.substr(parsed.password.begin, parsed.password.len));
  401. EXPECT_EQ(u"\x30B0\x30FC\x30B0\x30EB.jp",
  402. formatted.substr(parsed.host.begin, parsed.host.len));
  403. EXPECT_EQ(u"8080", formatted.substr(parsed.port.begin, parsed.port.len));
  404. EXPECT_EQ(u"/%E3%82%B0/",
  405. formatted.substr(parsed.path.begin, parsed.path.len));
  406. EXPECT_EQ(u"q=%E3%82%B0",
  407. formatted.substr(parsed.query.begin, parsed.query.len));
  408. EXPECT_EQ(u"%E3%82%B0", formatted.substr(parsed.ref.begin, parsed.ref.len));
  409. // Unescape case.
  410. formatted =
  411. FormatUrl(GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/"
  412. "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"),
  413. kFormatUrlOmitNothing, base::UnescapeRule::NORMAL, &parsed,
  414. nullptr, nullptr);
  415. EXPECT_EQ(
  416. u"http://\x30B0:\x30FC@\x30B0\x30FC\x30B0\x30EB.jp:8080"
  417. u"/\x30B0/?q=\x30B0#\x30B0",
  418. formatted);
  419. EXPECT_EQ(u"\x30B0",
  420. formatted.substr(parsed.username.begin, parsed.username.len));
  421. EXPECT_EQ(u"\x30FC",
  422. formatted.substr(parsed.password.begin, parsed.password.len));
  423. EXPECT_EQ(u"\x30B0\x30FC\x30B0\x30EB.jp",
  424. formatted.substr(parsed.host.begin, parsed.host.len));
  425. EXPECT_EQ(u"8080", formatted.substr(parsed.port.begin, parsed.port.len));
  426. EXPECT_EQ(u"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len));
  427. EXPECT_EQ(u"q=\x30B0",
  428. formatted.substr(parsed.query.begin, parsed.query.len));
  429. EXPECT_EQ(u"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len));
  430. // Omit_username_password + unescape case.
  431. formatted =
  432. FormatUrl(GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/"
  433. "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"),
  434. kFormatUrlOmitUsernamePassword, base::UnescapeRule::NORMAL,
  435. &parsed, nullptr, nullptr);
  436. EXPECT_EQ(
  437. u"http://\x30B0\x30FC\x30B0\x30EB.jp:8080"
  438. u"/\x30B0/?q=\x30B0#\x30B0",
  439. formatted);
  440. EXPECT_FALSE(parsed.username.is_valid());
  441. EXPECT_FALSE(parsed.password.is_valid());
  442. EXPECT_EQ(u"\x30B0\x30FC\x30B0\x30EB.jp",
  443. formatted.substr(parsed.host.begin, parsed.host.len));
  444. EXPECT_EQ(u"8080", formatted.substr(parsed.port.begin, parsed.port.len));
  445. EXPECT_EQ(u"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len));
  446. EXPECT_EQ(u"q=\x30B0",
  447. formatted.substr(parsed.query.begin, parsed.query.len));
  448. EXPECT_EQ(u"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len));
  449. // View-source case.
  450. formatted =
  451. FormatUrl(GURL("view-source:http://user:passwd@host:81/path?query#ref"),
  452. kFormatUrlOmitUsernamePassword, base::UnescapeRule::NORMAL,
  453. &parsed, nullptr, nullptr);
  454. EXPECT_EQ(u"view-source:http://host:81/path?query#ref", formatted);
  455. EXPECT_EQ(u"view-source:http",
  456. formatted.substr(parsed.scheme.begin, parsed.scheme.len));
  457. EXPECT_FALSE(parsed.username.is_valid());
  458. EXPECT_FALSE(parsed.password.is_valid());
  459. EXPECT_EQ(u"host", formatted.substr(parsed.host.begin, parsed.host.len));
  460. EXPECT_EQ(u"81", formatted.substr(parsed.port.begin, parsed.port.len));
  461. EXPECT_EQ(u"/path", formatted.substr(parsed.path.begin, parsed.path.len));
  462. EXPECT_EQ(u"query", formatted.substr(parsed.query.begin, parsed.query.len));
  463. EXPECT_EQ(u"ref", formatted.substr(parsed.ref.begin, parsed.ref.len));
  464. // Repeated view-source separated by a space.
  465. formatted = FormatUrl(
  466. GURL(
  467. "view-source: view-source:http://user:passwd@host:81/path?query#ref"),
  468. kFormatUrlOmitUsernamePassword, base::UnescapeRule::NORMAL, &parsed,
  469. nullptr, nullptr);
  470. EXPECT_EQ(
  471. u"view-source: view-source:http://user:passwd@host:81/path?query#ref",
  472. formatted);
  473. EXPECT_EQ(u"view-source",
  474. formatted.substr(parsed.scheme.begin, parsed.scheme.len));
  475. EXPECT_FALSE(parsed.username.is_valid());
  476. EXPECT_FALSE(parsed.password.is_valid());
  477. EXPECT_FALSE(parsed.host.is_valid());
  478. EXPECT_FALSE(parsed.port.is_valid());
  479. EXPECT_EQ(u" view-source:http://user:passwd@host:81/path",
  480. formatted.substr(parsed.path.begin, parsed.path.len));
  481. EXPECT_EQ(u"query", formatted.substr(parsed.query.begin, parsed.query.len));
  482. EXPECT_EQ(u"ref", formatted.substr(parsed.ref.begin, parsed.ref.len));
  483. // omit http case.
  484. formatted = FormatUrl(GURL("http://host:8000/a?b=c#d"), kFormatUrlOmitHTTP,
  485. base::UnescapeRule::NORMAL, &parsed, nullptr, nullptr);
  486. EXPECT_EQ(u"host:8000/a?b=c#d", formatted);
  487. EXPECT_FALSE(parsed.scheme.is_valid());
  488. EXPECT_FALSE(parsed.username.is_valid());
  489. EXPECT_FALSE(parsed.password.is_valid());
  490. EXPECT_EQ(u"host", formatted.substr(parsed.host.begin, parsed.host.len));
  491. EXPECT_EQ(u"8000", formatted.substr(parsed.port.begin, parsed.port.len));
  492. EXPECT_EQ(u"/a", formatted.substr(parsed.path.begin, parsed.path.len));
  493. EXPECT_EQ(u"b=c", formatted.substr(parsed.query.begin, parsed.query.len));
  494. EXPECT_EQ(u"d", formatted.substr(parsed.ref.begin, parsed.ref.len));
  495. // omit http starts with ftp case.
  496. formatted =
  497. FormatUrl(GURL("http://ftp.host:8000/a?b=c#d"), kFormatUrlOmitHTTP,
  498. base::UnescapeRule::NORMAL, &parsed, nullptr, nullptr);
  499. EXPECT_EQ(u"http://ftp.host:8000/a?b=c#d", formatted);
  500. EXPECT_TRUE(parsed.scheme.is_valid());
  501. EXPECT_FALSE(parsed.username.is_valid());
  502. EXPECT_FALSE(parsed.password.is_valid());
  503. EXPECT_EQ(u"http", formatted.substr(parsed.scheme.begin, parsed.scheme.len));
  504. EXPECT_EQ(u"ftp.host", formatted.substr(parsed.host.begin, parsed.host.len));
  505. EXPECT_EQ(u"8000", formatted.substr(parsed.port.begin, parsed.port.len));
  506. EXPECT_EQ(u"/a", formatted.substr(parsed.path.begin, parsed.path.len));
  507. EXPECT_EQ(u"b=c", formatted.substr(parsed.query.begin, parsed.query.len));
  508. EXPECT_EQ(u"d", formatted.substr(parsed.ref.begin, parsed.ref.len));
  509. // omit http starts with 'f' case.
  510. formatted = FormatUrl(GURL("http://f/"), kFormatUrlOmitHTTP,
  511. base::UnescapeRule::NORMAL, &parsed, nullptr, nullptr);
  512. EXPECT_EQ(u"f/", formatted);
  513. EXPECT_FALSE(parsed.scheme.is_valid());
  514. EXPECT_FALSE(parsed.username.is_valid());
  515. EXPECT_FALSE(parsed.password.is_valid());
  516. EXPECT_FALSE(parsed.port.is_valid());
  517. EXPECT_TRUE(parsed.path.is_valid());
  518. EXPECT_FALSE(parsed.query.is_valid());
  519. EXPECT_FALSE(parsed.ref.is_valid());
  520. EXPECT_EQ(u"f", formatted.substr(parsed.host.begin, parsed.host.len));
  521. EXPECT_EQ(u"/", formatted.substr(parsed.path.begin, parsed.path.len));
  522. }
  523. // Make sure that calling FormatUrl on a GURL and then converting back to a GURL
  524. // results in the original GURL, for each ASCII character in the path.
  525. TEST(UrlFormatterTest, FormatUrlRoundTripPathASCII) {
  526. for (unsigned char test_char = 32; test_char < 128; ++test_char) {
  527. GURL url(std::string("http://www.google.com/") +
  528. static_cast<char>(test_char));
  529. size_t prefix_len;
  530. std::u16string formatted =
  531. FormatUrl(url, kFormatUrlOmitUsernamePassword,
  532. base::UnescapeRule::NORMAL, nullptr, &prefix_len, nullptr);
  533. EXPECT_EQ(url.spec(), GURL(formatted).spec());
  534. }
  535. }
  536. // Make sure that calling FormatUrl on a GURL and then converting back to a GURL
  537. // results in the original GURL, for each escaped ASCII character in the path.
  538. TEST(UrlFormatterTest, FormatUrlRoundTripPathEscaped) {
  539. for (unsigned char test_char = 32; test_char < 128; ++test_char) {
  540. std::string original_url("http://www.google.com/");
  541. original_url.push_back('%');
  542. original_url.append(base::HexEncode(&test_char, 1));
  543. GURL url(original_url);
  544. size_t prefix_len;
  545. std::u16string formatted =
  546. FormatUrl(url, kFormatUrlOmitUsernamePassword,
  547. base::UnescapeRule::NORMAL, nullptr, &prefix_len, nullptr);
  548. EXPECT_EQ(url.spec(), GURL(formatted).spec());
  549. }
  550. }
  551. // Make sure that calling FormatUrl on a GURL and then converting back to a GURL
  552. // results in the original GURL, for each ASCII character in the query.
  553. TEST(UrlFormatterTest, FormatUrlRoundTripQueryASCII) {
  554. for (unsigned char test_char = 32; test_char < 128; ++test_char) {
  555. GURL url(std::string("http://www.google.com/?") +
  556. static_cast<char>(test_char));
  557. size_t prefix_len;
  558. std::u16string formatted =
  559. FormatUrl(url, kFormatUrlOmitUsernamePassword,
  560. base::UnescapeRule::NORMAL, nullptr, &prefix_len, nullptr);
  561. EXPECT_EQ(url.spec(), GURL(formatted).spec());
  562. }
  563. }
  564. // Make sure that calling FormatUrl on a GURL and then converting back to a GURL
  565. // only results in a different GURL for certain characters.
  566. TEST(UrlFormatterTest, FormatUrlRoundTripQueryEscaped) {
  567. // A full list of characters which FormatURL should unescape and GURL should
  568. // not escape again, when they appear in a query string.
  569. const char kUnescapedCharacters[] =
  570. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_~";
  571. for (unsigned char test_char = 0; test_char < 128; ++test_char) {
  572. std::string original_url("http://www.google.com/?");
  573. original_url.push_back('%');
  574. original_url.append(base::HexEncode(&test_char, 1));
  575. GURL url(original_url);
  576. size_t prefix_len;
  577. std::u16string formatted =
  578. FormatUrl(url, kFormatUrlOmitUsernamePassword,
  579. base::UnescapeRule::NORMAL, nullptr, &prefix_len, nullptr);
  580. if (test_char &&
  581. strchr(kUnescapedCharacters, static_cast<char>(test_char))) {
  582. EXPECT_NE(url.spec(), GURL(formatted).spec());
  583. } else {
  584. EXPECT_EQ(url.spec(), GURL(formatted).spec());
  585. }
  586. }
  587. }
  588. TEST(UrlFormatterTest, StripWWWFromHostComponent) {
  589. {
  590. // Typical public URL should have www stripped.
  591. std::string url = "https://www.google.com/abc";
  592. url::Component host(8, 14);
  593. ASSERT_EQ("www.google.com", url.substr(host.begin, host.len));
  594. StripWWWFromHostComponent(url, &host);
  595. EXPECT_EQ("google.com", url.substr(host.begin, host.len));
  596. }
  597. {
  598. // Intranet hostname should not have www stripped.
  599. std::string url = "https://www.foobar/abc";
  600. url::Component host(8, 10);
  601. ASSERT_EQ("www.foobar", url.substr(host.begin, host.len));
  602. StripWWWFromHostComponent(url, &host);
  603. EXPECT_EQ("www.foobar", url.substr(host.begin, host.len));
  604. }
  605. {
  606. // Domain and registry should be excluded from www stripping.
  607. std::string url = "https://www.co.uk/abc";
  608. url::Component host(8, 9);
  609. ASSERT_EQ("www.co.uk", url.substr(host.begin, host.len));
  610. StripWWWFromHostComponent(url, &host);
  611. EXPECT_EQ("www.co.uk", url.substr(host.begin, host.len));
  612. }
  613. }
  614. TEST(UrlFormatterTest, FormatUrlWithOffsets) {
  615. CheckAdjustedOffsets(std::string(), kFormatUrlOmitNothing,
  616. base::UnescapeRule::NORMAL, nullptr);
  617. const size_t basic_offsets[] = {
  618. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  619. 21, 22, 23, 24, 25
  620. };
  621. CheckAdjustedOffsets("http://www.google.com/foo/", kFormatUrlOmitNothing,
  622. base::UnescapeRule::NORMAL, basic_offsets);
  623. const size_t omit_auth_offsets_1[] = {
  624. 0, 1, 2, 3, 4, 5, 6, 7, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 7,
  625. 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
  626. };
  627. CheckAdjustedOffsets("http://foo:bar@www.google.com/",
  628. kFormatUrlOmitUsernamePassword,
  629. base::UnescapeRule::NORMAL, omit_auth_offsets_1);
  630. const size_t omit_auth_offsets_2[] = {
  631. 0, 1, 2, 3, 4, 5, 6, 7, kNpos, kNpos, kNpos, 7, 8, 9, 10, 11, 12, 13, 14,
  632. 15, 16, 17, 18, 19, 20, 21
  633. };
  634. CheckAdjustedOffsets("http://foo@www.google.com/",
  635. kFormatUrlOmitUsernamePassword,
  636. base::UnescapeRule::NORMAL, omit_auth_offsets_2);
  637. const size_t dont_omit_auth_offsets[] = {
  638. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
  639. kNpos, kNpos, 11, 12, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
  640. kNpos, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  641. 30, 31
  642. };
  643. // Unescape to "http://foo\x30B0:\x30B0bar@www.google.com".
  644. CheckAdjustedOffsets("http://foo%E3%82%B0:%E3%82%B0bar@www.google.com/",
  645. kFormatUrlOmitNothing, base::UnescapeRule::NORMAL,
  646. dont_omit_auth_offsets);
  647. const size_t view_source_offsets[] = {
  648. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, kNpos,
  649. kNpos, kNpos, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33
  650. };
  651. CheckAdjustedOffsets("view-source:http://foo@www.google.com/",
  652. kFormatUrlOmitUsernamePassword,
  653. base::UnescapeRule::NORMAL, view_source_offsets);
  654. const size_t idn_hostname_offsets_1[] = {
  655. 0, 1, 2, 3, 4, 5, 6, 7, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
  656. kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 12,
  657. 13, 14, 15, 16, 17, 18, 19
  658. };
  659. // Convert punycode to "http://\x671d\x65e5\x3042\x3055\x3072.jp/foo/".
  660. CheckAdjustedOffsets("http://xn--l8jvb1ey91xtjb.jp/foo/",
  661. kFormatUrlOmitNothing, base::UnescapeRule::NORMAL,
  662. idn_hostname_offsets_1);
  663. const size_t idn_hostname_offsets_2[] = {
  664. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, kNpos, kNpos, kNpos, kNpos, kNpos,
  665. kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 14, 15, kNpos, kNpos, kNpos,
  666. kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
  667. kNpos, 19, 20, 21, 22, 23, 24
  668. };
  669. // Convert punycode to
  670. // "http://test.\x89c6\x9891.\x5317\x4eac\x5927\x5b78.test/".
  671. CheckAdjustedOffsets("http://test.xn--cy2a840a.xn--1lq90ic7f1rc.test/",
  672. kFormatUrlOmitNothing, base::UnescapeRule::NORMAL,
  673. idn_hostname_offsets_2);
  674. const size_t unescape_offsets[] = {
  675. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  676. 21, 22, 23, 24, 25, kNpos, kNpos, 26, 27, 28, 29, 30, kNpos, kNpos, kNpos,
  677. kNpos, kNpos, kNpos, kNpos, kNpos, 31, kNpos, kNpos, kNpos, kNpos, kNpos,
  678. kNpos, kNpos, kNpos, 32, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
  679. kNpos, 33, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos
  680. };
  681. // Unescape to "http://www.google.com/foo bar/\x30B0\x30FC\x30B0\x30EB".
  682. CheckAdjustedOffsets(
  683. "http://www.google.com/foo%20bar/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB",
  684. kFormatUrlOmitNothing, base::UnescapeRule::SPACES, unescape_offsets);
  685. const size_t ref_offsets[] = {
  686. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  687. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  688. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  689. 30, 31, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
  690. 32, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 33};
  691. // Unescape to "http://www.google.com/foo.html#\x30B0\x30B0z".
  692. CheckAdjustedOffsets("http://www.google.com/foo.html#%E3%82%B0%E3%82%B0z",
  693. kFormatUrlOmitNothing, base::UnescapeRule::NORMAL,
  694. ref_offsets);
  695. const size_t omit_http_offsets[] = {
  696. 0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  697. 10, 11, 12, 13, 14
  698. };
  699. CheckAdjustedOffsets("http://www.google.com/", kFormatUrlOmitHTTP,
  700. base::UnescapeRule::NORMAL, omit_http_offsets);
  701. const size_t omit_http_start_with_ftp_offsets[] = {
  702. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
  703. };
  704. CheckAdjustedOffsets("http://ftp.google.com/", kFormatUrlOmitHTTP,
  705. base::UnescapeRule::NORMAL,
  706. omit_http_start_with_ftp_offsets);
  707. const size_t omit_all_offsets[] = {
  708. 0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, kNpos, kNpos, kNpos, kNpos,
  709. 0, 1, 2, 3, 4, 5, 6, 7
  710. };
  711. CheckAdjustedOffsets("http://user@foo.com/", kFormatUrlOmitDefaults,
  712. base::UnescapeRule::NORMAL, omit_all_offsets);
  713. const size_t trim_after_host_offsets[] = {
  714. 0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, 1, 2, 3, 4,
  715. 5, 6, 7, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 9};
  716. CheckAdjustedOffsets("http://foo.com/abcdefg",
  717. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  718. base::UnescapeRule::NORMAL, trim_after_host_offsets);
  719. CheckAdjustedOffsets("http://foo.com/abc/def",
  720. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  721. base::UnescapeRule::NORMAL, trim_after_host_offsets);
  722. CheckAdjustedOffsets("http://foo.com/abc?a=b",
  723. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  724. base::UnescapeRule::NORMAL, trim_after_host_offsets);
  725. CheckAdjustedOffsets("http://foo.com/abc#def",
  726. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  727. base::UnescapeRule::NORMAL, trim_after_host_offsets);
  728. CheckAdjustedOffsets("http://foo.com/a?a=b#f",
  729. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  730. base::UnescapeRule::NORMAL, trim_after_host_offsets);
  731. CheckAdjustedOffsets("http://foo.com//??###",
  732. kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
  733. base::UnescapeRule::NORMAL, trim_after_host_offsets);
  734. const size_t omit_https_offsets[] = {
  735. 0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, 1, 2, 3,
  736. 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
  737. CheckAdjustedOffsets("https://www.google.com/", kFormatUrlOmitHTTPS,
  738. base::UnescapeRule::NORMAL, omit_https_offsets);
  739. const size_t omit_https_with_auth_offsets[] = {
  740. 0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0,
  741. kNpos, kNpos, kNpos, 0, 1, 2, 3, 4, 5,
  742. 6, 7, 8, 9, 10, 11, 12, 13, 14};
  743. CheckAdjustedOffsets("https://u:p@www.google.com/",
  744. kFormatUrlOmitDefaults | kFormatUrlOmitHTTPS,
  745. base::UnescapeRule::NORMAL,
  746. omit_https_with_auth_offsets);
  747. const size_t strip_trivial_subdomains_offsets_1[] = {
  748. 0, 1, 2, 3, 4, 5, 6, 7, kNpos, kNpos, kNpos, 7, 8,
  749. 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};
  750. CheckAdjustedOffsets(
  751. "http://www.google.com/foo/", kFormatUrlOmitTrivialSubdomains,
  752. base::UnescapeRule::NORMAL, strip_trivial_subdomains_offsets_1);
  753. const size_t strip_trivial_subdomains_from_idn_offsets[] = {
  754. 0, 1, 2, 3, 4, 5, 6, 7, kNpos, kNpos,
  755. kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
  756. kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 12,
  757. 13, 14, 15, 16, 17, 18, 19};
  758. CheckAdjustedOffsets(
  759. "http://www.xn--l8jvb1ey91xtjb.jp/foo/", kFormatUrlOmitTrivialSubdomains,
  760. base::UnescapeRule::NORMAL, strip_trivial_subdomains_from_idn_offsets);
  761. }
  762. } // namespace url_formatter