UtilTest.cc 21 KB

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