UtilTest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include "Util.h"
  2. #include <string>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. using namespace std;
  5. class UtilTest:public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(UtilTest);
  7. CPPUNIT_TEST(testTrim);
  8. CPPUNIT_TEST(testSplit);
  9. CPPUNIT_TEST(testSlice);
  10. CPPUNIT_TEST(testEndsWith);
  11. CPPUNIT_TEST(testReplace);
  12. CPPUNIT_TEST(testStartsWith);
  13. // may be moved to other helper class in the future.
  14. CPPUNIT_TEST(testGetContentDispositionFilename);
  15. CPPUNIT_TEST(testComputeFastSet);
  16. CPPUNIT_TEST(testRandomAlpha);
  17. CPPUNIT_TEST(testFileChecksum);
  18. CPPUNIT_TEST(testToUpper);
  19. CPPUNIT_TEST(testToLower);
  20. CPPUNIT_TEST(testUrldecode);
  21. CPPUNIT_TEST(testCountBit);
  22. CPPUNIT_TEST(testGetRealSize);
  23. CPPUNIT_TEST(testAbbrevSize);
  24. CPPUNIT_TEST(testToStream);
  25. CPPUNIT_TEST_SUITE_END();
  26. private:
  27. public:
  28. void setUp() {
  29. }
  30. void testTrim();
  31. void testSplit();
  32. void testSlice();
  33. void testEndsWith();
  34. void testReplace();
  35. void testStartsWith();
  36. void testComputeFastSet();
  37. // may be moved to other helper class in the future.
  38. void testGetContentDispositionFilename();
  39. void testRandomAlpha();
  40. void testFileChecksum();
  41. void testToUpper();
  42. void testToLower();
  43. void testUrldecode();
  44. void testCountBit();
  45. void testGetRealSize();
  46. void testAbbrevSize();
  47. void testToStream();
  48. };
  49. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  50. void UtilTest::testTrim() {
  51. string str1 = "aria2";
  52. CPPUNIT_ASSERT_EQUAL(str1, Util::trim("aria2"));
  53. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2"));
  54. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  55. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  56. string str2 = "aria2 debut";
  57. CPPUNIT_ASSERT_EQUAL(str2, Util::trim("aria2 debut"));
  58. CPPUNIT_ASSERT_EQUAL(str2, Util::trim(" aria2 debut "));
  59. string str3 = "";
  60. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(""));
  61. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  62. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  63. string str4 = "A";
  64. CPPUNIT_ASSERT_EQUAL(str4, Util::trim("A"));
  65. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  66. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  67. }
  68. void UtilTest::testSplit() {
  69. pair<string, string> p1;
  70. Util::split(p1, "name=value", '=');
  71. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  72. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  73. Util::split(p1, " name = value ", '=');
  74. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  75. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  76. Util::split(p1, "=value", '=');
  77. CPPUNIT_ASSERT_EQUAL(string(""), p1.first);
  78. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  79. Util::split(p1, "name=", '=');
  80. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  81. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  82. Util::split(p1, "name", '=');
  83. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  84. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  85. }
  86. void UtilTest::testSlice() {
  87. Strings v1;
  88. Util::slice(v1, "name1=value1; name2=value2; name3=value3;", ';', true);
  89. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  90. v1.clear();
  91. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', true);
  92. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  93. Strings::iterator itr = v1.begin();
  94. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  95. CPPUNIT_ASSERT_EQUAL(string("name2=value2"), *itr++);
  96. CPPUNIT_ASSERT_EQUAL(string("name3=value3"), *itr++);
  97. v1.clear();
  98. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', false);
  99. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  100. itr = v1.begin();
  101. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  102. CPPUNIT_ASSERT_EQUAL(string(" name2=value2"), *itr++);
  103. CPPUNIT_ASSERT_EQUAL(string(" name3=value3"), *itr++);
  104. }
  105. void UtilTest::testEndsWith() {
  106. string target = "abcdefg";
  107. string part = "fg";
  108. CPPUNIT_ASSERT(Util::endsWith(target, part));
  109. target = "abdefg";
  110. part = "g";
  111. CPPUNIT_ASSERT(Util::endsWith(target, part));
  112. target = "abdefg";
  113. part = "eg";
  114. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  115. target = "g";
  116. part = "eg";
  117. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  118. target = "g";
  119. part = "g";
  120. CPPUNIT_ASSERT(Util::endsWith(target, part));
  121. target = "g";
  122. part = "";
  123. CPPUNIT_ASSERT(Util::endsWith(target, part));
  124. target = "";
  125. part = "";
  126. CPPUNIT_ASSERT(Util::endsWith(target, part));
  127. target = "";
  128. part = "g";
  129. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  130. }
  131. void UtilTest::testReplace() {
  132. CPPUNIT_ASSERT_EQUAL(string("abc\n"), Util::replace("abc\r\n", "\r", ""));
  133. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc\r\n", "\r\n", ""));
  134. CPPUNIT_ASSERT_EQUAL(string(""), Util::replace("", "\r\n", ""));
  135. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc", "", "a"));
  136. CPPUNIT_ASSERT_EQUAL(string("xbc"), Util::replace("abc", "a", "x"));
  137. }
  138. void UtilTest::testStartsWith() {
  139. string target;
  140. string part;
  141. target = "abcdefg";
  142. part = "abc";
  143. CPPUNIT_ASSERT(Util::startsWith(target, part));
  144. target = "abcdefg";
  145. part = "abx";
  146. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  147. target = "abcdefg";
  148. part = "bcd";
  149. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  150. target = "";
  151. part = "a";
  152. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  153. target = "";
  154. part = "";
  155. CPPUNIT_ASSERT(Util::startsWith(target, part));
  156. target = "a";
  157. part = "";
  158. CPPUNIT_ASSERT(Util::startsWith(target, part));
  159. target = "a";
  160. part = "a";
  161. CPPUNIT_ASSERT(Util::startsWith(target, part));
  162. }
  163. void UtilTest::testGetContentDispositionFilename() {
  164. string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  165. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h1));
  166. string h2 = "attachment; filename=\"\"";
  167. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h2));
  168. string h3 = "attachment; filename=\"";
  169. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h3));
  170. string h4 = "attachment;";
  171. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h4));
  172. string h5 = "attachment; filename=aria2.tar.bz2";
  173. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h5));
  174. string h6 = "attachment; filename='aria2.tar.bz2'";
  175. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h6));
  176. string h7 = "attachment; filename='aria2.tar.bz2";
  177. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h7));
  178. string h8 = "attachment; filename=aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT";
  179. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h8));
  180. string h9 = "attachment; filename=\"aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT\"";
  181. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT"), Util::getContentDispositionFilename(h9));
  182. string h10 = "attachment; filename=";
  183. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h10));
  184. string h11 = "attachment; filename=;";
  185. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h11));
  186. }
  187. class Printer {
  188. public:
  189. template<class T>
  190. void operator()(T t) {
  191. cerr << t << ", ";
  192. }
  193. };
  194. void UtilTest::testComputeFastSet() {
  195. string ipaddr = "192.168.0.1";
  196. unsigned char infoHash[20];
  197. memset(infoHash, 0, sizeof(infoHash));
  198. infoHash[0] = 0xff;
  199. int pieces = 1000;
  200. int fastSetSize = 10;
  201. Integers fastSet = Util::computeFastSet(ipaddr, infoHash, pieces, fastSetSize);
  202. //for_each(fastSet.begin(), fastSet.end(), Printer());
  203. //cerr << endl;
  204. int ans1[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
  205. Integers ansSet1(&ans1[0], &ans1[10]);
  206. CPPUNIT_ASSERT(equal(fastSet.begin(), fastSet.end(), ansSet1.begin()));
  207. ipaddr = "10.0.0.1";
  208. fastSet = Util::computeFastSet(ipaddr, infoHash, pieces, fastSetSize);
  209. int ans2[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
  210. Integers ansSet2(&ans2[0], &ans2[10]);
  211. CPPUNIT_ASSERT(equal(fastSet.begin(), fastSet.end(), ansSet2.begin()));
  212. }
  213. void UtilTest::testRandomAlpha() {
  214. CPPUNIT_ASSERT_EQUAL(string("rUopvKRn"), Util::randomAlpha(8));
  215. }
  216. void UtilTest::testFileChecksum() {
  217. unsigned char buf[20];
  218. string filename = "4096chunk.txt";
  219. Util::fileChecksum(filename, buf, DIGEST_ALGO_SHA1);
  220. string sha1 = Util::toHex(buf, 20);
  221. CPPUNIT_ASSERT_EQUAL(string("608cabc0f2fa18c260cafd974516865c772363d5"),
  222. sha1);
  223. Util::fileChecksum(filename, buf, DIGEST_ALGO_MD5);
  224. string md5 = Util::toHex(buf, 16);
  225. CPPUNIT_ASSERT_EQUAL(string("82a7348c2e03731109d0cf45a7325b88"),
  226. md5);
  227. }
  228. void UtilTest::testToUpper() {
  229. string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  230. string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  231. CPPUNIT_ASSERT_EQUAL(upp, Util::toUpper(src));
  232. }
  233. void UtilTest::testToLower() {
  234. string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  235. string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  236. CPPUNIT_ASSERT_EQUAL(upp, Util::toLower(src));
  237. }
  238. #include "SharedHandle.h"
  239. void UtilTest::testUrldecode() {
  240. string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  241. CPPUNIT_ASSERT_EQUAL(string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  242. Util::urldecode(src));
  243. string src2 = "aria2+aria2";
  244. CPPUNIT_ASSERT_EQUAL(string("aria2+aria2"), Util::urldecode(src2));
  245. string src3 = "%5t%20";
  246. CPPUNIT_ASSERT_EQUAL(string("%5t "), Util::urldecode(src3));
  247. string src4 = "%";
  248. CPPUNIT_ASSERT_EQUAL(string("%"), Util::urldecode(src4));
  249. string src5 = "%3";
  250. CPPUNIT_ASSERT_EQUAL(string("%3"), Util::urldecode(src5));
  251. string src6 = "%2f";
  252. CPPUNIT_ASSERT_EQUAL(string("/"), Util::urldecode(src6));
  253. }
  254. void UtilTest::testCountBit() {
  255. CPPUNIT_ASSERT_EQUAL(32, Util::countBit(UINT32_MAX));
  256. CPPUNIT_ASSERT_EQUAL(8, Util::countBit(255));
  257. }
  258. void UtilTest::testGetRealSize()
  259. {
  260. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, Util::getRealSize("4096M"));
  261. CPPUNIT_ASSERT_EQUAL((int64_t)1024, Util::getRealSize("1K"));
  262. CPPUNIT_ASSERT_EQUAL((int64_t)0, Util::getRealSize(""));
  263. CPPUNIT_ASSERT_EQUAL((int64_t)0, Util::getRealSize("foo"));
  264. }
  265. void UtilTest::testAbbrevSize()
  266. {
  267. CPPUNIT_ASSERT_EQUAL(string("4,096.0Mi"), Util::abbrevSize(4294967296LL));
  268. CPPUNIT_ASSERT_EQUAL(string("1.0Ki"), Util::abbrevSize(1024));
  269. CPPUNIT_ASSERT_EQUAL(string("1,023"), Util::abbrevSize(1023));
  270. CPPUNIT_ASSERT_EQUAL(string("0"), Util::abbrevSize(0));
  271. CPPUNIT_ASSERT_EQUAL(string("1.1Ki"), Util::abbrevSize(1127));
  272. CPPUNIT_ASSERT_EQUAL(string("1.5Mi"), Util::abbrevSize(1572864));
  273. }
  274. void UtilTest::testToStream()
  275. {
  276. ostringstream os;
  277. FileEntryHandle f1 = new FileEntry("aria2.tar.bz2", 12300, 0);
  278. FileEntryHandle f2 = new FileEntry("aria2.txt", 556, 0);
  279. FileEntries entries;
  280. entries.push_back(f1);
  281. entries.push_back(f2);
  282. Util::toStream(os, entries);
  283. CPPUNIT_ASSERT_EQUAL(
  284. string("Files:\n"
  285. "idx|path/length\n"
  286. "===+===========================================================================\n"
  287. " 1|aria2.tar.bz2\n"
  288. " |12,300 bytes\n"
  289. "---+---------------------------------------------------------------------------\n"
  290. " 2|aria2.txt\n"
  291. " |556 bytes\n"
  292. "---+---------------------------------------------------------------------------\n"),
  293. os.str());
  294. }