UtilTest.cc 83 KB

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