UtilTest.cc 9.2 KB

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