UtilTest.cc 8.3 KB

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