UtilTest.cc 44 KB

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