UtilTest.cc 11 KB

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