UtilTest.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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(testTrim);
  17. CPPUNIT_TEST(testSplit);
  18. CPPUNIT_TEST(testSplit_many);
  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(testUrldecode);
  28. CPPUNIT_TEST(testGetRealSize);
  29. CPPUNIT_TEST(testAbbrevSize);
  30. CPPUNIT_TEST(testToStream);
  31. CPPUNIT_TEST(testIsNumber);
  32. CPPUNIT_TEST(testIsLowercase);
  33. CPPUNIT_TEST(testIsUppercase);
  34. CPPUNIT_TEST(testAlphaToNum);
  35. CPPUNIT_TEST(testMkdirs);
  36. CPPUNIT_TEST(testConvertBitfield);
  37. CPPUNIT_TEST(testParseIntRange);
  38. CPPUNIT_TEST(testParseIntRange_invalidRange);
  39. CPPUNIT_TEST(testParseInt);
  40. CPPUNIT_TEST(testParseUInt);
  41. CPPUNIT_TEST(testParseLLInt);
  42. CPPUNIT_TEST(testParseULLInt);
  43. CPPUNIT_TEST(testToString_binaryStream);
  44. CPPUNIT_TEST(testItos);
  45. CPPUNIT_TEST(testUitos);
  46. CPPUNIT_TEST(testNtoh64);
  47. CPPUNIT_TEST(testUrlencode);
  48. CPPUNIT_TEST(testHtmlEscape);
  49. CPPUNIT_TEST(testJoinPath);
  50. CPPUNIT_TEST(testParseIndexPath);
  51. CPPUNIT_TEST(testCreateIndexPathMap);
  52. CPPUNIT_TEST(testGenerateRandomData);
  53. CPPUNIT_TEST_SUITE_END();
  54. private:
  55. public:
  56. void setUp() {
  57. }
  58. void testTrim();
  59. void testSplit();
  60. void testSplit_many();
  61. void testEndsWith();
  62. void testReplace();
  63. void testStartsWith();
  64. // may be moved to other helper class in the future.
  65. void testGetContentDispositionFilename();
  66. void testRandomAlpha();
  67. void testToUpper();
  68. void testToLower();
  69. void testUrldecode();
  70. void testGetRealSize();
  71. void testAbbrevSize();
  72. void testToStream();
  73. void testIsNumber();
  74. void testIsLowercase();
  75. void testIsUppercase();
  76. void testAlphaToNum();
  77. void testMkdirs();
  78. void testConvertBitfield();
  79. void testParseIntRange();
  80. void testParseIntRange_invalidRange();
  81. void testParseInt();
  82. void testParseUInt();
  83. void testParseLLInt();
  84. void testParseULLInt();
  85. void testToString_binaryStream();
  86. void testItos();
  87. void testUitos();
  88. void testNtoh64();
  89. void testUrlencode();
  90. void testHtmlEscape();
  91. void testJoinPath();
  92. void testParseIndexPath();
  93. void testCreateIndexPathMap();
  94. void testGenerateRandomData();
  95. };
  96. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  97. void UtilTest::testTrim() {
  98. std::string str1 = "aria2";
  99. CPPUNIT_ASSERT_EQUAL(str1, util::trim("aria2"));
  100. CPPUNIT_ASSERT_EQUAL(str1, util::trim(" aria2"));
  101. CPPUNIT_ASSERT_EQUAL(str1, util::trim(" aria2 "));
  102. CPPUNIT_ASSERT_EQUAL(str1, util::trim(" aria2 "));
  103. std::string str2 = "aria2 debut";
  104. CPPUNIT_ASSERT_EQUAL(str2, util::trim("aria2 debut"));
  105. CPPUNIT_ASSERT_EQUAL(str2, util::trim(" aria2 debut "));
  106. std::string str3 = "";
  107. CPPUNIT_ASSERT_EQUAL(str3, util::trim(""));
  108. CPPUNIT_ASSERT_EQUAL(str3, util::trim(" "));
  109. CPPUNIT_ASSERT_EQUAL(str3, util::trim(" "));
  110. std::string str4 = "A";
  111. CPPUNIT_ASSERT_EQUAL(str4, util::trim("A"));
  112. CPPUNIT_ASSERT_EQUAL(str4, util::trim(" A "));
  113. CPPUNIT_ASSERT_EQUAL(str4, util::trim(" A "));
  114. }
  115. void UtilTest::testSplit() {
  116. std::pair<std::string, std::string> p1;
  117. util::split(p1, "name=value", '=');
  118. CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
  119. CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
  120. util::split(p1, " name = value ", '=');
  121. CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
  122. CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
  123. util::split(p1, "=value", '=');
  124. CPPUNIT_ASSERT_EQUAL(std::string(""), p1.first);
  125. CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
  126. util::split(p1, "name=", '=');
  127. CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
  128. CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second);
  129. util::split(p1, "name", '=');
  130. CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
  131. CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second);
  132. }
  133. void UtilTest::testSplit_many() {
  134. std::deque<std::string> v1;
  135. util::split("name1=value1; name2=value2; name3=value3",std::back_inserter(v1),
  136. ";", true);
  137. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  138. std::deque<std::string>::iterator itr = v1.begin();
  139. CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);
  140. CPPUNIT_ASSERT_EQUAL(std::string("name2=value2"), *itr++);
  141. CPPUNIT_ASSERT_EQUAL(std::string("name3=value3"), *itr++);
  142. v1.clear();
  143. util::split("name1=value1; name2=value2; name3=value3",std::back_inserter(v1),
  144. ";", false);
  145. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  146. itr = v1.begin();
  147. CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);
  148. CPPUNIT_ASSERT_EQUAL(std::string(" name2=value2"), *itr++);
  149. CPPUNIT_ASSERT_EQUAL(std::string(" name3=value3"), *itr++);
  150. }
  151. void UtilTest::testEndsWith() {
  152. std::string target = "abcdefg";
  153. std::string part = "fg";
  154. CPPUNIT_ASSERT(util::endsWith(target, part));
  155. target = "abdefg";
  156. part = "g";
  157. CPPUNIT_ASSERT(util::endsWith(target, part));
  158. target = "abdefg";
  159. part = "eg";
  160. CPPUNIT_ASSERT(!util::endsWith(target, part));
  161. target = "g";
  162. part = "eg";
  163. CPPUNIT_ASSERT(!util::endsWith(target, part));
  164. target = "g";
  165. part = "g";
  166. CPPUNIT_ASSERT(util::endsWith(target, part));
  167. target = "g";
  168. part = "";
  169. CPPUNIT_ASSERT(util::endsWith(target, part));
  170. target = "";
  171. part = "";
  172. CPPUNIT_ASSERT(util::endsWith(target, part));
  173. target = "";
  174. part = "g";
  175. CPPUNIT_ASSERT(!util::endsWith(target, part));
  176. }
  177. void UtilTest::testReplace() {
  178. CPPUNIT_ASSERT_EQUAL(std::string("abc\n"), util::replace("abc\r\n", "\r", ""));
  179. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc\r\n", "\r\n", ""));
  180. CPPUNIT_ASSERT_EQUAL(std::string(""), util::replace("", "\r\n", ""));
  181. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc", "", "a"));
  182. CPPUNIT_ASSERT_EQUAL(std::string("xbc"), util::replace("abc", "a", "x"));
  183. }
  184. void UtilTest::testStartsWith() {
  185. std::string target;
  186. std::string part;
  187. target = "abcdefg";
  188. part = "abc";
  189. CPPUNIT_ASSERT(util::startsWith(target, part));
  190. target = "abcdefg";
  191. part = "abx";
  192. CPPUNIT_ASSERT(!util::startsWith(target, part));
  193. target = "abcdefg";
  194. part = "bcd";
  195. CPPUNIT_ASSERT(!util::startsWith(target, part));
  196. target = "";
  197. part = "a";
  198. CPPUNIT_ASSERT(!util::startsWith(target, part));
  199. target = "";
  200. part = "";
  201. CPPUNIT_ASSERT(util::startsWith(target, part));
  202. target = "a";
  203. part = "";
  204. CPPUNIT_ASSERT(util::startsWith(target, part));
  205. target = "a";
  206. part = "a";
  207. CPPUNIT_ASSERT(util::startsWith(target, part));
  208. }
  209. void UtilTest::testGetContentDispositionFilename() {
  210. std::string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  211. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h1));
  212. std::string h2 = "attachment; filename=\"\"";
  213. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h2));
  214. std::string h3 = "attachment; filename=\"";
  215. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h3));
  216. std::string h4 = "attachment;";
  217. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h4));
  218. std::string h5 = "attachment; filename=aria2.tar.bz2";
  219. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h5));
  220. std::string h6 = "attachment; filename='aria2.tar.bz2'";
  221. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h6));
  222. std::string h7 = "attachment; filename='aria2.tar.bz2";
  223. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h7));
  224. std::string h8 = "attachment; filename=aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT";
  225. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h8));
  226. std::string h9 = "attachment; filename=\"aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT\"";
  227. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT"), util::getContentDispositionFilename(h9));
  228. std::string h10 = "attachment; filename=";
  229. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h10));
  230. std::string h11 = "attachment; filename=;";
  231. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h11));
  232. std::string filenameWithDir = "attachment; filename=dir/file";
  233. CPPUNIT_ASSERT_EQUAL(std::string("file"),
  234. util::getContentDispositionFilename(filenameWithDir));
  235. std::string parentDir = "attachment; filename=..";
  236. CPPUNIT_ASSERT_EQUAL(std::string(),
  237. util::getContentDispositionFilename(parentDir));
  238. std::string currentDir = "attachment; filename=.";
  239. CPPUNIT_ASSERT_EQUAL(std::string(),
  240. util::getContentDispositionFilename(currentDir));
  241. }
  242. class Printer {
  243. public:
  244. template<class T>
  245. void operator()(T t) {
  246. std::cerr << t << ", ";
  247. }
  248. };
  249. void UtilTest::testRandomAlpha() {
  250. SharedHandle<Randomizer> rand(new FixedNumberRandomizer());
  251. std::string s = util::randomAlpha(8, rand);
  252. CPPUNIT_ASSERT_EQUAL(std::string("AAAAAAAA"), s);
  253. }
  254. void UtilTest::testToUpper() {
  255. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  256. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  257. CPPUNIT_ASSERT_EQUAL(upp, util::toUpper(src));
  258. }
  259. void UtilTest::testToLower() {
  260. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  261. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  262. CPPUNIT_ASSERT_EQUAL(upp, util::toLower(src));
  263. }
  264. void UtilTest::testUrldecode() {
  265. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  266. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  267. util::urldecode(src));
  268. std::string src2 = "aria2+aria2";
  269. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"), util::urldecode(src2));
  270. std::string src3 = "%5t%20";
  271. CPPUNIT_ASSERT_EQUAL(std::string("%5t "), util::urldecode(src3));
  272. std::string src4 = "%";
  273. CPPUNIT_ASSERT_EQUAL(std::string("%"), util::urldecode(src4));
  274. std::string src5 = "%3";
  275. CPPUNIT_ASSERT_EQUAL(std::string("%3"), util::urldecode(src5));
  276. std::string src6 = "%2f";
  277. CPPUNIT_ASSERT_EQUAL(std::string("/"), util::urldecode(src6));
  278. }
  279. void UtilTest::testGetRealSize()
  280. {
  281. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096M"));
  282. CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1K"));
  283. try {
  284. util::getRealSize("");
  285. CPPUNIT_FAIL("exception must be thrown.");
  286. } catch(Exception& e) {
  287. std::cerr << e.stackTrace();
  288. }
  289. try {
  290. util::getRealSize("foo");
  291. CPPUNIT_FAIL("exception must be thrown.");
  292. } catch(Exception& e) {
  293. std::cerr << e.stackTrace();
  294. }
  295. try {
  296. util::getRealSize("-1");
  297. CPPUNIT_FAIL("exception must be thrown.");
  298. } catch(Exception& e) {
  299. std::cerr << e.stackTrace();
  300. }
  301. try {
  302. util::getRealSize("9223372036854775807K");
  303. CPPUNIT_FAIL("exception must be thrown.");
  304. } catch(Exception& e) {
  305. std::cerr << e.stackTrace();
  306. }
  307. try {
  308. util::getRealSize("9223372036854775807M");
  309. CPPUNIT_FAIL("exception must be thrown.");
  310. } catch(Exception& e) {
  311. std::cerr << e.stackTrace();
  312. }
  313. }
  314. void UtilTest::testAbbrevSize()
  315. {
  316. CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), util::abbrevSize(4294967296LL));
  317. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1024));
  318. CPPUNIT_ASSERT_EQUAL(std::string("1,023"), util::abbrevSize(1023));
  319. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::abbrevSize(0));
  320. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), util::abbrevSize(1127));
  321. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), util::abbrevSize(1572864));
  322. }
  323. void UtilTest::testToStream()
  324. {
  325. std::ostringstream os;
  326. SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  327. SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  328. std::deque<SharedHandle<FileEntry> > entries;
  329. entries.push_back(f1);
  330. entries.push_back(f2);
  331. util::toStream(entries.begin(), entries.end(), os);
  332. CPPUNIT_ASSERT_EQUAL(
  333. std::string("Files:\n"
  334. "idx|path/length\n"
  335. "===+===========================================================================\n"
  336. " 1|aria2.tar.bz2\n"
  337. " |12.0KiB (12,300)\n"
  338. "---+---------------------------------------------------------------------------\n"
  339. " 2|aria2.txt\n"
  340. " |556B (556)\n"
  341. "---+---------------------------------------------------------------------------\n"),
  342. os.str());
  343. }
  344. void UtilTest::testIsNumber()
  345. {
  346. CPPUNIT_ASSERT_EQUAL(true, util::isNumber("000"));
  347. CPPUNIT_ASSERT_EQUAL(false, util::isNumber("a"));
  348. CPPUNIT_ASSERT_EQUAL(false, util::isNumber("0a"));
  349. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(""));
  350. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(" "));
  351. }
  352. void UtilTest::testIsLowercase()
  353. {
  354. CPPUNIT_ASSERT_EQUAL(true, util::isLowercase("alpha"));
  355. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase("Alpha"));
  356. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase("1alpha"));
  357. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(""));
  358. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(" "));
  359. }
  360. void UtilTest::testIsUppercase()
  361. {
  362. CPPUNIT_ASSERT_EQUAL(true, util::isUppercase("ALPHA"));
  363. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase("Alpha"));
  364. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase("1ALPHA"));
  365. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(""));
  366. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(" "));
  367. }
  368. void UtilTest::testAlphaToNum()
  369. {
  370. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("a"));
  371. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("aa"));
  372. CPPUNIT_ASSERT_EQUAL(1U, util::alphaToNum("b"));
  373. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("zz")); // 25*26+25
  374. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("ZZ")); // 25*26+25
  375. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum(""));
  376. CPPUNIT_ASSERT_EQUAL(4294967295U, util::alphaToNum("NXMRLXV"));
  377. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("NXMRLXW")); // uint32_t overflow
  378. }
  379. void UtilTest::testMkdirs()
  380. {
  381. std::string dir = "/tmp/aria2-UtilTest-testMkdirs";
  382. File d(dir);
  383. if(d.exists()) {
  384. CPPUNIT_ASSERT(d.remove());
  385. }
  386. CPPUNIT_ASSERT(!d.exists());
  387. util::mkdirs(dir);
  388. CPPUNIT_ASSERT(d.isDir());
  389. std::string file = "./UtilTest.cc";
  390. File f(file);
  391. CPPUNIT_ASSERT(f.isFile());
  392. try {
  393. util::mkdirs(file);
  394. CPPUNIT_FAIL("exception must be thrown.");
  395. } catch(DlAbortEx& ex) {
  396. std::cerr << ex.stackTrace() << std::endl;
  397. }
  398. }
  399. void UtilTest::testConvertBitfield()
  400. {
  401. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  402. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  403. srcBitfield.setAllBit();
  404. srcBitfield.unsetBit(2);// <- range [768, 1152)
  405. // which corresponds to the index [1,2] in destBitfield
  406. util::convertBitfield(&destBitfield, &srcBitfield);
  407. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  408. util::toHex(destBitfield.getBitfield(),
  409. destBitfield.getBitfieldLength()));
  410. }
  411. void UtilTest::testParseIntRange()
  412. {
  413. IntSequence seq = util::parseIntRange("1,3-8,10");
  414. CPPUNIT_ASSERT(seq.hasNext());
  415. CPPUNIT_ASSERT_EQUAL((int32_t)1, seq.next());
  416. CPPUNIT_ASSERT(seq.hasNext());
  417. CPPUNIT_ASSERT_EQUAL((int32_t)3, seq.next());
  418. CPPUNIT_ASSERT(seq.hasNext());
  419. CPPUNIT_ASSERT_EQUAL((int32_t)4, seq.next());
  420. CPPUNIT_ASSERT(seq.hasNext());
  421. CPPUNIT_ASSERT_EQUAL((int32_t)5, seq.next());
  422. CPPUNIT_ASSERT(seq.hasNext());
  423. CPPUNIT_ASSERT_EQUAL((int32_t)6, seq.next());
  424. CPPUNIT_ASSERT(seq.hasNext());
  425. CPPUNIT_ASSERT_EQUAL((int32_t)7, seq.next());
  426. CPPUNIT_ASSERT(seq.hasNext());
  427. CPPUNIT_ASSERT_EQUAL((int32_t)8, seq.next());
  428. CPPUNIT_ASSERT(seq.hasNext());
  429. CPPUNIT_ASSERT_EQUAL((int32_t)10, seq.next());
  430. CPPUNIT_ASSERT(!seq.hasNext());
  431. CPPUNIT_ASSERT_EQUAL((int32_t)0, seq.next());
  432. }
  433. void UtilTest::testParseIntRange_invalidRange()
  434. {
  435. try {
  436. IntSequence seq = util::parseIntRange("-1");
  437. CPPUNIT_FAIL("exception must be thrown.");
  438. } catch(Exception& e) {
  439. std::cerr << e.stackTrace();
  440. }
  441. try {
  442. IntSequence seq = util::parseIntRange("2147483648");
  443. CPPUNIT_FAIL("exception must be thrown.");
  444. } catch(Exception& e) {
  445. std::cerr << e.stackTrace();
  446. }
  447. try {
  448. IntSequence seq = util::parseIntRange("2147483647-2147483648");
  449. CPPUNIT_FAIL("exception must be thrown.");
  450. } catch(Exception& e) {
  451. std::cerr << e.stackTrace();
  452. }
  453. try {
  454. IntSequence seq = util::parseIntRange("1-2x");
  455. CPPUNIT_FAIL("exception must be thrown.");
  456. } catch(Exception& e) {
  457. std::cerr << e.stackTrace();
  458. }
  459. try {
  460. IntSequence seq = util::parseIntRange("3x-4");
  461. CPPUNIT_FAIL("exception must be thrown.");
  462. } catch(Exception& e) {
  463. std::cerr << e.stackTrace();
  464. }
  465. }
  466. void UtilTest::testParseInt()
  467. {
  468. CPPUNIT_ASSERT_EQUAL(-1, util::parseInt(" -1 "));
  469. CPPUNIT_ASSERT_EQUAL(2147483647, util::parseInt("2147483647"));
  470. try {
  471. util::parseInt("2147483648");
  472. CPPUNIT_FAIL("exception must be thrown.");
  473. } catch(Exception& e) {
  474. std::cerr << e.stackTrace();
  475. }
  476. try {
  477. util::parseInt("-2147483649");
  478. CPPUNIT_FAIL("exception must be thrown.");
  479. } catch(Exception& e) {
  480. std::cerr << e.stackTrace();
  481. }
  482. try {
  483. util::parseInt("12x");
  484. CPPUNIT_FAIL("exception must be thrown.");
  485. } catch(Exception& e) {
  486. std::cerr << e.stackTrace();
  487. }
  488. try {
  489. util::parseInt("");
  490. CPPUNIT_FAIL("exception must be thrown.");
  491. } catch(Exception& e) {
  492. std::cerr << e.stackTrace();
  493. }
  494. }
  495. void UtilTest::testParseUInt()
  496. {
  497. CPPUNIT_ASSERT_EQUAL(4294967295U, util::parseUInt(" 4294967295 "));
  498. try {
  499. util::parseUInt("-1");
  500. CPPUNIT_FAIL("exception must be thrown.");
  501. } catch(Exception& e) {
  502. std::cerr << e.stackTrace();
  503. }
  504. try {
  505. util::parseUInt("4294967296");
  506. CPPUNIT_FAIL("exception must be thrown.");
  507. } catch(Exception& e) {
  508. std::cerr << e.stackTrace();
  509. }
  510. }
  511. void UtilTest::testParseLLInt()
  512. {
  513. CPPUNIT_ASSERT_EQUAL((int64_t)-1LL, util::parseLLInt(" -1 "));
  514. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
  515. util::parseLLInt("9223372036854775807"));
  516. try {
  517. util::parseLLInt("9223372036854775808");
  518. CPPUNIT_FAIL("exception must be thrown.");
  519. } catch(Exception& e) {
  520. std::cerr << e.stackTrace();
  521. }
  522. try {
  523. util::parseLLInt("-9223372036854775809");
  524. CPPUNIT_FAIL("exception must be thrown.");
  525. } catch(Exception& e) {
  526. std::cerr << e.stackTrace();
  527. }
  528. try {
  529. util::parseLLInt("12x");
  530. CPPUNIT_FAIL("exception must be thrown.");
  531. } catch(Exception& e) {
  532. std::cerr << e.stackTrace();
  533. }
  534. try {
  535. util::parseLLInt("");
  536. CPPUNIT_FAIL("exception must be thrown.");
  537. } catch(Exception& e) {
  538. std::cerr << e.stackTrace();
  539. }
  540. }
  541. void UtilTest::testParseULLInt()
  542. {
  543. CPPUNIT_ASSERT_EQUAL((uint64_t)18446744073709551615ULL,
  544. util::parseULLInt("18446744073709551615"));
  545. try {
  546. util::parseUInt("-1");
  547. CPPUNIT_FAIL("exception must be thrown.");
  548. } catch(Exception& e) {
  549. std::cerr << e.stackTrace();
  550. }
  551. try {
  552. util::parseLLInt("18446744073709551616");
  553. CPPUNIT_FAIL("exception must be thrown.");
  554. } catch(Exception& e) {
  555. std::cerr << e.stackTrace();
  556. }
  557. }
  558. void UtilTest::testToString_binaryStream()
  559. {
  560. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  561. std::string data(16*1024+256, 'a');
  562. dw->initAndOpenFile();
  563. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  564. std::string readData = util::toString(dw);
  565. CPPUNIT_ASSERT_EQUAL(data, readData);
  566. }
  567. void UtilTest::testItos()
  568. {
  569. {
  570. int i = 0;
  571. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::itos(i));
  572. }
  573. {
  574. int i = 100;
  575. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i, true));
  576. }
  577. {
  578. int i = 100;
  579. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i));
  580. }
  581. {
  582. int i = 12345;
  583. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), util::itos(i, true));
  584. }
  585. {
  586. int i = 12345;
  587. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::itos(i));
  588. }
  589. {
  590. int i = -12345;
  591. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), util::itos(i, true));
  592. }
  593. {
  594. int64_t i = INT64_MAX;
  595. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"), util::itos(i, true));
  596. }
  597. }
  598. void UtilTest::testUitos()
  599. {
  600. {
  601. uint16_t i = 12345;
  602. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::uitos(i));
  603. }
  604. {
  605. int16_t i = -12345;
  606. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), util::uitos(i));
  607. }
  608. }
  609. void UtilTest::testNtoh64()
  610. {
  611. uint64_t x = 0xff00ff00ee00ee00LL;
  612. #ifdef WORDS_BIGENDIAN
  613. CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
  614. CPPUNIT_ASSERT_EQUAL(x, hton64(x));
  615. #else // !WORDS_BIGENDIAN
  616. uint64_t y = 0x00ee00ee00ff00ffLL;
  617. CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
  618. CPPUNIT_ASSERT_EQUAL(x, hton64(y));
  619. #endif // !WORDS_BIGENDIAN
  620. }
  621. void UtilTest::testUrlencode()
  622. {
  623. CPPUNIT_ASSERT_EQUAL
  624. (std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
  625. util::urlencode(":/?#[]@!%&'()*+,;="));
  626. std::string unreserved =
  627. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  628. "abcdefghijklmnopqrstuvwxyz"
  629. "0123456789"
  630. "-._~";
  631. CPPUNIT_ASSERT_EQUAL(unreserved, util::urlencode(unreserved));
  632. CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::urlencode("1^A "));
  633. }
  634. void UtilTest::testHtmlEscape()
  635. {
  636. CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),
  637. util::htmlEscape("aria2<>\"'util"));
  638. }
  639. void UtilTest::testJoinPath()
  640. {
  641. const std::string dir1dir2file[] = { "dir1", "dir2", "file" };
  642. CPPUNIT_ASSERT_EQUAL
  643. (std::string("dir1/dir2/file"),
  644. util::joinPath(&dir1dir2file[0],
  645. &dir1dir2file[arrayLength(dir1dir2file)]));
  646. const std::string dirparentfile[] = { "dir", "..", "file" };
  647. CPPUNIT_ASSERT_EQUAL
  648. (std::string("file"),
  649. util::joinPath(&dirparentfile[0],
  650. &dirparentfile[arrayLength(dirparentfile)]));
  651. const std::string dirparentparentfile[] = { "dir", "..", "..", "file" };
  652. CPPUNIT_ASSERT_EQUAL
  653. (std::string("file"),
  654. util::joinPath(&dirparentparentfile[0],
  655. &dirparentparentfile[arrayLength(dirparentparentfile)]));
  656. const std::string dirdotfile[] = { "dir", ".", "file" };
  657. CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
  658. util::joinPath(&dirdotfile[0],
  659. &dirdotfile[arrayLength(dirdotfile)]));
  660. const std::string empty[] = {};
  661. CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(&empty[0], &empty[0]));
  662. const std::string parentdot[] = { "..", "." };
  663. CPPUNIT_ASSERT_EQUAL(std::string(""),
  664. util::joinPath(&parentdot[0],
  665. &parentdot[arrayLength(parentdot)]));
  666. }
  667. void UtilTest::testParseIndexPath()
  668. {
  669. std::map<size_t, std::string>::value_type p = util::parseIndexPath("1=foo");
  670. CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
  671. CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
  672. try {
  673. util::parseIndexPath("1X=foo");
  674. CPPUNIT_FAIL("exception must be thrown.");
  675. } catch(Exception& e) {
  676. // success
  677. }
  678. try {
  679. util::parseIndexPath("1=");
  680. CPPUNIT_FAIL("exception must be thrown.");
  681. } catch(Exception& e) {
  682. // success
  683. }
  684. }
  685. void UtilTest::testCreateIndexPathMap()
  686. {
  687. std::stringstream in
  688. ("1=/tmp/myfile\n"
  689. "100=/myhome/mypicture.png\n");
  690. std::map<size_t, std::string> m = util::createIndexPathMap(in);
  691. CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
  692. CPPUNIT_ASSERT(m.find(1) != m.end());
  693. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[1]);
  694. CPPUNIT_ASSERT(m.find(100) != m.end());
  695. CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[100]);
  696. }
  697. void UtilTest::testGenerateRandomData()
  698. {
  699. unsigned char data1[20];
  700. util::generateRandomData(data1, sizeof(data1));
  701. unsigned char data2[20];
  702. util::generateRandomData(data2, sizeof(data2));
  703. CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
  704. }
  705. } // namespace aria2