UtilTest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #include "Util.h"
  2. #include "FixedNumberRandomizer.h"
  3. #include "DlAbortEx.h"
  4. #include <string>
  5. #include <cppunit/extensions/HelperMacros.h>
  6. using namespace std;
  7. class UtilTest:public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(UtilTest);
  9. CPPUNIT_TEST(testTrim);
  10. CPPUNIT_TEST(testSplit);
  11. CPPUNIT_TEST(testSlice);
  12. CPPUNIT_TEST(testEndsWith);
  13. CPPUNIT_TEST(testReplace);
  14. CPPUNIT_TEST(testStartsWith);
  15. // may be moved to other helper class in the future.
  16. CPPUNIT_TEST(testGetContentDispositionFilename);
  17. CPPUNIT_TEST(testRandomAlpha);
  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(testToStream);
  25. CPPUNIT_TEST(testIsNumber);
  26. CPPUNIT_TEST(testIsLowercase);
  27. CPPUNIT_TEST(testIsUppercase);
  28. CPPUNIT_TEST(testAlphaToNum);
  29. CPPUNIT_TEST(testMkdirs);
  30. CPPUNIT_TEST_SUITE_END();
  31. private:
  32. public:
  33. void setUp() {
  34. }
  35. void testTrim();
  36. void testSplit();
  37. void testSlice();
  38. void testEndsWith();
  39. void testReplace();
  40. void testStartsWith();
  41. // may be moved to other helper class in the future.
  42. void testGetContentDispositionFilename();
  43. void testRandomAlpha();
  44. void testToUpper();
  45. void testToLower();
  46. void testUrldecode();
  47. void testCountBit();
  48. void testGetRealSize();
  49. void testAbbrevSize();
  50. void testToStream();
  51. void testIsNumber();
  52. void testIsLowercase();
  53. void testIsUppercase();
  54. void testAlphaToNum();
  55. void testMkdirs();
  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. void UtilTest::testRandomAlpha() {
  203. string s = Util::randomAlpha(8, new FixedNumberRandomizer());
  204. CPPUNIT_ASSERT_EQUAL(string("AAAAAAAA"), s);
  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. }
  232. void UtilTest::testCountBit() {
  233. CPPUNIT_ASSERT_EQUAL((int32_t)32, Util::countBit(UINT32_MAX));
  234. CPPUNIT_ASSERT_EQUAL((int32_t)8, Util::countBit(255));
  235. }
  236. void UtilTest::testGetRealSize()
  237. {
  238. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, Util::getRealSize("4096M"));
  239. CPPUNIT_ASSERT_EQUAL((int64_t)1024, Util::getRealSize("1K"));
  240. CPPUNIT_ASSERT_EQUAL((int64_t)0, Util::getRealSize(""));
  241. CPPUNIT_ASSERT_EQUAL((int64_t)0, Util::getRealSize("foo"));
  242. }
  243. void UtilTest::testAbbrevSize()
  244. {
  245. CPPUNIT_ASSERT_EQUAL(string("4,096.0Mi"), Util::abbrevSize(4294967296LL));
  246. CPPUNIT_ASSERT_EQUAL(string("1.0Ki"), Util::abbrevSize(1024));
  247. CPPUNIT_ASSERT_EQUAL(string("1,023"), Util::abbrevSize(1023));
  248. CPPUNIT_ASSERT_EQUAL(string("0"), Util::abbrevSize(0));
  249. CPPUNIT_ASSERT_EQUAL(string("1.1Ki"), Util::abbrevSize(1127));
  250. CPPUNIT_ASSERT_EQUAL(string("1.5Mi"), Util::abbrevSize(1572864));
  251. }
  252. void UtilTest::testToStream()
  253. {
  254. ostringstream os;
  255. FileEntryHandle f1 = new FileEntry("aria2.tar.bz2", 12300, 0);
  256. FileEntryHandle f2 = new FileEntry("aria2.txt", 556, 0);
  257. FileEntries entries;
  258. entries.push_back(f1);
  259. entries.push_back(f2);
  260. Util::toStream(os, entries);
  261. CPPUNIT_ASSERT_EQUAL(
  262. string("Files:\n"
  263. "idx|path/length\n"
  264. "===+===========================================================================\n"
  265. " 1|aria2.tar.bz2\n"
  266. " |12.0KiB\n"
  267. "---+---------------------------------------------------------------------------\n"
  268. " 2|aria2.txt\n"
  269. " |556B\n"
  270. "---+---------------------------------------------------------------------------\n"),
  271. os.str());
  272. }
  273. void UtilTest::testIsNumber()
  274. {
  275. CPPUNIT_ASSERT_EQUAL(true, Util::isNumber("000"));
  276. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("a"));
  277. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("0a"));
  278. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(""));
  279. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(" "));
  280. }
  281. void UtilTest::testIsLowercase()
  282. {
  283. CPPUNIT_ASSERT_EQUAL(true, Util::isLowercase("alpha"));
  284. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("Alpha"));
  285. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("1alpha"));
  286. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(""));
  287. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(" "));
  288. }
  289. void UtilTest::testIsUppercase()
  290. {
  291. CPPUNIT_ASSERT_EQUAL(true, Util::isUppercase("ALPHA"));
  292. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("Alpha"));
  293. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("1ALPHA"));
  294. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(""));
  295. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(" "));
  296. }
  297. void UtilTest::testAlphaToNum()
  298. {
  299. CPPUNIT_ASSERT_EQUAL((int32_t)0, Util::alphaToNum("a"));
  300. CPPUNIT_ASSERT_EQUAL((int32_t)0, Util::alphaToNum("aa"));
  301. CPPUNIT_ASSERT_EQUAL((int32_t)1, Util::alphaToNum("b"));
  302. CPPUNIT_ASSERT_EQUAL((int32_t)675, Util::alphaToNum("zz")); // 25*26+25
  303. CPPUNIT_ASSERT_EQUAL((int32_t)675, Util::alphaToNum("ZZ")); // 25*26+25
  304. CPPUNIT_ASSERT_EQUAL((int32_t)0, Util::alphaToNum(""));
  305. }
  306. void UtilTest::testMkdirs()
  307. {
  308. string dir = "/tmp/aria2-UtilTest-testMkdirs";
  309. File d(dir);
  310. if(d.exists()) {
  311. CPPUNIT_ASSERT(d.remove());
  312. }
  313. CPPUNIT_ASSERT(!d.exists());
  314. Util::mkdirs(dir);
  315. CPPUNIT_ASSERT(d.isDir());
  316. string file = "./UtilTest.cc";
  317. File f(file);
  318. CPPUNIT_ASSERT(f.isFile());
  319. try {
  320. Util::mkdirs(file);
  321. CPPUNIT_FAIL("exception must be thrown.");
  322. } catch(DlAbortEx* ex) {
  323. cerr << ex->getMsg() << endl;
  324. delete ex;
  325. }
  326. }