UtilTest.cc 24 KB

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