UtilTest.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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_SUITE_END();
  24. private:
  25. public:
  26. void setUp() {
  27. }
  28. void testTrim();
  29. void testSplit();
  30. void testSlice();
  31. void testEndsWith();
  32. void testReplace();
  33. void testStartsWith();
  34. void testComputeFastSet();
  35. // may be moved to other helper class in the future.
  36. void testGetContentDispositionFilename();
  37. void testRandomAlpha();
  38. void testFileChecksum();
  39. void testToUpper();
  40. void testToLower();
  41. void testUrldecode();
  42. void testCountBit();
  43. void testGetRealSize();
  44. };
  45. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  46. void UtilTest::testTrim() {
  47. string str1 = "aria2";
  48. CPPUNIT_ASSERT_EQUAL(str1, Util::trim("aria2"));
  49. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2"));
  50. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  51. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  52. string str2 = "aria2 debut";
  53. CPPUNIT_ASSERT_EQUAL(str2, Util::trim("aria2 debut"));
  54. CPPUNIT_ASSERT_EQUAL(str2, Util::trim(" aria2 debut "));
  55. string str3 = "";
  56. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(""));
  57. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  58. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  59. string str4 = "A";
  60. CPPUNIT_ASSERT_EQUAL(str4, Util::trim("A"));
  61. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  62. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  63. }
  64. void UtilTest::testSplit() {
  65. pair<string, string> p1;
  66. Util::split(p1, "name=value", '=');
  67. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  68. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  69. Util::split(p1, " name = value ", '=');
  70. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  71. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  72. Util::split(p1, "=value", '=');
  73. CPPUNIT_ASSERT_EQUAL(string(""), p1.first);
  74. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  75. Util::split(p1, "name=", '=');
  76. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  77. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  78. Util::split(p1, "name", '=');
  79. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  80. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  81. }
  82. void UtilTest::testSlice() {
  83. Strings v1;
  84. Util::slice(v1, "name1=value1; name2=value2; name3=value3;", ';', true);
  85. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  86. v1.clear();
  87. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', true);
  88. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  89. Strings::iterator itr = v1.begin();
  90. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  91. CPPUNIT_ASSERT_EQUAL(string("name2=value2"), *itr++);
  92. CPPUNIT_ASSERT_EQUAL(string("name3=value3"), *itr++);
  93. v1.clear();
  94. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', false);
  95. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  96. itr = v1.begin();
  97. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  98. CPPUNIT_ASSERT_EQUAL(string(" name2=value2"), *itr++);
  99. CPPUNIT_ASSERT_EQUAL(string(" name3=value3"), *itr++);
  100. }
  101. void UtilTest::testEndsWith() {
  102. string target = "abcdefg";
  103. string part = "fg";
  104. CPPUNIT_ASSERT(Util::endsWith(target, part));
  105. target = "abdefg";
  106. part = "g";
  107. CPPUNIT_ASSERT(Util::endsWith(target, part));
  108. target = "abdefg";
  109. part = "eg";
  110. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  111. target = "g";
  112. part = "eg";
  113. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  114. target = "g";
  115. part = "g";
  116. CPPUNIT_ASSERT(Util::endsWith(target, part));
  117. target = "g";
  118. part = "";
  119. CPPUNIT_ASSERT(Util::endsWith(target, part));
  120. target = "";
  121. part = "";
  122. CPPUNIT_ASSERT(Util::endsWith(target, part));
  123. target = "";
  124. part = "g";
  125. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  126. }
  127. void UtilTest::testReplace() {
  128. CPPUNIT_ASSERT_EQUAL(string("abc\n"), Util::replace("abc\r\n", "\r", ""));
  129. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc\r\n", "\r\n", ""));
  130. CPPUNIT_ASSERT_EQUAL(string(""), Util::replace("", "\r\n", ""));
  131. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc", "", "a"));
  132. CPPUNIT_ASSERT_EQUAL(string("xbc"), Util::replace("abc", "a", "x"));
  133. }
  134. void UtilTest::testStartsWith() {
  135. string target;
  136. string part;
  137. target = "abcdefg";
  138. part = "abc";
  139. CPPUNIT_ASSERT(Util::startsWith(target, part));
  140. target = "abcdefg";
  141. part = "abx";
  142. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  143. target = "abcdefg";
  144. part = "bcd";
  145. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  146. target = "";
  147. part = "a";
  148. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  149. target = "";
  150. part = "";
  151. CPPUNIT_ASSERT(Util::startsWith(target, part));
  152. target = "a";
  153. part = "";
  154. CPPUNIT_ASSERT(Util::startsWith(target, part));
  155. target = "a";
  156. part = "a";
  157. CPPUNIT_ASSERT(Util::startsWith(target, part));
  158. }
  159. void UtilTest::testGetContentDispositionFilename() {
  160. string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  161. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h1));
  162. string h2 = "attachment; filename=\"\"";
  163. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h2));
  164. string h3 = "attachment; filename=\"";
  165. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h3));
  166. string h4 = "attachment;";
  167. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h4));
  168. }
  169. class Printer {
  170. public:
  171. template<class T>
  172. void operator()(T t) {
  173. cerr << t << ", ";
  174. }
  175. };
  176. void UtilTest::testComputeFastSet() {
  177. string ipaddr = "192.168.0.1";
  178. unsigned char infoHash[20];
  179. memset(infoHash, 0, sizeof(infoHash));
  180. infoHash[0] = 0xff;
  181. int pieces = 1000;
  182. int fastSetSize = 10;
  183. Integers fastSet = Util::computeFastSet(ipaddr, infoHash, pieces, fastSetSize);
  184. //for_each(fastSet.begin(), fastSet.end(), Printer());
  185. //cerr << endl;
  186. int ans1[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
  187. Integers ansSet1(&ans1[0], &ans1[10]);
  188. CPPUNIT_ASSERT(equal(fastSet.begin(), fastSet.end(), ansSet1.begin()));
  189. ipaddr = "10.0.0.1";
  190. fastSet = Util::computeFastSet(ipaddr, infoHash, pieces, fastSetSize);
  191. int ans2[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
  192. Integers ansSet2(&ans2[0], &ans2[10]);
  193. CPPUNIT_ASSERT(equal(fastSet.begin(), fastSet.end(), ansSet2.begin()));
  194. }
  195. void UtilTest::testRandomAlpha() {
  196. CPPUNIT_ASSERT_EQUAL(string("rUopvKRn"), Util::randomAlpha(8));
  197. }
  198. void UtilTest::testFileChecksum() {
  199. unsigned char buf[20];
  200. string filename = "4096chunk.txt";
  201. Util::fileChecksum(filename, buf, DIGEST_ALGO_SHA1);
  202. string sha1 = Util::toHex(buf, 20);
  203. CPPUNIT_ASSERT_EQUAL(string("608cabc0f2fa18c260cafd974516865c772363d5"),
  204. sha1);
  205. Util::fileChecksum(filename, buf, DIGEST_ALGO_MD5);
  206. string md5 = Util::toHex(buf, 16);
  207. CPPUNIT_ASSERT_EQUAL(string("82a7348c2e03731109d0cf45a7325b88"),
  208. md5);
  209. }
  210. void UtilTest::testToUpper() {
  211. string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  212. string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  213. CPPUNIT_ASSERT_EQUAL(upp, Util::toUpper(src));
  214. }
  215. void UtilTest::testToLower() {
  216. string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  217. string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  218. CPPUNIT_ASSERT_EQUAL(upp, Util::toLower(src));
  219. }
  220. #include "SharedHandle.h"
  221. void UtilTest::testUrldecode() {
  222. string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  223. CPPUNIT_ASSERT_EQUAL(string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  224. Util::urldecode(src));
  225. string src2 = "aria2+aria2";
  226. CPPUNIT_ASSERT_EQUAL(string("aria2+aria2"), Util::urldecode(src2));
  227. string src3 = "%5t%20";
  228. CPPUNIT_ASSERT_EQUAL(string("%5t "), Util::urldecode(src3));
  229. string src4 = "%";
  230. CPPUNIT_ASSERT_EQUAL(string("%"), Util::urldecode(src4));
  231. string src5 = "%3";
  232. CPPUNIT_ASSERT_EQUAL(string("%3"), Util::urldecode(src5));
  233. string src6 = "%2f";
  234. CPPUNIT_ASSERT_EQUAL(string("/"), Util::urldecode(src6));
  235. }
  236. void UtilTest::testCountBit() {
  237. CPPUNIT_ASSERT_EQUAL(32, Util::countBit(UINT32_MAX));
  238. CPPUNIT_ASSERT_EQUAL(8, Util::countBit(255));
  239. }
  240. void UtilTest::testGetRealSize()
  241. {
  242. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, Util::getRealSize("4096M"));
  243. CPPUNIT_ASSERT_EQUAL((int64_t)1024, Util::getRealSize("1K"));
  244. CPPUNIT_ASSERT_EQUAL((int64_t)0, Util::getRealSize(""));
  245. CPPUNIT_ASSERT_EQUAL((int64_t)0, Util::getRealSize("foo"));
  246. }