UtilTest.cc 23 KB

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