UtilTest.cc 44 KB

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