UtilTest.cc 45 KB

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