UtilTest.cc 83 KB

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