UtilTest.cc 41 KB

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