UtilTest.cc 7.1 KB

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