UtilTest.cc 7.8 KB

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