UtilTest.cc 82 KB

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