UtilTest.cc 21 KB

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