UtilTest.cc 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  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::string s = "name=value";
  249. auto p1 = util::divide(std::begin(s), std::end(s), '=');
  250. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  251. std::string(p1.first.first, p1.first.second));
  252. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  253. std::string(p1.second.first, p1.second.second));
  254. s = " name = value ";
  255. p1 = util::divide(std::begin(s), std::end(s), '=');
  256. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  257. std::string(p1.first.first, p1.first.second));
  258. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  259. std::string(p1.second.first, p1.second.second));
  260. s = "=value";
  261. p1 = util::divide(std::begin(s), std::end(s), '=');
  262. CPPUNIT_ASSERT_EQUAL(std::string(""),
  263. std::string(p1.first.first, p1.first.second));
  264. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  265. std::string(p1.second.first, p1.second.second));
  266. s = "name=";
  267. p1 = util::divide(std::begin(s), std::end(s), '=');
  268. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  269. std::string(p1.first.first, p1.first.second));
  270. CPPUNIT_ASSERT_EQUAL(std::string(""),
  271. std::string(p1.second.first, p1.second.second));
  272. s = "name";
  273. p1 = util::divide(std::begin(s), std::end(s), '=');
  274. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  275. std::string(p1.first.first, p1.first.second));
  276. CPPUNIT_ASSERT_EQUAL(std::string(""),
  277. std::string(p1.second.first, p1.second.second));
  278. }
  279. void UtilTest::testSplit() {
  280. std::vector<std::string> v;
  281. std::string s = "k1; k2;; k3";
  282. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true);
  283. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  284. std::vector<std::string>::iterator itr = v.begin();
  285. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  286. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  287. CPPUNIT_ASSERT_EQUAL(std::string("k3"), *itr++);
  288. v.clear();
  289. s = "k1; k2; k3";
  290. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  291. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  292. itr = v.begin();
  293. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  294. CPPUNIT_ASSERT_EQUAL(std::string(" k2"), *itr++);
  295. CPPUNIT_ASSERT_EQUAL(std::string(" k3"), *itr++);
  296. v.clear();
  297. s = "k=v";
  298. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  299. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  300. itr = v.begin();
  301. CPPUNIT_ASSERT_EQUAL(std::string("k=v"), *itr++);
  302. v.clear();
  303. s = ";;k1;;k2;";
  304. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  305. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  306. itr = v.begin();
  307. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  308. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  309. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  310. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  311. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  312. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  313. v.clear();
  314. s = ";;k1;;k2;";
  315. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  316. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  317. itr = v.begin();
  318. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  319. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  320. v.clear();
  321. s = "k; ";
  322. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  323. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  324. itr = v.begin();
  325. CPPUNIT_ASSERT_EQUAL(std::string("k"), *itr++);
  326. CPPUNIT_ASSERT_EQUAL(std::string(" "), *itr++);
  327. v.clear();
  328. s = " ";
  329. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true, true);
  330. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  331. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  332. v.clear();
  333. s = " ";
  334. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true);
  335. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  336. v.clear();
  337. s = " ";
  338. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  339. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  340. CPPUNIT_ASSERT_EQUAL(std::string(" "), v[0]);
  341. v.clear();
  342. s = ";";
  343. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  344. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  345. v.clear();
  346. s = ";";
  347. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  348. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  349. itr = v.begin();
  350. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  351. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  352. v.clear();
  353. s = "";
  354. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  355. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  356. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  357. }
  358. void UtilTest::testSplitIter() {
  359. std::vector<Scip> v;
  360. std::string s = "k1; k2;; k3";
  361. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true);
  362. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  363. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  364. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  365. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  366. v.clear();
  367. s = "k1; k2; k3";
  368. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  369. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  370. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  371. CPPUNIT_ASSERT_EQUAL(std::string(" k2"),
  372. std::string(v[1].first, v[1].second));
  373. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  374. std::string(v[2].first, v[2].second));
  375. v.clear();
  376. s = "k=v";
  377. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  378. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  379. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  380. std::string(v[0].first, v[0].second));
  381. v.clear();
  382. s = ";;k1;;k2;";
  383. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  384. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  385. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  386. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  387. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  388. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  389. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  390. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  391. v.clear();
  392. s = ";;k1;;k2;";
  393. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  394. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  395. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  396. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  397. v.clear();
  398. s = "k; ";
  399. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  400. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  401. CPPUNIT_ASSERT_EQUAL(std::string("k"), std::string(v[0].first, v[0].second));
  402. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  403. v.clear();
  404. s = " ";
  405. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true, true);
  406. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  407. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  408. v.clear();
  409. s = " ";
  410. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true);
  411. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  412. v.clear();
  413. s = " ";
  414. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  415. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  416. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[0].first, v[0].second));
  417. v.clear();
  418. s = ";";
  419. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  420. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  421. v.clear();
  422. s = ";";
  423. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  424. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  425. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  426. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  427. v.clear();
  428. s = "";
  429. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  430. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  431. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  432. }
  433. void UtilTest::testSplitIterM() {
  434. const char d[] = ";";
  435. const char md[] = "; ";
  436. std::vector<Scip> v;
  437. std::string s = "k1; k2;; k3";
  438. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true);
  439. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  440. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  441. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  442. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  443. v.clear();
  444. s = "k1; k2; k3";
  445. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  446. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  447. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  448. CPPUNIT_ASSERT_EQUAL(std::string(" k2"),
  449. std::string(v[1].first, v[1].second));
  450. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  451. std::string(v[2].first, v[2].second));
  452. v.clear();
  453. s = "k1; k2; k3";
  454. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), md);
  455. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  456. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  457. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  458. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  459. v.clear();
  460. s = "k1; k2; k3;";
  461. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), md, false, true);
  462. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  463. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  464. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  465. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[2].first, v[2].second));
  466. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  467. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[4].first, v[4].second));
  468. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  469. v.clear();
  470. s = "k=v";
  471. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  472. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  473. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  474. std::string(v[0].first, v[0].second));
  475. v.clear();
  476. s = ";;k1;;k2;";
  477. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  478. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  479. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  480. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  481. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  482. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  483. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  484. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  485. v.clear();
  486. s = ";;k1;;k2;";
  487. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  488. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  489. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  490. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  491. v.clear();
  492. s = "k; ";
  493. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  494. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  495. CPPUNIT_ASSERT_EQUAL(std::string("k"), std::string(v[0].first, v[0].second));
  496. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  497. v.clear();
  498. s = " ";
  499. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true, true);
  500. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  501. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  502. v.clear();
  503. s = " ";
  504. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true);
  505. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  506. v.clear();
  507. s = " ";
  508. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  509. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  510. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[0].first, v[0].second));
  511. v.clear();
  512. s = ";";
  513. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  514. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  515. v.clear();
  516. s = ";";
  517. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  518. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  519. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  520. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  521. v.clear();
  522. s = "";
  523. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  524. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  525. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  526. }
  527. void UtilTest::testEndsWith() {
  528. std::string target = "abcdefg";
  529. std::string part = "fg";
  530. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  531. part.begin(), part.end()));
  532. target = "abdefg";
  533. part = "g";
  534. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  535. part.begin(), part.end()));
  536. target = "abdefg";
  537. part = "eg";
  538. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  539. part.begin(), part.end()));
  540. target = "g";
  541. part = "eg";
  542. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  543. part.begin(), part.end()));
  544. target = "g";
  545. part = "g";
  546. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  547. part.begin(), part.end()));
  548. target = "g";
  549. part = "";
  550. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  551. part.begin(), part.end()));
  552. target = "";
  553. part = "";
  554. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  555. part.begin(), part.end()));
  556. target = "";
  557. part = "g";
  558. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  559. part.begin(), part.end()));
  560. }
  561. void UtilTest::testIendsWith() {
  562. std::string target = "abcdefg";
  563. std::string part = "Fg";
  564. CPPUNIT_ASSERT(util::iendsWith(target.begin(), target.end(),
  565. part.begin(), part.end()));
  566. target = "abdefg";
  567. part = "ef";
  568. CPPUNIT_ASSERT(!util::iendsWith(target.begin(), target.end(),
  569. part.begin(), part.end()));
  570. }
  571. void UtilTest::testStreq()
  572. {
  573. std::string s1, s2;
  574. s1 = "foo";
  575. s2 = "foo";
  576. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  577. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.c_str()));
  578. s2 = "fooo";
  579. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  580. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  581. s2 = "fo";
  582. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  583. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  584. s2 = "";
  585. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  586. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  587. s1 = "";
  588. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  589. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.c_str()));
  590. }
  591. void UtilTest::testStrieq()
  592. {
  593. std::string s1, s2;
  594. s1 = "foo";
  595. s2 = "foo";
  596. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  597. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  598. s1 = "FoO";
  599. s2 = "fOo";
  600. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  601. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  602. s2 = "fooo";
  603. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  604. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  605. s2 = "fo";
  606. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  607. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  608. s2 = "";
  609. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  610. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  611. s1 = "";
  612. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  613. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  614. }
  615. void UtilTest::testStrifind()
  616. {
  617. std::string s1, s2;
  618. s1 = "yamagakani mukashi wo toheba hARU no tuki";
  619. s2 = "HaRu";
  620. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  621. != s1.end());
  622. s2 = "aki";
  623. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  624. == s1.end());
  625. s1 = "h";
  626. s2 = "HH";
  627. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  628. == s1.end());
  629. }
  630. void UtilTest::testReplace() {
  631. CPPUNIT_ASSERT_EQUAL(std::string("abc\n"), util::replace("abc\r\n", "\r", ""));
  632. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc\r\n", "\r\n", ""));
  633. CPPUNIT_ASSERT_EQUAL(std::string(""), util::replace("", "\r\n", ""));
  634. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc", "", "a"));
  635. CPPUNIT_ASSERT_EQUAL(std::string("xbc"), util::replace("abc", "a", "x"));
  636. }
  637. void UtilTest::testStartsWith() {
  638. std::string target;
  639. std::string part;
  640. target = "abcdefg";
  641. part = "abc";
  642. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  643. part.begin(), part.end()));
  644. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  645. target = "abcdefg";
  646. part = "abx";
  647. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  648. part.begin(), part.end()));
  649. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
  650. target = "abcdefg";
  651. part = "bcd";
  652. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  653. part.begin(), part.end()));
  654. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
  655. target = "";
  656. part = "a";
  657. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  658. part.begin(), part.end()));
  659. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
  660. target = "";
  661. part = "";
  662. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  663. part.begin(), part.end()));
  664. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  665. target = "a";
  666. part = "";
  667. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  668. part.begin(), part.end()));
  669. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  670. target = "a";
  671. part = "a";
  672. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  673. part.begin(), part.end()));
  674. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  675. }
  676. void UtilTest::testIstartsWith() {
  677. std::string target;
  678. std::string part;
  679. target = "abcdefg";
  680. part = "aBc";
  681. CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(),
  682. part.begin(), part.end()));
  683. CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(), part.c_str()));
  684. target = "abcdefg";
  685. part = "abx";
  686. CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(),
  687. part.begin(), part.end()));
  688. CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(), part.c_str()));
  689. }
  690. void UtilTest::testGetContentDispositionFilename() {
  691. std::string val;
  692. val = "attachment; filename=\"aria2.tar.bz2\"";
  693. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  694. util::getContentDispositionFilename(val));
  695. val = "attachment; filename=\"\"";
  696. CPPUNIT_ASSERT_EQUAL(std::string(""),
  697. util::getContentDispositionFilename(val));
  698. val = "attachment; filename=\"";
  699. CPPUNIT_ASSERT_EQUAL(std::string(""),
  700. util::getContentDispositionFilename(val));
  701. val = "attachment; filename= \" aria2.tar.bz2 \"";
  702. CPPUNIT_ASSERT_EQUAL(std::string(" aria2.tar.bz2 "),
  703. util::getContentDispositionFilename(val));
  704. val = "attachment; filename=dir/file";
  705. CPPUNIT_ASSERT_EQUAL(std::string(""),
  706. util::getContentDispositionFilename(val));
  707. val = "attachment; filename=dir\\file";
  708. CPPUNIT_ASSERT_EQUAL(std::string(""),
  709. util::getContentDispositionFilename(val));
  710. val = "attachment; filename=\"dir/file\"";
  711. CPPUNIT_ASSERT_EQUAL(std::string(""),
  712. util::getContentDispositionFilename(val));
  713. val = "attachment; filename=\"dir\\\\file\"";
  714. CPPUNIT_ASSERT_EQUAL(std::string(""),
  715. util::getContentDispositionFilename(val));
  716. val = "attachment; filename=\"/etc/passwd\"";
  717. CPPUNIT_ASSERT_EQUAL(std::string(""),
  718. util::getContentDispositionFilename(val));
  719. val = "attachment; filename=\"..\"";
  720. CPPUNIT_ASSERT_EQUAL(std::string(""),
  721. util::getContentDispositionFilename(val));
  722. val = "attachment; filename=..";
  723. CPPUNIT_ASSERT_EQUAL(std::string(""),
  724. util::getContentDispositionFilename(val));
  725. // Unescaping %2E%2E%2F produces "../". But since we won't unescape,
  726. // we just accept it as is.
  727. val = "attachment; filename=\"%2E%2E%2Ffoo.html\"";
  728. CPPUNIT_ASSERT_EQUAL(std::string("%2E%2E%2Ffoo.html"),
  729. util::getContentDispositionFilename(val));
  730. // iso-8859-1 string will be converted to utf-8.
  731. val = "attachment; filename*=iso-8859-1''foo-%E4.html";
  732. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  733. util::getContentDispositionFilename(val));
  734. val = "attachment; filename*= UTF-8''foo-%c3%a4.html";
  735. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  736. util::getContentDispositionFilename(val));
  737. // iso-8859-1 string will be converted to utf-8.
  738. val = "attachment; filename=\"foo-%E4.html\"";
  739. val = util::percentDecode(val.begin(), val.end());
  740. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  741. util::getContentDispositionFilename(val));
  742. }
  743. void UtilTest::testParseContentDisposition() {
  744. char dest[1024];
  745. size_t destlen = sizeof(dest);
  746. const char *cs;
  747. size_t cslen;
  748. std::string val;
  749. // test cases from http://greenbytes.de/tech/tc2231/
  750. // inlonly
  751. val = "inline";
  752. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  753. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  754. // inlonlyquoted
  755. val = "\"inline\"";
  756. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  757. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  758. // inlwithasciifilename
  759. val = "inline; filename=\"foo.html\"";
  760. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  761. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  762. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  763. std::string(&dest[0], &dest[8]));
  764. // inlwithfnattach
  765. val = "inline; filename=\"Not an attachment!\"";
  766. CPPUNIT_ASSERT_EQUAL((ssize_t)18, util::parse_content_disposition
  767. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  768. CPPUNIT_ASSERT_EQUAL(std::string("Not an attachment!"),
  769. std::string(&dest[0], &dest[18]));
  770. // inlwithasciifilenamepdf
  771. val = "inline; filename=\"foo.pdf\"";
  772. CPPUNIT_ASSERT_EQUAL((ssize_t)7, util::parse_content_disposition
  773. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  774. CPPUNIT_ASSERT_EQUAL(std::string("foo.pdf"),
  775. std::string(&dest[0], &dest[7]));
  776. // attwithasciifilename25
  777. val = "attachment; filename=\"0000000000111111111122222\"";
  778. CPPUNIT_ASSERT_EQUAL((ssize_t)25, util::parse_content_disposition
  779. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  780. CPPUNIT_ASSERT_EQUAL(std::string("0000000000111111111122222"),
  781. std::string(&dest[0], &dest[25]));
  782. // attwithasciifilename35
  783. val = "attachment; filename=\"00000000001111111111222222222233333\"";
  784. CPPUNIT_ASSERT_EQUAL((ssize_t)35, util::parse_content_disposition
  785. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  786. CPPUNIT_ASSERT_EQUAL(std::string("00000000001111111111222222222233333"),
  787. std::string(&dest[0], &dest[35]));
  788. // attwithasciifnescapedchar
  789. val = "attachment; filename=\"f\\oo.html\"";
  790. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  791. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  792. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  793. std::string(&dest[0], &dest[8]));
  794. // attwithasciifnescapedquote
  795. val = "attachment; filename=\"\\\"quoting\\\" tested.html\"";
  796. CPPUNIT_ASSERT_EQUAL((ssize_t)21, util::parse_content_disposition
  797. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  798. CPPUNIT_ASSERT_EQUAL(std::string("\"quoting\" tested.html"),
  799. std::string(&dest[0], &dest[21]));
  800. // attwithquotedsemicolon
  801. val = "attachment; filename=\"Here's a semicolon;.html\"";
  802. CPPUNIT_ASSERT_EQUAL((ssize_t)24, util::parse_content_disposition
  803. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  804. CPPUNIT_ASSERT_EQUAL(std::string("Here's a semicolon;.html"),
  805. std::string(&dest[0], &dest[24]));
  806. // attwithfilenameandextparam
  807. val = "attachment; foo=\"bar\"; filename=\"foo.html\"";
  808. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  809. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  810. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  811. std::string(&dest[0], &dest[8]));
  812. // attwithfilenameandextparamescaped
  813. val = "attachment; foo=\"\\\"\\\\\";filename=\"foo.html\"";
  814. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  815. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  816. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  817. std::string(&dest[0], &dest[8]));
  818. // attwithasciifilenameucase
  819. val = "attachment; FILENAME=\"foo.html\"";
  820. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  821. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  822. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  823. std::string(&dest[0], &dest[8]));
  824. // attwithasciifilenamenq
  825. val = "attachment; filename=foo.html";
  826. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  827. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  828. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  829. std::string(&dest[0], &dest[8]));
  830. // attwithtokfncommanq
  831. val = "attachment; filename=foo,bar.html";
  832. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  833. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  834. // attwithasciifilenamenqs
  835. val = "attachment; filename=foo.html ;";
  836. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  837. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  838. // attemptyparam
  839. val = "attachment; ;filename=foo";
  840. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  841. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  842. // attwithasciifilenamenqws
  843. val = "attachment; filename=foo bar.html";
  844. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  845. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  846. // attwithfntokensq
  847. val = "attachment; filename='foo.bar'";
  848. CPPUNIT_ASSERT_EQUAL((ssize_t)9, util::parse_content_disposition
  849. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  850. CPPUNIT_ASSERT_EQUAL(std::string("'foo.bar'"),
  851. std::string(&dest[0], &dest[9]));
  852. // attwithisofnplain
  853. // attachment; filename="foo-ä.html"
  854. val = "attachment; filename=\"foo-%E4.html\"";
  855. val = util::percentDecode(val.begin(), val.end());
  856. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  857. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  858. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  859. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[10])));
  860. // attwithutf8fnplain
  861. // attachment; filename="foo-ä.html"
  862. val = "attachment; filename=\"foo-%C3%A4.html\"";
  863. val = util::percentDecode(val.begin(), val.end());
  864. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  865. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  866. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  867. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[11])));
  868. // attwithfnrawpctenca
  869. val = "attachment; filename=\"foo-%41.html\"";
  870. CPPUNIT_ASSERT_EQUAL((ssize_t)12, util::parse_content_disposition
  871. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  872. CPPUNIT_ASSERT_EQUAL(std::string("foo-%41.html"),
  873. std::string(&dest[0], &dest[12]));
  874. // attwithfnusingpct
  875. val = "attachment; filename=\"50%.html\"";
  876. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  877. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  878. CPPUNIT_ASSERT_EQUAL(std::string("50%.html"),
  879. std::string(&dest[0], &dest[8]));
  880. // attwithfnrawpctencaq
  881. val = "attachment; filename=\"foo-%\\41.html\"";
  882. CPPUNIT_ASSERT_EQUAL((ssize_t)12, util::parse_content_disposition
  883. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  884. CPPUNIT_ASSERT_EQUAL(std::string("foo-%41.html"),
  885. std::string(&dest[0], &dest[12]));
  886. // attwithnamepct
  887. val = "attachment; name=\"foo-%41.html\"";
  888. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  889. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  890. // attwithfilenamepctandiso
  891. // attachment; filename="ä-%41.html"
  892. val = "attachment; filename=\"%E4-%2541.html\"";
  893. val = util::percentDecode(val.begin(), val.end());
  894. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  895. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  896. CPPUNIT_ASSERT_EQUAL(std::string("ä-%41.html"),
  897. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[10])));
  898. // attwithfnrawpctenclong
  899. val = "attachment; filename=\"foo-%c3%a4-%e2%82%ac.html\"";
  900. CPPUNIT_ASSERT_EQUAL((ssize_t)25, util::parse_content_disposition
  901. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  902. CPPUNIT_ASSERT_EQUAL(std::string("foo-%c3%a4-%e2%82%ac.html"),
  903. std::string(&dest[0], &dest[25]));
  904. // attwithasciifilenamews1
  905. val = "attachment; filename =\"foo.html\"";
  906. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  907. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  908. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  909. std::string(&dest[0], &dest[8]));
  910. // attwith2filenames
  911. val = "attachment; filename=\"foo.html\"; filename=\"bar.html\"";
  912. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  913. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  914. // attfnbrokentoken
  915. val = "attachment; filename=foo[1](2).html";
  916. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  917. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  918. // attfnbrokentokeniso
  919. val = "attachment; filename=foo-%E4.html";
  920. val = util::percentDecode(val.begin(), val.end());
  921. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  922. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  923. // attfnbrokentokenutf
  924. // attachment; filename=foo-ä.html
  925. val = "attachment; filename=foo-ä.html";
  926. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  927. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  928. // attmissingdisposition
  929. val = "filename=foo.html";
  930. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  931. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  932. // attmissingdisposition2
  933. val = "x=y; filename=foo.html";
  934. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  935. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  936. // attmissingdisposition3
  937. val = "\"foo; filename=bar;baz\"; filename=qux";
  938. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  939. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  940. // attmissingdisposition4
  941. val = "filename=foo.html, filename=bar.html";
  942. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  943. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  944. // emptydisposition
  945. val = "; filename=foo.html";
  946. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  947. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  948. // doublecolon
  949. val = ": inline; attachment; filename=foo.html";
  950. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  951. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  952. // attandinline
  953. val = "inline; attachment; filename=foo.html";
  954. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  955. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  956. // attandinline2
  957. val = "attachment; inline; filename=foo.html";
  958. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  959. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  960. // attbrokenquotedfn
  961. val = "attachment; filename=\"foo.html\".txt";
  962. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  963. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  964. // attbrokenquotedfn2
  965. val = "attachment; filename=\"bar";
  966. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  967. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  968. // attbrokenquotedfn3
  969. val = "attachment; filename=foo\"bar;baz\"qux";
  970. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  971. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  972. // attmultinstances
  973. val = "attachment; filename=foo.html, attachment; filename=bar.html";
  974. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  975. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  976. // attmissingdelim
  977. val = "attachment; foo=foo filename=bar";
  978. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  979. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  980. // attmissingdelim2
  981. val = "attachment; filename=bar foo=foo ";
  982. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  983. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  984. // attmissingdelim3
  985. val = "attachment filename=bar";
  986. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  987. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  988. // attreversed
  989. val = "filename=foo.html; attachment";
  990. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  991. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  992. // attconfusedparam
  993. val = "attachment; xfilename=foo.html";
  994. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  995. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  996. // attabspath
  997. val = "attachment; filename=\"/foo.html\"";
  998. CPPUNIT_ASSERT_EQUAL((ssize_t)9, util::parse_content_disposition
  999. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1000. CPPUNIT_ASSERT_EQUAL(std::string("/foo.html"),
  1001. std::string(&dest[0], &dest[9]));
  1002. // attabspathwin
  1003. val = "attachment; filename=\"\\\\foo.html\"";
  1004. CPPUNIT_ASSERT_EQUAL((ssize_t)9, util::parse_content_disposition
  1005. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1006. CPPUNIT_ASSERT_EQUAL(std::string("\\foo.html"),
  1007. std::string(&dest[0], &dest[9]));
  1008. // attcdate
  1009. val = "attachment; creation-date=\"Wed, 12 Feb 1997 16:29:51 -0500\"";
  1010. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1011. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1012. // dispext
  1013. val = "foobar";
  1014. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1015. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1016. // dispextbadfn
  1017. val = "attachment; example=\"filename=example.txt\"";
  1018. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1019. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1020. // attwithisofn2231iso
  1021. val = "attachment; filename*=iso-8859-1''foo-%E4.html";
  1022. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  1023. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1024. CPPUNIT_ASSERT_EQUAL(std::string("iso-8859-1"), std::string(cs, cslen));
  1025. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1026. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[10])));
  1027. // attwithfn2231utf8
  1028. val = "attachment; filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html";
  1029. CPPUNIT_ASSERT_EQUAL((ssize_t)15, util::parse_content_disposition
  1030. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1031. CPPUNIT_ASSERT_EQUAL(std::string("UTF-8"), std::string(cs, cslen));
  1032. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä-€.html"),
  1033. std::string(&dest[0], &dest[15]));
  1034. // attwithfn2231noc
  1035. val = "attachment; filename*=''foo-%c3%a4-%e2%82%ac.html";
  1036. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1037. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1038. // attwithfn2231utf8comp
  1039. val = "attachment; filename*=UTF-8''foo-a%cc%88.html";
  1040. CPPUNIT_ASSERT_EQUAL((ssize_t)12, util::parse_content_disposition
  1041. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1042. val = "foo-a%cc%88.html";
  1043. CPPUNIT_ASSERT_EQUAL(std::string(util::percentDecode(val.begin(),
  1044. val.end())),
  1045. std::string(&dest[0], &dest[12]));
  1046. // attwithfn2231utf8-bad
  1047. val = "attachment; filename*=iso-8859-1''foo-%c3%a4-%e2%82%ac.html";
  1048. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1049. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1050. // attwithfn2231iso-bad
  1051. val = "attachment; filename*=utf-8''foo-%E4.html";
  1052. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1053. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1054. // attwithfn2231ws1
  1055. val = "attachment; filename *=UTF-8''foo-%c3%a4.html";
  1056. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1057. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1058. // attwithfn2231ws2
  1059. val = "attachment; filename*= UTF-8''foo-%c3%a4.html";
  1060. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  1061. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1062. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1063. std::string(&dest[0], &dest[11]));
  1064. // attwithfn2231ws3
  1065. val = "attachment; filename* =UTF-8''foo-%c3%a4.html";
  1066. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  1067. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1068. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1069. std::string(&dest[0], &dest[11]));
  1070. // attwithfn2231quot
  1071. val = "attachment; filename*=\"UTF-8''foo-%c3%a4.html\"";
  1072. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1073. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1074. // attwithfn2231quot2
  1075. val = "attachment; filename*=\"foo%20bar.html\"";
  1076. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1077. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1078. // attwithfn2231singleqmissing
  1079. val = "attachment; filename*=UTF-8'foo-%c3%a4.html";
  1080. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1081. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1082. // attwithfn2231nbadpct1
  1083. val = "attachment; filename*=UTF-8''foo%";
  1084. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1085. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1086. // attwithfn2231nbadpct2
  1087. val = "attachment; filename*=UTF-8''f%oo.html";
  1088. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1089. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1090. // attwithfn2231dpct
  1091. val = "attachment; filename*=UTF-8''A-%2541.html";
  1092. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  1093. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1094. CPPUNIT_ASSERT_EQUAL(std::string("A-%41.html"),
  1095. std::string(&dest[0], &dest[10]));
  1096. // attwithfn2231abspathdisguised
  1097. val = "attachment; filename*=UTF-8''%5cfoo.html";
  1098. CPPUNIT_ASSERT_EQUAL((ssize_t)9, util::parse_content_disposition
  1099. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1100. CPPUNIT_ASSERT_EQUAL(std::string("\\foo.html"),
  1101. std::string(&dest[0], &dest[9]));
  1102. // attfnboth
  1103. val = "attachment; filename=\"foo-ae.html\"; filename*=UTF-8''foo-%c3%a4.html";
  1104. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  1105. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1106. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1107. std::string(&dest[0], &dest[11]));
  1108. // attfnboth2
  1109. val = "attachment; filename*=UTF-8''foo-%c3%a4.html; filename=\"foo-ae.html\"";
  1110. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  1111. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1112. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1113. std::string(&dest[0], &dest[11]));
  1114. // attfnboth3
  1115. val = "attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4; filename*=ISO-8859-1''currency-sign%3d%a4";
  1116. CPPUNIT_ASSERT_EQUAL((ssize_t)15, util::parse_content_disposition
  1117. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1118. CPPUNIT_ASSERT_EQUAL(std::string("ISO-8859-1"), std::string(cs, cslen));
  1119. CPPUNIT_ASSERT_EQUAL(std::string("currency-sign=¤"),
  1120. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[15])));
  1121. // attnewandfn
  1122. val = "attachment; foobar=x; filename=\"foo.html\"";
  1123. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1124. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1125. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1126. std::string(&dest[0], &dest[8]));
  1127. // attrfc2047token
  1128. val = "attachment; filename==?ISO-8859-1?Q?foo-=E4.html?=";
  1129. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1130. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1131. // attrfc2047quoted
  1132. val = "attachment; filename=\"=?ISO-8859-1?Q?foo-=E4.html?=\"";
  1133. CPPUNIT_ASSERT_EQUAL((ssize_t)29, util::parse_content_disposition
  1134. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1135. CPPUNIT_ASSERT_EQUAL(std::string("=?ISO-8859-1?Q?foo-=E4.html?="),
  1136. std::string(&dest[0], &dest[29]));
  1137. // aria2 original testcases
  1138. // zero-length filename. token cannot be empty, so this is invalid.
  1139. val = "attachment; filename=";
  1140. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1141. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1142. // zero-length filename. quoted-string can be empty string, so this
  1143. // is ok.
  1144. val = "attachment; filename=\"\"";
  1145. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1146. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1147. // empty value is not allowed
  1148. val = "attachment; filename=;";
  1149. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1150. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1151. // / is not valid char in token.
  1152. val = "attachment; filename=dir/file";
  1153. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1154. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1155. // value-chars is *(pct-encoded / attr-char), so empty string is
  1156. // allowed.
  1157. val = "attachment; filename*=UTF-8''";
  1158. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1159. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1160. CPPUNIT_ASSERT_EQUAL(std::string("UTF-8"), std::string(cs, cslen));
  1161. val = "attachment; filename*=UTF-8''; filename=foo";
  1162. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1163. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1164. CPPUNIT_ASSERT_EQUAL(std::string("UTF-8"), std::string(cs, cslen));
  1165. val = "attachment; filename*=UTF-8'' ; filename=foo";
  1166. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1167. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1168. CPPUNIT_ASSERT_EQUAL(std::string("UTF-8"), std::string(cs, cslen));
  1169. // with language
  1170. val = "attachment; filename*=UTF-8'japanese'konnichiwa";
  1171. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  1172. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1173. CPPUNIT_ASSERT_EQUAL(std::string("konnichiwa"),
  1174. std::string(&dest[0], &dest[10]));
  1175. // lws before and after "="
  1176. val = "attachment; filename = foo.html";
  1177. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1178. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1179. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1180. std::string(&dest[0], &dest[8]));
  1181. // lws before and after "=" with quoted-string
  1182. val = "attachment; filename = \"foo.html\"";
  1183. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1184. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1185. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1186. std::string(&dest[0], &dest[8]));
  1187. // lws after parm
  1188. val = "attachment; filename=foo.html ";
  1189. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1190. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1191. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1192. std::string(&dest[0], &dest[8]));
  1193. val = "attachment; filename=foo.html ; hello=world";
  1194. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1195. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1196. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1197. std::string(&dest[0], &dest[8]));
  1198. val = "attachment; filename=\"foo.html\" ";
  1199. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1200. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1201. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1202. std::string(&dest[0], &dest[8]));
  1203. val = "attachment; filename=\"foo.html\" ; hello=world";
  1204. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1205. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1206. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1207. std::string(&dest[0], &dest[8]));
  1208. val = "attachment; filename*=UTF-8''foo.html ; hello=world";
  1209. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1210. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1211. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1212. std::string(&dest[0], &dest[8]));
  1213. }
  1214. class Printer {
  1215. public:
  1216. template<class T>
  1217. void operator()(T t) {
  1218. std::cerr << t << ", ";
  1219. }
  1220. };
  1221. void UtilTest::testToUpper() {
  1222. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  1223. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  1224. CPPUNIT_ASSERT_EQUAL(upp, util::toUpper(src));
  1225. }
  1226. void UtilTest::testToLower() {
  1227. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  1228. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  1229. CPPUNIT_ASSERT_EQUAL(upp, util::toLower(src));
  1230. }
  1231. void UtilTest::testUppercase() {
  1232. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  1233. std::string ans = "608CABC0F2FA18C260CAFD974516865C772363D5";
  1234. util::uppercase(src);
  1235. CPPUNIT_ASSERT_EQUAL(ans, src);
  1236. }
  1237. void UtilTest::testLowercase() {
  1238. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  1239. std::string ans = "608cabc0f2fa18c260cafd974516865c772363d5";
  1240. util::lowercase(src);
  1241. CPPUNIT_ASSERT_EQUAL(ans, src);
  1242. }
  1243. void UtilTest::testPercentDecode() {
  1244. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  1245. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  1246. util::percentDecode(src.begin(), src.end()));
  1247. std::string src2 = "aria2+aria2";
  1248. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"),
  1249. util::percentDecode(src2.begin(), src2.end()));
  1250. std::string src3 = "%5t%20";
  1251. CPPUNIT_ASSERT_EQUAL(std::string("%5t "),
  1252. util::percentDecode(src3.begin(), src3.end()));
  1253. std::string src4 = "%";
  1254. CPPUNIT_ASSERT_EQUAL(std::string("%"),
  1255. util::percentDecode(src4.begin(), src4.end()));
  1256. std::string src5 = "%3";
  1257. CPPUNIT_ASSERT_EQUAL(std::string("%3"),
  1258. util::percentDecode(src5.begin(), src5.end()));
  1259. std::string src6 = "%2f";
  1260. CPPUNIT_ASSERT_EQUAL(std::string("/"),
  1261. util::percentDecode(src6.begin(), src6.end()));
  1262. }
  1263. void UtilTest::testGetRealSize()
  1264. {
  1265. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096M"));
  1266. CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1K"));
  1267. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096m"));
  1268. CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1k"));
  1269. try {
  1270. util::getRealSize("");
  1271. CPPUNIT_FAIL("exception must be thrown.");
  1272. } catch(Exception& e) {
  1273. std::cerr << e.stackTrace();
  1274. }
  1275. try {
  1276. util::getRealSize("foo");
  1277. CPPUNIT_FAIL("exception must be thrown.");
  1278. } catch(Exception& e) {
  1279. std::cerr << e.stackTrace();
  1280. }
  1281. try {
  1282. util::getRealSize("-1");
  1283. CPPUNIT_FAIL("exception must be thrown.");
  1284. } catch(Exception& e) {
  1285. std::cerr << e.stackTrace();
  1286. }
  1287. try {
  1288. util::getRealSize("9223372036854775807K");
  1289. CPPUNIT_FAIL("exception must be thrown.");
  1290. } catch(Exception& e) {
  1291. std::cerr << e.stackTrace();
  1292. }
  1293. try {
  1294. util::getRealSize("9223372036854775807M");
  1295. CPPUNIT_FAIL("exception must be thrown.");
  1296. } catch(Exception& e) {
  1297. std::cerr << e.stackTrace();
  1298. }
  1299. }
  1300. void UtilTest::testAbbrevSize()
  1301. {
  1302. CPPUNIT_ASSERT_EQUAL(std::string("8,589,934,591Gi"),
  1303. util::abbrevSize(9223372036854775807LL));
  1304. CPPUNIT_ASSERT_EQUAL(std::string("4.0Gi"), util::abbrevSize(4294967296LL));
  1305. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1024));
  1306. CPPUNIT_ASSERT_EQUAL(std::string("0.9Ki"), util::abbrevSize(1023));
  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. std::shared_ptr<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  1316. std::shared_ptr<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  1317. std::deque<std::shared_ptr<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. " |556B (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. std::shared_ptr<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(std::begin(dir1dir2file), std::end(dir1dir2file)));
  1613. const std::string dirparentfile[] = { "dir", "..", "file" };
  1614. CPPUNIT_ASSERT_EQUAL
  1615. (std::string("file"),
  1616. util::joinPath(std::begin(dirparentfile), std::end(dirparentfile)));
  1617. const std::string dirparentparentfile[] = { "dir", "..", "..", "file" };
  1618. CPPUNIT_ASSERT_EQUAL
  1619. (std::string("file"),
  1620. util::joinPath(std::begin(dirparentparentfile),
  1621. std::end(dirparentparentfile)));
  1622. const std::string dirdotfile[] = { "dir", ".", "file" };
  1623. CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
  1624. util::joinPath(std::begin(dirdotfile),
  1625. std::end(dirdotfile)));
  1626. const std::string empty[] = {};
  1627. CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(&empty[0], &empty[0]));
  1628. const std::string parentdot[] = { "..", "." };
  1629. CPPUNIT_ASSERT_EQUAL(std::string(""),
  1630. util::joinPath(std::begin(parentdot),
  1631. std::end(parentdot)));
  1632. }
  1633. void UtilTest::testParseIndexPath()
  1634. {
  1635. std::pair<size_t, std::string> p = util::parseIndexPath("1=foo");
  1636. CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
  1637. CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
  1638. try {
  1639. util::parseIndexPath("1X=foo");
  1640. CPPUNIT_FAIL("exception must be thrown.");
  1641. } catch(Exception& e) {
  1642. // success
  1643. }
  1644. try {
  1645. util::parseIndexPath("1=");
  1646. CPPUNIT_FAIL("exception must be thrown.");
  1647. } catch(Exception& e) {
  1648. // success
  1649. }
  1650. }
  1651. void UtilTest::testCreateIndexPaths()
  1652. {
  1653. std::stringstream in
  1654. ("1=/tmp/myfile\n"
  1655. "100=/myhome/mypicture.png\n");
  1656. std::vector<std::pair<size_t, std::string> > m = util::createIndexPaths(in);
  1657. CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
  1658. CPPUNIT_ASSERT_EQUAL((size_t)1, m[0].first);
  1659. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[0].second);
  1660. CPPUNIT_ASSERT_EQUAL((size_t)100, m[1].first);
  1661. CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[1].second);
  1662. }
  1663. void UtilTest::testGenerateRandomData()
  1664. {
  1665. unsigned char data1[20];
  1666. util::generateRandomData(data1, sizeof(data1));
  1667. unsigned char data2[20];
  1668. util::generateRandomData(data2, sizeof(data2));
  1669. CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
  1670. }
  1671. void UtilTest::testFromHex()
  1672. {
  1673. std::string src;
  1674. std::string dest;
  1675. src = "0011fF";
  1676. dest = util::fromHex(src.begin(), src.end());
  1677. CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
  1678. CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
  1679. CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
  1680. CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
  1681. src = "0011f";
  1682. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  1683. src = "001g";
  1684. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  1685. }
  1686. void UtilTest::testParsePrioritizePieceRange()
  1687. {
  1688. // piece index
  1689. // 0 1 2 3 4 5 6 7
  1690. // | | | |
  1691. // file1 | | |
  1692. // | | |
  1693. // file2 | |
  1694. // file3 |
  1695. // | |
  1696. // file4 |
  1697. size_t pieceLength = 1024;
  1698. std::vector<std::shared_ptr<FileEntry> > entries(4, std::shared_ptr<FileEntry>());
  1699. entries[0].reset(new FileEntry("file1", 1024, 0));
  1700. entries[1].reset(new FileEntry("file2",2560,entries[0]->getLastOffset()));
  1701. entries[2].reset(new FileEntry("file3",0,entries[1]->getLastOffset()));
  1702. entries[3].reset(new FileEntry("file4",3584,entries[2]->getLastOffset()));
  1703. std::vector<size_t> result;
  1704. util::parsePrioritizePieceRange(result, "head=1", entries, pieceLength);
  1705. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1706. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1707. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1708. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1709. result.clear();
  1710. util::parsePrioritizePieceRange(result, "tail=1", entries, pieceLength);
  1711. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1712. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1713. CPPUNIT_ASSERT_EQUAL((size_t)3, result[1]);
  1714. CPPUNIT_ASSERT_EQUAL((size_t)6, result[2]);
  1715. result.clear();
  1716. util::parsePrioritizePieceRange(result, "head=1K", entries, pieceLength);
  1717. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1718. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1719. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1720. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1721. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1722. result.clear();
  1723. util::parsePrioritizePieceRange(result, "head", entries, pieceLength, 1024);
  1724. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1725. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1726. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1727. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1728. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1729. result.clear();
  1730. util::parsePrioritizePieceRange(result, "tail=1K", entries, pieceLength);
  1731. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1732. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1733. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1734. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1735. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1736. result.clear();
  1737. util::parsePrioritizePieceRange(result, "tail", entries, pieceLength, 1024);
  1738. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1739. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1740. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1741. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1742. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1743. result.clear();
  1744. util::parsePrioritizePieceRange(result, "head=1,tail=1", entries, pieceLength);
  1745. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1746. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1747. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1748. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1749. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1750. result.clear();
  1751. util::parsePrioritizePieceRange(result, "head=300M,tail=300M",
  1752. entries, pieceLength);
  1753. CPPUNIT_ASSERT_EQUAL((size_t)7, result.size());
  1754. for(size_t i = 0; i < 7; ++i) {
  1755. CPPUNIT_ASSERT_EQUAL(i, result[i]);
  1756. }
  1757. result.clear();
  1758. util::parsePrioritizePieceRange(result, "", entries, pieceLength);
  1759. CPPUNIT_ASSERT(result.empty());
  1760. }
  1761. void UtilTest::testApplyDir()
  1762. {
  1763. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir("", "pred"));
  1764. CPPUNIT_ASSERT_EQUAL(std::string("/pred"), util::applyDir("/", "pred"));
  1765. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir(".", "pred"));
  1766. CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred"));
  1767. }
  1768. void UtilTest::testFixTaintedBasename()
  1769. {
  1770. CPPUNIT_ASSERT_EQUAL(std::string("a%2Fb"), util::fixTaintedBasename("a/b"));
  1771. #ifdef __MINGW32__
  1772. CPPUNIT_ASSERT_EQUAL(std::string("a%5Cb"), util::fixTaintedBasename("a\\b"));
  1773. #else // !__MINGW32__
  1774. CPPUNIT_ASSERT_EQUAL(std::string("a\\b"), util::fixTaintedBasename("a\\b"));
  1775. #endif // !__MINGW32__
  1776. }
  1777. void UtilTest::testIsNumericHost()
  1778. {
  1779. CPPUNIT_ASSERT(util::isNumericHost("192.168.0.1"));
  1780. CPPUNIT_ASSERT(!util::isNumericHost("aria2.sf.net"));
  1781. CPPUNIT_ASSERT(util::isNumericHost("::1"));
  1782. }
  1783. void UtilTest::testDetectDirTraversal()
  1784. {
  1785. CPPUNIT_ASSERT(util::detectDirTraversal("/foo"));
  1786. CPPUNIT_ASSERT(util::detectDirTraversal("./foo"));
  1787. CPPUNIT_ASSERT(util::detectDirTraversal("../foo"));
  1788. CPPUNIT_ASSERT(util::detectDirTraversal("foo/../bar"));
  1789. CPPUNIT_ASSERT(util::detectDirTraversal("foo/./bar"));
  1790. CPPUNIT_ASSERT(util::detectDirTraversal("foo/."));
  1791. CPPUNIT_ASSERT(util::detectDirTraversal("foo/.."));
  1792. CPPUNIT_ASSERT(util::detectDirTraversal("."));
  1793. CPPUNIT_ASSERT(util::detectDirTraversal(".."));
  1794. CPPUNIT_ASSERT(util::detectDirTraversal("/"));
  1795. CPPUNIT_ASSERT(util::detectDirTraversal("foo/"));
  1796. CPPUNIT_ASSERT(util::detectDirTraversal("\t"));
  1797. CPPUNIT_ASSERT(!util::detectDirTraversal("foo/bar"));
  1798. CPPUNIT_ASSERT(!util::detectDirTraversal("foo"));
  1799. }
  1800. void UtilTest::testEscapePath()
  1801. {
  1802. CPPUNIT_ASSERT_EQUAL(std::string("foo%00bar%00%01"),
  1803. util::escapePath(std::string("foo")+(char)0x00+
  1804. std::string("bar")+(char)0x00+
  1805. (char)0x01));
  1806. #ifdef __MINGW32__
  1807. CPPUNIT_ASSERT_EQUAL(std::string("foo%5Cbar"), util::escapePath("foo\\bar"));
  1808. #else // !__MINGW32__
  1809. CPPUNIT_ASSERT_EQUAL(std::string("foo\\bar"), util::escapePath("foo\\bar"));
  1810. #endif // !__MINGW32__
  1811. }
  1812. void UtilTest::testInSameCidrBlock()
  1813. {
  1814. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 16));
  1815. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 17));
  1816. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "192.168.0.1", 32));
  1817. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.0.1", "192.168.0.0", 32));
  1818. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "10.0.0.1", 0));
  1819. CPPUNIT_ASSERT(util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 28));
  1820. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 29));
  1821. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "192.168.0.1", 8));
  1822. }
  1823. void UtilTest::testIsUtf8String()
  1824. {
  1825. CPPUNIT_ASSERT(util::isUtf8("ascii"));
  1826. // "Hello World" in Japanese UTF-8
  1827. CPPUNIT_ASSERT(util::isUtf8
  1828. (fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
  1829. // "World" in Shift_JIS
  1830. CPPUNIT_ASSERT(!util::isUtf8(fromHex("90a28a")+"E"));
  1831. // UTF8-2
  1832. CPPUNIT_ASSERT(util::isUtf8(fromHex("c280")));
  1833. CPPUNIT_ASSERT(util::isUtf8(fromHex("dfbf")));
  1834. // UTF8-3
  1835. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0a080")));
  1836. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0bf80")));
  1837. CPPUNIT_ASSERT(util::isUtf8(fromHex("e18080")));
  1838. CPPUNIT_ASSERT(util::isUtf8(fromHex("ec8080")));
  1839. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed8080")));
  1840. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed9f80")));
  1841. CPPUNIT_ASSERT(util::isUtf8(fromHex("ee8080")));
  1842. CPPUNIT_ASSERT(util::isUtf8(fromHex("ef8080")));
  1843. // UTF8-4
  1844. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0908080")));
  1845. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0bf8080")));
  1846. CPPUNIT_ASSERT(util::isUtf8(fromHex("f1808080")));
  1847. CPPUNIT_ASSERT(util::isUtf8(fromHex("f3808080")));
  1848. CPPUNIT_ASSERT(util::isUtf8(fromHex("f4808080")));
  1849. CPPUNIT_ASSERT(util::isUtf8(fromHex("f48f8080")));
  1850. CPPUNIT_ASSERT(util::isUtf8(""));
  1851. CPPUNIT_ASSERT(!util::isUtf8(fromHex("00")));
  1852. }
  1853. void UtilTest::testNextParam()
  1854. {
  1855. std::string s1 = " :a : b=c :d=b::::g::";
  1856. std::pair<std::string::iterator, bool> r;
  1857. std::string name, value;
  1858. r = util::nextParam(name, value, s1.begin(), s1.end(), ':');
  1859. CPPUNIT_ASSERT(r.second);
  1860. CPPUNIT_ASSERT_EQUAL(std::string("a"), name);
  1861. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1862. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1863. CPPUNIT_ASSERT(r.second);
  1864. CPPUNIT_ASSERT_EQUAL(std::string("b"), name);
  1865. CPPUNIT_ASSERT_EQUAL(std::string("c"), value);
  1866. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1867. CPPUNIT_ASSERT(r.second);
  1868. CPPUNIT_ASSERT_EQUAL(std::string("d"), name);
  1869. CPPUNIT_ASSERT_EQUAL(std::string("b"), value);
  1870. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1871. CPPUNIT_ASSERT(r.second);
  1872. CPPUNIT_ASSERT_EQUAL(std::string("g"), name);
  1873. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1874. std::string s2 = "";
  1875. r = util::nextParam(name, value, s2.begin(), s2.end(), ':');
  1876. CPPUNIT_ASSERT(!r.second);
  1877. std::string s3 = " ";
  1878. r = util::nextParam(name, value, s3.begin(), s3.end(), ':');
  1879. CPPUNIT_ASSERT(!r.second);
  1880. std::string s4 = ":::";
  1881. r = util::nextParam(name, value, s4.begin(), s4.end(), ':');
  1882. CPPUNIT_ASSERT(!r.second);
  1883. }
  1884. void UtilTest::testNoProxyDomainMatch()
  1885. {
  1886. CPPUNIT_ASSERT(util::noProxyDomainMatch("localhost", "localhost"));
  1887. CPPUNIT_ASSERT(util::noProxyDomainMatch("192.168.0.1", "192.168.0.1"));
  1888. CPPUNIT_ASSERT(util::noProxyDomainMatch("www.example.org", ".example.org"));
  1889. CPPUNIT_ASSERT(!util::noProxyDomainMatch("www.example.org", "example.org"));
  1890. CPPUNIT_ASSERT(!util::noProxyDomainMatch("192.168.0.1", "0.1"));
  1891. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "example.com"));
  1892. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org"));
  1893. }
  1894. void UtilTest::testInPrivateAddress()
  1895. {
  1896. CPPUNIT_ASSERT(!util::inPrivateAddress("localhost"));
  1897. CPPUNIT_ASSERT(util::inPrivateAddress("192.168.0.1"));
  1898. // Only checks prefix..
  1899. CPPUNIT_ASSERT(util::inPrivateAddress("10."));
  1900. CPPUNIT_ASSERT(!util::inPrivateAddress("172."));
  1901. CPPUNIT_ASSERT(!util::inPrivateAddress("172.15.0.0"));
  1902. CPPUNIT_ASSERT(util::inPrivateAddress("172.16.0.0"));
  1903. CPPUNIT_ASSERT(util::inPrivateAddress("172.31.0.0"));
  1904. CPPUNIT_ASSERT(!util::inPrivateAddress("172.32.0.0"));
  1905. }
  1906. void UtilTest::testSecfmt()
  1907. {
  1908. CPPUNIT_ASSERT_EQUAL(std::string("0s"), util::secfmt(0));
  1909. CPPUNIT_ASSERT_EQUAL(std::string("1s"), util::secfmt(1));
  1910. CPPUNIT_ASSERT_EQUAL(std::string("9s"), util::secfmt(9));
  1911. CPPUNIT_ASSERT_EQUAL(std::string("10s"), util::secfmt(10));
  1912. CPPUNIT_ASSERT_EQUAL(std::string("1m"), util::secfmt(60));
  1913. CPPUNIT_ASSERT_EQUAL(std::string("1m59s"), util::secfmt(119));
  1914. CPPUNIT_ASSERT_EQUAL(std::string("2m"), util::secfmt(120));
  1915. CPPUNIT_ASSERT_EQUAL(std::string("59m59s"), util::secfmt(3599));
  1916. CPPUNIT_ASSERT_EQUAL(std::string("1h"), util::secfmt(3600));
  1917. }
  1918. void UtilTest::testTlsHostnameMatch()
  1919. {
  1920. CPPUNIT_ASSERT(util::tlsHostnameMatch("Foo.com", "foo.com"));
  1921. CPPUNIT_ASSERT(util::tlsHostnameMatch("*.a.com", "foo.a.com"));
  1922. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*.a.com", "bar.foo.a.com"));
  1923. CPPUNIT_ASSERT(!util::tlsHostnameMatch("f*.com", "foo.com"));
  1924. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*.com", "bar.com"));
  1925. CPPUNIT_ASSERT(util::tlsHostnameMatch("com", "com"));
  1926. CPPUNIT_ASSERT(!util::tlsHostnameMatch("foo.*", "foo.com"));
  1927. CPPUNIT_ASSERT(util::tlsHostnameMatch("a.foo.com", "A.foo.com"));
  1928. CPPUNIT_ASSERT(!util::tlsHostnameMatch("a.foo.com", "b.foo.com"));
  1929. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*a.foo.com", "a.foo.com"));
  1930. CPPUNIT_ASSERT(util::tlsHostnameMatch("*a.foo.com", "ba.foo.com"));
  1931. CPPUNIT_ASSERT(!util::tlsHostnameMatch("a*.foo.com", "a.foo.com"));
  1932. CPPUNIT_ASSERT(util::tlsHostnameMatch("a*.foo.com", "ab.foo.com"));
  1933. CPPUNIT_ASSERT(!util::tlsHostnameMatch("foo.b*z.foo.com", "foo.baz.foo.com"));
  1934. CPPUNIT_ASSERT(util::tlsHostnameMatch("B*z.foo.com", "bAZ.Foo.com"));
  1935. CPPUNIT_ASSERT(!util::tlsHostnameMatch("b*z.foo.com", "bz.foo.com"));
  1936. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*", "foo"));
  1937. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*", ""));
  1938. CPPUNIT_ASSERT(util::tlsHostnameMatch("", ""));
  1939. CPPUNIT_ASSERT(!util::tlsHostnameMatch("xn--*.a.b", "xn--c.a.b"));
  1940. }
  1941. } // namespace aria2