UtilTest.cc 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  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. const char d[] = ";";
  442. const char md[] = "; ";
  443. std::vector<Scip> v;
  444. std::string s = "k1; k2;; k3";
  445. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true);
  446. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  447. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  448. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  449. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  450. v.clear();
  451. s = "k1; k2; k3";
  452. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  453. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  454. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  455. CPPUNIT_ASSERT_EQUAL(std::string(" k2"),
  456. std::string(v[1].first, v[1].second));
  457. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  458. std::string(v[2].first, v[2].second));
  459. v.clear();
  460. s = "k1; k2; k3";
  461. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), md);
  462. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  463. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  464. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  465. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  466. v.clear();
  467. s = "k1; k2; k3;";
  468. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), md, false, true);
  469. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  470. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  471. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  472. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[2].first, v[2].second));
  473. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  474. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[4].first, v[4].second));
  475. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  476. v.clear();
  477. s = "k=v";
  478. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  479. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  480. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  481. std::string(v[0].first, v[0].second));
  482. v.clear();
  483. s = ";;k1;;k2;";
  484. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  485. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  486. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  487. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  488. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  489. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  490. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  491. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  492. v.clear();
  493. s = ";;k1;;k2;";
  494. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  495. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  496. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  497. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  498. v.clear();
  499. s = "k; ";
  500. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  501. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  502. CPPUNIT_ASSERT_EQUAL(std::string("k"), std::string(v[0].first, v[0].second));
  503. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  504. v.clear();
  505. s = " ";
  506. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true, true);
  507. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  508. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  509. v.clear();
  510. s = " ";
  511. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true);
  512. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  513. v.clear();
  514. s = " ";
  515. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  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), d);
  521. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  522. v.clear();
  523. s = ";";
  524. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  525. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  526. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  527. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  528. v.clear();
  529. s = "";
  530. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  531. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  532. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  533. }
  534. void UtilTest::testEndsWith() {
  535. std::string target = "abcdefg";
  536. std::string part = "fg";
  537. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  538. part.begin(), part.end()));
  539. target = "abdefg";
  540. part = "g";
  541. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  542. part.begin(), part.end()));
  543. target = "abdefg";
  544. part = "eg";
  545. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  546. part.begin(), part.end()));
  547. target = "g";
  548. part = "eg";
  549. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  550. part.begin(), part.end()));
  551. target = "g";
  552. part = "g";
  553. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  554. part.begin(), part.end()));
  555. target = "g";
  556. part = "";
  557. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  558. part.begin(), part.end()));
  559. target = "";
  560. part = "";
  561. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  562. part.begin(), part.end()));
  563. target = "";
  564. part = "g";
  565. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  566. part.begin(), part.end()));
  567. }
  568. void UtilTest::testIendsWith() {
  569. std::string target = "abcdefg";
  570. std::string part = "Fg";
  571. CPPUNIT_ASSERT(util::iendsWith(target.begin(), target.end(),
  572. part.begin(), part.end()));
  573. target = "abdefg";
  574. part = "ef";
  575. CPPUNIT_ASSERT(!util::iendsWith(target.begin(), target.end(),
  576. part.begin(), part.end()));
  577. }
  578. void UtilTest::testStreq()
  579. {
  580. std::string s1, s2;
  581. s1 = "foo";
  582. s2 = "foo";
  583. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  584. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.c_str()));
  585. s2 = "fooo";
  586. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  587. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  588. s2 = "fo";
  589. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  590. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  591. s2 = "";
  592. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  593. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  594. s1 = "";
  595. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  596. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.c_str()));
  597. }
  598. void UtilTest::testStrieq()
  599. {
  600. std::string s1, s2;
  601. s1 = "foo";
  602. s2 = "foo";
  603. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  604. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  605. s1 = "FoO";
  606. s2 = "fOo";
  607. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  608. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  609. s2 = "fooo";
  610. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  611. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  612. s2 = "fo";
  613. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  614. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  615. s2 = "";
  616. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  617. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  618. s1 = "";
  619. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  620. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  621. }
  622. void UtilTest::testStrifind()
  623. {
  624. std::string s1, s2;
  625. s1 = "yamagakani mukashi wo toheba hARU no tuki";
  626. s2 = "HaRu";
  627. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  628. != s1.end());
  629. s2 = "aki";
  630. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  631. == s1.end());
  632. s1 = "h";
  633. s2 = "HH";
  634. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  635. == s1.end());
  636. }
  637. void UtilTest::testReplace() {
  638. CPPUNIT_ASSERT_EQUAL(std::string("abc\n"), util::replace("abc\r\n", "\r", ""));
  639. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc\r\n", "\r\n", ""));
  640. CPPUNIT_ASSERT_EQUAL(std::string(""), util::replace("", "\r\n", ""));
  641. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc", "", "a"));
  642. CPPUNIT_ASSERT_EQUAL(std::string("xbc"), util::replace("abc", "a", "x"));
  643. }
  644. void UtilTest::testStartsWith() {
  645. std::string target;
  646. std::string part;
  647. target = "abcdefg";
  648. part = "abc";
  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 = "abx";
  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 = "abcdefg";
  658. part = "bcd";
  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 = "a";
  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 = "";
  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 = "";
  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. target = "a";
  678. part = "a";
  679. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  680. part.begin(), part.end()));
  681. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  682. }
  683. void UtilTest::testIstartsWith() {
  684. std::string target;
  685. std::string part;
  686. target = "abcdefg";
  687. part = "aBc";
  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. target = "abcdefg";
  692. part = "abx";
  693. CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(),
  694. part.begin(), part.end()));
  695. CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(), part.c_str()));
  696. }
  697. void UtilTest::testGetContentDispositionFilename() {
  698. std::string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  699. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h1));
  700. std::string h2 = "attachment; filename=\"\"";
  701. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h2));
  702. std::string h3 = "attachment; filename=\"";
  703. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h3));
  704. std::string h3_2 = "attachment; filename= \" aria2.tar.bz2 \"";
  705. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  706. util::getContentDispositionFilename(h3_2));
  707. std::string h4 = "attachment;";
  708. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h4));
  709. std::string h5 = "attachment; filename=aria2.tar.bz2";
  710. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h5));
  711. std::string h6 = "attachment; filename='aria2.tar.bz2'";
  712. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h6));
  713. std::string h7 = "attachment; filename='aria2.tar.bz2";
  714. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h7));
  715. std::string h8 = "attachment; filename=aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT";
  716. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h8));
  717. std::string h9 = "attachment; filename=\"aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT\"";
  718. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT"),
  719. util::getContentDispositionFilename(h9));
  720. std::string h10 = "attachment; filename=";
  721. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h10));
  722. std::string h11 = "attachment; filename=;";
  723. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h11));
  724. std::string filenameWithDir = "attachment; filename=dir/file";
  725. CPPUNIT_ASSERT_EQUAL(std::string(""),
  726. util::getContentDispositionFilename(filenameWithDir));
  727. std::string semicolonInside = "attachment; filename=\"foo;bar\"";
  728. CPPUNIT_ASSERT_EQUAL(std::string("foo;bar"),
  729. util::getContentDispositionFilename(semicolonInside));
  730. CPPUNIT_ASSERT_EQUAL
  731. (std::string(""),
  732. util::getContentDispositionFilename("filename=\"%2E%2E%2Ffoo.html\""));
  733. // RFC2231 Section4
  734. std::string extparam2 = "filename*=''aria2";
  735. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  736. util::getContentDispositionFilename(extparam2));
  737. std::string extparam3 = "filename*='''";
  738. CPPUNIT_ASSERT_EQUAL(std::string(""),
  739. util::getContentDispositionFilename(extparam3));
  740. std::string extparam4 = "filename*='aria2";
  741. CPPUNIT_ASSERT_EQUAL(std::string(""),
  742. util::getContentDispositionFilename(extparam4));
  743. std::string extparam5 = "filename*='''aria2";
  744. CPPUNIT_ASSERT_EQUAL(std::string(""),
  745. util::getContentDispositionFilename(extparam5));
  746. std::string extparam6 = "filename*";
  747. CPPUNIT_ASSERT_EQUAL(std::string(""),
  748. util::getContentDispositionFilename(extparam6));
  749. std::string extparam7 = "filename*=UTF-8''aria2;filename=hello%20world";
  750. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  751. util::getContentDispositionFilename(extparam7));
  752. std::string extparam8 = "filename=aria2;filename*=UTF-8''hello%20world";
  753. CPPUNIT_ASSERT_EQUAL(std::string("hello world"),
  754. util::getContentDispositionFilename(extparam8));
  755. std::string extparam9 = "filename*=ISO-8859-1''%A3";
  756. std::string extparam9ans;
  757. extparam9ans += 0xc2;
  758. extparam9ans += 0xa3;
  759. CPPUNIT_ASSERT_EQUAL(extparam9ans,
  760. util::getContentDispositionFilename(extparam9));
  761. CPPUNIT_ASSERT_EQUAL
  762. (std::string(""),
  763. util::getContentDispositionFilename("filename*=UTF-8''foo%2F.html"));
  764. CPPUNIT_ASSERT_EQUAL
  765. (std::string("foo.html"),
  766. util::getContentDispositionFilename("filename*=UTF-8'';filename=\"foo.html\""));
  767. CPPUNIT_ASSERT_EQUAL
  768. (std::string(""),
  769. util::getContentDispositionFilename("filename*=UTF-8''%2E%2E%2Ffoo.html"));
  770. // Tests from http://greenbytes.de/tech/tc2231/
  771. // attwithasciifnescapedchar
  772. CPPUNIT_ASSERT_EQUAL
  773. (std::string("foo.html"),
  774. util::getContentDispositionFilename("filename=\"f\\oo.html\""));
  775. // attwithasciifilenameucase
  776. CPPUNIT_ASSERT_EQUAL
  777. (std::string("foo.html"),
  778. util::getContentDispositionFilename("FILENAME=\"foo.html\""));
  779. // attwithisofn2231iso
  780. CPPUNIT_ASSERT_EQUAL
  781. (std::string("foo-ä.html"),
  782. util::getContentDispositionFilename("filename*=iso-8859-1''foo-%E4.html"));
  783. // attwithfn2231utf8
  784. CPPUNIT_ASSERT_EQUAL
  785. (std::string("foo-ä-€.html"),
  786. util::getContentDispositionFilename
  787. ("filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html"));
  788. // attwithfn2231utf8-bad
  789. CPPUNIT_ASSERT_EQUAL
  790. (std::string(""),
  791. util::getContentDispositionFilename
  792. ("filename*=iso-8859-1''foo-%c3%a4-%e2%82%ac.html"));
  793. // attwithfn2231ws1
  794. CPPUNIT_ASSERT_EQUAL
  795. (std::string(""),
  796. util::getContentDispositionFilename("filename *=UTF-8''foo-%c3%a4.html"));
  797. // attwithfn2231ws2
  798. CPPUNIT_ASSERT_EQUAL
  799. (std::string("foo-ä.html"),
  800. util::getContentDispositionFilename("filename*= UTF-8''foo-%c3%a4.html"));
  801. // attwithfn2231ws3
  802. CPPUNIT_ASSERT_EQUAL
  803. (std::string("foo-ä.html"),
  804. util::getContentDispositionFilename("filename* =UTF-8''foo-%c3%a4.html"));
  805. // attwithfn2231quot
  806. CPPUNIT_ASSERT_EQUAL
  807. (std::string(""),
  808. util::getContentDispositionFilename
  809. ("filename*=\"UTF-8''foo-%c3%a4.html\""));
  810. }
  811. class Printer {
  812. public:
  813. template<class T>
  814. void operator()(T t) {
  815. std::cerr << t << ", ";
  816. }
  817. };
  818. void UtilTest::testRandomAlpha() {
  819. SharedHandle<Randomizer> rand(new FixedNumberRandomizer());
  820. std::string s = util::randomAlpha(8, rand);
  821. CPPUNIT_ASSERT_EQUAL(std::string("AAAAAAAA"), s);
  822. }
  823. void UtilTest::testToUpper() {
  824. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  825. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  826. CPPUNIT_ASSERT_EQUAL(upp, util::toUpper(src));
  827. }
  828. void UtilTest::testToLower() {
  829. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  830. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  831. CPPUNIT_ASSERT_EQUAL(upp, util::toLower(src));
  832. }
  833. void UtilTest::testUppercase() {
  834. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  835. std::string ans = "608CABC0F2FA18C260CAFD974516865C772363D5";
  836. util::uppercase(src);
  837. CPPUNIT_ASSERT_EQUAL(ans, src);
  838. }
  839. void UtilTest::testLowercase() {
  840. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  841. std::string ans = "608cabc0f2fa18c260cafd974516865c772363d5";
  842. util::lowercase(src);
  843. CPPUNIT_ASSERT_EQUAL(ans, src);
  844. }
  845. void UtilTest::testPercentDecode() {
  846. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  847. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  848. util::percentDecode(src.begin(), src.end()));
  849. std::string src2 = "aria2+aria2";
  850. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"),
  851. util::percentDecode(src2.begin(), src2.end()));
  852. std::string src3 = "%5t%20";
  853. CPPUNIT_ASSERT_EQUAL(std::string("%5t "),
  854. util::percentDecode(src3.begin(), src3.end()));
  855. std::string src4 = "%";
  856. CPPUNIT_ASSERT_EQUAL(std::string("%"),
  857. util::percentDecode(src4.begin(), src4.end()));
  858. std::string src5 = "%3";
  859. CPPUNIT_ASSERT_EQUAL(std::string("%3"),
  860. util::percentDecode(src5.begin(), src5.end()));
  861. std::string src6 = "%2f";
  862. CPPUNIT_ASSERT_EQUAL(std::string("/"),
  863. util::percentDecode(src6.begin(), src6.end()));
  864. }
  865. void UtilTest::testGetRealSize()
  866. {
  867. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096M"));
  868. CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1K"));
  869. try {
  870. util::getRealSize("");
  871. CPPUNIT_FAIL("exception must be thrown.");
  872. } catch(Exception& e) {
  873. std::cerr << e.stackTrace();
  874. }
  875. try {
  876. util::getRealSize("foo");
  877. CPPUNIT_FAIL("exception must be thrown.");
  878. } catch(Exception& e) {
  879. std::cerr << e.stackTrace();
  880. }
  881. try {
  882. util::getRealSize("-1");
  883. CPPUNIT_FAIL("exception must be thrown.");
  884. } catch(Exception& e) {
  885. std::cerr << e.stackTrace();
  886. }
  887. try {
  888. util::getRealSize("9223372036854775807K");
  889. CPPUNIT_FAIL("exception must be thrown.");
  890. } catch(Exception& e) {
  891. std::cerr << e.stackTrace();
  892. }
  893. try {
  894. util::getRealSize("9223372036854775807M");
  895. CPPUNIT_FAIL("exception must be thrown.");
  896. } catch(Exception& e) {
  897. std::cerr << e.stackTrace();
  898. }
  899. }
  900. void UtilTest::testAbbrevSize()
  901. {
  902. CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), util::abbrevSize(4294967296LL));
  903. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1024));
  904. CPPUNIT_ASSERT_EQUAL(std::string("1,023"), util::abbrevSize(1023));
  905. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::abbrevSize(0));
  906. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), util::abbrevSize(1127));
  907. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), util::abbrevSize(1572864));
  908. }
  909. void UtilTest::testToStream()
  910. {
  911. std::ostringstream os;
  912. SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  913. SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  914. std::deque<SharedHandle<FileEntry> > entries;
  915. entries.push_back(f1);
  916. entries.push_back(f2);
  917. std::string filename = A2_TEST_OUT_DIR"/aria2_UtilTest_testToStream";
  918. BufferedFile fp(filename, BufferedFile::WRITE);
  919. util::toStream(entries.begin(), entries.end(), fp);
  920. fp.close();
  921. CPPUNIT_ASSERT_EQUAL(
  922. std::string("Files:\n"
  923. "idx|path/length\n"
  924. "===+===========================================================================\n"
  925. " 1|aria2.tar.bz2\n"
  926. " |12.0KiB (12,300)\n"
  927. "---+---------------------------------------------------------------------------\n"
  928. " 2|aria2.txt\n"
  929. " |556B (556)\n"
  930. "---+---------------------------------------------------------------------------\n"),
  931. readFile(filename));
  932. }
  933. void UtilTest::testIsNumber()
  934. {
  935. std::string s = "000";
  936. CPPUNIT_ASSERT_EQUAL(true, util::isNumber(s.begin(),s.end()));
  937. s = "a";
  938. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  939. s = "0a";
  940. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  941. s = "";
  942. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  943. s = " ";
  944. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  945. }
  946. void UtilTest::testIsLowercase()
  947. {
  948. std::string s = "alpha";
  949. CPPUNIT_ASSERT_EQUAL(true, util::isLowercase(s.begin(), s.end()));
  950. s = "Alpha";
  951. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  952. s = "1alpha";
  953. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  954. s = "";
  955. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  956. s = " ";
  957. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  958. }
  959. void UtilTest::testIsUppercase()
  960. {
  961. std::string s = "ALPHA";
  962. CPPUNIT_ASSERT_EQUAL(true, util::isUppercase(s.begin(), s.end()));
  963. s = "Alpha";
  964. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  965. s = "1ALPHA";
  966. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  967. s = "";
  968. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  969. s = " ";
  970. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  971. }
  972. void UtilTest::testAlphaToNum()
  973. {
  974. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("a"));
  975. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("aa"));
  976. CPPUNIT_ASSERT_EQUAL(1U, util::alphaToNum("b"));
  977. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("zz")); // 25*26+25
  978. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("ZZ")); // 25*26+25
  979. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum(""));
  980. CPPUNIT_ASSERT_EQUAL(4294967295U, util::alphaToNum("NXMRLXV"));
  981. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("NXMRLXW")); // uint32_t overflow
  982. }
  983. void UtilTest::testMkdirs()
  984. {
  985. std::string dir = A2_TEST_OUT_DIR"/aria2-UtilTest-testMkdirs";
  986. File d(dir);
  987. if(d.exists()) {
  988. CPPUNIT_ASSERT(d.remove());
  989. }
  990. CPPUNIT_ASSERT(!d.exists());
  991. util::mkdirs(dir);
  992. CPPUNIT_ASSERT(d.isDir());
  993. std::string file = A2_TEST_DIR"/UtilTest.cc";
  994. File f(file);
  995. CPPUNIT_ASSERT(f.isFile());
  996. try {
  997. util::mkdirs(file);
  998. CPPUNIT_FAIL("exception must be thrown.");
  999. } catch(DlAbortEx& ex) {
  1000. std::cerr << ex.stackTrace() << std::endl;
  1001. }
  1002. }
  1003. void UtilTest::testConvertBitfield()
  1004. {
  1005. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  1006. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  1007. srcBitfield.setAllBit();
  1008. srcBitfield.unsetBit(2);// <- range [768, 1152)
  1009. // which corresponds to the index [1,2] in destBitfield
  1010. util::convertBitfield(&destBitfield, &srcBitfield);
  1011. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  1012. util::toHex(destBitfield.getBitfield(),
  1013. destBitfield.getBitfieldLength()));
  1014. }
  1015. void UtilTest::testParseIntSegments()
  1016. {
  1017. SegList<int> sgl;
  1018. util::parseIntSegments(sgl, "1,3-8,10");
  1019. CPPUNIT_ASSERT(sgl.hasNext());
  1020. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  1021. CPPUNIT_ASSERT(sgl.hasNext());
  1022. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  1023. CPPUNIT_ASSERT(sgl.hasNext());
  1024. CPPUNIT_ASSERT_EQUAL(4, sgl.next());
  1025. CPPUNIT_ASSERT(sgl.hasNext());
  1026. CPPUNIT_ASSERT_EQUAL(5, sgl.next());
  1027. CPPUNIT_ASSERT(sgl.hasNext());
  1028. CPPUNIT_ASSERT_EQUAL(6, sgl.next());
  1029. CPPUNIT_ASSERT(sgl.hasNext());
  1030. CPPUNIT_ASSERT_EQUAL(7, sgl.next());
  1031. CPPUNIT_ASSERT(sgl.hasNext());
  1032. CPPUNIT_ASSERT_EQUAL(8, sgl.next());
  1033. CPPUNIT_ASSERT(sgl.hasNext());
  1034. CPPUNIT_ASSERT_EQUAL(10, sgl.next());
  1035. CPPUNIT_ASSERT(!sgl.hasNext());
  1036. CPPUNIT_ASSERT_EQUAL(0, sgl.next());
  1037. sgl.clear();
  1038. util::parseIntSegments(sgl, ",,,1,,,3,,,");
  1039. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  1040. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  1041. CPPUNIT_ASSERT(!sgl.hasNext());
  1042. }
  1043. void UtilTest::testParseIntSegments_invalidRange()
  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, "1-");
  1054. CPPUNIT_FAIL("exception must be thrown.");
  1055. } catch(Exception& e) {
  1056. }
  1057. try {
  1058. SegList<int> sgl;
  1059. util::parseIntSegments(sgl, "2147483648");
  1060. CPPUNIT_FAIL("exception must be thrown.");
  1061. } catch(Exception& e) {
  1062. }
  1063. try {
  1064. SegList<int> sgl;
  1065. util::parseIntSegments(sgl, "2147483647-2147483648");
  1066. CPPUNIT_FAIL("exception must be thrown.");
  1067. } catch(Exception& e) {
  1068. }
  1069. try {
  1070. SegList<int> sgl;
  1071. util::parseIntSegments(sgl, "1-2x");
  1072. CPPUNIT_FAIL("exception must be thrown.");
  1073. } catch(Exception& e) {
  1074. }
  1075. try {
  1076. SegList<int> sgl;
  1077. util::parseIntSegments(sgl, "3x-4");
  1078. CPPUNIT_FAIL("exception must be thrown.");
  1079. } catch(Exception& e) {
  1080. }
  1081. }
  1082. void UtilTest::testParseInt()
  1083. {
  1084. std::string s;
  1085. s = " -1 ";
  1086. CPPUNIT_ASSERT_EQUAL(-1, util::parseInt(s));
  1087. s = "2147483647";
  1088. CPPUNIT_ASSERT_EQUAL(2147483647, util::parseInt(s));
  1089. try {
  1090. s = "2147483648";
  1091. util::parseInt(s);
  1092. CPPUNIT_FAIL("exception must be thrown.");
  1093. } catch(Exception& e) {
  1094. }
  1095. try {
  1096. s = "-2147483649";
  1097. util::parseInt(s);
  1098. CPPUNIT_FAIL("exception must be thrown.");
  1099. } catch(Exception& e) {
  1100. }
  1101. try {
  1102. s = "12x";
  1103. util::parseInt(s);
  1104. CPPUNIT_FAIL("exception must be thrown.");
  1105. } catch(Exception& e) {
  1106. }
  1107. try {
  1108. s = "";
  1109. util::parseInt(s);
  1110. CPPUNIT_FAIL("exception must be thrown.");
  1111. } catch(Exception& e) {
  1112. }
  1113. }
  1114. void UtilTest::testParseUInt()
  1115. {
  1116. std::string s;
  1117. s = " 2147483647 ";
  1118. CPPUNIT_ASSERT_EQUAL(2147483647U, util::parseUInt(s));
  1119. try {
  1120. s = "-1";
  1121. util::parseUInt(s);
  1122. CPPUNIT_FAIL("exception must be thrown.");
  1123. } catch(Exception& e) {
  1124. }
  1125. try {
  1126. s = "2147483648";
  1127. util::parseUInt(s);
  1128. CPPUNIT_FAIL("exception must be thrown.");
  1129. } catch(Exception& e) {
  1130. }
  1131. }
  1132. void UtilTest::testParseLLInt()
  1133. {
  1134. std::string s;
  1135. {
  1136. s = " -1 ";
  1137. CPPUNIT_ASSERT_EQUAL((int64_t)-1LL, util::parseLLInt(s));
  1138. }
  1139. {
  1140. s = "9223372036854775807";
  1141. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
  1142. util::parseLLInt(s));
  1143. }
  1144. try {
  1145. s = "9223372036854775808";
  1146. util::parseLLInt(s);
  1147. CPPUNIT_FAIL("exception must be thrown.");
  1148. } catch(Exception& e) {
  1149. std::cerr << e.stackTrace();
  1150. }
  1151. try {
  1152. s = "-9223372036854775809";
  1153. util::parseLLInt(s);
  1154. CPPUNIT_FAIL("exception must be thrown.");
  1155. } catch(Exception& e) {
  1156. std::cerr << e.stackTrace();
  1157. }
  1158. try {
  1159. s = "12x";
  1160. util::parseLLInt(s);
  1161. CPPUNIT_FAIL("exception must be thrown.");
  1162. } catch(Exception& e) {
  1163. std::cerr << e.stackTrace();
  1164. }
  1165. try {
  1166. s = "";
  1167. util::parseLLInt(s);
  1168. CPPUNIT_FAIL("exception must be thrown.");
  1169. } catch(Exception& e) {
  1170. std::cerr << e.stackTrace();
  1171. }
  1172. }
  1173. void UtilTest::testParseIntNoThrow()
  1174. {
  1175. std::string s;
  1176. int32_t n;
  1177. s = " -1 ";
  1178. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s));
  1179. CPPUNIT_ASSERT_EQUAL((int32_t)-1, n);
  1180. s = "2147483647";
  1181. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s));
  1182. CPPUNIT_ASSERT_EQUAL((int32_t)2147483647, n);
  1183. s = "2147483648";
  1184. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1185. s = "-2147483649";
  1186. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1187. s = "12x";
  1188. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1189. s = "";
  1190. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1191. }
  1192. void UtilTest::testParseUIntNoThrow()
  1193. {
  1194. std::string s;
  1195. uint32_t n;
  1196. s = " 2147483647 ";
  1197. CPPUNIT_ASSERT(util::parseUIntNoThrow(n, s));
  1198. CPPUNIT_ASSERT_EQUAL((uint32_t)INT32_MAX, n);
  1199. s = "2147483648";
  1200. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s));
  1201. s = "-1";
  1202. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s));
  1203. }
  1204. void UtilTest::testParseLLIntNoThrow()
  1205. {
  1206. std::string s;
  1207. int64_t n;
  1208. s = " 9223372036854775807 ";
  1209. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s));
  1210. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MAX, n);
  1211. s = "9223372036854775808";
  1212. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s));
  1213. s = "-9223372036854775808";
  1214. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s));
  1215. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MIN, n);
  1216. s = "-9223372036854775809";
  1217. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s));
  1218. }
  1219. void UtilTest::testToString_binaryStream()
  1220. {
  1221. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  1222. std::string data(16*1024+256, 'a');
  1223. dw->initAndOpenFile();
  1224. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  1225. std::string readData = util::toString(dw);
  1226. CPPUNIT_ASSERT_EQUAL(data, readData);
  1227. }
  1228. void UtilTest::testItos()
  1229. {
  1230. {
  1231. int i = 0;
  1232. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::itos(i));
  1233. }
  1234. {
  1235. int i = 100;
  1236. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i, true));
  1237. }
  1238. {
  1239. int i = 100;
  1240. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i));
  1241. }
  1242. {
  1243. int i = 12345;
  1244. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), util::itos(i, true));
  1245. }
  1246. {
  1247. int i = 12345;
  1248. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::itos(i));
  1249. }
  1250. {
  1251. int i = -12345;
  1252. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), util::itos(i, true));
  1253. }
  1254. {
  1255. int64_t i = INT64_MAX;
  1256. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"),
  1257. util::itos(i, true));
  1258. }
  1259. {
  1260. int64_t i = INT64_MIN;
  1261. CPPUNIT_ASSERT_EQUAL(std::string("-9,223,372,036,854,775,808"),
  1262. util::itos(i, true));
  1263. }
  1264. }
  1265. void UtilTest::testUitos()
  1266. {
  1267. {
  1268. uint16_t i = 12345;
  1269. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::uitos(i));
  1270. }
  1271. {
  1272. int16_t i = -12345;
  1273. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), util::uitos(i));
  1274. }
  1275. }
  1276. void UtilTest::testNtoh64()
  1277. {
  1278. uint64_t x = 0xff00ff00ee00ee00LL;
  1279. #ifdef WORDS_BIGENDIAN
  1280. CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
  1281. CPPUNIT_ASSERT_EQUAL(x, hton64(x));
  1282. #else // !WORDS_BIGENDIAN
  1283. uint64_t y = 0x00ee00ee00ff00ffLL;
  1284. CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
  1285. CPPUNIT_ASSERT_EQUAL(x, hton64(y));
  1286. #endif // !WORDS_BIGENDIAN
  1287. }
  1288. void UtilTest::testPercentEncode()
  1289. {
  1290. CPPUNIT_ASSERT_EQUAL
  1291. (std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
  1292. util::percentEncode(":/?#[]@!%&'()*+,;="));
  1293. std::string unreserved =
  1294. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1295. "abcdefghijklmnopqrstuvwxyz"
  1296. "0123456789"
  1297. "-._~";
  1298. CPPUNIT_ASSERT_EQUAL(unreserved, util::percentEncode(unreserved));
  1299. CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::percentEncode("1^A "));
  1300. }
  1301. void UtilTest::testHtmlEscape()
  1302. {
  1303. CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),
  1304. util::htmlEscape("aria2<>\"'util"));
  1305. }
  1306. void UtilTest::testJoinPath()
  1307. {
  1308. const std::string dir1dir2file[] = { "dir1", "dir2", "file" };
  1309. CPPUNIT_ASSERT_EQUAL
  1310. (std::string("dir1/dir2/file"),
  1311. util::joinPath(vbegin(dir1dir2file), vend(dir1dir2file)));
  1312. const std::string dirparentfile[] = { "dir", "..", "file" };
  1313. CPPUNIT_ASSERT_EQUAL
  1314. (std::string("file"),
  1315. util::joinPath(vbegin(dirparentfile), vend(dirparentfile)));
  1316. const std::string dirparentparentfile[] = { "dir", "..", "..", "file" };
  1317. CPPUNIT_ASSERT_EQUAL
  1318. (std::string("file"),
  1319. util::joinPath(vbegin(dirparentparentfile), vend(dirparentparentfile)));
  1320. const std::string dirdotfile[] = { "dir", ".", "file" };
  1321. CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
  1322. util::joinPath(vbegin(dirdotfile), vend(dirdotfile)));
  1323. const std::string empty[] = {};
  1324. CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(&empty[0], &empty[0]));
  1325. const std::string parentdot[] = { "..", "." };
  1326. CPPUNIT_ASSERT_EQUAL(std::string(""),
  1327. util::joinPath(vbegin(parentdot), vend(parentdot)));
  1328. }
  1329. void UtilTest::testParseIndexPath()
  1330. {
  1331. std::pair<size_t, std::string> p = util::parseIndexPath("1=foo");
  1332. CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
  1333. CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
  1334. try {
  1335. util::parseIndexPath("1X=foo");
  1336. CPPUNIT_FAIL("exception must be thrown.");
  1337. } catch(Exception& e) {
  1338. // success
  1339. }
  1340. try {
  1341. util::parseIndexPath("1=");
  1342. CPPUNIT_FAIL("exception must be thrown.");
  1343. } catch(Exception& e) {
  1344. // success
  1345. }
  1346. }
  1347. void UtilTest::testCreateIndexPaths()
  1348. {
  1349. std::stringstream in
  1350. ("1=/tmp/myfile\n"
  1351. "100=/myhome/mypicture.png\n");
  1352. std::vector<std::pair<size_t, std::string> > m = util::createIndexPaths(in);
  1353. CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
  1354. CPPUNIT_ASSERT_EQUAL((size_t)1, m[0].first);
  1355. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[0].second);
  1356. CPPUNIT_ASSERT_EQUAL((size_t)100, m[1].first);
  1357. CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[1].second);
  1358. }
  1359. void UtilTest::testGenerateRandomData()
  1360. {
  1361. unsigned char data1[20];
  1362. util::generateRandomData(data1, sizeof(data1));
  1363. unsigned char data2[20];
  1364. util::generateRandomData(data2, sizeof(data2));
  1365. CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
  1366. }
  1367. void UtilTest::testFromHex()
  1368. {
  1369. std::string src;
  1370. std::string dest;
  1371. src = "0011fF";
  1372. dest = util::fromHex(src.begin(), src.end());
  1373. CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
  1374. CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
  1375. CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
  1376. CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
  1377. src = "0011f";
  1378. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  1379. src = "001g";
  1380. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  1381. }
  1382. void UtilTest::testParsePrioritizePieceRange()
  1383. {
  1384. // piece index
  1385. // 0 1 2 3 4 5 6 7
  1386. // | | | |
  1387. // file1 | | |
  1388. // | | |
  1389. // file2 | |
  1390. // file3 |
  1391. // | |
  1392. // file4 |
  1393. size_t pieceLength = 1024;
  1394. std::vector<SharedHandle<FileEntry> > entries(4, SharedHandle<FileEntry>());
  1395. entries[0].reset(new FileEntry("file1", 1024, 0));
  1396. entries[1].reset(new FileEntry("file2",2560,entries[0]->getLastOffset()));
  1397. entries[2].reset(new FileEntry("file3",0,entries[1]->getLastOffset()));
  1398. entries[3].reset(new FileEntry("file4",3584,entries[2]->getLastOffset()));
  1399. std::vector<size_t> result;
  1400. util::parsePrioritizePieceRange(result, "head=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)1, result[1]);
  1404. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1405. result.clear();
  1406. util::parsePrioritizePieceRange(result, "tail=1", entries, pieceLength);
  1407. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1408. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1409. CPPUNIT_ASSERT_EQUAL((size_t)3, result[1]);
  1410. CPPUNIT_ASSERT_EQUAL((size_t)6, result[2]);
  1411. result.clear();
  1412. util::parsePrioritizePieceRange(result, "head=1K", entries, pieceLength);
  1413. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1414. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1415. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1416. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1417. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1418. result.clear();
  1419. util::parsePrioritizePieceRange(result, "head", entries, pieceLength, 1024);
  1420. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1421. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1422. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1423. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1424. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1425. result.clear();
  1426. util::parsePrioritizePieceRange(result, "tail=1K", entries, pieceLength);
  1427. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1428. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1429. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1430. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1431. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1432. result.clear();
  1433. util::parsePrioritizePieceRange(result, "tail", entries, pieceLength, 1024);
  1434. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1435. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1436. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1437. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1438. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1439. result.clear();
  1440. util::parsePrioritizePieceRange(result, "head=1,tail=1", entries, pieceLength);
  1441. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1442. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1443. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1444. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1445. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1446. result.clear();
  1447. util::parsePrioritizePieceRange(result, "head=300M,tail=300M",
  1448. entries, pieceLength);
  1449. CPPUNIT_ASSERT_EQUAL((size_t)7, result.size());
  1450. for(size_t i = 0; i < 7; ++i) {
  1451. CPPUNIT_ASSERT_EQUAL(i, result[i]);
  1452. }
  1453. result.clear();
  1454. util::parsePrioritizePieceRange(result, "", entries, pieceLength);
  1455. CPPUNIT_ASSERT(result.empty());
  1456. }
  1457. void UtilTest::testApplyDir()
  1458. {
  1459. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir("", "pred"));
  1460. CPPUNIT_ASSERT_EQUAL(std::string("/pred"), util::applyDir("/", "pred"));
  1461. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir(".", "pred"));
  1462. CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred"));
  1463. }
  1464. void UtilTest::testFixTaintedBasename()
  1465. {
  1466. CPPUNIT_ASSERT_EQUAL(std::string("a%2Fb"), util::fixTaintedBasename("a/b"));
  1467. #ifdef __MINGW32__
  1468. CPPUNIT_ASSERT_EQUAL(std::string("a%5Cb"), util::fixTaintedBasename("a\\b"));
  1469. #else // !__MINGW32__
  1470. CPPUNIT_ASSERT_EQUAL(std::string("a\\b"), util::fixTaintedBasename("a\\b"));
  1471. #endif // !__MINGW32__
  1472. }
  1473. void UtilTest::testIsNumericHost()
  1474. {
  1475. CPPUNIT_ASSERT(util::isNumericHost("192.168.0.1"));
  1476. CPPUNIT_ASSERT(!util::isNumericHost("aria2.sf.net"));
  1477. CPPUNIT_ASSERT(util::isNumericHost("::1"));
  1478. }
  1479. void UtilTest::testDetectDirTraversal()
  1480. {
  1481. CPPUNIT_ASSERT(util::detectDirTraversal("/foo"));
  1482. CPPUNIT_ASSERT(util::detectDirTraversal("./foo"));
  1483. CPPUNIT_ASSERT(util::detectDirTraversal("../foo"));
  1484. CPPUNIT_ASSERT(util::detectDirTraversal("foo/../bar"));
  1485. CPPUNIT_ASSERT(util::detectDirTraversal("foo/./bar"));
  1486. CPPUNIT_ASSERT(util::detectDirTraversal("foo/."));
  1487. CPPUNIT_ASSERT(util::detectDirTraversal("foo/.."));
  1488. CPPUNIT_ASSERT(util::detectDirTraversal("."));
  1489. CPPUNIT_ASSERT(util::detectDirTraversal(".."));
  1490. CPPUNIT_ASSERT(util::detectDirTraversal("/"));
  1491. CPPUNIT_ASSERT(util::detectDirTraversal("foo/"));
  1492. CPPUNIT_ASSERT(util::detectDirTraversal("\t"));
  1493. CPPUNIT_ASSERT(!util::detectDirTraversal("foo/bar"));
  1494. CPPUNIT_ASSERT(!util::detectDirTraversal("foo"));
  1495. }
  1496. void UtilTest::testEscapePath()
  1497. {
  1498. CPPUNIT_ASSERT_EQUAL(std::string("foo%00bar%00%01"),
  1499. util::escapePath(std::string("foo")+(char)0x00+
  1500. std::string("bar")+(char)0x00+
  1501. (char)0x01));
  1502. #ifdef __MINGW32__
  1503. CPPUNIT_ASSERT_EQUAL(std::string("foo%5Cbar"), util::escapePath("foo\\bar"));
  1504. #else // !__MINGW32__
  1505. CPPUNIT_ASSERT_EQUAL(std::string("foo\\bar"), util::escapePath("foo\\bar"));
  1506. #endif // !__MINGW32__
  1507. }
  1508. void UtilTest::testInSameCidrBlock()
  1509. {
  1510. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 16));
  1511. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 17));
  1512. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "192.168.0.1", 32));
  1513. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.0.1", "192.168.0.0", 32));
  1514. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "10.0.0.1", 0));
  1515. CPPUNIT_ASSERT(util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 28));
  1516. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 29));
  1517. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "192.168.0.1", 8));
  1518. }
  1519. void UtilTest::testIsUtf8String()
  1520. {
  1521. CPPUNIT_ASSERT(util::isUtf8("ascii"));
  1522. // "Hello World" in Japanese UTF-8
  1523. CPPUNIT_ASSERT(util::isUtf8
  1524. (fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
  1525. // "World" in Shift_JIS
  1526. CPPUNIT_ASSERT(!util::isUtf8(fromHex("90a28a")+"E"));
  1527. // UTF8-2
  1528. CPPUNIT_ASSERT(util::isUtf8(fromHex("c280")));
  1529. CPPUNIT_ASSERT(util::isUtf8(fromHex("dfbf")));
  1530. // UTF8-3
  1531. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0a080")));
  1532. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0bf80")));
  1533. CPPUNIT_ASSERT(util::isUtf8(fromHex("e18080")));
  1534. CPPUNIT_ASSERT(util::isUtf8(fromHex("ec8080")));
  1535. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed8080")));
  1536. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed9f80")));
  1537. CPPUNIT_ASSERT(util::isUtf8(fromHex("ee8080")));
  1538. CPPUNIT_ASSERT(util::isUtf8(fromHex("ef8080")));
  1539. // UTF8-4
  1540. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0908080")));
  1541. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0bf8080")));
  1542. CPPUNIT_ASSERT(util::isUtf8(fromHex("f1808080")));
  1543. CPPUNIT_ASSERT(util::isUtf8(fromHex("f3808080")));
  1544. CPPUNIT_ASSERT(util::isUtf8(fromHex("f4808080")));
  1545. CPPUNIT_ASSERT(util::isUtf8(fromHex("f48f8080")));
  1546. CPPUNIT_ASSERT(util::isUtf8(""));
  1547. CPPUNIT_ASSERT(!util::isUtf8(fromHex("00")));
  1548. }
  1549. void UtilTest::testNextParam()
  1550. {
  1551. std::string s1 = " :a : b=c :d=b::::g::";
  1552. std::pair<std::string::iterator, bool> r;
  1553. std::string name, value;
  1554. r = util::nextParam(name, value, s1.begin(), s1.end(), ':');
  1555. CPPUNIT_ASSERT(r.second);
  1556. CPPUNIT_ASSERT_EQUAL(std::string("a"), name);
  1557. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1558. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1559. CPPUNIT_ASSERT(r.second);
  1560. CPPUNIT_ASSERT_EQUAL(std::string("b"), name);
  1561. CPPUNIT_ASSERT_EQUAL(std::string("c"), value);
  1562. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1563. CPPUNIT_ASSERT(r.second);
  1564. CPPUNIT_ASSERT_EQUAL(std::string("d"), name);
  1565. CPPUNIT_ASSERT_EQUAL(std::string("b"), value);
  1566. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1567. CPPUNIT_ASSERT(r.second);
  1568. CPPUNIT_ASSERT_EQUAL(std::string("g"), name);
  1569. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1570. std::string s2 = "";
  1571. r = util::nextParam(name, value, s2.begin(), s2.end(), ':');
  1572. CPPUNIT_ASSERT(!r.second);
  1573. std::string s3 = " ";
  1574. r = util::nextParam(name, value, s3.begin(), s3.end(), ':');
  1575. CPPUNIT_ASSERT(!r.second);
  1576. std::string s4 = ":::";
  1577. r = util::nextParam(name, value, s4.begin(), s4.end(), ':');
  1578. CPPUNIT_ASSERT(!r.second);
  1579. }
  1580. void UtilTest::testNoProxyDomainMatch()
  1581. {
  1582. CPPUNIT_ASSERT(util::noProxyDomainMatch("localhost", "localhost"));
  1583. CPPUNIT_ASSERT(util::noProxyDomainMatch("192.168.0.1", "192.168.0.1"));
  1584. CPPUNIT_ASSERT(util::noProxyDomainMatch("www.example.org", ".example.org"));
  1585. CPPUNIT_ASSERT(!util::noProxyDomainMatch("www.example.org", "example.org"));
  1586. CPPUNIT_ASSERT(!util::noProxyDomainMatch("192.168.0.1", "0.1"));
  1587. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "example.com"));
  1588. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org"));
  1589. }
  1590. void UtilTest::testInPrivateAddress()
  1591. {
  1592. CPPUNIT_ASSERT(!util::inPrivateAddress("localhost"));
  1593. CPPUNIT_ASSERT(util::inPrivateAddress("192.168.0.1"));
  1594. // Only checks prefix..
  1595. CPPUNIT_ASSERT(util::inPrivateAddress("10."));
  1596. CPPUNIT_ASSERT(!util::inPrivateAddress("172."));
  1597. CPPUNIT_ASSERT(!util::inPrivateAddress("172.15.0.0"));
  1598. CPPUNIT_ASSERT(util::inPrivateAddress("172.16.0.0"));
  1599. CPPUNIT_ASSERT(util::inPrivateAddress("172.31.0.0"));
  1600. CPPUNIT_ASSERT(!util::inPrivateAddress("172.32.0.0"));
  1601. }
  1602. void UtilTest::testSecfmt()
  1603. {
  1604. CPPUNIT_ASSERT_EQUAL(std::string("0s"), util::secfmt(0));
  1605. CPPUNIT_ASSERT_EQUAL(std::string("1s"), util::secfmt(1));
  1606. CPPUNIT_ASSERT_EQUAL(std::string("9s"), util::secfmt(9));
  1607. CPPUNIT_ASSERT_EQUAL(std::string("10s"), util::secfmt(10));
  1608. CPPUNIT_ASSERT_EQUAL(std::string("1m"), util::secfmt(60));
  1609. CPPUNIT_ASSERT_EQUAL(std::string("1m59s"), util::secfmt(119));
  1610. CPPUNIT_ASSERT_EQUAL(std::string("2m"), util::secfmt(120));
  1611. CPPUNIT_ASSERT_EQUAL(std::string("59m59s"), util::secfmt(3599));
  1612. CPPUNIT_ASSERT_EQUAL(std::string("1h"), util::secfmt(3600));
  1613. }
  1614. } // namespace aria2