UtilTest.cc 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  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(testParseContentDisposition);
  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(testMkdirs);
  50. CPPUNIT_TEST(testConvertBitfield);
  51. CPPUNIT_TEST(testParseIntSegments);
  52. CPPUNIT_TEST(testParseIntSegments_invalidRange);
  53. CPPUNIT_TEST(testParseIntNoThrow);
  54. CPPUNIT_TEST(testParseUIntNoThrow);
  55. CPPUNIT_TEST(testParseLLIntNoThrow);
  56. CPPUNIT_TEST(testToString_binaryStream);
  57. CPPUNIT_TEST(testItos);
  58. CPPUNIT_TEST(testUitos);
  59. CPPUNIT_TEST(testNtoh64);
  60. CPPUNIT_TEST(testPercentEncode);
  61. CPPUNIT_TEST(testHtmlEscape);
  62. CPPUNIT_TEST(testJoinPath);
  63. CPPUNIT_TEST(testParseIndexPath);
  64. CPPUNIT_TEST(testCreateIndexPaths);
  65. CPPUNIT_TEST(testGenerateRandomData);
  66. CPPUNIT_TEST(testFromHex);
  67. CPPUNIT_TEST(testParsePrioritizePieceRange);
  68. CPPUNIT_TEST(testApplyDir);
  69. CPPUNIT_TEST(testFixTaintedBasename);
  70. CPPUNIT_TEST(testIsNumericHost);
  71. CPPUNIT_TEST(testDetectDirTraversal);
  72. CPPUNIT_TEST(testEscapePath);
  73. CPPUNIT_TEST(testInSameCidrBlock);
  74. CPPUNIT_TEST(testIsUtf8String);
  75. CPPUNIT_TEST(testNextParam);
  76. CPPUNIT_TEST(testNoProxyDomainMatch);
  77. CPPUNIT_TEST(testInPrivateAddress);
  78. CPPUNIT_TEST(testSecfmt);
  79. CPPUNIT_TEST(testTlsHostnameMatch);
  80. CPPUNIT_TEST_SUITE_END();
  81. private:
  82. public:
  83. void setUp() {
  84. }
  85. void testStrip();
  86. void testStripIter();
  87. void testLstripIter();
  88. void testLstripIter_char();
  89. void testDivide();
  90. void testSplit();
  91. void testSplitIter();
  92. void testSplitIterM();
  93. void testStreq();
  94. void testStrieq();
  95. void testStrifind();
  96. void testEndsWith();
  97. void testIendsWith();
  98. void testReplace();
  99. void testStartsWith();
  100. void testIstartsWith();
  101. // may be moved to other helper class in the future.
  102. void testGetContentDispositionFilename();
  103. void testParseContentDisposition();
  104. void testToUpper();
  105. void testToLower();
  106. void testUppercase();
  107. void testLowercase();
  108. void testPercentDecode();
  109. void testGetRealSize();
  110. void testAbbrevSize();
  111. void testToStream();
  112. void testIsNumber();
  113. void testIsLowercase();
  114. void testIsUppercase();
  115. void testMkdirs();
  116. void testConvertBitfield();
  117. void testParseIntSegments();
  118. void testParseIntSegments_invalidRange();
  119. void testParseIntNoThrow();
  120. void testParseUIntNoThrow();
  121. void testParseLLIntNoThrow();
  122. void testToString_binaryStream();
  123. void testItos();
  124. void testUitos();
  125. void testNtoh64();
  126. void testPercentEncode();
  127. void testHtmlEscape();
  128. void testJoinPath();
  129. void testParseIndexPath();
  130. void testCreateIndexPaths();
  131. void testGenerateRandomData();
  132. void testFromHex();
  133. void testParsePrioritizePieceRange();
  134. void testApplyDir();
  135. void testFixTaintedBasename();
  136. void testIsNumericHost();
  137. void testDetectDirTraversal();
  138. void testEscapePath();
  139. void testInSameCidrBlock();
  140. void testIsUtf8String();
  141. void testNextParam();
  142. void testNoProxyDomainMatch();
  143. void testInPrivateAddress();
  144. void testSecfmt();
  145. void testTlsHostnameMatch();
  146. };
  147. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  148. void UtilTest::testStrip()
  149. {
  150. std::string str1 = "aria2";
  151. CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2"));
  152. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2"));
  153. CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2 "));
  154. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
  155. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
  156. std::string str2 = "aria2 debut";
  157. CPPUNIT_ASSERT_EQUAL(str2, util::strip("aria2 debut"));
  158. CPPUNIT_ASSERT_EQUAL(str2, util::strip(" aria2 debut "));
  159. std::string str3 = "";
  160. CPPUNIT_ASSERT_EQUAL(str3, util::strip(""));
  161. CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
  162. CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
  163. std::string str4 = "A";
  164. CPPUNIT_ASSERT_EQUAL(str4, util::strip("A"));
  165. CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
  166. CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
  167. }
  168. void UtilTest::testStripIter()
  169. {
  170. Scip p;
  171. std::string str1 = "aria2";
  172. std::string s = "aria2";
  173. p = util::stripIter(s.begin(), s.end());
  174. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  175. s = " aria2";
  176. p = util::stripIter(s.begin(), s.end());
  177. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  178. s = "aria2 ";
  179. p = util::stripIter(s.begin(), s.end());
  180. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  181. s = " aria2 ";
  182. p = util::stripIter(s.begin(), s.end());
  183. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  184. s = " aria2 ";
  185. p = util::stripIter(s.begin(), s.end());
  186. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  187. std::string str2 = "aria2 debut";
  188. s = "aria2 debut";
  189. p = util::stripIter(s.begin(), s.end());
  190. CPPUNIT_ASSERT_EQUAL(str2, std::string(p.first, p.second));
  191. s = " aria2 debut ";
  192. p = util::stripIter(s.begin(), s.end());
  193. CPPUNIT_ASSERT_EQUAL(str2, std::string(p.first, p.second));
  194. std::string str3 = "";
  195. s = "";
  196. p = util::stripIter(s.begin(), s.end());
  197. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  198. s = " ";
  199. p = util::stripIter(s.begin(), s.end());
  200. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  201. s = " ";
  202. p = util::stripIter(s.begin(), s.end());
  203. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  204. std::string str4 = "A";
  205. s = "A";
  206. p = util::stripIter(s.begin(), s.end());
  207. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  208. s = " A ";
  209. p = util::stripIter(s.begin(), s.end());
  210. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  211. s = " A ";
  212. p = util::stripIter(s.begin(), s.end());
  213. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  214. }
  215. void UtilTest::testLstripIter()
  216. {
  217. std::string::iterator r;
  218. std::string s = "foo";
  219. r = util::lstripIter(s.begin(), s.end());
  220. CPPUNIT_ASSERT_EQUAL(std::string("foo"), std::string(r, s.end()));
  221. s = " foo bar ";
  222. r = util::lstripIter(s.begin(), s.end());
  223. CPPUNIT_ASSERT_EQUAL(std::string("foo bar "), std::string(r, s.end()));
  224. s = "f";
  225. r = util::lstripIter(s.begin(), s.end());
  226. CPPUNIT_ASSERT_EQUAL(std::string("f"), std::string(r, s.end()));
  227. s = "foo ";
  228. r = util::lstripIter(s.begin(), s.end());
  229. CPPUNIT_ASSERT_EQUAL(std::string("foo "), std::string(r, s.end()));
  230. }
  231. void UtilTest::testLstripIter_char()
  232. {
  233. std::string::iterator r;
  234. std::string s = "foo";
  235. r = util::lstripIter(s.begin(), s.end(), '$');
  236. CPPUNIT_ASSERT_EQUAL(std::string("foo"), std::string(r, s.end()));
  237. s = "$$foo$bar$$";
  238. r = util::lstripIter(s.begin(), s.end(), '$');
  239. CPPUNIT_ASSERT_EQUAL(std::string("foo$bar$$"), std::string(r, s.end()));
  240. s = "f";
  241. r = util::lstripIter(s.begin(), s.end(), '$');
  242. CPPUNIT_ASSERT_EQUAL(std::string("f"), std::string(r, s.end()));
  243. s = "foo$$";
  244. r = util::lstripIter(s.begin(), s.end(), '$');
  245. CPPUNIT_ASSERT_EQUAL(std::string("foo$$"), std::string(r, s.end()));
  246. }
  247. void UtilTest::testDivide() {
  248. std::pair<Sip, Sip> p1;
  249. std::string s = "name=value";
  250. util::divide(p1, s.begin(), s.end(), '=');
  251. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  252. std::string(p1.first.first, p1.first.second));
  253. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  254. std::string(p1.second.first, p1.second.second));
  255. s = " name = value ";
  256. util::divide(p1, s.begin(), s.end(), '=');
  257. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  258. std::string(p1.first.first, p1.first.second));
  259. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  260. std::string(p1.second.first, p1.second.second));
  261. s = "=value";
  262. util::divide(p1, s.begin(), s.end(), '=');
  263. CPPUNIT_ASSERT_EQUAL(std::string(""),
  264. std::string(p1.first.first, p1.first.second));
  265. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  266. std::string(p1.second.first, p1.second.second));
  267. s = "name=";
  268. util::divide(p1, s.begin(), s.end(), '=');
  269. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  270. std::string(p1.first.first, p1.first.second));
  271. CPPUNIT_ASSERT_EQUAL(std::string(""),
  272. std::string(p1.second.first, p1.second.second));
  273. s = "name";
  274. util::divide(p1, s.begin(), s.end(), '=');
  275. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  276. std::string(p1.first.first, p1.first.second));
  277. CPPUNIT_ASSERT_EQUAL(std::string(""),
  278. std::string(p1.second.first, p1.second.second));
  279. }
  280. void UtilTest::testSplit() {
  281. std::vector<std::string> v;
  282. std::string s = "k1; k2;; k3";
  283. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true);
  284. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  285. std::vector<std::string>::iterator itr = v.begin();
  286. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  287. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  288. CPPUNIT_ASSERT_EQUAL(std::string("k3"), *itr++);
  289. v.clear();
  290. s = "k1; k2; k3";
  291. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  292. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  293. 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 = "k=v";
  299. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  300. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  301. itr = v.begin();
  302. CPPUNIT_ASSERT_EQUAL(std::string("k=v"), *itr++);
  303. v.clear();
  304. s = ";;k1;;k2;";
  305. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  306. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  307. itr = v.begin();
  308. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  309. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  310. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  311. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  312. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  313. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  314. v.clear();
  315. s = ";;k1;;k2;";
  316. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  317. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  318. itr = v.begin();
  319. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  320. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  321. v.clear();
  322. s = "k; ";
  323. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  324. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  325. itr = v.begin();
  326. CPPUNIT_ASSERT_EQUAL(std::string("k"), *itr++);
  327. CPPUNIT_ASSERT_EQUAL(std::string(" "), *itr++);
  328. v.clear();
  329. s = " ";
  330. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true, true);
  331. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  332. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  333. v.clear();
  334. s = " ";
  335. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true);
  336. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  337. v.clear();
  338. s = " ";
  339. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  340. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  341. CPPUNIT_ASSERT_EQUAL(std::string(" "), v[0]);
  342. v.clear();
  343. s = ";";
  344. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  345. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  346. v.clear();
  347. s = ";";
  348. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  349. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  350. itr = v.begin();
  351. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  352. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  353. v.clear();
  354. s = "";
  355. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  356. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  357. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  358. }
  359. void UtilTest::testSplitIter() {
  360. std::vector<Scip> v;
  361. std::string s = "k1; k2;; k3";
  362. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true);
  363. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  364. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  365. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  366. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  367. v.clear();
  368. s = "k1; k2; k3";
  369. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  370. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  371. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  372. CPPUNIT_ASSERT_EQUAL(std::string(" k2"),
  373. std::string(v[1].first, v[1].second));
  374. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  375. std::string(v[2].first, v[2].second));
  376. v.clear();
  377. s = "k=v";
  378. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  379. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  380. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  381. std::string(v[0].first, v[0].second));
  382. v.clear();
  383. s = ";;k1;;k2;";
  384. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  385. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  386. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  387. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  388. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  389. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  390. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  391. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  392. v.clear();
  393. s = ";;k1;;k2;";
  394. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  395. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  396. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  397. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  398. v.clear();
  399. s = "k; ";
  400. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  401. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  402. CPPUNIT_ASSERT_EQUAL(std::string("k"), std::string(v[0].first, v[0].second));
  403. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  404. v.clear();
  405. s = " ";
  406. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true, true);
  407. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  408. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  409. v.clear();
  410. s = " ";
  411. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true);
  412. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  413. v.clear();
  414. s = " ";
  415. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  416. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  417. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[0].first, v[0].second));
  418. v.clear();
  419. s = ";";
  420. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  421. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  422. v.clear();
  423. s = ";";
  424. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  425. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  426. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  427. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  428. v.clear();
  429. s = "";
  430. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  431. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  432. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  433. }
  434. void UtilTest::testSplitIterM() {
  435. const char d[] = ";";
  436. const char md[] = "; ";
  437. std::vector<Scip> v;
  438. std::string s = "k1; k2;; k3";
  439. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true);
  440. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  441. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  442. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  443. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  444. v.clear();
  445. s = "k1; k2; k3";
  446. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  447. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  448. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  449. CPPUNIT_ASSERT_EQUAL(std::string(" k2"),
  450. std::string(v[1].first, v[1].second));
  451. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  452. std::string(v[2].first, v[2].second));
  453. v.clear();
  454. s = "k1; k2; k3";
  455. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), md);
  456. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  457. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  458. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  459. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  460. v.clear();
  461. s = "k1; k2; k3;";
  462. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), md, false, true);
  463. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  464. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  465. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  466. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[2].first, v[2].second));
  467. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  468. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[4].first, v[4].second));
  469. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  470. v.clear();
  471. s = "k=v";
  472. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  473. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  474. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  475. std::string(v[0].first, v[0].second));
  476. v.clear();
  477. s = ";;k1;;k2;";
  478. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  479. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  480. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  481. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  482. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  483. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  484. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  485. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  486. v.clear();
  487. s = ";;k1;;k2;";
  488. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  489. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  490. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  491. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  492. v.clear();
  493. s = "k; ";
  494. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  495. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  496. CPPUNIT_ASSERT_EQUAL(std::string("k"), std::string(v[0].first, v[0].second));
  497. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  498. v.clear();
  499. s = " ";
  500. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true, true);
  501. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  502. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  503. v.clear();
  504. s = " ";
  505. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true);
  506. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  507. v.clear();
  508. s = " ";
  509. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  510. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  511. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[0].first, v[0].second));
  512. v.clear();
  513. s = ";";
  514. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  515. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  516. v.clear();
  517. s = ";";
  518. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  519. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  520. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  521. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  522. v.clear();
  523. s = "";
  524. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  525. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  526. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  527. }
  528. void UtilTest::testEndsWith() {
  529. std::string target = "abcdefg";
  530. std::string part = "fg";
  531. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  532. part.begin(), part.end()));
  533. target = "abdefg";
  534. part = "g";
  535. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  536. part.begin(), part.end()));
  537. target = "abdefg";
  538. part = "eg";
  539. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  540. part.begin(), part.end()));
  541. target = "g";
  542. part = "eg";
  543. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  544. part.begin(), part.end()));
  545. target = "g";
  546. part = "g";
  547. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  548. part.begin(), part.end()));
  549. target = "g";
  550. part = "";
  551. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  552. part.begin(), part.end()));
  553. target = "";
  554. part = "";
  555. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  556. part.begin(), part.end()));
  557. target = "";
  558. part = "g";
  559. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  560. part.begin(), part.end()));
  561. }
  562. void UtilTest::testIendsWith() {
  563. std::string target = "abcdefg";
  564. std::string part = "Fg";
  565. CPPUNIT_ASSERT(util::iendsWith(target.begin(), target.end(),
  566. part.begin(), part.end()));
  567. target = "abdefg";
  568. part = "ef";
  569. CPPUNIT_ASSERT(!util::iendsWith(target.begin(), target.end(),
  570. part.begin(), part.end()));
  571. }
  572. void UtilTest::testStreq()
  573. {
  574. std::string s1, s2;
  575. s1 = "foo";
  576. s2 = "foo";
  577. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  578. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.c_str()));
  579. s2 = "fooo";
  580. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  581. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  582. s2 = "fo";
  583. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  584. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  585. s2 = "";
  586. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  587. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  588. s1 = "";
  589. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  590. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.c_str()));
  591. }
  592. void UtilTest::testStrieq()
  593. {
  594. std::string s1, s2;
  595. s1 = "foo";
  596. s2 = "foo";
  597. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  598. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  599. s1 = "FoO";
  600. s2 = "fOo";
  601. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  602. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  603. s2 = "fooo";
  604. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  605. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  606. s2 = "fo";
  607. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  608. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  609. s2 = "";
  610. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  611. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  612. s1 = "";
  613. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  614. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  615. }
  616. void UtilTest::testStrifind()
  617. {
  618. std::string s1, s2;
  619. s1 = "yamagakani mukashi wo toheba hARU no tuki";
  620. s2 = "HaRu";
  621. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  622. != s1.end());
  623. s2 = "aki";
  624. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  625. == s1.end());
  626. s1 = "h";
  627. s2 = "HH";
  628. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  629. == s1.end());
  630. }
  631. void UtilTest::testReplace() {
  632. CPPUNIT_ASSERT_EQUAL(std::string("abc\n"), util::replace("abc\r\n", "\r", ""));
  633. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc\r\n", "\r\n", ""));
  634. CPPUNIT_ASSERT_EQUAL(std::string(""), util::replace("", "\r\n", ""));
  635. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc", "", "a"));
  636. CPPUNIT_ASSERT_EQUAL(std::string("xbc"), util::replace("abc", "a", "x"));
  637. }
  638. void UtilTest::testStartsWith() {
  639. std::string target;
  640. std::string part;
  641. target = "abcdefg";
  642. part = "abc";
  643. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  644. part.begin(), part.end()));
  645. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  646. target = "abcdefg";
  647. part = "abx";
  648. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  649. part.begin(), part.end()));
  650. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
  651. target = "abcdefg";
  652. part = "bcd";
  653. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  654. part.begin(), part.end()));
  655. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
  656. target = "";
  657. part = "a";
  658. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  659. part.begin(), part.end()));
  660. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
  661. target = "";
  662. part = "";
  663. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  664. part.begin(), part.end()));
  665. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  666. target = "a";
  667. part = "";
  668. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  669. part.begin(), part.end()));
  670. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  671. target = "a";
  672. part = "a";
  673. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  674. part.begin(), part.end()));
  675. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  676. }
  677. void UtilTest::testIstartsWith() {
  678. std::string target;
  679. std::string part;
  680. target = "abcdefg";
  681. part = "aBc";
  682. CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(),
  683. part.begin(), part.end()));
  684. CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(), part.c_str()));
  685. target = "abcdefg";
  686. part = "abx";
  687. CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(),
  688. part.begin(), part.end()));
  689. CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(), part.c_str()));
  690. }
  691. void UtilTest::testGetContentDispositionFilename() {
  692. std::string val;
  693. val = "attachment; filename=\"aria2.tar.bz2\"";
  694. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  695. util::getContentDispositionFilename(val));
  696. val = "attachment; filename=\"\"";
  697. CPPUNIT_ASSERT_EQUAL(std::string(""),
  698. util::getContentDispositionFilename(val));
  699. val = "attachment; filename=\"";
  700. CPPUNIT_ASSERT_EQUAL(std::string(""),
  701. util::getContentDispositionFilename(val));
  702. val = "attachment; filename= \" aria2.tar.bz2 \"";
  703. CPPUNIT_ASSERT_EQUAL(std::string(" aria2.tar.bz2 "),
  704. util::getContentDispositionFilename(val));
  705. val = "attachment; filename=dir/file";
  706. CPPUNIT_ASSERT_EQUAL(std::string(""),
  707. util::getContentDispositionFilename(val));
  708. val = "attachment; filename=dir\\file";
  709. CPPUNIT_ASSERT_EQUAL(std::string(""),
  710. util::getContentDispositionFilename(val));
  711. val = "attachment; filename=\"dir/file\"";
  712. CPPUNIT_ASSERT_EQUAL(std::string(""),
  713. util::getContentDispositionFilename(val));
  714. val = "attachment; filename=\"dir\\\\file\"";
  715. CPPUNIT_ASSERT_EQUAL(std::string(""),
  716. util::getContentDispositionFilename(val));
  717. val = "attachment; filename=\"/etc/passwd\"";
  718. CPPUNIT_ASSERT_EQUAL(std::string(""),
  719. util::getContentDispositionFilename(val));
  720. val = "attachment; filename=\"..\"";
  721. CPPUNIT_ASSERT_EQUAL(std::string(""),
  722. util::getContentDispositionFilename(val));
  723. val = "attachment; filename=..";
  724. CPPUNIT_ASSERT_EQUAL(std::string(""),
  725. util::getContentDispositionFilename(val));
  726. // Unescaping %2E%2E%2F produces "../". But since we won't unescape,
  727. // we just accept it as is.
  728. val = "attachment; filename=\"%2E%2E%2Ffoo.html\"";
  729. CPPUNIT_ASSERT_EQUAL(std::string("%2E%2E%2Ffoo.html"),
  730. util::getContentDispositionFilename(val));
  731. // iso-8859-1 string will be converted to utf-8.
  732. val = "attachment; filename*=iso-8859-1''foo-%E4.html";
  733. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  734. util::getContentDispositionFilename(val));
  735. val = "attachment; filename*= UTF-8''foo-%c3%a4.html";
  736. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  737. util::getContentDispositionFilename(val));
  738. // iso-8859-1 string will be converted to utf-8.
  739. val = "attachment; filename=\"foo-%E4.html\"";
  740. val = util::percentDecode(val.begin(), val.end());
  741. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  742. util::getContentDispositionFilename(val));
  743. }
  744. void UtilTest::testParseContentDisposition() {
  745. char dest[1024];
  746. size_t destlen = sizeof(dest);
  747. const char *cs;
  748. size_t cslen;
  749. std::string val;
  750. // test cases from http://greenbytes.de/tech/tc2231/
  751. // inlonly
  752. val = "inline";
  753. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  754. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  755. // inlonlyquoted
  756. val = "\"inline\"";
  757. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  758. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  759. // inlwithasciifilename
  760. val = "inline; filename=\"foo.html\"";
  761. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  762. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  763. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  764. std::string(&dest[0], &dest[8]));
  765. // inlwithfnattach
  766. val = "inline; filename=\"Not an attachment!\"";
  767. CPPUNIT_ASSERT_EQUAL((ssize_t)18, util::parse_content_disposition
  768. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  769. CPPUNIT_ASSERT_EQUAL(std::string("Not an attachment!"),
  770. std::string(&dest[0], &dest[18]));
  771. // inlwithasciifilenamepdf
  772. val = "inline; filename=\"foo.pdf\"";
  773. CPPUNIT_ASSERT_EQUAL((ssize_t)7, util::parse_content_disposition
  774. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  775. CPPUNIT_ASSERT_EQUAL(std::string("foo.pdf"),
  776. std::string(&dest[0], &dest[7]));
  777. // attwithasciifilename25
  778. val = "attachment; filename=\"0000000000111111111122222\"";
  779. CPPUNIT_ASSERT_EQUAL((ssize_t)25, util::parse_content_disposition
  780. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  781. CPPUNIT_ASSERT_EQUAL(std::string("0000000000111111111122222"),
  782. std::string(&dest[0], &dest[25]));
  783. // attwithasciifilename35
  784. val = "attachment; filename=\"00000000001111111111222222222233333\"";
  785. CPPUNIT_ASSERT_EQUAL((ssize_t)35, util::parse_content_disposition
  786. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  787. CPPUNIT_ASSERT_EQUAL(std::string("00000000001111111111222222222233333"),
  788. std::string(&dest[0], &dest[35]));
  789. // attwithasciifnescapedchar
  790. val = "attachment; filename=\"f\\oo.html\"";
  791. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  792. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  793. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  794. std::string(&dest[0], &dest[8]));
  795. // attwithasciifnescapedquote
  796. val = "attachment; filename=\"\\\"quoting\\\" tested.html\"";
  797. CPPUNIT_ASSERT_EQUAL((ssize_t)21, util::parse_content_disposition
  798. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  799. CPPUNIT_ASSERT_EQUAL(std::string("\"quoting\" tested.html"),
  800. std::string(&dest[0], &dest[21]));
  801. // attwithquotedsemicolon
  802. val = "attachment; filename=\"Here's a semicolon;.html\"";
  803. CPPUNIT_ASSERT_EQUAL((ssize_t)24, util::parse_content_disposition
  804. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  805. CPPUNIT_ASSERT_EQUAL(std::string("Here's a semicolon;.html"),
  806. std::string(&dest[0], &dest[24]));
  807. // attwithfilenameandextparam
  808. val = "attachment; foo=\"bar\"; filename=\"foo.html\"";
  809. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  810. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  811. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  812. std::string(&dest[0], &dest[8]));
  813. // attwithfilenameandextparamescaped
  814. val = "attachment; foo=\"\\\"\\\\\";filename=\"foo.html\"";
  815. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  816. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  817. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  818. std::string(&dest[0], &dest[8]));
  819. // attwithasciifilenameucase
  820. val = "attachment; FILENAME=\"foo.html\"";
  821. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  822. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  823. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  824. std::string(&dest[0], &dest[8]));
  825. // attwithasciifilenamenq
  826. val = "attachment; filename=foo.html";
  827. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  828. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  829. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  830. std::string(&dest[0], &dest[8]));
  831. // attwithtokfncommanq
  832. val = "attachment; filename=foo,bar.html";
  833. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  834. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  835. // attwithasciifilenamenqs
  836. val = "attachment; filename=foo.html ;";
  837. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  838. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  839. // attemptyparam
  840. val = "attachment; ;filename=foo";
  841. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  842. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  843. // attwithasciifilenamenqws
  844. val = "attachment; filename=foo bar.html";
  845. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  846. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  847. // attwithfntokensq
  848. val = "attachment; filename='foo.bar'";
  849. CPPUNIT_ASSERT_EQUAL((ssize_t)9, util::parse_content_disposition
  850. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  851. CPPUNIT_ASSERT_EQUAL(std::string("'foo.bar'"),
  852. std::string(&dest[0], &dest[9]));
  853. // attwithisofnplain
  854. // attachment; filename="foo-ä.html"
  855. val = "attachment; filename=\"foo-%E4.html\"";
  856. val = util::percentDecode(val.begin(), val.end());
  857. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  858. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  859. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  860. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[10])));
  861. // attwithutf8fnplain
  862. // attachment; filename="foo-ä.html"
  863. val = "attachment; filename=\"foo-%C3%A4.html\"";
  864. val = util::percentDecode(val.begin(), val.end());
  865. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  866. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  867. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  868. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[11])));
  869. // attwithfnrawpctenca
  870. val = "attachment; filename=\"foo-%41.html\"";
  871. CPPUNIT_ASSERT_EQUAL((ssize_t)12, util::parse_content_disposition
  872. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  873. CPPUNIT_ASSERT_EQUAL(std::string("foo-%41.html"),
  874. std::string(&dest[0], &dest[12]));
  875. // attwithfnusingpct
  876. val = "attachment; filename=\"50%.html\"";
  877. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  878. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  879. CPPUNIT_ASSERT_EQUAL(std::string("50%.html"),
  880. std::string(&dest[0], &dest[8]));
  881. // attwithfnrawpctencaq
  882. val = "attachment; filename=\"foo-%\\41.html\"";
  883. CPPUNIT_ASSERT_EQUAL((ssize_t)12, util::parse_content_disposition
  884. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  885. CPPUNIT_ASSERT_EQUAL(std::string("foo-%41.html"),
  886. std::string(&dest[0], &dest[12]));
  887. // attwithnamepct
  888. val = "attachment; name=\"foo-%41.html\"";
  889. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  890. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  891. // attwithfilenamepctandiso
  892. // attachment; filename="ä-%41.html"
  893. val = "attachment; filename=\"%E4-%2541.html\"";
  894. val = util::percentDecode(val.begin(), val.end());
  895. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  896. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  897. CPPUNIT_ASSERT_EQUAL(std::string("ä-%41.html"),
  898. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[10])));
  899. // attwithfnrawpctenclong
  900. val = "attachment; filename=\"foo-%c3%a4-%e2%82%ac.html\"";
  901. CPPUNIT_ASSERT_EQUAL((ssize_t)25, util::parse_content_disposition
  902. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  903. CPPUNIT_ASSERT_EQUAL(std::string("foo-%c3%a4-%e2%82%ac.html"),
  904. std::string(&dest[0], &dest[25]));
  905. // attwithasciifilenamews1
  906. val = "attachment; filename =\"foo.html\"";
  907. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  908. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  909. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  910. std::string(&dest[0], &dest[8]));
  911. // attwith2filenames
  912. val = "attachment; filename=\"foo.html\"; filename=\"bar.html\"";
  913. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  914. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  915. // attfnbrokentoken
  916. val = "attachment; filename=foo[1](2).html";
  917. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  918. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  919. // attfnbrokentokeniso
  920. val = "attachment; filename=foo-%E4.html";
  921. val = util::percentDecode(val.begin(), val.end());
  922. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  923. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  924. // attfnbrokentokenutf
  925. // attachment; filename=foo-ä.html
  926. val = "attachment; filename=foo-ä.html";
  927. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  928. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  929. // attmissingdisposition
  930. val = "filename=foo.html";
  931. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  932. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  933. // attmissingdisposition2
  934. val = "x=y; filename=foo.html";
  935. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  936. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  937. // attmissingdisposition3
  938. val = "\"foo; filename=bar;baz\"; filename=qux";
  939. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  940. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  941. // attmissingdisposition4
  942. val = "filename=foo.html, filename=bar.html";
  943. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  944. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  945. // emptydisposition
  946. val = "; filename=foo.html";
  947. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  948. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  949. // doublecolon
  950. val = ": inline; attachment; filename=foo.html";
  951. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  952. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  953. // attandinline
  954. val = "inline; attachment; filename=foo.html";
  955. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  956. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  957. // attandinline2
  958. val = "attachment; inline; filename=foo.html";
  959. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  960. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  961. // attbrokenquotedfn
  962. val = "attachment; filename=\"foo.html\".txt";
  963. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  964. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  965. // attbrokenquotedfn2
  966. val = "attachment; filename=\"bar";
  967. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  968. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  969. // attbrokenquotedfn3
  970. val = "attachment; filename=foo\"bar;baz\"qux";
  971. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  972. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  973. // attmultinstances
  974. val = "attachment; filename=foo.html, attachment; filename=bar.html";
  975. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  976. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  977. // attmissingdelim
  978. val = "attachment; foo=foo filename=bar";
  979. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  980. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  981. // attmissingdelim2
  982. val = "attachment; filename=bar foo=foo ";
  983. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  984. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  985. // attmissingdelim3
  986. val = "attachment filename=bar";
  987. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  988. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  989. // attreversed
  990. val = "filename=foo.html; attachment";
  991. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  992. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  993. // attconfusedparam
  994. val = "attachment; xfilename=foo.html";
  995. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  996. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  997. // attabspath
  998. val = "attachment; filename=\"/foo.html\"";
  999. CPPUNIT_ASSERT_EQUAL((ssize_t)9, util::parse_content_disposition
  1000. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1001. CPPUNIT_ASSERT_EQUAL(std::string("/foo.html"),
  1002. std::string(&dest[0], &dest[9]));
  1003. // attabspathwin
  1004. val = "attachment; filename=\"\\\\foo.html\"";
  1005. CPPUNIT_ASSERT_EQUAL((ssize_t)9, util::parse_content_disposition
  1006. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1007. CPPUNIT_ASSERT_EQUAL(std::string("\\foo.html"),
  1008. std::string(&dest[0], &dest[9]));
  1009. // attcdate
  1010. val = "attachment; creation-date=\"Wed, 12 Feb 1997 16:29:51 -0500\"";
  1011. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1012. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1013. // dispext
  1014. val = "foobar";
  1015. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1016. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1017. // dispextbadfn
  1018. val = "attachment; example=\"filename=example.txt\"";
  1019. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1020. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1021. // attwithisofn2231iso
  1022. val = "attachment; filename*=iso-8859-1''foo-%E4.html";
  1023. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  1024. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1025. CPPUNIT_ASSERT_EQUAL(std::string("iso-8859-1"), std::string(cs, cslen));
  1026. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1027. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[10])));
  1028. // attwithfn2231utf8
  1029. val = "attachment; filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html";
  1030. CPPUNIT_ASSERT_EQUAL((ssize_t)15, util::parse_content_disposition
  1031. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1032. CPPUNIT_ASSERT_EQUAL(std::string("UTF-8"), std::string(cs, cslen));
  1033. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä-€.html"),
  1034. std::string(&dest[0], &dest[15]));
  1035. // attwithfn2231noc
  1036. val = "attachment; filename*=''foo-%c3%a4-%e2%82%ac.html";
  1037. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1038. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1039. // attwithfn2231utf8comp
  1040. val = "attachment; filename*=UTF-8''foo-a%cc%88.html";
  1041. CPPUNIT_ASSERT_EQUAL((ssize_t)12, util::parse_content_disposition
  1042. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1043. val = "foo-a%cc%88.html";
  1044. CPPUNIT_ASSERT_EQUAL(std::string(util::percentDecode(val.begin(),
  1045. val.end())),
  1046. std::string(&dest[0], &dest[12]));
  1047. // attwithfn2231utf8-bad
  1048. val = "attachment; filename*=iso-8859-1''foo-%c3%a4-%e2%82%ac.html";
  1049. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1050. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1051. // attwithfn2231iso-bad
  1052. val = "attachment; filename*=utf-8''foo-%E4.html";
  1053. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1054. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1055. // attwithfn2231ws1
  1056. val = "attachment; filename *=UTF-8''foo-%c3%a4.html";
  1057. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1058. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1059. // attwithfn2231ws2
  1060. val = "attachment; filename*= UTF-8''foo-%c3%a4.html";
  1061. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  1062. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1063. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1064. std::string(&dest[0], &dest[11]));
  1065. // attwithfn2231ws3
  1066. val = "attachment; filename* =UTF-8''foo-%c3%a4.html";
  1067. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  1068. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1069. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1070. std::string(&dest[0], &dest[11]));
  1071. // attwithfn2231quot
  1072. val = "attachment; filename*=\"UTF-8''foo-%c3%a4.html\"";
  1073. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1074. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1075. // attwithfn2231quot2
  1076. val = "attachment; filename*=\"foo%20bar.html\"";
  1077. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1078. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1079. // attwithfn2231singleqmissing
  1080. val = "attachment; filename*=UTF-8'foo-%c3%a4.html";
  1081. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1082. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1083. // attwithfn2231nbadpct1
  1084. val = "attachment; filename*=UTF-8''foo%";
  1085. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1086. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1087. // attwithfn2231nbadpct2
  1088. val = "attachment; filename*=UTF-8''f%oo.html";
  1089. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1090. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1091. // attwithfn2231dpct
  1092. val = "attachment; filename*=UTF-8''A-%2541.html";
  1093. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  1094. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1095. CPPUNIT_ASSERT_EQUAL(std::string("A-%41.html"),
  1096. std::string(&dest[0], &dest[10]));
  1097. // attwithfn2231abspathdisguised
  1098. val = "attachment; filename*=UTF-8''%5cfoo.html";
  1099. CPPUNIT_ASSERT_EQUAL((ssize_t)9, util::parse_content_disposition
  1100. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1101. CPPUNIT_ASSERT_EQUAL(std::string("\\foo.html"),
  1102. std::string(&dest[0], &dest[9]));
  1103. // attfnboth
  1104. val = "attachment; filename=\"foo-ae.html\"; filename*=UTF-8''foo-%c3%a4.html";
  1105. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  1106. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1107. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1108. std::string(&dest[0], &dest[11]));
  1109. // attfnboth2
  1110. val = "attachment; filename*=UTF-8''foo-%c3%a4.html; filename=\"foo-ae.html\"";
  1111. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  1112. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1113. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1114. std::string(&dest[0], &dest[11]));
  1115. // attfnboth3
  1116. val = "attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4; filename*=ISO-8859-1''currency-sign%3d%a4";
  1117. CPPUNIT_ASSERT_EQUAL((ssize_t)15, util::parse_content_disposition
  1118. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1119. CPPUNIT_ASSERT_EQUAL(std::string("ISO-8859-1"), std::string(cs, cslen));
  1120. CPPUNIT_ASSERT_EQUAL(std::string("currency-sign=¤"),
  1121. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[15])));
  1122. // attnewandfn
  1123. val = "attachment; foobar=x; filename=\"foo.html\"";
  1124. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1125. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1126. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1127. std::string(&dest[0], &dest[8]));
  1128. // attrfc2047token
  1129. val = "attachment; filename==?ISO-8859-1?Q?foo-=E4.html?=";
  1130. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1131. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1132. // attrfc2047quoted
  1133. val = "attachment; filename=\"=?ISO-8859-1?Q?foo-=E4.html?=\"";
  1134. CPPUNIT_ASSERT_EQUAL((ssize_t)29, util::parse_content_disposition
  1135. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1136. CPPUNIT_ASSERT_EQUAL(std::string("=?ISO-8859-1?Q?foo-=E4.html?="),
  1137. std::string(&dest[0], &dest[29]));
  1138. // aria2 original testcases
  1139. // zero-length filename. token cannot be empty, so this is invalid.
  1140. val = "attachment; filename=";
  1141. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1142. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1143. // zero-length filename. quoted-string can be empty string, so this
  1144. // is ok.
  1145. val = "attachment; filename=\"\"";
  1146. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1147. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1148. // empty value is not allowed
  1149. val = "attachment; filename=;";
  1150. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1151. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1152. // / is not valid char in token.
  1153. val = "attachment; filename=dir/file";
  1154. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1155. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1156. // value-chars is *(pct-encoded / attr-char), so empty string is
  1157. // allowed.
  1158. val = "attachment; filename*=UTF-8''";
  1159. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1160. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1161. CPPUNIT_ASSERT_EQUAL(std::string("UTF-8"), std::string(cs, cslen));
  1162. val = "attachment; filename*=UTF-8''; filename=foo";
  1163. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1164. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1165. CPPUNIT_ASSERT_EQUAL(std::string("UTF-8"), std::string(cs, cslen));
  1166. val = "attachment; filename*=UTF-8'' ; filename=foo";
  1167. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1168. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1169. CPPUNIT_ASSERT_EQUAL(std::string("UTF-8"), std::string(cs, cslen));
  1170. // with language
  1171. val = "attachment; filename*=UTF-8'japanese'konnichiwa";
  1172. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  1173. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1174. CPPUNIT_ASSERT_EQUAL(std::string("konnichiwa"),
  1175. std::string(&dest[0], &dest[10]));
  1176. // lws before and after "="
  1177. val = "attachment; filename = foo.html";
  1178. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1179. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1180. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1181. std::string(&dest[0], &dest[8]));
  1182. // lws before and after "=" with quoted-string
  1183. val = "attachment; filename = \"foo.html\"";
  1184. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1185. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1186. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1187. std::string(&dest[0], &dest[8]));
  1188. // lws after parm
  1189. val = "attachment; filename=foo.html ";
  1190. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1191. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1192. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1193. std::string(&dest[0], &dest[8]));
  1194. val = "attachment; filename=foo.html ; hello=world";
  1195. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1196. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1197. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1198. std::string(&dest[0], &dest[8]));
  1199. val = "attachment; filename=\"foo.html\" ";
  1200. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1201. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1202. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1203. std::string(&dest[0], &dest[8]));
  1204. val = "attachment; filename=\"foo.html\" ; hello=world";
  1205. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1206. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1207. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1208. std::string(&dest[0], &dest[8]));
  1209. val = "attachment; filename*=UTF-8''foo.html ; hello=world";
  1210. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1211. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1212. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1213. std::string(&dest[0], &dest[8]));
  1214. }
  1215. class Printer {
  1216. public:
  1217. template<class T>
  1218. void operator()(T t) {
  1219. std::cerr << t << ", ";
  1220. }
  1221. };
  1222. void UtilTest::testToUpper() {
  1223. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  1224. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  1225. CPPUNIT_ASSERT_EQUAL(upp, util::toUpper(src));
  1226. }
  1227. void UtilTest::testToLower() {
  1228. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  1229. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  1230. CPPUNIT_ASSERT_EQUAL(upp, util::toLower(src));
  1231. }
  1232. void UtilTest::testUppercase() {
  1233. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  1234. std::string ans = "608CABC0F2FA18C260CAFD974516865C772363D5";
  1235. util::uppercase(src);
  1236. CPPUNIT_ASSERT_EQUAL(ans, src);
  1237. }
  1238. void UtilTest::testLowercase() {
  1239. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  1240. std::string ans = "608cabc0f2fa18c260cafd974516865c772363d5";
  1241. util::lowercase(src);
  1242. CPPUNIT_ASSERT_EQUAL(ans, src);
  1243. }
  1244. void UtilTest::testPercentDecode() {
  1245. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  1246. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  1247. util::percentDecode(src.begin(), src.end()));
  1248. std::string src2 = "aria2+aria2";
  1249. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"),
  1250. util::percentDecode(src2.begin(), src2.end()));
  1251. std::string src3 = "%5t%20";
  1252. CPPUNIT_ASSERT_EQUAL(std::string("%5t "),
  1253. util::percentDecode(src3.begin(), src3.end()));
  1254. std::string src4 = "%";
  1255. CPPUNIT_ASSERT_EQUAL(std::string("%"),
  1256. util::percentDecode(src4.begin(), src4.end()));
  1257. std::string src5 = "%3";
  1258. CPPUNIT_ASSERT_EQUAL(std::string("%3"),
  1259. util::percentDecode(src5.begin(), src5.end()));
  1260. std::string src6 = "%2f";
  1261. CPPUNIT_ASSERT_EQUAL(std::string("/"),
  1262. util::percentDecode(src6.begin(), src6.end()));
  1263. }
  1264. void UtilTest::testGetRealSize()
  1265. {
  1266. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096M"));
  1267. CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1K"));
  1268. try {
  1269. util::getRealSize("");
  1270. CPPUNIT_FAIL("exception must be thrown.");
  1271. } catch(Exception& e) {
  1272. std::cerr << e.stackTrace();
  1273. }
  1274. try {
  1275. util::getRealSize("foo");
  1276. CPPUNIT_FAIL("exception must be thrown.");
  1277. } catch(Exception& e) {
  1278. std::cerr << e.stackTrace();
  1279. }
  1280. try {
  1281. util::getRealSize("-1");
  1282. CPPUNIT_FAIL("exception must be thrown.");
  1283. } catch(Exception& e) {
  1284. std::cerr << e.stackTrace();
  1285. }
  1286. try {
  1287. util::getRealSize("9223372036854775807K");
  1288. CPPUNIT_FAIL("exception must be thrown.");
  1289. } catch(Exception& e) {
  1290. std::cerr << e.stackTrace();
  1291. }
  1292. try {
  1293. util::getRealSize("9223372036854775807M");
  1294. CPPUNIT_FAIL("exception must be thrown.");
  1295. } catch(Exception& e) {
  1296. std::cerr << e.stackTrace();
  1297. }
  1298. }
  1299. void UtilTest::testAbbrevSize()
  1300. {
  1301. CPPUNIT_ASSERT_EQUAL(std::string("8,589,934,591Gi"),
  1302. util::abbrevSize(9223372036854775807LL));
  1303. CPPUNIT_ASSERT_EQUAL(std::string("4.0Gi"), util::abbrevSize(4294967296LL));
  1304. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1024));
  1305. CPPUNIT_ASSERT_EQUAL(std::string("0.9Ki"), util::abbrevSize(1023));
  1306. CPPUNIT_ASSERT_EQUAL(std::string("511"), util::abbrevSize(511));
  1307. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::abbrevSize(0));
  1308. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), util::abbrevSize(1127));
  1309. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), util::abbrevSize(1572864));
  1310. }
  1311. void UtilTest::testToStream()
  1312. {
  1313. std::ostringstream os;
  1314. SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  1315. SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  1316. std::deque<SharedHandle<FileEntry> > entries;
  1317. entries.push_back(f1);
  1318. entries.push_back(f2);
  1319. const char* filename = A2_TEST_OUT_DIR"/aria2_UtilTest_testToStream";
  1320. BufferedFile fp(filename, BufferedFile::WRITE);
  1321. util::toStream(entries.begin(), entries.end(), fp);
  1322. fp.close();
  1323. CPPUNIT_ASSERT_EQUAL(
  1324. std::string("Files:\n"
  1325. "idx|path/length\n"
  1326. "===+===========================================================================\n"
  1327. " 1|aria2.tar.bz2\n"
  1328. " |12KiB (12,300)\n"
  1329. "---+---------------------------------------------------------------------------\n"
  1330. " 2|aria2.txt\n"
  1331. " |556B (556)\n"
  1332. "---+---------------------------------------------------------------------------\n"),
  1333. readFile(filename));
  1334. }
  1335. void UtilTest::testIsNumber()
  1336. {
  1337. std::string s = "000";
  1338. CPPUNIT_ASSERT_EQUAL(true, util::isNumber(s.begin(),s.end()));
  1339. s = "a";
  1340. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  1341. s = "0a";
  1342. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  1343. s = "";
  1344. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  1345. s = " ";
  1346. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  1347. }
  1348. void UtilTest::testIsLowercase()
  1349. {
  1350. std::string s = "alpha";
  1351. CPPUNIT_ASSERT_EQUAL(true, util::isLowercase(s.begin(), s.end()));
  1352. s = "Alpha";
  1353. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  1354. s = "1alpha";
  1355. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  1356. s = "";
  1357. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  1358. s = " ";
  1359. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  1360. }
  1361. void UtilTest::testIsUppercase()
  1362. {
  1363. std::string s = "ALPHA";
  1364. CPPUNIT_ASSERT_EQUAL(true, util::isUppercase(s.begin(), s.end()));
  1365. s = "Alpha";
  1366. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  1367. s = "1ALPHA";
  1368. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  1369. s = "";
  1370. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  1371. s = " ";
  1372. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  1373. }
  1374. void UtilTest::testMkdirs()
  1375. {
  1376. std::string dir = A2_TEST_OUT_DIR"/aria2-UtilTest-testMkdirs";
  1377. File d(dir);
  1378. if(d.exists()) {
  1379. CPPUNIT_ASSERT(d.remove());
  1380. }
  1381. CPPUNIT_ASSERT(!d.exists());
  1382. util::mkdirs(dir);
  1383. CPPUNIT_ASSERT(d.isDir());
  1384. std::string file = A2_TEST_DIR"/UtilTest.cc";
  1385. File f(file);
  1386. CPPUNIT_ASSERT(f.isFile());
  1387. try {
  1388. util::mkdirs(file);
  1389. CPPUNIT_FAIL("exception must be thrown.");
  1390. } catch(DlAbortEx& ex) {
  1391. std::cerr << ex.stackTrace() << std::endl;
  1392. }
  1393. }
  1394. void UtilTest::testConvertBitfield()
  1395. {
  1396. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  1397. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  1398. srcBitfield.setAllBit();
  1399. srcBitfield.unsetBit(2);// <- range [768, 1152)
  1400. // which corresponds to the index [1,2] in destBitfield
  1401. util::convertBitfield(&destBitfield, &srcBitfield);
  1402. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  1403. util::toHex(destBitfield.getBitfield(),
  1404. destBitfield.getBitfieldLength()));
  1405. }
  1406. void UtilTest::testParseIntSegments()
  1407. {
  1408. SegList<int> sgl;
  1409. util::parseIntSegments(sgl, "1,3-8,10");
  1410. CPPUNIT_ASSERT(sgl.hasNext());
  1411. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  1412. CPPUNIT_ASSERT(sgl.hasNext());
  1413. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  1414. CPPUNIT_ASSERT(sgl.hasNext());
  1415. CPPUNIT_ASSERT_EQUAL(4, sgl.next());
  1416. CPPUNIT_ASSERT(sgl.hasNext());
  1417. CPPUNIT_ASSERT_EQUAL(5, sgl.next());
  1418. CPPUNIT_ASSERT(sgl.hasNext());
  1419. CPPUNIT_ASSERT_EQUAL(6, sgl.next());
  1420. CPPUNIT_ASSERT(sgl.hasNext());
  1421. CPPUNIT_ASSERT_EQUAL(7, sgl.next());
  1422. CPPUNIT_ASSERT(sgl.hasNext());
  1423. CPPUNIT_ASSERT_EQUAL(8, sgl.next());
  1424. CPPUNIT_ASSERT(sgl.hasNext());
  1425. CPPUNIT_ASSERT_EQUAL(10, sgl.next());
  1426. CPPUNIT_ASSERT(!sgl.hasNext());
  1427. CPPUNIT_ASSERT_EQUAL(0, sgl.next());
  1428. sgl.clear();
  1429. util::parseIntSegments(sgl, ",,,1,,,3,,,");
  1430. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  1431. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  1432. CPPUNIT_ASSERT(!sgl.hasNext());
  1433. }
  1434. void UtilTest::testParseIntSegments_invalidRange()
  1435. {
  1436. try {
  1437. SegList<int> sgl;
  1438. util::parseIntSegments(sgl, "-1");
  1439. CPPUNIT_FAIL("exception must be thrown.");
  1440. } catch(Exception& e) {
  1441. }
  1442. try {
  1443. SegList<int> sgl;
  1444. util::parseIntSegments(sgl, "1-");
  1445. CPPUNIT_FAIL("exception must be thrown.");
  1446. } catch(Exception& e) {
  1447. }
  1448. try {
  1449. SegList<int> sgl;
  1450. util::parseIntSegments(sgl, "2147483648");
  1451. CPPUNIT_FAIL("exception must be thrown.");
  1452. } catch(Exception& e) {
  1453. }
  1454. try {
  1455. SegList<int> sgl;
  1456. util::parseIntSegments(sgl, "2147483647-2147483648");
  1457. CPPUNIT_FAIL("exception must be thrown.");
  1458. } catch(Exception& e) {
  1459. }
  1460. try {
  1461. SegList<int> sgl;
  1462. util::parseIntSegments(sgl, "1-2x");
  1463. CPPUNIT_FAIL("exception must be thrown.");
  1464. } catch(Exception& e) {
  1465. }
  1466. try {
  1467. SegList<int> sgl;
  1468. util::parseIntSegments(sgl, "3x-4");
  1469. CPPUNIT_FAIL("exception must be thrown.");
  1470. } catch(Exception& e) {
  1471. }
  1472. }
  1473. void UtilTest::testParseIntNoThrow()
  1474. {
  1475. std::string s;
  1476. int32_t n;
  1477. s = " -1 ";
  1478. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s));
  1479. CPPUNIT_ASSERT_EQUAL((int32_t)-1, n);
  1480. s = "2147483647";
  1481. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s));
  1482. CPPUNIT_ASSERT_EQUAL((int32_t)2147483647, n);
  1483. s = "2147483648";
  1484. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1485. s = "-2147483649";
  1486. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1487. s = "12x";
  1488. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1489. s = "";
  1490. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1491. }
  1492. void UtilTest::testParseUIntNoThrow()
  1493. {
  1494. std::string s;
  1495. uint32_t n;
  1496. s = " 2147483647 ";
  1497. CPPUNIT_ASSERT(util::parseUIntNoThrow(n, s));
  1498. CPPUNIT_ASSERT_EQUAL((uint32_t)INT32_MAX, n);
  1499. s = "2147483648";
  1500. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s));
  1501. s = "-1";
  1502. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s));
  1503. }
  1504. void UtilTest::testParseLLIntNoThrow()
  1505. {
  1506. std::string s;
  1507. int64_t n;
  1508. s = " 9223372036854775807 ";
  1509. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s));
  1510. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MAX, n);
  1511. s = "9223372036854775808";
  1512. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s));
  1513. s = "-9223372036854775808";
  1514. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s));
  1515. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MIN, n);
  1516. s = "-9223372036854775809";
  1517. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s));
  1518. }
  1519. void UtilTest::testToString_binaryStream()
  1520. {
  1521. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  1522. std::string data(16*1024+256, 'a');
  1523. dw->initAndOpenFile();
  1524. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  1525. std::string readData = util::toString(dw);
  1526. CPPUNIT_ASSERT_EQUAL(data, readData);
  1527. }
  1528. void UtilTest::testItos()
  1529. {
  1530. {
  1531. int i = 0;
  1532. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::itos(i));
  1533. }
  1534. {
  1535. int i = 100;
  1536. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i, true));
  1537. }
  1538. {
  1539. int i = 100;
  1540. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i));
  1541. }
  1542. {
  1543. int i = 12345;
  1544. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), util::itos(i, true));
  1545. }
  1546. {
  1547. int i = 12345;
  1548. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::itos(i));
  1549. }
  1550. {
  1551. int i = -12345;
  1552. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), util::itos(i, true));
  1553. }
  1554. {
  1555. int64_t i = INT64_MAX;
  1556. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"),
  1557. util::itos(i, true));
  1558. }
  1559. {
  1560. int64_t i = INT64_MIN;
  1561. CPPUNIT_ASSERT_EQUAL(std::string("-9,223,372,036,854,775,808"),
  1562. util::itos(i, true));
  1563. }
  1564. }
  1565. void UtilTest::testUitos()
  1566. {
  1567. {
  1568. uint16_t i = 12345;
  1569. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::uitos(i));
  1570. }
  1571. {
  1572. int16_t i = -12345;
  1573. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), util::uitos(i));
  1574. }
  1575. }
  1576. void UtilTest::testNtoh64()
  1577. {
  1578. uint64_t x = 0xff00ff00ee00ee00LL;
  1579. #ifdef WORDS_BIGENDIAN
  1580. CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
  1581. CPPUNIT_ASSERT_EQUAL(x, hton64(x));
  1582. #else // !WORDS_BIGENDIAN
  1583. uint64_t y = 0x00ee00ee00ff00ffLL;
  1584. CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
  1585. CPPUNIT_ASSERT_EQUAL(x, hton64(y));
  1586. #endif // !WORDS_BIGENDIAN
  1587. }
  1588. void UtilTest::testPercentEncode()
  1589. {
  1590. CPPUNIT_ASSERT_EQUAL
  1591. (std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
  1592. util::percentEncode(":/?#[]@!%&'()*+,;="));
  1593. std::string unreserved =
  1594. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1595. "abcdefghijklmnopqrstuvwxyz"
  1596. "0123456789"
  1597. "-._~";
  1598. CPPUNIT_ASSERT_EQUAL(unreserved, util::percentEncode(unreserved));
  1599. CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::percentEncode("1^A "));
  1600. }
  1601. void UtilTest::testHtmlEscape()
  1602. {
  1603. CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),
  1604. util::htmlEscape("aria2<>\"'util"));
  1605. }
  1606. void UtilTest::testJoinPath()
  1607. {
  1608. const std::string dir1dir2file[] = { "dir1", "dir2", "file" };
  1609. CPPUNIT_ASSERT_EQUAL
  1610. (std::string("dir1/dir2/file"),
  1611. util::joinPath(vbegin(dir1dir2file), vend(dir1dir2file)));
  1612. const std::string dirparentfile[] = { "dir", "..", "file" };
  1613. CPPUNIT_ASSERT_EQUAL
  1614. (std::string("file"),
  1615. util::joinPath(vbegin(dirparentfile), vend(dirparentfile)));
  1616. const std::string dirparentparentfile[] = { "dir", "..", "..", "file" };
  1617. CPPUNIT_ASSERT_EQUAL
  1618. (std::string("file"),
  1619. util::joinPath(vbegin(dirparentparentfile), vend(dirparentparentfile)));
  1620. const std::string dirdotfile[] = { "dir", ".", "file" };
  1621. CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
  1622. util::joinPath(vbegin(dirdotfile), vend(dirdotfile)));
  1623. const std::string empty[] = {};
  1624. CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(&empty[0], &empty[0]));
  1625. const std::string parentdot[] = { "..", "." };
  1626. CPPUNIT_ASSERT_EQUAL(std::string(""),
  1627. util::joinPath(vbegin(parentdot), vend(parentdot)));
  1628. }
  1629. void UtilTest::testParseIndexPath()
  1630. {
  1631. std::pair<size_t, std::string> p = util::parseIndexPath("1=foo");
  1632. CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
  1633. CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
  1634. try {
  1635. util::parseIndexPath("1X=foo");
  1636. CPPUNIT_FAIL("exception must be thrown.");
  1637. } catch(Exception& e) {
  1638. // success
  1639. }
  1640. try {
  1641. util::parseIndexPath("1=");
  1642. CPPUNIT_FAIL("exception must be thrown.");
  1643. } catch(Exception& e) {
  1644. // success
  1645. }
  1646. }
  1647. void UtilTest::testCreateIndexPaths()
  1648. {
  1649. std::stringstream in
  1650. ("1=/tmp/myfile\n"
  1651. "100=/myhome/mypicture.png\n");
  1652. std::vector<std::pair<size_t, std::string> > m = util::createIndexPaths(in);
  1653. CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
  1654. CPPUNIT_ASSERT_EQUAL((size_t)1, m[0].first);
  1655. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[0].second);
  1656. CPPUNIT_ASSERT_EQUAL((size_t)100, m[1].first);
  1657. CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[1].second);
  1658. }
  1659. void UtilTest::testGenerateRandomData()
  1660. {
  1661. unsigned char data1[20];
  1662. util::generateRandomData(data1, sizeof(data1));
  1663. unsigned char data2[20];
  1664. util::generateRandomData(data2, sizeof(data2));
  1665. CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
  1666. }
  1667. void UtilTest::testFromHex()
  1668. {
  1669. std::string src;
  1670. std::string dest;
  1671. src = "0011fF";
  1672. dest = util::fromHex(src.begin(), src.end());
  1673. CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
  1674. CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
  1675. CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
  1676. CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
  1677. src = "0011f";
  1678. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  1679. src = "001g";
  1680. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  1681. }
  1682. void UtilTest::testParsePrioritizePieceRange()
  1683. {
  1684. // piece index
  1685. // 0 1 2 3 4 5 6 7
  1686. // | | | |
  1687. // file1 | | |
  1688. // | | |
  1689. // file2 | |
  1690. // file3 |
  1691. // | |
  1692. // file4 |
  1693. size_t pieceLength = 1024;
  1694. std::vector<SharedHandle<FileEntry> > entries(4, SharedHandle<FileEntry>());
  1695. entries[0].reset(new FileEntry("file1", 1024, 0));
  1696. entries[1].reset(new FileEntry("file2",2560,entries[0]->getLastOffset()));
  1697. entries[2].reset(new FileEntry("file3",0,entries[1]->getLastOffset()));
  1698. entries[3].reset(new FileEntry("file4",3584,entries[2]->getLastOffset()));
  1699. std::vector<size_t> result;
  1700. util::parsePrioritizePieceRange(result, "head=1", entries, pieceLength);
  1701. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1702. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1703. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1704. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1705. result.clear();
  1706. util::parsePrioritizePieceRange(result, "tail=1", entries, pieceLength);
  1707. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1708. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1709. CPPUNIT_ASSERT_EQUAL((size_t)3, result[1]);
  1710. CPPUNIT_ASSERT_EQUAL((size_t)6, result[2]);
  1711. result.clear();
  1712. util::parsePrioritizePieceRange(result, "head=1K", entries, pieceLength);
  1713. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1714. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1715. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1716. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1717. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1718. result.clear();
  1719. util::parsePrioritizePieceRange(result, "head", entries, pieceLength, 1024);
  1720. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1721. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1722. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1723. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1724. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1725. result.clear();
  1726. util::parsePrioritizePieceRange(result, "tail=1K", entries, pieceLength);
  1727. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1728. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1729. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1730. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1731. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1732. result.clear();
  1733. util::parsePrioritizePieceRange(result, "tail", entries, pieceLength, 1024);
  1734. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1735. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1736. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1737. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1738. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1739. result.clear();
  1740. util::parsePrioritizePieceRange(result, "head=1,tail=1", entries, pieceLength);
  1741. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1742. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1743. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1744. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1745. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1746. result.clear();
  1747. util::parsePrioritizePieceRange(result, "head=300M,tail=300M",
  1748. entries, pieceLength);
  1749. CPPUNIT_ASSERT_EQUAL((size_t)7, result.size());
  1750. for(size_t i = 0; i < 7; ++i) {
  1751. CPPUNIT_ASSERT_EQUAL(i, result[i]);
  1752. }
  1753. result.clear();
  1754. util::parsePrioritizePieceRange(result, "", entries, pieceLength);
  1755. CPPUNIT_ASSERT(result.empty());
  1756. }
  1757. void UtilTest::testApplyDir()
  1758. {
  1759. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir("", "pred"));
  1760. CPPUNIT_ASSERT_EQUAL(std::string("/pred"), util::applyDir("/", "pred"));
  1761. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir(".", "pred"));
  1762. CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred"));
  1763. }
  1764. void UtilTest::testFixTaintedBasename()
  1765. {
  1766. CPPUNIT_ASSERT_EQUAL(std::string("a%2Fb"), util::fixTaintedBasename("a/b"));
  1767. #ifdef __MINGW32__
  1768. CPPUNIT_ASSERT_EQUAL(std::string("a%5Cb"), util::fixTaintedBasename("a\\b"));
  1769. #else // !__MINGW32__
  1770. CPPUNIT_ASSERT_EQUAL(std::string("a\\b"), util::fixTaintedBasename("a\\b"));
  1771. #endif // !__MINGW32__
  1772. }
  1773. void UtilTest::testIsNumericHost()
  1774. {
  1775. CPPUNIT_ASSERT(util::isNumericHost("192.168.0.1"));
  1776. CPPUNIT_ASSERT(!util::isNumericHost("aria2.sf.net"));
  1777. CPPUNIT_ASSERT(util::isNumericHost("::1"));
  1778. }
  1779. void UtilTest::testDetectDirTraversal()
  1780. {
  1781. CPPUNIT_ASSERT(util::detectDirTraversal("/foo"));
  1782. CPPUNIT_ASSERT(util::detectDirTraversal("./foo"));
  1783. CPPUNIT_ASSERT(util::detectDirTraversal("../foo"));
  1784. CPPUNIT_ASSERT(util::detectDirTraversal("foo/../bar"));
  1785. CPPUNIT_ASSERT(util::detectDirTraversal("foo/./bar"));
  1786. CPPUNIT_ASSERT(util::detectDirTraversal("foo/."));
  1787. CPPUNIT_ASSERT(util::detectDirTraversal("foo/.."));
  1788. CPPUNIT_ASSERT(util::detectDirTraversal("."));
  1789. CPPUNIT_ASSERT(util::detectDirTraversal(".."));
  1790. CPPUNIT_ASSERT(util::detectDirTraversal("/"));
  1791. CPPUNIT_ASSERT(util::detectDirTraversal("foo/"));
  1792. CPPUNIT_ASSERT(util::detectDirTraversal("\t"));
  1793. CPPUNIT_ASSERT(!util::detectDirTraversal("foo/bar"));
  1794. CPPUNIT_ASSERT(!util::detectDirTraversal("foo"));
  1795. }
  1796. void UtilTest::testEscapePath()
  1797. {
  1798. CPPUNIT_ASSERT_EQUAL(std::string("foo%00bar%00%01"),
  1799. util::escapePath(std::string("foo")+(char)0x00+
  1800. std::string("bar")+(char)0x00+
  1801. (char)0x01));
  1802. #ifdef __MINGW32__
  1803. CPPUNIT_ASSERT_EQUAL(std::string("foo%5Cbar"), util::escapePath("foo\\bar"));
  1804. #else // !__MINGW32__
  1805. CPPUNIT_ASSERT_EQUAL(std::string("foo\\bar"), util::escapePath("foo\\bar"));
  1806. #endif // !__MINGW32__
  1807. }
  1808. void UtilTest::testInSameCidrBlock()
  1809. {
  1810. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 16));
  1811. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 17));
  1812. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "192.168.0.1", 32));
  1813. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.0.1", "192.168.0.0", 32));
  1814. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "10.0.0.1", 0));
  1815. CPPUNIT_ASSERT(util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 28));
  1816. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 29));
  1817. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "192.168.0.1", 8));
  1818. }
  1819. void UtilTest::testIsUtf8String()
  1820. {
  1821. CPPUNIT_ASSERT(util::isUtf8("ascii"));
  1822. // "Hello World" in Japanese UTF-8
  1823. CPPUNIT_ASSERT(util::isUtf8
  1824. (fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
  1825. // "World" in Shift_JIS
  1826. CPPUNIT_ASSERT(!util::isUtf8(fromHex("90a28a")+"E"));
  1827. // UTF8-2
  1828. CPPUNIT_ASSERT(util::isUtf8(fromHex("c280")));
  1829. CPPUNIT_ASSERT(util::isUtf8(fromHex("dfbf")));
  1830. // UTF8-3
  1831. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0a080")));
  1832. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0bf80")));
  1833. CPPUNIT_ASSERT(util::isUtf8(fromHex("e18080")));
  1834. CPPUNIT_ASSERT(util::isUtf8(fromHex("ec8080")));
  1835. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed8080")));
  1836. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed9f80")));
  1837. CPPUNIT_ASSERT(util::isUtf8(fromHex("ee8080")));
  1838. CPPUNIT_ASSERT(util::isUtf8(fromHex("ef8080")));
  1839. // UTF8-4
  1840. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0908080")));
  1841. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0bf8080")));
  1842. CPPUNIT_ASSERT(util::isUtf8(fromHex("f1808080")));
  1843. CPPUNIT_ASSERT(util::isUtf8(fromHex("f3808080")));
  1844. CPPUNIT_ASSERT(util::isUtf8(fromHex("f4808080")));
  1845. CPPUNIT_ASSERT(util::isUtf8(fromHex("f48f8080")));
  1846. CPPUNIT_ASSERT(util::isUtf8(""));
  1847. CPPUNIT_ASSERT(!util::isUtf8(fromHex("00")));
  1848. }
  1849. void UtilTest::testNextParam()
  1850. {
  1851. std::string s1 = " :a : b=c :d=b::::g::";
  1852. std::pair<std::string::iterator, bool> r;
  1853. std::string name, value;
  1854. r = util::nextParam(name, value, s1.begin(), s1.end(), ':');
  1855. CPPUNIT_ASSERT(r.second);
  1856. CPPUNIT_ASSERT_EQUAL(std::string("a"), name);
  1857. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1858. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1859. CPPUNIT_ASSERT(r.second);
  1860. CPPUNIT_ASSERT_EQUAL(std::string("b"), name);
  1861. CPPUNIT_ASSERT_EQUAL(std::string("c"), value);
  1862. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1863. CPPUNIT_ASSERT(r.second);
  1864. CPPUNIT_ASSERT_EQUAL(std::string("d"), name);
  1865. CPPUNIT_ASSERT_EQUAL(std::string("b"), value);
  1866. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1867. CPPUNIT_ASSERT(r.second);
  1868. CPPUNIT_ASSERT_EQUAL(std::string("g"), name);
  1869. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1870. std::string s2 = "";
  1871. r = util::nextParam(name, value, s2.begin(), s2.end(), ':');
  1872. CPPUNIT_ASSERT(!r.second);
  1873. std::string s3 = " ";
  1874. r = util::nextParam(name, value, s3.begin(), s3.end(), ':');
  1875. CPPUNIT_ASSERT(!r.second);
  1876. std::string s4 = ":::";
  1877. r = util::nextParam(name, value, s4.begin(), s4.end(), ':');
  1878. CPPUNIT_ASSERT(!r.second);
  1879. }
  1880. void UtilTest::testNoProxyDomainMatch()
  1881. {
  1882. CPPUNIT_ASSERT(util::noProxyDomainMatch("localhost", "localhost"));
  1883. CPPUNIT_ASSERT(util::noProxyDomainMatch("192.168.0.1", "192.168.0.1"));
  1884. CPPUNIT_ASSERT(util::noProxyDomainMatch("www.example.org", ".example.org"));
  1885. CPPUNIT_ASSERT(!util::noProxyDomainMatch("www.example.org", "example.org"));
  1886. CPPUNIT_ASSERT(!util::noProxyDomainMatch("192.168.0.1", "0.1"));
  1887. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "example.com"));
  1888. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org"));
  1889. }
  1890. void UtilTest::testInPrivateAddress()
  1891. {
  1892. CPPUNIT_ASSERT(!util::inPrivateAddress("localhost"));
  1893. CPPUNIT_ASSERT(util::inPrivateAddress("192.168.0.1"));
  1894. // Only checks prefix..
  1895. CPPUNIT_ASSERT(util::inPrivateAddress("10."));
  1896. CPPUNIT_ASSERT(!util::inPrivateAddress("172."));
  1897. CPPUNIT_ASSERT(!util::inPrivateAddress("172.15.0.0"));
  1898. CPPUNIT_ASSERT(util::inPrivateAddress("172.16.0.0"));
  1899. CPPUNIT_ASSERT(util::inPrivateAddress("172.31.0.0"));
  1900. CPPUNIT_ASSERT(!util::inPrivateAddress("172.32.0.0"));
  1901. }
  1902. void UtilTest::testSecfmt()
  1903. {
  1904. CPPUNIT_ASSERT_EQUAL(std::string("0s"), util::secfmt(0));
  1905. CPPUNIT_ASSERT_EQUAL(std::string("1s"), util::secfmt(1));
  1906. CPPUNIT_ASSERT_EQUAL(std::string("9s"), util::secfmt(9));
  1907. CPPUNIT_ASSERT_EQUAL(std::string("10s"), util::secfmt(10));
  1908. CPPUNIT_ASSERT_EQUAL(std::string("1m"), util::secfmt(60));
  1909. CPPUNIT_ASSERT_EQUAL(std::string("1m59s"), util::secfmt(119));
  1910. CPPUNIT_ASSERT_EQUAL(std::string("2m"), util::secfmt(120));
  1911. CPPUNIT_ASSERT_EQUAL(std::string("59m59s"), util::secfmt(3599));
  1912. CPPUNIT_ASSERT_EQUAL(std::string("1h"), util::secfmt(3600));
  1913. }
  1914. void UtilTest::testTlsHostnameMatch()
  1915. {
  1916. CPPUNIT_ASSERT(util::tlsHostnameMatch("Foo.com", "foo.com"));
  1917. CPPUNIT_ASSERT(util::tlsHostnameMatch("*.a.com", "foo.a.com"));
  1918. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*.a.com", "bar.foo.a.com"));
  1919. CPPUNIT_ASSERT(!util::tlsHostnameMatch("f*.com", "foo.com"));
  1920. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*.com", "bar.com"));
  1921. CPPUNIT_ASSERT(util::tlsHostnameMatch("com", "com"));
  1922. CPPUNIT_ASSERT(!util::tlsHostnameMatch("foo.*", "foo.com"));
  1923. CPPUNIT_ASSERT(util::tlsHostnameMatch("a.foo.com", "A.foo.com"));
  1924. CPPUNIT_ASSERT(!util::tlsHostnameMatch("a.foo.com", "b.foo.com"));
  1925. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*a.foo.com", "a.foo.com"));
  1926. CPPUNIT_ASSERT(util::tlsHostnameMatch("*a.foo.com", "ba.foo.com"));
  1927. CPPUNIT_ASSERT(!util::tlsHostnameMatch("a*.foo.com", "a.foo.com"));
  1928. CPPUNIT_ASSERT(util::tlsHostnameMatch("a*.foo.com", "ab.foo.com"));
  1929. CPPUNIT_ASSERT(!util::tlsHostnameMatch("foo.b*z.foo.com", "foo.baz.foo.com"));
  1930. CPPUNIT_ASSERT(util::tlsHostnameMatch("B*z.foo.com", "bAZ.Foo.com"));
  1931. CPPUNIT_ASSERT(!util::tlsHostnameMatch("b*z.foo.com", "bz.foo.com"));
  1932. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*", "foo"));
  1933. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*", ""));
  1934. CPPUNIT_ASSERT(util::tlsHostnameMatch("", ""));
  1935. CPPUNIT_ASSERT(!util::tlsHostnameMatch("xn--*.a.b", "xn--c.a.b"));
  1936. }
  1937. } // namespace aria2