UtilTest.cc 23 KB

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