UtilTest.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. std::string filenameWithDir = "attachment; filename=dir/file";
  228. CPPUNIT_ASSERT_EQUAL(std::string("file"),
  229. Util::getContentDispositionFilename(filenameWithDir));
  230. std::string parentDir = "attachment; filename=..";
  231. CPPUNIT_ASSERT_EQUAL(std::string(),
  232. Util::getContentDispositionFilename(parentDir));
  233. std::string currentDir = "attachment; filename=.";
  234. CPPUNIT_ASSERT_EQUAL(std::string(),
  235. Util::getContentDispositionFilename(currentDir));
  236. }
  237. class Printer {
  238. public:
  239. template<class T>
  240. void operator()(T t) {
  241. std::cerr << t << ", ";
  242. }
  243. };
  244. void UtilTest::testRandomAlpha() {
  245. SharedHandle<Randomizer> rand(new FixedNumberRandomizer());
  246. std::string s = Util::randomAlpha(8, rand);
  247. CPPUNIT_ASSERT_EQUAL(std::string("AAAAAAAA"), s);
  248. }
  249. void UtilTest::testToUpper() {
  250. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  251. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  252. CPPUNIT_ASSERT_EQUAL(upp, Util::toUpper(src));
  253. }
  254. void UtilTest::testToLower() {
  255. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  256. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  257. CPPUNIT_ASSERT_EQUAL(upp, Util::toLower(src));
  258. }
  259. void UtilTest::testUrldecode() {
  260. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  261. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  262. Util::urldecode(src));
  263. std::string src2 = "aria2+aria2";
  264. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"), Util::urldecode(src2));
  265. std::string src3 = "%5t%20";
  266. CPPUNIT_ASSERT_EQUAL(std::string("%5t "), Util::urldecode(src3));
  267. std::string src4 = "%";
  268. CPPUNIT_ASSERT_EQUAL(std::string("%"), Util::urldecode(src4));
  269. std::string src5 = "%3";
  270. CPPUNIT_ASSERT_EQUAL(std::string("%3"), Util::urldecode(src5));
  271. std::string src6 = "%2f";
  272. CPPUNIT_ASSERT_EQUAL(std::string("/"), Util::urldecode(src6));
  273. }
  274. void UtilTest::testCountBit() {
  275. CPPUNIT_ASSERT_EQUAL((unsigned int)32, Util::countBit(UINT32_MAX));
  276. CPPUNIT_ASSERT_EQUAL((unsigned int)8, Util::countBit(255));
  277. }
  278. void UtilTest::testGetRealSize()
  279. {
  280. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, Util::getRealSize("4096M"));
  281. CPPUNIT_ASSERT_EQUAL((int64_t)1024, Util::getRealSize("1K"));
  282. try {
  283. Util::getRealSize("");
  284. CPPUNIT_FAIL("exception must be thrown.");
  285. } catch(Exception& e) {
  286. std::cerr << e.stackTrace();
  287. }
  288. try {
  289. Util::getRealSize("foo");
  290. CPPUNIT_FAIL("exception must be thrown.");
  291. } catch(Exception& e) {
  292. std::cerr << e.stackTrace();
  293. }
  294. try {
  295. Util::getRealSize("-1");
  296. CPPUNIT_FAIL("exception must be thrown.");
  297. } catch(Exception& e) {
  298. std::cerr << e.stackTrace();
  299. }
  300. try {
  301. Util::getRealSize("9223372036854775807K");
  302. CPPUNIT_FAIL("exception must be thrown.");
  303. } catch(Exception& e) {
  304. std::cerr << e.stackTrace();
  305. }
  306. try {
  307. Util::getRealSize("9223372036854775807M");
  308. CPPUNIT_FAIL("exception must be thrown.");
  309. } catch(Exception& e) {
  310. std::cerr << e.stackTrace();
  311. }
  312. }
  313. void UtilTest::testAbbrevSize()
  314. {
  315. CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), Util::abbrevSize(4294967296LL));
  316. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), Util::abbrevSize(1024));
  317. CPPUNIT_ASSERT_EQUAL(std::string("1,023"), Util::abbrevSize(1023));
  318. CPPUNIT_ASSERT_EQUAL(std::string("0"), Util::abbrevSize(0));
  319. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), Util::abbrevSize(1127));
  320. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), Util::abbrevSize(1572864));
  321. }
  322. void UtilTest::testToStream()
  323. {
  324. std::ostringstream os;
  325. SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  326. SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  327. std::deque<SharedHandle<FileEntry> > entries;
  328. entries.push_back(f1);
  329. entries.push_back(f2);
  330. Util::toStream(os, entries);
  331. CPPUNIT_ASSERT_EQUAL(
  332. std::string("Files:\n"
  333. "idx|path/length\n"
  334. "===+===========================================================================\n"
  335. " 1|aria2.tar.bz2\n"
  336. " |12.0KiB\n"
  337. "---+---------------------------------------------------------------------------\n"
  338. " 2|aria2.txt\n"
  339. " |556B\n"
  340. "---+---------------------------------------------------------------------------\n"),
  341. os.str());
  342. }
  343. void UtilTest::testIsNumber()
  344. {
  345. CPPUNIT_ASSERT_EQUAL(true, Util::isNumber("000"));
  346. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("a"));
  347. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("0a"));
  348. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(""));
  349. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(" "));
  350. }
  351. void UtilTest::testIsLowercase()
  352. {
  353. CPPUNIT_ASSERT_EQUAL(true, Util::isLowercase("alpha"));
  354. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("Alpha"));
  355. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("1alpha"));
  356. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(""));
  357. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(" "));
  358. }
  359. void UtilTest::testIsUppercase()
  360. {
  361. CPPUNIT_ASSERT_EQUAL(true, Util::isUppercase("ALPHA"));
  362. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("Alpha"));
  363. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("1ALPHA"));
  364. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(""));
  365. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(" "));
  366. }
  367. void UtilTest::testAlphaToNum()
  368. {
  369. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("a"));
  370. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("aa"));
  371. CPPUNIT_ASSERT_EQUAL(1U, Util::alphaToNum("b"));
  372. CPPUNIT_ASSERT_EQUAL(675U, Util::alphaToNum("zz")); // 25*26+25
  373. CPPUNIT_ASSERT_EQUAL(675U, Util::alphaToNum("ZZ")); // 25*26+25
  374. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum(""));
  375. CPPUNIT_ASSERT_EQUAL(4294967295U, Util::alphaToNum("NXMRLXV"));
  376. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("NXMRLXW")); // uint32_t overflow
  377. }
  378. void UtilTest::testMkdirs()
  379. {
  380. std::string dir = "/tmp/aria2-UtilTest-testMkdirs";
  381. File d(dir);
  382. if(d.exists()) {
  383. CPPUNIT_ASSERT(d.remove());
  384. }
  385. CPPUNIT_ASSERT(!d.exists());
  386. Util::mkdirs(dir);
  387. CPPUNIT_ASSERT(d.isDir());
  388. std::string file = "./UtilTest.cc";
  389. File f(file);
  390. CPPUNIT_ASSERT(f.isFile());
  391. try {
  392. Util::mkdirs(file);
  393. CPPUNIT_FAIL("exception must be thrown.");
  394. } catch(DlAbortEx& ex) {
  395. std::cerr << ex.stackTrace() << std::endl;
  396. }
  397. }
  398. void UtilTest::testConvertBitfield()
  399. {
  400. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  401. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  402. srcBitfield.setAllBit();
  403. srcBitfield.unsetBit(2);// <- range [768, 1152)
  404. // which corresponds to the index [1,2] in destBitfield
  405. Util::convertBitfield(&destBitfield, &srcBitfield);
  406. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  407. Util::toHex(destBitfield.getBitfield(),
  408. destBitfield.getBitfieldLength()));
  409. }
  410. void UtilTest::testParseIntRange()
  411. {
  412. IntSequence seq = Util::parseIntRange("1,3-8,10");
  413. CPPUNIT_ASSERT(seq.hasNext());
  414. CPPUNIT_ASSERT_EQUAL((int32_t)1, seq.next());
  415. CPPUNIT_ASSERT(seq.hasNext());
  416. CPPUNIT_ASSERT_EQUAL((int32_t)3, seq.next());
  417. CPPUNIT_ASSERT(seq.hasNext());
  418. CPPUNIT_ASSERT_EQUAL((int32_t)4, seq.next());
  419. CPPUNIT_ASSERT(seq.hasNext());
  420. CPPUNIT_ASSERT_EQUAL((int32_t)5, seq.next());
  421. CPPUNIT_ASSERT(seq.hasNext());
  422. CPPUNIT_ASSERT_EQUAL((int32_t)6, seq.next());
  423. CPPUNIT_ASSERT(seq.hasNext());
  424. CPPUNIT_ASSERT_EQUAL((int32_t)7, seq.next());
  425. CPPUNIT_ASSERT(seq.hasNext());
  426. CPPUNIT_ASSERT_EQUAL((int32_t)8, seq.next());
  427. CPPUNIT_ASSERT(seq.hasNext());
  428. CPPUNIT_ASSERT_EQUAL((int32_t)10, seq.next());
  429. CPPUNIT_ASSERT(!seq.hasNext());
  430. CPPUNIT_ASSERT_EQUAL((int32_t)0, seq.next());
  431. }
  432. void UtilTest::testParseIntRange_invalidRange()
  433. {
  434. try {
  435. IntSequence seq = Util::parseIntRange("-1");
  436. CPPUNIT_FAIL("exception must be thrown.");
  437. } catch(Exception& e) {
  438. std::cerr << e.stackTrace();
  439. }
  440. try {
  441. IntSequence seq = Util::parseIntRange("2147483648");
  442. CPPUNIT_FAIL("exception must be thrown.");
  443. } catch(Exception& e) {
  444. std::cerr << e.stackTrace();
  445. }
  446. try {
  447. IntSequence seq = Util::parseIntRange("2147483647-2147483648");
  448. CPPUNIT_FAIL("exception must be thrown.");
  449. } catch(Exception& e) {
  450. std::cerr << e.stackTrace();
  451. }
  452. try {
  453. IntSequence seq = Util::parseIntRange("1-2x");
  454. CPPUNIT_FAIL("exception must be thrown.");
  455. } catch(Exception& e) {
  456. std::cerr << e.stackTrace();
  457. }
  458. try {
  459. IntSequence seq = Util::parseIntRange("3x-4");
  460. CPPUNIT_FAIL("exception must be thrown.");
  461. } catch(Exception& e) {
  462. std::cerr << e.stackTrace();
  463. }
  464. }
  465. void UtilTest::testParseInt()
  466. {
  467. CPPUNIT_ASSERT_EQUAL(-1, Util::parseInt(" -1 "));
  468. CPPUNIT_ASSERT_EQUAL(2147483647, Util::parseInt("2147483647"));
  469. try {
  470. Util::parseInt("2147483648");
  471. CPPUNIT_FAIL("exception must be thrown.");
  472. } catch(Exception& e) {
  473. std::cerr << e.stackTrace();
  474. }
  475. try {
  476. Util::parseInt("-2147483649");
  477. CPPUNIT_FAIL("exception must be thrown.");
  478. } catch(Exception& e) {
  479. std::cerr << e.stackTrace();
  480. }
  481. try {
  482. Util::parseInt("12x");
  483. CPPUNIT_FAIL("exception must be thrown.");
  484. } catch(Exception& e) {
  485. std::cerr << e.stackTrace();
  486. }
  487. try {
  488. Util::parseInt("");
  489. CPPUNIT_FAIL("exception must be thrown.");
  490. } catch(Exception& e) {
  491. std::cerr << e.stackTrace();
  492. }
  493. }
  494. void UtilTest::testParseUInt()
  495. {
  496. CPPUNIT_ASSERT_EQUAL(4294967295U, Util::parseUInt(" 4294967295 "));
  497. try {
  498. Util::parseUInt("-1");
  499. CPPUNIT_FAIL("exception must be thrown.");
  500. } catch(Exception& e) {
  501. std::cerr << e.stackTrace();
  502. }
  503. try {
  504. Util::parseUInt("4294967296");
  505. CPPUNIT_FAIL("exception must be thrown.");
  506. } catch(Exception& e) {
  507. std::cerr << e.stackTrace();
  508. }
  509. }
  510. void UtilTest::testParseLLInt()
  511. {
  512. CPPUNIT_ASSERT_EQUAL((int64_t)-1LL, Util::parseLLInt(" -1 "));
  513. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
  514. Util::parseLLInt("9223372036854775807"));
  515. try {
  516. Util::parseLLInt("9223372036854775808");
  517. CPPUNIT_FAIL("exception must be thrown.");
  518. } catch(Exception& e) {
  519. std::cerr << e.stackTrace();
  520. }
  521. try {
  522. Util::parseLLInt("-9223372036854775809");
  523. CPPUNIT_FAIL("exception must be thrown.");
  524. } catch(Exception& e) {
  525. std::cerr << e.stackTrace();
  526. }
  527. try {
  528. Util::parseLLInt("12x");
  529. CPPUNIT_FAIL("exception must be thrown.");
  530. } catch(Exception& e) {
  531. std::cerr << e.stackTrace();
  532. }
  533. try {
  534. Util::parseLLInt("");
  535. CPPUNIT_FAIL("exception must be thrown.");
  536. } catch(Exception& e) {
  537. std::cerr << e.stackTrace();
  538. }
  539. }
  540. void UtilTest::testParseULLInt()
  541. {
  542. CPPUNIT_ASSERT_EQUAL((uint64_t)18446744073709551615ULL,
  543. Util::parseULLInt("18446744073709551615"));
  544. try {
  545. Util::parseUInt("-1");
  546. CPPUNIT_FAIL("exception must be thrown.");
  547. } catch(Exception& e) {
  548. std::cerr << e.stackTrace();
  549. }
  550. try {
  551. Util::parseLLInt("18446744073709551616");
  552. CPPUNIT_FAIL("exception must be thrown.");
  553. } catch(Exception& e) {
  554. std::cerr << e.stackTrace();
  555. }
  556. }
  557. void UtilTest::testToString_binaryStream()
  558. {
  559. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  560. std::string data(16*1024+256, 'a');
  561. dw->initAndOpenFile("dummy");
  562. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  563. std::string readData = Util::toString(dw);
  564. CPPUNIT_ASSERT_EQUAL(data, readData);
  565. }
  566. void UtilTest::testItos()
  567. {
  568. {
  569. int i = 0;
  570. CPPUNIT_ASSERT_EQUAL(std::string("0"), Util::itos(i));
  571. }
  572. {
  573. int i = 100;
  574. CPPUNIT_ASSERT_EQUAL(std::string("100"), Util::itos(i, true));
  575. }
  576. {
  577. int i = 100;
  578. CPPUNIT_ASSERT_EQUAL(std::string("100"), Util::itos(i));
  579. }
  580. {
  581. int i = 12345;
  582. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), Util::itos(i, true));
  583. }
  584. {
  585. int i = 12345;
  586. CPPUNIT_ASSERT_EQUAL(std::string("12345"), Util::itos(i));
  587. }
  588. {
  589. int i = -12345;
  590. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), Util::itos(i, true));
  591. }
  592. {
  593. int64_t i = INT64_MAX;
  594. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"), Util::itos(i, true));
  595. }
  596. }
  597. void UtilTest::testUitos()
  598. {
  599. {
  600. uint16_t i = 12345;
  601. CPPUNIT_ASSERT_EQUAL(std::string("12345"), Util::uitos(i));
  602. }
  603. {
  604. int16_t i = -12345;
  605. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), Util::uitos(i));
  606. }
  607. }
  608. void UtilTest::testHttpGMT()
  609. {
  610. CPPUNIT_ASSERT_EQUAL((time_t)0, Util::httpGMT("Thu, 1970-01-01 0:0:0 GMT"));
  611. CPPUNIT_ASSERT_EQUAL((time_t)2147483647,
  612. Util::httpGMT("Tue, 2038-01-19 3:14:7 GMT"));
  613. if(sizeof(time_t) == 4) {
  614. CPPUNIT_ASSERT_EQUAL((time_t)2147483647,
  615. Util::httpGMT("Tue, 2038-01-19 3:14:8 GMT"));
  616. } else if(sizeof(time_t) == 8) {
  617. CPPUNIT_ASSERT_EQUAL((int64_t)2147483648LL,
  618. (int64_t)Util::httpGMT("Tue, 2038-01-19 3:14:8 GMT"));
  619. }
  620. CPPUNIT_ASSERT_EQUAL((time_t)-1,
  621. Util::httpGMT("Tue, 2008/10/10 23:33:33 UTC"));
  622. }
  623. void UtilTest::testNtoh64()
  624. {
  625. uint64_t x = 0xff00ff00ee00ee00LL;
  626. #ifdef WORDS_BIGENDIAN
  627. CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
  628. CPPUNIT_ASSERT_EQUAL(x, hton64(x));
  629. #else // !WORDS_BIGENDIAN
  630. uint64_t y = 0x00ee00ee00ff00ffLL;
  631. CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
  632. CPPUNIT_ASSERT_EQUAL(x, hton64(y));
  633. #endif // !WORDS_BIGENDIAN
  634. }
  635. void UtilTest::testUrlencode()
  636. {
  637. CPPUNIT_ASSERT_EQUAL
  638. (std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
  639. Util::urlencode(":/?#[]@!%&'()*+,;="));
  640. std::string unreserved =
  641. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  642. "abcdefghijklmnopqrstuvwxyz"
  643. "0123456789"
  644. "-._~";
  645. CPPUNIT_ASSERT_EQUAL(unreserved, Util::urlencode(unreserved));
  646. CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), Util::urlencode("1^A "));
  647. }
  648. void UtilTest::testHtmlEscape()
  649. {
  650. CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),
  651. Util::htmlEscape("aria2<>\"'util"));
  652. }
  653. } // namespace aria2