UtilTest.cc 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  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(testRandomAlpha);
  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(testAlphaToNum);
  50. CPPUNIT_TEST(testMkdirs);
  51. CPPUNIT_TEST(testConvertBitfield);
  52. CPPUNIT_TEST(testParseIntSegments);
  53. CPPUNIT_TEST(testParseIntSegments_invalidRange);
  54. CPPUNIT_TEST(testParseInt);
  55. CPPUNIT_TEST(testParseUInt);
  56. CPPUNIT_TEST(testParseLLInt);
  57. CPPUNIT_TEST(testParseIntNoThrow);
  58. CPPUNIT_TEST(testParseUIntNoThrow);
  59. CPPUNIT_TEST(testParseLLIntNoThrow);
  60. CPPUNIT_TEST(testToString_binaryStream);
  61. CPPUNIT_TEST(testItos);
  62. CPPUNIT_TEST(testUitos);
  63. CPPUNIT_TEST(testNtoh64);
  64. CPPUNIT_TEST(testPercentEncode);
  65. CPPUNIT_TEST(testHtmlEscape);
  66. CPPUNIT_TEST(testJoinPath);
  67. CPPUNIT_TEST(testParseIndexPath);
  68. CPPUNIT_TEST(testCreateIndexPaths);
  69. CPPUNIT_TEST(testGenerateRandomData);
  70. CPPUNIT_TEST(testFromHex);
  71. CPPUNIT_TEST(testParsePrioritizePieceRange);
  72. CPPUNIT_TEST(testApplyDir);
  73. CPPUNIT_TEST(testFixTaintedBasename);
  74. CPPUNIT_TEST(testIsNumericHost);
  75. CPPUNIT_TEST(testDetectDirTraversal);
  76. CPPUNIT_TEST(testEscapePath);
  77. CPPUNIT_TEST(testInSameCidrBlock);
  78. CPPUNIT_TEST(testIsUtf8String);
  79. CPPUNIT_TEST(testNextParam);
  80. CPPUNIT_TEST(testNoProxyDomainMatch);
  81. CPPUNIT_TEST(testInPrivateAddress);
  82. CPPUNIT_TEST(testSecfmt);
  83. CPPUNIT_TEST_SUITE_END();
  84. private:
  85. public:
  86. void setUp() {
  87. }
  88. void testStrip();
  89. void testStripIter();
  90. void testLstripIter();
  91. void testLstripIter_char();
  92. void testDivide();
  93. void testSplit();
  94. void testSplitIter();
  95. void testSplitIterM();
  96. void testStreq();
  97. void testStrieq();
  98. void testStrifind();
  99. void testEndsWith();
  100. void testIendsWith();
  101. void testReplace();
  102. void testStartsWith();
  103. void testIstartsWith();
  104. // may be moved to other helper class in the future.
  105. void testGetContentDispositionFilename();
  106. void testRandomAlpha();
  107. void testToUpper();
  108. void testToLower();
  109. void testUppercase();
  110. void testLowercase();
  111. void testPercentDecode();
  112. void testGetRealSize();
  113. void testAbbrevSize();
  114. void testToStream();
  115. void testIsNumber();
  116. void testIsLowercase();
  117. void testIsUppercase();
  118. void testAlphaToNum();
  119. void testMkdirs();
  120. void testConvertBitfield();
  121. void testParseIntSegments();
  122. void testParseIntSegments_invalidRange();
  123. void testParseInt();
  124. void testParseUInt();
  125. void testParseLLInt();
  126. void testParseIntNoThrow();
  127. void testParseUIntNoThrow();
  128. void testParseLLIntNoThrow();
  129. void testToString_binaryStream();
  130. void testItos();
  131. void testUitos();
  132. void testNtoh64();
  133. void testPercentEncode();
  134. void testHtmlEscape();
  135. void testJoinPath();
  136. void testParseIndexPath();
  137. void testCreateIndexPaths();
  138. void testGenerateRandomData();
  139. void testFromHex();
  140. void testParsePrioritizePieceRange();
  141. void testApplyDir();
  142. void testFixTaintedBasename();
  143. void testIsNumericHost();
  144. void testDetectDirTraversal();
  145. void testEscapePath();
  146. void testInSameCidrBlock();
  147. void testIsUtf8String();
  148. void testNextParam();
  149. void testNoProxyDomainMatch();
  150. void testInPrivateAddress();
  151. void testSecfmt();
  152. };
  153. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  154. void UtilTest::testStrip()
  155. {
  156. std::string str1 = "aria2";
  157. CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2"));
  158. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2"));
  159. CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2 "));
  160. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
  161. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
  162. std::string str2 = "aria2 debut";
  163. CPPUNIT_ASSERT_EQUAL(str2, util::strip("aria2 debut"));
  164. CPPUNIT_ASSERT_EQUAL(str2, util::strip(" aria2 debut "));
  165. std::string str3 = "";
  166. CPPUNIT_ASSERT_EQUAL(str3, util::strip(""));
  167. CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
  168. CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
  169. std::string str4 = "A";
  170. CPPUNIT_ASSERT_EQUAL(str4, util::strip("A"));
  171. CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
  172. CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
  173. }
  174. void UtilTest::testStripIter()
  175. {
  176. Scip p;
  177. std::string str1 = "aria2";
  178. std::string s = "aria2";
  179. p = util::stripIter(s.begin(), s.end());
  180. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  181. s = " aria2";
  182. p = util::stripIter(s.begin(), s.end());
  183. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  184. s = "aria2 ";
  185. p = util::stripIter(s.begin(), s.end());
  186. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  187. s = " aria2 ";
  188. p = util::stripIter(s.begin(), s.end());
  189. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  190. s = " aria2 ";
  191. p = util::stripIter(s.begin(), s.end());
  192. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  193. std::string str2 = "aria2 debut";
  194. s = "aria2 debut";
  195. p = util::stripIter(s.begin(), s.end());
  196. CPPUNIT_ASSERT_EQUAL(str2, std::string(p.first, p.second));
  197. s = " aria2 debut ";
  198. p = util::stripIter(s.begin(), s.end());
  199. CPPUNIT_ASSERT_EQUAL(str2, std::string(p.first, p.second));
  200. std::string str3 = "";
  201. s = "";
  202. p = util::stripIter(s.begin(), s.end());
  203. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  204. s = " ";
  205. p = util::stripIter(s.begin(), s.end());
  206. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  207. s = " ";
  208. p = util::stripIter(s.begin(), s.end());
  209. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  210. std::string str4 = "A";
  211. s = "A";
  212. p = util::stripIter(s.begin(), s.end());
  213. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  214. s = " A ";
  215. p = util::stripIter(s.begin(), s.end());
  216. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  217. s = " A ";
  218. p = util::stripIter(s.begin(), s.end());
  219. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  220. }
  221. void UtilTest::testLstripIter()
  222. {
  223. std::string::iterator r;
  224. std::string s = "foo";
  225. r = util::lstripIter(s.begin(), s.end());
  226. CPPUNIT_ASSERT_EQUAL(std::string("foo"), std::string(r, s.end()));
  227. s = " foo bar ";
  228. r = util::lstripIter(s.begin(), s.end());
  229. CPPUNIT_ASSERT_EQUAL(std::string("foo bar "), std::string(r, s.end()));
  230. s = "f";
  231. r = util::lstripIter(s.begin(), s.end());
  232. CPPUNIT_ASSERT_EQUAL(std::string("f"), std::string(r, s.end()));
  233. s = "foo ";
  234. r = util::lstripIter(s.begin(), s.end());
  235. CPPUNIT_ASSERT_EQUAL(std::string("foo "), std::string(r, s.end()));
  236. }
  237. void UtilTest::testLstripIter_char()
  238. {
  239. std::string::iterator r;
  240. std::string s = "foo";
  241. r = util::lstripIter(s.begin(), s.end(), '$');
  242. CPPUNIT_ASSERT_EQUAL(std::string("foo"), std::string(r, s.end()));
  243. s = "$$foo$bar$$";
  244. r = util::lstripIter(s.begin(), s.end(), '$');
  245. CPPUNIT_ASSERT_EQUAL(std::string("foo$bar$$"), std::string(r, s.end()));
  246. s = "f";
  247. r = util::lstripIter(s.begin(), s.end(), '$');
  248. CPPUNIT_ASSERT_EQUAL(std::string("f"), std::string(r, s.end()));
  249. s = "foo$$";
  250. r = util::lstripIter(s.begin(), s.end(), '$');
  251. CPPUNIT_ASSERT_EQUAL(std::string("foo$$"), std::string(r, s.end()));
  252. }
  253. void UtilTest::testDivide() {
  254. std::pair<Sip, Sip> p1;
  255. std::string s = "name=value";
  256. util::divide(p1, s.begin(), s.end(), '=');
  257. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  258. std::string(p1.first.first, p1.first.second));
  259. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  260. std::string(p1.second.first, p1.second.second));
  261. s = " name = value ";
  262. util::divide(p1, s.begin(), s.end(), '=');
  263. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  264. std::string(p1.first.first, p1.first.second));
  265. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  266. std::string(p1.second.first, p1.second.second));
  267. s = "=value";
  268. util::divide(p1, s.begin(), s.end(), '=');
  269. CPPUNIT_ASSERT_EQUAL(std::string(""),
  270. std::string(p1.first.first, p1.first.second));
  271. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  272. std::string(p1.second.first, p1.second.second));
  273. s = "name=";
  274. util::divide(p1, s.begin(), s.end(), '=');
  275. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  276. std::string(p1.first.first, p1.first.second));
  277. CPPUNIT_ASSERT_EQUAL(std::string(""),
  278. std::string(p1.second.first, p1.second.second));
  279. s = "name";
  280. util::divide(p1, s.begin(), s.end(), '=');
  281. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  282. std::string(p1.first.first, p1.first.second));
  283. CPPUNIT_ASSERT_EQUAL(std::string(""),
  284. std::string(p1.second.first, p1.second.second));
  285. }
  286. void UtilTest::testSplit() {
  287. std::vector<std::string> v;
  288. std::string s = "k1; k2;; k3";
  289. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true);
  290. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  291. std::vector<std::string>::iterator itr = v.begin();
  292. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  293. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  294. CPPUNIT_ASSERT_EQUAL(std::string("k3"), *itr++);
  295. v.clear();
  296. s = "k1; k2; k3";
  297. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  298. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  299. itr = v.begin();
  300. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  301. CPPUNIT_ASSERT_EQUAL(std::string(" k2"), *itr++);
  302. CPPUNIT_ASSERT_EQUAL(std::string(" k3"), *itr++);
  303. v.clear();
  304. s = "k=v";
  305. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  306. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  307. itr = v.begin();
  308. CPPUNIT_ASSERT_EQUAL(std::string("k=v"), *itr++);
  309. v.clear();
  310. s = ";;k1;;k2;";
  311. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  312. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  313. itr = v.begin();
  314. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  315. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  316. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  317. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  318. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  319. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  320. v.clear();
  321. s = ";;k1;;k2;";
  322. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  323. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  324. itr = v.begin();
  325. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  326. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  327. v.clear();
  328. s = "k; ";
  329. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  330. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  331. itr = v.begin();
  332. CPPUNIT_ASSERT_EQUAL(std::string("k"), *itr++);
  333. CPPUNIT_ASSERT_EQUAL(std::string(" "), *itr++);
  334. v.clear();
  335. s = " ";
  336. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true, true);
  337. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  338. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  339. v.clear();
  340. s = " ";
  341. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true);
  342. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  343. v.clear();
  344. s = " ";
  345. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  346. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  347. CPPUNIT_ASSERT_EQUAL(std::string(" "), v[0]);
  348. v.clear();
  349. s = ";";
  350. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  351. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  352. v.clear();
  353. s = ";";
  354. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  355. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  356. itr = v.begin();
  357. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  358. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  359. v.clear();
  360. s = "";
  361. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  362. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  363. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  364. }
  365. void UtilTest::testSplitIter() {
  366. std::vector<Scip> v;
  367. std::string s = "k1; k2;; k3";
  368. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true);
  369. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  370. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  371. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  372. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  373. v.clear();
  374. s = "k1; k2; k3";
  375. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  376. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  377. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  378. CPPUNIT_ASSERT_EQUAL(std::string(" k2"),
  379. std::string(v[1].first, v[1].second));
  380. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  381. std::string(v[2].first, v[2].second));
  382. v.clear();
  383. s = "k=v";
  384. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  385. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  386. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  387. std::string(v[0].first, v[0].second));
  388. v.clear();
  389. s = ";;k1;;k2;";
  390. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  391. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  392. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  393. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  394. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  395. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  396. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  397. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  398. v.clear();
  399. s = ";;k1;;k2;";
  400. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  401. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  402. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  403. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  404. v.clear();
  405. s = "k; ";
  406. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  407. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  408. CPPUNIT_ASSERT_EQUAL(std::string("k"), std::string(v[0].first, v[0].second));
  409. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  410. v.clear();
  411. s = " ";
  412. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true, true);
  413. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  414. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  415. v.clear();
  416. s = " ";
  417. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true);
  418. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  419. v.clear();
  420. s = " ";
  421. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  422. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  423. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[0].first, v[0].second));
  424. v.clear();
  425. s = ";";
  426. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  427. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  428. v.clear();
  429. s = ";";
  430. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  431. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  432. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  433. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  434. v.clear();
  435. s = "";
  436. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  437. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  438. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  439. }
  440. void UtilTest::testSplitIterM() {
  441. std::string d = ";";
  442. std::string md = "; ";
  443. std::vector<Scip> v;
  444. std::string s = "k1; k2;; k3";
  445. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  446. d.begin(), d.end(), true);
  447. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  448. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  449. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  450. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  451. v.clear();
  452. s = "k1; k2; k3";
  453. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  454. d.begin(), d.end());
  455. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  456. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  457. CPPUNIT_ASSERT_EQUAL(std::string(" k2"),
  458. std::string(v[1].first, v[1].second));
  459. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  460. 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),
  464. md.begin(), md.end());
  465. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  466. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  467. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  468. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  469. v.clear();
  470. s = "k1; k2; k3;";
  471. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  472. md.begin(), md.end(), false, true);
  473. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  474. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  475. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  476. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[2].first, v[2].second));
  477. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  478. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[4].first, v[4].second));
  479. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  480. v.clear();
  481. s = "k=v";
  482. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  483. d.begin(), d.end(), false, true);
  484. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  485. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  486. std::string(v[0].first, v[0].second));
  487. v.clear();
  488. s = ";;k1;;k2;";
  489. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  490. d.begin(), d.end(), false, true);
  491. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  492. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  493. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  494. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  495. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  496. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  497. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  498. v.clear();
  499. s = ";;k1;;k2;";
  500. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  501. d.begin(), d.end());
  502. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  503. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  504. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  505. v.clear();
  506. s = "k; ";
  507. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  508. d.begin(), d.end());
  509. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  510. CPPUNIT_ASSERT_EQUAL(std::string("k"), std::string(v[0].first, v[0].second));
  511. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  512. v.clear();
  513. s = " ";
  514. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  515. d.begin(), d.end(), true, true);
  516. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  517. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  518. v.clear();
  519. s = " ";
  520. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  521. d.begin(), d.end(), true);
  522. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  523. v.clear();
  524. s = " ";
  525. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  526. d.begin(), d.end());
  527. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  528. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[0].first, v[0].second));
  529. v.clear();
  530. s = ";";
  531. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  532. d.begin(), d.end());
  533. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  534. v.clear();
  535. s = ";";
  536. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  537. d.begin(), d.end(), false, true);
  538. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  539. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  540. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  541. v.clear();
  542. s = "";
  543. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  544. d.begin(), d.end(), false, true);
  545. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  546. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  547. }
  548. void UtilTest::testEndsWith() {
  549. std::string target = "abcdefg";
  550. std::string part = "fg";
  551. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  552. part.begin(), part.end()));
  553. target = "abdefg";
  554. part = "g";
  555. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  556. part.begin(), part.end()));
  557. target = "abdefg";
  558. part = "eg";
  559. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  560. part.begin(), part.end()));
  561. target = "g";
  562. part = "eg";
  563. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  564. part.begin(), part.end()));
  565. target = "g";
  566. part = "g";
  567. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  568. part.begin(), part.end()));
  569. target = "g";
  570. part = "";
  571. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  572. part.begin(), part.end()));
  573. target = "";
  574. part = "";
  575. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  576. part.begin(), part.end()));
  577. target = "";
  578. part = "g";
  579. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  580. part.begin(), part.end()));
  581. }
  582. void UtilTest::testIendsWith() {
  583. std::string target = "abcdefg";
  584. std::string part = "Fg";
  585. CPPUNIT_ASSERT(util::iendsWith(target.begin(), target.end(),
  586. part.begin(), part.end()));
  587. target = "abdefg";
  588. part = "ef";
  589. CPPUNIT_ASSERT(!util::iendsWith(target.begin(), target.end(),
  590. part.begin(), part.end()));
  591. }
  592. void UtilTest::testStreq()
  593. {
  594. std::string s1, s2;
  595. s1 = "foo";
  596. s2 = "foo";
  597. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  598. s2 = "fooo";
  599. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  600. s2 = "fo";
  601. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  602. s2 = "";
  603. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  604. s1 = "";
  605. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  606. }
  607. void UtilTest::testStrieq()
  608. {
  609. std::string s1, s2;
  610. s1 = "foo";
  611. s2 = "foo";
  612. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  613. s1 = "FoO";
  614. s2 = "fOo";
  615. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  616. s2 = "fooo";
  617. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  618. s2 = "fo";
  619. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  620. s2 = "";
  621. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  622. s1 = "";
  623. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  624. }
  625. void UtilTest::testStrifind()
  626. {
  627. std::string s1, s2;
  628. s1 = "yamagakani mukashi wo toheba hARU no tuki";
  629. s2 = "HaRu";
  630. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  631. != s1.end());
  632. s2 = "aki";
  633. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  634. == s1.end());
  635. s1 = "h";
  636. s2 = "HH";
  637. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  638. == s1.end());
  639. }
  640. void UtilTest::testReplace() {
  641. CPPUNIT_ASSERT_EQUAL(std::string("abc\n"), util::replace("abc\r\n", "\r", ""));
  642. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc\r\n", "\r\n", ""));
  643. CPPUNIT_ASSERT_EQUAL(std::string(""), util::replace("", "\r\n", ""));
  644. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc", "", "a"));
  645. CPPUNIT_ASSERT_EQUAL(std::string("xbc"), util::replace("abc", "a", "x"));
  646. }
  647. void UtilTest::testStartsWith() {
  648. std::string target;
  649. std::string part;
  650. target = "abcdefg";
  651. part = "abc";
  652. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  653. part.begin(), part.end()));
  654. target = "abcdefg";
  655. part = "abx";
  656. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  657. part.begin(), part.end()));
  658. target = "abcdefg";
  659. part = "bcd";
  660. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  661. part.begin(), part.end()));
  662. target = "";
  663. part = "a";
  664. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  665. part.begin(), part.end()));
  666. target = "";
  667. part = "";
  668. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  669. part.begin(), part.end()));
  670. target = "a";
  671. part = "";
  672. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  673. part.begin(), part.end()));
  674. target = "a";
  675. part = "a";
  676. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  677. part.begin(), part.end()));
  678. }
  679. void UtilTest::testIstartsWith() {
  680. std::string target;
  681. std::string part;
  682. target = "abcdefg";
  683. part = "aBc";
  684. CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(),
  685. part.begin(), part.end()));
  686. target = "abcdefg";
  687. part = "abx";
  688. CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(),
  689. part.begin(), part.end()));
  690. }
  691. void UtilTest::testGetContentDispositionFilename() {
  692. std::string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  693. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h1));
  694. std::string h2 = "attachment; filename=\"\"";
  695. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h2));
  696. std::string h3 = "attachment; filename=\"";
  697. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h3));
  698. std::string h3_2 = "attachment; filename= \" aria2.tar.bz2 \"";
  699. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  700. util::getContentDispositionFilename(h3_2));
  701. std::string h4 = "attachment;";
  702. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h4));
  703. std::string h5 = "attachment; filename=aria2.tar.bz2";
  704. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h5));
  705. std::string h6 = "attachment; filename='aria2.tar.bz2'";
  706. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h6));
  707. std::string h7 = "attachment; filename='aria2.tar.bz2";
  708. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h7));
  709. std::string h8 = "attachment; filename=aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT";
  710. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h8));
  711. std::string h9 = "attachment; filename=\"aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT\"";
  712. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT"),
  713. util::getContentDispositionFilename(h9));
  714. std::string h10 = "attachment; filename=";
  715. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h10));
  716. std::string h11 = "attachment; filename=;";
  717. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h11));
  718. std::string filenameWithDir = "attachment; filename=dir/file";
  719. CPPUNIT_ASSERT_EQUAL(std::string(""),
  720. util::getContentDispositionFilename(filenameWithDir));
  721. std::string semicolonInside = "attachment; filename=\"foo;bar\"";
  722. CPPUNIT_ASSERT_EQUAL(std::string("foo;bar"),
  723. util::getContentDispositionFilename(semicolonInside));
  724. CPPUNIT_ASSERT_EQUAL
  725. (std::string(""),
  726. util::getContentDispositionFilename("filename=\"%2E%2E%2Ffoo.html\""));
  727. // RFC2231 Section4
  728. std::string extparam2 = "filename*=''aria2";
  729. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  730. util::getContentDispositionFilename(extparam2));
  731. std::string extparam3 = "filename*='''";
  732. CPPUNIT_ASSERT_EQUAL(std::string(""),
  733. util::getContentDispositionFilename(extparam3));
  734. std::string extparam4 = "filename*='aria2";
  735. CPPUNIT_ASSERT_EQUAL(std::string(""),
  736. util::getContentDispositionFilename(extparam4));
  737. std::string extparam5 = "filename*='''aria2";
  738. CPPUNIT_ASSERT_EQUAL(std::string(""),
  739. util::getContentDispositionFilename(extparam5));
  740. std::string extparam6 = "filename*";
  741. CPPUNIT_ASSERT_EQUAL(std::string(""),
  742. util::getContentDispositionFilename(extparam6));
  743. std::string extparam7 = "filename*=UTF-8''aria2;filename=hello%20world";
  744. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  745. util::getContentDispositionFilename(extparam7));
  746. std::string extparam8 = "filename=aria2;filename*=UTF-8''hello%20world";
  747. CPPUNIT_ASSERT_EQUAL(std::string("hello world"),
  748. util::getContentDispositionFilename(extparam8));
  749. std::string extparam9 = "filename*=ISO-8859-1''%A3";
  750. std::string extparam9ans;
  751. extparam9ans += 0xc2;
  752. extparam9ans += 0xa3;
  753. CPPUNIT_ASSERT_EQUAL(extparam9ans,
  754. util::getContentDispositionFilename(extparam9));
  755. CPPUNIT_ASSERT_EQUAL
  756. (std::string(""),
  757. util::getContentDispositionFilename("filename*=UTF-8''foo%2F.html"));
  758. CPPUNIT_ASSERT_EQUAL
  759. (std::string("foo.html"),
  760. util::getContentDispositionFilename("filename*=UTF-8'';filename=\"foo.html\""));
  761. CPPUNIT_ASSERT_EQUAL
  762. (std::string(""),
  763. util::getContentDispositionFilename("filename*=UTF-8''%2E%2E%2Ffoo.html"));
  764. // Tests from http://greenbytes.de/tech/tc2231/
  765. // attwithasciifnescapedchar
  766. CPPUNIT_ASSERT_EQUAL
  767. (std::string("foo.html"),
  768. util::getContentDispositionFilename("filename=\"f\\oo.html\""));
  769. // attwithasciifilenameucase
  770. CPPUNIT_ASSERT_EQUAL
  771. (std::string("foo.html"),
  772. util::getContentDispositionFilename("FILENAME=\"foo.html\""));
  773. // attwithisofn2231iso
  774. CPPUNIT_ASSERT_EQUAL
  775. (std::string("foo-ä.html"),
  776. util::getContentDispositionFilename("filename*=iso-8859-1''foo-%E4.html"));
  777. // attwithfn2231utf8
  778. CPPUNIT_ASSERT_EQUAL
  779. (std::string("foo-ä-€.html"),
  780. util::getContentDispositionFilename
  781. ("filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html"));
  782. // attwithfn2231utf8-bad
  783. CPPUNIT_ASSERT_EQUAL
  784. (std::string(""),
  785. util::getContentDispositionFilename
  786. ("filename*=iso-8859-1''foo-%c3%a4-%e2%82%ac.html"));
  787. // attwithfn2231ws1
  788. CPPUNIT_ASSERT_EQUAL
  789. (std::string(""),
  790. util::getContentDispositionFilename("filename *=UTF-8''foo-%c3%a4.html"));
  791. // attwithfn2231ws2
  792. CPPUNIT_ASSERT_EQUAL
  793. (std::string("foo-ä.html"),
  794. util::getContentDispositionFilename("filename*= UTF-8''foo-%c3%a4.html"));
  795. // attwithfn2231ws3
  796. CPPUNIT_ASSERT_EQUAL
  797. (std::string("foo-ä.html"),
  798. util::getContentDispositionFilename("filename* =UTF-8''foo-%c3%a4.html"));
  799. // attwithfn2231quot
  800. CPPUNIT_ASSERT_EQUAL
  801. (std::string(""),
  802. util::getContentDispositionFilename
  803. ("filename*=\"UTF-8''foo-%c3%a4.html\""));
  804. }
  805. class Printer {
  806. public:
  807. template<class T>
  808. void operator()(T t) {
  809. std::cerr << t << ", ";
  810. }
  811. };
  812. void UtilTest::testRandomAlpha() {
  813. SharedHandle<Randomizer> rand(new FixedNumberRandomizer());
  814. std::string s = util::randomAlpha(8, rand);
  815. CPPUNIT_ASSERT_EQUAL(std::string("AAAAAAAA"), s);
  816. }
  817. void UtilTest::testToUpper() {
  818. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  819. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  820. CPPUNIT_ASSERT_EQUAL(upp, util::toUpper(src));
  821. }
  822. void UtilTest::testToLower() {
  823. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  824. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  825. CPPUNIT_ASSERT_EQUAL(upp, util::toLower(src));
  826. }
  827. void UtilTest::testUppercase() {
  828. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  829. std::string ans = "608CABC0F2FA18C260CAFD974516865C772363D5";
  830. util::uppercase(src);
  831. CPPUNIT_ASSERT_EQUAL(ans, src);
  832. }
  833. void UtilTest::testLowercase() {
  834. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  835. std::string ans = "608cabc0f2fa18c260cafd974516865c772363d5";
  836. util::lowercase(src);
  837. CPPUNIT_ASSERT_EQUAL(ans, src);
  838. }
  839. void UtilTest::testPercentDecode() {
  840. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  841. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  842. util::percentDecode(src.begin(), src.end()));
  843. std::string src2 = "aria2+aria2";
  844. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"),
  845. util::percentDecode(src2.begin(), src2.end()));
  846. std::string src3 = "%5t%20";
  847. CPPUNIT_ASSERT_EQUAL(std::string("%5t "),
  848. util::percentDecode(src3.begin(), src3.end()));
  849. std::string src4 = "%";
  850. CPPUNIT_ASSERT_EQUAL(std::string("%"),
  851. util::percentDecode(src4.begin(), src4.end()));
  852. std::string src5 = "%3";
  853. CPPUNIT_ASSERT_EQUAL(std::string("%3"),
  854. util::percentDecode(src5.begin(), src5.end()));
  855. std::string src6 = "%2f";
  856. CPPUNIT_ASSERT_EQUAL(std::string("/"),
  857. util::percentDecode(src6.begin(), src6.end()));
  858. }
  859. void UtilTest::testGetRealSize()
  860. {
  861. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096M"));
  862. CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1K"));
  863. try {
  864. util::getRealSize("");
  865. CPPUNIT_FAIL("exception must be thrown.");
  866. } catch(Exception& e) {
  867. std::cerr << e.stackTrace();
  868. }
  869. try {
  870. util::getRealSize("foo");
  871. CPPUNIT_FAIL("exception must be thrown.");
  872. } catch(Exception& e) {
  873. std::cerr << e.stackTrace();
  874. }
  875. try {
  876. util::getRealSize("-1");
  877. CPPUNIT_FAIL("exception must be thrown.");
  878. } catch(Exception& e) {
  879. std::cerr << e.stackTrace();
  880. }
  881. try {
  882. util::getRealSize("9223372036854775807K");
  883. CPPUNIT_FAIL("exception must be thrown.");
  884. } catch(Exception& e) {
  885. std::cerr << e.stackTrace();
  886. }
  887. try {
  888. util::getRealSize("9223372036854775807M");
  889. CPPUNIT_FAIL("exception must be thrown.");
  890. } catch(Exception& e) {
  891. std::cerr << e.stackTrace();
  892. }
  893. }
  894. void UtilTest::testAbbrevSize()
  895. {
  896. CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), util::abbrevSize(4294967296LL));
  897. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1024));
  898. CPPUNIT_ASSERT_EQUAL(std::string("1,023"), util::abbrevSize(1023));
  899. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::abbrevSize(0));
  900. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), util::abbrevSize(1127));
  901. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), util::abbrevSize(1572864));
  902. }
  903. void UtilTest::testToStream()
  904. {
  905. std::ostringstream os;
  906. SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  907. SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  908. std::deque<SharedHandle<FileEntry> > entries;
  909. entries.push_back(f1);
  910. entries.push_back(f2);
  911. std::string filename = A2_TEST_OUT_DIR"/aria2_UtilTest_testToStream";
  912. BufferedFile fp(filename, BufferedFile::WRITE);
  913. util::toStream(entries.begin(), entries.end(), fp);
  914. fp.close();
  915. CPPUNIT_ASSERT_EQUAL(
  916. std::string("Files:\n"
  917. "idx|path/length\n"
  918. "===+===========================================================================\n"
  919. " 1|aria2.tar.bz2\n"
  920. " |12.0KiB (12,300)\n"
  921. "---+---------------------------------------------------------------------------\n"
  922. " 2|aria2.txt\n"
  923. " |556B (556)\n"
  924. "---+---------------------------------------------------------------------------\n"),
  925. readFile(filename));
  926. }
  927. void UtilTest::testIsNumber()
  928. {
  929. std::string s = "000";
  930. CPPUNIT_ASSERT_EQUAL(true, util::isNumber(s.begin(),s.end()));
  931. s = "a";
  932. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  933. s = "0a";
  934. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  935. s = "";
  936. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  937. s = " ";
  938. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  939. }
  940. void UtilTest::testIsLowercase()
  941. {
  942. std::string s = "alpha";
  943. CPPUNIT_ASSERT_EQUAL(true, util::isLowercase(s.begin(), s.end()));
  944. s = "Alpha";
  945. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  946. s = "1alpha";
  947. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  948. s = "";
  949. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  950. s = " ";
  951. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  952. }
  953. void UtilTest::testIsUppercase()
  954. {
  955. std::string s = "ALPHA";
  956. CPPUNIT_ASSERT_EQUAL(true, util::isUppercase(s.begin(), s.end()));
  957. s = "Alpha";
  958. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  959. s = "1ALPHA";
  960. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  961. s = "";
  962. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  963. s = " ";
  964. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  965. }
  966. void UtilTest::testAlphaToNum()
  967. {
  968. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("a"));
  969. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("aa"));
  970. CPPUNIT_ASSERT_EQUAL(1U, util::alphaToNum("b"));
  971. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("zz")); // 25*26+25
  972. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("ZZ")); // 25*26+25
  973. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum(""));
  974. CPPUNIT_ASSERT_EQUAL(4294967295U, util::alphaToNum("NXMRLXV"));
  975. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("NXMRLXW")); // uint32_t overflow
  976. }
  977. void UtilTest::testMkdirs()
  978. {
  979. std::string dir = A2_TEST_OUT_DIR"/aria2-UtilTest-testMkdirs";
  980. File d(dir);
  981. if(d.exists()) {
  982. CPPUNIT_ASSERT(d.remove());
  983. }
  984. CPPUNIT_ASSERT(!d.exists());
  985. util::mkdirs(dir);
  986. CPPUNIT_ASSERT(d.isDir());
  987. std::string file = A2_TEST_DIR"/UtilTest.cc";
  988. File f(file);
  989. CPPUNIT_ASSERT(f.isFile());
  990. try {
  991. util::mkdirs(file);
  992. CPPUNIT_FAIL("exception must be thrown.");
  993. } catch(DlAbortEx& ex) {
  994. std::cerr << ex.stackTrace() << std::endl;
  995. }
  996. }
  997. void UtilTest::testConvertBitfield()
  998. {
  999. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  1000. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  1001. srcBitfield.setAllBit();
  1002. srcBitfield.unsetBit(2);// <- range [768, 1152)
  1003. // which corresponds to the index [1,2] in destBitfield
  1004. util::convertBitfield(&destBitfield, &srcBitfield);
  1005. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  1006. util::toHex(destBitfield.getBitfield(),
  1007. destBitfield.getBitfieldLength()));
  1008. }
  1009. void UtilTest::testParseIntSegments()
  1010. {
  1011. SegList<int> sgl;
  1012. util::parseIntSegments(sgl, "1,3-8,10");
  1013. CPPUNIT_ASSERT(sgl.hasNext());
  1014. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  1015. CPPUNIT_ASSERT(sgl.hasNext());
  1016. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  1017. CPPUNIT_ASSERT(sgl.hasNext());
  1018. CPPUNIT_ASSERT_EQUAL(4, sgl.next());
  1019. CPPUNIT_ASSERT(sgl.hasNext());
  1020. CPPUNIT_ASSERT_EQUAL(5, sgl.next());
  1021. CPPUNIT_ASSERT(sgl.hasNext());
  1022. CPPUNIT_ASSERT_EQUAL(6, sgl.next());
  1023. CPPUNIT_ASSERT(sgl.hasNext());
  1024. CPPUNIT_ASSERT_EQUAL(7, sgl.next());
  1025. CPPUNIT_ASSERT(sgl.hasNext());
  1026. CPPUNIT_ASSERT_EQUAL(8, sgl.next());
  1027. CPPUNIT_ASSERT(sgl.hasNext());
  1028. CPPUNIT_ASSERT_EQUAL(10, sgl.next());
  1029. CPPUNIT_ASSERT(!sgl.hasNext());
  1030. CPPUNIT_ASSERT_EQUAL(0, sgl.next());
  1031. sgl.clear();
  1032. util::parseIntSegments(sgl, ",,,1,,,3,,,");
  1033. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  1034. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  1035. CPPUNIT_ASSERT(!sgl.hasNext());
  1036. }
  1037. void UtilTest::testParseIntSegments_invalidRange()
  1038. {
  1039. try {
  1040. SegList<int> sgl;
  1041. util::parseIntSegments(sgl, "-1");
  1042. CPPUNIT_FAIL("exception must be thrown.");
  1043. } catch(Exception& e) {
  1044. }
  1045. try {
  1046. SegList<int> sgl;
  1047. util::parseIntSegments(sgl, "1-");
  1048. CPPUNIT_FAIL("exception must be thrown.");
  1049. } catch(Exception& e) {
  1050. }
  1051. try {
  1052. SegList<int> sgl;
  1053. util::parseIntSegments(sgl, "2147483648");
  1054. CPPUNIT_FAIL("exception must be thrown.");
  1055. } catch(Exception& e) {
  1056. }
  1057. try {
  1058. SegList<int> sgl;
  1059. util::parseIntSegments(sgl, "2147483647-2147483648");
  1060. CPPUNIT_FAIL("exception must be thrown.");
  1061. } catch(Exception& e) {
  1062. }
  1063. try {
  1064. SegList<int> sgl;
  1065. util::parseIntSegments(sgl, "1-2x");
  1066. CPPUNIT_FAIL("exception must be thrown.");
  1067. } catch(Exception& e) {
  1068. }
  1069. try {
  1070. SegList<int> sgl;
  1071. util::parseIntSegments(sgl, "3x-4");
  1072. CPPUNIT_FAIL("exception must be thrown.");
  1073. } catch(Exception& e) {
  1074. }
  1075. }
  1076. void UtilTest::testParseInt()
  1077. {
  1078. std::string s;
  1079. s = " -1 ";
  1080. CPPUNIT_ASSERT_EQUAL(-1, util::parseInt(s));
  1081. s = "2147483647";
  1082. CPPUNIT_ASSERT_EQUAL(2147483647, util::parseInt(s));
  1083. try {
  1084. s = "2147483648";
  1085. util::parseInt(s);
  1086. CPPUNIT_FAIL("exception must be thrown.");
  1087. } catch(Exception& e) {
  1088. }
  1089. try {
  1090. s = "-2147483649";
  1091. util::parseInt(s);
  1092. CPPUNIT_FAIL("exception must be thrown.");
  1093. } catch(Exception& e) {
  1094. }
  1095. try {
  1096. s = "12x";
  1097. util::parseInt(s);
  1098. CPPUNIT_FAIL("exception must be thrown.");
  1099. } catch(Exception& e) {
  1100. }
  1101. try {
  1102. s = "";
  1103. util::parseInt(s);
  1104. CPPUNIT_FAIL("exception must be thrown.");
  1105. } catch(Exception& e) {
  1106. }
  1107. }
  1108. void UtilTest::testParseUInt()
  1109. {
  1110. std::string s;
  1111. s = " 2147483647 ";
  1112. CPPUNIT_ASSERT_EQUAL(2147483647U, util::parseUInt(s));
  1113. try {
  1114. s = "-1";
  1115. util::parseUInt(s);
  1116. CPPUNIT_FAIL("exception must be thrown.");
  1117. } catch(Exception& e) {
  1118. }
  1119. try {
  1120. s = "2147483648";
  1121. util::parseUInt(s);
  1122. CPPUNIT_FAIL("exception must be thrown.");
  1123. } catch(Exception& e) {
  1124. }
  1125. }
  1126. void UtilTest::testParseLLInt()
  1127. {
  1128. std::string s;
  1129. {
  1130. s = " -1 ";
  1131. CPPUNIT_ASSERT_EQUAL((int64_t)-1LL, util::parseLLInt(s));
  1132. }
  1133. {
  1134. s = "9223372036854775807";
  1135. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
  1136. util::parseLLInt(s));
  1137. }
  1138. try {
  1139. s = "9223372036854775808";
  1140. util::parseLLInt(s);
  1141. CPPUNIT_FAIL("exception must be thrown.");
  1142. } catch(Exception& e) {
  1143. std::cerr << e.stackTrace();
  1144. }
  1145. try {
  1146. s = "-9223372036854775809";
  1147. util::parseLLInt(s);
  1148. CPPUNIT_FAIL("exception must be thrown.");
  1149. } catch(Exception& e) {
  1150. std::cerr << e.stackTrace();
  1151. }
  1152. try {
  1153. s = "12x";
  1154. util::parseLLInt(s);
  1155. CPPUNIT_FAIL("exception must be thrown.");
  1156. } catch(Exception& e) {
  1157. std::cerr << e.stackTrace();
  1158. }
  1159. try {
  1160. s = "";
  1161. util::parseLLInt(s);
  1162. CPPUNIT_FAIL("exception must be thrown.");
  1163. } catch(Exception& e) {
  1164. std::cerr << e.stackTrace();
  1165. }
  1166. }
  1167. void UtilTest::testParseIntNoThrow()
  1168. {
  1169. std::string s;
  1170. int32_t n;
  1171. s = " -1 ";
  1172. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s));
  1173. CPPUNIT_ASSERT_EQUAL((int32_t)-1, n);
  1174. s = "2147483647";
  1175. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s));
  1176. CPPUNIT_ASSERT_EQUAL((int32_t)2147483647, n);
  1177. s = "2147483648";
  1178. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1179. s = "-2147483649";
  1180. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1181. s = "12x";
  1182. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1183. s = "";
  1184. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1185. }
  1186. void UtilTest::testParseUIntNoThrow()
  1187. {
  1188. std::string s;
  1189. uint32_t n;
  1190. s = " 2147483647 ";
  1191. CPPUNIT_ASSERT(util::parseUIntNoThrow(n, s));
  1192. CPPUNIT_ASSERT_EQUAL((uint32_t)INT32_MAX, n);
  1193. s = "2147483648";
  1194. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s));
  1195. s = "-1";
  1196. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s));
  1197. }
  1198. void UtilTest::testParseLLIntNoThrow()
  1199. {
  1200. std::string s;
  1201. int64_t n;
  1202. s = " 9223372036854775807 ";
  1203. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s));
  1204. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MAX, n);
  1205. s = "9223372036854775808";
  1206. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s));
  1207. s = "-9223372036854775808";
  1208. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s));
  1209. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MIN, n);
  1210. s = "-9223372036854775809";
  1211. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s));
  1212. }
  1213. void UtilTest::testToString_binaryStream()
  1214. {
  1215. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  1216. std::string data(16*1024+256, 'a');
  1217. dw->initAndOpenFile();
  1218. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  1219. std::string readData = util::toString(dw);
  1220. CPPUNIT_ASSERT_EQUAL(data, readData);
  1221. }
  1222. void UtilTest::testItos()
  1223. {
  1224. {
  1225. int i = 0;
  1226. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::itos(i));
  1227. }
  1228. {
  1229. int i = 100;
  1230. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i, true));
  1231. }
  1232. {
  1233. int i = 100;
  1234. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i));
  1235. }
  1236. {
  1237. int i = 12345;
  1238. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), util::itos(i, true));
  1239. }
  1240. {
  1241. int i = 12345;
  1242. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::itos(i));
  1243. }
  1244. {
  1245. int i = -12345;
  1246. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), util::itos(i, true));
  1247. }
  1248. {
  1249. int64_t i = INT64_MAX;
  1250. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"),
  1251. util::itos(i, true));
  1252. }
  1253. {
  1254. int64_t i = INT64_MIN;
  1255. CPPUNIT_ASSERT_EQUAL(std::string("-9,223,372,036,854,775,808"),
  1256. util::itos(i, true));
  1257. }
  1258. }
  1259. void UtilTest::testUitos()
  1260. {
  1261. {
  1262. uint16_t i = 12345;
  1263. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::uitos(i));
  1264. }
  1265. {
  1266. int16_t i = -12345;
  1267. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), util::uitos(i));
  1268. }
  1269. }
  1270. void UtilTest::testNtoh64()
  1271. {
  1272. uint64_t x = 0xff00ff00ee00ee00LL;
  1273. #ifdef WORDS_BIGENDIAN
  1274. CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
  1275. CPPUNIT_ASSERT_EQUAL(x, hton64(x));
  1276. #else // !WORDS_BIGENDIAN
  1277. uint64_t y = 0x00ee00ee00ff00ffLL;
  1278. CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
  1279. CPPUNIT_ASSERT_EQUAL(x, hton64(y));
  1280. #endif // !WORDS_BIGENDIAN
  1281. }
  1282. void UtilTest::testPercentEncode()
  1283. {
  1284. CPPUNIT_ASSERT_EQUAL
  1285. (std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
  1286. util::percentEncode(":/?#[]@!%&'()*+,;="));
  1287. std::string unreserved =
  1288. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1289. "abcdefghijklmnopqrstuvwxyz"
  1290. "0123456789"
  1291. "-._~";
  1292. CPPUNIT_ASSERT_EQUAL(unreserved, util::percentEncode(unreserved));
  1293. CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::percentEncode("1^A "));
  1294. }
  1295. void UtilTest::testHtmlEscape()
  1296. {
  1297. CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),
  1298. util::htmlEscape("aria2<>\"'util"));
  1299. }
  1300. void UtilTest::testJoinPath()
  1301. {
  1302. const std::string dir1dir2file[] = { "dir1", "dir2", "file" };
  1303. CPPUNIT_ASSERT_EQUAL
  1304. (std::string("dir1/dir2/file"),
  1305. util::joinPath(vbegin(dir1dir2file), vend(dir1dir2file)));
  1306. const std::string dirparentfile[] = { "dir", "..", "file" };
  1307. CPPUNIT_ASSERT_EQUAL
  1308. (std::string("file"),
  1309. util::joinPath(vbegin(dirparentfile), vend(dirparentfile)));
  1310. const std::string dirparentparentfile[] = { "dir", "..", "..", "file" };
  1311. CPPUNIT_ASSERT_EQUAL
  1312. (std::string("file"),
  1313. util::joinPath(vbegin(dirparentparentfile), vend(dirparentparentfile)));
  1314. const std::string dirdotfile[] = { "dir", ".", "file" };
  1315. CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
  1316. util::joinPath(vbegin(dirdotfile), vend(dirdotfile)));
  1317. const std::string empty[] = {};
  1318. CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(&empty[0], &empty[0]));
  1319. const std::string parentdot[] = { "..", "." };
  1320. CPPUNIT_ASSERT_EQUAL(std::string(""),
  1321. util::joinPath(vbegin(parentdot), vend(parentdot)));
  1322. }
  1323. void UtilTest::testParseIndexPath()
  1324. {
  1325. std::pair<size_t, std::string> p = util::parseIndexPath("1=foo");
  1326. CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
  1327. CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
  1328. try {
  1329. util::parseIndexPath("1X=foo");
  1330. CPPUNIT_FAIL("exception must be thrown.");
  1331. } catch(Exception& e) {
  1332. // success
  1333. }
  1334. try {
  1335. util::parseIndexPath("1=");
  1336. CPPUNIT_FAIL("exception must be thrown.");
  1337. } catch(Exception& e) {
  1338. // success
  1339. }
  1340. }
  1341. void UtilTest::testCreateIndexPaths()
  1342. {
  1343. std::stringstream in
  1344. ("1=/tmp/myfile\n"
  1345. "100=/myhome/mypicture.png\n");
  1346. std::vector<std::pair<size_t, std::string> > m = util::createIndexPaths(in);
  1347. CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
  1348. CPPUNIT_ASSERT_EQUAL((size_t)1, m[0].first);
  1349. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[0].second);
  1350. CPPUNIT_ASSERT_EQUAL((size_t)100, m[1].first);
  1351. CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[1].second);
  1352. }
  1353. void UtilTest::testGenerateRandomData()
  1354. {
  1355. unsigned char data1[20];
  1356. util::generateRandomData(data1, sizeof(data1));
  1357. unsigned char data2[20];
  1358. util::generateRandomData(data2, sizeof(data2));
  1359. CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
  1360. }
  1361. void UtilTest::testFromHex()
  1362. {
  1363. std::string src;
  1364. std::string dest;
  1365. src = "0011fF";
  1366. dest = util::fromHex(src.begin(), src.end());
  1367. CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
  1368. CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
  1369. CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
  1370. CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
  1371. src = "0011f";
  1372. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  1373. src = "001g";
  1374. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  1375. }
  1376. void UtilTest::testParsePrioritizePieceRange()
  1377. {
  1378. // piece index
  1379. // 0 1 2 3 4 5 6 7
  1380. // | | | |
  1381. // file1 | | |
  1382. // | | |
  1383. // file2 | |
  1384. // file3 |
  1385. // | |
  1386. // file4 |
  1387. size_t pieceLength = 1024;
  1388. std::vector<SharedHandle<FileEntry> > entries(4, SharedHandle<FileEntry>());
  1389. entries[0].reset(new FileEntry("file1", 1024, 0));
  1390. entries[1].reset(new FileEntry("file2",2560,entries[0]->getLastOffset()));
  1391. entries[2].reset(new FileEntry("file3",0,entries[1]->getLastOffset()));
  1392. entries[3].reset(new FileEntry("file4",3584,entries[2]->getLastOffset()));
  1393. std::vector<size_t> result;
  1394. util::parsePrioritizePieceRange(result, "head=1", entries, pieceLength);
  1395. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1396. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1397. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1398. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1399. result.clear();
  1400. util::parsePrioritizePieceRange(result, "tail=1", entries, pieceLength);
  1401. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1402. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1403. CPPUNIT_ASSERT_EQUAL((size_t)3, result[1]);
  1404. CPPUNIT_ASSERT_EQUAL((size_t)6, result[2]);
  1405. result.clear();
  1406. util::parsePrioritizePieceRange(result, "head=1K", entries, pieceLength);
  1407. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1408. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1409. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1410. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1411. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1412. result.clear();
  1413. util::parsePrioritizePieceRange(result, "head", entries, pieceLength, 1024);
  1414. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1415. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1416. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1417. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1418. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1419. result.clear();
  1420. util::parsePrioritizePieceRange(result, "tail=1K", entries, pieceLength);
  1421. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1422. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1423. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1424. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1425. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1426. result.clear();
  1427. util::parsePrioritizePieceRange(result, "tail", entries, pieceLength, 1024);
  1428. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1429. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1430. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1431. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1432. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1433. result.clear();
  1434. util::parsePrioritizePieceRange(result, "head=1,tail=1", entries, pieceLength);
  1435. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1436. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1437. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1438. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1439. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1440. result.clear();
  1441. util::parsePrioritizePieceRange(result, "head=300M,tail=300M",
  1442. entries, pieceLength);
  1443. CPPUNIT_ASSERT_EQUAL((size_t)7, result.size());
  1444. for(size_t i = 0; i < 7; ++i) {
  1445. CPPUNIT_ASSERT_EQUAL(i, result[i]);
  1446. }
  1447. result.clear();
  1448. util::parsePrioritizePieceRange(result, "", entries, pieceLength);
  1449. CPPUNIT_ASSERT(result.empty());
  1450. }
  1451. void UtilTest::testApplyDir()
  1452. {
  1453. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir("", "pred"));
  1454. CPPUNIT_ASSERT_EQUAL(std::string("/pred"), util::applyDir("/", "pred"));
  1455. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir(".", "pred"));
  1456. CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred"));
  1457. }
  1458. void UtilTest::testFixTaintedBasename()
  1459. {
  1460. CPPUNIT_ASSERT_EQUAL(std::string("a%2Fb"), util::fixTaintedBasename("a/b"));
  1461. #ifdef __MINGW32__
  1462. CPPUNIT_ASSERT_EQUAL(std::string("a%5Cb"), util::fixTaintedBasename("a\\b"));
  1463. #else // !__MINGW32__
  1464. CPPUNIT_ASSERT_EQUAL(std::string("a\\b"), util::fixTaintedBasename("a\\b"));
  1465. #endif // !__MINGW32__
  1466. }
  1467. void UtilTest::testIsNumericHost()
  1468. {
  1469. CPPUNIT_ASSERT(util::isNumericHost("192.168.0.1"));
  1470. CPPUNIT_ASSERT(!util::isNumericHost("aria2.sf.net"));
  1471. CPPUNIT_ASSERT(util::isNumericHost("::1"));
  1472. }
  1473. void UtilTest::testDetectDirTraversal()
  1474. {
  1475. CPPUNIT_ASSERT(util::detectDirTraversal("/foo"));
  1476. CPPUNIT_ASSERT(util::detectDirTraversal("./foo"));
  1477. CPPUNIT_ASSERT(util::detectDirTraversal("../foo"));
  1478. CPPUNIT_ASSERT(util::detectDirTraversal("foo/../bar"));
  1479. CPPUNIT_ASSERT(util::detectDirTraversal("foo/./bar"));
  1480. CPPUNIT_ASSERT(util::detectDirTraversal("foo/."));
  1481. CPPUNIT_ASSERT(util::detectDirTraversal("foo/.."));
  1482. CPPUNIT_ASSERT(util::detectDirTraversal("."));
  1483. CPPUNIT_ASSERT(util::detectDirTraversal(".."));
  1484. CPPUNIT_ASSERT(util::detectDirTraversal("/"));
  1485. CPPUNIT_ASSERT(util::detectDirTraversal("foo/"));
  1486. CPPUNIT_ASSERT(util::detectDirTraversal("\t"));
  1487. CPPUNIT_ASSERT(!util::detectDirTraversal("foo/bar"));
  1488. CPPUNIT_ASSERT(!util::detectDirTraversal("foo"));
  1489. }
  1490. void UtilTest::testEscapePath()
  1491. {
  1492. CPPUNIT_ASSERT_EQUAL(std::string("foo%00bar%00%01"),
  1493. util::escapePath(std::string("foo")+(char)0x00+
  1494. std::string("bar")+(char)0x00+
  1495. (char)0x01));
  1496. #ifdef __MINGW32__
  1497. CPPUNIT_ASSERT_EQUAL(std::string("foo%5Cbar"), util::escapePath("foo\\bar"));
  1498. #else // !__MINGW32__
  1499. CPPUNIT_ASSERT_EQUAL(std::string("foo\\bar"), util::escapePath("foo\\bar"));
  1500. #endif // !__MINGW32__
  1501. }
  1502. void UtilTest::testInSameCidrBlock()
  1503. {
  1504. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 16));
  1505. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 17));
  1506. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "192.168.0.1", 32));
  1507. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.0.1", "192.168.0.0", 32));
  1508. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "10.0.0.1", 0));
  1509. CPPUNIT_ASSERT(util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 28));
  1510. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 29));
  1511. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "192.168.0.1", 8));
  1512. }
  1513. void UtilTest::testIsUtf8String()
  1514. {
  1515. CPPUNIT_ASSERT(util::isUtf8("ascii"));
  1516. // "Hello World" in Japanese UTF-8
  1517. CPPUNIT_ASSERT(util::isUtf8
  1518. (fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
  1519. // "World" in Shift_JIS
  1520. CPPUNIT_ASSERT(!util::isUtf8(fromHex("90a28a")+"E"));
  1521. // UTF8-2
  1522. CPPUNIT_ASSERT(util::isUtf8(fromHex("c280")));
  1523. CPPUNIT_ASSERT(util::isUtf8(fromHex("dfbf")));
  1524. // UTF8-3
  1525. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0a080")));
  1526. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0bf80")));
  1527. CPPUNIT_ASSERT(util::isUtf8(fromHex("e18080")));
  1528. CPPUNIT_ASSERT(util::isUtf8(fromHex("ec8080")));
  1529. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed8080")));
  1530. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed9f80")));
  1531. CPPUNIT_ASSERT(util::isUtf8(fromHex("ee8080")));
  1532. CPPUNIT_ASSERT(util::isUtf8(fromHex("ef8080")));
  1533. // UTF8-4
  1534. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0908080")));
  1535. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0bf8080")));
  1536. CPPUNIT_ASSERT(util::isUtf8(fromHex("f1808080")));
  1537. CPPUNIT_ASSERT(util::isUtf8(fromHex("f3808080")));
  1538. CPPUNIT_ASSERT(util::isUtf8(fromHex("f4808080")));
  1539. CPPUNIT_ASSERT(util::isUtf8(fromHex("f48f8080")));
  1540. CPPUNIT_ASSERT(util::isUtf8(""));
  1541. CPPUNIT_ASSERT(!util::isUtf8(fromHex("00")));
  1542. }
  1543. void UtilTest::testNextParam()
  1544. {
  1545. std::string s1 = " :a : b=c :d=b::::g::";
  1546. std::pair<std::string::iterator, bool> r;
  1547. std::string name, value;
  1548. r = util::nextParam(name, value, s1.begin(), s1.end(), ':');
  1549. CPPUNIT_ASSERT(r.second);
  1550. CPPUNIT_ASSERT_EQUAL(std::string("a"), name);
  1551. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1552. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1553. CPPUNIT_ASSERT(r.second);
  1554. CPPUNIT_ASSERT_EQUAL(std::string("b"), name);
  1555. CPPUNIT_ASSERT_EQUAL(std::string("c"), value);
  1556. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1557. CPPUNIT_ASSERT(r.second);
  1558. CPPUNIT_ASSERT_EQUAL(std::string("d"), name);
  1559. CPPUNIT_ASSERT_EQUAL(std::string("b"), value);
  1560. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1561. CPPUNIT_ASSERT(r.second);
  1562. CPPUNIT_ASSERT_EQUAL(std::string("g"), name);
  1563. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1564. std::string s2 = "";
  1565. r = util::nextParam(name, value, s2.begin(), s2.end(), ':');
  1566. CPPUNIT_ASSERT(!r.second);
  1567. std::string s3 = " ";
  1568. r = util::nextParam(name, value, s3.begin(), s3.end(), ':');
  1569. CPPUNIT_ASSERT(!r.second);
  1570. std::string s4 = ":::";
  1571. r = util::nextParam(name, value, s4.begin(), s4.end(), ':');
  1572. CPPUNIT_ASSERT(!r.second);
  1573. }
  1574. void UtilTest::testNoProxyDomainMatch()
  1575. {
  1576. CPPUNIT_ASSERT(util::noProxyDomainMatch("localhost", "localhost"));
  1577. CPPUNIT_ASSERT(util::noProxyDomainMatch("192.168.0.1", "192.168.0.1"));
  1578. CPPUNIT_ASSERT(util::noProxyDomainMatch("www.example.org", ".example.org"));
  1579. CPPUNIT_ASSERT(!util::noProxyDomainMatch("www.example.org", "example.org"));
  1580. CPPUNIT_ASSERT(!util::noProxyDomainMatch("192.168.0.1", "0.1"));
  1581. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "example.com"));
  1582. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org"));
  1583. }
  1584. void UtilTest::testInPrivateAddress()
  1585. {
  1586. CPPUNIT_ASSERT(!util::inPrivateAddress("localhost"));
  1587. CPPUNIT_ASSERT(util::inPrivateAddress("192.168.0.1"));
  1588. // Only checks prefix..
  1589. CPPUNIT_ASSERT(util::inPrivateAddress("10."));
  1590. CPPUNIT_ASSERT(!util::inPrivateAddress("172."));
  1591. CPPUNIT_ASSERT(!util::inPrivateAddress("172.15.0.0"));
  1592. CPPUNIT_ASSERT(util::inPrivateAddress("172.16.0.0"));
  1593. CPPUNIT_ASSERT(util::inPrivateAddress("172.31.0.0"));
  1594. CPPUNIT_ASSERT(!util::inPrivateAddress("172.32.0.0"));
  1595. }
  1596. void UtilTest::testSecfmt()
  1597. {
  1598. CPPUNIT_ASSERT_EQUAL(std::string("0s"), util::secfmt(0));
  1599. CPPUNIT_ASSERT_EQUAL(std::string("1s"), util::secfmt(1));
  1600. CPPUNIT_ASSERT_EQUAL(std::string("9s"), util::secfmt(9));
  1601. CPPUNIT_ASSERT_EQUAL(std::string("10s"), util::secfmt(10));
  1602. CPPUNIT_ASSERT_EQUAL(std::string("1m"), util::secfmt(60));
  1603. CPPUNIT_ASSERT_EQUAL(std::string("1m59s"), util::secfmt(119));
  1604. CPPUNIT_ASSERT_EQUAL(std::string("2m"), util::secfmt(120));
  1605. CPPUNIT_ASSERT_EQUAL(std::string("59m59s"), util::secfmt(3599));
  1606. CPPUNIT_ASSERT_EQUAL(std::string("1h"), util::secfmt(3600));
  1607. }
  1608. } // namespace aria2