url_canon_unittest.cc 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611
  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_canon.h"
  5. #include <errno.h>
  6. #include <stddef.h>
  7. #include "base/strings/string_piece.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "base/test/gtest_util.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. #include "url/third_party/mozilla/url_parse.h"
  12. #include "url/url_canon_internal.h"
  13. #include "url/url_canon_stdstring.h"
  14. #include "url/url_test_utils.h"
  15. namespace url {
  16. namespace {
  17. struct ComponentCase {
  18. const char* input;
  19. const char* expected;
  20. Component expected_component;
  21. bool expected_success;
  22. };
  23. // ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests
  24. // treat each input as optional, and will only try processing if non-NULL.
  25. // The output is always 8-bit.
  26. struct DualComponentCase {
  27. const char* input8;
  28. const wchar_t* input16;
  29. const char* expected;
  30. Component expected_component;
  31. bool expected_success;
  32. };
  33. // Test cases for CanonicalizeIPAddress(). The inputs are identical to
  34. // DualComponentCase, but the output has extra CanonHostInfo fields.
  35. struct IPAddressCase {
  36. const char* input8;
  37. const wchar_t* input16;
  38. const char* expected;
  39. Component expected_component;
  40. // CanonHostInfo fields, for verbose output.
  41. CanonHostInfo::Family expected_family;
  42. int expected_num_ipv4_components;
  43. const char* expected_address_hex; // Two hex chars per IP address byte.
  44. };
  45. std::string BytesToHexString(unsigned char bytes[16], int length) {
  46. EXPECT_TRUE(length == 0 || length == 4 || length == 16)
  47. << "Bad IP address length: " << length;
  48. std::string result;
  49. for (int i = 0; i < length; ++i) {
  50. result.push_back(kHexCharLookup[(bytes[i] >> 4) & 0xf]);
  51. result.push_back(kHexCharLookup[bytes[i] & 0xf]);
  52. }
  53. return result;
  54. }
  55. struct ReplaceCase {
  56. const char* base;
  57. const char* scheme;
  58. const char* username;
  59. const char* password;
  60. const char* host;
  61. const char* port;
  62. const char* path;
  63. const char* query;
  64. const char* ref;
  65. const char* expected;
  66. };
  67. // Magic string used in the replacements code that tells SetupReplComp to
  68. // call the clear function.
  69. const char kDeleteComp[] = "|";
  70. // Sets up a replacement for a single component. This is given pointers to
  71. // the set and clear function for the component being replaced, and will
  72. // either set the component (if it exists) or clear it (if the replacement
  73. // string matches kDeleteComp).
  74. //
  75. // This template is currently used only for the 8-bit case, and the strlen
  76. // causes it to fail in other cases. It is left a template in case we have
  77. // tests for wide replacements.
  78. template<typename CHAR>
  79. void SetupReplComp(
  80. void (Replacements<CHAR>::*set)(const CHAR*, const Component&),
  81. void (Replacements<CHAR>::*clear)(),
  82. Replacements<CHAR>* rep,
  83. const CHAR* str) {
  84. if (str && str[0] == kDeleteComp[0]) {
  85. (rep->*clear)();
  86. } else if (str) {
  87. (rep->*set)(str, Component(0, static_cast<int>(strlen(str))));
  88. }
  89. }
  90. } // namespace
  91. TEST(URLCanonTest, DoAppendUTF8) {
  92. struct UTF8Case {
  93. unsigned input;
  94. const char* output;
  95. } utf_cases[] = {
  96. // Valid code points.
  97. {0x24, "\x24"},
  98. {0xA2, "\xC2\xA2"},
  99. {0x20AC, "\xE2\x82\xAC"},
  100. {0x24B62, "\xF0\xA4\xAD\xA2"},
  101. {0x10FFFF, "\xF4\x8F\xBF\xBF"},
  102. };
  103. std::string out_str;
  104. for (size_t i = 0; i < std::size(utf_cases); i++) {
  105. out_str.clear();
  106. StdStringCanonOutput output(&out_str);
  107. AppendUTF8Value(utf_cases[i].input, &output);
  108. output.Complete();
  109. EXPECT_EQ(utf_cases[i].output, out_str);
  110. }
  111. }
  112. TEST(URLCanonTest, DoAppendUTF8Invalid) {
  113. std::string out_str;
  114. StdStringCanonOutput output(&out_str);
  115. // Invalid code point (too large).
  116. EXPECT_DCHECK_DEATH({
  117. AppendUTF8Value(0x110000, &output);
  118. output.Complete();
  119. });
  120. }
  121. TEST(URLCanonTest, UTF) {
  122. // Low-level test that we handle reading, canonicalization, and writing
  123. // UTF-8/UTF-16 strings properly.
  124. struct UTFCase {
  125. const char* input8;
  126. const wchar_t* input16;
  127. bool expected_success;
  128. const char* output;
  129. } utf_cases[] = {
  130. // Valid canonical input should get passed through & escaped.
  131. {"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true, "%E4%BD%A0%E5%A5%BD"},
  132. // Test a character that takes > 16 bits (U+10300 = old italic letter A)
  133. {"\xF0\x90\x8C\x80", L"\xd800\xdf00", true, "%F0%90%8C%80"},
  134. // Non-shortest-form UTF-8 characters are invalid. The bad bytes should
  135. // each be replaced with the invalid character (EF BF DB in UTF-8).
  136. {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", nullptr, false,
  137. "%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%E5%A5%BD"},
  138. // Invalid UTF-8 sequences should be marked as invalid (the first
  139. // sequence is truncated).
  140. {"\xe4\xa0\xe5\xa5\xbd", L"\xd800\x597d", false, "%EF%BF%BD%E5%A5%BD"},
  141. // Character going off the end.
  142. {"\xe4\xbd\xa0\xe5\xa5", L"\x4f60\xd800", false, "%E4%BD%A0%EF%BF%BD"},
  143. // ...same with low surrogates with no high surrogate.
  144. {nullptr, L"\xdc00", false, "%EF%BF%BD"},
  145. // Test a UTF-8 encoded surrogate value is marked as invalid.
  146. // ED A0 80 = U+D800
  147. {"\xed\xa0\x80", nullptr, false, "%EF%BF%BD%EF%BF%BD%EF%BF%BD"},
  148. // ...even when paired.
  149. {"\xed\xa0\x80\xed\xb0\x80", nullptr, false,
  150. "%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD"},
  151. };
  152. std::string out_str;
  153. for (size_t i = 0; i < std::size(utf_cases); i++) {
  154. if (utf_cases[i].input8) {
  155. out_str.clear();
  156. StdStringCanonOutput output(&out_str);
  157. size_t input_len = strlen(utf_cases[i].input8);
  158. bool success = true;
  159. for (size_t ch = 0; ch < input_len; ch++) {
  160. success &= AppendUTF8EscapedChar(utf_cases[i].input8, &ch, input_len,
  161. &output);
  162. }
  163. output.Complete();
  164. EXPECT_EQ(utf_cases[i].expected_success, success);
  165. EXPECT_EQ(std::string(utf_cases[i].output), out_str);
  166. }
  167. if (utf_cases[i].input16) {
  168. out_str.clear();
  169. StdStringCanonOutput output(&out_str);
  170. std::u16string input_str(
  171. test_utils::TruncateWStringToUTF16(utf_cases[i].input16));
  172. size_t input_len = input_str.length();
  173. bool success = true;
  174. for (size_t ch = 0; ch < input_len; ch++) {
  175. success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len,
  176. &output);
  177. }
  178. output.Complete();
  179. EXPECT_EQ(utf_cases[i].expected_success, success);
  180. EXPECT_EQ(std::string(utf_cases[i].output), out_str);
  181. }
  182. if (utf_cases[i].input8 && utf_cases[i].input16 &&
  183. utf_cases[i].expected_success) {
  184. // Check that the UTF-8 and UTF-16 inputs are equivalent.
  185. // UTF-16 -> UTF-8
  186. std::string input8_str(utf_cases[i].input8);
  187. std::u16string input16_str(
  188. test_utils::TruncateWStringToUTF16(utf_cases[i].input16));
  189. EXPECT_EQ(input8_str, base::UTF16ToUTF8(input16_str));
  190. // UTF-8 -> UTF-16
  191. EXPECT_EQ(input16_str, base::UTF8ToUTF16(input8_str));
  192. }
  193. }
  194. }
  195. TEST(URLCanonTest, Scheme) {
  196. // Here, we're mostly testing that unusual characters are handled properly.
  197. // The canonicalizer doesn't do any parsing or whitespace detection. It will
  198. // also do its best on error, and will escape funny sequences (these won't be
  199. // valid schemes and it will return error).
  200. //
  201. // Note that the canonicalizer will append a colon to the output to separate
  202. // out the rest of the URL, which is not present in the input. We check,
  203. // however, that the output range includes everything but the colon.
  204. ComponentCase scheme_cases[] = {
  205. {"http", "http:", Component(0, 4), true},
  206. {"HTTP", "http:", Component(0, 4), true},
  207. {" HTTP ", "%20http%20:", Component(0, 10), false},
  208. {"htt: ", "htt%3A%20:", Component(0, 9), false},
  209. {"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", Component(0, 22), false},
  210. // Don't re-escape something already escaped. Note that it will
  211. // "canonicalize" the 'A' to 'a', but that's OK.
  212. {"ht%3Atp", "ht%3atp:", Component(0, 7), false},
  213. {"", ":", Component(0, 0), false},
  214. };
  215. std::string out_str;
  216. for (size_t i = 0; i < std::size(scheme_cases); i++) {
  217. int url_len = static_cast<int>(strlen(scheme_cases[i].input));
  218. Component in_comp(0, url_len);
  219. Component out_comp;
  220. out_str.clear();
  221. StdStringCanonOutput output1(&out_str);
  222. bool success = CanonicalizeScheme(scheme_cases[i].input, in_comp, &output1,
  223. &out_comp);
  224. output1.Complete();
  225. EXPECT_EQ(scheme_cases[i].expected_success, success);
  226. EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
  227. EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
  228. EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
  229. // Now try the wide version.
  230. out_str.clear();
  231. StdStringCanonOutput output2(&out_str);
  232. std::u16string wide_input(base::UTF8ToUTF16(scheme_cases[i].input));
  233. in_comp.len = static_cast<int>(wide_input.length());
  234. success = CanonicalizeScheme(wide_input.c_str(), in_comp, &output2,
  235. &out_comp);
  236. output2.Complete();
  237. EXPECT_EQ(scheme_cases[i].expected_success, success);
  238. EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
  239. EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
  240. EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
  241. }
  242. // Test the case where the scheme is declared nonexistent, it should be
  243. // converted into an empty scheme.
  244. Component out_comp;
  245. out_str.clear();
  246. StdStringCanonOutput output(&out_str);
  247. EXPECT_FALSE(CanonicalizeScheme("", Component(0, -1), &output, &out_comp));
  248. output.Complete();
  249. EXPECT_EQ(std::string(":"), out_str);
  250. EXPECT_EQ(0, out_comp.begin);
  251. EXPECT_EQ(0, out_comp.len);
  252. }
  253. TEST(URLCanonTest, Host) {
  254. IPAddressCase host_cases[] = {
  255. // Basic canonicalization, uppercase should be converted to lowercase.
  256. {"GoOgLe.CoM", L"GoOgLe.CoM", "google.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
  257. // Spaces and some other characters should be escaped.
  258. {"Goo%20 goo%7C|.com", L"Goo%20 goo%7C|.com", "goo%20%20goo%7C%7C.com", Component(0, 22), CanonHostInfo::NEUTRAL, -1, ""},
  259. // Exciting different types of spaces!
  260. {NULL, L"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", Component(0, 16), CanonHostInfo::NEUTRAL, -1, ""},
  261. // Other types of space (no-break, zero-width, zero-width-no-break) are
  262. // name-prepped away to nothing.
  263. {NULL, L"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
  264. // Ideographic full stop (full-width period for Chinese, etc.) should be
  265. // treated as a dot.
  266. {NULL, L"www.foo\x3002" L"bar.com", "www.foo.bar.com", Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
  267. // Invalid unicode characters should fail...
  268. // ...In wide input, ICU will barf and we'll end up with the input as
  269. // escaped UTF-8 (the invalid character should be replaced with the
  270. // replacement character).
  271. {"\xef\xb7\x90zyx.com", L"\xfdd0zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
  272. // ...This is the same as previous but with with escaped.
  273. {"%ef%b7%90zyx.com", L"%ef%b7%90zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
  274. // Test name prepping, fullwidth input should be converted to ASCII and NOT
  275. // IDN-ized. This is "Go" in fullwidth UTF-8/UTF-16.
  276. {"\xef\xbc\xa7\xef\xbd\x8f.com", L"\xff27\xff4f.com", "go.com", Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
  277. // Test that fullwidth escaped values are properly name-prepped,
  278. // then converted or rejected.
  279. // ...%41 in fullwidth = 'A' (also as escaped UTF-8 input)
  280. {"\xef\xbc\x85\xef\xbc\x94\xef\xbc\x91.com", L"\xff05\xff14\xff11.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
  281. {"%ef%bc%85%ef%bc%94%ef%bc%91.com", L"%ef%bc%85%ef%bc%94%ef%bc%91.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
  282. // ...%00 in fullwidth should fail (also as escaped UTF-8 input)
  283. {"\xef\xbc\x85\xef\xbc\x90\xef\xbc\x90.com", L"\xff05\xff10\xff10.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
  284. {"%ef%bc%85%ef%bc%90%ef%bc%90.com", L"%ef%bc%85%ef%bc%90%ef%bc%90.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
  285. // ICU will convert weird percents into ASCII percents, but not unescape
  286. // further. A weird percent is U+FE6A (EF B9 AA in UTF-8) which is a
  287. // "small percent". At this point we should be within our rights to mark
  288. // anything as invalid since the URL is corrupt or malicious. The code
  289. // happens to allow ASCII characters (%41 = "A" -> 'a') to be unescaped
  290. // and kept as valid, so we validate that behavior here, but this level
  291. // of fixing the input shouldn't be seen as required. "%81" is invalid.
  292. {"\xef\xb9\xaa" "41.com", L"\xfe6a" L"41.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
  293. {"%ef%b9%aa" "41.com", L"\xfe6a" L"41.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
  294. {"\xef\xb9\xaa" "81.com", L"\xfe6a" L"81.com", "%81.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
  295. {"%ef%b9%aa" "81.com", L"\xfe6a" L"81.com", "%81.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
  296. // Basic IDN support, UTF-8 and UTF-16 input should be converted to IDN
  297. {"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d\x4f60\x597d", "xn--6qqa088eba", Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
  298. // See http://unicode.org/cldr/utility/idna.jsp for other
  299. // examples/experiments and http://goo.gl/7yG11o
  300. // for the full list of characters handled differently by
  301. // IDNA 2003, UTS 46 (http://unicode.org/reports/tr46/ ) and IDNA 2008.
  302. // 4 Deviation characters are mapped/ignored in UTS 46 transitional
  303. // mechansm. UTS 46, table 4 row (g).
  304. // Sharp-s is mapped to 'ss' in UTS 46 and IDNA 2003.
  305. // Otherwise, it'd be "xn--fuball-cta.de".
  306. {"fu\xc3\x9f" "ball.de", L"fu\x00df" L"ball.de", "fussball.de",
  307. Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
  308. // Final-sigma (U+03C3) is mapped to regular sigma (U+03C2).
  309. // Otherwise, it'd be "xn--wxaijb9b".
  310. {"\xcf\x83\xcf\x8c\xce\xbb\xce\xbf\xcf\x82", L"\x3c3\x3cc\x3bb\x3bf\x3c2",
  311. "xn--wxaikc6b", Component(0, 12),
  312. CanonHostInfo::NEUTRAL, -1, ""},
  313. // ZWNJ (U+200C) and ZWJ (U+200D) are mapped away in UTS 46 transitional
  314. // handling as well as in IDNA 2003.
  315. {"a\xe2\x80\x8c" "b\xe2\x80\x8d" "c", L"a\x200c" L"b\x200d" L"c", "abc",
  316. Component(0, 3), CanonHostInfo::NEUTRAL, -1, ""},
  317. // ZWJ between Devanagari characters is still mapped away in UTS 46
  318. // transitional handling. IDNA 2008 would give xn--11bo0mv54g.
  319. {"\xe0\xa4\x95\xe0\xa5\x8d\xe2\x80\x8d\xe0\xa4\x9c",
  320. L"\x915\x94d\x200d\x91c", "xn--11bo0m",
  321. Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
  322. // Fullwidth exclamation mark is disallowed. UTS 46, table 4, row (b)
  323. // However, we do allow this at the moment because we don't use
  324. // STD3 rules and canonicalize full-width ASCII to ASCII.
  325. {"wow\xef\xbc\x81", L"wow\xff01", "wow%21",
  326. Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
  327. // U+2132 (turned capital F) is disallowed. UTS 46, table 4, row (c)
  328. // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
  329. {"\xe2\x84\xb2oo", L"\x2132oo", "%E2%84%B2oo",
  330. Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
  331. // U+2F868 (CJK Comp) is disallowed. UTS 46, table 4, row (d)
  332. // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
  333. {"\xf0\xaf\xa1\xa8\xe5\xa7\xbb.cn", L"\xd87e\xdc68\x59fb.cn",
  334. "%F0%AF%A1%A8%E5%A7%BB.cn",
  335. Component(0, 24), CanonHostInfo::BROKEN, -1, ""},
  336. // Maps uppercase letters to lower case letters. UTS 46 table 4 row (e)
  337. {"M\xc3\x9cNCHEN", L"M\xdcNCHEN", "xn--mnchen-3ya",
  338. Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
  339. // An already-IDNA host is not modified.
  340. {"xn--mnchen-3ya", L"xn--mnchen-3ya", "xn--mnchen-3ya",
  341. Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
  342. // Symbol/punctuations are allowed in IDNA 2003/UTS46.
  343. // Not allowed in IDNA 2008. UTS 46 table 4 row (f).
  344. {"\xe2\x99\xa5ny.us", L"\x2665ny.us", "xn--ny-s0x.us",
  345. Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
  346. // U+11013 is new in Unicode 6.0 and is allowed. UTS 46 table 4, row (h)
  347. // We used to allow it because we passed through unassigned code points.
  348. {"\xf0\x91\x80\x93.com", L"\xd804\xdc13.com", "xn--n00d.com",
  349. Component(0, 12), CanonHostInfo::NEUTRAL, -1, ""},
  350. // U+0602 is disallowed in UTS46/IDNA 2008. UTS 46 table 4, row(i)
  351. // Used to be allowed in INDA 2003.
  352. {"\xd8\x82.eg", L"\x602.eg", "%D8%82.eg",
  353. Component(0, 9), CanonHostInfo::BROKEN, -1, ""},
  354. // U+20B7 is new in Unicode 5.2 (not a part of IDNA 2003 based
  355. // on Unicode 3.2). We did allow it in the past because we let unassigned
  356. // code point pass. We continue to allow it even though it's a
  357. // "punctuation and symbol" blocked in IDNA 2008.
  358. // UTS 46 table 4, row (j)
  359. {"\xe2\x82\xb7.com", L"\x20b7.com", "xn--wzg.com",
  360. Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
  361. // Maps uppercase letters to lower case letters.
  362. // In IDNA 2003, it's allowed without case-folding
  363. // ( xn--bc-7cb.com ) because it's not defined in Unicode 3.2
  364. // (added in Unicode 4.1). UTS 46 table 4 row (k)
  365. {"bc\xc8\xba.com", L"bc\x23a.com", "xn--bc-is1a.com",
  366. Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
  367. // Maps U+FF43 (Full Width Small Letter C) to 'c'.
  368. {"ab\xef\xbd\x83.xyz", L"ab\xff43.xyz", "abc.xyz",
  369. Component(0, 7), CanonHostInfo::NEUTRAL, -1, ""},
  370. // Maps U+1D68C (Math Monospace Small C) to 'c'.
  371. // U+1D68C = \xD835\xDE8C in UTF-16
  372. {"ab\xf0\x9d\x9a\x8c.xyz", L"ab\xd835\xde8c.xyz", "abc.xyz",
  373. Component(0, 7), CanonHostInfo::NEUTRAL, -1, ""},
  374. // BiDi check test
  375. // "Divehi" in Divehi (Thaana script) ends with BidiClass=NSM.
  376. // Disallowed in IDNA 2003 but now allowed in UTS 46/IDNA 2008.
  377. {"\xde\x8b\xde\xa8\xde\x88\xde\xac\xde\x80\xde\xa8",
  378. L"\x78b\x7a8\x788\x7ac\x780\x7a8", "xn--hqbpi0jcw",
  379. Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
  380. // Disallowed in both IDNA 2003 and 2008 with BiDi check.
  381. // Labels starting with a RTL character cannot end with a LTR character.
  382. {"\xd8\xac\xd8\xa7\xd8\xb1xyz", L"\x62c\x627\x631xyz",
  383. "%D8%AC%D8%A7%D8%B1xyz", Component(0, 21),
  384. CanonHostInfo::BROKEN, -1, ""},
  385. // Labels starting with a RTL character can end with BC=EN (European
  386. // number). Disallowed in IDNA 2003 but now allowed.
  387. {"\xd8\xac\xd8\xa7\xd8\xb1" "2", L"\x62c\x627\x631" L"2",
  388. "xn--2-ymcov", Component(0, 11),
  389. CanonHostInfo::NEUTRAL, -1, ""},
  390. // Labels starting with a RTL character cannot have "L" characters
  391. // even if it ends with an BC=EN. Disallowed in both IDNA 2003/2008.
  392. {"\xd8\xac\xd8\xa7\xd8\xb1xy2", L"\x62c\x627\x631xy2",
  393. "%D8%AC%D8%A7%D8%B1xy2", Component(0, 21),
  394. CanonHostInfo::BROKEN, -1, ""},
  395. // Labels starting with a RTL character can end with BC=AN (Arabic number)
  396. // Disallowed in IDNA 2003, but now allowed.
  397. {"\xd8\xac\xd8\xa7\xd8\xb1\xd9\xa2", L"\x62c\x627\x631\x662",
  398. "xn--mgbjq0r", Component(0, 11),
  399. CanonHostInfo::NEUTRAL, -1, ""},
  400. // Labels starting with a RTL character cannot have "L" characters
  401. // even if it ends with an BC=AN (Arabic number).
  402. // Disallowed in both IDNA 2003/2008.
  403. {"\xd8\xac\xd8\xa7\xd8\xb1xy\xd9\xa2", L"\x62c\x627\x631xy\x662",
  404. "%D8%AC%D8%A7%D8%B1xy%D9%A2", Component(0, 26),
  405. CanonHostInfo::BROKEN, -1, ""},
  406. // Labels starting with a RTL character cannot mix BC=EN and BC=AN
  407. {"\xd8\xac\xd8\xa7\xd8\xb1xy2\xd9\xa2", L"\x62c\x627\x631xy2\x662",
  408. "%D8%AC%D8%A7%D8%B1xy2%D9%A2", Component(0, 27),
  409. CanonHostInfo::BROKEN, -1, ""},
  410. // As of Unicode 6.2, U+20CF is not assigned. We do not allow it.
  411. {"\xe2\x83\x8f.com", L"\x20cf.com", "%E2%83%8F.com",
  412. Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
  413. // U+0080 is not allowed.
  414. {"\xc2\x80.com", L"\x80.com", "%C2%80.com",
  415. Component(0, 10), CanonHostInfo::BROKEN, -1, ""},
  416. // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
  417. // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
  418. // UTF-8 (wide case). The output should be equivalent to the true wide
  419. // character input above).
  420. {"%E4%BD%A0%E5%A5%BD\xe4\xbd\xa0\xe5\xa5\xbd",
  421. L"%E4%BD%A0%E5%A5%BD\x4f60\x597d", "xn--6qqa088eba",
  422. Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
  423. // Invalid escaped characters should fail and the percents should be
  424. // escaped.
  425. {"%zz%66%a", L"%zz%66%a", "%25zzf%25a", Component(0, 10),
  426. CanonHostInfo::BROKEN, -1, ""},
  427. // If we get an invalid character that has been escaped.
  428. {"%25", L"%25", "%25", Component(0, 3),
  429. CanonHostInfo::BROKEN, -1, ""},
  430. {"hello%00", L"hello%00", "hello%00", Component(0, 8),
  431. CanonHostInfo::BROKEN, -1, ""},
  432. // Escaped numbers should be treated like IP addresses if they are.
  433. {"%30%78%63%30%2e%30%32%35%30.01", L"%30%78%63%30%2e%30%32%35%30.01",
  434. "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
  435. "C0A80001"},
  436. {"%30%78%63%30%2e%30%32%35%30.01%2e", L"%30%78%63%30%2e%30%32%35%30.01%2e",
  437. "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
  438. "C0A80001"},
  439. // Invalid escaping should trigger the regular host error handling.
  440. {"%3g%78%63%30%2e%30%32%35%30%2E.01", L"%3g%78%63%30%2e%30%32%35%30%2E.01", "%253gxc0.0250..01", Component(0, 17), CanonHostInfo::BROKEN, -1, ""},
  441. // Something that isn't exactly an IP should get treated as a host and
  442. // spaces escaped.
  443. {"192.168.0.1 hello", L"192.168.0.1 hello", "192.168.0.1%20hello", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
  444. // Fullwidth and escaped UTF-8 fullwidth should still be treated as IP.
  445. // These are "0Xc0.0250.01" in fullwidth.
  446. {"\xef\xbc\x90%Ef%bc\xb8%ef%Bd%83\xef\xbc\x90%EF%BC%8E\xef\xbc\x90\xef\xbc\x92\xef\xbc\x95\xef\xbc\x90\xef\xbc%8E\xef\xbc\x90\xef\xbc\x91", L"\xff10\xff38\xff43\xff10\xff0e\xff10\xff12\xff15\xff10\xff0e\xff10\xff11", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
  447. // Broken IP addresses get marked as such.
  448. {"192.168.0.257", L"192.168.0.257", "192.168.0.257", Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
  449. {"[google.com]", L"[google.com]", "[google.com]", Component(0, 12), CanonHostInfo::BROKEN, -1, ""},
  450. // Cyrillic letter followed by '(' should return punycode for '(' escaped
  451. // before punycode string was created. I.e.
  452. // if '(' is escaped after punycode is created we would get xn--%28-8tb
  453. // (incorrect).
  454. {"\xd1\x82(", L"\x0442(", "xn--%28-7ed", Component(0, 11),
  455. CanonHostInfo::NEUTRAL, -1, ""},
  456. // Address with all hexidecimal characters with leading number of 1<<32
  457. // or greater and should return NEUTRAL rather than BROKEN if not all
  458. // components are numbers.
  459. {"12345678912345.de", L"12345678912345.de", "12345678912345.de", Component(0, 17), CanonHostInfo::NEUTRAL, -1, ""},
  460. {"1.12345678912345.de", L"1.12345678912345.de", "1.12345678912345.de", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
  461. {"12345678912345.12345678912345.de", L"12345678912345.12345678912345.de", "12345678912345.12345678912345.de", Component(0, 32), CanonHostInfo::NEUTRAL, -1, ""},
  462. {"1.2.0xB3A73CE5B59.de", L"1.2.0xB3A73CE5B59.de", "1.2.0xb3a73ce5b59.de", Component(0, 20), CanonHostInfo::NEUTRAL, -1, ""},
  463. {"12345678912345.0xde", L"12345678912345.0xde", "12345678912345.0xde", Component(0, 19), CanonHostInfo::BROKEN, -1, ""},
  464. // A label that starts with "xn--" but contains non-ASCII characters should
  465. // be an error. Escape the invalid characters.
  466. {"xn--m\xc3\xbcnchen", L"xn--m\xfcnchen", "xn--m%C3%BCnchen", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
  467. };
  468. // CanonicalizeHost() non-verbose.
  469. std::string out_str;
  470. for (size_t i = 0; i < std::size(host_cases); i++) {
  471. // Narrow version.
  472. if (host_cases[i].input8) {
  473. int host_len = static_cast<int>(strlen(host_cases[i].input8));
  474. Component in_comp(0, host_len);
  475. Component out_comp;
  476. out_str.clear();
  477. StdStringCanonOutput output(&out_str);
  478. bool success = CanonicalizeHost(host_cases[i].input8, in_comp, &output,
  479. &out_comp);
  480. output.Complete();
  481. EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
  482. success) << "for input: " << host_cases[i].input8;
  483. EXPECT_EQ(std::string(host_cases[i].expected), out_str) <<
  484. "for input: " << host_cases[i].input8;
  485. EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin) <<
  486. "for input: " << host_cases[i].input8;
  487. EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len) <<
  488. "for input: " << host_cases[i].input8;
  489. }
  490. // Wide version.
  491. if (host_cases[i].input16) {
  492. std::u16string input16(
  493. test_utils::TruncateWStringToUTF16(host_cases[i].input16));
  494. int host_len = static_cast<int>(input16.length());
  495. Component in_comp(0, host_len);
  496. Component out_comp;
  497. out_str.clear();
  498. StdStringCanonOutput output(&out_str);
  499. bool success = CanonicalizeHost(input16.c_str(), in_comp, &output,
  500. &out_comp);
  501. output.Complete();
  502. EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
  503. success);
  504. EXPECT_EQ(std::string(host_cases[i].expected), out_str);
  505. EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin);
  506. EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len);
  507. }
  508. }
  509. // CanonicalizeHostVerbose()
  510. for (size_t i = 0; i < std::size(host_cases); i++) {
  511. // Narrow version.
  512. if (host_cases[i].input8) {
  513. int host_len = static_cast<int>(strlen(host_cases[i].input8));
  514. Component in_comp(0, host_len);
  515. out_str.clear();
  516. StdStringCanonOutput output(&out_str);
  517. CanonHostInfo host_info;
  518. CanonicalizeHostVerbose(host_cases[i].input8, in_comp, &output,
  519. &host_info);
  520. output.Complete();
  521. EXPECT_EQ(host_cases[i].expected_family, host_info.family);
  522. EXPECT_EQ(std::string(host_cases[i].expected), out_str);
  523. EXPECT_EQ(host_cases[i].expected_component.begin,
  524. host_info.out_host.begin);
  525. EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
  526. EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
  527. BytesToHexString(host_info.address, host_info.AddressLength()));
  528. if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
  529. EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
  530. host_info.num_ipv4_components);
  531. }
  532. }
  533. // Wide version.
  534. if (host_cases[i].input16) {
  535. std::u16string input16(
  536. test_utils::TruncateWStringToUTF16(host_cases[i].input16));
  537. int host_len = static_cast<int>(input16.length());
  538. Component in_comp(0, host_len);
  539. out_str.clear();
  540. StdStringCanonOutput output(&out_str);
  541. CanonHostInfo host_info;
  542. CanonicalizeHostVerbose(input16.c_str(), in_comp, &output, &host_info);
  543. output.Complete();
  544. EXPECT_EQ(host_cases[i].expected_family, host_info.family);
  545. EXPECT_EQ(std::string(host_cases[i].expected), out_str);
  546. EXPECT_EQ(host_cases[i].expected_component.begin,
  547. host_info.out_host.begin);
  548. EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
  549. EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
  550. BytesToHexString(host_info.address, host_info.AddressLength()));
  551. if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
  552. EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
  553. host_info.num_ipv4_components);
  554. }
  555. }
  556. }
  557. }
  558. TEST(URLCanonTest, IPv4) {
  559. // clang-format off
  560. IPAddressCase cases[] = {
  561. // Empty is not an IP address.
  562. {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  563. {".", L".", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  564. // Regular IP addresses in different bases.
  565. {"192.168.0.1", L"192.168.0.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
  566. {"0300.0250.00.01", L"0300.0250.00.01", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
  567. {"0xC0.0Xa8.0x0.0x1", L"0xC0.0Xa8.0x0.0x1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
  568. // Non-IP addresses due to invalid characters.
  569. {"192.168.9.com", L"192.168.9.com", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  570. // Hostnames with a numeric final component but other components that don't
  571. // parse as numbers should be considered broken.
  572. {"19a.168.0.1", L"19a.168.0.1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  573. {"19a.168.0.1.", L"19a.168.0.1.", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  574. {"0308.0250.00.01", L"0308.0250.00.01", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  575. {"0308.0250.00.01.", L"0308.0250.00.01.", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  576. {"0xCG.0xA8.0x0.0x1", L"0xCG.0xA8.0x0.0x1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  577. {"0xCG.0xA8.0x0.0x1.", L"0xCG.0xA8.0x0.0x1.", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  578. // Non-numeric terminal compeonent should be considered not IPv4 hostnames, but valid.
  579. {"19.168.0.1a", L"19.168.0.1a", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  580. {"0xC.0xA8.0x0.0x1G", L"0xC.0xA8.0x0.0x1G", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  581. // Hostnames that would be considered broken IPv4 hostnames should be considered valid non-IPv4 hostnames if they end with two dots instead of 0 or 1.
  582. {"19a.168.0.1..", L"19a.168.0.1..", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  583. {"0308.0250.00.01..", L"0308.0250.00.01..", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  584. {"0xCG.0xA8.0x0.0x1..", L"0xCG.0xA8.0x0.0x1..", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  585. // Hosts with components that aren't considered valid IPv4 numbers but are entirely numeric should be considered invalid.
  586. {"1.2.3.08", L"1.2.3.08", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  587. {"1.2.3.08.", L"1.2.3.08.", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  588. // If there are not enough components, the last one should fill them out.
  589. {"192", L"192", "0.0.0.192", Component(0, 9), CanonHostInfo::IPV4, 1, "000000C0"},
  590. {"0xC0a80001", L"0xC0a80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
  591. {"030052000001", L"030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
  592. {"000030052000001", L"000030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
  593. {"192.168", L"192.168", "192.0.0.168", Component(0, 11), CanonHostInfo::IPV4, 2, "C00000A8"},
  594. {"192.0x00A80001", L"192.0x000A80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
  595. {"0xc0.052000001", L"0xc0.052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
  596. {"192.168.1", L"192.168.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
  597. // Hostnames with too many components, but a numeric final numeric component are invalid.
  598. {"192.168.0.0.1", L"192.168.0.0.1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  599. // We allow a single trailing dot.
  600. {"192.168.0.1.", L"192.168.0.1.", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
  601. {"192.168.0.1. hello", L"192.168.0.1. hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  602. {"192.168.0.1..", L"192.168.0.1..", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  603. // Hosts with two dots in a row with a final numeric component are considered invalid.
  604. {"192.168..1", L"192.168..1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  605. {"192.168..1.", L"192.168..1.", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  606. // Any numerical overflow should be marked as BROKEN.
  607. {"0x100.0", L"0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  608. {"0x100.0.0", L"0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  609. {"0x100.0.0.0", L"0x100.0.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  610. {"0.0x100.0.0", L"0.0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  611. {"0.0.0x100.0", L"0.0.0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  612. {"0.0.0.0x100", L"0.0.0.0x100", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  613. {"0.0.0x10000", L"0.0.0x10000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  614. {"0.0x1000000", L"0.0x1000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  615. {"0x100000000", L"0x100000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  616. // Repeat the previous tests, minus 1, to verify boundaries.
  617. {"0xFF.0", L"0xFF.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 2, "FF000000"},
  618. {"0xFF.0.0", L"0xFF.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 3, "FF000000"},
  619. {"0xFF.0.0.0", L"0xFF.0.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "FF000000"},
  620. {"0.0xFF.0.0", L"0.0xFF.0.0", "0.255.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "00FF0000"},
  621. {"0.0.0xFF.0", L"0.0.0xFF.0", "0.0.255.0", Component(0, 9), CanonHostInfo::IPV4, 4, "0000FF00"},
  622. {"0.0.0.0xFF", L"0.0.0.0xFF", "0.0.0.255", Component(0, 9), CanonHostInfo::IPV4, 4, "000000FF"},
  623. {"0.0.0xFFFF", L"0.0.0xFFFF", "0.0.255.255", Component(0, 11), CanonHostInfo::IPV4, 3, "0000FFFF"},
  624. {"0.0xFFFFFF", L"0.0xFFFFFF", "0.255.255.255", Component(0, 13), CanonHostInfo::IPV4, 2, "00FFFFFF"},
  625. {"0xFFFFFFFF", L"0xFFFFFFFF", "255.255.255.255", Component(0, 15), CanonHostInfo::IPV4, 1, "FFFFFFFF"},
  626. // Old trunctations tests. They're all "BROKEN" now.
  627. {"276.256.0xf1a2.077777", L"276.256.0xf1a2.077777", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  628. {"192.168.0.257", L"192.168.0.257", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  629. {"192.168.0xa20001", L"192.168.0xa20001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  630. {"192.015052000001", L"192.015052000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  631. {"0X12C0a80001", L"0X12C0a80001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  632. {"276.1.2", L"276.1.2", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  633. // Too many components should be rejected, in valid ranges or not.
  634. {"255.255.255.255.255", L"255.255.255.255.255", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  635. {"256.256.256.256.256", L"256.256.256.256.256", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  636. // Spaces should be rejected.
  637. {"192.168.0.1 hello", L"192.168.0.1 hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  638. // Very large numbers.
  639. {"0000000000000300.0x00000000000000fF.00000000000000001", L"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0FF0001"},
  640. {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
  641. // A number has no length limit, but long numbers can still overflow.
  642. {"00000000000000000001", L"00000000000000000001", "0.0.0.1", Component(0, 7), CanonHostInfo::IPV4, 1, "00000001"},
  643. {"0000000000000000100000000000000001", L"0000000000000000100000000000000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  644. // If a long component is non-numeric, it's a hostname, *not* a broken IP.
  645. {"0.0.0.000000000000000000z", L"0.0.0.000000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  646. {"0.0.0.100000000000000000z", L"0.0.0.100000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  647. // Truncation of all zeros should still result in 0.
  648. {"0.00.0x.0x0", L"0.00.0x.0x0", "0.0.0.0", Component(0, 7), CanonHostInfo::IPV4, 4, "00000000"},
  649. // Non-ASCII characters in final component should return NEUTRAL.
  650. {"1.2.3.\xF0\x9F\x92\xA9", L"1.2.3.\xD83D\xDCA9", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  651. {"1.2.3.4\xF0\x9F\x92\xA9", L"1.2.3.4\xD83D\xDCA9", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  652. {"1.2.3.0x\xF0\x9F\x92\xA9", L"1.2.3.0x\xD83D\xDCA9", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  653. {"1.2.3.0\xF0\x9F\x92\xA9", L"1.2.3.0\xD83D\xDCA9", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  654. // Non-ASCII characters in other components should result in broken IPs when final component is numeric.
  655. {"1.2.\xF0\x9F\x92\xA9.4", L"1.2.\xD83D\xDCA9.4", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  656. {"1.2.3\xF0\x9F\x92\xA9.4", L"1.2.3\xD83D\xDCA9.4", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  657. {"1.2.0x\xF0\x9F\x92\xA9.4", L"1.2.0x\xD83D\xDCA9.4", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  658. {"1.2.0\xF0\x9F\x92\xA9.4", L"1.2.0\xD83D\xDCA9.4", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  659. {"\xF0\x9F\x92\xA9.2.3.4", L"\xD83D\xDCA9.2.3.4", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  660. };
  661. // clang-format on
  662. for (const auto& test_case : cases) {
  663. SCOPED_TRACE(test_case.input8);
  664. // 8-bit version.
  665. Component component(0, static_cast<int>(strlen(test_case.input8)));
  666. std::string out_str1;
  667. StdStringCanonOutput output1(&out_str1);
  668. CanonHostInfo host_info;
  669. CanonicalizeIPAddress(test_case.input8, component, &output1, &host_info);
  670. output1.Complete();
  671. EXPECT_EQ(test_case.expected_family, host_info.family);
  672. EXPECT_EQ(std::string(test_case.expected_address_hex),
  673. BytesToHexString(host_info.address, host_info.AddressLength()));
  674. if (host_info.family == CanonHostInfo::IPV4) {
  675. EXPECT_STREQ(test_case.expected, out_str1.c_str());
  676. EXPECT_EQ(test_case.expected_component.begin, host_info.out_host.begin);
  677. EXPECT_EQ(test_case.expected_component.len, host_info.out_host.len);
  678. EXPECT_EQ(test_case.expected_num_ipv4_components,
  679. host_info.num_ipv4_components);
  680. }
  681. // 16-bit version.
  682. std::u16string input16(
  683. test_utils::TruncateWStringToUTF16(test_case.input16));
  684. component = Component(0, static_cast<int>(input16.length()));
  685. std::string out_str2;
  686. StdStringCanonOutput output2(&out_str2);
  687. CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
  688. output2.Complete();
  689. EXPECT_EQ(test_case.expected_family, host_info.family);
  690. EXPECT_EQ(std::string(test_case.expected_address_hex),
  691. BytesToHexString(host_info.address, host_info.AddressLength()));
  692. if (host_info.family == CanonHostInfo::IPV4) {
  693. EXPECT_STREQ(test_case.expected, out_str2.c_str());
  694. EXPECT_EQ(test_case.expected_component.begin, host_info.out_host.begin);
  695. EXPECT_EQ(test_case.expected_component.len, host_info.out_host.len);
  696. EXPECT_EQ(test_case.expected_num_ipv4_components,
  697. host_info.num_ipv4_components);
  698. }
  699. }
  700. }
  701. TEST(URLCanonTest, IPv6) {
  702. IPAddressCase cases[] = {
  703. // Empty is not an IP address.
  704. {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
  705. // Non-IPs with [:] characters are marked BROKEN.
  706. {":", L":", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  707. {"[", L"[", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  708. {"[:", L"[:", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  709. {"]", L"]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  710. {":]", L":]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  711. {"[]", L"[]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  712. {"[:]", L"[:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  713. // Regular IP address is invalid without bounding '[' and ']'.
  714. {"2001:db8::1", L"2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  715. {"[2001:db8::1", L"[2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  716. {"2001:db8::1]", L"2001:db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  717. // Regular IP addresses.
  718. {"[::]", L"[::]", "[::]", Component(0,4), CanonHostInfo::IPV6, -1, "00000000000000000000000000000000"},
  719. {"[::1]", L"[::1]", "[::1]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000001"},
  720. {"[1::]", L"[1::]", "[1::]", Component(0,5), CanonHostInfo::IPV6, -1, "00010000000000000000000000000000"},
  721. // Leading zeros should be stripped.
  722. {"[000:01:02:003:004:5:6:007]", L"[000:01:02:003:004:5:6:007]", "[0:1:2:3:4:5:6:7]", Component(0,17), CanonHostInfo::IPV6, -1, "00000001000200030004000500060007"},
  723. // Upper case letters should be lowercased.
  724. {"[A:b:c:DE:fF:0:1:aC]", L"[A:b:c:DE:fF:0:1:aC]", "[a:b:c:de:ff:0:1:ac]", Component(0,20), CanonHostInfo::IPV6, -1, "000A000B000C00DE00FF0000000100AC"},
  725. // The same address can be written with different contractions, but should
  726. // get canonicalized to the same thing.
  727. {"[1:0:0:2::3:0]", L"[1:0:0:2::3:0]", "[1::2:0:0:3:0]", Component(0,14), CanonHostInfo::IPV6, -1, "00010000000000020000000000030000"},
  728. {"[1::2:0:0:3:0]", L"[1::2:0:0:3:0]", "[1::2:0:0:3:0]", Component(0,14), CanonHostInfo::IPV6, -1, "00010000000000020000000000030000"},
  729. // Addresses with embedded IPv4.
  730. {"[::192.168.0.1]", L"[::192.168.0.1]", "[::c0a8:1]", Component(0,10), CanonHostInfo::IPV6, -1, "000000000000000000000000C0A80001"},
  731. {"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
  732. {"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "[::eeee:c0a8:1]", Component(0, 15), CanonHostInfo::IPV6, -1, "00000000000000000000EEEEC0A80001"},
  733. {"[2001::192.168.0.1]", L"[2001::192.168.0.1]", "[2001::c0a8:1]", Component(0, 14), CanonHostInfo::IPV6, -1, "200100000000000000000000C0A80001"},
  734. {"[1:2:192.168.0.1:5:6]", L"[1:2:192.168.0.1:5:6]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  735. // IPv4 with last component missing.
  736. {"[::ffff:192.1.2]", L"[::ffff:192.1.2]", "[::ffff:c001:2]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0010002"},
  737. // IPv4 using hex.
  738. // TODO(eroman): Should this format be disallowed?
  739. {"[::ffff:0xC0.0Xa8.0x0.0x1]", L"[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
  740. // There may be zeros surrounding the "::" contraction.
  741. {"[0:0::0:0:8]", L"[0:0::0:0:8]", "[::8]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000008"},
  742. {"[2001:db8::1]", L"[2001:db8::1]", "[2001:db8::1]", Component(0,13), CanonHostInfo::IPV6, -1, "20010DB8000000000000000000000001"},
  743. // Can only have one "::" contraction in an IPv6 string literal.
  744. {"[2001::db8::1]", L"[2001::db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  745. // No more than 2 consecutive ':'s.
  746. {"[2001:db8:::1]", L"[2001:db8:::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  747. {"[:::]", L"[:::]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  748. // Non-IP addresses due to invalid characters.
  749. {"[2001::.com]", L"[2001::.com]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  750. // If there are not enough components, the last one should fill them out.
  751. // ... omitted at this time ...
  752. // Too many components means not an IP address. Similarly, with too few
  753. // if using IPv4 compat or mapped addresses.
  754. {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  755. {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  756. {"[1:2:3:4:5:6:7:8:9]", L"[1:2:3:4:5:6:7:8:9]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  757. // Too many bits (even though 8 comonents, the last one holds 32 bits).
  758. {"[0:0:0:0:0:0:0:192.168.0.1]", L"[0:0:0:0:0:0:0:192.168.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  759. // Too many bits specified -- the contraction would have to be zero-length
  760. // to not exceed 128 bits.
  761. {"[1:2:3:4:5:6::192.168.0.1]", L"[1:2:3:4:5:6::192.168.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  762. // The contraction is for 16 bits of zero.
  763. {"[1:2:3:4:5:6::8]", L"[1:2:3:4:5:6::8]", "[1:2:3:4:5:6:0:8]", Component(0,17), CanonHostInfo::IPV6, -1, "00010002000300040005000600000008"},
  764. // Cannot have a trailing colon.
  765. {"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  766. {"[1:2:3:4:5:6:192.168.0.1:]", L"[1:2:3:4:5:6:192.168.0.1:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  767. // Cannot have negative numbers.
  768. {"[-1:2:3:4:5:6:7:8]", L"[-1:2:3:4:5:6:7:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  769. // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
  770. // The scope_id should be included in the canonicalized URL, and is an
  771. // unsigned decimal number.
  772. // Invalid because no ID was given after the percent.
  773. // Don't allow scope-id
  774. {"[1::%1]", L"[1::%1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  775. {"[1::%eth0]", L"[1::%eth0]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  776. {"[1::%]", L"[1::%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  777. {"[%]", L"[%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  778. {"[::%:]", L"[::%:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  779. // Don't allow leading or trailing colons.
  780. {"[:0:0::0:0:8]", L"[:0:0::0:0:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  781. {"[0:0::0:0:8:]", L"[0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  782. {"[:0:0::0:0:8:]", L"[:0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  783. // We allow a single trailing dot.
  784. // ... omitted at this time ...
  785. // Two dots in a row means not an IP address.
  786. {"[::192.168..1]", L"[::192.168..1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  787. // Any non-first components get truncated to one byte.
  788. // ... omitted at this time ...
  789. // Spaces should be rejected.
  790. {"[::1 hello]", L"[::1 hello]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
  791. };
  792. for (size_t i = 0; i < std::size(cases); i++) {
  793. // 8-bit version.
  794. Component component(0, static_cast<int>(strlen(cases[i].input8)));
  795. std::string out_str1;
  796. StdStringCanonOutput output1(&out_str1);
  797. CanonHostInfo host_info;
  798. CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
  799. output1.Complete();
  800. EXPECT_EQ(cases[i].expected_family, host_info.family);
  801. EXPECT_EQ(std::string(cases[i].expected_address_hex),
  802. BytesToHexString(host_info.address, host_info.AddressLength())) << "iter " << i << " host " << cases[i].input8;
  803. if (host_info.family == CanonHostInfo::IPV6) {
  804. EXPECT_STREQ(cases[i].expected, out_str1.c_str());
  805. EXPECT_EQ(cases[i].expected_component.begin,
  806. host_info.out_host.begin);
  807. EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
  808. }
  809. // 16-bit version.
  810. std::u16string input16(
  811. test_utils::TruncateWStringToUTF16(cases[i].input16));
  812. component = Component(0, static_cast<int>(input16.length()));
  813. std::string out_str2;
  814. StdStringCanonOutput output2(&out_str2);
  815. CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
  816. output2.Complete();
  817. EXPECT_EQ(cases[i].expected_family, host_info.family);
  818. EXPECT_EQ(std::string(cases[i].expected_address_hex),
  819. BytesToHexString(host_info.address, host_info.AddressLength()));
  820. if (host_info.family == CanonHostInfo::IPV6) {
  821. EXPECT_STREQ(cases[i].expected, out_str2.c_str());
  822. EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
  823. EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
  824. }
  825. }
  826. }
  827. TEST(URLCanonTest, IPEmpty) {
  828. std::string out_str1;
  829. StdStringCanonOutput output1(&out_str1);
  830. CanonHostInfo host_info;
  831. // This tests tests.
  832. const char spec[] = "192.168.0.1";
  833. CanonicalizeIPAddress(spec, Component(), &output1, &host_info);
  834. EXPECT_FALSE(host_info.IsIPAddress());
  835. CanonicalizeIPAddress(spec, Component(0, 0), &output1, &host_info);
  836. EXPECT_FALSE(host_info.IsIPAddress());
  837. }
  838. // Verifies that CanonicalizeHostSubstring produces the expected output and
  839. // does not "fix" IP addresses. Because this code is a subset of
  840. // CanonicalizeHost, the shared functionality is not tested.
  841. TEST(URLCanonTest, CanonicalizeHostSubstring) {
  842. // Basic sanity check.
  843. {
  844. std::string out_str;
  845. StdStringCanonOutput output(&out_str);
  846. EXPECT_TRUE(CanonicalizeHostSubstring("M\xc3\x9cNCHEN.com",
  847. Component(0, 12), &output));
  848. output.Complete();
  849. EXPECT_EQ("xn--mnchen-3ya.com", out_str);
  850. }
  851. // Failure case.
  852. {
  853. std::string out_str;
  854. StdStringCanonOutput output(&out_str);
  855. EXPECT_FALSE(CanonicalizeHostSubstring(
  856. test_utils::TruncateWStringToUTF16(L"\xfdd0zyx.com").c_str(),
  857. Component(0, 8), &output));
  858. output.Complete();
  859. EXPECT_EQ("%EF%BF%BDzyx.com", out_str);
  860. }
  861. // Should return true for empty input strings.
  862. {
  863. std::string out_str;
  864. StdStringCanonOutput output(&out_str);
  865. EXPECT_TRUE(CanonicalizeHostSubstring("", Component(0, 0), &output));
  866. output.Complete();
  867. EXPECT_EQ(std::string(), out_str);
  868. }
  869. // Numbers that look like IP addresses should not be changed.
  870. {
  871. std::string out_str;
  872. StdStringCanonOutput output(&out_str);
  873. EXPECT_TRUE(
  874. CanonicalizeHostSubstring("01.02.03.04", Component(0, 11), &output));
  875. output.Complete();
  876. EXPECT_EQ("01.02.03.04", out_str);
  877. }
  878. }
  879. TEST(URLCanonTest, UserInfo) {
  880. // Note that the canonicalizer should escape and treat empty components as
  881. // not being there.
  882. // We actually parse a full input URL so we can get the initial components.
  883. struct UserComponentCase {
  884. const char* input;
  885. const char* expected;
  886. Component expected_username;
  887. Component expected_password;
  888. bool expected_success;
  889. } user_info_cases[] = {
  890. {"http://user:pass@host.com/", "user:pass@", Component(0, 4), Component(5, 4), true},
  891. {"http://@host.com/", "", Component(0, -1), Component(0, -1), true},
  892. {"http://:@host.com/", "", Component(0, -1), Component(0, -1), true},
  893. {"http://foo:@host.com/", "foo@", Component(0, 3), Component(0, -1), true},
  894. {"http://:foo@host.com/", ":foo@", Component(0, 0), Component(1, 3), true},
  895. {"http://^ :$\t@host.com/", "%5E%20:$%09@", Component(0, 6), Component(7, 4), true},
  896. {"http://user:pass@/", "user:pass@", Component(0, 4), Component(5, 4), true},
  897. {"http://%2540:bar@domain.com/", "%2540:bar@", Component(0, 5), Component(6, 3), true },
  898. // IE7 compatibility: old versions allowed backslashes in usernames, but
  899. // IE7 does not. We disallow it as well.
  900. {"ftp://me\\mydomain:pass@foo.com/", "", Component(0, -1), Component(0, -1), true},
  901. };
  902. for (size_t i = 0; i < std::size(user_info_cases); i++) {
  903. int url_len = static_cast<int>(strlen(user_info_cases[i].input));
  904. Parsed parsed;
  905. ParseStandardURL(user_info_cases[i].input, url_len, &parsed);
  906. Component out_user, out_pass;
  907. std::string out_str;
  908. StdStringCanonOutput output1(&out_str);
  909. bool success = CanonicalizeUserInfo(user_info_cases[i].input,
  910. parsed.username,
  911. user_info_cases[i].input,
  912. parsed.password,
  913. &output1,
  914. &out_user,
  915. &out_pass);
  916. output1.Complete();
  917. EXPECT_EQ(user_info_cases[i].expected_success, success);
  918. EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
  919. EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
  920. EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
  921. EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
  922. EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
  923. // Now try the wide version
  924. out_str.clear();
  925. StdStringCanonOutput output2(&out_str);
  926. std::u16string wide_input(base::UTF8ToUTF16(user_info_cases[i].input));
  927. success = CanonicalizeUserInfo(wide_input.c_str(),
  928. parsed.username,
  929. wide_input.c_str(),
  930. parsed.password,
  931. &output2,
  932. &out_user,
  933. &out_pass);
  934. output2.Complete();
  935. EXPECT_EQ(user_info_cases[i].expected_success, success);
  936. EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
  937. EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
  938. EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
  939. EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
  940. EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
  941. }
  942. }
  943. TEST(URLCanonTest, Port) {
  944. // We only need to test that the number gets properly put into the output
  945. // buffer. The parser unit tests will test scanning the number correctly.
  946. //
  947. // Note that the CanonicalizePort will always prepend a colon to the output
  948. // to separate it from the colon that it assumes precedes it.
  949. struct PortCase {
  950. const char* input;
  951. int default_port;
  952. const char* expected;
  953. Component expected_component;
  954. bool expected_success;
  955. } port_cases[] = {
  956. // Invalid input should be copied w/ failure.
  957. {"as df", 80, ":as%20df", Component(1, 7), false},
  958. {"-2", 80, ":-2", Component(1, 2), false},
  959. // Default port should be omitted.
  960. {"80", 80, "", Component(0, -1), true},
  961. {"8080", 80, ":8080", Component(1, 4), true},
  962. // PORT_UNSPECIFIED should mean always keep the port.
  963. {"80", PORT_UNSPECIFIED, ":80", Component(1, 2), true},
  964. };
  965. for (size_t i = 0; i < std::size(port_cases); i++) {
  966. int url_len = static_cast<int>(strlen(port_cases[i].input));
  967. Component in_comp(0, url_len);
  968. Component out_comp;
  969. std::string out_str;
  970. StdStringCanonOutput output1(&out_str);
  971. bool success = CanonicalizePort(port_cases[i].input,
  972. in_comp,
  973. port_cases[i].default_port,
  974. &output1,
  975. &out_comp);
  976. output1.Complete();
  977. EXPECT_EQ(port_cases[i].expected_success, success);
  978. EXPECT_EQ(std::string(port_cases[i].expected), out_str);
  979. EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
  980. EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
  981. // Now try the wide version
  982. out_str.clear();
  983. StdStringCanonOutput output2(&out_str);
  984. std::u16string wide_input(base::UTF8ToUTF16(port_cases[i].input));
  985. success = CanonicalizePort(wide_input.c_str(),
  986. in_comp,
  987. port_cases[i].default_port,
  988. &output2,
  989. &out_comp);
  990. output2.Complete();
  991. EXPECT_EQ(port_cases[i].expected_success, success);
  992. EXPECT_EQ(std::string(port_cases[i].expected), out_str);
  993. EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
  994. EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
  995. }
  996. }
  997. DualComponentCase kCommonPathCases[] = {
  998. // ----- path collapsing tests -----
  999. {"/././foo", L"/././foo", "/foo", Component(0, 4), true},
  1000. {"/./.foo", L"/./.foo", "/.foo", Component(0, 5), true},
  1001. {"/foo/.", L"/foo/.", "/foo/", Component(0, 5), true},
  1002. {"/foo/./", L"/foo/./", "/foo/", Component(0, 5), true},
  1003. // double dots followed by a slash or the end of the string count
  1004. {"/foo/bar/..", L"/foo/bar/..", "/foo/", Component(0, 5), true},
  1005. {"/foo/bar/../", L"/foo/bar/../", "/foo/", Component(0, 5), true},
  1006. // don't count double dots when they aren't followed by a slash
  1007. {"/foo/..bar", L"/foo/..bar", "/foo/..bar", Component(0, 10), true},
  1008. // some in the middle
  1009. {"/foo/bar/../ton", L"/foo/bar/../ton", "/foo/ton", Component(0, 8), true},
  1010. {"/foo/bar/../ton/../../a", L"/foo/bar/../ton/../../a", "/a",
  1011. Component(0, 2), true},
  1012. // we should not be able to go above the root
  1013. {"/foo/../../..", L"/foo/../../..", "/", Component(0, 1), true},
  1014. {"/foo/../../../ton", L"/foo/../../../ton", "/ton", Component(0, 4), true},
  1015. // escaped dots should be unescaped and treated the same as dots
  1016. {"/foo/%2e", L"/foo/%2e", "/foo/", Component(0, 5), true},
  1017. {"/foo/%2e%2", L"/foo/%2e%2", "/foo/.%2", Component(0, 8), true},
  1018. {"/foo/%2e./%2e%2e/.%2e/%2e.bar", L"/foo/%2e./%2e%2e/.%2e/%2e.bar",
  1019. "/..bar", Component(0, 6), true},
  1020. // Multiple slashes in a row should be preserved and treated like empty
  1021. // directory names.
  1022. {"////../..", L"////../..", "//", Component(0, 2), true},
  1023. // ----- escaping tests -----
  1024. {"/foo", L"/foo", "/foo", Component(0, 4), true},
  1025. // Valid escape sequence
  1026. {"/%20foo", L"/%20foo", "/%20foo", Component(0, 7), true},
  1027. // Invalid escape sequence we should pass through unchanged.
  1028. {"/foo%", L"/foo%", "/foo%", Component(0, 5), true},
  1029. {"/foo%2", L"/foo%2", "/foo%2", Component(0, 6), true},
  1030. // Invalid escape sequence: bad characters should be treated the same as
  1031. // the surrounding text, not as escaped (in this case, UTF-8).
  1032. {"/foo%2zbar", L"/foo%2zbar", "/foo%2zbar", Component(0, 10), true},
  1033. {"/foo%2\xc2\xa9zbar", nullptr, "/foo%2%C2%A9zbar", Component(0, 16), true},
  1034. {nullptr, L"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", Component(0, 22),
  1035. true},
  1036. // Regular characters that are escaped should be unescaped
  1037. {"/foo%41%7a", L"/foo%41%7a", "/fooAz", Component(0, 6), true},
  1038. // Funny characters that are unescaped should be escaped
  1039. {"/foo\x09\x91%91", nullptr, "/foo%09%91%91", Component(0, 13), true},
  1040. {nullptr, L"/foo\x09\x91%91", "/foo%09%C2%91%91", Component(0, 16), true},
  1041. // Invalid characters that are escaped should cause a failure.
  1042. {"/foo%00%51", L"/foo%00%51", "/foo%00Q", Component(0, 8), false},
  1043. // Some characters should be passed through unchanged regardless of esc.
  1044. {"/(%28:%3A%29)", L"/(%28:%3A%29)", "/(%28:%3A%29)", Component(0, 13),
  1045. true},
  1046. // Characters that are properly escaped should not have the case changed
  1047. // of hex letters.
  1048. {"/%3A%3a%3C%3c", L"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", Component(0, 13),
  1049. true},
  1050. // Funny characters that are unescaped should be escaped
  1051. {"/foo\tbar", L"/foo\tbar", "/foo%09bar", Component(0, 10), true},
  1052. // Backslashes should get converted to forward slashes
  1053. {"\\foo\\bar", L"\\foo\\bar", "/foo/bar", Component(0, 8), true},
  1054. // Hashes found in paths (possibly only when the caller explicitly sets
  1055. // the path on an already-parsed URL) should be escaped.
  1056. {"/foo#bar", L"/foo#bar", "/foo%23bar", Component(0, 10), true},
  1057. // %7f should be allowed and %3D should not be unescaped (these were wrong
  1058. // in a previous version).
  1059. {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L"/%7Ffp3%3Eju%3Dduvgw%3Dd",
  1060. "/%7Ffp3%3Eju%3Dduvgw%3Dd", Component(0, 24), true},
  1061. // @ should be passed through unchanged (escaped or unescaped).
  1062. {"/@asdf%40", L"/@asdf%40", "/@asdf%40", Component(0, 9), true},
  1063. // Nested escape sequences should result in escaping the leading '%' if
  1064. // unescaping would result in a new escape sequence.
  1065. {"/%A%42", L"/%A%42", "/%25AB", Component(0, 6), true},
  1066. {"/%%41B", L"/%%41B", "/%25AB", Component(0, 6), true},
  1067. {"/%%41%42", L"/%%41%42", "/%25AB", Component(0, 6), true},
  1068. // Make sure truncated "nested" escapes don't result in reading off the
  1069. // string end.
  1070. {"/%%41", L"/%%41", "/%A", Component(0, 3), true},
  1071. // Don't unescape the leading '%' if unescaping doesn't result in a valid
  1072. // new escape sequence.
  1073. {"/%%470", L"/%%470", "/%G0", Component(0, 4), true},
  1074. {"/%%2D%41", L"/%%2D%41", "/%-A", Component(0, 4), true},
  1075. // Don't erroneously downcast a UTF-16 character in a way that makes it
  1076. // look like part of an escape sequence.
  1077. {nullptr, L"/%%41\x0130", "/%A%C4%B0", Component(0, 9), true},
  1078. // ----- encoding tests -----
  1079. // Basic conversions
  1080. {"/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd",
  1081. L"/\x4f60\x597d\x4f60\x597d", "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD",
  1082. Component(0, 37), true},
  1083. // Invalid unicode characters should fail. We only do validation on
  1084. // UTF-16 input, so this doesn't happen on 8-bit.
  1085. {"/\xef\xb7\x90zyx", nullptr, "/%EF%B7%90zyx", Component(0, 13), true},
  1086. {nullptr, L"/\xfdd0zyx", "/%EF%BF%BDzyx", Component(0, 13), false},
  1087. };
  1088. typedef bool (*CanonFunc8Bit)(const char*,
  1089. const Component&,
  1090. CanonOutput*,
  1091. Component*);
  1092. typedef bool (*CanonFunc16Bit)(const char16_t*,
  1093. const Component&,
  1094. CanonOutput*,
  1095. Component*);
  1096. void DoPathTest(const DualComponentCase* path_cases,
  1097. size_t num_cases,
  1098. CanonFunc8Bit canon_func_8,
  1099. CanonFunc16Bit canon_func_16) {
  1100. for (size_t i = 0; i < num_cases; i++) {
  1101. testing::Message scope_message;
  1102. scope_message << path_cases[i].input8 << "," << path_cases[i].input16;
  1103. SCOPED_TRACE(scope_message);
  1104. if (path_cases[i].input8) {
  1105. int len = static_cast<int>(strlen(path_cases[i].input8));
  1106. Component in_comp(0, len);
  1107. Component out_comp;
  1108. std::string out_str;
  1109. StdStringCanonOutput output(&out_str);
  1110. bool success =
  1111. canon_func_8(path_cases[i].input8, in_comp, &output, &out_comp);
  1112. output.Complete();
  1113. EXPECT_EQ(path_cases[i].expected_success, success);
  1114. EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
  1115. EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
  1116. EXPECT_EQ(path_cases[i].expected, out_str);
  1117. }
  1118. if (path_cases[i].input16) {
  1119. std::u16string input16(
  1120. test_utils::TruncateWStringToUTF16(path_cases[i].input16));
  1121. int len = static_cast<int>(input16.length());
  1122. Component in_comp(0, len);
  1123. Component out_comp;
  1124. std::string out_str;
  1125. StdStringCanonOutput output(&out_str);
  1126. bool success =
  1127. canon_func_16(input16.c_str(), in_comp, &output, &out_comp);
  1128. output.Complete();
  1129. EXPECT_EQ(path_cases[i].expected_success, success);
  1130. EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
  1131. EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
  1132. EXPECT_EQ(path_cases[i].expected, out_str);
  1133. }
  1134. }
  1135. }
  1136. TEST(URLCanonTest, Path) {
  1137. DoPathTest(kCommonPathCases, std::size(kCommonPathCases), CanonicalizePath,
  1138. CanonicalizePath);
  1139. // Manual test: embedded NULLs should be escaped and the URL should be marked
  1140. // as invalid.
  1141. const char path_with_null[] = "/ab\0c";
  1142. Component in_comp(0, 5);
  1143. Component out_comp;
  1144. std::string out_str;
  1145. StdStringCanonOutput output(&out_str);
  1146. bool success = CanonicalizePath(path_with_null, in_comp, &output, &out_comp);
  1147. output.Complete();
  1148. EXPECT_FALSE(success);
  1149. EXPECT_EQ("/ab%00c", out_str);
  1150. }
  1151. TEST(URLCanonTest, PartialPath) {
  1152. DualComponentCase partial_path_cases[] = {
  1153. {".html", L".html", ".html", Component(0, 5), true},
  1154. {"", L"", "", Component(0, 0), true},
  1155. };
  1156. DoPathTest(kCommonPathCases, std::size(kCommonPathCases),
  1157. CanonicalizePartialPath, CanonicalizePartialPath);
  1158. DoPathTest(partial_path_cases, std::size(partial_path_cases),
  1159. CanonicalizePartialPath, CanonicalizePartialPath);
  1160. }
  1161. TEST(URLCanonTest, Query) {
  1162. struct QueryCase {
  1163. const char* input8;
  1164. const wchar_t* input16;
  1165. const char* expected;
  1166. } query_cases[] = {
  1167. // Regular ASCII case.
  1168. {"foo=bar", L"foo=bar", "?foo=bar"},
  1169. // Allow question marks in the query without escaping
  1170. {"as?df", L"as?df", "?as?df"},
  1171. // Always escape '#' since it would mark the ref.
  1172. {"as#df", L"as#df", "?as%23df"},
  1173. // Escape some questionable 8-bit characters, but never unescape.
  1174. {"\x02hello\x7f bye", L"\x02hello\x7f bye", "?%02hello%7F%20bye"},
  1175. {"%40%41123", L"%40%41123", "?%40%41123"},
  1176. // Chinese input/output
  1177. {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", "?q=%E4%BD%A0%E5%A5%BD"},
  1178. // Invalid UTF-8/16 input should be replaced with invalid characters.
  1179. {"q=\xed\xed", L"q=\xd800\xd800", "?q=%EF%BF%BD%EF%BF%BD"},
  1180. // Don't allow < or > because sometimes they are used for XSS if the
  1181. // URL is echoed in content. Firefox does this, IE doesn't.
  1182. {"q=<asdf>", L"q=<asdf>", "?q=%3Casdf%3E"},
  1183. // Escape double quotemarks in the query.
  1184. {"q=\"asdf\"", L"q=\"asdf\"", "?q=%22asdf%22"},
  1185. };
  1186. for (size_t i = 0; i < std::size(query_cases); i++) {
  1187. Component out_comp;
  1188. if (query_cases[i].input8) {
  1189. int len = static_cast<int>(strlen(query_cases[i].input8));
  1190. Component in_comp(0, len);
  1191. std::string out_str;
  1192. StdStringCanonOutput output(&out_str);
  1193. CanonicalizeQuery(query_cases[i].input8, in_comp, NULL, &output,
  1194. &out_comp);
  1195. output.Complete();
  1196. EXPECT_EQ(query_cases[i].expected, out_str);
  1197. }
  1198. if (query_cases[i].input16) {
  1199. std::u16string input16(
  1200. test_utils::TruncateWStringToUTF16(query_cases[i].input16));
  1201. int len = static_cast<int>(input16.length());
  1202. Component in_comp(0, len);
  1203. std::string out_str;
  1204. StdStringCanonOutput output(&out_str);
  1205. CanonicalizeQuery(input16.c_str(), in_comp, NULL, &output, &out_comp);
  1206. output.Complete();
  1207. EXPECT_EQ(query_cases[i].expected, out_str);
  1208. }
  1209. }
  1210. // Extra test for input with embedded NULL;
  1211. std::string out_str;
  1212. StdStringCanonOutput output(&out_str);
  1213. Component out_comp;
  1214. CanonicalizeQuery("a \x00z\x01", Component(0, 5), NULL, &output, &out_comp);
  1215. output.Complete();
  1216. EXPECT_EQ("?a%20%00z%01", out_str);
  1217. }
  1218. TEST(URLCanonTest, Ref) {
  1219. // Refs are trivial, it just checks the encoding.
  1220. DualComponentCase ref_cases[] = {
  1221. {"hello!", L"hello!", "#hello!", Component(1, 6), true},
  1222. // We should escape spaces, double-quotes, angled braces, and backtics.
  1223. {"hello, world", L"hello, world", "#hello,%20world", Component(1, 14),
  1224. true},
  1225. {"hello,\"world", L"hello,\"world", "#hello,%22world", Component(1, 14),
  1226. true},
  1227. {"hello,<world", L"hello,<world", "#hello,%3Cworld", Component(1, 14),
  1228. true},
  1229. {"hello,>world", L"hello,>world", "#hello,%3Eworld", Component(1, 14),
  1230. true},
  1231. {"hello,`world", L"hello,`world", "#hello,%60world", Component(1, 14),
  1232. true},
  1233. // UTF-8/wide input should be preserved
  1234. {"\xc2\xa9", L"\xa9", "#%C2%A9", Component(1, 6), true},
  1235. // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
  1236. {"\xF0\x90\x8C\x80ss", L"\xd800\xdf00ss", "#%F0%90%8C%80ss",
  1237. Component(1, 14), true},
  1238. // Escaping should be preserved unchanged, even invalid ones
  1239. {"%41%a", L"%41%a", "#%41%a", Component(1, 5), true},
  1240. // Invalid UTF-8/16 input should be flagged and the input made valid
  1241. {"\xc2", nullptr, "#%EF%BF%BD", Component(1, 9), true},
  1242. {nullptr, L"\xd800\x597d", "#%EF%BF%BD%E5%A5%BD", Component(1, 18), true},
  1243. // Test a Unicode invalid character.
  1244. {"a\xef\xb7\x90", L"a\xfdd0", "#a%EF%BF%BD", Component(1, 10), true},
  1245. // Refs can have # signs and we should preserve them.
  1246. {"asdf#qwer", L"asdf#qwer", "#asdf#qwer", Component(1, 9), true},
  1247. {"#asdf", L"#asdf", "##asdf", Component(1, 5), true},
  1248. };
  1249. for (size_t i = 0; i < std::size(ref_cases); i++) {
  1250. // 8-bit input
  1251. if (ref_cases[i].input8) {
  1252. int len = static_cast<int>(strlen(ref_cases[i].input8));
  1253. Component in_comp(0, len);
  1254. Component out_comp;
  1255. std::string out_str;
  1256. StdStringCanonOutput output(&out_str);
  1257. CanonicalizeRef(ref_cases[i].input8, in_comp, &output, &out_comp);
  1258. output.Complete();
  1259. EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
  1260. EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
  1261. EXPECT_EQ(ref_cases[i].expected, out_str);
  1262. }
  1263. // 16-bit input
  1264. if (ref_cases[i].input16) {
  1265. std::u16string input16(
  1266. test_utils::TruncateWStringToUTF16(ref_cases[i].input16));
  1267. int len = static_cast<int>(input16.length());
  1268. Component in_comp(0, len);
  1269. Component out_comp;
  1270. std::string out_str;
  1271. StdStringCanonOutput output(&out_str);
  1272. CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp);
  1273. output.Complete();
  1274. EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
  1275. EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
  1276. EXPECT_EQ(ref_cases[i].expected, out_str);
  1277. }
  1278. }
  1279. // Try one with an embedded NULL. It should be stripped.
  1280. const char null_input[5] = "ab\x00z";
  1281. Component null_input_component(0, 4);
  1282. Component out_comp;
  1283. std::string out_str;
  1284. StdStringCanonOutput output(&out_str);
  1285. CanonicalizeRef(null_input, null_input_component, &output, &out_comp);
  1286. output.Complete();
  1287. EXPECT_EQ(1, out_comp.begin);
  1288. EXPECT_EQ(6, out_comp.len);
  1289. EXPECT_EQ("#ab%00z", out_str);
  1290. }
  1291. TEST(URLCanonTest, CanonicalizeStandardURL) {
  1292. // The individual component canonicalize tests should have caught the cases
  1293. // for each of those components. Here, we just need to test that the various
  1294. // parts are included or excluded properly, and have the correct separators.
  1295. struct URLCase {
  1296. const char* input;
  1297. const char* expected;
  1298. bool expected_success;
  1299. } cases[] = {
  1300. {"http://www.google.com/foo?bar=baz#",
  1301. "http://www.google.com/foo?bar=baz#", true},
  1302. {"http://[www.google.com]/", "http://[www.google.com]/", false},
  1303. {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#",
  1304. false},
  1305. {"http:////////user:@google.com:99?foo", "http://user@google.com:99/?foo",
  1306. true},
  1307. {"www.google.com", ":www.google.com/", false},
  1308. {"http://192.0x00A80001", "http://192.168.0.1/", true},
  1309. {"http://www/foo%2Ehtml", "http://www/foo.html", true},
  1310. {"http://user:pass@/", "http://user:pass@/", false},
  1311. {"http://%25DOMAIN:foobar@foodomain.com/",
  1312. "http://%25DOMAIN:foobar@foodomain.com/", true},
  1313. // Backslashes should get converted to forward slashes.
  1314. {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
  1315. // Busted refs shouldn't make the whole thing fail.
  1316. {"http://www.google.com/asdf#\xc2",
  1317. "http://www.google.com/asdf#%EF%BF%BD", true},
  1318. // Basic port tests.
  1319. {"http://foo:80/", "http://foo/", true},
  1320. {"http://foo:81/", "http://foo:81/", true},
  1321. {"httpa://foo:80/", "httpa://foo:80/", true},
  1322. {"http://foo:-80/", "http://foo:-80/", false},
  1323. {"https://foo:443/", "https://foo/", true},
  1324. {"https://foo:80/", "https://foo:80/", true},
  1325. {"ftp://foo:21/", "ftp://foo/", true},
  1326. {"ftp://foo:80/", "ftp://foo:80/", true},
  1327. {"gopher://foo:70/", "gopher://foo:70/", true},
  1328. {"gopher://foo:443/", "gopher://foo:443/", true},
  1329. {"ws://foo:80/", "ws://foo/", true},
  1330. {"ws://foo:81/", "ws://foo:81/", true},
  1331. {"ws://foo:443/", "ws://foo:443/", true},
  1332. {"ws://foo:815/", "ws://foo:815/", true},
  1333. {"wss://foo:80/", "wss://foo:80/", true},
  1334. {"wss://foo:81/", "wss://foo:81/", true},
  1335. {"wss://foo:443/", "wss://foo/", true},
  1336. {"wss://foo:815/", "wss://foo:815/", true},
  1337. // This particular code path ends up "backing up" to replace an invalid
  1338. // host ICU generated with an escaped version. Test that in the context
  1339. // of a full URL to make sure the backing up doesn't mess up the non-host
  1340. // parts of the URL. "EF B9 AA" is U+FE6A which is a type of percent that
  1341. // ICU will convert to an ASCII one, generating "%81".
  1342. {"ws:)W\x1eW\xef\xb9\xaa"
  1343. "81:80/",
  1344. "ws://%29w%1ew%81/", false},
  1345. // Regression test for the last_invalid_percent_index bug described in
  1346. // https://crbug.com/1080890#c10.
  1347. {R"(HTTP:S/5%\../>%41)", "http://s/%3EA", true},
  1348. };
  1349. for (size_t i = 0; i < std::size(cases); i++) {
  1350. int url_len = static_cast<int>(strlen(cases[i].input));
  1351. Parsed parsed;
  1352. ParseStandardURL(cases[i].input, url_len, &parsed);
  1353. Parsed out_parsed;
  1354. std::string out_str;
  1355. StdStringCanonOutput output(&out_str);
  1356. bool success = CanonicalizeStandardURL(
  1357. cases[i].input, url_len, parsed,
  1358. SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, NULL, &output, &out_parsed);
  1359. output.Complete();
  1360. EXPECT_EQ(cases[i].expected_success, success);
  1361. EXPECT_EQ(cases[i].expected, out_str);
  1362. }
  1363. }
  1364. // The codepath here is the same as for regular canonicalization, so we just
  1365. // need to test that things are replaced or not correctly.
  1366. TEST(URLCanonTest, ReplaceStandardURL) {
  1367. ReplaceCase replace_cases[] = {
  1368. // Common case of truncating the path.
  1369. {"http://www.google.com/foo?bar=baz#ref", nullptr, nullptr, nullptr,
  1370. nullptr, nullptr, "/", kDeleteComp, kDeleteComp,
  1371. "http://www.google.com/"},
  1372. // Replace everything
  1373. {"http://a:b@google.com:22/foo;bar?baz@cat", "https", "me", "pw",
  1374. "host.com", "99", "/path", "query", "ref",
  1375. "https://me:pw@host.com:99/path?query#ref"},
  1376. // Replace nothing
  1377. {"http://a:b@google.com:22/foo?baz@cat", nullptr, nullptr, nullptr,
  1378. nullptr, nullptr, nullptr, nullptr, nullptr,
  1379. "http://a:b@google.com:22/foo?baz@cat"},
  1380. // Replace scheme with filesystem. The result is garbage, but you asked
  1381. // for it.
  1382. {"http://a:b@google.com:22/foo?baz@cat", "filesystem", nullptr, nullptr,
  1383. nullptr, nullptr, nullptr, nullptr, nullptr,
  1384. "filesystem://a:b@google.com:22/foo?baz@cat"},
  1385. };
  1386. for (size_t i = 0; i < std::size(replace_cases); i++) {
  1387. const ReplaceCase& cur = replace_cases[i];
  1388. int base_len = static_cast<int>(strlen(cur.base));
  1389. Parsed parsed;
  1390. ParseStandardURL(cur.base, base_len, &parsed);
  1391. Replacements<char> r;
  1392. typedef Replacements<char> R; // Clean up syntax.
  1393. // Note that for the scheme we pass in a different clear function since
  1394. // there is no function to clear the scheme.
  1395. SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
  1396. SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
  1397. SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
  1398. SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
  1399. SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
  1400. SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
  1401. SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
  1402. SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
  1403. std::string out_str;
  1404. StdStringCanonOutput output(&out_str);
  1405. Parsed out_parsed;
  1406. ReplaceStandardURL(replace_cases[i].base, parsed, r,
  1407. SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, NULL,
  1408. &output, &out_parsed);
  1409. output.Complete();
  1410. EXPECT_EQ(replace_cases[i].expected, out_str);
  1411. }
  1412. // The path pointer should be ignored if the address is invalid.
  1413. {
  1414. const char src[] = "http://www.google.com/here_is_the_path";
  1415. int src_len = static_cast<int>(strlen(src));
  1416. Parsed parsed;
  1417. ParseStandardURL(src, src_len, &parsed);
  1418. // Replace the path to 0 length string. By using 1 as the string address,
  1419. // the test should get an access violation if it tries to dereference it.
  1420. Replacements<char> r;
  1421. r.SetPath(reinterpret_cast<char*>(0x00000001), Component(0, 0));
  1422. std::string out_str1;
  1423. StdStringCanonOutput output1(&out_str1);
  1424. Parsed new_parsed;
  1425. ReplaceStandardURL(src, parsed, r,
  1426. SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, NULL,
  1427. &output1, &new_parsed);
  1428. output1.Complete();
  1429. EXPECT_STREQ("http://www.google.com/", out_str1.c_str());
  1430. // Same with an "invalid" path.
  1431. r.SetPath(reinterpret_cast<char*>(0x00000001), Component());
  1432. std::string out_str2;
  1433. StdStringCanonOutput output2(&out_str2);
  1434. ReplaceStandardURL(src, parsed, r,
  1435. SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, NULL,
  1436. &output2, &new_parsed);
  1437. output2.Complete();
  1438. EXPECT_STREQ("http://www.google.com/", out_str2.c_str());
  1439. }
  1440. }
  1441. TEST(URLCanonTest, ReplaceFileURL) {
  1442. ReplaceCase replace_cases[] = {
  1443. // Replace everything
  1444. {"file:///C:/gaba?query#ref", nullptr, nullptr, nullptr, "filer", nullptr,
  1445. "/foo", "b", "c", "file://filer/foo?b#c"},
  1446. // Replace nothing
  1447. {"file:///C:/gaba?query#ref", nullptr, nullptr, nullptr, nullptr, nullptr,
  1448. nullptr, nullptr, nullptr, "file:///C:/gaba?query#ref"},
  1449. {"file:///Y:", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  1450. nullptr, nullptr, "file:///Y:"},
  1451. {"file:///Y:/", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  1452. nullptr, nullptr, "file:///Y:/"},
  1453. {"file:///./Y", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  1454. nullptr, nullptr, "file:///Y"},
  1455. {"file:///./Y:", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  1456. nullptr, nullptr, "file:///Y:"},
  1457. // Clear non-path components (common)
  1458. {"file:///C:/gaba?query#ref", nullptr, nullptr, nullptr, nullptr, nullptr,
  1459. nullptr, kDeleteComp, kDeleteComp, "file:///C:/gaba"},
  1460. // Replace path with something that doesn't begin with a slash and make
  1461. // sure it gets added properly.
  1462. {"file:///C:/gaba", nullptr, nullptr, nullptr, nullptr, nullptr,
  1463. "interesting/", nullptr, nullptr, "file:///interesting/"},
  1464. {"file:///home/gaba?query#ref", nullptr, nullptr, nullptr, "filer",
  1465. nullptr, "/foo", "b", "c", "file://filer/foo?b#c"},
  1466. {"file:///home/gaba?query#ref", nullptr, nullptr, nullptr, nullptr,
  1467. nullptr, nullptr, nullptr, nullptr, "file:///home/gaba?query#ref"},
  1468. {"file:///home/gaba?query#ref", nullptr, nullptr, nullptr, nullptr,
  1469. nullptr, nullptr, kDeleteComp, kDeleteComp, "file:///home/gaba"},
  1470. {"file:///home/gaba", nullptr, nullptr, nullptr, nullptr, nullptr,
  1471. "interesting/", nullptr, nullptr, "file:///interesting/"},
  1472. // Replace scheme -- shouldn't do anything.
  1473. {"file:///C:/gaba?query#ref", "http", nullptr, nullptr, nullptr, nullptr,
  1474. nullptr, nullptr, nullptr, "file:///C:/gaba?query#ref"},
  1475. };
  1476. for (size_t i = 0; i < std::size(replace_cases); i++) {
  1477. const ReplaceCase& cur = replace_cases[i];
  1478. SCOPED_TRACE(cur.base);
  1479. int base_len = static_cast<int>(strlen(cur.base));
  1480. Parsed parsed;
  1481. ParseFileURL(cur.base, base_len, &parsed);
  1482. Replacements<char> r;
  1483. typedef Replacements<char> R; // Clean up syntax.
  1484. SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
  1485. SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
  1486. SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
  1487. SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
  1488. SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
  1489. SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
  1490. SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
  1491. SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
  1492. std::string out_str;
  1493. StdStringCanonOutput output(&out_str);
  1494. Parsed out_parsed;
  1495. ReplaceFileURL(cur.base, parsed, r, NULL, &output, &out_parsed);
  1496. output.Complete();
  1497. EXPECT_EQ(replace_cases[i].expected, out_str);
  1498. }
  1499. }
  1500. TEST(URLCanonTest, ReplaceFileSystemURL) {
  1501. ReplaceCase replace_cases[] = {
  1502. // Replace everything in the outer URL.
  1503. {"filesystem:file:///temporary/gaba?query#ref", nullptr, nullptr, nullptr,
  1504. nullptr, nullptr, "/foo", "b", "c",
  1505. "filesystem:file:///temporary/foo?b#c"},
  1506. // Replace nothing
  1507. {"filesystem:file:///temporary/gaba?query#ref", nullptr, nullptr, nullptr,
  1508. nullptr, nullptr, nullptr, nullptr, nullptr,
  1509. "filesystem:file:///temporary/gaba?query#ref"},
  1510. // Clear non-path components (common)
  1511. {"filesystem:file:///temporary/gaba?query#ref", nullptr, nullptr, nullptr,
  1512. nullptr, nullptr, nullptr, kDeleteComp, kDeleteComp,
  1513. "filesystem:file:///temporary/gaba"},
  1514. // Replace path with something that doesn't begin with a slash and make
  1515. // sure it gets added properly.
  1516. {"filesystem:file:///temporary/gaba?query#ref", nullptr, nullptr, nullptr,
  1517. nullptr, nullptr, "interesting/", nullptr, nullptr,
  1518. "filesystem:file:///temporary/interesting/?query#ref"},
  1519. // Replace scheme -- shouldn't do anything except canonicalize.
  1520. {"filesystem:http://u:p@bar.com/t/gaba?query#ref", "http", nullptr,
  1521. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  1522. "filesystem:http://bar.com/t/gaba?query#ref"},
  1523. // Replace username -- shouldn't do anything except canonicalize.
  1524. {"filesystem:http://u:p@bar.com/t/gaba?query#ref", nullptr, "u2", nullptr,
  1525. nullptr, nullptr, nullptr, nullptr, nullptr,
  1526. "filesystem:http://bar.com/t/gaba?query#ref"},
  1527. // Replace password -- shouldn't do anything except canonicalize.
  1528. {"filesystem:http://u:p@bar.com/t/gaba?query#ref", nullptr, nullptr,
  1529. "pw2", nullptr, nullptr, nullptr, nullptr, nullptr,
  1530. "filesystem:http://bar.com/t/gaba?query#ref"},
  1531. // Replace host -- shouldn't do anything except canonicalize.
  1532. {"filesystem:http://u:p@bar.com:80/t/gaba?query#ref", nullptr, nullptr,
  1533. nullptr, "foo.com", nullptr, nullptr, nullptr, nullptr,
  1534. "filesystem:http://bar.com/t/gaba?query#ref"},
  1535. // Replace port -- shouldn't do anything except canonicalize.
  1536. {"filesystem:http://u:p@bar.com:40/t/gaba?query#ref", nullptr, nullptr,
  1537. nullptr, nullptr, "41", nullptr, nullptr, nullptr,
  1538. "filesystem:http://bar.com:40/t/gaba?query#ref"},
  1539. };
  1540. for (size_t i = 0; i < std::size(replace_cases); i++) {
  1541. const ReplaceCase& cur = replace_cases[i];
  1542. int base_len = static_cast<int>(strlen(cur.base));
  1543. Parsed parsed;
  1544. ParseFileSystemURL(cur.base, base_len, &parsed);
  1545. Replacements<char> r;
  1546. typedef Replacements<char> R; // Clean up syntax.
  1547. SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
  1548. SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
  1549. SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
  1550. SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
  1551. SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
  1552. SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
  1553. SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
  1554. SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
  1555. std::string out_str;
  1556. StdStringCanonOutput output(&out_str);
  1557. Parsed out_parsed;
  1558. ReplaceFileSystemURL(cur.base, parsed, r, NULL, &output, &out_parsed);
  1559. output.Complete();
  1560. EXPECT_EQ(replace_cases[i].expected, out_str);
  1561. }
  1562. }
  1563. TEST(URLCanonTest, ReplacePathURL) {
  1564. ReplaceCase replace_cases[] = {
  1565. // Replace everything
  1566. {"data:foo", "javascript", nullptr, nullptr, nullptr, nullptr,
  1567. "alert('foo?');", nullptr, nullptr, "javascript:alert('foo?');"},
  1568. // Replace nothing
  1569. {"data:foo", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  1570. nullptr, nullptr, "data:foo"},
  1571. // Replace one or the other
  1572. {"data:foo", "javascript", nullptr, nullptr, nullptr, nullptr, nullptr,
  1573. nullptr, nullptr, "javascript:foo"},
  1574. {"data:foo", nullptr, nullptr, nullptr, nullptr, nullptr, "bar", nullptr,
  1575. nullptr, "data:bar"},
  1576. {"data:foo", nullptr, nullptr, nullptr, nullptr, nullptr, kDeleteComp,
  1577. nullptr, nullptr, "data:"},
  1578. };
  1579. for (size_t i = 0; i < std::size(replace_cases); i++) {
  1580. const ReplaceCase& cur = replace_cases[i];
  1581. int base_len = static_cast<int>(strlen(cur.base));
  1582. Parsed parsed;
  1583. ParsePathURL(cur.base, base_len, false, &parsed);
  1584. Replacements<char> r;
  1585. typedef Replacements<char> R; // Clean up syntax.
  1586. SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
  1587. SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
  1588. SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
  1589. SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
  1590. SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
  1591. SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
  1592. SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
  1593. SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
  1594. std::string out_str;
  1595. StdStringCanonOutput output(&out_str);
  1596. Parsed out_parsed;
  1597. ReplacePathURL(cur.base, parsed, r, &output, &out_parsed);
  1598. output.Complete();
  1599. EXPECT_EQ(replace_cases[i].expected, out_str);
  1600. }
  1601. }
  1602. TEST(URLCanonTest, ReplaceMailtoURL) {
  1603. ReplaceCase replace_cases[] = {
  1604. // Replace everything
  1605. {"mailto:jon@foo.com?body=sup", "mailto", NULL, NULL, NULL, NULL, "addr1", "to=tony", NULL, "mailto:addr1?to=tony"},
  1606. // Replace nothing
  1607. {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "mailto:jon@foo.com?body=sup"},
  1608. // Replace the path
  1609. {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", NULL, NULL, "mailto:jason?body=sup"},
  1610. // Replace the query
  1611. {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "custom=1", NULL, "mailto:jon@foo.com?custom=1"},
  1612. // Replace the path and query
  1613. {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", "custom=1", NULL, "mailto:jason?custom=1"},
  1614. // Set the query to empty (should leave trailing question mark)
  1615. {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "", NULL, "mailto:jon@foo.com?"},
  1616. // Clear the query
  1617. {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "|", NULL, "mailto:jon@foo.com"},
  1618. // Clear the path
  1619. {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "|", NULL, NULL, "mailto:?body=sup"},
  1620. // Clear the path + query
  1621. {"mailto:", NULL, NULL, NULL, NULL, NULL, "|", "|", NULL, "mailto:"},
  1622. // Setting the ref should have no effect
  1623. {"mailto:addr1", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "BLAH", "mailto:addr1"},
  1624. };
  1625. for (size_t i = 0; i < std::size(replace_cases); i++) {
  1626. const ReplaceCase& cur = replace_cases[i];
  1627. int base_len = static_cast<int>(strlen(cur.base));
  1628. Parsed parsed;
  1629. ParseMailtoURL(cur.base, base_len, &parsed);
  1630. Replacements<char> r;
  1631. typedef Replacements<char> R;
  1632. SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
  1633. SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
  1634. SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
  1635. SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
  1636. SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
  1637. SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
  1638. SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
  1639. SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
  1640. std::string out_str;
  1641. StdStringCanonOutput output(&out_str);
  1642. Parsed out_parsed;
  1643. ReplaceMailtoURL(cur.base, parsed, r, &output, &out_parsed);
  1644. output.Complete();
  1645. EXPECT_EQ(replace_cases[i].expected, out_str);
  1646. }
  1647. }
  1648. TEST(URLCanonTest, CanonicalizeFileURL) {
  1649. struct URLCase {
  1650. const char* input;
  1651. const char* expected;
  1652. bool expected_success;
  1653. Component expected_host;
  1654. Component expected_path;
  1655. } cases[] = {
  1656. #ifdef _WIN32
  1657. // Windows-style paths
  1658. {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, Component(),
  1659. Component(7, 16)},
  1660. {" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true,
  1661. Component(), Component(7, 19)},
  1662. {"file:", "file:///", true, Component(), Component(7, 1)},
  1663. {"file:UNChost/path", "file://unchost/path", true, Component(7, 7),
  1664. Component(14, 5)},
  1665. // CanonicalizeFileURL supports absolute Windows style paths for IE
  1666. // compatibility. Note that the caller must decide that this is a file
  1667. // URL itself so it can call the file canonicalizer. This is usually
  1668. // done automatically as part of relative URL resolving.
  1669. {"c:\\foo\\bar", "file:///C:/foo/bar", true, Component(),
  1670. Component(7, 11)},
  1671. {"C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
  1672. {"/C|\\foo\\bar", "file:///C:/foo/bar", true, Component(),
  1673. Component(7, 11)},
  1674. {"//C|/foo/bar", "file:///C:/foo/bar", true, Component(),
  1675. Component(7, 11)},
  1676. {"//server/file", "file://server/file", true, Component(7, 6),
  1677. Component(13, 5)},
  1678. {"\\\\server\\file", "file://server/file", true, Component(7, 6),
  1679. Component(13, 5)},
  1680. {"/\\server/file", "file://server/file", true, Component(7, 6),
  1681. Component(13, 5)},
  1682. // We should preserve the number of slashes after the colon for IE
  1683. // compatibility, except when there is none, in which case we should
  1684. // add one.
  1685. {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, Component(),
  1686. Component(7, 16)},
  1687. {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true,
  1688. Component(), Component(7, 19)},
  1689. // Three slashes should be non-UNC, even if there is no drive spec (IE
  1690. // does this, which makes the resulting request invalid).
  1691. {"file:///foo/bar.txt", "file:///foo/bar.txt", true, Component(),
  1692. Component(7, 12)},
  1693. // TODO(brettw) we should probably fail for invalid host names, which
  1694. // would change the expected result on this test. We also currently allow
  1695. // colon even though it's probably invalid, because its currently the
  1696. // "natural" result of the way the canonicalizer is written. There doesn't
  1697. // seem to be a strong argument for why allowing it here would be bad, so
  1698. // we just tolerate it and the load will fail later.
  1699. {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false,
  1700. Component(7, 2), Component(9, 16)},
  1701. {"file:filer/home\\me", "file://filer/home/me", true, Component(7, 5),
  1702. Component(12, 8)},
  1703. // Make sure relative paths can't go above the "C:"
  1704. {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true,
  1705. Component(), Component(7, 12)},
  1706. // Busted refs shouldn't make the whole thing fail.
  1707. {"file:///C:/asdf#\xc2", "file:///C:/asdf#%EF%BF%BD", true, Component(),
  1708. Component(7, 8)},
  1709. {"file:///./s:", "file:///S:", true, Component(), Component(7, 3)},
  1710. #else
  1711. // Unix-style paths
  1712. {"file:///home/me", "file:///home/me", true, Component(),
  1713. Component(7, 8)},
  1714. // Windowsy ones should get still treated as Unix-style.
  1715. {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, Component(),
  1716. Component(7, 16)},
  1717. {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true,
  1718. Component(), Component(7, 19)},
  1719. {"file:///./s:", "file:///s:", true, Component(), Component(7, 3)},
  1720. // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html)
  1721. {"//", "file:///", true, Component(), Component(7, 1)},
  1722. {"///", "file:///", true, Component(), Component(7, 1)},
  1723. {"///test", "file:///test", true, Component(), Component(7, 5)},
  1724. {"file://test", "file://test/", true, Component(7, 4), Component(11, 1)},
  1725. {"file://localhost", "file://localhost/", true, Component(7, 9),
  1726. Component(16, 1)},
  1727. {"file://localhost/", "file://localhost/", true, Component(7, 9),
  1728. Component(16, 1)},
  1729. {"file://localhost/test", "file://localhost/test", true, Component(7, 9),
  1730. Component(16, 5)},
  1731. #endif // _WIN32
  1732. };
  1733. for (size_t i = 0; i < std::size(cases); i++) {
  1734. int url_len = static_cast<int>(strlen(cases[i].input));
  1735. Parsed parsed;
  1736. ParseFileURL(cases[i].input, url_len, &parsed);
  1737. Parsed out_parsed;
  1738. std::string out_str;
  1739. StdStringCanonOutput output(&out_str);
  1740. bool success = CanonicalizeFileURL(cases[i].input, url_len, parsed, NULL,
  1741. &output, &out_parsed);
  1742. output.Complete();
  1743. EXPECT_EQ(cases[i].expected_success, success);
  1744. EXPECT_EQ(cases[i].expected, out_str);
  1745. // Make sure the spec was properly identified, the file canonicalizer has
  1746. // different code for writing the spec.
  1747. EXPECT_EQ(0, out_parsed.scheme.begin);
  1748. EXPECT_EQ(4, out_parsed.scheme.len);
  1749. EXPECT_EQ(cases[i].expected_host.begin, out_parsed.host.begin);
  1750. EXPECT_EQ(cases[i].expected_host.len, out_parsed.host.len);
  1751. EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
  1752. EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
  1753. }
  1754. }
  1755. TEST(URLCanonTest, CanonicalizeFileSystemURL) {
  1756. struct URLCase {
  1757. const char* input;
  1758. const char* expected;
  1759. bool expected_success;
  1760. } cases[] = {
  1761. {"Filesystem:htTp://www.Foo.com:80/tempoRary",
  1762. "filesystem:http://www.foo.com/tempoRary/", true},
  1763. {"filesystem:httpS://www.foo.com/temporary/",
  1764. "filesystem:https://www.foo.com/temporary/", true},
  1765. {"filesystem:http://www.foo.com//", "filesystem:http://www.foo.com//",
  1766. false},
  1767. {"filesystem:http://www.foo.com/persistent/bob?query#ref",
  1768. "filesystem:http://www.foo.com/persistent/bob?query#ref", true},
  1769. {"filesystem:fIle://\\temporary/", "filesystem:file:///temporary/", true},
  1770. {"filesystem:fiLe:///temporary", "filesystem:file:///temporary/", true},
  1771. {"filesystem:File:///temporary/Bob?qUery#reF",
  1772. "filesystem:file:///temporary/Bob?qUery#reF", true},
  1773. {"FilEsysteM:htTp:E=/.", "filesystem:http://e%3D//", false},
  1774. };
  1775. for (size_t i = 0; i < std::size(cases); i++) {
  1776. int url_len = static_cast<int>(strlen(cases[i].input));
  1777. Parsed parsed;
  1778. ParseFileSystemURL(cases[i].input, url_len, &parsed);
  1779. Parsed out_parsed;
  1780. std::string out_str;
  1781. StdStringCanonOutput output(&out_str);
  1782. bool success = CanonicalizeFileSystemURL(cases[i].input, url_len, parsed,
  1783. NULL, &output, &out_parsed);
  1784. output.Complete();
  1785. EXPECT_EQ(cases[i].expected_success, success);
  1786. EXPECT_EQ(cases[i].expected, out_str);
  1787. // Make sure the spec was properly identified, the filesystem canonicalizer
  1788. // has different code for writing the spec.
  1789. EXPECT_EQ(0, out_parsed.scheme.begin);
  1790. EXPECT_EQ(10, out_parsed.scheme.len);
  1791. if (success)
  1792. EXPECT_GT(out_parsed.path.len, 0);
  1793. }
  1794. }
  1795. TEST(URLCanonTest, CanonicalizePathURL) {
  1796. // Path URLs should get canonicalized schemes but nothing else.
  1797. struct PathCase {
  1798. const char* input;
  1799. const char* expected;
  1800. } path_cases[] = {
  1801. {"javascript:", "javascript:"},
  1802. {"JavaScript:Foo", "javascript:Foo"},
  1803. {"Foo:\":This /is interesting;?#", "foo:\":This /is interesting;?#"},
  1804. // Validation errors should not cause failure. See
  1805. // https://crbug.com/925614.
  1806. {"javascript:\uFFFF", "javascript:%EF%BF%BD"},
  1807. };
  1808. for (size_t i = 0; i < std::size(path_cases); i++) {
  1809. int url_len = static_cast<int>(strlen(path_cases[i].input));
  1810. Parsed parsed;
  1811. ParsePathURL(path_cases[i].input, url_len, true, &parsed);
  1812. Parsed out_parsed;
  1813. std::string out_str;
  1814. StdStringCanonOutput output(&out_str);
  1815. bool success = CanonicalizePathURL(path_cases[i].input, url_len, parsed,
  1816. &output, &out_parsed);
  1817. output.Complete();
  1818. EXPECT_TRUE(success);
  1819. EXPECT_EQ(path_cases[i].expected, out_str);
  1820. EXPECT_EQ(0, out_parsed.host.begin);
  1821. EXPECT_EQ(-1, out_parsed.host.len);
  1822. // When we end with a colon at the end, there should be no path.
  1823. if (path_cases[i].input[url_len - 1] == ':') {
  1824. EXPECT_EQ(0, out_parsed.GetContent().begin);
  1825. EXPECT_EQ(-1, out_parsed.GetContent().len);
  1826. }
  1827. }
  1828. }
  1829. TEST(URLCanonTest, CanonicalizePathURLPath) {
  1830. struct PathCase {
  1831. std::string input;
  1832. std::wstring input16;
  1833. std::string expected;
  1834. } path_cases[] = {
  1835. {"Foo", L"Foo", "Foo"},
  1836. {"\":This /is interesting;?#", L"\":This /is interesting;?#",
  1837. "\":This /is interesting;?#"},
  1838. {"\uFFFF", L"\uFFFF", "%EF%BF%BD"},
  1839. };
  1840. for (size_t i = 0; i < std::size(path_cases); i++) {
  1841. // 8-bit string input
  1842. std::string out_str;
  1843. StdStringCanonOutput output(&out_str);
  1844. url::Component out_component;
  1845. CanonicalizePathURLPath(path_cases[i].input.data(),
  1846. Component(0, path_cases[i].input.size()), &output,
  1847. &out_component);
  1848. output.Complete();
  1849. EXPECT_EQ(path_cases[i].expected, out_str);
  1850. EXPECT_EQ(0, out_component.begin);
  1851. EXPECT_EQ(path_cases[i].expected.size(),
  1852. static_cast<size_t>(out_component.len));
  1853. // 16-bit string input
  1854. std::string out_str16;
  1855. StdStringCanonOutput output16(&out_str16);
  1856. url::Component out_component16;
  1857. std::u16string input16(
  1858. test_utils::TruncateWStringToUTF16(path_cases[i].input16.data()));
  1859. CanonicalizePathURLPath(input16.c_str(),
  1860. Component(0, path_cases[i].input16.size()),
  1861. &output16, &out_component16);
  1862. output16.Complete();
  1863. EXPECT_EQ(path_cases[i].expected, out_str16);
  1864. EXPECT_EQ(0, out_component16.begin);
  1865. EXPECT_EQ(path_cases[i].expected.size(),
  1866. static_cast<size_t>(out_component16.len));
  1867. }
  1868. }
  1869. TEST(URLCanonTest, CanonicalizeMailtoURL) {
  1870. struct URLCase {
  1871. const char* input;
  1872. const char* expected;
  1873. bool expected_success;
  1874. Component expected_path;
  1875. Component expected_query;
  1876. } cases[] = {
  1877. // Null character should be escaped to %00.
  1878. // Keep this test first in the list as it is handled specially below.
  1879. {"mailto:addr1\0addr2?foo",
  1880. "mailto:addr1%00addr2?foo",
  1881. true, Component(7, 13), Component(21, 3)},
  1882. {"mailto:addr1",
  1883. "mailto:addr1",
  1884. true, Component(7, 5), Component()},
  1885. {"mailto:addr1@foo.com",
  1886. "mailto:addr1@foo.com",
  1887. true, Component(7, 13), Component()},
  1888. // Trailing whitespace is stripped.
  1889. {"MaIlTo:addr1 \t ",
  1890. "mailto:addr1",
  1891. true, Component(7, 5), Component()},
  1892. {"MaIlTo:addr1?to=jon",
  1893. "mailto:addr1?to=jon",
  1894. true, Component(7, 5), Component(13,6)},
  1895. {"mailto:addr1,addr2",
  1896. "mailto:addr1,addr2",
  1897. true, Component(7, 11), Component()},
  1898. // Embedded spaces must be encoded.
  1899. {"mailto:addr1, addr2",
  1900. "mailto:addr1,%20addr2",
  1901. true, Component(7, 14), Component()},
  1902. {"mailto:addr1, addr2?subject=one two ",
  1903. "mailto:addr1,%20addr2?subject=one%20two",
  1904. true, Component(7, 14), Component(22, 17)},
  1905. {"mailto:addr1%2caddr2",
  1906. "mailto:addr1%2caddr2",
  1907. true, Component(7, 13), Component()},
  1908. {"mailto:\xF0\x90\x8C\x80",
  1909. "mailto:%F0%90%8C%80",
  1910. true, Component(7, 12), Component()},
  1911. // Invalid -- UTF-8 encoded surrogate value.
  1912. {"mailto:\xed\xa0\x80",
  1913. "mailto:%EF%BF%BD%EF%BF%BD%EF%BF%BD",
  1914. false, Component(7, 27), Component()},
  1915. {"mailto:addr1?",
  1916. "mailto:addr1?",
  1917. true, Component(7, 5), Component(13, 0)},
  1918. // Certain characters have special meanings and must be encoded.
  1919. {"mailto:! \x22$&()+,-./09:;<=>@AZ[\\]&_`az{|}~\x7f?Query! \x22$&()+,-./09:;<=>@AZ[\\]&_`az{|}~",
  1920. "mailto:!%20%22$&()+,-./09:;%3C=%3E@AZ[\\]&_%60az%7B%7C%7D~%7F?Query!%20%22$&()+,-./09:;%3C=%3E@AZ[\\]&_`az{|}~",
  1921. true, Component(7, 53), Component(61, 47)},
  1922. };
  1923. // Define outside of loop to catch bugs where components aren't reset
  1924. Parsed parsed;
  1925. Parsed out_parsed;
  1926. for (size_t i = 0; i < std::size(cases); i++) {
  1927. int url_len = static_cast<int>(strlen(cases[i].input));
  1928. if (i == 0) {
  1929. // The first test case purposely has a '\0' in it -- don't count it
  1930. // as the string terminator.
  1931. url_len = 22;
  1932. }
  1933. ParseMailtoURL(cases[i].input, url_len, &parsed);
  1934. std::string out_str;
  1935. StdStringCanonOutput output(&out_str);
  1936. bool success = CanonicalizeMailtoURL(cases[i].input, url_len, parsed,
  1937. &output, &out_parsed);
  1938. output.Complete();
  1939. EXPECT_EQ(cases[i].expected_success, success);
  1940. EXPECT_EQ(cases[i].expected, out_str);
  1941. // Make sure the spec was properly identified
  1942. EXPECT_EQ(0, out_parsed.scheme.begin);
  1943. EXPECT_EQ(6, out_parsed.scheme.len);
  1944. EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
  1945. EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
  1946. EXPECT_EQ(cases[i].expected_query.begin, out_parsed.query.begin);
  1947. EXPECT_EQ(cases[i].expected_query.len, out_parsed.query.len);
  1948. }
  1949. }
  1950. #ifndef WIN32
  1951. TEST(URLCanonTest, _itoa_s) {
  1952. // We fill the buffer with 0xff to ensure that it's getting properly
  1953. // null-terminated. We also allocate one byte more than what we tell
  1954. // _itoa_s about, and ensure that the extra byte is untouched.
  1955. char buf[6];
  1956. memset(buf, 0xff, sizeof(buf));
  1957. EXPECT_EQ(0, _itoa_s(12, buf, sizeof(buf) - 1, 10));
  1958. EXPECT_STREQ("12", buf);
  1959. EXPECT_EQ('\xFF', buf[3]);
  1960. // Test the edge cases - exactly the buffer size and one over
  1961. memset(buf, 0xff, sizeof(buf));
  1962. EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 10));
  1963. EXPECT_STREQ("1234", buf);
  1964. EXPECT_EQ('\xFF', buf[5]);
  1965. memset(buf, 0xff, sizeof(buf));
  1966. EXPECT_EQ(EINVAL, _itoa_s(12345, buf, sizeof(buf) - 1, 10));
  1967. EXPECT_EQ('\xFF', buf[5]); // should never write to this location
  1968. // Test the template overload (note that this will see the full buffer)
  1969. memset(buf, 0xff, sizeof(buf));
  1970. EXPECT_EQ(0, _itoa_s(12, buf, 10));
  1971. EXPECT_STREQ("12", buf);
  1972. EXPECT_EQ('\xFF', buf[3]);
  1973. memset(buf, 0xff, sizeof(buf));
  1974. EXPECT_EQ(0, _itoa_s(12345, buf, 10));
  1975. EXPECT_STREQ("12345", buf);
  1976. EXPECT_EQ(EINVAL, _itoa_s(123456, buf, 10));
  1977. // Test that radix 16 is supported.
  1978. memset(buf, 0xff, sizeof(buf));
  1979. EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 16));
  1980. EXPECT_STREQ("4d2", buf);
  1981. EXPECT_EQ('\xFF', buf[5]);
  1982. }
  1983. TEST(URLCanonTest, _itow_s) {
  1984. // We fill the buffer with 0xff to ensure that it's getting properly
  1985. // null-terminated. We also allocate one byte more than what we tell
  1986. // _itoa_s about, and ensure that the extra byte is untouched.
  1987. char16_t buf[6];
  1988. const char fill_mem = 0xff;
  1989. const char16_t fill_char = 0xffff;
  1990. memset(buf, fill_mem, sizeof(buf));
  1991. EXPECT_EQ(0, _itow_s(12, buf, sizeof(buf) / 2 - 1, 10));
  1992. EXPECT_EQ(u"12", std::u16string(buf));
  1993. EXPECT_EQ(fill_char, buf[3]);
  1994. // Test the edge cases - exactly the buffer size and one over
  1995. EXPECT_EQ(0, _itow_s(1234, buf, sizeof(buf) / 2 - 1, 10));
  1996. EXPECT_EQ(u"1234", std::u16string(buf));
  1997. EXPECT_EQ(fill_char, buf[5]);
  1998. memset(buf, fill_mem, sizeof(buf));
  1999. EXPECT_EQ(EINVAL, _itow_s(12345, buf, sizeof(buf) / 2 - 1, 10));
  2000. EXPECT_EQ(fill_char, buf[5]); // should never write to this location
  2001. // Test the template overload (note that this will see the full buffer)
  2002. memset(buf, fill_mem, sizeof(buf));
  2003. EXPECT_EQ(0, _itow_s(12, buf, 10));
  2004. EXPECT_EQ(u"12", std::u16string(buf));
  2005. EXPECT_EQ(fill_char, buf[3]);
  2006. memset(buf, fill_mem, sizeof(buf));
  2007. EXPECT_EQ(0, _itow_s(12345, buf, 10));
  2008. EXPECT_EQ(u"12345", std::u16string(buf));
  2009. EXPECT_EQ(EINVAL, _itow_s(123456, buf, 10));
  2010. }
  2011. #endif // !WIN32
  2012. // Returns true if the given two structures are the same.
  2013. static bool ParsedIsEqual(const Parsed& a, const Parsed& b) {
  2014. return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len &&
  2015. a.username.begin == b.username.begin && a.username.len == b.username.len &&
  2016. a.password.begin == b.password.begin && a.password.len == b.password.len &&
  2017. a.host.begin == b.host.begin && a.host.len == b.host.len &&
  2018. a.port.begin == b.port.begin && a.port.len == b.port.len &&
  2019. a.path.begin == b.path.begin && a.path.len == b.path.len &&
  2020. a.query.begin == b.query.begin && a.query.len == b.query.len &&
  2021. a.ref.begin == b.ref.begin && a.ref.len == b.ref.len;
  2022. }
  2023. TEST(URLCanonTest, ResolveRelativeURL) {
  2024. struct RelativeCase {
  2025. const char* base; // Input base URL: MUST BE CANONICAL
  2026. bool is_base_hier; // Is the base URL hierarchical
  2027. bool is_base_file; // Tells us if the base is a file URL.
  2028. const char* test; // Input URL to test against.
  2029. bool succeed_relative; // Whether we expect IsRelativeURL to succeed
  2030. bool is_rel; // Whether we expect |test| to be relative or not.
  2031. bool succeed_resolve; // Whether we expect ResolveRelativeURL to succeed.
  2032. const char* resolved; // What we expect in the result when resolving.
  2033. } rel_cases[] = {
  2034. // Basic absolute input.
  2035. {"http://host/a", true, false, "http://another/", true, false, false, NULL},
  2036. {"http://host/a", true, false, "http:////another/", true, false, false, NULL},
  2037. // Empty relative URLs should only remove the ref part of the URL,
  2038. // leaving the rest unchanged.
  2039. {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
  2040. {"http://foo/bar#ref", true, false, "", true, true, true, "http://foo/bar"},
  2041. {"http://foo/bar#", true, false, "", true, true, true, "http://foo/bar"},
  2042. // Spaces at the ends of the relative path should be ignored.
  2043. {"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"},
  2044. {"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"},
  2045. {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
  2046. // Matching schemes without two slashes are treated as relative.
  2047. {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
  2048. {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
  2049. {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
  2050. {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
  2051. // Nonmatching schemes are absolute.
  2052. {"http://host/a", true, false, "https:host2", true, false, false, NULL},
  2053. {"http://host/a", true, false, "htto:/host2", true, false, false, NULL},
  2054. // Absolute path input
  2055. {"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"},
  2056. {"http://host/a", true, false, "\\b\\c\\d", true, true, true, "http://host/b/c/d"},
  2057. {"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"},
  2058. {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
  2059. {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
  2060. {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
  2061. // Relative path input
  2062. {"http://host/a", true, false, "b", true, true, true, "http://host/b"},
  2063. {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
  2064. {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
  2065. {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
  2066. {"http://host/a/", true, false, "..", true, true, true, "http://host/"},
  2067. {"http://host/a/", true, false, "./..", true, true, true, "http://host/"},
  2068. {"http://host/a/", true, false, "../.", true, true, true, "http://host/"},
  2069. {"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"},
  2070. {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
  2071. // Query input
  2072. {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
  2073. {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
  2074. {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
  2075. // Ref input
  2076. {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
  2077. {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
  2078. {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
  2079. // Non-hierarchical base: no relative handling. Relative input should
  2080. // error, and if a scheme is present, it should be treated as absolute.
  2081. {"data:foobar", false, false, "baz.html", false, false, false, NULL},
  2082. {"data:foobar", false, false, "data:baz", true, false, false, NULL},
  2083. {"data:foobar", false, false, "data:/base", true, false, false, NULL},
  2084. // Non-hierarchical base: absolute input should succeed.
  2085. {"data:foobar", false, false, "http://host/", true, false, false, NULL},
  2086. {"data:foobar", false, false, "http:host", true, false, false, NULL},
  2087. // Non-hierarchical base: empty URL should give error.
  2088. {"data:foobar", false, false, "", false, false, false, NULL},
  2089. // Invalid schemes should be treated as relative.
  2090. {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
  2091. {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
  2092. {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
  2093. {"data:asdf", false, false, ":foo", false, false, false, NULL},
  2094. {"data:asdf", false, false, "bad(':foo')", false, false, false, NULL},
  2095. // We should treat semicolons like any other character in URL resolving
  2096. {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
  2097. {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
  2098. {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
  2099. // Relative URLs can also be written as "//foo/bar" which is relative to
  2100. // the scheme. In this case, it would take the old scheme, so for http
  2101. // the example would resolve to "http://foo/bar".
  2102. {"http://host/a", true, false, "//another", true, true, true, "http://another/"},
  2103. {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
  2104. {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
  2105. {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
  2106. {"http://host/a", true, false, "//", true, true, false, "http:"},
  2107. // IE will also allow one or the other to be a backslash to get the same
  2108. // behavior.
  2109. {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
  2110. {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
  2111. #ifdef WIN32
  2112. // Resolving against Windows file base URLs.
  2113. {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL},
  2114. {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
  2115. {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
  2116. {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
  2117. // But two backslashes on Windows should be UNC so should be treated
  2118. // as absolute.
  2119. {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL},
  2120. // IE doesn't support drive specs starting with two slashes. It fails
  2121. // immediately and doesn't even try to load. We fix it up to either
  2122. // an absolute path or UNC depending on what it looks like.
  2123. {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
  2124. {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
  2125. // Windows drive specs should be allowed and treated as absolute.
  2126. {"file:///C:/foo", true, true, "c:", true, false, false, NULL},
  2127. {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL},
  2128. {"http://host/a", true, false, "c:\\foo", true, false, false, NULL},
  2129. // Relative paths with drive letters should be allowed when the base is
  2130. // also a file.
  2131. {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
  2132. // Treat absolute paths as being off of the drive.
  2133. {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
  2134. {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
  2135. {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
  2136. // On Windows, two slashes without a drive letter when the base is a file
  2137. // means that the path is UNC.
  2138. {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
  2139. {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
  2140. #else
  2141. // On Unix we fall back to relative behavior since there's nothing else
  2142. // reasonable to do.
  2143. {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
  2144. #endif
  2145. // Even on Windows, we don't allow relative drive specs when the base
  2146. // is not file.
  2147. {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
  2148. {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
  2149. // Cross-platform relative file: resolution behavior.
  2150. {"file://host/a", true, true, "/", true, true, true, "file://host/"},
  2151. {"file://host/a", true, true, "//", true, true, true, "file:///"},
  2152. {"file://host/a", true, true, "/b", true, true, true, "file://host/b"},
  2153. {"file://host/a", true, true, "//b", true, true, true, "file://b/"},
  2154. // Ensure that ports aren't allowed for hosts relative to a file url.
  2155. // Although the result string shows a host:port portion, the call to
  2156. // resolve the relative URL returns false, indicating parse failure,
  2157. // which is what is required.
  2158. {"file:///foo.txt", true, true, "//host:80/bar.txt", true, true, false, "file://host:80/bar.txt"},
  2159. // Filesystem URL tests; filesystem URLs are only valid and relative if
  2160. // they have no scheme, e.g. "./index.html". There's no valid equivalent
  2161. // to http:index.html.
  2162. {"filesystem:http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
  2163. {"filesystem:http://host/t/path", true, false, "filesystem:https://host/t/path2", true, false, false, NULL},
  2164. {"filesystem:http://host/t/path", true, false, "http://host/t/path2", true, false, false, NULL},
  2165. {"http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
  2166. {"filesystem:http://host/t/path", true, false, "./path2", true, true, true, "filesystem:http://host/t/path2"},
  2167. {"filesystem:http://host/t/path/", true, false, "path2", true, true, true, "filesystem:http://host/t/path/path2"},
  2168. {"filesystem:http://host/t/path", true, false, "filesystem:http:path2", true, false, false, NULL},
  2169. // Absolute URLs are still not relative to a non-standard base URL.
  2170. {"about:blank", false, false, "http://X/A", true, false, true, ""},
  2171. {"about:blank", false, false, "content://content.Provider/", true, false, true, ""},
  2172. };
  2173. for (size_t i = 0; i < std::size(rel_cases); i++) {
  2174. const RelativeCase& cur_case = rel_cases[i];
  2175. Parsed parsed;
  2176. int base_len = static_cast<int>(strlen(cur_case.base));
  2177. if (cur_case.is_base_file)
  2178. ParseFileURL(cur_case.base, base_len, &parsed);
  2179. else if (cur_case.is_base_hier)
  2180. ParseStandardURL(cur_case.base, base_len, &parsed);
  2181. else
  2182. ParsePathURL(cur_case.base, base_len, false, &parsed);
  2183. // First see if it is relative.
  2184. int test_len = static_cast<int>(strlen(cur_case.test));
  2185. bool is_relative;
  2186. Component relative_component;
  2187. bool succeed_is_rel = IsRelativeURL(
  2188. cur_case.base, parsed, cur_case.test, test_len, cur_case.is_base_hier,
  2189. &is_relative, &relative_component);
  2190. EXPECT_EQ(cur_case.succeed_relative, succeed_is_rel) <<
  2191. "succeed is rel failure on " << cur_case.test;
  2192. EXPECT_EQ(cur_case.is_rel, is_relative) <<
  2193. "is rel failure on " << cur_case.test;
  2194. // Now resolve it.
  2195. if (succeed_is_rel && is_relative && cur_case.is_rel) {
  2196. std::string resolved;
  2197. StdStringCanonOutput output(&resolved);
  2198. Parsed resolved_parsed;
  2199. bool succeed_resolve = ResolveRelativeURL(
  2200. cur_case.base, parsed, cur_case.is_base_file, cur_case.test,
  2201. relative_component, NULL, &output, &resolved_parsed);
  2202. output.Complete();
  2203. EXPECT_EQ(cur_case.succeed_resolve, succeed_resolve);
  2204. EXPECT_EQ(cur_case.resolved, resolved) << " on " << cur_case.test;
  2205. // Verify that the output parsed structure is the same as parsing a
  2206. // the URL freshly.
  2207. Parsed ref_parsed;
  2208. int resolved_len = static_cast<int>(resolved.size());
  2209. if (cur_case.is_base_file) {
  2210. ParseFileURL(resolved.c_str(), resolved_len, &ref_parsed);
  2211. } else if (cur_case.is_base_hier) {
  2212. ParseStandardURL(resolved.c_str(), resolved_len, &ref_parsed);
  2213. } else {
  2214. ParsePathURL(resolved.c_str(), resolved_len, false, &ref_parsed);
  2215. }
  2216. EXPECT_TRUE(ParsedIsEqual(ref_parsed, resolved_parsed));
  2217. }
  2218. }
  2219. }
  2220. // It used to be the case that when we did a replacement with a long buffer of
  2221. // UTF-16 characters, we would get invalid data in the URL. This is because the
  2222. // buffer that it used to hold the UTF-8 data was resized, while some pointers
  2223. // were still kept to the old buffer that was removed.
  2224. TEST(URLCanonTest, ReplacementOverflow) {
  2225. const char src[] = "file:///C:/foo/bar";
  2226. int src_len = static_cast<int>(strlen(src));
  2227. Parsed parsed;
  2228. ParseFileURL(src, src_len, &parsed);
  2229. // Override two components, the path with something short, and the query with
  2230. // something long enough to trigger the bug.
  2231. Replacements<char16_t> repl;
  2232. std::u16string new_query;
  2233. for (int i = 0; i < 4800; i++)
  2234. new_query.push_back('a');
  2235. std::u16string new_path(test_utils::TruncateWStringToUTF16(L"/foo"));
  2236. repl.SetPath(new_path.c_str(), Component(0, 4));
  2237. repl.SetQuery(new_query.c_str(),
  2238. Component(0, static_cast<int>(new_query.length())));
  2239. // Call ReplaceComponents on the string. It doesn't matter if we call it for
  2240. // standard URLs, file URLs, etc, since they will go to the same replacement
  2241. // function that was buggy.
  2242. Parsed repl_parsed;
  2243. std::string repl_str;
  2244. StdStringCanonOutput repl_output(&repl_str);
  2245. ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed);
  2246. repl_output.Complete();
  2247. // Generate the expected string and check.
  2248. std::string expected("file:///foo?");
  2249. for (size_t i = 0; i < new_query.length(); i++)
  2250. expected.push_back('a');
  2251. EXPECT_TRUE(expected == repl_str);
  2252. }
  2253. TEST(URLCanonTest, DefaultPortForScheme) {
  2254. struct TestCases {
  2255. const char* scheme;
  2256. const int expected_port;
  2257. } cases[]{
  2258. {"http", 80},
  2259. {"https", 443},
  2260. {"ftp", 21},
  2261. {"ws", 80},
  2262. {"wss", 443},
  2263. {"fake-scheme", PORT_UNSPECIFIED},
  2264. {"HTTP", PORT_UNSPECIFIED},
  2265. {"HTTPS", PORT_UNSPECIFIED},
  2266. {"FTP", PORT_UNSPECIFIED},
  2267. {"WS", PORT_UNSPECIFIED},
  2268. {"WSS", PORT_UNSPECIFIED},
  2269. };
  2270. for (auto& test_case : cases) {
  2271. SCOPED_TRACE(test_case.scheme);
  2272. EXPECT_EQ(test_case.expected_port,
  2273. DefaultPortForScheme(test_case.scheme, strlen(test_case.scheme)));
  2274. }
  2275. }
  2276. TEST(URLCanonTest, FindWindowsDriveLetter) {
  2277. struct TestCase {
  2278. base::StringPiece spec;
  2279. int begin;
  2280. int end; // -1 for end of spec
  2281. int expected_drive_letter_pos;
  2282. } cases[] = {
  2283. {"/", 0, -1, -1},
  2284. {"c:/foo", 0, -1, 0},
  2285. {"/c:/foo", 0, -1, 1},
  2286. {"//c:/foo", 0, -1, -1}, // "//" does not canonicalize to "/"
  2287. {"\\C|\\foo", 0, -1, 1},
  2288. {"/cd:/foo", 0, -1, -1}, // "/c" does not canonicalize to "/"
  2289. {"/./c:/foo", 0, -1, 3},
  2290. {"/.//c:/foo", 0, -1, -1}, // "/.//" does not canonicalize to "/"
  2291. {"/././c:/foo", 0, -1, 5},
  2292. {"/abc/c:/foo", 0, -1, -1}, // "/abc/" does not canonicalize to "/"
  2293. {"/abc/./../c:/foo", 0, -1, 10},
  2294. {"/c:/c:/foo", 3, -1, 4}, // actual input is "/c:/foo"
  2295. {"/c:/foo", 3, -1, -1}, // actual input is "/foo"
  2296. {"/c:/foo", 0, 1, -1}, // actual input is "/"
  2297. };
  2298. for (const auto& c : cases) {
  2299. int end = c.end;
  2300. if (end == -1)
  2301. end = c.spec.size();
  2302. EXPECT_EQ(c.expected_drive_letter_pos,
  2303. FindWindowsDriveLetter(c.spec.data(), c.begin, end))
  2304. << "for " << c.spec << "[" << c.begin << ":" << end << "] (UTF-8)";
  2305. std::u16string spec16 = base::ASCIIToUTF16(c.spec);
  2306. EXPECT_EQ(c.expected_drive_letter_pos,
  2307. FindWindowsDriveLetter(spec16.data(), c.begin, end))
  2308. << "for " << c.spec << "[" << c.begin << ":" << end << "] (UTF-16)";
  2309. }
  2310. }
  2311. TEST(URLCanonTest, IDNToASCII) {
  2312. RawCanonOutputW<1024> output;
  2313. // Basic ASCII test.
  2314. std::u16string str = u"hello";
  2315. EXPECT_TRUE(IDNToASCII(str.data(), str.length(), &output));
  2316. EXPECT_EQ(u"hello", std::u16string(output.data()));
  2317. output.set_length(0);
  2318. // Mixed ASCII/non-ASCII.
  2319. str = u"hellö";
  2320. EXPECT_TRUE(IDNToASCII(str.data(), str.length(), &output));
  2321. EXPECT_EQ(u"xn--hell-8qa", std::u16string(output.data()));
  2322. output.set_length(0);
  2323. // All non-ASCII.
  2324. str = u"你好";
  2325. EXPECT_TRUE(IDNToASCII(str.data(), str.length(), &output));
  2326. EXPECT_EQ(u"xn--6qq79v", std::u16string(output.data()));
  2327. output.set_length(0);
  2328. // Characters that need mapping (the resulting Punycode is the encoding for
  2329. // "1⁄4").
  2330. str = u"¼";
  2331. EXPECT_TRUE(IDNToASCII(str.data(), str.length(), &output));
  2332. EXPECT_EQ(u"xn--14-c6t", std::u16string(output.data()));
  2333. output.set_length(0);
  2334. // String to encode already starts with "xn--", and all ASCII. Should not
  2335. // modify the string.
  2336. str = u"xn--hell-8qa";
  2337. EXPECT_TRUE(IDNToASCII(str.data(), str.length(), &output));
  2338. EXPECT_EQ(u"xn--hell-8qa", std::u16string(output.data()));
  2339. output.set_length(0);
  2340. // String to encode already starts with "xn--", and mixed ASCII/non-ASCII.
  2341. // Should fail, due to a special case: if the label starts with "xn--", it
  2342. // should be parsed as Punycode, which must be all ASCII.
  2343. str = u"xn--hellö";
  2344. EXPECT_FALSE(IDNToASCII(str.data(), str.length(), &output));
  2345. output.set_length(0);
  2346. // String to encode already starts with "xn--", and mixed ASCII/non-ASCII.
  2347. // This tests that there is still an error for the character '⁄' (U+2044),
  2348. // which would be a valid ASCII character, U+0044, if the high byte were
  2349. // ignored.
  2350. str = u"xn--1⁄4";
  2351. EXPECT_FALSE(IDNToASCII(str.data(), str.length(), &output));
  2352. output.set_length(0);
  2353. }
  2354. } // namespace url