UtilTest.cc 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. #include "util.h"
  2. #include <cstring>
  3. #include <string>
  4. #include <iostream>
  5. #include <cppunit/extensions/HelperMacros.h>
  6. #include "FixedNumberRandomizer.h"
  7. #include "DlAbortEx.h"
  8. #include "BitfieldMan.h"
  9. #include "ByteArrayDiskWriter.h"
  10. #include "FileEntry.h"
  11. #include "File.h"
  12. #include "array_fun.h"
  13. #include "BufferedFile.h"
  14. #include "TestUtil.h"
  15. namespace aria2 {
  16. class UtilTest:public CppUnit::TestFixture {
  17. CPPUNIT_TEST_SUITE(UtilTest);
  18. CPPUNIT_TEST(testStrip);
  19. CPPUNIT_TEST(testDivide);
  20. CPPUNIT_TEST(testSplit);
  21. CPPUNIT_TEST(testEndsWith);
  22. CPPUNIT_TEST(testReplace);
  23. CPPUNIT_TEST(testStartsWith);
  24. // may be moved to other helper class in the future.
  25. CPPUNIT_TEST(testGetContentDispositionFilename);
  26. CPPUNIT_TEST(testRandomAlpha);
  27. CPPUNIT_TEST(testToUpper);
  28. CPPUNIT_TEST(testToLower);
  29. CPPUNIT_TEST(testUppercase);
  30. CPPUNIT_TEST(testLowercase);
  31. CPPUNIT_TEST(testPercentDecode);
  32. CPPUNIT_TEST(testGetRealSize);
  33. CPPUNIT_TEST(testAbbrevSize);
  34. CPPUNIT_TEST(testToStream);
  35. CPPUNIT_TEST(testIsNumber);
  36. CPPUNIT_TEST(testIsLowercase);
  37. CPPUNIT_TEST(testIsUppercase);
  38. CPPUNIT_TEST(testAlphaToNum);
  39. CPPUNIT_TEST(testMkdirs);
  40. CPPUNIT_TEST(testConvertBitfield);
  41. CPPUNIT_TEST(testParseIntRange);
  42. CPPUNIT_TEST(testParseIntRange_invalidRange);
  43. CPPUNIT_TEST(testParseInt);
  44. CPPUNIT_TEST(testParseUInt);
  45. CPPUNIT_TEST(testParseLLInt);
  46. CPPUNIT_TEST(testParseULLInt);
  47. CPPUNIT_TEST(testParseIntNoThrow);
  48. CPPUNIT_TEST(testParseUIntNoThrow);
  49. CPPUNIT_TEST(testParseLLIntNoThrow);
  50. CPPUNIT_TEST(testToString_binaryStream);
  51. CPPUNIT_TEST(testItos);
  52. CPPUNIT_TEST(testUitos);
  53. CPPUNIT_TEST(testNtoh64);
  54. CPPUNIT_TEST(testPercentEncode);
  55. CPPUNIT_TEST(testHtmlEscape);
  56. CPPUNIT_TEST(testJoinPath);
  57. CPPUNIT_TEST(testParseIndexPath);
  58. CPPUNIT_TEST(testCreateIndexPathMap);
  59. CPPUNIT_TEST(testGenerateRandomData);
  60. CPPUNIT_TEST(testFromHex);
  61. CPPUNIT_TEST(testParsePrioritizePieceRange);
  62. CPPUNIT_TEST(testApplyDir);
  63. CPPUNIT_TEST(testFixTaintedBasename);
  64. CPPUNIT_TEST(testIsNumericHost);
  65. CPPUNIT_TEST(testDetectDirTraversal);
  66. CPPUNIT_TEST(testEscapePath);
  67. CPPUNIT_TEST(testGetCidrPrefix);
  68. CPPUNIT_TEST(testInSameCidrBlock);
  69. CPPUNIT_TEST(testIsUtf8String);
  70. CPPUNIT_TEST(testNextParam);
  71. CPPUNIT_TEST_SUITE_END();
  72. private:
  73. public:
  74. void setUp() {
  75. }
  76. void testStrip();
  77. void testDivide();
  78. void testSplit();
  79. void testEndsWith();
  80. void testReplace();
  81. void testStartsWith();
  82. // may be moved to other helper class in the future.
  83. void testGetContentDispositionFilename();
  84. void testRandomAlpha();
  85. void testToUpper();
  86. void testToLower();
  87. void testUppercase();
  88. void testLowercase();
  89. void testPercentDecode();
  90. void testGetRealSize();
  91. void testAbbrevSize();
  92. void testToStream();
  93. void testIsNumber();
  94. void testIsLowercase();
  95. void testIsUppercase();
  96. void testAlphaToNum();
  97. void testMkdirs();
  98. void testConvertBitfield();
  99. void testParseIntRange();
  100. void testParseIntRange_invalidRange();
  101. void testParseInt();
  102. void testParseUInt();
  103. void testParseLLInt();
  104. void testParseULLInt();
  105. void testParseIntNoThrow();
  106. void testParseUIntNoThrow();
  107. void testParseLLIntNoThrow();
  108. void testToString_binaryStream();
  109. void testItos();
  110. void testUitos();
  111. void testNtoh64();
  112. void testPercentEncode();
  113. void testHtmlEscape();
  114. void testJoinPath();
  115. void testParseIndexPath();
  116. void testCreateIndexPathMap();
  117. void testGenerateRandomData();
  118. void testFromHex();
  119. void testParsePrioritizePieceRange();
  120. void testApplyDir();
  121. void testFixTaintedBasename();
  122. void testIsNumericHost();
  123. void testDetectDirTraversal();
  124. void testEscapePath();
  125. void testGetCidrPrefix();
  126. void testInSameCidrBlock();
  127. void testIsUtf8String();
  128. void testNextParam();
  129. };
  130. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  131. void UtilTest::testStrip()
  132. {
  133. std::string str1 = "aria2";
  134. CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2"));
  135. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2"));
  136. CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2 "));
  137. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
  138. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
  139. std::string str2 = "aria2 debut";
  140. CPPUNIT_ASSERT_EQUAL(str2, util::strip("aria2 debut"));
  141. CPPUNIT_ASSERT_EQUAL(str2, util::strip(" aria2 debut "));
  142. std::string str3 = "";
  143. CPPUNIT_ASSERT_EQUAL(str3, util::strip(""));
  144. CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
  145. CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
  146. std::string str4 = "A";
  147. CPPUNIT_ASSERT_EQUAL(str4, util::strip("A"));
  148. CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
  149. CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
  150. }
  151. void UtilTest::testDivide() {
  152. std::pair<std::string, std::string> p1;
  153. util::divide(p1, "name=value", '=');
  154. CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
  155. CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
  156. util::divide(p1, " name = value ", '=');
  157. CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
  158. CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
  159. util::divide(p1, "=value", '=');
  160. CPPUNIT_ASSERT_EQUAL(std::string(""), p1.first);
  161. CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
  162. util::divide(p1, "name=", '=');
  163. CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
  164. CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second);
  165. util::divide(p1, "name", '=');
  166. CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
  167. CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second);
  168. }
  169. void UtilTest::testSplit() {
  170. std::vector<std::string> v;
  171. util::split("k1; k2;; k3", std::back_inserter(v), ";", true);
  172. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  173. std::vector<std::string>::iterator itr = v.begin();
  174. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  175. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  176. CPPUNIT_ASSERT_EQUAL(std::string("k3"), *itr++);
  177. v.clear();
  178. util::split("k1; k2; k3",
  179. std::back_inserter(v), ";");
  180. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  181. itr = v.begin();
  182. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  183. CPPUNIT_ASSERT_EQUAL(std::string(" k2"), *itr++);
  184. CPPUNIT_ASSERT_EQUAL(std::string(" k3"), *itr++);
  185. v.clear();
  186. util::split("k=v", std::back_inserter(v), ";", false, true);
  187. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  188. itr = v.begin();
  189. CPPUNIT_ASSERT_EQUAL(std::string("k=v"), *itr++);
  190. v.clear();
  191. util::split(";;k1;;k2;", std::back_inserter(v), ";", false, true);
  192. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  193. itr = v.begin();
  194. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  195. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  196. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  197. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  198. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  199. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  200. v.clear();
  201. util::split(";;k1;;k2;", std::back_inserter(v), ";");
  202. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  203. itr = v.begin();
  204. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  205. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  206. v.clear();
  207. util::split("k; ", std::back_inserter(v), ";");
  208. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  209. itr = v.begin();
  210. CPPUNIT_ASSERT_EQUAL(std::string("k"), *itr++);
  211. CPPUNIT_ASSERT_EQUAL(std::string(" "), *itr++);
  212. v.clear();
  213. util::split(" ", std::back_inserter(v), ";", true, true);
  214. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  215. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  216. v.clear();
  217. util::split(" ", std::back_inserter(v), ";", true);
  218. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  219. v.clear();
  220. util::split(" ", std::back_inserter(v), ";");
  221. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  222. CPPUNIT_ASSERT_EQUAL(std::string(" "), v[0]);
  223. v.clear();
  224. util::split(";", std::back_inserter(v), ";");
  225. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  226. v.clear();
  227. util::split(";", std::back_inserter(v), ";", false, true);
  228. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  229. itr = v.begin();
  230. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  231. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  232. v.clear();
  233. util::split("", std::back_inserter(v), ";", false, true);
  234. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  235. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  236. }
  237. void UtilTest::testEndsWith() {
  238. std::string target = "abcdefg";
  239. std::string part = "fg";
  240. CPPUNIT_ASSERT(util::endsWith(target, part));
  241. target = "abdefg";
  242. part = "g";
  243. CPPUNIT_ASSERT(util::endsWith(target, part));
  244. target = "abdefg";
  245. part = "eg";
  246. CPPUNIT_ASSERT(!util::endsWith(target, part));
  247. target = "g";
  248. part = "eg";
  249. CPPUNIT_ASSERT(!util::endsWith(target, part));
  250. target = "g";
  251. part = "g";
  252. CPPUNIT_ASSERT(util::endsWith(target, part));
  253. target = "g";
  254. part = "";
  255. CPPUNIT_ASSERT(util::endsWith(target, part));
  256. target = "";
  257. part = "";
  258. CPPUNIT_ASSERT(util::endsWith(target, part));
  259. target = "";
  260. part = "g";
  261. CPPUNIT_ASSERT(!util::endsWith(target, part));
  262. }
  263. void UtilTest::testReplace() {
  264. CPPUNIT_ASSERT_EQUAL(std::string("abc\n"), util::replace("abc\r\n", "\r", ""));
  265. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc\r\n", "\r\n", ""));
  266. CPPUNIT_ASSERT_EQUAL(std::string(""), util::replace("", "\r\n", ""));
  267. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc", "", "a"));
  268. CPPUNIT_ASSERT_EQUAL(std::string("xbc"), util::replace("abc", "a", "x"));
  269. }
  270. void UtilTest::testStartsWith() {
  271. std::string target;
  272. std::string part;
  273. target = "abcdefg";
  274. part = "abc";
  275. CPPUNIT_ASSERT(util::startsWith(target, part));
  276. target = "abcdefg";
  277. part = "abx";
  278. CPPUNIT_ASSERT(!util::startsWith(target, part));
  279. target = "abcdefg";
  280. part = "bcd";
  281. CPPUNIT_ASSERT(!util::startsWith(target, part));
  282. target = "";
  283. part = "a";
  284. CPPUNIT_ASSERT(!util::startsWith(target, part));
  285. target = "";
  286. part = "";
  287. CPPUNIT_ASSERT(util::startsWith(target, part));
  288. target = "a";
  289. part = "";
  290. CPPUNIT_ASSERT(util::startsWith(target, part));
  291. target = "a";
  292. part = "a";
  293. CPPUNIT_ASSERT(util::startsWith(target, part));
  294. }
  295. void UtilTest::testGetContentDispositionFilename() {
  296. std::string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  297. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h1));
  298. std::string h2 = "attachment; filename=\"\"";
  299. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h2));
  300. std::string h3 = "attachment; filename=\"";
  301. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h3));
  302. std::string h4 = "attachment;";
  303. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h4));
  304. std::string h5 = "attachment; filename=aria2.tar.bz2";
  305. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h5));
  306. std::string h6 = "attachment; filename='aria2.tar.bz2'";
  307. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h6));
  308. std::string h7 = "attachment; filename='aria2.tar.bz2";
  309. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h7));
  310. std::string h8 = "attachment; filename=aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT";
  311. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h8));
  312. std::string h9 = "attachment; filename=\"aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT\"";
  313. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT"),
  314. util::getContentDispositionFilename(h9));
  315. std::string h10 = "attachment; filename=";
  316. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h10));
  317. std::string h11 = "attachment; filename=;";
  318. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h11));
  319. std::string filenameWithDir = "attachment; filename=dir/file";
  320. CPPUNIT_ASSERT_EQUAL(std::string(""),
  321. util::getContentDispositionFilename(filenameWithDir));
  322. std::string semicolonInside = "attachment; filename=\"foo;bar\"";
  323. CPPUNIT_ASSERT_EQUAL(std::string("foo;bar"),
  324. util::getContentDispositionFilename(semicolonInside));
  325. CPPUNIT_ASSERT_EQUAL
  326. (std::string(""),
  327. util::getContentDispositionFilename("filename=\"%2E%2E%2Ffoo.html\""));
  328. // RFC2231 Section4
  329. std::string extparam2 = "filename*=''aria2";
  330. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  331. util::getContentDispositionFilename(extparam2));
  332. std::string extparam3 = "filename*='''";
  333. CPPUNIT_ASSERT_EQUAL(std::string(""),
  334. util::getContentDispositionFilename(extparam3));
  335. std::string extparam4 = "filename*='aria2";
  336. CPPUNIT_ASSERT_EQUAL(std::string(""),
  337. util::getContentDispositionFilename(extparam4));
  338. std::string extparam5 = "filename*='''aria2";
  339. CPPUNIT_ASSERT_EQUAL(std::string(""),
  340. util::getContentDispositionFilename(extparam5));
  341. std::string extparam6 = "filename*";
  342. CPPUNIT_ASSERT_EQUAL(std::string(""),
  343. util::getContentDispositionFilename(extparam6));
  344. std::string extparam7 = "filename*=UTF-8''aria2;filename=hello%20world";
  345. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  346. util::getContentDispositionFilename(extparam7));
  347. std::string extparam8 = "filename=aria2;filename*=UTF-8''hello%20world";
  348. CPPUNIT_ASSERT_EQUAL(std::string("hello world"),
  349. util::getContentDispositionFilename(extparam8));
  350. std::string extparam9 = "filename*=ISO-8859-1''%A3";
  351. std::string extparam9ans;
  352. extparam9ans += 0xc2;
  353. extparam9ans += 0xa3;
  354. CPPUNIT_ASSERT_EQUAL(extparam9ans,
  355. util::getContentDispositionFilename(extparam9));
  356. CPPUNIT_ASSERT_EQUAL
  357. (std::string(""),
  358. util::getContentDispositionFilename("filename*=UTF-8''foo%2F.html"));
  359. CPPUNIT_ASSERT_EQUAL
  360. (std::string("foo.html"),
  361. util::getContentDispositionFilename("filename*=UTF-8'';filename=\"foo.html\""));
  362. CPPUNIT_ASSERT_EQUAL
  363. (std::string(""),
  364. util::getContentDispositionFilename("filename*=UTF-8''%2E%2E%2Ffoo.html"));
  365. // Tests from http://greenbytes.de/tech/tc2231/
  366. // attwithasciifnescapedchar
  367. CPPUNIT_ASSERT_EQUAL
  368. (std::string("foo.html"),
  369. util::getContentDispositionFilename("filename=\"f\\oo.html\""));
  370. // attwithasciifilenameucase
  371. CPPUNIT_ASSERT_EQUAL
  372. (std::string("foo.html"),
  373. util::getContentDispositionFilename("FILENAME=\"foo.html\""));
  374. // attwithisofn2231iso
  375. CPPUNIT_ASSERT_EQUAL
  376. (std::string("foo-ä.html"),
  377. util::getContentDispositionFilename("filename*=iso-8859-1''foo-%E4.html"));
  378. // attwithfn2231utf8
  379. CPPUNIT_ASSERT_EQUAL
  380. (std::string("foo-ä-€.html"),
  381. util::getContentDispositionFilename
  382. ("filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html"));
  383. // attwithfn2231utf8-bad
  384. CPPUNIT_ASSERT_EQUAL
  385. (std::string(""),
  386. util::getContentDispositionFilename
  387. ("filename*=iso-8859-1''foo-%c3%a4-%e2%82%ac.html"));
  388. // attwithfn2231ws1
  389. CPPUNIT_ASSERT_EQUAL
  390. (std::string(""),
  391. util::getContentDispositionFilename("filename *=UTF-8''foo-%c3%a4.html"));
  392. // attwithfn2231ws2
  393. CPPUNIT_ASSERT_EQUAL
  394. (std::string("foo-ä.html"),
  395. util::getContentDispositionFilename("filename*= UTF-8''foo-%c3%a4.html"));
  396. // attwithfn2231ws3
  397. CPPUNIT_ASSERT_EQUAL
  398. (std::string("foo-ä.html"),
  399. util::getContentDispositionFilename("filename* =UTF-8''foo-%c3%a4.html"));
  400. // attwithfn2231quot
  401. CPPUNIT_ASSERT_EQUAL
  402. (std::string(""),
  403. util::getContentDispositionFilename
  404. ("filename*=\"UTF-8''foo-%c3%a4.html\""));
  405. }
  406. class Printer {
  407. public:
  408. template<class T>
  409. void operator()(T t) {
  410. std::cerr << t << ", ";
  411. }
  412. };
  413. void UtilTest::testRandomAlpha() {
  414. SharedHandle<Randomizer> rand(new FixedNumberRandomizer());
  415. std::string s = util::randomAlpha(8, rand);
  416. CPPUNIT_ASSERT_EQUAL(std::string("AAAAAAAA"), s);
  417. }
  418. void UtilTest::testToUpper() {
  419. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  420. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  421. CPPUNIT_ASSERT_EQUAL(upp, util::toUpper(src));
  422. }
  423. void UtilTest::testToLower() {
  424. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  425. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  426. CPPUNIT_ASSERT_EQUAL(upp, util::toLower(src));
  427. }
  428. void UtilTest::testUppercase() {
  429. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  430. std::string ans = "608CABC0F2FA18C260CAFD974516865C772363D5";
  431. util::uppercase(src);
  432. CPPUNIT_ASSERT_EQUAL(ans, src);
  433. }
  434. void UtilTest::testLowercase() {
  435. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  436. std::string ans = "608cabc0f2fa18c260cafd974516865c772363d5";
  437. util::lowercase(src);
  438. CPPUNIT_ASSERT_EQUAL(ans, src);
  439. }
  440. void UtilTest::testPercentDecode() {
  441. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  442. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  443. util::percentDecode(src));
  444. std::string src2 = "aria2+aria2";
  445. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"), util::percentDecode(src2));
  446. std::string src3 = "%5t%20";
  447. CPPUNIT_ASSERT_EQUAL(std::string("%5t "), util::percentDecode(src3));
  448. std::string src4 = "%";
  449. CPPUNIT_ASSERT_EQUAL(std::string("%"), util::percentDecode(src4));
  450. std::string src5 = "%3";
  451. CPPUNIT_ASSERT_EQUAL(std::string("%3"), util::percentDecode(src5));
  452. std::string src6 = "%2f";
  453. CPPUNIT_ASSERT_EQUAL(std::string("/"), util::percentDecode(src6));
  454. }
  455. void UtilTest::testGetRealSize()
  456. {
  457. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096M"));
  458. CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1K"));
  459. try {
  460. util::getRealSize("");
  461. CPPUNIT_FAIL("exception must be thrown.");
  462. } catch(Exception& e) {
  463. std::cerr << e.stackTrace();
  464. }
  465. try {
  466. util::getRealSize("foo");
  467. CPPUNIT_FAIL("exception must be thrown.");
  468. } catch(Exception& e) {
  469. std::cerr << e.stackTrace();
  470. }
  471. try {
  472. util::getRealSize("-1");
  473. CPPUNIT_FAIL("exception must be thrown.");
  474. } catch(Exception& e) {
  475. std::cerr << e.stackTrace();
  476. }
  477. try {
  478. util::getRealSize("9223372036854775807K");
  479. CPPUNIT_FAIL("exception must be thrown.");
  480. } catch(Exception& e) {
  481. std::cerr << e.stackTrace();
  482. }
  483. try {
  484. util::getRealSize("9223372036854775807M");
  485. CPPUNIT_FAIL("exception must be thrown.");
  486. } catch(Exception& e) {
  487. std::cerr << e.stackTrace();
  488. }
  489. }
  490. void UtilTest::testAbbrevSize()
  491. {
  492. CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), util::abbrevSize(4294967296LL));
  493. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1024));
  494. CPPUNIT_ASSERT_EQUAL(std::string("1,023"), util::abbrevSize(1023));
  495. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::abbrevSize(0));
  496. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), util::abbrevSize(1127));
  497. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), util::abbrevSize(1572864));
  498. }
  499. void UtilTest::testToStream()
  500. {
  501. std::ostringstream os;
  502. SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  503. SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  504. std::deque<SharedHandle<FileEntry> > entries;
  505. entries.push_back(f1);
  506. entries.push_back(f2);
  507. std::string filename = A2_TEST_OUT_DIR"/aria2_UtilTest_testToStream";
  508. BufferedFile fp(filename, BufferedFile::WRITE);
  509. util::toStream(entries.begin(), entries.end(), fp);
  510. fp.close();
  511. CPPUNIT_ASSERT_EQUAL(
  512. std::string("Files:\n"
  513. "idx|path/length\n"
  514. "===+===========================================================================\n"
  515. " 1|aria2.tar.bz2\n"
  516. " |12.0KiB (12,300)\n"
  517. "---+---------------------------------------------------------------------------\n"
  518. " 2|aria2.txt\n"
  519. " |556B (556)\n"
  520. "---+---------------------------------------------------------------------------\n"),
  521. readFile(filename));
  522. }
  523. void UtilTest::testIsNumber()
  524. {
  525. CPPUNIT_ASSERT_EQUAL(true, util::isNumber("000"));
  526. CPPUNIT_ASSERT_EQUAL(false, util::isNumber("a"));
  527. CPPUNIT_ASSERT_EQUAL(false, util::isNumber("0a"));
  528. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(""));
  529. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(" "));
  530. }
  531. void UtilTest::testIsLowercase()
  532. {
  533. CPPUNIT_ASSERT_EQUAL(true, util::isLowercase("alpha"));
  534. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase("Alpha"));
  535. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase("1alpha"));
  536. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(""));
  537. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(" "));
  538. }
  539. void UtilTest::testIsUppercase()
  540. {
  541. CPPUNIT_ASSERT_EQUAL(true, util::isUppercase("ALPHA"));
  542. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase("Alpha"));
  543. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase("1ALPHA"));
  544. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(""));
  545. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(" "));
  546. }
  547. void UtilTest::testAlphaToNum()
  548. {
  549. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("a"));
  550. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("aa"));
  551. CPPUNIT_ASSERT_EQUAL(1U, util::alphaToNum("b"));
  552. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("zz")); // 25*26+25
  553. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("ZZ")); // 25*26+25
  554. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum(""));
  555. CPPUNIT_ASSERT_EQUAL(4294967295U, util::alphaToNum("NXMRLXV"));
  556. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("NXMRLXW")); // uint32_t overflow
  557. }
  558. void UtilTest::testMkdirs()
  559. {
  560. std::string dir = A2_TEST_OUT_DIR"/aria2-UtilTest-testMkdirs";
  561. File d(dir);
  562. if(d.exists()) {
  563. CPPUNIT_ASSERT(d.remove());
  564. }
  565. CPPUNIT_ASSERT(!d.exists());
  566. util::mkdirs(dir);
  567. CPPUNIT_ASSERT(d.isDir());
  568. std::string file = A2_TEST_DIR"/UtilTest.cc";
  569. File f(file);
  570. CPPUNIT_ASSERT(f.isFile());
  571. try {
  572. util::mkdirs(file);
  573. CPPUNIT_FAIL("exception must be thrown.");
  574. } catch(DlAbortEx& ex) {
  575. std::cerr << ex.stackTrace() << std::endl;
  576. }
  577. }
  578. void UtilTest::testConvertBitfield()
  579. {
  580. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  581. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  582. srcBitfield.setAllBit();
  583. srcBitfield.unsetBit(2);// <- range [768, 1152)
  584. // which corresponds to the index [1,2] in destBitfield
  585. util::convertBitfield(&destBitfield, &srcBitfield);
  586. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  587. util::toHex(destBitfield.getBitfield(),
  588. destBitfield.getBitfieldLength()));
  589. }
  590. void UtilTest::testParseIntRange()
  591. {
  592. IntSequence seq = util::parseIntRange("1,3-8,10");
  593. CPPUNIT_ASSERT(seq.hasNext());
  594. CPPUNIT_ASSERT_EQUAL((int32_t)1, seq.next());
  595. CPPUNIT_ASSERT(seq.hasNext());
  596. CPPUNIT_ASSERT_EQUAL((int32_t)3, seq.next());
  597. CPPUNIT_ASSERT(seq.hasNext());
  598. CPPUNIT_ASSERT_EQUAL((int32_t)4, seq.next());
  599. CPPUNIT_ASSERT(seq.hasNext());
  600. CPPUNIT_ASSERT_EQUAL((int32_t)5, seq.next());
  601. CPPUNIT_ASSERT(seq.hasNext());
  602. CPPUNIT_ASSERT_EQUAL((int32_t)6, seq.next());
  603. CPPUNIT_ASSERT(seq.hasNext());
  604. CPPUNIT_ASSERT_EQUAL((int32_t)7, seq.next());
  605. CPPUNIT_ASSERT(seq.hasNext());
  606. CPPUNIT_ASSERT_EQUAL((int32_t)8, seq.next());
  607. CPPUNIT_ASSERT(seq.hasNext());
  608. CPPUNIT_ASSERT_EQUAL((int32_t)10, seq.next());
  609. CPPUNIT_ASSERT(!seq.hasNext());
  610. CPPUNIT_ASSERT_EQUAL((int32_t)0, seq.next());
  611. }
  612. void UtilTest::testParseIntRange_invalidRange()
  613. {
  614. try {
  615. IntSequence seq = util::parseIntRange("-1");
  616. CPPUNIT_FAIL("exception must be thrown.");
  617. } catch(Exception& e) {
  618. std::cerr << e.stackTrace();
  619. }
  620. try {
  621. IntSequence seq = util::parseIntRange("2147483648");
  622. CPPUNIT_FAIL("exception must be thrown.");
  623. } catch(Exception& e) {
  624. std::cerr << e.stackTrace();
  625. }
  626. try {
  627. IntSequence seq = util::parseIntRange("2147483647-2147483648");
  628. CPPUNIT_FAIL("exception must be thrown.");
  629. } catch(Exception& e) {
  630. std::cerr << e.stackTrace();
  631. }
  632. try {
  633. IntSequence seq = util::parseIntRange("1-2x");
  634. CPPUNIT_FAIL("exception must be thrown.");
  635. } catch(Exception& e) {
  636. std::cerr << e.stackTrace();
  637. }
  638. try {
  639. IntSequence seq = util::parseIntRange("3x-4");
  640. CPPUNIT_FAIL("exception must be thrown.");
  641. } catch(Exception& e) {
  642. std::cerr << e.stackTrace();
  643. }
  644. }
  645. void UtilTest::testParseInt()
  646. {
  647. CPPUNIT_ASSERT_EQUAL(-1, util::parseInt(" -1 "));
  648. CPPUNIT_ASSERT_EQUAL(2147483647, util::parseInt("2147483647"));
  649. try {
  650. util::parseInt("2147483648");
  651. CPPUNIT_FAIL("exception must be thrown.");
  652. } catch(Exception& e) {
  653. std::cerr << e.stackTrace();
  654. }
  655. try {
  656. util::parseInt("-2147483649");
  657. CPPUNIT_FAIL("exception must be thrown.");
  658. } catch(Exception& e) {
  659. std::cerr << e.stackTrace();
  660. }
  661. try {
  662. util::parseInt("12x");
  663. CPPUNIT_FAIL("exception must be thrown.");
  664. } catch(Exception& e) {
  665. std::cerr << e.stackTrace();
  666. }
  667. try {
  668. util::parseInt("");
  669. CPPUNIT_FAIL("exception must be thrown.");
  670. } catch(Exception& e) {
  671. std::cerr << e.stackTrace();
  672. }
  673. }
  674. void UtilTest::testParseUInt()
  675. {
  676. CPPUNIT_ASSERT_EQUAL(4294967295U, util::parseUInt(" 4294967295 "));
  677. try {
  678. util::parseUInt("-1");
  679. CPPUNIT_FAIL("exception must be thrown.");
  680. } catch(Exception& e) {
  681. std::cerr << e.stackTrace();
  682. }
  683. try {
  684. util::parseUInt("4294967296");
  685. CPPUNIT_FAIL("exception must be thrown.");
  686. } catch(Exception& e) {
  687. std::cerr << e.stackTrace();
  688. }
  689. }
  690. void UtilTest::testParseLLInt()
  691. {
  692. CPPUNIT_ASSERT_EQUAL((int64_t)-1LL, util::parseLLInt(" -1 "));
  693. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
  694. util::parseLLInt("9223372036854775807"));
  695. try {
  696. util::parseLLInt("9223372036854775808");
  697. CPPUNIT_FAIL("exception must be thrown.");
  698. } catch(Exception& e) {
  699. std::cerr << e.stackTrace();
  700. }
  701. try {
  702. util::parseLLInt("-9223372036854775809");
  703. CPPUNIT_FAIL("exception must be thrown.");
  704. } catch(Exception& e) {
  705. std::cerr << e.stackTrace();
  706. }
  707. try {
  708. util::parseLLInt("12x");
  709. CPPUNIT_FAIL("exception must be thrown.");
  710. } catch(Exception& e) {
  711. std::cerr << e.stackTrace();
  712. }
  713. try {
  714. util::parseLLInt("");
  715. CPPUNIT_FAIL("exception must be thrown.");
  716. } catch(Exception& e) {
  717. std::cerr << e.stackTrace();
  718. }
  719. }
  720. void UtilTest::testParseULLInt()
  721. {
  722. CPPUNIT_ASSERT_EQUAL((uint64_t)18446744073709551615ULL,
  723. util::parseULLInt("18446744073709551615"));
  724. try {
  725. util::parseUInt("-1");
  726. CPPUNIT_FAIL("exception must be thrown.");
  727. } catch(Exception& e) {
  728. std::cerr << e.stackTrace();
  729. }
  730. try {
  731. util::parseLLInt("18446744073709551616");
  732. CPPUNIT_FAIL("exception must be thrown.");
  733. } catch(Exception& e) {
  734. std::cerr << e.stackTrace();
  735. }
  736. }
  737. void UtilTest::testParseIntNoThrow()
  738. {
  739. int32_t n;
  740. CPPUNIT_ASSERT(util::parseIntNoThrow(n, " -1 "));
  741. CPPUNIT_ASSERT_EQUAL((int32_t)-1, n);
  742. CPPUNIT_ASSERT(util::parseIntNoThrow(n, "2147483647"));
  743. CPPUNIT_ASSERT_EQUAL((int32_t)2147483647, n);
  744. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, "2147483648"));
  745. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, "-2147483649"));
  746. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, "12x"));
  747. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, ""));
  748. }
  749. void UtilTest::testParseUIntNoThrow()
  750. {
  751. uint32_t n;
  752. CPPUNIT_ASSERT(util::parseUIntNoThrow(n, " 4294967295 "));
  753. CPPUNIT_ASSERT_EQUAL((uint32_t)UINT32_MAX, n);
  754. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, "4294967296"));
  755. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, "-1"));
  756. }
  757. void UtilTest::testParseLLIntNoThrow()
  758. {
  759. int64_t n;
  760. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, " 9223372036854775807 "));
  761. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MAX, n);
  762. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, "9223372036854775808"));
  763. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, "-9223372036854775808"));
  764. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MIN, n);
  765. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, "-9223372036854775809"));
  766. }
  767. void UtilTest::testToString_binaryStream()
  768. {
  769. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  770. std::string data(16*1024+256, 'a');
  771. dw->initAndOpenFile();
  772. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  773. std::string readData = util::toString(dw);
  774. CPPUNIT_ASSERT_EQUAL(data, readData);
  775. }
  776. void UtilTest::testItos()
  777. {
  778. {
  779. int i = 0;
  780. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::itos(i));
  781. }
  782. {
  783. int i = 100;
  784. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i, true));
  785. }
  786. {
  787. int i = 100;
  788. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i));
  789. }
  790. {
  791. int i = 12345;
  792. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), util::itos(i, true));
  793. }
  794. {
  795. int i = 12345;
  796. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::itos(i));
  797. }
  798. {
  799. int i = -12345;
  800. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), util::itos(i, true));
  801. }
  802. {
  803. int64_t i = INT64_MAX;
  804. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"),
  805. util::itos(i, true));
  806. }
  807. {
  808. int64_t i = INT64_MIN;
  809. CPPUNIT_ASSERT_EQUAL(std::string("-9,223,372,036,854,775,808"),
  810. util::itos(i, true));
  811. }
  812. }
  813. void UtilTest::testUitos()
  814. {
  815. {
  816. uint16_t i = 12345;
  817. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::uitos(i));
  818. }
  819. {
  820. int16_t i = -12345;
  821. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), util::uitos(i));
  822. }
  823. }
  824. void UtilTest::testNtoh64()
  825. {
  826. uint64_t x = 0xff00ff00ee00ee00LL;
  827. #ifdef WORDS_BIGENDIAN
  828. CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
  829. CPPUNIT_ASSERT_EQUAL(x, hton64(x));
  830. #else // !WORDS_BIGENDIAN
  831. uint64_t y = 0x00ee00ee00ff00ffLL;
  832. CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
  833. CPPUNIT_ASSERT_EQUAL(x, hton64(y));
  834. #endif // !WORDS_BIGENDIAN
  835. }
  836. void UtilTest::testPercentEncode()
  837. {
  838. CPPUNIT_ASSERT_EQUAL
  839. (std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
  840. util::percentEncode(":/?#[]@!%&'()*+,;="));
  841. std::string unreserved =
  842. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  843. "abcdefghijklmnopqrstuvwxyz"
  844. "0123456789"
  845. "-._~";
  846. CPPUNIT_ASSERT_EQUAL(unreserved, util::percentEncode(unreserved));
  847. CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::percentEncode("1^A "));
  848. }
  849. void UtilTest::testHtmlEscape()
  850. {
  851. CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),
  852. util::htmlEscape("aria2<>\"'util"));
  853. }
  854. void UtilTest::testJoinPath()
  855. {
  856. const std::string dir1dir2file[] = { "dir1", "dir2", "file" };
  857. CPPUNIT_ASSERT_EQUAL
  858. (std::string("dir1/dir2/file"),
  859. util::joinPath(vbegin(dir1dir2file), vend(dir1dir2file)));
  860. const std::string dirparentfile[] = { "dir", "..", "file" };
  861. CPPUNIT_ASSERT_EQUAL
  862. (std::string("file"),
  863. util::joinPath(vbegin(dirparentfile), vend(dirparentfile)));
  864. const std::string dirparentparentfile[] = { "dir", "..", "..", "file" };
  865. CPPUNIT_ASSERT_EQUAL
  866. (std::string("file"),
  867. util::joinPath(vbegin(dirparentparentfile), vend(dirparentparentfile)));
  868. const std::string dirdotfile[] = { "dir", ".", "file" };
  869. CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
  870. util::joinPath(vbegin(dirdotfile), vend(dirdotfile)));
  871. const std::string empty[] = {};
  872. CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(&empty[0], &empty[0]));
  873. const std::string parentdot[] = { "..", "." };
  874. CPPUNIT_ASSERT_EQUAL(std::string(""),
  875. util::joinPath(vbegin(parentdot), vend(parentdot)));
  876. }
  877. void UtilTest::testParseIndexPath()
  878. {
  879. std::map<size_t, std::string>::value_type p = util::parseIndexPath("1=foo");
  880. CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
  881. CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
  882. try {
  883. util::parseIndexPath("1X=foo");
  884. CPPUNIT_FAIL("exception must be thrown.");
  885. } catch(Exception& e) {
  886. // success
  887. }
  888. try {
  889. util::parseIndexPath("1=");
  890. CPPUNIT_FAIL("exception must be thrown.");
  891. } catch(Exception& e) {
  892. // success
  893. }
  894. }
  895. void UtilTest::testCreateIndexPathMap()
  896. {
  897. std::stringstream in
  898. ("1=/tmp/myfile\n"
  899. "100=/myhome/mypicture.png\n");
  900. std::map<size_t, std::string> m = util::createIndexPathMap(in);
  901. CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
  902. CPPUNIT_ASSERT(m.find(1) != m.end());
  903. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[1]);
  904. CPPUNIT_ASSERT(m.find(100) != m.end());
  905. CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[100]);
  906. }
  907. void UtilTest::testGenerateRandomData()
  908. {
  909. unsigned char data1[20];
  910. util::generateRandomData(data1, sizeof(data1));
  911. unsigned char data2[20];
  912. util::generateRandomData(data2, sizeof(data2));
  913. CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
  914. }
  915. void UtilTest::testFromHex()
  916. {
  917. std::string src;
  918. std::string dest;
  919. src = "0011fF";
  920. dest = util::fromHex(src);
  921. CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
  922. CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
  923. CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
  924. CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
  925. src = "0011f";
  926. CPPUNIT_ASSERT(util::fromHex(src).empty());
  927. src = "001g";
  928. CPPUNIT_ASSERT(util::fromHex(src).empty());
  929. }
  930. void UtilTest::testParsePrioritizePieceRange()
  931. {
  932. // piece index
  933. // 0 1 2 3 4 5 6 7
  934. // | | | |
  935. // file1 | | |
  936. // | | |
  937. // file2 | |
  938. // file3 |
  939. // | |
  940. // file4 |
  941. size_t pieceLength = 1024;
  942. std::vector<SharedHandle<FileEntry> > entries(4, SharedHandle<FileEntry>());
  943. entries[0].reset(new FileEntry("file1", 1024, 0));
  944. entries[1].reset(new FileEntry("file2",2560,entries[0]->getLastOffset()));
  945. entries[2].reset(new FileEntry("file3",0,entries[1]->getLastOffset()));
  946. entries[3].reset(new FileEntry("file4",3584,entries[2]->getLastOffset()));
  947. std::vector<size_t> result;
  948. util::parsePrioritizePieceRange(result, "head=1", entries, pieceLength);
  949. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  950. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  951. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  952. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  953. result.clear();
  954. util::parsePrioritizePieceRange(result, "tail=1", entries, pieceLength);
  955. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  956. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  957. CPPUNIT_ASSERT_EQUAL((size_t)3, result[1]);
  958. CPPUNIT_ASSERT_EQUAL((size_t)6, result[2]);
  959. result.clear();
  960. util::parsePrioritizePieceRange(result, "head=1K", entries, pieceLength);
  961. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  962. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  963. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  964. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  965. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  966. result.clear();
  967. util::parsePrioritizePieceRange(result, "head", entries, pieceLength, 1024);
  968. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  969. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  970. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  971. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  972. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  973. result.clear();
  974. util::parsePrioritizePieceRange(result, "tail=1K", entries, pieceLength);
  975. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  976. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  977. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  978. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  979. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  980. result.clear();
  981. util::parsePrioritizePieceRange(result, "tail", entries, pieceLength, 1024);
  982. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  983. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  984. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  985. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  986. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  987. result.clear();
  988. util::parsePrioritizePieceRange(result, "head=1,tail=1", entries, pieceLength);
  989. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  990. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  991. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  992. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  993. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  994. result.clear();
  995. util::parsePrioritizePieceRange(result, "head=300M,tail=300M",
  996. entries, pieceLength);
  997. CPPUNIT_ASSERT_EQUAL((size_t)7, result.size());
  998. for(size_t i = 0; i < 7; ++i) {
  999. CPPUNIT_ASSERT_EQUAL(i, result[i]);
  1000. }
  1001. result.clear();
  1002. util::parsePrioritizePieceRange(result, "", entries, pieceLength);
  1003. CPPUNIT_ASSERT(result.empty());
  1004. }
  1005. void UtilTest::testApplyDir()
  1006. {
  1007. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir("", "pred"));
  1008. CPPUNIT_ASSERT_EQUAL(std::string("/pred"), util::applyDir("/", "pred"));
  1009. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir(".", "pred"));
  1010. CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred"));
  1011. }
  1012. void UtilTest::testFixTaintedBasename()
  1013. {
  1014. CPPUNIT_ASSERT_EQUAL(std::string("a%2Fb"), util::fixTaintedBasename("a/b"));
  1015. #ifdef __MINGW32__
  1016. CPPUNIT_ASSERT_EQUAL(std::string("a%5Cb"), util::fixTaintedBasename("a\\b"));
  1017. #else // !__MINGW32__
  1018. CPPUNIT_ASSERT_EQUAL(std::string("a\\b"), util::fixTaintedBasename("a\\b"));
  1019. #endif // !__MINGW32__
  1020. }
  1021. void UtilTest::testIsNumericHost()
  1022. {
  1023. CPPUNIT_ASSERT(util::isNumericHost("192.168.0.1"));
  1024. CPPUNIT_ASSERT(!util::isNumericHost("aria2.sf.net"));
  1025. CPPUNIT_ASSERT(util::isNumericHost("::1"));
  1026. }
  1027. void UtilTest::testDetectDirTraversal()
  1028. {
  1029. CPPUNIT_ASSERT(util::detectDirTraversal("/foo"));
  1030. CPPUNIT_ASSERT(util::detectDirTraversal("./foo"));
  1031. CPPUNIT_ASSERT(util::detectDirTraversal("../foo"));
  1032. CPPUNIT_ASSERT(util::detectDirTraversal("foo/../bar"));
  1033. CPPUNIT_ASSERT(util::detectDirTraversal("foo/./bar"));
  1034. CPPUNIT_ASSERT(util::detectDirTraversal("foo/."));
  1035. CPPUNIT_ASSERT(util::detectDirTraversal("foo/.."));
  1036. CPPUNIT_ASSERT(util::detectDirTraversal("."));
  1037. CPPUNIT_ASSERT(util::detectDirTraversal(".."));
  1038. CPPUNIT_ASSERT(util::detectDirTraversal("/"));
  1039. CPPUNIT_ASSERT(util::detectDirTraversal("foo/"));
  1040. CPPUNIT_ASSERT(util::detectDirTraversal("\t"));
  1041. CPPUNIT_ASSERT(!util::detectDirTraversal("foo/bar"));
  1042. CPPUNIT_ASSERT(!util::detectDirTraversal("foo"));
  1043. }
  1044. void UtilTest::testEscapePath()
  1045. {
  1046. CPPUNIT_ASSERT_EQUAL(std::string("foo%00bar%00%01"),
  1047. util::escapePath(std::string("foo")+(char)0x00+
  1048. std::string("bar")+(char)0x00+
  1049. (char)0x01));
  1050. #ifdef __MINGW32__
  1051. CPPUNIT_ASSERT_EQUAL(std::string("foo%5Cbar"), util::escapePath("foo\\bar"));
  1052. #else // !__MINGW32__
  1053. CPPUNIT_ASSERT_EQUAL(std::string("foo\\bar"), util::escapePath("foo\\bar"));
  1054. #endif // !__MINGW32__
  1055. }
  1056. void UtilTest::testGetCidrPrefix()
  1057. {
  1058. struct in_addr in;
  1059. CPPUNIT_ASSERT(util::getCidrPrefix(in, "192.168.0.1", 16));
  1060. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.0"), std::string(inet_ntoa(in)));
  1061. CPPUNIT_ASSERT(util::getCidrPrefix(in, "192.168.255.255", 17));
  1062. CPPUNIT_ASSERT_EQUAL(std::string("192.168.128.0"),std::string(inet_ntoa(in)));
  1063. CPPUNIT_ASSERT(util::getCidrPrefix(in, "192.168.128.1", 16));
  1064. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.0"), std::string(inet_ntoa(in)));
  1065. CPPUNIT_ASSERT(util::getCidrPrefix(in, "192.168.0.1", 32));
  1066. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), std::string(inet_ntoa(in)));
  1067. CPPUNIT_ASSERT(util::getCidrPrefix(in, "192.168.0.1", 0));
  1068. CPPUNIT_ASSERT_EQUAL(std::string("0.0.0.0"), std::string(inet_ntoa(in)));
  1069. CPPUNIT_ASSERT(util::getCidrPrefix(in, "10.10.1.44", 27));
  1070. CPPUNIT_ASSERT_EQUAL(std::string("10.10.1.32"), std::string(inet_ntoa(in)));
  1071. CPPUNIT_ASSERT(!util::getCidrPrefix(in, "::1", 32));
  1072. }
  1073. void UtilTest::testInSameCidrBlock()
  1074. {
  1075. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 16));
  1076. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 17));
  1077. }
  1078. void UtilTest::testIsUtf8String()
  1079. {
  1080. CPPUNIT_ASSERT(util::isUtf8("ascii"));
  1081. // "Hello World" in Japanese UTF-8
  1082. CPPUNIT_ASSERT(util::isUtf8
  1083. (util::fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
  1084. // "World" in Shift_JIS
  1085. CPPUNIT_ASSERT(!util::isUtf8(util::fromHex("90a28a")+"E"));
  1086. // UTF8-2
  1087. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("c280")));
  1088. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("dfbf")));
  1089. // UTF8-3
  1090. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e0a080")));
  1091. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e0bf80")));
  1092. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e18080")));
  1093. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ec8080")));
  1094. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ed8080")));
  1095. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ed9f80")));
  1096. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ee8080")));
  1097. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ef8080")));
  1098. // UTF8-4
  1099. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f0908080")));
  1100. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f0bf8080")));
  1101. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f1808080")));
  1102. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f3808080")));
  1103. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f4808080")));
  1104. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f48f8080")));
  1105. CPPUNIT_ASSERT(util::isUtf8(""));
  1106. CPPUNIT_ASSERT(!util::isUtf8(util::fromHex("00")));
  1107. }
  1108. void UtilTest::testNextParam()
  1109. {
  1110. std::string s1 = " :a : b=c :d=b::::g::";
  1111. std::pair<std::string::iterator, bool> r;
  1112. std::string name, value;
  1113. r = util::nextParam(name, value, s1.begin(), s1.end(), ':');
  1114. CPPUNIT_ASSERT(r.second);
  1115. CPPUNIT_ASSERT_EQUAL(std::string("a"), name);
  1116. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1117. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1118. CPPUNIT_ASSERT(r.second);
  1119. CPPUNIT_ASSERT_EQUAL(std::string("b"), name);
  1120. CPPUNIT_ASSERT_EQUAL(std::string("c"), value);
  1121. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1122. CPPUNIT_ASSERT(r.second);
  1123. CPPUNIT_ASSERT_EQUAL(std::string("d"), name);
  1124. CPPUNIT_ASSERT_EQUAL(std::string("b"), value);
  1125. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1126. CPPUNIT_ASSERT(r.second);
  1127. CPPUNIT_ASSERT_EQUAL(std::string("g"), name);
  1128. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1129. std::string s2 = "";
  1130. r = util::nextParam(name, value, s2.begin(), s2.end(), ':');
  1131. CPPUNIT_ASSERT(!r.second);
  1132. std::string s3 = " ";
  1133. r = util::nextParam(name, value, s3.begin(), s3.end(), ':');
  1134. CPPUNIT_ASSERT(!r.second);
  1135. std::string s4 = ":::";
  1136. r = util::nextParam(name, value, s4.begin(), s4.end(), ':');
  1137. CPPUNIT_ASSERT(!r.second);
  1138. }
  1139. } // namespace aria2