UtilTest.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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;", ';', true);
  81. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  82. v1.clear();
  83. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', true);
  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. v1.clear();
  90. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', false);
  91. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  92. itr = v1.begin();
  93. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  94. CPPUNIT_ASSERT_EQUAL(string(" name2=value2"), *itr++);
  95. CPPUNIT_ASSERT_EQUAL(string(" name3=value3"), *itr++);
  96. }
  97. void UtilTest::testEndsWith() {
  98. string target = "abcdefg";
  99. string part = "fg";
  100. CPPUNIT_ASSERT(Util::endsWith(target, part));
  101. target = "abdefg";
  102. part = "g";
  103. CPPUNIT_ASSERT(Util::endsWith(target, part));
  104. target = "abdefg";
  105. part = "eg";
  106. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  107. target = "g";
  108. part = "eg";
  109. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  110. target = "g";
  111. part = "g";
  112. CPPUNIT_ASSERT(Util::endsWith(target, part));
  113. target = "g";
  114. part = "";
  115. CPPUNIT_ASSERT(Util::endsWith(target, part));
  116. target = "";
  117. part = "";
  118. CPPUNIT_ASSERT(Util::endsWith(target, part));
  119. target = "";
  120. part = "g";
  121. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  122. }
  123. void UtilTest::testReplace() {
  124. CPPUNIT_ASSERT_EQUAL(string("abc\n"), Util::replace("abc\r\n", "\r", ""));
  125. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc\r\n", "\r\n", ""));
  126. CPPUNIT_ASSERT_EQUAL(string(""), Util::replace("", "\r\n", ""));
  127. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc", "", "a"));
  128. CPPUNIT_ASSERT_EQUAL(string("xbc"), Util::replace("abc", "a", "x"));
  129. }
  130. void UtilTest::testStartsWith() {
  131. string target;
  132. string part;
  133. target = "abcdefg";
  134. part = "abc";
  135. CPPUNIT_ASSERT(Util::startsWith(target, part));
  136. target = "abcdefg";
  137. part = "abx";
  138. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  139. target = "abcdefg";
  140. part = "bcd";
  141. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  142. target = "";
  143. part = "a";
  144. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  145. target = "";
  146. part = "";
  147. CPPUNIT_ASSERT(Util::startsWith(target, part));
  148. target = "a";
  149. part = "";
  150. CPPUNIT_ASSERT(Util::startsWith(target, part));
  151. target = "a";
  152. part = "a";
  153. CPPUNIT_ASSERT(Util::startsWith(target, part));
  154. }
  155. void UtilTest::testGetContentDispositionFilename() {
  156. string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  157. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h1));
  158. string h2 = "attachment; filename=\"\"";
  159. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h2));
  160. string h3 = "attachment; filename=\"";
  161. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h3));
  162. string h4 = "attachment;";
  163. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h4));
  164. }
  165. class Printer {
  166. public:
  167. template<class T>
  168. void operator()(T t) {
  169. cerr << t << ", ";
  170. }
  171. };
  172. void UtilTest::testComputeFastSet() {
  173. string ipaddr = "192.168.0.1";
  174. unsigned char infoHash[20];
  175. memset(infoHash, 0, sizeof(infoHash));
  176. infoHash[0] = 0xff;
  177. int pieces = 1000;
  178. int fastSetSize = 10;
  179. Integers fastSet = Util::computeFastSet(ipaddr, infoHash, pieces, fastSetSize);
  180. //for_each(fastSet.begin(), fastSet.end(), Printer());
  181. //cerr << endl;
  182. int ans1[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
  183. Integers ansSet1(&ans1[0], &ans1[10]);
  184. CPPUNIT_ASSERT(equal(fastSet.begin(), fastSet.end(), ansSet1.begin()));
  185. ipaddr = "10.0.0.1";
  186. fastSet = Util::computeFastSet(ipaddr, infoHash, pieces, fastSetSize);
  187. int ans2[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
  188. Integers ansSet2(&ans2[0], &ans2[10]);
  189. CPPUNIT_ASSERT(equal(fastSet.begin(), fastSet.end(), ansSet2.begin()));
  190. }
  191. void UtilTest::testRandomAlpha() {
  192. CPPUNIT_ASSERT_EQUAL(string("rUopvKRn"), Util::randomAlpha(8));
  193. }
  194. void UtilTest::testFileChecksum() {
  195. unsigned char buf[20];
  196. string filename = "4096chunk.txt";
  197. Util::fileChecksum(filename, buf, DIGEST_ALGO_SHA1);
  198. string sha1 = Util::toHex(buf, 20);
  199. CPPUNIT_ASSERT_EQUAL(string("608cabc0f2fa18c260cafd974516865c772363d5"),
  200. sha1);
  201. Util::fileChecksum(filename, buf, DIGEST_ALGO_MD5);
  202. string md5 = Util::toHex(buf, 16);
  203. CPPUNIT_ASSERT_EQUAL(string("82a7348c2e03731109d0cf45a7325b88"),
  204. md5);
  205. }
  206. void UtilTest::testToUpper() {
  207. string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  208. string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  209. CPPUNIT_ASSERT_EQUAL(upp, Util::toUpper(src));
  210. }
  211. void UtilTest::testToLower() {
  212. string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  213. string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  214. CPPUNIT_ASSERT_EQUAL(upp, Util::toLower(src));
  215. }
  216. #include "SharedHandle.h"
  217. void UtilTest::testUrldecode() {
  218. string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  219. CPPUNIT_ASSERT_EQUAL(string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  220. Util::urldecode(src));
  221. string src2 = "aria2+aria2";
  222. CPPUNIT_ASSERT_EQUAL(string("aria2+aria2"), Util::urldecode(src2));
  223. string src3 = "%5t%20";
  224. CPPUNIT_ASSERT_EQUAL(string("%5t "), Util::urldecode(src3));
  225. string src4 = "%";
  226. CPPUNIT_ASSERT_EQUAL(string("%"), Util::urldecode(src4));
  227. string src5 = "%3";
  228. CPPUNIT_ASSERT_EQUAL(string("%3"), Util::urldecode(src5));
  229. string src6 = "%2f";
  230. CPPUNIT_ASSERT_EQUAL(string("/"), Util::urldecode(src6));
  231. }