UtilTest.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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(testCountBit);
  28. CPPUNIT_TEST(testGetRealSize);
  29. CPPUNIT_TEST(testAbbrevSize);
  30. CPPUNIT_TEST(testToStream);
  31. CPPUNIT_TEST(testIsNumber);
  32. CPPUNIT_TEST(testIsLowercase);
  33. CPPUNIT_TEST(testIsUppercase);
  34. CPPUNIT_TEST(testAlphaToNum);
  35. CPPUNIT_TEST(testMkdirs);
  36. CPPUNIT_TEST(testConvertBitfield);
  37. CPPUNIT_TEST(testParseIntRange);
  38. CPPUNIT_TEST(testParseIntRange_invalidRange);
  39. CPPUNIT_TEST(testParseInt);
  40. CPPUNIT_TEST(testParseUInt);
  41. CPPUNIT_TEST(testParseLLInt);
  42. CPPUNIT_TEST(testParseULLInt);
  43. CPPUNIT_TEST(testToString_binaryStream);
  44. CPPUNIT_TEST(testItos);
  45. CPPUNIT_TEST(testUitos);
  46. CPPUNIT_TEST(testHttpGMT);
  47. CPPUNIT_TEST(testNtoh64);
  48. CPPUNIT_TEST(testUrlencode);
  49. CPPUNIT_TEST(testHtmlEscape);
  50. CPPUNIT_TEST(testJoinPath);
  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 testCountBit();
  69. void testGetRealSize();
  70. void testAbbrevSize();
  71. void testToStream();
  72. void testIsNumber();
  73. void testIsLowercase();
  74. void testIsUppercase();
  75. void testAlphaToNum();
  76. void testMkdirs();
  77. void testConvertBitfield();
  78. void testParseIntRange();
  79. void testParseIntRange_invalidRange();
  80. void testParseInt();
  81. void testParseUInt();
  82. void testParseLLInt();
  83. void testParseULLInt();
  84. void testToString_binaryStream();
  85. void testItos();
  86. void testUitos();
  87. void testHttpGMT();
  88. void testNtoh64();
  89. void testUrlencode();
  90. void testHtmlEscape();
  91. void testJoinPath();
  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::testCountBit() {
  278. CPPUNIT_ASSERT_EQUAL((unsigned int)32, Util::countBit(UINT32_MAX));
  279. CPPUNIT_ASSERT_EQUAL((unsigned int)8, Util::countBit(255));
  280. }
  281. void UtilTest::testGetRealSize()
  282. {
  283. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, Util::getRealSize("4096M"));
  284. CPPUNIT_ASSERT_EQUAL((int64_t)1024, Util::getRealSize("1K"));
  285. try {
  286. Util::getRealSize("");
  287. CPPUNIT_FAIL("exception must be thrown.");
  288. } catch(Exception& e) {
  289. std::cerr << e.stackTrace();
  290. }
  291. try {
  292. Util::getRealSize("foo");
  293. CPPUNIT_FAIL("exception must be thrown.");
  294. } catch(Exception& e) {
  295. std::cerr << e.stackTrace();
  296. }
  297. try {
  298. Util::getRealSize("-1");
  299. CPPUNIT_FAIL("exception must be thrown.");
  300. } catch(Exception& e) {
  301. std::cerr << e.stackTrace();
  302. }
  303. try {
  304. Util::getRealSize("9223372036854775807K");
  305. CPPUNIT_FAIL("exception must be thrown.");
  306. } catch(Exception& e) {
  307. std::cerr << e.stackTrace();
  308. }
  309. try {
  310. Util::getRealSize("9223372036854775807M");
  311. CPPUNIT_FAIL("exception must be thrown.");
  312. } catch(Exception& e) {
  313. std::cerr << e.stackTrace();
  314. }
  315. }
  316. void UtilTest::testAbbrevSize()
  317. {
  318. CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), Util::abbrevSize(4294967296LL));
  319. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), Util::abbrevSize(1024));
  320. CPPUNIT_ASSERT_EQUAL(std::string("1,023"), Util::abbrevSize(1023));
  321. CPPUNIT_ASSERT_EQUAL(std::string("0"), Util::abbrevSize(0));
  322. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), Util::abbrevSize(1127));
  323. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), Util::abbrevSize(1572864));
  324. }
  325. void UtilTest::testToStream()
  326. {
  327. std::ostringstream os;
  328. SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  329. SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  330. std::deque<SharedHandle<FileEntry> > entries;
  331. entries.push_back(f1);
  332. entries.push_back(f2);
  333. Util::toStream(os, entries);
  334. CPPUNIT_ASSERT_EQUAL(
  335. std::string("Files:\n"
  336. "idx|path/length\n"
  337. "===+===========================================================================\n"
  338. " 1|aria2.tar.bz2\n"
  339. " |12.0KiB\n"
  340. "---+---------------------------------------------------------------------------\n"
  341. " 2|aria2.txt\n"
  342. " |556B\n"
  343. "---+---------------------------------------------------------------------------\n"),
  344. os.str());
  345. }
  346. void UtilTest::testIsNumber()
  347. {
  348. CPPUNIT_ASSERT_EQUAL(true, Util::isNumber("000"));
  349. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("a"));
  350. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("0a"));
  351. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(""));
  352. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(" "));
  353. }
  354. void UtilTest::testIsLowercase()
  355. {
  356. CPPUNIT_ASSERT_EQUAL(true, Util::isLowercase("alpha"));
  357. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("Alpha"));
  358. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("1alpha"));
  359. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(""));
  360. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(" "));
  361. }
  362. void UtilTest::testIsUppercase()
  363. {
  364. CPPUNIT_ASSERT_EQUAL(true, Util::isUppercase("ALPHA"));
  365. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("Alpha"));
  366. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("1ALPHA"));
  367. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(""));
  368. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(" "));
  369. }
  370. void UtilTest::testAlphaToNum()
  371. {
  372. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("a"));
  373. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("aa"));
  374. CPPUNIT_ASSERT_EQUAL(1U, Util::alphaToNum("b"));
  375. CPPUNIT_ASSERT_EQUAL(675U, Util::alphaToNum("zz")); // 25*26+25
  376. CPPUNIT_ASSERT_EQUAL(675U, Util::alphaToNum("ZZ")); // 25*26+25
  377. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum(""));
  378. CPPUNIT_ASSERT_EQUAL(4294967295U, Util::alphaToNum("NXMRLXV"));
  379. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("NXMRLXW")); // uint32_t overflow
  380. }
  381. void UtilTest::testMkdirs()
  382. {
  383. std::string dir = "/tmp/aria2-UtilTest-testMkdirs";
  384. File d(dir);
  385. if(d.exists()) {
  386. CPPUNIT_ASSERT(d.remove());
  387. }
  388. CPPUNIT_ASSERT(!d.exists());
  389. Util::mkdirs(dir);
  390. CPPUNIT_ASSERT(d.isDir());
  391. std::string file = "./UtilTest.cc";
  392. File f(file);
  393. CPPUNIT_ASSERT(f.isFile());
  394. try {
  395. Util::mkdirs(file);
  396. CPPUNIT_FAIL("exception must be thrown.");
  397. } catch(DlAbortEx& ex) {
  398. std::cerr << ex.stackTrace() << std::endl;
  399. }
  400. }
  401. void UtilTest::testConvertBitfield()
  402. {
  403. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  404. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  405. srcBitfield.setAllBit();
  406. srcBitfield.unsetBit(2);// <- range [768, 1152)
  407. // which corresponds to the index [1,2] in destBitfield
  408. Util::convertBitfield(&destBitfield, &srcBitfield);
  409. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  410. Util::toHex(destBitfield.getBitfield(),
  411. destBitfield.getBitfieldLength()));
  412. }
  413. void UtilTest::testParseIntRange()
  414. {
  415. IntSequence seq = Util::parseIntRange("1,3-8,10");
  416. CPPUNIT_ASSERT(seq.hasNext());
  417. CPPUNIT_ASSERT_EQUAL((int32_t)1, seq.next());
  418. CPPUNIT_ASSERT(seq.hasNext());
  419. CPPUNIT_ASSERT_EQUAL((int32_t)3, seq.next());
  420. CPPUNIT_ASSERT(seq.hasNext());
  421. CPPUNIT_ASSERT_EQUAL((int32_t)4, seq.next());
  422. CPPUNIT_ASSERT(seq.hasNext());
  423. CPPUNIT_ASSERT_EQUAL((int32_t)5, seq.next());
  424. CPPUNIT_ASSERT(seq.hasNext());
  425. CPPUNIT_ASSERT_EQUAL((int32_t)6, seq.next());
  426. CPPUNIT_ASSERT(seq.hasNext());
  427. CPPUNIT_ASSERT_EQUAL((int32_t)7, seq.next());
  428. CPPUNIT_ASSERT(seq.hasNext());
  429. CPPUNIT_ASSERT_EQUAL((int32_t)8, seq.next());
  430. CPPUNIT_ASSERT(seq.hasNext());
  431. CPPUNIT_ASSERT_EQUAL((int32_t)10, seq.next());
  432. CPPUNIT_ASSERT(!seq.hasNext());
  433. CPPUNIT_ASSERT_EQUAL((int32_t)0, seq.next());
  434. }
  435. void UtilTest::testParseIntRange_invalidRange()
  436. {
  437. try {
  438. IntSequence seq = Util::parseIntRange("-1");
  439. CPPUNIT_FAIL("exception must be thrown.");
  440. } catch(Exception& e) {
  441. std::cerr << e.stackTrace();
  442. }
  443. try {
  444. IntSequence seq = Util::parseIntRange("2147483648");
  445. CPPUNIT_FAIL("exception must be thrown.");
  446. } catch(Exception& e) {
  447. std::cerr << e.stackTrace();
  448. }
  449. try {
  450. IntSequence seq = Util::parseIntRange("2147483647-2147483648");
  451. CPPUNIT_FAIL("exception must be thrown.");
  452. } catch(Exception& e) {
  453. std::cerr << e.stackTrace();
  454. }
  455. try {
  456. IntSequence seq = Util::parseIntRange("1-2x");
  457. CPPUNIT_FAIL("exception must be thrown.");
  458. } catch(Exception& e) {
  459. std::cerr << e.stackTrace();
  460. }
  461. try {
  462. IntSequence seq = Util::parseIntRange("3x-4");
  463. CPPUNIT_FAIL("exception must be thrown.");
  464. } catch(Exception& e) {
  465. std::cerr << e.stackTrace();
  466. }
  467. }
  468. void UtilTest::testParseInt()
  469. {
  470. CPPUNIT_ASSERT_EQUAL(-1, Util::parseInt(" -1 "));
  471. CPPUNIT_ASSERT_EQUAL(2147483647, Util::parseInt("2147483647"));
  472. try {
  473. Util::parseInt("2147483648");
  474. CPPUNIT_FAIL("exception must be thrown.");
  475. } catch(Exception& e) {
  476. std::cerr << e.stackTrace();
  477. }
  478. try {
  479. Util::parseInt("-2147483649");
  480. CPPUNIT_FAIL("exception must be thrown.");
  481. } catch(Exception& e) {
  482. std::cerr << e.stackTrace();
  483. }
  484. try {
  485. Util::parseInt("12x");
  486. CPPUNIT_FAIL("exception must be thrown.");
  487. } catch(Exception& e) {
  488. std::cerr << e.stackTrace();
  489. }
  490. try {
  491. Util::parseInt("");
  492. CPPUNIT_FAIL("exception must be thrown.");
  493. } catch(Exception& e) {
  494. std::cerr << e.stackTrace();
  495. }
  496. }
  497. void UtilTest::testParseUInt()
  498. {
  499. CPPUNIT_ASSERT_EQUAL(4294967295U, Util::parseUInt(" 4294967295 "));
  500. try {
  501. Util::parseUInt("-1");
  502. CPPUNIT_FAIL("exception must be thrown.");
  503. } catch(Exception& e) {
  504. std::cerr << e.stackTrace();
  505. }
  506. try {
  507. Util::parseUInt("4294967296");
  508. CPPUNIT_FAIL("exception must be thrown.");
  509. } catch(Exception& e) {
  510. std::cerr << e.stackTrace();
  511. }
  512. }
  513. void UtilTest::testParseLLInt()
  514. {
  515. CPPUNIT_ASSERT_EQUAL((int64_t)-1LL, Util::parseLLInt(" -1 "));
  516. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
  517. Util::parseLLInt("9223372036854775807"));
  518. try {
  519. Util::parseLLInt("9223372036854775808");
  520. CPPUNIT_FAIL("exception must be thrown.");
  521. } catch(Exception& e) {
  522. std::cerr << e.stackTrace();
  523. }
  524. try {
  525. Util::parseLLInt("-9223372036854775809");
  526. CPPUNIT_FAIL("exception must be thrown.");
  527. } catch(Exception& e) {
  528. std::cerr << e.stackTrace();
  529. }
  530. try {
  531. Util::parseLLInt("12x");
  532. CPPUNIT_FAIL("exception must be thrown.");
  533. } catch(Exception& e) {
  534. std::cerr << e.stackTrace();
  535. }
  536. try {
  537. Util::parseLLInt("");
  538. CPPUNIT_FAIL("exception must be thrown.");
  539. } catch(Exception& e) {
  540. std::cerr << e.stackTrace();
  541. }
  542. }
  543. void UtilTest::testParseULLInt()
  544. {
  545. CPPUNIT_ASSERT_EQUAL((uint64_t)18446744073709551615ULL,
  546. Util::parseULLInt("18446744073709551615"));
  547. try {
  548. Util::parseUInt("-1");
  549. CPPUNIT_FAIL("exception must be thrown.");
  550. } catch(Exception& e) {
  551. std::cerr << e.stackTrace();
  552. }
  553. try {
  554. Util::parseLLInt("18446744073709551616");
  555. CPPUNIT_FAIL("exception must be thrown.");
  556. } catch(Exception& e) {
  557. std::cerr << e.stackTrace();
  558. }
  559. }
  560. void UtilTest::testToString_binaryStream()
  561. {
  562. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  563. std::string data(16*1024+256, 'a');
  564. dw->initAndOpenFile("dummy");
  565. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  566. std::string readData = Util::toString(dw);
  567. CPPUNIT_ASSERT_EQUAL(data, readData);
  568. }
  569. void UtilTest::testItos()
  570. {
  571. {
  572. int i = 0;
  573. CPPUNIT_ASSERT_EQUAL(std::string("0"), Util::itos(i));
  574. }
  575. {
  576. int i = 100;
  577. CPPUNIT_ASSERT_EQUAL(std::string("100"), Util::itos(i, true));
  578. }
  579. {
  580. int i = 100;
  581. CPPUNIT_ASSERT_EQUAL(std::string("100"), Util::itos(i));
  582. }
  583. {
  584. int i = 12345;
  585. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), Util::itos(i, true));
  586. }
  587. {
  588. int i = 12345;
  589. CPPUNIT_ASSERT_EQUAL(std::string("12345"), Util::itos(i));
  590. }
  591. {
  592. int i = -12345;
  593. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), Util::itos(i, true));
  594. }
  595. {
  596. int64_t i = INT64_MAX;
  597. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"), Util::itos(i, true));
  598. }
  599. }
  600. void UtilTest::testUitos()
  601. {
  602. {
  603. uint16_t i = 12345;
  604. CPPUNIT_ASSERT_EQUAL(std::string("12345"), Util::uitos(i));
  605. }
  606. {
  607. int16_t i = -12345;
  608. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), Util::uitos(i));
  609. }
  610. }
  611. void UtilTest::testHttpGMT()
  612. {
  613. CPPUNIT_ASSERT_EQUAL((time_t)0, Util::httpGMT("Thu, 1970-01-01 0:0:0 GMT"));
  614. CPPUNIT_ASSERT_EQUAL((time_t)2147483647,
  615. Util::httpGMT("Tue, 2038-01-19 3:14:7 GMT"));
  616. if(sizeof(time_t) == 4) {
  617. CPPUNIT_ASSERT_EQUAL((time_t)2147483647,
  618. Util::httpGMT("Tue, 2038-01-19 3:14:8 GMT"));
  619. } else if(sizeof(time_t) == 8) {
  620. CPPUNIT_ASSERT_EQUAL((int64_t)2147483648LL,
  621. (int64_t)Util::httpGMT("Tue, 2038-01-19 3:14:8 GMT"));
  622. }
  623. CPPUNIT_ASSERT_EQUAL((time_t)-1,
  624. Util::httpGMT("Tue, 2008/10/10 23:33:33 UTC"));
  625. }
  626. void UtilTest::testNtoh64()
  627. {
  628. uint64_t x = 0xff00ff00ee00ee00LL;
  629. #ifdef WORDS_BIGENDIAN
  630. CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
  631. CPPUNIT_ASSERT_EQUAL(x, hton64(x));
  632. #else // !WORDS_BIGENDIAN
  633. uint64_t y = 0x00ee00ee00ff00ffLL;
  634. CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
  635. CPPUNIT_ASSERT_EQUAL(x, hton64(y));
  636. #endif // !WORDS_BIGENDIAN
  637. }
  638. void UtilTest::testUrlencode()
  639. {
  640. CPPUNIT_ASSERT_EQUAL
  641. (std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
  642. Util::urlencode(":/?#[]@!%&'()*+,;="));
  643. std::string unreserved =
  644. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  645. "abcdefghijklmnopqrstuvwxyz"
  646. "0123456789"
  647. "-._~";
  648. CPPUNIT_ASSERT_EQUAL(unreserved, Util::urlencode(unreserved));
  649. CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), Util::urlencode("1^A "));
  650. }
  651. void UtilTest::testHtmlEscape()
  652. {
  653. CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),
  654. Util::htmlEscape("aria2<>\"'util"));
  655. }
  656. void UtilTest::testJoinPath()
  657. {
  658. const std::string dir1dir2file[] = { "dir1", "dir2", "file" };
  659. CPPUNIT_ASSERT_EQUAL
  660. (std::string("dir1/dir2/file"),
  661. Util::joinPath(&dir1dir2file[0],
  662. &dir1dir2file[arrayLength(dir1dir2file)]));
  663. const std::string dirparentfile[] = { "dir", "..", "file" };
  664. CPPUNIT_ASSERT_EQUAL
  665. (std::string("file"),
  666. Util::joinPath(&dirparentfile[0],
  667. &dirparentfile[arrayLength(dirparentfile)]));
  668. const std::string dirparentparentfile[] = { "dir", "..", "..", "file" };
  669. CPPUNIT_ASSERT_EQUAL
  670. (std::string("file"),
  671. Util::joinPath(&dirparentparentfile[0],
  672. &dirparentparentfile[arrayLength(dirparentparentfile)]));
  673. const std::string dirdotfile[] = { "dir", ".", "file" };
  674. CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
  675. Util::joinPath(&dirdotfile[0],
  676. &dirdotfile[arrayLength(dirdotfile)]));
  677. const std::string empty[] = {};
  678. CPPUNIT_ASSERT_EQUAL(std::string(""), Util::joinPath(&empty[0], &empty[0]));
  679. const std::string parentdot[] = { "..", "." };
  680. CPPUNIT_ASSERT_EQUAL(std::string(""),
  681. Util::joinPath(&parentdot[0],
  682. &parentdot[arrayLength(parentdot)]));
  683. }
  684. } // namespace aria2