UtilTest.cc 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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.begin(), src.end()));
  494. std::string src2 = "aria2+aria2";
  495. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"),
  496. util::percentDecode(src2.begin(), src2.end()));
  497. std::string src3 = "%5t%20";
  498. CPPUNIT_ASSERT_EQUAL(std::string("%5t "),
  499. util::percentDecode(src3.begin(), src3.end()));
  500. std::string src4 = "%";
  501. CPPUNIT_ASSERT_EQUAL(std::string("%"),
  502. util::percentDecode(src4.begin(), src4.end()));
  503. std::string src5 = "%3";
  504. CPPUNIT_ASSERT_EQUAL(std::string("%3"),
  505. util::percentDecode(src5.begin(), src5.end()));
  506. std::string src6 = "%2f";
  507. CPPUNIT_ASSERT_EQUAL(std::string("/"),
  508. util::percentDecode(src6.begin(), src6.end()));
  509. }
  510. void UtilTest::testGetRealSize()
  511. {
  512. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096M"));
  513. CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1K"));
  514. try {
  515. util::getRealSize("");
  516. CPPUNIT_FAIL("exception must be thrown.");
  517. } catch(Exception& e) {
  518. std::cerr << e.stackTrace();
  519. }
  520. try {
  521. util::getRealSize("foo");
  522. CPPUNIT_FAIL("exception must be thrown.");
  523. } catch(Exception& e) {
  524. std::cerr << e.stackTrace();
  525. }
  526. try {
  527. util::getRealSize("-1");
  528. CPPUNIT_FAIL("exception must be thrown.");
  529. } catch(Exception& e) {
  530. std::cerr << e.stackTrace();
  531. }
  532. try {
  533. util::getRealSize("9223372036854775807K");
  534. CPPUNIT_FAIL("exception must be thrown.");
  535. } catch(Exception& e) {
  536. std::cerr << e.stackTrace();
  537. }
  538. try {
  539. util::getRealSize("9223372036854775807M");
  540. CPPUNIT_FAIL("exception must be thrown.");
  541. } catch(Exception& e) {
  542. std::cerr << e.stackTrace();
  543. }
  544. }
  545. void UtilTest::testAbbrevSize()
  546. {
  547. CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), util::abbrevSize(4294967296LL));
  548. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1024));
  549. CPPUNIT_ASSERT_EQUAL(std::string("1,023"), util::abbrevSize(1023));
  550. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::abbrevSize(0));
  551. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), util::abbrevSize(1127));
  552. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), util::abbrevSize(1572864));
  553. }
  554. void UtilTest::testToStream()
  555. {
  556. std::ostringstream os;
  557. SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  558. SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  559. std::deque<SharedHandle<FileEntry> > entries;
  560. entries.push_back(f1);
  561. entries.push_back(f2);
  562. std::string filename = A2_TEST_OUT_DIR"/aria2_UtilTest_testToStream";
  563. BufferedFile fp(filename, BufferedFile::WRITE);
  564. util::toStream(entries.begin(), entries.end(), fp);
  565. fp.close();
  566. CPPUNIT_ASSERT_EQUAL(
  567. std::string("Files:\n"
  568. "idx|path/length\n"
  569. "===+===========================================================================\n"
  570. " 1|aria2.tar.bz2\n"
  571. " |12.0KiB (12,300)\n"
  572. "---+---------------------------------------------------------------------------\n"
  573. " 2|aria2.txt\n"
  574. " |556B (556)\n"
  575. "---+---------------------------------------------------------------------------\n"),
  576. readFile(filename));
  577. }
  578. void UtilTest::testIsNumber()
  579. {
  580. CPPUNIT_ASSERT_EQUAL(true, util::isNumber("000"));
  581. CPPUNIT_ASSERT_EQUAL(false, util::isNumber("a"));
  582. CPPUNIT_ASSERT_EQUAL(false, util::isNumber("0a"));
  583. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(""));
  584. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(" "));
  585. }
  586. void UtilTest::testIsLowercase()
  587. {
  588. CPPUNIT_ASSERT_EQUAL(true, util::isLowercase("alpha"));
  589. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase("Alpha"));
  590. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase("1alpha"));
  591. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(""));
  592. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(" "));
  593. }
  594. void UtilTest::testIsUppercase()
  595. {
  596. CPPUNIT_ASSERT_EQUAL(true, util::isUppercase("ALPHA"));
  597. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase("Alpha"));
  598. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase("1ALPHA"));
  599. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(""));
  600. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(" "));
  601. }
  602. void UtilTest::testAlphaToNum()
  603. {
  604. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("a"));
  605. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("aa"));
  606. CPPUNIT_ASSERT_EQUAL(1U, util::alphaToNum("b"));
  607. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("zz")); // 25*26+25
  608. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("ZZ")); // 25*26+25
  609. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum(""));
  610. CPPUNIT_ASSERT_EQUAL(4294967295U, util::alphaToNum("NXMRLXV"));
  611. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("NXMRLXW")); // uint32_t overflow
  612. }
  613. void UtilTest::testMkdirs()
  614. {
  615. std::string dir = A2_TEST_OUT_DIR"/aria2-UtilTest-testMkdirs";
  616. File d(dir);
  617. if(d.exists()) {
  618. CPPUNIT_ASSERT(d.remove());
  619. }
  620. CPPUNIT_ASSERT(!d.exists());
  621. util::mkdirs(dir);
  622. CPPUNIT_ASSERT(d.isDir());
  623. std::string file = A2_TEST_DIR"/UtilTest.cc";
  624. File f(file);
  625. CPPUNIT_ASSERT(f.isFile());
  626. try {
  627. util::mkdirs(file);
  628. CPPUNIT_FAIL("exception must be thrown.");
  629. } catch(DlAbortEx& ex) {
  630. std::cerr << ex.stackTrace() << std::endl;
  631. }
  632. }
  633. void UtilTest::testConvertBitfield()
  634. {
  635. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  636. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  637. srcBitfield.setAllBit();
  638. srcBitfield.unsetBit(2);// <- range [768, 1152)
  639. // which corresponds to the index [1,2] in destBitfield
  640. util::convertBitfield(&destBitfield, &srcBitfield);
  641. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  642. util::toHex(destBitfield.getBitfield(),
  643. destBitfield.getBitfieldLength()));
  644. }
  645. void UtilTest::testParseIntSegments()
  646. {
  647. SegList<int> sgl;
  648. util::parseIntSegments(sgl, "1,3-8,10");
  649. CPPUNIT_ASSERT(sgl.hasNext());
  650. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  651. CPPUNIT_ASSERT(sgl.hasNext());
  652. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  653. CPPUNIT_ASSERT(sgl.hasNext());
  654. CPPUNIT_ASSERT_EQUAL(4, sgl.next());
  655. CPPUNIT_ASSERT(sgl.hasNext());
  656. CPPUNIT_ASSERT_EQUAL(5, sgl.next());
  657. CPPUNIT_ASSERT(sgl.hasNext());
  658. CPPUNIT_ASSERT_EQUAL(6, sgl.next());
  659. CPPUNIT_ASSERT(sgl.hasNext());
  660. CPPUNIT_ASSERT_EQUAL(7, sgl.next());
  661. CPPUNIT_ASSERT(sgl.hasNext());
  662. CPPUNIT_ASSERT_EQUAL(8, sgl.next());
  663. CPPUNIT_ASSERT(sgl.hasNext());
  664. CPPUNIT_ASSERT_EQUAL(10, sgl.next());
  665. CPPUNIT_ASSERT(!sgl.hasNext());
  666. CPPUNIT_ASSERT_EQUAL(0, sgl.next());
  667. sgl.clear();
  668. util::parseIntSegments(sgl, ",,,1,,,3,,,");
  669. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  670. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  671. CPPUNIT_ASSERT(!sgl.hasNext());
  672. }
  673. void UtilTest::testParseIntSegments_invalidRange()
  674. {
  675. try {
  676. SegList<int> sgl;
  677. util::parseIntSegments(sgl, "-1");
  678. CPPUNIT_FAIL("exception must be thrown.");
  679. } catch(Exception& e) {
  680. }
  681. try {
  682. SegList<int> sgl;
  683. util::parseIntSegments(sgl, "1-");
  684. CPPUNIT_FAIL("exception must be thrown.");
  685. } catch(Exception& e) {
  686. }
  687. try {
  688. SegList<int> sgl;
  689. util::parseIntSegments(sgl, "2147483648");
  690. CPPUNIT_FAIL("exception must be thrown.");
  691. } catch(Exception& e) {
  692. }
  693. try {
  694. SegList<int> sgl;
  695. util::parseIntSegments(sgl, "2147483647-2147483648");
  696. CPPUNIT_FAIL("exception must be thrown.");
  697. } catch(Exception& e) {
  698. }
  699. try {
  700. SegList<int> sgl;
  701. util::parseIntSegments(sgl, "1-2x");
  702. CPPUNIT_FAIL("exception must be thrown.");
  703. } catch(Exception& e) {
  704. }
  705. try {
  706. SegList<int> sgl;
  707. util::parseIntSegments(sgl, "3x-4");
  708. CPPUNIT_FAIL("exception must be thrown.");
  709. } catch(Exception& e) {
  710. }
  711. }
  712. void UtilTest::testParseInt()
  713. {
  714. std::string s;
  715. s = " -1 ";
  716. CPPUNIT_ASSERT_EQUAL(-1, util::parseInt(s.begin(), s.end()));
  717. s = "2147483647";
  718. CPPUNIT_ASSERT_EQUAL(2147483647, util::parseInt(s.begin(), s.end()));
  719. try {
  720. s = "2147483648";
  721. util::parseInt(s.begin(), s.end());
  722. CPPUNIT_FAIL("exception must be thrown.");
  723. } catch(Exception& e) {
  724. }
  725. try {
  726. s = "-2147483649";
  727. util::parseInt(s.begin(), s.end());
  728. CPPUNIT_FAIL("exception must be thrown.");
  729. } catch(Exception& e) {
  730. }
  731. try {
  732. s = "12x";
  733. util::parseInt(s.begin(), s.end());
  734. CPPUNIT_FAIL("exception must be thrown.");
  735. } catch(Exception& e) {
  736. }
  737. try {
  738. s = "";
  739. util::parseInt(s.begin(), s.end());
  740. CPPUNIT_FAIL("exception must be thrown.");
  741. } catch(Exception& e) {
  742. }
  743. }
  744. void UtilTest::testParseUInt()
  745. {
  746. std::string s;
  747. s = " 4294967295 ";
  748. CPPUNIT_ASSERT_EQUAL(4294967295U, util::parseUInt(s.begin(), s.end()));
  749. try {
  750. s = "-1";
  751. util::parseUInt(s.begin(), s.end());
  752. CPPUNIT_FAIL("exception must be thrown.");
  753. } catch(Exception& e) {
  754. }
  755. try {
  756. s = "4294967296";
  757. util::parseUInt(s.begin(), s.end());
  758. CPPUNIT_FAIL("exception must be thrown.");
  759. } catch(Exception& e) {
  760. }
  761. }
  762. void UtilTest::testParseLLInt()
  763. {
  764. std::string s;
  765. {
  766. s = " -1 ";
  767. CPPUNIT_ASSERT_EQUAL((int64_t)-1LL, util::parseLLInt(s.begin(), s.end()));
  768. }
  769. {
  770. s = "9223372036854775807";
  771. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
  772. util::parseLLInt(s.begin(), s.end()));
  773. }
  774. try {
  775. s = "9223372036854775808";
  776. util::parseLLInt(s.begin(), s.end());
  777. CPPUNIT_FAIL("exception must be thrown.");
  778. } catch(Exception& e) {
  779. std::cerr << e.stackTrace();
  780. }
  781. try {
  782. s = "-9223372036854775809";
  783. util::parseLLInt(s.begin(), s.end());
  784. CPPUNIT_FAIL("exception must be thrown.");
  785. } catch(Exception& e) {
  786. std::cerr << e.stackTrace();
  787. }
  788. try {
  789. s = "12x";
  790. util::parseLLInt(s.begin(), s.end());
  791. CPPUNIT_FAIL("exception must be thrown.");
  792. } catch(Exception& e) {
  793. std::cerr << e.stackTrace();
  794. }
  795. try {
  796. s = "";
  797. util::parseLLInt(s.begin(), s.end());
  798. CPPUNIT_FAIL("exception must be thrown.");
  799. } catch(Exception& e) {
  800. std::cerr << e.stackTrace();
  801. }
  802. }
  803. void UtilTest::testParseULLInt()
  804. {
  805. std::string s;
  806. s = "18446744073709551615";
  807. CPPUNIT_ASSERT_EQUAL((uint64_t)18446744073709551615ULL,
  808. util::parseULLInt(s.begin(), s.end()));
  809. try {
  810. s = "-1";
  811. util::parseULLInt(s.begin(), s.end());
  812. CPPUNIT_FAIL("exception must be thrown.");
  813. } catch(Exception& e) {
  814. }
  815. try {
  816. s = "18446744073709551616";
  817. util::parseULLInt(s.begin(), s.end());
  818. CPPUNIT_FAIL("exception must be thrown.");
  819. } catch(Exception& e) {
  820. }
  821. }
  822. void UtilTest::testParseIntNoThrow()
  823. {
  824. std::string s;
  825. int32_t n;
  826. s = " -1 ";
  827. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s.begin(), s.end()));
  828. CPPUNIT_ASSERT_EQUAL((int32_t)-1, n);
  829. s = "2147483647";
  830. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s.begin(), s.end()));
  831. CPPUNIT_ASSERT_EQUAL((int32_t)2147483647, n);
  832. s = "2147483648";
  833. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s.begin(), s.end()));
  834. s = "-2147483649";
  835. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s.begin(), s.end()));
  836. s = "12x";
  837. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s.begin(), s.end()));
  838. s = "";
  839. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s.begin(), s.end()));
  840. }
  841. void UtilTest::testParseUIntNoThrow()
  842. {
  843. std::string s;
  844. uint32_t n;
  845. s = " 4294967295 ";
  846. CPPUNIT_ASSERT(util::parseUIntNoThrow(n, s.begin(), s.end()));
  847. CPPUNIT_ASSERT_EQUAL((uint32_t)UINT32_MAX, n);
  848. s = "4294967296";
  849. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s.begin(), s.end()));
  850. s = "-1";
  851. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s.begin(), s.end()));
  852. }
  853. void UtilTest::testParseLLIntNoThrow()
  854. {
  855. std::string s;
  856. int64_t n;
  857. s = " 9223372036854775807 ";
  858. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s.begin(), s.end()));
  859. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MAX, n);
  860. s = "9223372036854775808";
  861. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s.begin(), s.end()));
  862. s = "-9223372036854775808";
  863. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s.begin(), s.end()));
  864. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MIN, n);
  865. s = "-9223372036854775809";
  866. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s.begin(), s.end()));
  867. }
  868. void UtilTest::testToString_binaryStream()
  869. {
  870. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  871. std::string data(16*1024+256, 'a');
  872. dw->initAndOpenFile();
  873. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  874. std::string readData = util::toString(dw);
  875. CPPUNIT_ASSERT_EQUAL(data, readData);
  876. }
  877. void UtilTest::testItos()
  878. {
  879. {
  880. int i = 0;
  881. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::itos(i));
  882. }
  883. {
  884. int i = 100;
  885. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i, true));
  886. }
  887. {
  888. int i = 100;
  889. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i));
  890. }
  891. {
  892. int i = 12345;
  893. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), util::itos(i, true));
  894. }
  895. {
  896. int i = 12345;
  897. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::itos(i));
  898. }
  899. {
  900. int i = -12345;
  901. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), util::itos(i, true));
  902. }
  903. {
  904. int64_t i = INT64_MAX;
  905. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"),
  906. util::itos(i, true));
  907. }
  908. {
  909. int64_t i = INT64_MIN;
  910. CPPUNIT_ASSERT_EQUAL(std::string("-9,223,372,036,854,775,808"),
  911. util::itos(i, true));
  912. }
  913. }
  914. void UtilTest::testUitos()
  915. {
  916. {
  917. uint16_t i = 12345;
  918. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::uitos(i));
  919. }
  920. {
  921. int16_t i = -12345;
  922. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), util::uitos(i));
  923. }
  924. }
  925. void UtilTest::testNtoh64()
  926. {
  927. uint64_t x = 0xff00ff00ee00ee00LL;
  928. #ifdef WORDS_BIGENDIAN
  929. CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
  930. CPPUNIT_ASSERT_EQUAL(x, hton64(x));
  931. #else // !WORDS_BIGENDIAN
  932. uint64_t y = 0x00ee00ee00ff00ffLL;
  933. CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
  934. CPPUNIT_ASSERT_EQUAL(x, hton64(y));
  935. #endif // !WORDS_BIGENDIAN
  936. }
  937. void UtilTest::testPercentEncode()
  938. {
  939. CPPUNIT_ASSERT_EQUAL
  940. (std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
  941. util::percentEncode(":/?#[]@!%&'()*+,;="));
  942. std::string unreserved =
  943. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  944. "abcdefghijklmnopqrstuvwxyz"
  945. "0123456789"
  946. "-._~";
  947. CPPUNIT_ASSERT_EQUAL(unreserved, util::percentEncode(unreserved));
  948. CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::percentEncode("1^A "));
  949. }
  950. void UtilTest::testHtmlEscape()
  951. {
  952. CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),
  953. util::htmlEscape("aria2<>\"'util"));
  954. }
  955. void UtilTest::testJoinPath()
  956. {
  957. const std::string dir1dir2file[] = { "dir1", "dir2", "file" };
  958. CPPUNIT_ASSERT_EQUAL
  959. (std::string("dir1/dir2/file"),
  960. util::joinPath(vbegin(dir1dir2file), vend(dir1dir2file)));
  961. const std::string dirparentfile[] = { "dir", "..", "file" };
  962. CPPUNIT_ASSERT_EQUAL
  963. (std::string("file"),
  964. util::joinPath(vbegin(dirparentfile), vend(dirparentfile)));
  965. const std::string dirparentparentfile[] = { "dir", "..", "..", "file" };
  966. CPPUNIT_ASSERT_EQUAL
  967. (std::string("file"),
  968. util::joinPath(vbegin(dirparentparentfile), vend(dirparentparentfile)));
  969. const std::string dirdotfile[] = { "dir", ".", "file" };
  970. CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
  971. util::joinPath(vbegin(dirdotfile), vend(dirdotfile)));
  972. const std::string empty[] = {};
  973. CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(&empty[0], &empty[0]));
  974. const std::string parentdot[] = { "..", "." };
  975. CPPUNIT_ASSERT_EQUAL(std::string(""),
  976. util::joinPath(vbegin(parentdot), vend(parentdot)));
  977. }
  978. void UtilTest::testParseIndexPath()
  979. {
  980. std::pair<size_t, std::string> p = util::parseIndexPath("1=foo");
  981. CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
  982. CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
  983. try {
  984. util::parseIndexPath("1X=foo");
  985. CPPUNIT_FAIL("exception must be thrown.");
  986. } catch(Exception& e) {
  987. // success
  988. }
  989. try {
  990. util::parseIndexPath("1=");
  991. CPPUNIT_FAIL("exception must be thrown.");
  992. } catch(Exception& e) {
  993. // success
  994. }
  995. }
  996. void UtilTest::testCreateIndexPaths()
  997. {
  998. std::stringstream in
  999. ("1=/tmp/myfile\n"
  1000. "100=/myhome/mypicture.png\n");
  1001. std::vector<std::pair<size_t, std::string> > m = util::createIndexPaths(in);
  1002. CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
  1003. CPPUNIT_ASSERT_EQUAL((size_t)1, m[0].first);
  1004. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[0].second);
  1005. CPPUNIT_ASSERT_EQUAL((size_t)100, m[1].first);
  1006. CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[1].second);
  1007. }
  1008. void UtilTest::testGenerateRandomData()
  1009. {
  1010. unsigned char data1[20];
  1011. util::generateRandomData(data1, sizeof(data1));
  1012. unsigned char data2[20];
  1013. util::generateRandomData(data2, sizeof(data2));
  1014. CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
  1015. }
  1016. void UtilTest::testFromHex()
  1017. {
  1018. std::string src;
  1019. std::string dest;
  1020. src = "0011fF";
  1021. dest = util::fromHex(src);
  1022. CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
  1023. CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
  1024. CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
  1025. CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
  1026. src = "0011f";
  1027. CPPUNIT_ASSERT(util::fromHex(src).empty());
  1028. src = "001g";
  1029. CPPUNIT_ASSERT(util::fromHex(src).empty());
  1030. }
  1031. void UtilTest::testParsePrioritizePieceRange()
  1032. {
  1033. // piece index
  1034. // 0 1 2 3 4 5 6 7
  1035. // | | | |
  1036. // file1 | | |
  1037. // | | |
  1038. // file2 | |
  1039. // file3 |
  1040. // | |
  1041. // file4 |
  1042. size_t pieceLength = 1024;
  1043. std::vector<SharedHandle<FileEntry> > entries(4, SharedHandle<FileEntry>());
  1044. entries[0].reset(new FileEntry("file1", 1024, 0));
  1045. entries[1].reset(new FileEntry("file2",2560,entries[0]->getLastOffset()));
  1046. entries[2].reset(new FileEntry("file3",0,entries[1]->getLastOffset()));
  1047. entries[3].reset(new FileEntry("file4",3584,entries[2]->getLastOffset()));
  1048. std::vector<size_t> result;
  1049. util::parsePrioritizePieceRange(result, "head=1", entries, pieceLength);
  1050. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1051. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1052. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1053. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1054. result.clear();
  1055. util::parsePrioritizePieceRange(result, "tail=1", entries, pieceLength);
  1056. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1057. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1058. CPPUNIT_ASSERT_EQUAL((size_t)3, result[1]);
  1059. CPPUNIT_ASSERT_EQUAL((size_t)6, result[2]);
  1060. result.clear();
  1061. util::parsePrioritizePieceRange(result, "head=1K", entries, pieceLength);
  1062. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1063. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1064. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1065. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1066. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1067. result.clear();
  1068. util::parsePrioritizePieceRange(result, "head", entries, pieceLength, 1024);
  1069. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1070. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1071. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1072. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1073. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1074. result.clear();
  1075. util::parsePrioritizePieceRange(result, "tail=1K", entries, pieceLength);
  1076. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1077. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1078. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1079. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1080. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1081. result.clear();
  1082. util::parsePrioritizePieceRange(result, "tail", entries, pieceLength, 1024);
  1083. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1084. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1085. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1086. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1087. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1088. result.clear();
  1089. util::parsePrioritizePieceRange(result, "head=1,tail=1", entries, pieceLength);
  1090. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1091. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1092. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1093. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1094. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1095. result.clear();
  1096. util::parsePrioritizePieceRange(result, "head=300M,tail=300M",
  1097. entries, pieceLength);
  1098. CPPUNIT_ASSERT_EQUAL((size_t)7, result.size());
  1099. for(size_t i = 0; i < 7; ++i) {
  1100. CPPUNIT_ASSERT_EQUAL(i, result[i]);
  1101. }
  1102. result.clear();
  1103. util::parsePrioritizePieceRange(result, "", entries, pieceLength);
  1104. CPPUNIT_ASSERT(result.empty());
  1105. }
  1106. void UtilTest::testApplyDir()
  1107. {
  1108. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir("", "pred"));
  1109. CPPUNIT_ASSERT_EQUAL(std::string("/pred"), util::applyDir("/", "pred"));
  1110. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir(".", "pred"));
  1111. CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred"));
  1112. }
  1113. void UtilTest::testFixTaintedBasename()
  1114. {
  1115. CPPUNIT_ASSERT_EQUAL(std::string("a%2Fb"), util::fixTaintedBasename("a/b"));
  1116. #ifdef __MINGW32__
  1117. CPPUNIT_ASSERT_EQUAL(std::string("a%5Cb"), util::fixTaintedBasename("a\\b"));
  1118. #else // !__MINGW32__
  1119. CPPUNIT_ASSERT_EQUAL(std::string("a\\b"), util::fixTaintedBasename("a\\b"));
  1120. #endif // !__MINGW32__
  1121. }
  1122. void UtilTest::testIsNumericHost()
  1123. {
  1124. CPPUNIT_ASSERT(util::isNumericHost("192.168.0.1"));
  1125. CPPUNIT_ASSERT(!util::isNumericHost("aria2.sf.net"));
  1126. CPPUNIT_ASSERT(util::isNumericHost("::1"));
  1127. }
  1128. void UtilTest::testDetectDirTraversal()
  1129. {
  1130. CPPUNIT_ASSERT(util::detectDirTraversal("/foo"));
  1131. CPPUNIT_ASSERT(util::detectDirTraversal("./foo"));
  1132. CPPUNIT_ASSERT(util::detectDirTraversal("../foo"));
  1133. CPPUNIT_ASSERT(util::detectDirTraversal("foo/../bar"));
  1134. CPPUNIT_ASSERT(util::detectDirTraversal("foo/./bar"));
  1135. CPPUNIT_ASSERT(util::detectDirTraversal("foo/."));
  1136. CPPUNIT_ASSERT(util::detectDirTraversal("foo/.."));
  1137. CPPUNIT_ASSERT(util::detectDirTraversal("."));
  1138. CPPUNIT_ASSERT(util::detectDirTraversal(".."));
  1139. CPPUNIT_ASSERT(util::detectDirTraversal("/"));
  1140. CPPUNIT_ASSERT(util::detectDirTraversal("foo/"));
  1141. CPPUNIT_ASSERT(util::detectDirTraversal("\t"));
  1142. CPPUNIT_ASSERT(!util::detectDirTraversal("foo/bar"));
  1143. CPPUNIT_ASSERT(!util::detectDirTraversal("foo"));
  1144. }
  1145. void UtilTest::testEscapePath()
  1146. {
  1147. CPPUNIT_ASSERT_EQUAL(std::string("foo%00bar%00%01"),
  1148. util::escapePath(std::string("foo")+(char)0x00+
  1149. std::string("bar")+(char)0x00+
  1150. (char)0x01));
  1151. #ifdef __MINGW32__
  1152. CPPUNIT_ASSERT_EQUAL(std::string("foo%5Cbar"), util::escapePath("foo\\bar"));
  1153. #else // !__MINGW32__
  1154. CPPUNIT_ASSERT_EQUAL(std::string("foo\\bar"), util::escapePath("foo\\bar"));
  1155. #endif // !__MINGW32__
  1156. }
  1157. void UtilTest::testInSameCidrBlock()
  1158. {
  1159. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 16));
  1160. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 17));
  1161. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "192.168.0.1", 32));
  1162. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.0.1", "192.168.0.0", 32));
  1163. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "10.0.0.1", 0));
  1164. CPPUNIT_ASSERT(util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 28));
  1165. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 29));
  1166. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "192.168.0.1", 8));
  1167. }
  1168. void UtilTest::testIsUtf8String()
  1169. {
  1170. CPPUNIT_ASSERT(util::isUtf8("ascii"));
  1171. // "Hello World" in Japanese UTF-8
  1172. CPPUNIT_ASSERT(util::isUtf8
  1173. (util::fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
  1174. // "World" in Shift_JIS
  1175. CPPUNIT_ASSERT(!util::isUtf8(util::fromHex("90a28a")+"E"));
  1176. // UTF8-2
  1177. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("c280")));
  1178. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("dfbf")));
  1179. // UTF8-3
  1180. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e0a080")));
  1181. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e0bf80")));
  1182. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e18080")));
  1183. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ec8080")));
  1184. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ed8080")));
  1185. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ed9f80")));
  1186. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ee8080")));
  1187. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ef8080")));
  1188. // UTF8-4
  1189. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f0908080")));
  1190. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f0bf8080")));
  1191. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f1808080")));
  1192. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f3808080")));
  1193. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f4808080")));
  1194. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f48f8080")));
  1195. CPPUNIT_ASSERT(util::isUtf8(""));
  1196. CPPUNIT_ASSERT(!util::isUtf8(util::fromHex("00")));
  1197. }
  1198. void UtilTest::testNextParam()
  1199. {
  1200. std::string s1 = " :a : b=c :d=b::::g::";
  1201. std::pair<std::string::iterator, bool> r;
  1202. std::string name, value;
  1203. r = util::nextParam(name, value, s1.begin(), s1.end(), ':');
  1204. CPPUNIT_ASSERT(r.second);
  1205. CPPUNIT_ASSERT_EQUAL(std::string("a"), name);
  1206. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1207. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1208. CPPUNIT_ASSERT(r.second);
  1209. CPPUNIT_ASSERT_EQUAL(std::string("b"), name);
  1210. CPPUNIT_ASSERT_EQUAL(std::string("c"), value);
  1211. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1212. CPPUNIT_ASSERT(r.second);
  1213. CPPUNIT_ASSERT_EQUAL(std::string("d"), name);
  1214. CPPUNIT_ASSERT_EQUAL(std::string("b"), value);
  1215. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1216. CPPUNIT_ASSERT(r.second);
  1217. CPPUNIT_ASSERT_EQUAL(std::string("g"), name);
  1218. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1219. std::string s2 = "";
  1220. r = util::nextParam(name, value, s2.begin(), s2.end(), ':');
  1221. CPPUNIT_ASSERT(!r.second);
  1222. std::string s3 = " ";
  1223. r = util::nextParam(name, value, s3.begin(), s3.end(), ':');
  1224. CPPUNIT_ASSERT(!r.second);
  1225. std::string s4 = ":::";
  1226. r = util::nextParam(name, value, s4.begin(), s4.end(), ':');
  1227. CPPUNIT_ASSERT(!r.second);
  1228. }
  1229. void UtilTest::testNoProxyDomainMatch()
  1230. {
  1231. CPPUNIT_ASSERT(util::noProxyDomainMatch("localhost", "localhost"));
  1232. CPPUNIT_ASSERT(util::noProxyDomainMatch("192.168.0.1", "192.168.0.1"));
  1233. CPPUNIT_ASSERT(util::noProxyDomainMatch("www.example.org", ".example.org"));
  1234. CPPUNIT_ASSERT(!util::noProxyDomainMatch("www.example.org", "example.org"));
  1235. CPPUNIT_ASSERT(!util::noProxyDomainMatch("192.168.0.1", "0.1"));
  1236. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "example.com"));
  1237. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org"));
  1238. }
  1239. } // namespace aria2