UtilTest.cc 24 KB

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