UtilTest.cc 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. #include "util.h"
  2. #include <cstring>
  3. #include <string>
  4. #include <iostream>
  5. #include <cppunit/extensions/HelperMacros.h>
  6. #include "FixedNumberRandomizer.h"
  7. #include "DlAbortEx.h"
  8. #include "BitfieldMan.h"
  9. #include "ByteArrayDiskWriter.h"
  10. #include "FileEntry.h"
  11. #include "File.h"
  12. #include "array_fun.h"
  13. #include "BufferedFile.h"
  14. #include "TestUtil.h"
  15. #include "SocketCore.h"
  16. namespace aria2 {
  17. class UtilTest:public CppUnit::TestFixture {
  18. CPPUNIT_TEST_SUITE(UtilTest);
  19. CPPUNIT_TEST(testStrip);
  20. CPPUNIT_TEST(testStripIter);
  21. CPPUNIT_TEST(testLstripIter);
  22. CPPUNIT_TEST(testLstripIter_char);
  23. CPPUNIT_TEST(testDivide);
  24. CPPUNIT_TEST(testSplit);
  25. CPPUNIT_TEST(testSplitIter);
  26. CPPUNIT_TEST(testSplitIterM);
  27. CPPUNIT_TEST(testStreq);
  28. CPPUNIT_TEST(testStrieq);
  29. CPPUNIT_TEST(testStrifind);
  30. CPPUNIT_TEST(testEndsWith);
  31. CPPUNIT_TEST(testIendsWith);
  32. CPPUNIT_TEST(testReplace);
  33. CPPUNIT_TEST(testStartsWith);
  34. CPPUNIT_TEST(testIstartsWith);
  35. // may be moved to other helper class in the future.
  36. CPPUNIT_TEST(testGetContentDispositionFilename);
  37. CPPUNIT_TEST(testRandomAlpha);
  38. CPPUNIT_TEST(testToUpper);
  39. CPPUNIT_TEST(testToLower);
  40. CPPUNIT_TEST(testUppercase);
  41. CPPUNIT_TEST(testLowercase);
  42. CPPUNIT_TEST(testPercentDecode);
  43. CPPUNIT_TEST(testGetRealSize);
  44. CPPUNIT_TEST(testAbbrevSize);
  45. CPPUNIT_TEST(testToStream);
  46. CPPUNIT_TEST(testIsNumber);
  47. CPPUNIT_TEST(testIsLowercase);
  48. CPPUNIT_TEST(testIsUppercase);
  49. CPPUNIT_TEST(testAlphaToNum);
  50. CPPUNIT_TEST(testMkdirs);
  51. CPPUNIT_TEST(testConvertBitfield);
  52. CPPUNIT_TEST(testParseIntSegments);
  53. CPPUNIT_TEST(testParseIntSegments_invalidRange);
  54. CPPUNIT_TEST(testParseInt);
  55. CPPUNIT_TEST(testParseUInt);
  56. CPPUNIT_TEST(testParseLLInt);
  57. CPPUNIT_TEST(testParseIntNoThrow);
  58. CPPUNIT_TEST(testParseUIntNoThrow);
  59. CPPUNIT_TEST(testParseLLIntNoThrow);
  60. CPPUNIT_TEST(testToString_binaryStream);
  61. CPPUNIT_TEST(testItos);
  62. CPPUNIT_TEST(testUitos);
  63. CPPUNIT_TEST(testNtoh64);
  64. CPPUNIT_TEST(testPercentEncode);
  65. CPPUNIT_TEST(testHtmlEscape);
  66. CPPUNIT_TEST(testJoinPath);
  67. CPPUNIT_TEST(testParseIndexPath);
  68. CPPUNIT_TEST(testCreateIndexPaths);
  69. CPPUNIT_TEST(testGenerateRandomData);
  70. CPPUNIT_TEST(testFromHex);
  71. CPPUNIT_TEST(testParsePrioritizePieceRange);
  72. CPPUNIT_TEST(testApplyDir);
  73. CPPUNIT_TEST(testFixTaintedBasename);
  74. CPPUNIT_TEST(testIsNumericHost);
  75. CPPUNIT_TEST(testDetectDirTraversal);
  76. CPPUNIT_TEST(testEscapePath);
  77. CPPUNIT_TEST(testInSameCidrBlock);
  78. CPPUNIT_TEST(testIsUtf8String);
  79. CPPUNIT_TEST(testNextParam);
  80. CPPUNIT_TEST(testNoProxyDomainMatch);
  81. CPPUNIT_TEST(testInPrivateAddress);
  82. CPPUNIT_TEST(testSecfmt);
  83. CPPUNIT_TEST(testTlsHostnameMatch);
  84. CPPUNIT_TEST_SUITE_END();
  85. private:
  86. public:
  87. void setUp() {
  88. }
  89. void testStrip();
  90. void testStripIter();
  91. void testLstripIter();
  92. void testLstripIter_char();
  93. void testDivide();
  94. void testSplit();
  95. void testSplitIter();
  96. void testSplitIterM();
  97. void testStreq();
  98. void testStrieq();
  99. void testStrifind();
  100. void testEndsWith();
  101. void testIendsWith();
  102. void testReplace();
  103. void testStartsWith();
  104. void testIstartsWith();
  105. // may be moved to other helper class in the future.
  106. void testGetContentDispositionFilename();
  107. void testRandomAlpha();
  108. void testToUpper();
  109. void testToLower();
  110. void testUppercase();
  111. void testLowercase();
  112. void testPercentDecode();
  113. void testGetRealSize();
  114. void testAbbrevSize();
  115. void testToStream();
  116. void testIsNumber();
  117. void testIsLowercase();
  118. void testIsUppercase();
  119. void testAlphaToNum();
  120. void testMkdirs();
  121. void testConvertBitfield();
  122. void testParseIntSegments();
  123. void testParseIntSegments_invalidRange();
  124. void testParseInt();
  125. void testParseUInt();
  126. void testParseLLInt();
  127. void testParseIntNoThrow();
  128. void testParseUIntNoThrow();
  129. void testParseLLIntNoThrow();
  130. void testToString_binaryStream();
  131. void testItos();
  132. void testUitos();
  133. void testNtoh64();
  134. void testPercentEncode();
  135. void testHtmlEscape();
  136. void testJoinPath();
  137. void testParseIndexPath();
  138. void testCreateIndexPaths();
  139. void testGenerateRandomData();
  140. void testFromHex();
  141. void testParsePrioritizePieceRange();
  142. void testApplyDir();
  143. void testFixTaintedBasename();
  144. void testIsNumericHost();
  145. void testDetectDirTraversal();
  146. void testEscapePath();
  147. void testInSameCidrBlock();
  148. void testIsUtf8String();
  149. void testNextParam();
  150. void testNoProxyDomainMatch();
  151. void testInPrivateAddress();
  152. void testSecfmt();
  153. void testTlsHostnameMatch();
  154. };
  155. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  156. void UtilTest::testStrip()
  157. {
  158. std::string str1 = "aria2";
  159. CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2"));
  160. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2"));
  161. CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2 "));
  162. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
  163. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
  164. std::string str2 = "aria2 debut";
  165. CPPUNIT_ASSERT_EQUAL(str2, util::strip("aria2 debut"));
  166. CPPUNIT_ASSERT_EQUAL(str2, util::strip(" aria2 debut "));
  167. std::string str3 = "";
  168. CPPUNIT_ASSERT_EQUAL(str3, util::strip(""));
  169. CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
  170. CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
  171. std::string str4 = "A";
  172. CPPUNIT_ASSERT_EQUAL(str4, util::strip("A"));
  173. CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
  174. CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
  175. }
  176. void UtilTest::testStripIter()
  177. {
  178. Scip p;
  179. std::string str1 = "aria2";
  180. std::string s = "aria2";
  181. p = util::stripIter(s.begin(), s.end());
  182. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  183. s = " aria2";
  184. p = util::stripIter(s.begin(), s.end());
  185. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  186. s = "aria2 ";
  187. p = util::stripIter(s.begin(), s.end());
  188. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  189. s = " aria2 ";
  190. p = util::stripIter(s.begin(), s.end());
  191. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  192. s = " aria2 ";
  193. p = util::stripIter(s.begin(), s.end());
  194. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  195. std::string str2 = "aria2 debut";
  196. s = "aria2 debut";
  197. p = util::stripIter(s.begin(), s.end());
  198. CPPUNIT_ASSERT_EQUAL(str2, std::string(p.first, p.second));
  199. s = " aria2 debut ";
  200. p = util::stripIter(s.begin(), s.end());
  201. CPPUNIT_ASSERT_EQUAL(str2, std::string(p.first, p.second));
  202. std::string str3 = "";
  203. s = "";
  204. p = util::stripIter(s.begin(), s.end());
  205. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  206. s = " ";
  207. p = util::stripIter(s.begin(), s.end());
  208. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  209. s = " ";
  210. p = util::stripIter(s.begin(), s.end());
  211. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  212. std::string str4 = "A";
  213. s = "A";
  214. p = util::stripIter(s.begin(), s.end());
  215. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  216. s = " A ";
  217. p = util::stripIter(s.begin(), s.end());
  218. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  219. s = " A ";
  220. p = util::stripIter(s.begin(), s.end());
  221. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  222. }
  223. void UtilTest::testLstripIter()
  224. {
  225. std::string::iterator r;
  226. std::string s = "foo";
  227. r = util::lstripIter(s.begin(), s.end());
  228. CPPUNIT_ASSERT_EQUAL(std::string("foo"), std::string(r, s.end()));
  229. s = " foo bar ";
  230. r = util::lstripIter(s.begin(), s.end());
  231. CPPUNIT_ASSERT_EQUAL(std::string("foo bar "), std::string(r, s.end()));
  232. s = "f";
  233. r = util::lstripIter(s.begin(), s.end());
  234. CPPUNIT_ASSERT_EQUAL(std::string("f"), std::string(r, s.end()));
  235. s = "foo ";
  236. r = util::lstripIter(s.begin(), s.end());
  237. CPPUNIT_ASSERT_EQUAL(std::string("foo "), std::string(r, s.end()));
  238. }
  239. void UtilTest::testLstripIter_char()
  240. {
  241. std::string::iterator r;
  242. std::string s = "foo";
  243. r = util::lstripIter(s.begin(), s.end(), '$');
  244. CPPUNIT_ASSERT_EQUAL(std::string("foo"), std::string(r, s.end()));
  245. s = "$$foo$bar$$";
  246. r = util::lstripIter(s.begin(), s.end(), '$');
  247. CPPUNIT_ASSERT_EQUAL(std::string("foo$bar$$"), std::string(r, s.end()));
  248. s = "f";
  249. r = util::lstripIter(s.begin(), s.end(), '$');
  250. CPPUNIT_ASSERT_EQUAL(std::string("f"), std::string(r, s.end()));
  251. s = "foo$$";
  252. r = util::lstripIter(s.begin(), s.end(), '$');
  253. CPPUNIT_ASSERT_EQUAL(std::string("foo$$"), std::string(r, s.end()));
  254. }
  255. void UtilTest::testDivide() {
  256. std::pair<Sip, Sip> p1;
  257. std::string s = "name=value";
  258. util::divide(p1, s.begin(), s.end(), '=');
  259. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  260. std::string(p1.first.first, p1.first.second));
  261. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  262. std::string(p1.second.first, p1.second.second));
  263. s = " name = value ";
  264. util::divide(p1, s.begin(), s.end(), '=');
  265. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  266. std::string(p1.first.first, p1.first.second));
  267. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  268. std::string(p1.second.first, p1.second.second));
  269. s = "=value";
  270. util::divide(p1, s.begin(), s.end(), '=');
  271. CPPUNIT_ASSERT_EQUAL(std::string(""),
  272. std::string(p1.first.first, p1.first.second));
  273. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  274. std::string(p1.second.first, p1.second.second));
  275. s = "name=";
  276. util::divide(p1, s.begin(), s.end(), '=');
  277. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  278. std::string(p1.first.first, p1.first.second));
  279. CPPUNIT_ASSERT_EQUAL(std::string(""),
  280. std::string(p1.second.first, p1.second.second));
  281. s = "name";
  282. util::divide(p1, s.begin(), s.end(), '=');
  283. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  284. std::string(p1.first.first, p1.first.second));
  285. CPPUNIT_ASSERT_EQUAL(std::string(""),
  286. std::string(p1.second.first, p1.second.second));
  287. }
  288. void UtilTest::testSplit() {
  289. std::vector<std::string> v;
  290. std::string s = "k1; k2;; k3";
  291. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true);
  292. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  293. std::vector<std::string>::iterator itr = v.begin();
  294. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  295. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  296. CPPUNIT_ASSERT_EQUAL(std::string("k3"), *itr++);
  297. v.clear();
  298. s = "k1; k2; k3";
  299. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  300. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  301. itr = v.begin();
  302. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  303. CPPUNIT_ASSERT_EQUAL(std::string(" k2"), *itr++);
  304. CPPUNIT_ASSERT_EQUAL(std::string(" k3"), *itr++);
  305. v.clear();
  306. s = "k=v";
  307. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  308. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  309. itr = v.begin();
  310. CPPUNIT_ASSERT_EQUAL(std::string("k=v"), *itr++);
  311. v.clear();
  312. s = ";;k1;;k2;";
  313. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  314. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  315. itr = v.begin();
  316. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  317. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  318. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  319. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  320. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  321. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  322. v.clear();
  323. s = ";;k1;;k2;";
  324. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  325. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  326. itr = v.begin();
  327. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  328. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  329. v.clear();
  330. s = "k; ";
  331. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  332. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  333. itr = v.begin();
  334. CPPUNIT_ASSERT_EQUAL(std::string("k"), *itr++);
  335. CPPUNIT_ASSERT_EQUAL(std::string(" "), *itr++);
  336. v.clear();
  337. s = " ";
  338. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true, true);
  339. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  340. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  341. v.clear();
  342. s = " ";
  343. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true);
  344. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  345. v.clear();
  346. s = " ";
  347. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  348. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  349. CPPUNIT_ASSERT_EQUAL(std::string(" "), v[0]);
  350. v.clear();
  351. s = ";";
  352. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  353. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  354. v.clear();
  355. s = ";";
  356. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  357. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  358. itr = v.begin();
  359. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  360. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  361. v.clear();
  362. s = "";
  363. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  364. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  365. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  366. }
  367. void UtilTest::testSplitIter() {
  368. std::vector<Scip> v;
  369. std::string s = "k1; k2;; k3";
  370. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true);
  371. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  372. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  373. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  374. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  375. v.clear();
  376. s = "k1; k2; k3";
  377. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  378. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  379. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  380. CPPUNIT_ASSERT_EQUAL(std::string(" k2"),
  381. std::string(v[1].first, v[1].second));
  382. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  383. std::string(v[2].first, v[2].second));
  384. v.clear();
  385. s = "k=v";
  386. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  387. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  388. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  389. std::string(v[0].first, v[0].second));
  390. v.clear();
  391. s = ";;k1;;k2;";
  392. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  393. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  394. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  395. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  396. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  397. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  398. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  399. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  400. v.clear();
  401. s = ";;k1;;k2;";
  402. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  403. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  404. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  405. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  406. v.clear();
  407. s = "k; ";
  408. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  409. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  410. CPPUNIT_ASSERT_EQUAL(std::string("k"), std::string(v[0].first, v[0].second));
  411. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  412. v.clear();
  413. s = " ";
  414. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true, true);
  415. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  416. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  417. v.clear();
  418. s = " ";
  419. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true);
  420. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  421. v.clear();
  422. s = " ";
  423. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  424. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  425. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[0].first, v[0].second));
  426. v.clear();
  427. s = ";";
  428. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  429. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  430. v.clear();
  431. s = ";";
  432. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  433. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  434. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  435. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  436. v.clear();
  437. s = "";
  438. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  439. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  440. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  441. }
  442. void UtilTest::testSplitIterM() {
  443. const char d[] = ";";
  444. const char md[] = "; ";
  445. std::vector<Scip> v;
  446. std::string s = "k1; k2;; k3";
  447. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true);
  448. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  449. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  450. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  451. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  452. v.clear();
  453. s = "k1; k2; k3";
  454. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  455. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  456. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  457. CPPUNIT_ASSERT_EQUAL(std::string(" k2"),
  458. std::string(v[1].first, v[1].second));
  459. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  460. std::string(v[2].first, v[2].second));
  461. v.clear();
  462. s = "k1; k2; k3";
  463. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), md);
  464. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  465. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  466. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  467. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  468. v.clear();
  469. s = "k1; k2; k3;";
  470. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), md, false, true);
  471. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  472. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  473. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  474. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[2].first, v[2].second));
  475. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  476. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[4].first, v[4].second));
  477. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  478. v.clear();
  479. s = "k=v";
  480. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  481. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  482. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  483. std::string(v[0].first, v[0].second));
  484. v.clear();
  485. s = ";;k1;;k2;";
  486. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  487. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  488. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  489. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  490. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  491. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  492. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  493. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  494. v.clear();
  495. s = ";;k1;;k2;";
  496. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  497. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  498. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  499. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  500. v.clear();
  501. s = "k; ";
  502. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  503. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  504. CPPUNIT_ASSERT_EQUAL(std::string("k"), std::string(v[0].first, v[0].second));
  505. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  506. v.clear();
  507. s = " ";
  508. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true, true);
  509. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  510. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  511. v.clear();
  512. s = " ";
  513. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true);
  514. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  515. v.clear();
  516. s = " ";
  517. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  518. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  519. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[0].first, v[0].second));
  520. v.clear();
  521. s = ";";
  522. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  523. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  524. v.clear();
  525. s = ";";
  526. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  527. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  528. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  529. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  530. v.clear();
  531. s = "";
  532. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  533. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  534. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  535. }
  536. void UtilTest::testEndsWith() {
  537. std::string target = "abcdefg";
  538. std::string part = "fg";
  539. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  540. part.begin(), part.end()));
  541. target = "abdefg";
  542. part = "g";
  543. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  544. part.begin(), part.end()));
  545. target = "abdefg";
  546. part = "eg";
  547. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  548. part.begin(), part.end()));
  549. target = "g";
  550. part = "eg";
  551. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  552. part.begin(), part.end()));
  553. target = "g";
  554. part = "g";
  555. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  556. part.begin(), part.end()));
  557. target = "g";
  558. part = "";
  559. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  560. part.begin(), part.end()));
  561. target = "";
  562. part = "";
  563. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  564. part.begin(), part.end()));
  565. target = "";
  566. part = "g";
  567. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  568. part.begin(), part.end()));
  569. }
  570. void UtilTest::testIendsWith() {
  571. std::string target = "abcdefg";
  572. std::string part = "Fg";
  573. CPPUNIT_ASSERT(util::iendsWith(target.begin(), target.end(),
  574. part.begin(), part.end()));
  575. target = "abdefg";
  576. part = "ef";
  577. CPPUNIT_ASSERT(!util::iendsWith(target.begin(), target.end(),
  578. part.begin(), part.end()));
  579. }
  580. void UtilTest::testStreq()
  581. {
  582. std::string s1, s2;
  583. s1 = "foo";
  584. s2 = "foo";
  585. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  586. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.c_str()));
  587. s2 = "fooo";
  588. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  589. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  590. s2 = "fo";
  591. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  592. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  593. s2 = "";
  594. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  595. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  596. s1 = "";
  597. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  598. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.c_str()));
  599. }
  600. void UtilTest::testStrieq()
  601. {
  602. std::string s1, s2;
  603. s1 = "foo";
  604. s2 = "foo";
  605. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  606. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  607. s1 = "FoO";
  608. s2 = "fOo";
  609. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  610. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  611. s2 = "fooo";
  612. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  613. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  614. s2 = "fo";
  615. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  616. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  617. s2 = "";
  618. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  619. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  620. s1 = "";
  621. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  622. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  623. }
  624. void UtilTest::testStrifind()
  625. {
  626. std::string s1, s2;
  627. s1 = "yamagakani mukashi wo toheba hARU no tuki";
  628. s2 = "HaRu";
  629. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  630. != s1.end());
  631. s2 = "aki";
  632. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  633. == s1.end());
  634. s1 = "h";
  635. s2 = "HH";
  636. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  637. == s1.end());
  638. }
  639. void UtilTest::testReplace() {
  640. CPPUNIT_ASSERT_EQUAL(std::string("abc\n"), util::replace("abc\r\n", "\r", ""));
  641. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc\r\n", "\r\n", ""));
  642. CPPUNIT_ASSERT_EQUAL(std::string(""), util::replace("", "\r\n", ""));
  643. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc", "", "a"));
  644. CPPUNIT_ASSERT_EQUAL(std::string("xbc"), util::replace("abc", "a", "x"));
  645. }
  646. void UtilTest::testStartsWith() {
  647. std::string target;
  648. std::string part;
  649. target = "abcdefg";
  650. part = "abc";
  651. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  652. part.begin(), part.end()));
  653. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  654. target = "abcdefg";
  655. part = "abx";
  656. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  657. part.begin(), part.end()));
  658. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
  659. target = "abcdefg";
  660. part = "bcd";
  661. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  662. part.begin(), part.end()));
  663. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
  664. target = "";
  665. part = "a";
  666. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  667. part.begin(), part.end()));
  668. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
  669. target = "";
  670. part = "";
  671. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  672. part.begin(), part.end()));
  673. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  674. target = "a";
  675. part = "";
  676. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  677. part.begin(), part.end()));
  678. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  679. target = "a";
  680. part = "a";
  681. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  682. part.begin(), part.end()));
  683. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  684. }
  685. void UtilTest::testIstartsWith() {
  686. std::string target;
  687. std::string part;
  688. target = "abcdefg";
  689. part = "aBc";
  690. CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(),
  691. part.begin(), part.end()));
  692. CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(), part.c_str()));
  693. target = "abcdefg";
  694. part = "abx";
  695. CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(),
  696. part.begin(), part.end()));
  697. CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(), part.c_str()));
  698. }
  699. void UtilTest::testGetContentDispositionFilename() {
  700. std::string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  701. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h1));
  702. std::string h2 = "attachment; filename=\"\"";
  703. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h2));
  704. std::string h3 = "attachment; filename=\"";
  705. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h3));
  706. std::string h3_2 = "attachment; filename= \" aria2.tar.bz2 \"";
  707. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  708. util::getContentDispositionFilename(h3_2));
  709. std::string h4 = "attachment;";
  710. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h4));
  711. std::string h5 = "attachment; filename=aria2.tar.bz2";
  712. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h5));
  713. std::string h6 = "attachment; filename='aria2.tar.bz2'";
  714. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h6));
  715. std::string h7 = "attachment; filename='aria2.tar.bz2";
  716. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h7));
  717. std::string h8 = "attachment; filename=aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT";
  718. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h8));
  719. std::string h9 = "attachment; filename=\"aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT\"";
  720. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT"),
  721. util::getContentDispositionFilename(h9));
  722. std::string h10 = "attachment; filename=";
  723. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h10));
  724. std::string h11 = "attachment; filename=;";
  725. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h11));
  726. std::string filenameWithDir = "attachment; filename=dir/file";
  727. CPPUNIT_ASSERT_EQUAL(std::string(""),
  728. util::getContentDispositionFilename(filenameWithDir));
  729. std::string semicolonInside = "attachment; filename=\"foo;bar\"";
  730. CPPUNIT_ASSERT_EQUAL(std::string("foo;bar"),
  731. util::getContentDispositionFilename(semicolonInside));
  732. CPPUNIT_ASSERT_EQUAL
  733. (std::string(""),
  734. util::getContentDispositionFilename("filename=\"%2E%2E%2Ffoo.html\""));
  735. // RFC2231 Section4
  736. std::string extparam2 = "filename*=''aria2";
  737. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  738. util::getContentDispositionFilename(extparam2));
  739. std::string extparam3 = "filename*='''";
  740. CPPUNIT_ASSERT_EQUAL(std::string(""),
  741. util::getContentDispositionFilename(extparam3));
  742. std::string extparam4 = "filename*='aria2";
  743. CPPUNIT_ASSERT_EQUAL(std::string(""),
  744. util::getContentDispositionFilename(extparam4));
  745. std::string extparam5 = "filename*='''aria2";
  746. CPPUNIT_ASSERT_EQUAL(std::string(""),
  747. util::getContentDispositionFilename(extparam5));
  748. std::string extparam6 = "filename*";
  749. CPPUNIT_ASSERT_EQUAL(std::string(""),
  750. util::getContentDispositionFilename(extparam6));
  751. std::string extparam7 = "filename*=UTF-8''aria2;filename=hello%20world";
  752. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  753. util::getContentDispositionFilename(extparam7));
  754. std::string extparam8 = "filename=aria2;filename*=UTF-8''hello%20world";
  755. CPPUNIT_ASSERT_EQUAL(std::string("hello world"),
  756. util::getContentDispositionFilename(extparam8));
  757. std::string extparam9 = "filename*=ISO-8859-1''%A3";
  758. std::string extparam9ans;
  759. extparam9ans += 0xc2;
  760. extparam9ans += 0xa3;
  761. CPPUNIT_ASSERT_EQUAL(extparam9ans,
  762. util::getContentDispositionFilename(extparam9));
  763. CPPUNIT_ASSERT_EQUAL
  764. (std::string(""),
  765. util::getContentDispositionFilename("filename*=UTF-8''foo%2F.html"));
  766. CPPUNIT_ASSERT_EQUAL
  767. (std::string("foo.html"),
  768. util::getContentDispositionFilename("filename*=UTF-8'';filename=\"foo.html\""));
  769. CPPUNIT_ASSERT_EQUAL
  770. (std::string(""),
  771. util::getContentDispositionFilename("filename*=UTF-8''%2E%2E%2Ffoo.html"));
  772. // Tests from http://greenbytes.de/tech/tc2231/
  773. // attwithasciifnescapedchar
  774. CPPUNIT_ASSERT_EQUAL
  775. (std::string("foo.html"),
  776. util::getContentDispositionFilename("filename=\"f\\oo.html\""));
  777. // attwithasciifilenameucase
  778. CPPUNIT_ASSERT_EQUAL
  779. (std::string("foo.html"),
  780. util::getContentDispositionFilename("FILENAME=\"foo.html\""));
  781. // attwithisofn2231iso
  782. CPPUNIT_ASSERT_EQUAL
  783. (std::string("foo-ä.html"),
  784. util::getContentDispositionFilename("filename*=iso-8859-1''foo-%E4.html"));
  785. // attwithfn2231utf8
  786. CPPUNIT_ASSERT_EQUAL
  787. (std::string("foo-ä-€.html"),
  788. util::getContentDispositionFilename
  789. ("filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html"));
  790. // attwithfn2231utf8-bad
  791. CPPUNIT_ASSERT_EQUAL
  792. (std::string(""),
  793. util::getContentDispositionFilename
  794. ("filename*=iso-8859-1''foo-%c3%a4-%e2%82%ac.html"));
  795. // attwithfn2231ws1
  796. CPPUNIT_ASSERT_EQUAL
  797. (std::string(""),
  798. util::getContentDispositionFilename("filename *=UTF-8''foo-%c3%a4.html"));
  799. // attwithfn2231ws2
  800. CPPUNIT_ASSERT_EQUAL
  801. (std::string("foo-ä.html"),
  802. util::getContentDispositionFilename("filename*= UTF-8''foo-%c3%a4.html"));
  803. // attwithfn2231ws3
  804. CPPUNIT_ASSERT_EQUAL
  805. (std::string("foo-ä.html"),
  806. util::getContentDispositionFilename("filename* =UTF-8''foo-%c3%a4.html"));
  807. // attwithfn2231quot
  808. CPPUNIT_ASSERT_EQUAL
  809. (std::string(""),
  810. util::getContentDispositionFilename
  811. ("filename*=\"UTF-8''foo-%c3%a4.html\""));
  812. }
  813. class Printer {
  814. public:
  815. template<class T>
  816. void operator()(T t) {
  817. std::cerr << t << ", ";
  818. }
  819. };
  820. void UtilTest::testRandomAlpha() {
  821. SharedHandle<Randomizer> rand(new FixedNumberRandomizer());
  822. std::string s = util::randomAlpha(8, rand);
  823. CPPUNIT_ASSERT_EQUAL(std::string("AAAAAAAA"), s);
  824. }
  825. void UtilTest::testToUpper() {
  826. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  827. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  828. CPPUNIT_ASSERT_EQUAL(upp, util::toUpper(src));
  829. }
  830. void UtilTest::testToLower() {
  831. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  832. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  833. CPPUNIT_ASSERT_EQUAL(upp, util::toLower(src));
  834. }
  835. void UtilTest::testUppercase() {
  836. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  837. std::string ans = "608CABC0F2FA18C260CAFD974516865C772363D5";
  838. util::uppercase(src);
  839. CPPUNIT_ASSERT_EQUAL(ans, src);
  840. }
  841. void UtilTest::testLowercase() {
  842. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  843. std::string ans = "608cabc0f2fa18c260cafd974516865c772363d5";
  844. util::lowercase(src);
  845. CPPUNIT_ASSERT_EQUAL(ans, src);
  846. }
  847. void UtilTest::testPercentDecode() {
  848. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  849. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  850. util::percentDecode(src.begin(), src.end()));
  851. std::string src2 = "aria2+aria2";
  852. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"),
  853. util::percentDecode(src2.begin(), src2.end()));
  854. std::string src3 = "%5t%20";
  855. CPPUNIT_ASSERT_EQUAL(std::string("%5t "),
  856. util::percentDecode(src3.begin(), src3.end()));
  857. std::string src4 = "%";
  858. CPPUNIT_ASSERT_EQUAL(std::string("%"),
  859. util::percentDecode(src4.begin(), src4.end()));
  860. std::string src5 = "%3";
  861. CPPUNIT_ASSERT_EQUAL(std::string("%3"),
  862. util::percentDecode(src5.begin(), src5.end()));
  863. std::string src6 = "%2f";
  864. CPPUNIT_ASSERT_EQUAL(std::string("/"),
  865. util::percentDecode(src6.begin(), src6.end()));
  866. }
  867. void UtilTest::testGetRealSize()
  868. {
  869. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096M"));
  870. CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1K"));
  871. try {
  872. util::getRealSize("");
  873. CPPUNIT_FAIL("exception must be thrown.");
  874. } catch(Exception& e) {
  875. std::cerr << e.stackTrace();
  876. }
  877. try {
  878. util::getRealSize("foo");
  879. CPPUNIT_FAIL("exception must be thrown.");
  880. } catch(Exception& e) {
  881. std::cerr << e.stackTrace();
  882. }
  883. try {
  884. util::getRealSize("-1");
  885. CPPUNIT_FAIL("exception must be thrown.");
  886. } catch(Exception& e) {
  887. std::cerr << e.stackTrace();
  888. }
  889. try {
  890. util::getRealSize("9223372036854775807K");
  891. CPPUNIT_FAIL("exception must be thrown.");
  892. } catch(Exception& e) {
  893. std::cerr << e.stackTrace();
  894. }
  895. try {
  896. util::getRealSize("9223372036854775807M");
  897. CPPUNIT_FAIL("exception must be thrown.");
  898. } catch(Exception& e) {
  899. std::cerr << e.stackTrace();
  900. }
  901. }
  902. void UtilTest::testAbbrevSize()
  903. {
  904. CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), util::abbrevSize(4294967296LL));
  905. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1024));
  906. CPPUNIT_ASSERT_EQUAL(std::string("1,023"), util::abbrevSize(1023));
  907. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::abbrevSize(0));
  908. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), util::abbrevSize(1127));
  909. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), util::abbrevSize(1572864));
  910. }
  911. void UtilTest::testToStream()
  912. {
  913. std::ostringstream os;
  914. SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  915. SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  916. std::deque<SharedHandle<FileEntry> > entries;
  917. entries.push_back(f1);
  918. entries.push_back(f2);
  919. std::string filename = A2_TEST_OUT_DIR"/aria2_UtilTest_testToStream";
  920. BufferedFile fp(filename, BufferedFile::WRITE);
  921. util::toStream(entries.begin(), entries.end(), fp);
  922. fp.close();
  923. CPPUNIT_ASSERT_EQUAL(
  924. std::string("Files:\n"
  925. "idx|path/length\n"
  926. "===+===========================================================================\n"
  927. " 1|aria2.tar.bz2\n"
  928. " |12.0KiB (12,300)\n"
  929. "---+---------------------------------------------------------------------------\n"
  930. " 2|aria2.txt\n"
  931. " |556B (556)\n"
  932. "---+---------------------------------------------------------------------------\n"),
  933. readFile(filename));
  934. }
  935. void UtilTest::testIsNumber()
  936. {
  937. std::string s = "000";
  938. CPPUNIT_ASSERT_EQUAL(true, util::isNumber(s.begin(),s.end()));
  939. s = "a";
  940. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  941. s = "0a";
  942. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  943. s = "";
  944. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  945. s = " ";
  946. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  947. }
  948. void UtilTest::testIsLowercase()
  949. {
  950. std::string s = "alpha";
  951. CPPUNIT_ASSERT_EQUAL(true, util::isLowercase(s.begin(), s.end()));
  952. s = "Alpha";
  953. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  954. s = "1alpha";
  955. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  956. s = "";
  957. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  958. s = " ";
  959. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  960. }
  961. void UtilTest::testIsUppercase()
  962. {
  963. std::string s = "ALPHA";
  964. CPPUNIT_ASSERT_EQUAL(true, util::isUppercase(s.begin(), s.end()));
  965. s = "Alpha";
  966. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  967. s = "1ALPHA";
  968. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  969. s = "";
  970. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  971. s = " ";
  972. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  973. }
  974. void UtilTest::testAlphaToNum()
  975. {
  976. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("a"));
  977. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("aa"));
  978. CPPUNIT_ASSERT_EQUAL(1U, util::alphaToNum("b"));
  979. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("zz")); // 25*26+25
  980. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("ZZ")); // 25*26+25
  981. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum(""));
  982. CPPUNIT_ASSERT_EQUAL(4294967295U, util::alphaToNum("NXMRLXV"));
  983. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("NXMRLXW")); // uint32_t overflow
  984. }
  985. void UtilTest::testMkdirs()
  986. {
  987. std::string dir = A2_TEST_OUT_DIR"/aria2-UtilTest-testMkdirs";
  988. File d(dir);
  989. if(d.exists()) {
  990. CPPUNIT_ASSERT(d.remove());
  991. }
  992. CPPUNIT_ASSERT(!d.exists());
  993. util::mkdirs(dir);
  994. CPPUNIT_ASSERT(d.isDir());
  995. std::string file = A2_TEST_DIR"/UtilTest.cc";
  996. File f(file);
  997. CPPUNIT_ASSERT(f.isFile());
  998. try {
  999. util::mkdirs(file);
  1000. CPPUNIT_FAIL("exception must be thrown.");
  1001. } catch(DlAbortEx& ex) {
  1002. std::cerr << ex.stackTrace() << std::endl;
  1003. }
  1004. }
  1005. void UtilTest::testConvertBitfield()
  1006. {
  1007. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  1008. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  1009. srcBitfield.setAllBit();
  1010. srcBitfield.unsetBit(2);// <- range [768, 1152)
  1011. // which corresponds to the index [1,2] in destBitfield
  1012. util::convertBitfield(&destBitfield, &srcBitfield);
  1013. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  1014. util::toHex(destBitfield.getBitfield(),
  1015. destBitfield.getBitfieldLength()));
  1016. }
  1017. void UtilTest::testParseIntSegments()
  1018. {
  1019. SegList<int> sgl;
  1020. util::parseIntSegments(sgl, "1,3-8,10");
  1021. CPPUNIT_ASSERT(sgl.hasNext());
  1022. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  1023. CPPUNIT_ASSERT(sgl.hasNext());
  1024. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  1025. CPPUNIT_ASSERT(sgl.hasNext());
  1026. CPPUNIT_ASSERT_EQUAL(4, sgl.next());
  1027. CPPUNIT_ASSERT(sgl.hasNext());
  1028. CPPUNIT_ASSERT_EQUAL(5, sgl.next());
  1029. CPPUNIT_ASSERT(sgl.hasNext());
  1030. CPPUNIT_ASSERT_EQUAL(6, sgl.next());
  1031. CPPUNIT_ASSERT(sgl.hasNext());
  1032. CPPUNIT_ASSERT_EQUAL(7, sgl.next());
  1033. CPPUNIT_ASSERT(sgl.hasNext());
  1034. CPPUNIT_ASSERT_EQUAL(8, sgl.next());
  1035. CPPUNIT_ASSERT(sgl.hasNext());
  1036. CPPUNIT_ASSERT_EQUAL(10, sgl.next());
  1037. CPPUNIT_ASSERT(!sgl.hasNext());
  1038. CPPUNIT_ASSERT_EQUAL(0, sgl.next());
  1039. sgl.clear();
  1040. util::parseIntSegments(sgl, ",,,1,,,3,,,");
  1041. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  1042. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  1043. CPPUNIT_ASSERT(!sgl.hasNext());
  1044. }
  1045. void UtilTest::testParseIntSegments_invalidRange()
  1046. {
  1047. try {
  1048. SegList<int> sgl;
  1049. util::parseIntSegments(sgl, "-1");
  1050. CPPUNIT_FAIL("exception must be thrown.");
  1051. } catch(Exception& e) {
  1052. }
  1053. try {
  1054. SegList<int> sgl;
  1055. util::parseIntSegments(sgl, "1-");
  1056. CPPUNIT_FAIL("exception must be thrown.");
  1057. } catch(Exception& e) {
  1058. }
  1059. try {
  1060. SegList<int> sgl;
  1061. util::parseIntSegments(sgl, "2147483648");
  1062. CPPUNIT_FAIL("exception must be thrown.");
  1063. } catch(Exception& e) {
  1064. }
  1065. try {
  1066. SegList<int> sgl;
  1067. util::parseIntSegments(sgl, "2147483647-2147483648");
  1068. CPPUNIT_FAIL("exception must be thrown.");
  1069. } catch(Exception& e) {
  1070. }
  1071. try {
  1072. SegList<int> sgl;
  1073. util::parseIntSegments(sgl, "1-2x");
  1074. CPPUNIT_FAIL("exception must be thrown.");
  1075. } catch(Exception& e) {
  1076. }
  1077. try {
  1078. SegList<int> sgl;
  1079. util::parseIntSegments(sgl, "3x-4");
  1080. CPPUNIT_FAIL("exception must be thrown.");
  1081. } catch(Exception& e) {
  1082. }
  1083. }
  1084. void UtilTest::testParseInt()
  1085. {
  1086. std::string s;
  1087. s = " -1 ";
  1088. CPPUNIT_ASSERT_EQUAL(-1, util::parseInt(s));
  1089. s = "2147483647";
  1090. CPPUNIT_ASSERT_EQUAL(2147483647, util::parseInt(s));
  1091. try {
  1092. s = "2147483648";
  1093. util::parseInt(s);
  1094. CPPUNIT_FAIL("exception must be thrown.");
  1095. } catch(Exception& e) {
  1096. }
  1097. try {
  1098. s = "-2147483649";
  1099. util::parseInt(s);
  1100. CPPUNIT_FAIL("exception must be thrown.");
  1101. } catch(Exception& e) {
  1102. }
  1103. try {
  1104. s = "12x";
  1105. util::parseInt(s);
  1106. CPPUNIT_FAIL("exception must be thrown.");
  1107. } catch(Exception& e) {
  1108. }
  1109. try {
  1110. s = "";
  1111. util::parseInt(s);
  1112. CPPUNIT_FAIL("exception must be thrown.");
  1113. } catch(Exception& e) {
  1114. }
  1115. }
  1116. void UtilTest::testParseUInt()
  1117. {
  1118. std::string s;
  1119. s = " 2147483647 ";
  1120. CPPUNIT_ASSERT_EQUAL(2147483647U, util::parseUInt(s));
  1121. try {
  1122. s = "-1";
  1123. util::parseUInt(s);
  1124. CPPUNIT_FAIL("exception must be thrown.");
  1125. } catch(Exception& e) {
  1126. }
  1127. try {
  1128. s = "2147483648";
  1129. util::parseUInt(s);
  1130. CPPUNIT_FAIL("exception must be thrown.");
  1131. } catch(Exception& e) {
  1132. }
  1133. }
  1134. void UtilTest::testParseLLInt()
  1135. {
  1136. std::string s;
  1137. {
  1138. s = " -1 ";
  1139. CPPUNIT_ASSERT_EQUAL((int64_t)-1LL, util::parseLLInt(s));
  1140. }
  1141. {
  1142. s = "9223372036854775807";
  1143. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
  1144. util::parseLLInt(s));
  1145. }
  1146. try {
  1147. s = "9223372036854775808";
  1148. util::parseLLInt(s);
  1149. CPPUNIT_FAIL("exception must be thrown.");
  1150. } catch(Exception& e) {
  1151. std::cerr << e.stackTrace();
  1152. }
  1153. try {
  1154. s = "-9223372036854775809";
  1155. util::parseLLInt(s);
  1156. CPPUNIT_FAIL("exception must be thrown.");
  1157. } catch(Exception& e) {
  1158. std::cerr << e.stackTrace();
  1159. }
  1160. try {
  1161. s = "12x";
  1162. util::parseLLInt(s);
  1163. CPPUNIT_FAIL("exception must be thrown.");
  1164. } catch(Exception& e) {
  1165. std::cerr << e.stackTrace();
  1166. }
  1167. try {
  1168. s = "";
  1169. util::parseLLInt(s);
  1170. CPPUNIT_FAIL("exception must be thrown.");
  1171. } catch(Exception& e) {
  1172. std::cerr << e.stackTrace();
  1173. }
  1174. }
  1175. void UtilTest::testParseIntNoThrow()
  1176. {
  1177. std::string s;
  1178. int32_t n;
  1179. s = " -1 ";
  1180. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s));
  1181. CPPUNIT_ASSERT_EQUAL((int32_t)-1, n);
  1182. s = "2147483647";
  1183. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s));
  1184. CPPUNIT_ASSERT_EQUAL((int32_t)2147483647, n);
  1185. s = "2147483648";
  1186. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1187. s = "-2147483649";
  1188. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1189. s = "12x";
  1190. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1191. s = "";
  1192. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1193. }
  1194. void UtilTest::testParseUIntNoThrow()
  1195. {
  1196. std::string s;
  1197. uint32_t n;
  1198. s = " 2147483647 ";
  1199. CPPUNIT_ASSERT(util::parseUIntNoThrow(n, s));
  1200. CPPUNIT_ASSERT_EQUAL((uint32_t)INT32_MAX, n);
  1201. s = "2147483648";
  1202. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s));
  1203. s = "-1";
  1204. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s));
  1205. }
  1206. void UtilTest::testParseLLIntNoThrow()
  1207. {
  1208. std::string s;
  1209. int64_t n;
  1210. s = " 9223372036854775807 ";
  1211. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s));
  1212. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MAX, n);
  1213. s = "9223372036854775808";
  1214. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s));
  1215. s = "-9223372036854775808";
  1216. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s));
  1217. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MIN, n);
  1218. s = "-9223372036854775809";
  1219. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s));
  1220. }
  1221. void UtilTest::testToString_binaryStream()
  1222. {
  1223. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  1224. std::string data(16*1024+256, 'a');
  1225. dw->initAndOpenFile();
  1226. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  1227. std::string readData = util::toString(dw);
  1228. CPPUNIT_ASSERT_EQUAL(data, readData);
  1229. }
  1230. void UtilTest::testItos()
  1231. {
  1232. {
  1233. int i = 0;
  1234. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::itos(i));
  1235. }
  1236. {
  1237. int i = 100;
  1238. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i, true));
  1239. }
  1240. {
  1241. int i = 100;
  1242. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i));
  1243. }
  1244. {
  1245. int i = 12345;
  1246. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), util::itos(i, true));
  1247. }
  1248. {
  1249. int i = 12345;
  1250. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::itos(i));
  1251. }
  1252. {
  1253. int i = -12345;
  1254. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), util::itos(i, true));
  1255. }
  1256. {
  1257. int64_t i = INT64_MAX;
  1258. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"),
  1259. util::itos(i, true));
  1260. }
  1261. {
  1262. int64_t i = INT64_MIN;
  1263. CPPUNIT_ASSERT_EQUAL(std::string("-9,223,372,036,854,775,808"),
  1264. util::itos(i, true));
  1265. }
  1266. }
  1267. void UtilTest::testUitos()
  1268. {
  1269. {
  1270. uint16_t i = 12345;
  1271. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::uitos(i));
  1272. }
  1273. {
  1274. int16_t i = -12345;
  1275. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), util::uitos(i));
  1276. }
  1277. }
  1278. void UtilTest::testNtoh64()
  1279. {
  1280. uint64_t x = 0xff00ff00ee00ee00LL;
  1281. #ifdef WORDS_BIGENDIAN
  1282. CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
  1283. CPPUNIT_ASSERT_EQUAL(x, hton64(x));
  1284. #else // !WORDS_BIGENDIAN
  1285. uint64_t y = 0x00ee00ee00ff00ffLL;
  1286. CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
  1287. CPPUNIT_ASSERT_EQUAL(x, hton64(y));
  1288. #endif // !WORDS_BIGENDIAN
  1289. }
  1290. void UtilTest::testPercentEncode()
  1291. {
  1292. CPPUNIT_ASSERT_EQUAL
  1293. (std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
  1294. util::percentEncode(":/?#[]@!%&'()*+,;="));
  1295. std::string unreserved =
  1296. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1297. "abcdefghijklmnopqrstuvwxyz"
  1298. "0123456789"
  1299. "-._~";
  1300. CPPUNIT_ASSERT_EQUAL(unreserved, util::percentEncode(unreserved));
  1301. CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::percentEncode("1^A "));
  1302. }
  1303. void UtilTest::testHtmlEscape()
  1304. {
  1305. CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),
  1306. util::htmlEscape("aria2<>\"'util"));
  1307. }
  1308. void UtilTest::testJoinPath()
  1309. {
  1310. const std::string dir1dir2file[] = { "dir1", "dir2", "file" };
  1311. CPPUNIT_ASSERT_EQUAL
  1312. (std::string("dir1/dir2/file"),
  1313. util::joinPath(vbegin(dir1dir2file), vend(dir1dir2file)));
  1314. const std::string dirparentfile[] = { "dir", "..", "file" };
  1315. CPPUNIT_ASSERT_EQUAL
  1316. (std::string("file"),
  1317. util::joinPath(vbegin(dirparentfile), vend(dirparentfile)));
  1318. const std::string dirparentparentfile[] = { "dir", "..", "..", "file" };
  1319. CPPUNIT_ASSERT_EQUAL
  1320. (std::string("file"),
  1321. util::joinPath(vbegin(dirparentparentfile), vend(dirparentparentfile)));
  1322. const std::string dirdotfile[] = { "dir", ".", "file" };
  1323. CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
  1324. util::joinPath(vbegin(dirdotfile), vend(dirdotfile)));
  1325. const std::string empty[] = {};
  1326. CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(&empty[0], &empty[0]));
  1327. const std::string parentdot[] = { "..", "." };
  1328. CPPUNIT_ASSERT_EQUAL(std::string(""),
  1329. util::joinPath(vbegin(parentdot), vend(parentdot)));
  1330. }
  1331. void UtilTest::testParseIndexPath()
  1332. {
  1333. std::pair<size_t, std::string> p = util::parseIndexPath("1=foo");
  1334. CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
  1335. CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
  1336. try {
  1337. util::parseIndexPath("1X=foo");
  1338. CPPUNIT_FAIL("exception must be thrown.");
  1339. } catch(Exception& e) {
  1340. // success
  1341. }
  1342. try {
  1343. util::parseIndexPath("1=");
  1344. CPPUNIT_FAIL("exception must be thrown.");
  1345. } catch(Exception& e) {
  1346. // success
  1347. }
  1348. }
  1349. void UtilTest::testCreateIndexPaths()
  1350. {
  1351. std::stringstream in
  1352. ("1=/tmp/myfile\n"
  1353. "100=/myhome/mypicture.png\n");
  1354. std::vector<std::pair<size_t, std::string> > m = util::createIndexPaths(in);
  1355. CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
  1356. CPPUNIT_ASSERT_EQUAL((size_t)1, m[0].first);
  1357. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[0].second);
  1358. CPPUNIT_ASSERT_EQUAL((size_t)100, m[1].first);
  1359. CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[1].second);
  1360. }
  1361. void UtilTest::testGenerateRandomData()
  1362. {
  1363. unsigned char data1[20];
  1364. util::generateRandomData(data1, sizeof(data1));
  1365. unsigned char data2[20];
  1366. util::generateRandomData(data2, sizeof(data2));
  1367. CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
  1368. }
  1369. void UtilTest::testFromHex()
  1370. {
  1371. std::string src;
  1372. std::string dest;
  1373. src = "0011fF";
  1374. dest = util::fromHex(src.begin(), src.end());
  1375. CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
  1376. CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
  1377. CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
  1378. CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
  1379. src = "0011f";
  1380. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  1381. src = "001g";
  1382. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  1383. }
  1384. void UtilTest::testParsePrioritizePieceRange()
  1385. {
  1386. // piece index
  1387. // 0 1 2 3 4 5 6 7
  1388. // | | | |
  1389. // file1 | | |
  1390. // | | |
  1391. // file2 | |
  1392. // file3 |
  1393. // | |
  1394. // file4 |
  1395. size_t pieceLength = 1024;
  1396. std::vector<SharedHandle<FileEntry> > entries(4, SharedHandle<FileEntry>());
  1397. entries[0].reset(new FileEntry("file1", 1024, 0));
  1398. entries[1].reset(new FileEntry("file2",2560,entries[0]->getLastOffset()));
  1399. entries[2].reset(new FileEntry("file3",0,entries[1]->getLastOffset()));
  1400. entries[3].reset(new FileEntry("file4",3584,entries[2]->getLastOffset()));
  1401. std::vector<size_t> result;
  1402. util::parsePrioritizePieceRange(result, "head=1", entries, pieceLength);
  1403. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1404. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1405. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1406. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1407. result.clear();
  1408. util::parsePrioritizePieceRange(result, "tail=1", entries, pieceLength);
  1409. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1410. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1411. CPPUNIT_ASSERT_EQUAL((size_t)3, result[1]);
  1412. CPPUNIT_ASSERT_EQUAL((size_t)6, result[2]);
  1413. result.clear();
  1414. util::parsePrioritizePieceRange(result, "head=1K", entries, pieceLength);
  1415. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1416. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1417. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1418. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1419. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1420. result.clear();
  1421. util::parsePrioritizePieceRange(result, "head", entries, pieceLength, 1024);
  1422. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1423. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1424. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1425. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1426. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1427. result.clear();
  1428. util::parsePrioritizePieceRange(result, "tail=1K", entries, pieceLength);
  1429. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1430. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1431. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1432. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1433. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1434. result.clear();
  1435. util::parsePrioritizePieceRange(result, "tail", entries, pieceLength, 1024);
  1436. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1437. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1438. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1439. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1440. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1441. result.clear();
  1442. util::parsePrioritizePieceRange(result, "head=1,tail=1", entries, pieceLength);
  1443. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1444. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1445. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1446. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1447. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1448. result.clear();
  1449. util::parsePrioritizePieceRange(result, "head=300M,tail=300M",
  1450. entries, pieceLength);
  1451. CPPUNIT_ASSERT_EQUAL((size_t)7, result.size());
  1452. for(size_t i = 0; i < 7; ++i) {
  1453. CPPUNIT_ASSERT_EQUAL(i, result[i]);
  1454. }
  1455. result.clear();
  1456. util::parsePrioritizePieceRange(result, "", entries, pieceLength);
  1457. CPPUNIT_ASSERT(result.empty());
  1458. }
  1459. void UtilTest::testApplyDir()
  1460. {
  1461. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir("", "pred"));
  1462. CPPUNIT_ASSERT_EQUAL(std::string("/pred"), util::applyDir("/", "pred"));
  1463. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir(".", "pred"));
  1464. CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred"));
  1465. }
  1466. void UtilTest::testFixTaintedBasename()
  1467. {
  1468. CPPUNIT_ASSERT_EQUAL(std::string("a%2Fb"), util::fixTaintedBasename("a/b"));
  1469. #ifdef __MINGW32__
  1470. CPPUNIT_ASSERT_EQUAL(std::string("a%5Cb"), util::fixTaintedBasename("a\\b"));
  1471. #else // !__MINGW32__
  1472. CPPUNIT_ASSERT_EQUAL(std::string("a\\b"), util::fixTaintedBasename("a\\b"));
  1473. #endif // !__MINGW32__
  1474. }
  1475. void UtilTest::testIsNumericHost()
  1476. {
  1477. CPPUNIT_ASSERT(util::isNumericHost("192.168.0.1"));
  1478. CPPUNIT_ASSERT(!util::isNumericHost("aria2.sf.net"));
  1479. CPPUNIT_ASSERT(util::isNumericHost("::1"));
  1480. }
  1481. void UtilTest::testDetectDirTraversal()
  1482. {
  1483. CPPUNIT_ASSERT(util::detectDirTraversal("/foo"));
  1484. CPPUNIT_ASSERT(util::detectDirTraversal("./foo"));
  1485. CPPUNIT_ASSERT(util::detectDirTraversal("../foo"));
  1486. CPPUNIT_ASSERT(util::detectDirTraversal("foo/../bar"));
  1487. CPPUNIT_ASSERT(util::detectDirTraversal("foo/./bar"));
  1488. CPPUNIT_ASSERT(util::detectDirTraversal("foo/."));
  1489. CPPUNIT_ASSERT(util::detectDirTraversal("foo/.."));
  1490. CPPUNIT_ASSERT(util::detectDirTraversal("."));
  1491. CPPUNIT_ASSERT(util::detectDirTraversal(".."));
  1492. CPPUNIT_ASSERT(util::detectDirTraversal("/"));
  1493. CPPUNIT_ASSERT(util::detectDirTraversal("foo/"));
  1494. CPPUNIT_ASSERT(util::detectDirTraversal("\t"));
  1495. CPPUNIT_ASSERT(!util::detectDirTraversal("foo/bar"));
  1496. CPPUNIT_ASSERT(!util::detectDirTraversal("foo"));
  1497. }
  1498. void UtilTest::testEscapePath()
  1499. {
  1500. CPPUNIT_ASSERT_EQUAL(std::string("foo%00bar%00%01"),
  1501. util::escapePath(std::string("foo")+(char)0x00+
  1502. std::string("bar")+(char)0x00+
  1503. (char)0x01));
  1504. #ifdef __MINGW32__
  1505. CPPUNIT_ASSERT_EQUAL(std::string("foo%5Cbar"), util::escapePath("foo\\bar"));
  1506. #else // !__MINGW32__
  1507. CPPUNIT_ASSERT_EQUAL(std::string("foo\\bar"), util::escapePath("foo\\bar"));
  1508. #endif // !__MINGW32__
  1509. }
  1510. void UtilTest::testInSameCidrBlock()
  1511. {
  1512. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 16));
  1513. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 17));
  1514. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "192.168.0.1", 32));
  1515. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.0.1", "192.168.0.0", 32));
  1516. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "10.0.0.1", 0));
  1517. CPPUNIT_ASSERT(util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 28));
  1518. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 29));
  1519. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "192.168.0.1", 8));
  1520. }
  1521. void UtilTest::testIsUtf8String()
  1522. {
  1523. CPPUNIT_ASSERT(util::isUtf8("ascii"));
  1524. // "Hello World" in Japanese UTF-8
  1525. CPPUNIT_ASSERT(util::isUtf8
  1526. (fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
  1527. // "World" in Shift_JIS
  1528. CPPUNIT_ASSERT(!util::isUtf8(fromHex("90a28a")+"E"));
  1529. // UTF8-2
  1530. CPPUNIT_ASSERT(util::isUtf8(fromHex("c280")));
  1531. CPPUNIT_ASSERT(util::isUtf8(fromHex("dfbf")));
  1532. // UTF8-3
  1533. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0a080")));
  1534. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0bf80")));
  1535. CPPUNIT_ASSERT(util::isUtf8(fromHex("e18080")));
  1536. CPPUNIT_ASSERT(util::isUtf8(fromHex("ec8080")));
  1537. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed8080")));
  1538. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed9f80")));
  1539. CPPUNIT_ASSERT(util::isUtf8(fromHex("ee8080")));
  1540. CPPUNIT_ASSERT(util::isUtf8(fromHex("ef8080")));
  1541. // UTF8-4
  1542. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0908080")));
  1543. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0bf8080")));
  1544. CPPUNIT_ASSERT(util::isUtf8(fromHex("f1808080")));
  1545. CPPUNIT_ASSERT(util::isUtf8(fromHex("f3808080")));
  1546. CPPUNIT_ASSERT(util::isUtf8(fromHex("f4808080")));
  1547. CPPUNIT_ASSERT(util::isUtf8(fromHex("f48f8080")));
  1548. CPPUNIT_ASSERT(util::isUtf8(""));
  1549. CPPUNIT_ASSERT(!util::isUtf8(fromHex("00")));
  1550. }
  1551. void UtilTest::testNextParam()
  1552. {
  1553. std::string s1 = " :a : b=c :d=b::::g::";
  1554. std::pair<std::string::iterator, bool> r;
  1555. std::string name, value;
  1556. r = util::nextParam(name, value, s1.begin(), s1.end(), ':');
  1557. CPPUNIT_ASSERT(r.second);
  1558. CPPUNIT_ASSERT_EQUAL(std::string("a"), name);
  1559. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1560. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1561. CPPUNIT_ASSERT(r.second);
  1562. CPPUNIT_ASSERT_EQUAL(std::string("b"), name);
  1563. CPPUNIT_ASSERT_EQUAL(std::string("c"), value);
  1564. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1565. CPPUNIT_ASSERT(r.second);
  1566. CPPUNIT_ASSERT_EQUAL(std::string("d"), name);
  1567. CPPUNIT_ASSERT_EQUAL(std::string("b"), value);
  1568. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1569. CPPUNIT_ASSERT(r.second);
  1570. CPPUNIT_ASSERT_EQUAL(std::string("g"), name);
  1571. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1572. std::string s2 = "";
  1573. r = util::nextParam(name, value, s2.begin(), s2.end(), ':');
  1574. CPPUNIT_ASSERT(!r.second);
  1575. std::string s3 = " ";
  1576. r = util::nextParam(name, value, s3.begin(), s3.end(), ':');
  1577. CPPUNIT_ASSERT(!r.second);
  1578. std::string s4 = ":::";
  1579. r = util::nextParam(name, value, s4.begin(), s4.end(), ':');
  1580. CPPUNIT_ASSERT(!r.second);
  1581. }
  1582. void UtilTest::testNoProxyDomainMatch()
  1583. {
  1584. CPPUNIT_ASSERT(util::noProxyDomainMatch("localhost", "localhost"));
  1585. CPPUNIT_ASSERT(util::noProxyDomainMatch("192.168.0.1", "192.168.0.1"));
  1586. CPPUNIT_ASSERT(util::noProxyDomainMatch("www.example.org", ".example.org"));
  1587. CPPUNIT_ASSERT(!util::noProxyDomainMatch("www.example.org", "example.org"));
  1588. CPPUNIT_ASSERT(!util::noProxyDomainMatch("192.168.0.1", "0.1"));
  1589. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "example.com"));
  1590. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org"));
  1591. }
  1592. void UtilTest::testInPrivateAddress()
  1593. {
  1594. CPPUNIT_ASSERT(!util::inPrivateAddress("localhost"));
  1595. CPPUNIT_ASSERT(util::inPrivateAddress("192.168.0.1"));
  1596. // Only checks prefix..
  1597. CPPUNIT_ASSERT(util::inPrivateAddress("10."));
  1598. CPPUNIT_ASSERT(!util::inPrivateAddress("172."));
  1599. CPPUNIT_ASSERT(!util::inPrivateAddress("172.15.0.0"));
  1600. CPPUNIT_ASSERT(util::inPrivateAddress("172.16.0.0"));
  1601. CPPUNIT_ASSERT(util::inPrivateAddress("172.31.0.0"));
  1602. CPPUNIT_ASSERT(!util::inPrivateAddress("172.32.0.0"));
  1603. }
  1604. void UtilTest::testSecfmt()
  1605. {
  1606. CPPUNIT_ASSERT_EQUAL(std::string("0s"), util::secfmt(0));
  1607. CPPUNIT_ASSERT_EQUAL(std::string("1s"), util::secfmt(1));
  1608. CPPUNIT_ASSERT_EQUAL(std::string("9s"), util::secfmt(9));
  1609. CPPUNIT_ASSERT_EQUAL(std::string("10s"), util::secfmt(10));
  1610. CPPUNIT_ASSERT_EQUAL(std::string("1m"), util::secfmt(60));
  1611. CPPUNIT_ASSERT_EQUAL(std::string("1m59s"), util::secfmt(119));
  1612. CPPUNIT_ASSERT_EQUAL(std::string("2m"), util::secfmt(120));
  1613. CPPUNIT_ASSERT_EQUAL(std::string("59m59s"), util::secfmt(3599));
  1614. CPPUNIT_ASSERT_EQUAL(std::string("1h"), util::secfmt(3600));
  1615. }
  1616. void UtilTest::testTlsHostnameMatch()
  1617. {
  1618. CPPUNIT_ASSERT(util::tlsHostnameMatch("Foo.com", "foo.com"));
  1619. CPPUNIT_ASSERT(util::tlsHostnameMatch("*.a.com", "foo.a.com"));
  1620. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*.a.com", "bar.foo.a.com"));
  1621. CPPUNIT_ASSERT(!util::tlsHostnameMatch("f*.com", "foo.com"));
  1622. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*.com", "bar.com"));
  1623. CPPUNIT_ASSERT(util::tlsHostnameMatch("com", "com"));
  1624. CPPUNIT_ASSERT(!util::tlsHostnameMatch("foo.*", "foo.com"));
  1625. CPPUNIT_ASSERT(util::tlsHostnameMatch("a.foo.com", "A.foo.com"));
  1626. CPPUNIT_ASSERT(!util::tlsHostnameMatch("a.foo.com", "b.foo.com"));
  1627. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*a.foo.com", "a.foo.com"));
  1628. CPPUNIT_ASSERT(util::tlsHostnameMatch("*a.foo.com", "ba.foo.com"));
  1629. CPPUNIT_ASSERT(!util::tlsHostnameMatch("a*.foo.com", "a.foo.com"));
  1630. CPPUNIT_ASSERT(util::tlsHostnameMatch("a*.foo.com", "ab.foo.com"));
  1631. CPPUNIT_ASSERT(!util::tlsHostnameMatch("foo.b*z.foo.com", "foo.baz.foo.com"));
  1632. CPPUNIT_ASSERT(util::tlsHostnameMatch("B*z.foo.com", "bAZ.Foo.com"));
  1633. CPPUNIT_ASSERT(!util::tlsHostnameMatch("b*z.foo.com", "bz.foo.com"));
  1634. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*", "foo"));
  1635. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*", ""));
  1636. CPPUNIT_ASSERT(util::tlsHostnameMatch("", ""));
  1637. CPPUNIT_ASSERT(!util::tlsHostnameMatch("xn--*.a.b", "xn--c.a.b"));
  1638. }
  1639. } // namespace aria2