UtilTest.cc 24 KB

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