UtilTest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #include "Util.h"
  2. #include "FixedNumberRandomizer.h"
  3. #include <string>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. using namespace std;
  6. class UtilTest:public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(UtilTest);
  8. CPPUNIT_TEST(testTrim);
  9. CPPUNIT_TEST(testSplit);
  10. CPPUNIT_TEST(testSlice);
  11. CPPUNIT_TEST(testEndsWith);
  12. CPPUNIT_TEST(testReplace);
  13. CPPUNIT_TEST(testStartsWith);
  14. // may be moved to other helper class in the future.
  15. CPPUNIT_TEST(testGetContentDispositionFilename);
  16. CPPUNIT_TEST(testRandomAlpha);
  17. CPPUNIT_TEST(testToUpper);
  18. CPPUNIT_TEST(testToLower);
  19. CPPUNIT_TEST(testUrldecode);
  20. CPPUNIT_TEST(testCountBit);
  21. CPPUNIT_TEST(testGetRealSize);
  22. CPPUNIT_TEST(testAbbrevSize);
  23. CPPUNIT_TEST(testToStream);
  24. CPPUNIT_TEST(testIsNumber);
  25. CPPUNIT_TEST(testIsLowercase);
  26. CPPUNIT_TEST(testIsUppercase);
  27. CPPUNIT_TEST(testAlphaToNum);
  28. CPPUNIT_TEST(testMkdirs);
  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. // may be moved to other helper class in the future.
  41. void testGetContentDispositionFilename();
  42. void testRandomAlpha();
  43. void testToUpper();
  44. void testToLower();
  45. void testUrldecode();
  46. void testCountBit();
  47. void testGetRealSize();
  48. void testAbbrevSize();
  49. void testToStream();
  50. void testIsNumber();
  51. void testIsLowercase();
  52. void testIsUppercase();
  53. void testAlphaToNum();
  54. void testMkdirs();
  55. };
  56. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  57. void UtilTest::testTrim() {
  58. string str1 = "aria2";
  59. CPPUNIT_ASSERT_EQUAL(str1, Util::trim("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. string str2 = "aria2 debut";
  64. CPPUNIT_ASSERT_EQUAL(str2, Util::trim("aria2 debut"));
  65. CPPUNIT_ASSERT_EQUAL(str2, Util::trim(" aria2 debut "));
  66. string str3 = "";
  67. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(""));
  68. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  69. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  70. string str4 = "A";
  71. CPPUNIT_ASSERT_EQUAL(str4, Util::trim("A"));
  72. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  73. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  74. }
  75. void UtilTest::testSplit() {
  76. pair<string, string> p1;
  77. Util::split(p1, "name=value", '=');
  78. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  79. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  80. Util::split(p1, " name = value ", '=');
  81. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  82. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  83. Util::split(p1, "=value", '=');
  84. CPPUNIT_ASSERT_EQUAL(string(""), p1.first);
  85. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  86. Util::split(p1, "name=", '=');
  87. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  88. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  89. Util::split(p1, "name", '=');
  90. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  91. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  92. }
  93. void UtilTest::testSlice() {
  94. Strings v1;
  95. Util::slice(v1, "name1=value1; name2=value2; name3=value3;", ';', true);
  96. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  97. v1.clear();
  98. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', true);
  99. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  100. Strings::iterator itr = v1.begin();
  101. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  102. CPPUNIT_ASSERT_EQUAL(string("name2=value2"), *itr++);
  103. CPPUNIT_ASSERT_EQUAL(string("name3=value3"), *itr++);
  104. v1.clear();
  105. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', false);
  106. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  107. itr = v1.begin();
  108. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  109. CPPUNIT_ASSERT_EQUAL(string(" name2=value2"), *itr++);
  110. CPPUNIT_ASSERT_EQUAL(string(" name3=value3"), *itr++);
  111. }
  112. void UtilTest::testEndsWith() {
  113. string target = "abcdefg";
  114. string part = "fg";
  115. CPPUNIT_ASSERT(Util::endsWith(target, part));
  116. target = "abdefg";
  117. part = "g";
  118. CPPUNIT_ASSERT(Util::endsWith(target, part));
  119. target = "abdefg";
  120. part = "eg";
  121. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  122. target = "g";
  123. part = "eg";
  124. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  125. target = "g";
  126. part = "g";
  127. CPPUNIT_ASSERT(Util::endsWith(target, part));
  128. target = "g";
  129. part = "";
  130. CPPUNIT_ASSERT(Util::endsWith(target, part));
  131. target = "";
  132. part = "";
  133. CPPUNIT_ASSERT(Util::endsWith(target, part));
  134. target = "";
  135. part = "g";
  136. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  137. }
  138. void UtilTest::testReplace() {
  139. CPPUNIT_ASSERT_EQUAL(string("abc\n"), Util::replace("abc\r\n", "\r", ""));
  140. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc\r\n", "\r\n", ""));
  141. CPPUNIT_ASSERT_EQUAL(string(""), Util::replace("", "\r\n", ""));
  142. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc", "", "a"));
  143. CPPUNIT_ASSERT_EQUAL(string("xbc"), Util::replace("abc", "a", "x"));
  144. }
  145. void UtilTest::testStartsWith() {
  146. string target;
  147. string part;
  148. target = "abcdefg";
  149. part = "abc";
  150. CPPUNIT_ASSERT(Util::startsWith(target, part));
  151. target = "abcdefg";
  152. part = "abx";
  153. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  154. target = "abcdefg";
  155. part = "bcd";
  156. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  157. target = "";
  158. part = "a";
  159. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  160. target = "";
  161. part = "";
  162. CPPUNIT_ASSERT(Util::startsWith(target, part));
  163. target = "a";
  164. part = "";
  165. CPPUNIT_ASSERT(Util::startsWith(target, part));
  166. target = "a";
  167. part = "a";
  168. CPPUNIT_ASSERT(Util::startsWith(target, part));
  169. }
  170. void UtilTest::testGetContentDispositionFilename() {
  171. string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  172. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h1));
  173. string h2 = "attachment; filename=\"\"";
  174. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h2));
  175. string h3 = "attachment; filename=\"";
  176. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h3));
  177. string h4 = "attachment;";
  178. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h4));
  179. string h5 = "attachment; filename=aria2.tar.bz2";
  180. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h5));
  181. string h6 = "attachment; filename='aria2.tar.bz2'";
  182. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h6));
  183. string h7 = "attachment; filename='aria2.tar.bz2";
  184. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h7));
  185. string h8 = "attachment; filename=aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT";
  186. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h8));
  187. string h9 = "attachment; filename=\"aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT\"";
  188. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT"), Util::getContentDispositionFilename(h9));
  189. string h10 = "attachment; filename=";
  190. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h10));
  191. string h11 = "attachment; filename=;";
  192. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h11));
  193. }
  194. class Printer {
  195. public:
  196. template<class T>
  197. void operator()(T t) {
  198. cerr << t << ", ";
  199. }
  200. };
  201. void UtilTest::testRandomAlpha() {
  202. string s = Util::randomAlpha(8, new FixedNumberRandomizer());
  203. CPPUNIT_ASSERT_EQUAL(string("AAAAAAAA"), s);
  204. }
  205. void UtilTest::testToUpper() {
  206. string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  207. string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  208. CPPUNIT_ASSERT_EQUAL(upp, Util::toUpper(src));
  209. }
  210. void UtilTest::testToLower() {
  211. string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  212. string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  213. CPPUNIT_ASSERT_EQUAL(upp, Util::toLower(src));
  214. }
  215. #include "SharedHandle.h"
  216. void UtilTest::testUrldecode() {
  217. string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  218. CPPUNIT_ASSERT_EQUAL(string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  219. Util::urldecode(src));
  220. string src2 = "aria2+aria2";
  221. CPPUNIT_ASSERT_EQUAL(string("aria2+aria2"), Util::urldecode(src2));
  222. string src3 = "%5t%20";
  223. CPPUNIT_ASSERT_EQUAL(string("%5t "), Util::urldecode(src3));
  224. string src4 = "%";
  225. CPPUNIT_ASSERT_EQUAL(string("%"), Util::urldecode(src4));
  226. string src5 = "%3";
  227. CPPUNIT_ASSERT_EQUAL(string("%3"), Util::urldecode(src5));
  228. string src6 = "%2f";
  229. CPPUNIT_ASSERT_EQUAL(string("/"), Util::urldecode(src6));
  230. }
  231. void UtilTest::testCountBit() {
  232. CPPUNIT_ASSERT_EQUAL((int32_t)32, Util::countBit(UINT32_MAX));
  233. CPPUNIT_ASSERT_EQUAL((int32_t)8, Util::countBit(255));
  234. }
  235. void UtilTest::testGetRealSize()
  236. {
  237. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, Util::getRealSize("4096M"));
  238. CPPUNIT_ASSERT_EQUAL((int64_t)1024, Util::getRealSize("1K"));
  239. CPPUNIT_ASSERT_EQUAL((int64_t)0, Util::getRealSize(""));
  240. CPPUNIT_ASSERT_EQUAL((int64_t)0, Util::getRealSize("foo"));
  241. }
  242. void UtilTest::testAbbrevSize()
  243. {
  244. CPPUNIT_ASSERT_EQUAL(string("4,096.0Mi"), Util::abbrevSize(4294967296LL));
  245. CPPUNIT_ASSERT_EQUAL(string("1.0Ki"), Util::abbrevSize(1024));
  246. CPPUNIT_ASSERT_EQUAL(string("1,023"), Util::abbrevSize(1023));
  247. CPPUNIT_ASSERT_EQUAL(string("0"), Util::abbrevSize(0));
  248. CPPUNIT_ASSERT_EQUAL(string("1.1Ki"), Util::abbrevSize(1127));
  249. CPPUNIT_ASSERT_EQUAL(string("1.5Mi"), Util::abbrevSize(1572864));
  250. }
  251. void UtilTest::testToStream()
  252. {
  253. ostringstream os;
  254. FileEntryHandle f1 = new FileEntry("aria2.tar.bz2", 12300, 0);
  255. FileEntryHandle f2 = new FileEntry("aria2.txt", 556, 0);
  256. FileEntries entries;
  257. entries.push_back(f1);
  258. entries.push_back(f2);
  259. Util::toStream(os, entries);
  260. CPPUNIT_ASSERT_EQUAL(
  261. string("Files:\n"
  262. "idx|path/length\n"
  263. "===+===========================================================================\n"
  264. " 1|aria2.tar.bz2\n"
  265. " |12.0KiB\n"
  266. "---+---------------------------------------------------------------------------\n"
  267. " 2|aria2.txt\n"
  268. " |556B\n"
  269. "---+---------------------------------------------------------------------------\n"),
  270. os.str());
  271. }
  272. void UtilTest::testIsNumber()
  273. {
  274. CPPUNIT_ASSERT_EQUAL(true, Util::isNumber("000"));
  275. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("a"));
  276. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("0a"));
  277. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(""));
  278. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(" "));
  279. }
  280. void UtilTest::testIsLowercase()
  281. {
  282. CPPUNIT_ASSERT_EQUAL(true, Util::isLowercase("alpha"));
  283. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("Alpha"));
  284. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("1alpha"));
  285. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(""));
  286. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(" "));
  287. }
  288. void UtilTest::testIsUppercase()
  289. {
  290. CPPUNIT_ASSERT_EQUAL(true, Util::isUppercase("ALPHA"));
  291. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("Alpha"));
  292. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("1ALPHA"));
  293. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(""));
  294. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(" "));
  295. }
  296. void UtilTest::testAlphaToNum()
  297. {
  298. CPPUNIT_ASSERT_EQUAL((int32_t)0, Util::alphaToNum("a"));
  299. CPPUNIT_ASSERT_EQUAL((int32_t)0, Util::alphaToNum("aa"));
  300. CPPUNIT_ASSERT_EQUAL((int32_t)1, Util::alphaToNum("b"));
  301. CPPUNIT_ASSERT_EQUAL((int32_t)675, Util::alphaToNum("zz")); // 25*26+25
  302. CPPUNIT_ASSERT_EQUAL((int32_t)675, Util::alphaToNum("ZZ")); // 25*26+25
  303. CPPUNIT_ASSERT_EQUAL((int32_t)0, Util::alphaToNum(""));
  304. }
  305. void UtilTest::testMkdirs()
  306. {
  307. string dir = "/tmp/aria2-UtilTest-testMkdirs";
  308. File d(dir);
  309. if(d.exists()) {
  310. CPPUNIT_ASSERT(d.remove());
  311. }
  312. CPPUNIT_ASSERT(!d.exists());
  313. Util::mkdirs(dir);
  314. CPPUNIT_ASSERT(d.isDir());
  315. string file = "./UtilTest.cc";
  316. File f(file);
  317. CPPUNIT_ASSERT(f.isFile());
  318. try {
  319. Util::mkdirs(file);
  320. CPPUNIT_FAIL("exception must be thrown.");
  321. } catch(DlAbortEx* ex) {
  322. cerr << ex->getMsg() << endl;
  323. delete ex;
  324. }
  325. }