UtilTest.cc 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  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. #include "SocketCore.h"
  16. namespace aria2 {
  17. class UtilTest:public CppUnit::TestFixture {
  18. CPPUNIT_TEST_SUITE(UtilTest);
  19. CPPUNIT_TEST(testStrip);
  20. CPPUNIT_TEST(testStripIter);
  21. CPPUNIT_TEST(testLstripIter);
  22. CPPUNIT_TEST(testLstripIter_char);
  23. CPPUNIT_TEST(testDivide);
  24. CPPUNIT_TEST(testSplit);
  25. CPPUNIT_TEST(testSplitIter);
  26. CPPUNIT_TEST(testSplitIterM);
  27. CPPUNIT_TEST(testStreq);
  28. CPPUNIT_TEST(testStrieq);
  29. CPPUNIT_TEST(testEndsWith);
  30. CPPUNIT_TEST(testIendsWith);
  31. CPPUNIT_TEST(testReplace);
  32. CPPUNIT_TEST(testStartsWith);
  33. CPPUNIT_TEST(testIstartsWith);
  34. // may be moved to other helper class in the future.
  35. CPPUNIT_TEST(testGetContentDispositionFilename);
  36. CPPUNIT_TEST(testRandomAlpha);
  37. CPPUNIT_TEST(testToUpper);
  38. CPPUNIT_TEST(testToLower);
  39. CPPUNIT_TEST(testUppercase);
  40. CPPUNIT_TEST(testLowercase);
  41. CPPUNIT_TEST(testPercentDecode);
  42. CPPUNIT_TEST(testGetRealSize);
  43. CPPUNIT_TEST(testAbbrevSize);
  44. CPPUNIT_TEST(testToStream);
  45. CPPUNIT_TEST(testIsNumber);
  46. CPPUNIT_TEST(testIsLowercase);
  47. CPPUNIT_TEST(testIsUppercase);
  48. CPPUNIT_TEST(testAlphaToNum);
  49. CPPUNIT_TEST(testMkdirs);
  50. CPPUNIT_TEST(testConvertBitfield);
  51. CPPUNIT_TEST(testParseIntSegments);
  52. CPPUNIT_TEST(testParseIntSegments_invalidRange);
  53. CPPUNIT_TEST(testParseInt);
  54. CPPUNIT_TEST(testParseUInt);
  55. CPPUNIT_TEST(testParseLLInt);
  56. CPPUNIT_TEST(testParseULLInt);
  57. CPPUNIT_TEST(testParseIntNoThrow);
  58. CPPUNIT_TEST(testParseUIntNoThrow);
  59. CPPUNIT_TEST(testParseLLIntNoThrow);
  60. CPPUNIT_TEST(testToString_binaryStream);
  61. CPPUNIT_TEST(testItos);
  62. CPPUNIT_TEST(testUitos);
  63. CPPUNIT_TEST(testNtoh64);
  64. CPPUNIT_TEST(testPercentEncode);
  65. CPPUNIT_TEST(testHtmlEscape);
  66. CPPUNIT_TEST(testJoinPath);
  67. CPPUNIT_TEST(testParseIndexPath);
  68. CPPUNIT_TEST(testCreateIndexPaths);
  69. CPPUNIT_TEST(testGenerateRandomData);
  70. CPPUNIT_TEST(testFromHex);
  71. CPPUNIT_TEST(testParsePrioritizePieceRange);
  72. CPPUNIT_TEST(testApplyDir);
  73. CPPUNIT_TEST(testFixTaintedBasename);
  74. CPPUNIT_TEST(testIsNumericHost);
  75. CPPUNIT_TEST(testDetectDirTraversal);
  76. CPPUNIT_TEST(testEscapePath);
  77. CPPUNIT_TEST(testInSameCidrBlock);
  78. CPPUNIT_TEST(testIsUtf8String);
  79. CPPUNIT_TEST(testNextParam);
  80. CPPUNIT_TEST(testNoProxyDomainMatch);
  81. CPPUNIT_TEST_SUITE_END();
  82. private:
  83. public:
  84. void setUp() {
  85. }
  86. void testStrip();
  87. void testStripIter();
  88. void testLstripIter();
  89. void testLstripIter_char();
  90. void testDivide();
  91. void testSplit();
  92. void testSplitIter();
  93. void testSplitIterM();
  94. void testStreq();
  95. void testStrieq();
  96. void testEndsWith();
  97. void testIendsWith();
  98. void testReplace();
  99. void testStartsWith();
  100. void testIstartsWith();
  101. // may be moved to other helper class in the future.
  102. void testGetContentDispositionFilename();
  103. void testRandomAlpha();
  104. void testToUpper();
  105. void testToLower();
  106. void testUppercase();
  107. void testLowercase();
  108. void testPercentDecode();
  109. void testGetRealSize();
  110. void testAbbrevSize();
  111. void testToStream();
  112. void testIsNumber();
  113. void testIsLowercase();
  114. void testIsUppercase();
  115. void testAlphaToNum();
  116. void testMkdirs();
  117. void testConvertBitfield();
  118. void testParseIntSegments();
  119. void testParseIntSegments_invalidRange();
  120. void testParseInt();
  121. void testParseUInt();
  122. void testParseLLInt();
  123. void testParseULLInt();
  124. void testParseIntNoThrow();
  125. void testParseUIntNoThrow();
  126. void testParseLLIntNoThrow();
  127. void testToString_binaryStream();
  128. void testItos();
  129. void testUitos();
  130. void testNtoh64();
  131. void testPercentEncode();
  132. void testHtmlEscape();
  133. void testJoinPath();
  134. void testParseIndexPath();
  135. void testCreateIndexPaths();
  136. void testGenerateRandomData();
  137. void testFromHex();
  138. void testParsePrioritizePieceRange();
  139. void testApplyDir();
  140. void testFixTaintedBasename();
  141. void testIsNumericHost();
  142. void testDetectDirTraversal();
  143. void testEscapePath();
  144. void testInSameCidrBlock();
  145. void testIsUtf8String();
  146. void testNextParam();
  147. void testNoProxyDomainMatch();
  148. };
  149. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  150. void UtilTest::testStrip()
  151. {
  152. std::string str1 = "aria2";
  153. CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2"));
  154. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2"));
  155. CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2 "));
  156. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
  157. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
  158. std::string str2 = "aria2 debut";
  159. CPPUNIT_ASSERT_EQUAL(str2, util::strip("aria2 debut"));
  160. CPPUNIT_ASSERT_EQUAL(str2, util::strip(" aria2 debut "));
  161. std::string str3 = "";
  162. CPPUNIT_ASSERT_EQUAL(str3, util::strip(""));
  163. CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
  164. CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
  165. std::string str4 = "A";
  166. CPPUNIT_ASSERT_EQUAL(str4, util::strip("A"));
  167. CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
  168. CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
  169. }
  170. void UtilTest::testStripIter()
  171. {
  172. Scip p;
  173. std::string str1 = "aria2";
  174. std::string s = "aria2";
  175. p = util::stripIter(s.begin(), s.end());
  176. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  177. s = " aria2";
  178. p = util::stripIter(s.begin(), s.end());
  179. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  180. s = "aria2 ";
  181. p = util::stripIter(s.begin(), s.end());
  182. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  183. s = " aria2 ";
  184. p = util::stripIter(s.begin(), s.end());
  185. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  186. s = " aria2 ";
  187. p = util::stripIter(s.begin(), s.end());
  188. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  189. std::string str2 = "aria2 debut";
  190. s = "aria2 debut";
  191. p = util::stripIter(s.begin(), s.end());
  192. CPPUNIT_ASSERT_EQUAL(str2, std::string(p.first, p.second));
  193. s = " aria2 debut ";
  194. p = util::stripIter(s.begin(), s.end());
  195. CPPUNIT_ASSERT_EQUAL(str2, std::string(p.first, p.second));
  196. std::string str3 = "";
  197. s = "";
  198. p = util::stripIter(s.begin(), s.end());
  199. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  200. s = " ";
  201. p = util::stripIter(s.begin(), s.end());
  202. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  203. s = " ";
  204. p = util::stripIter(s.begin(), s.end());
  205. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  206. std::string str4 = "A";
  207. s = "A";
  208. p = util::stripIter(s.begin(), s.end());
  209. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  210. s = " A ";
  211. p = util::stripIter(s.begin(), s.end());
  212. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  213. s = " A ";
  214. p = util::stripIter(s.begin(), s.end());
  215. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  216. }
  217. void UtilTest::testLstripIter()
  218. {
  219. std::string::iterator r;
  220. std::string s = "foo";
  221. r = util::lstripIter(s.begin(), s.end());
  222. CPPUNIT_ASSERT_EQUAL(std::string("foo"), std::string(r, s.end()));
  223. s = " foo bar ";
  224. r = util::lstripIter(s.begin(), s.end());
  225. CPPUNIT_ASSERT_EQUAL(std::string("foo bar "), std::string(r, s.end()));
  226. s = "f";
  227. r = util::lstripIter(s.begin(), s.end());
  228. CPPUNIT_ASSERT_EQUAL(std::string("f"), std::string(r, s.end()));
  229. s = "foo ";
  230. r = util::lstripIter(s.begin(), s.end());
  231. CPPUNIT_ASSERT_EQUAL(std::string("foo "), std::string(r, s.end()));
  232. }
  233. void UtilTest::testLstripIter_char()
  234. {
  235. std::string::iterator r;
  236. std::string s = "foo";
  237. r = util::lstripIter(s.begin(), s.end(), '$');
  238. CPPUNIT_ASSERT_EQUAL(std::string("foo"), std::string(r, s.end()));
  239. s = "$$foo$bar$$";
  240. r = util::lstripIter(s.begin(), s.end(), '$');
  241. CPPUNIT_ASSERT_EQUAL(std::string("foo$bar$$"), std::string(r, s.end()));
  242. s = "f";
  243. r = util::lstripIter(s.begin(), s.end(), '$');
  244. CPPUNIT_ASSERT_EQUAL(std::string("f"), std::string(r, s.end()));
  245. s = "foo$$";
  246. r = util::lstripIter(s.begin(), s.end(), '$');
  247. CPPUNIT_ASSERT_EQUAL(std::string("foo$$"), std::string(r, s.end()));
  248. }
  249. void UtilTest::testDivide() {
  250. std::pair<Scip, Scip> p1;
  251. std::string s = "name=value";
  252. util::divide(p1, s.begin(), s.end(), '=');
  253. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  254. std::string(p1.first.first, p1.first.second));
  255. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  256. std::string(p1.second.first, p1.second.second));
  257. s = " name = value ";
  258. util::divide(p1, s.begin(), s.end(), '=');
  259. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  260. std::string(p1.first.first, p1.first.second));
  261. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  262. std::string(p1.second.first, p1.second.second));
  263. s = "=value";
  264. util::divide(p1, s.begin(), s.end(), '=');
  265. CPPUNIT_ASSERT_EQUAL(std::string(""),
  266. std::string(p1.first.first, p1.first.second));
  267. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  268. std::string(p1.second.first, p1.second.second));
  269. s = "name=";
  270. util::divide(p1, s.begin(), s.end(), '=');
  271. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  272. std::string(p1.first.first, p1.first.second));
  273. CPPUNIT_ASSERT_EQUAL(std::string(""),
  274. std::string(p1.second.first, p1.second.second));
  275. s = "name";
  276. util::divide(p1, s.begin(), s.end(), '=');
  277. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  278. std::string(p1.first.first, p1.first.second));
  279. CPPUNIT_ASSERT_EQUAL(std::string(""),
  280. std::string(p1.second.first, p1.second.second));
  281. }
  282. void UtilTest::testSplit() {
  283. std::vector<std::string> v;
  284. std::string s = "k1; k2;; k3";
  285. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true);
  286. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  287. std::vector<std::string>::iterator itr = v.begin();
  288. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  289. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  290. CPPUNIT_ASSERT_EQUAL(std::string("k3"), *itr++);
  291. v.clear();
  292. s = "k1; k2; k3";
  293. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  294. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  295. itr = v.begin();
  296. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  297. CPPUNIT_ASSERT_EQUAL(std::string(" k2"), *itr++);
  298. CPPUNIT_ASSERT_EQUAL(std::string(" k3"), *itr++);
  299. v.clear();
  300. s = "k=v";
  301. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  302. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  303. itr = v.begin();
  304. CPPUNIT_ASSERT_EQUAL(std::string("k=v"), *itr++);
  305. v.clear();
  306. s = ";;k1;;k2;";
  307. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  308. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  309. itr = v.begin();
  310. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  311. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  312. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  313. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  314. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  315. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  316. v.clear();
  317. s = ";;k1;;k2;";
  318. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  319. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  320. itr = v.begin();
  321. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  322. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  323. v.clear();
  324. s = "k; ";
  325. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  326. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  327. itr = v.begin();
  328. CPPUNIT_ASSERT_EQUAL(std::string("k"), *itr++);
  329. CPPUNIT_ASSERT_EQUAL(std::string(" "), *itr++);
  330. v.clear();
  331. s = " ";
  332. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true, true);
  333. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  334. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  335. v.clear();
  336. s = " ";
  337. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true);
  338. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  339. v.clear();
  340. s = " ";
  341. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  342. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  343. CPPUNIT_ASSERT_EQUAL(std::string(" "), v[0]);
  344. v.clear();
  345. s = ";";
  346. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  347. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  348. v.clear();
  349. s = ";";
  350. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  351. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  352. itr = v.begin();
  353. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  354. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  355. v.clear();
  356. s = "";
  357. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  358. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  359. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  360. }
  361. void UtilTest::testSplitIter() {
  362. std::vector<Scip> v;
  363. std::string s = "k1; k2;; k3";
  364. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true);
  365. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  366. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  367. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  368. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  369. v.clear();
  370. s = "k1; k2; k3";
  371. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  372. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  373. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  374. CPPUNIT_ASSERT_EQUAL(std::string(" k2"),
  375. std::string(v[1].first, v[1].second));
  376. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  377. std::string(v[2].first, v[2].second));
  378. v.clear();
  379. s = "k=v";
  380. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  381. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  382. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  383. std::string(v[0].first, v[0].second));
  384. v.clear();
  385. s = ";;k1;;k2;";
  386. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  387. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  388. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  389. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  390. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  391. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  392. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  393. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  394. v.clear();
  395. s = ";;k1;;k2;";
  396. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  397. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  398. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  399. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  400. v.clear();
  401. s = "k; ";
  402. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  403. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  404. CPPUNIT_ASSERT_EQUAL(std::string("k"), std::string(v[0].first, v[0].second));
  405. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  406. v.clear();
  407. s = " ";
  408. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true, true);
  409. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  410. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  411. v.clear();
  412. s = " ";
  413. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true);
  414. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  415. v.clear();
  416. s = " ";
  417. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  418. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  419. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[0].first, v[0].second));
  420. v.clear();
  421. s = ";";
  422. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  423. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  424. v.clear();
  425. s = ";";
  426. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  427. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  428. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  429. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  430. v.clear();
  431. s = "";
  432. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  433. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  434. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  435. }
  436. void UtilTest::testSplitIterM() {
  437. std::string d = ";";
  438. std::string md = "; ";
  439. std::vector<Scip> v;
  440. std::string s = "k1; k2;; k3";
  441. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  442. d.begin(), d.end(), true);
  443. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  444. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  445. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  446. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  447. v.clear();
  448. s = "k1; k2; k3";
  449. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  450. d.begin(), d.end());
  451. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  452. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  453. CPPUNIT_ASSERT_EQUAL(std::string(" k2"),
  454. std::string(v[1].first, v[1].second));
  455. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  456. std::string(v[2].first, v[2].second));
  457. v.clear();
  458. s = "k1; k2; k3";
  459. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  460. md.begin(), md.end());
  461. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  462. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  463. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  464. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  465. v.clear();
  466. s = "k1; k2; k3;";
  467. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  468. md.begin(), md.end(), false, true);
  469. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  470. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  471. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  472. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[2].first, v[2].second));
  473. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  474. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[4].first, v[4].second));
  475. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  476. v.clear();
  477. s = "k=v";
  478. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  479. d.begin(), d.end(), false, true);
  480. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  481. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  482. std::string(v[0].first, v[0].second));
  483. v.clear();
  484. s = ";;k1;;k2;";
  485. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  486. d.begin(), d.end(), false, true);
  487. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  488. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  489. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  490. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  491. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  492. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  493. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  494. v.clear();
  495. s = ";;k1;;k2;";
  496. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  497. d.begin(), d.end());
  498. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  499. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  500. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  501. v.clear();
  502. s = "k; ";
  503. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  504. d.begin(), d.end());
  505. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  506. CPPUNIT_ASSERT_EQUAL(std::string("k"), std::string(v[0].first, v[0].second));
  507. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  508. v.clear();
  509. s = " ";
  510. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  511. d.begin(), d.end(), true, true);
  512. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  513. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  514. v.clear();
  515. s = " ";
  516. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  517. d.begin(), d.end(), true);
  518. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  519. v.clear();
  520. s = " ";
  521. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  522. d.begin(), d.end());
  523. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  524. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[0].first, v[0].second));
  525. v.clear();
  526. s = ";";
  527. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  528. d.begin(), d.end());
  529. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  530. v.clear();
  531. s = ";";
  532. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  533. d.begin(), d.end(), false, true);
  534. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  535. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  536. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  537. v.clear();
  538. s = "";
  539. util::splitIterM(s.begin(), s.end(), std::back_inserter(v),
  540. d.begin(), d.end(), false, true);
  541. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  542. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  543. }
  544. void UtilTest::testEndsWith() {
  545. std::string target = "abcdefg";
  546. std::string part = "fg";
  547. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  548. part.begin(), part.end()));
  549. target = "abdefg";
  550. part = "g";
  551. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  552. part.begin(), part.end()));
  553. target = "abdefg";
  554. part = "eg";
  555. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  556. part.begin(), part.end()));
  557. target = "g";
  558. part = "eg";
  559. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  560. part.begin(), part.end()));
  561. target = "g";
  562. part = "g";
  563. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  564. part.begin(), part.end()));
  565. target = "g";
  566. part = "";
  567. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  568. part.begin(), part.end()));
  569. target = "";
  570. part = "";
  571. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  572. part.begin(), part.end()));
  573. target = "";
  574. part = "g";
  575. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  576. part.begin(), part.end()));
  577. }
  578. void UtilTest::testIendsWith() {
  579. std::string target = "abcdefg";
  580. std::string part = "Fg";
  581. CPPUNIT_ASSERT(util::iendsWith(target.begin(), target.end(),
  582. part.begin(), part.end()));
  583. target = "abdefg";
  584. part = "ef";
  585. CPPUNIT_ASSERT(!util::iendsWith(target.begin(), target.end(),
  586. part.begin(), part.end()));
  587. }
  588. void UtilTest::testStreq()
  589. {
  590. std::string s1, s2;
  591. s1 = "foo";
  592. s2 = "foo";
  593. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  594. s2 = "fooo";
  595. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  596. s2 = "fo";
  597. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  598. s2 = "";
  599. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  600. s1 = "";
  601. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  602. }
  603. void UtilTest::testStrieq()
  604. {
  605. std::string s1, s2;
  606. s1 = "foo";
  607. s2 = "foo";
  608. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  609. s1 = "FoO";
  610. s2 = "fOo";
  611. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  612. s2 = "fooo";
  613. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  614. s2 = "fo";
  615. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  616. s2 = "";
  617. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  618. s1 = "";
  619. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  620. }
  621. void UtilTest::testReplace() {
  622. CPPUNIT_ASSERT_EQUAL(std::string("abc\n"), util::replace("abc\r\n", "\r", ""));
  623. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc\r\n", "\r\n", ""));
  624. CPPUNIT_ASSERT_EQUAL(std::string(""), util::replace("", "\r\n", ""));
  625. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc", "", "a"));
  626. CPPUNIT_ASSERT_EQUAL(std::string("xbc"), util::replace("abc", "a", "x"));
  627. }
  628. void UtilTest::testStartsWith() {
  629. std::string target;
  630. std::string part;
  631. target = "abcdefg";
  632. part = "abc";
  633. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  634. part.begin(), part.end()));
  635. target = "abcdefg";
  636. part = "abx";
  637. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  638. part.begin(), part.end()));
  639. target = "abcdefg";
  640. part = "bcd";
  641. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  642. part.begin(), part.end()));
  643. target = "";
  644. part = "a";
  645. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  646. part.begin(), part.end()));
  647. target = "";
  648. part = "";
  649. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  650. part.begin(), part.end()));
  651. target = "a";
  652. part = "";
  653. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  654. part.begin(), part.end()));
  655. target = "a";
  656. part = "a";
  657. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  658. part.begin(), part.end()));
  659. }
  660. void UtilTest::testIstartsWith() {
  661. std::string target;
  662. std::string part;
  663. target = "abcdefg";
  664. part = "aBc";
  665. CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(),
  666. part.begin(), part.end()));
  667. target = "abcdefg";
  668. part = "abx";
  669. CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(),
  670. part.begin(), part.end()));
  671. }
  672. void UtilTest::testGetContentDispositionFilename() {
  673. std::string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  674. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h1));
  675. std::string h2 = "attachment; filename=\"\"";
  676. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h2));
  677. std::string h3 = "attachment; filename=\"";
  678. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h3));
  679. std::string h3_2 = "attachment; filename= \" aria2.tar.bz2 \"";
  680. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  681. util::getContentDispositionFilename(h3_2));
  682. std::string h4 = "attachment;";
  683. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h4));
  684. std::string h5 = "attachment; filename=aria2.tar.bz2";
  685. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h5));
  686. std::string h6 = "attachment; filename='aria2.tar.bz2'";
  687. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h6));
  688. std::string h7 = "attachment; filename='aria2.tar.bz2";
  689. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h7));
  690. std::string h8 = "attachment; filename=aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT";
  691. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h8));
  692. std::string h9 = "attachment; filename=\"aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT\"";
  693. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT"),
  694. util::getContentDispositionFilename(h9));
  695. std::string h10 = "attachment; filename=";
  696. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h10));
  697. std::string h11 = "attachment; filename=;";
  698. CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h11));
  699. std::string filenameWithDir = "attachment; filename=dir/file";
  700. CPPUNIT_ASSERT_EQUAL(std::string(""),
  701. util::getContentDispositionFilename(filenameWithDir));
  702. std::string semicolonInside = "attachment; filename=\"foo;bar\"";
  703. CPPUNIT_ASSERT_EQUAL(std::string("foo;bar"),
  704. util::getContentDispositionFilename(semicolonInside));
  705. CPPUNIT_ASSERT_EQUAL
  706. (std::string(""),
  707. util::getContentDispositionFilename("filename=\"%2E%2E%2Ffoo.html\""));
  708. // RFC2231 Section4
  709. std::string extparam2 = "filename*=''aria2";
  710. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  711. util::getContentDispositionFilename(extparam2));
  712. std::string extparam3 = "filename*='''";
  713. CPPUNIT_ASSERT_EQUAL(std::string(""),
  714. util::getContentDispositionFilename(extparam3));
  715. std::string extparam4 = "filename*='aria2";
  716. CPPUNIT_ASSERT_EQUAL(std::string(""),
  717. util::getContentDispositionFilename(extparam4));
  718. std::string extparam5 = "filename*='''aria2";
  719. CPPUNIT_ASSERT_EQUAL(std::string(""),
  720. util::getContentDispositionFilename(extparam5));
  721. std::string extparam6 = "filename*";
  722. CPPUNIT_ASSERT_EQUAL(std::string(""),
  723. util::getContentDispositionFilename(extparam6));
  724. std::string extparam7 = "filename*=UTF-8''aria2;filename=hello%20world";
  725. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  726. util::getContentDispositionFilename(extparam7));
  727. std::string extparam8 = "filename=aria2;filename*=UTF-8''hello%20world";
  728. CPPUNIT_ASSERT_EQUAL(std::string("hello world"),
  729. util::getContentDispositionFilename(extparam8));
  730. std::string extparam9 = "filename*=ISO-8859-1''%A3";
  731. std::string extparam9ans;
  732. extparam9ans += 0xc2;
  733. extparam9ans += 0xa3;
  734. CPPUNIT_ASSERT_EQUAL(extparam9ans,
  735. util::getContentDispositionFilename(extparam9));
  736. CPPUNIT_ASSERT_EQUAL
  737. (std::string(""),
  738. util::getContentDispositionFilename("filename*=UTF-8''foo%2F.html"));
  739. CPPUNIT_ASSERT_EQUAL
  740. (std::string("foo.html"),
  741. util::getContentDispositionFilename("filename*=UTF-8'';filename=\"foo.html\""));
  742. CPPUNIT_ASSERT_EQUAL
  743. (std::string(""),
  744. util::getContentDispositionFilename("filename*=UTF-8''%2E%2E%2Ffoo.html"));
  745. // Tests from http://greenbytes.de/tech/tc2231/
  746. // attwithasciifnescapedchar
  747. CPPUNIT_ASSERT_EQUAL
  748. (std::string("foo.html"),
  749. util::getContentDispositionFilename("filename=\"f\\oo.html\""));
  750. // attwithasciifilenameucase
  751. CPPUNIT_ASSERT_EQUAL
  752. (std::string("foo.html"),
  753. util::getContentDispositionFilename("FILENAME=\"foo.html\""));
  754. // attwithisofn2231iso
  755. CPPUNIT_ASSERT_EQUAL
  756. (std::string("foo-ä.html"),
  757. util::getContentDispositionFilename("filename*=iso-8859-1''foo-%E4.html"));
  758. // attwithfn2231utf8
  759. CPPUNIT_ASSERT_EQUAL
  760. (std::string("foo-ä-€.html"),
  761. util::getContentDispositionFilename
  762. ("filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html"));
  763. // attwithfn2231utf8-bad
  764. CPPUNIT_ASSERT_EQUAL
  765. (std::string(""),
  766. util::getContentDispositionFilename
  767. ("filename*=iso-8859-1''foo-%c3%a4-%e2%82%ac.html"));
  768. // attwithfn2231ws1
  769. CPPUNIT_ASSERT_EQUAL
  770. (std::string(""),
  771. util::getContentDispositionFilename("filename *=UTF-8''foo-%c3%a4.html"));
  772. // attwithfn2231ws2
  773. CPPUNIT_ASSERT_EQUAL
  774. (std::string("foo-ä.html"),
  775. util::getContentDispositionFilename("filename*= UTF-8''foo-%c3%a4.html"));
  776. // attwithfn2231ws3
  777. CPPUNIT_ASSERT_EQUAL
  778. (std::string("foo-ä.html"),
  779. util::getContentDispositionFilename("filename* =UTF-8''foo-%c3%a4.html"));
  780. // attwithfn2231quot
  781. CPPUNIT_ASSERT_EQUAL
  782. (std::string(""),
  783. util::getContentDispositionFilename
  784. ("filename*=\"UTF-8''foo-%c3%a4.html\""));
  785. }
  786. class Printer {
  787. public:
  788. template<class T>
  789. void operator()(T t) {
  790. std::cerr << t << ", ";
  791. }
  792. };
  793. void UtilTest::testRandomAlpha() {
  794. SharedHandle<Randomizer> rand(new FixedNumberRandomizer());
  795. std::string s = util::randomAlpha(8, rand);
  796. CPPUNIT_ASSERT_EQUAL(std::string("AAAAAAAA"), s);
  797. }
  798. void UtilTest::testToUpper() {
  799. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  800. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  801. CPPUNIT_ASSERT_EQUAL(upp, util::toUpper(src));
  802. }
  803. void UtilTest::testToLower() {
  804. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  805. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  806. CPPUNIT_ASSERT_EQUAL(upp, util::toLower(src));
  807. }
  808. void UtilTest::testUppercase() {
  809. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  810. std::string ans = "608CABC0F2FA18C260CAFD974516865C772363D5";
  811. util::uppercase(src);
  812. CPPUNIT_ASSERT_EQUAL(ans, src);
  813. }
  814. void UtilTest::testLowercase() {
  815. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  816. std::string ans = "608cabc0f2fa18c260cafd974516865c772363d5";
  817. util::lowercase(src);
  818. CPPUNIT_ASSERT_EQUAL(ans, src);
  819. }
  820. void UtilTest::testPercentDecode() {
  821. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  822. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  823. util::percentDecode(src.begin(), src.end()));
  824. std::string src2 = "aria2+aria2";
  825. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"),
  826. util::percentDecode(src2.begin(), src2.end()));
  827. std::string src3 = "%5t%20";
  828. CPPUNIT_ASSERT_EQUAL(std::string("%5t "),
  829. util::percentDecode(src3.begin(), src3.end()));
  830. std::string src4 = "%";
  831. CPPUNIT_ASSERT_EQUAL(std::string("%"),
  832. util::percentDecode(src4.begin(), src4.end()));
  833. std::string src5 = "%3";
  834. CPPUNIT_ASSERT_EQUAL(std::string("%3"),
  835. util::percentDecode(src5.begin(), src5.end()));
  836. std::string src6 = "%2f";
  837. CPPUNIT_ASSERT_EQUAL(std::string("/"),
  838. util::percentDecode(src6.begin(), src6.end()));
  839. }
  840. void UtilTest::testGetRealSize()
  841. {
  842. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096M"));
  843. CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1K"));
  844. try {
  845. util::getRealSize("");
  846. CPPUNIT_FAIL("exception must be thrown.");
  847. } catch(Exception& e) {
  848. std::cerr << e.stackTrace();
  849. }
  850. try {
  851. util::getRealSize("foo");
  852. CPPUNIT_FAIL("exception must be thrown.");
  853. } catch(Exception& e) {
  854. std::cerr << e.stackTrace();
  855. }
  856. try {
  857. util::getRealSize("-1");
  858. CPPUNIT_FAIL("exception must be thrown.");
  859. } catch(Exception& e) {
  860. std::cerr << e.stackTrace();
  861. }
  862. try {
  863. util::getRealSize("9223372036854775807K");
  864. CPPUNIT_FAIL("exception must be thrown.");
  865. } catch(Exception& e) {
  866. std::cerr << e.stackTrace();
  867. }
  868. try {
  869. util::getRealSize("9223372036854775807M");
  870. CPPUNIT_FAIL("exception must be thrown.");
  871. } catch(Exception& e) {
  872. std::cerr << e.stackTrace();
  873. }
  874. }
  875. void UtilTest::testAbbrevSize()
  876. {
  877. CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), util::abbrevSize(4294967296LL));
  878. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1024));
  879. CPPUNIT_ASSERT_EQUAL(std::string("1,023"), util::abbrevSize(1023));
  880. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::abbrevSize(0));
  881. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), util::abbrevSize(1127));
  882. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), util::abbrevSize(1572864));
  883. }
  884. void UtilTest::testToStream()
  885. {
  886. std::ostringstream os;
  887. SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  888. SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  889. std::deque<SharedHandle<FileEntry> > entries;
  890. entries.push_back(f1);
  891. entries.push_back(f2);
  892. std::string filename = A2_TEST_OUT_DIR"/aria2_UtilTest_testToStream";
  893. BufferedFile fp(filename, BufferedFile::WRITE);
  894. util::toStream(entries.begin(), entries.end(), fp);
  895. fp.close();
  896. CPPUNIT_ASSERT_EQUAL(
  897. std::string("Files:\n"
  898. "idx|path/length\n"
  899. "===+===========================================================================\n"
  900. " 1|aria2.tar.bz2\n"
  901. " |12.0KiB (12,300)\n"
  902. "---+---------------------------------------------------------------------------\n"
  903. " 2|aria2.txt\n"
  904. " |556B (556)\n"
  905. "---+---------------------------------------------------------------------------\n"),
  906. readFile(filename));
  907. }
  908. void UtilTest::testIsNumber()
  909. {
  910. CPPUNIT_ASSERT_EQUAL(true, util::isNumber("000"));
  911. CPPUNIT_ASSERT_EQUAL(false, util::isNumber("a"));
  912. CPPUNIT_ASSERT_EQUAL(false, util::isNumber("0a"));
  913. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(""));
  914. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(" "));
  915. }
  916. void UtilTest::testIsLowercase()
  917. {
  918. CPPUNIT_ASSERT_EQUAL(true, util::isLowercase("alpha"));
  919. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase("Alpha"));
  920. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase("1alpha"));
  921. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(""));
  922. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(" "));
  923. }
  924. void UtilTest::testIsUppercase()
  925. {
  926. CPPUNIT_ASSERT_EQUAL(true, util::isUppercase("ALPHA"));
  927. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase("Alpha"));
  928. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase("1ALPHA"));
  929. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(""));
  930. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(" "));
  931. }
  932. void UtilTest::testAlphaToNum()
  933. {
  934. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("a"));
  935. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("aa"));
  936. CPPUNIT_ASSERT_EQUAL(1U, util::alphaToNum("b"));
  937. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("zz")); // 25*26+25
  938. CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("ZZ")); // 25*26+25
  939. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum(""));
  940. CPPUNIT_ASSERT_EQUAL(4294967295U, util::alphaToNum("NXMRLXV"));
  941. CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("NXMRLXW")); // uint32_t overflow
  942. }
  943. void UtilTest::testMkdirs()
  944. {
  945. std::string dir = A2_TEST_OUT_DIR"/aria2-UtilTest-testMkdirs";
  946. File d(dir);
  947. if(d.exists()) {
  948. CPPUNIT_ASSERT(d.remove());
  949. }
  950. CPPUNIT_ASSERT(!d.exists());
  951. util::mkdirs(dir);
  952. CPPUNIT_ASSERT(d.isDir());
  953. std::string file = A2_TEST_DIR"/UtilTest.cc";
  954. File f(file);
  955. CPPUNIT_ASSERT(f.isFile());
  956. try {
  957. util::mkdirs(file);
  958. CPPUNIT_FAIL("exception must be thrown.");
  959. } catch(DlAbortEx& ex) {
  960. std::cerr << ex.stackTrace() << std::endl;
  961. }
  962. }
  963. void UtilTest::testConvertBitfield()
  964. {
  965. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  966. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  967. srcBitfield.setAllBit();
  968. srcBitfield.unsetBit(2);// <- range [768, 1152)
  969. // which corresponds to the index [1,2] in destBitfield
  970. util::convertBitfield(&destBitfield, &srcBitfield);
  971. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  972. util::toHex(destBitfield.getBitfield(),
  973. destBitfield.getBitfieldLength()));
  974. }
  975. void UtilTest::testParseIntSegments()
  976. {
  977. SegList<int> sgl;
  978. util::parseIntSegments(sgl, "1,3-8,10");
  979. CPPUNIT_ASSERT(sgl.hasNext());
  980. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  981. CPPUNIT_ASSERT(sgl.hasNext());
  982. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  983. CPPUNIT_ASSERT(sgl.hasNext());
  984. CPPUNIT_ASSERT_EQUAL(4, sgl.next());
  985. CPPUNIT_ASSERT(sgl.hasNext());
  986. CPPUNIT_ASSERT_EQUAL(5, sgl.next());
  987. CPPUNIT_ASSERT(sgl.hasNext());
  988. CPPUNIT_ASSERT_EQUAL(6, sgl.next());
  989. CPPUNIT_ASSERT(sgl.hasNext());
  990. CPPUNIT_ASSERT_EQUAL(7, sgl.next());
  991. CPPUNIT_ASSERT(sgl.hasNext());
  992. CPPUNIT_ASSERT_EQUAL(8, sgl.next());
  993. CPPUNIT_ASSERT(sgl.hasNext());
  994. CPPUNIT_ASSERT_EQUAL(10, sgl.next());
  995. CPPUNIT_ASSERT(!sgl.hasNext());
  996. CPPUNIT_ASSERT_EQUAL(0, sgl.next());
  997. sgl.clear();
  998. util::parseIntSegments(sgl, ",,,1,,,3,,,");
  999. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  1000. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  1001. CPPUNIT_ASSERT(!sgl.hasNext());
  1002. }
  1003. void UtilTest::testParseIntSegments_invalidRange()
  1004. {
  1005. try {
  1006. SegList<int> sgl;
  1007. util::parseIntSegments(sgl, "-1");
  1008. CPPUNIT_FAIL("exception must be thrown.");
  1009. } catch(Exception& e) {
  1010. }
  1011. try {
  1012. SegList<int> sgl;
  1013. util::parseIntSegments(sgl, "1-");
  1014. CPPUNIT_FAIL("exception must be thrown.");
  1015. } catch(Exception& e) {
  1016. }
  1017. try {
  1018. SegList<int> sgl;
  1019. util::parseIntSegments(sgl, "2147483648");
  1020. CPPUNIT_FAIL("exception must be thrown.");
  1021. } catch(Exception& e) {
  1022. }
  1023. try {
  1024. SegList<int> sgl;
  1025. util::parseIntSegments(sgl, "2147483647-2147483648");
  1026. CPPUNIT_FAIL("exception must be thrown.");
  1027. } catch(Exception& e) {
  1028. }
  1029. try {
  1030. SegList<int> sgl;
  1031. util::parseIntSegments(sgl, "1-2x");
  1032. CPPUNIT_FAIL("exception must be thrown.");
  1033. } catch(Exception& e) {
  1034. }
  1035. try {
  1036. SegList<int> sgl;
  1037. util::parseIntSegments(sgl, "3x-4");
  1038. CPPUNIT_FAIL("exception must be thrown.");
  1039. } catch(Exception& e) {
  1040. }
  1041. }
  1042. void UtilTest::testParseInt()
  1043. {
  1044. std::string s;
  1045. s = " -1 ";
  1046. CPPUNIT_ASSERT_EQUAL(-1, util::parseInt(s.begin(), s.end()));
  1047. s = "2147483647";
  1048. CPPUNIT_ASSERT_EQUAL(2147483647, util::parseInt(s.begin(), s.end()));
  1049. try {
  1050. s = "2147483648";
  1051. util::parseInt(s.begin(), s.end());
  1052. CPPUNIT_FAIL("exception must be thrown.");
  1053. } catch(Exception& e) {
  1054. }
  1055. try {
  1056. s = "-2147483649";
  1057. util::parseInt(s.begin(), s.end());
  1058. CPPUNIT_FAIL("exception must be thrown.");
  1059. } catch(Exception& e) {
  1060. }
  1061. try {
  1062. s = "12x";
  1063. util::parseInt(s.begin(), s.end());
  1064. CPPUNIT_FAIL("exception must be thrown.");
  1065. } catch(Exception& e) {
  1066. }
  1067. try {
  1068. s = "";
  1069. util::parseInt(s.begin(), s.end());
  1070. CPPUNIT_FAIL("exception must be thrown.");
  1071. } catch(Exception& e) {
  1072. }
  1073. }
  1074. void UtilTest::testParseUInt()
  1075. {
  1076. std::string s;
  1077. s = " 4294967295 ";
  1078. CPPUNIT_ASSERT_EQUAL(4294967295U, util::parseUInt(s.begin(), s.end()));
  1079. try {
  1080. s = "-1";
  1081. util::parseUInt(s.begin(), s.end());
  1082. CPPUNIT_FAIL("exception must be thrown.");
  1083. } catch(Exception& e) {
  1084. }
  1085. try {
  1086. s = "4294967296";
  1087. util::parseUInt(s.begin(), s.end());
  1088. CPPUNIT_FAIL("exception must be thrown.");
  1089. } catch(Exception& e) {
  1090. }
  1091. }
  1092. void UtilTest::testParseLLInt()
  1093. {
  1094. std::string s;
  1095. {
  1096. s = " -1 ";
  1097. CPPUNIT_ASSERT_EQUAL((int64_t)-1LL, util::parseLLInt(s.begin(), s.end()));
  1098. }
  1099. {
  1100. s = "9223372036854775807";
  1101. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
  1102. util::parseLLInt(s.begin(), s.end()));
  1103. }
  1104. try {
  1105. s = "9223372036854775808";
  1106. util::parseLLInt(s.begin(), s.end());
  1107. CPPUNIT_FAIL("exception must be thrown.");
  1108. } catch(Exception& e) {
  1109. std::cerr << e.stackTrace();
  1110. }
  1111. try {
  1112. s = "-9223372036854775809";
  1113. util::parseLLInt(s.begin(), s.end());
  1114. CPPUNIT_FAIL("exception must be thrown.");
  1115. } catch(Exception& e) {
  1116. std::cerr << e.stackTrace();
  1117. }
  1118. try {
  1119. s = "12x";
  1120. util::parseLLInt(s.begin(), s.end());
  1121. CPPUNIT_FAIL("exception must be thrown.");
  1122. } catch(Exception& e) {
  1123. std::cerr << e.stackTrace();
  1124. }
  1125. try {
  1126. s = "";
  1127. util::parseLLInt(s.begin(), s.end());
  1128. CPPUNIT_FAIL("exception must be thrown.");
  1129. } catch(Exception& e) {
  1130. std::cerr << e.stackTrace();
  1131. }
  1132. }
  1133. void UtilTest::testParseULLInt()
  1134. {
  1135. std::string s;
  1136. s = "18446744073709551615";
  1137. CPPUNIT_ASSERT_EQUAL((uint64_t)18446744073709551615ULL,
  1138. util::parseULLInt(s.begin(), s.end()));
  1139. try {
  1140. s = "-1";
  1141. util::parseULLInt(s.begin(), s.end());
  1142. CPPUNIT_FAIL("exception must be thrown.");
  1143. } catch(Exception& e) {
  1144. }
  1145. try {
  1146. s = "18446744073709551616";
  1147. util::parseULLInt(s.begin(), s.end());
  1148. CPPUNIT_FAIL("exception must be thrown.");
  1149. } catch(Exception& e) {
  1150. }
  1151. }
  1152. void UtilTest::testParseIntNoThrow()
  1153. {
  1154. std::string s;
  1155. int32_t n;
  1156. s = " -1 ";
  1157. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s.begin(), s.end()));
  1158. CPPUNIT_ASSERT_EQUAL((int32_t)-1, n);
  1159. s = "2147483647";
  1160. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s.begin(), s.end()));
  1161. CPPUNIT_ASSERT_EQUAL((int32_t)2147483647, n);
  1162. s = "2147483648";
  1163. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s.begin(), s.end()));
  1164. s = "-2147483649";
  1165. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s.begin(), s.end()));
  1166. s = "12x";
  1167. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s.begin(), s.end()));
  1168. s = "";
  1169. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s.begin(), s.end()));
  1170. }
  1171. void UtilTest::testParseUIntNoThrow()
  1172. {
  1173. std::string s;
  1174. uint32_t n;
  1175. s = " 4294967295 ";
  1176. CPPUNIT_ASSERT(util::parseUIntNoThrow(n, s.begin(), s.end()));
  1177. CPPUNIT_ASSERT_EQUAL((uint32_t)UINT32_MAX, n);
  1178. s = "4294967296";
  1179. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s.begin(), s.end()));
  1180. s = "-1";
  1181. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s.begin(), s.end()));
  1182. }
  1183. void UtilTest::testParseLLIntNoThrow()
  1184. {
  1185. std::string s;
  1186. int64_t n;
  1187. s = " 9223372036854775807 ";
  1188. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s.begin(), s.end()));
  1189. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MAX, n);
  1190. s = "9223372036854775808";
  1191. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s.begin(), s.end()));
  1192. s = "-9223372036854775808";
  1193. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s.begin(), s.end()));
  1194. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MIN, n);
  1195. s = "-9223372036854775809";
  1196. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s.begin(), s.end()));
  1197. }
  1198. void UtilTest::testToString_binaryStream()
  1199. {
  1200. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  1201. std::string data(16*1024+256, 'a');
  1202. dw->initAndOpenFile();
  1203. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  1204. std::string readData = util::toString(dw);
  1205. CPPUNIT_ASSERT_EQUAL(data, readData);
  1206. }
  1207. void UtilTest::testItos()
  1208. {
  1209. {
  1210. int i = 0;
  1211. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::itos(i));
  1212. }
  1213. {
  1214. int i = 100;
  1215. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i, true));
  1216. }
  1217. {
  1218. int i = 100;
  1219. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i));
  1220. }
  1221. {
  1222. int i = 12345;
  1223. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), util::itos(i, true));
  1224. }
  1225. {
  1226. int i = 12345;
  1227. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::itos(i));
  1228. }
  1229. {
  1230. int i = -12345;
  1231. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), util::itos(i, true));
  1232. }
  1233. {
  1234. int64_t i = INT64_MAX;
  1235. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"),
  1236. util::itos(i, true));
  1237. }
  1238. {
  1239. int64_t i = INT64_MIN;
  1240. CPPUNIT_ASSERT_EQUAL(std::string("-9,223,372,036,854,775,808"),
  1241. util::itos(i, true));
  1242. }
  1243. }
  1244. void UtilTest::testUitos()
  1245. {
  1246. {
  1247. uint16_t i = 12345;
  1248. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::uitos(i));
  1249. }
  1250. {
  1251. int16_t i = -12345;
  1252. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), util::uitos(i));
  1253. }
  1254. }
  1255. void UtilTest::testNtoh64()
  1256. {
  1257. uint64_t x = 0xff00ff00ee00ee00LL;
  1258. #ifdef WORDS_BIGENDIAN
  1259. CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
  1260. CPPUNIT_ASSERT_EQUAL(x, hton64(x));
  1261. #else // !WORDS_BIGENDIAN
  1262. uint64_t y = 0x00ee00ee00ff00ffLL;
  1263. CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
  1264. CPPUNIT_ASSERT_EQUAL(x, hton64(y));
  1265. #endif // !WORDS_BIGENDIAN
  1266. }
  1267. void UtilTest::testPercentEncode()
  1268. {
  1269. CPPUNIT_ASSERT_EQUAL
  1270. (std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
  1271. util::percentEncode(":/?#[]@!%&'()*+,;="));
  1272. std::string unreserved =
  1273. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1274. "abcdefghijklmnopqrstuvwxyz"
  1275. "0123456789"
  1276. "-._~";
  1277. CPPUNIT_ASSERT_EQUAL(unreserved, util::percentEncode(unreserved));
  1278. CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::percentEncode("1^A "));
  1279. }
  1280. void UtilTest::testHtmlEscape()
  1281. {
  1282. CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),
  1283. util::htmlEscape("aria2<>\"'util"));
  1284. }
  1285. void UtilTest::testJoinPath()
  1286. {
  1287. const std::string dir1dir2file[] = { "dir1", "dir2", "file" };
  1288. CPPUNIT_ASSERT_EQUAL
  1289. (std::string("dir1/dir2/file"),
  1290. util::joinPath(vbegin(dir1dir2file), vend(dir1dir2file)));
  1291. const std::string dirparentfile[] = { "dir", "..", "file" };
  1292. CPPUNIT_ASSERT_EQUAL
  1293. (std::string("file"),
  1294. util::joinPath(vbegin(dirparentfile), vend(dirparentfile)));
  1295. const std::string dirparentparentfile[] = { "dir", "..", "..", "file" };
  1296. CPPUNIT_ASSERT_EQUAL
  1297. (std::string("file"),
  1298. util::joinPath(vbegin(dirparentparentfile), vend(dirparentparentfile)));
  1299. const std::string dirdotfile[] = { "dir", ".", "file" };
  1300. CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
  1301. util::joinPath(vbegin(dirdotfile), vend(dirdotfile)));
  1302. const std::string empty[] = {};
  1303. CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(&empty[0], &empty[0]));
  1304. const std::string parentdot[] = { "..", "." };
  1305. CPPUNIT_ASSERT_EQUAL(std::string(""),
  1306. util::joinPath(vbegin(parentdot), vend(parentdot)));
  1307. }
  1308. void UtilTest::testParseIndexPath()
  1309. {
  1310. std::pair<size_t, std::string> p = util::parseIndexPath("1=foo");
  1311. CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
  1312. CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
  1313. try {
  1314. util::parseIndexPath("1X=foo");
  1315. CPPUNIT_FAIL("exception must be thrown.");
  1316. } catch(Exception& e) {
  1317. // success
  1318. }
  1319. try {
  1320. util::parseIndexPath("1=");
  1321. CPPUNIT_FAIL("exception must be thrown.");
  1322. } catch(Exception& e) {
  1323. // success
  1324. }
  1325. }
  1326. void UtilTest::testCreateIndexPaths()
  1327. {
  1328. std::stringstream in
  1329. ("1=/tmp/myfile\n"
  1330. "100=/myhome/mypicture.png\n");
  1331. std::vector<std::pair<size_t, std::string> > m = util::createIndexPaths(in);
  1332. CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
  1333. CPPUNIT_ASSERT_EQUAL((size_t)1, m[0].first);
  1334. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[0].second);
  1335. CPPUNIT_ASSERT_EQUAL((size_t)100, m[1].first);
  1336. CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[1].second);
  1337. }
  1338. void UtilTest::testGenerateRandomData()
  1339. {
  1340. unsigned char data1[20];
  1341. util::generateRandomData(data1, sizeof(data1));
  1342. unsigned char data2[20];
  1343. util::generateRandomData(data2, sizeof(data2));
  1344. CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
  1345. }
  1346. void UtilTest::testFromHex()
  1347. {
  1348. std::string src;
  1349. std::string dest;
  1350. src = "0011fF";
  1351. dest = util::fromHex(src);
  1352. CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
  1353. CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
  1354. CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
  1355. CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
  1356. src = "0011f";
  1357. CPPUNIT_ASSERT(util::fromHex(src).empty());
  1358. src = "001g";
  1359. CPPUNIT_ASSERT(util::fromHex(src).empty());
  1360. }
  1361. void UtilTest::testParsePrioritizePieceRange()
  1362. {
  1363. // piece index
  1364. // 0 1 2 3 4 5 6 7
  1365. // | | | |
  1366. // file1 | | |
  1367. // | | |
  1368. // file2 | |
  1369. // file3 |
  1370. // | |
  1371. // file4 |
  1372. size_t pieceLength = 1024;
  1373. std::vector<SharedHandle<FileEntry> > entries(4, SharedHandle<FileEntry>());
  1374. entries[0].reset(new FileEntry("file1", 1024, 0));
  1375. entries[1].reset(new FileEntry("file2",2560,entries[0]->getLastOffset()));
  1376. entries[2].reset(new FileEntry("file3",0,entries[1]->getLastOffset()));
  1377. entries[3].reset(new FileEntry("file4",3584,entries[2]->getLastOffset()));
  1378. std::vector<size_t> result;
  1379. util::parsePrioritizePieceRange(result, "head=1", entries, pieceLength);
  1380. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1381. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1382. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1383. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1384. result.clear();
  1385. util::parsePrioritizePieceRange(result, "tail=1", entries, pieceLength);
  1386. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1387. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1388. CPPUNIT_ASSERT_EQUAL((size_t)3, result[1]);
  1389. CPPUNIT_ASSERT_EQUAL((size_t)6, result[2]);
  1390. result.clear();
  1391. util::parsePrioritizePieceRange(result, "head=1K", entries, pieceLength);
  1392. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1393. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1394. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1395. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1396. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1397. result.clear();
  1398. util::parsePrioritizePieceRange(result, "head", entries, pieceLength, 1024);
  1399. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1400. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1401. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1402. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1403. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1404. result.clear();
  1405. util::parsePrioritizePieceRange(result, "tail=1K", entries, pieceLength);
  1406. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1407. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1408. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1409. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1410. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1411. result.clear();
  1412. util::parsePrioritizePieceRange(result, "tail", entries, pieceLength, 1024);
  1413. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1414. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1415. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1416. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1417. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1418. result.clear();
  1419. util::parsePrioritizePieceRange(result, "head=1,tail=1", entries, pieceLength);
  1420. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1421. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1422. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1423. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1424. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1425. result.clear();
  1426. util::parsePrioritizePieceRange(result, "head=300M,tail=300M",
  1427. entries, pieceLength);
  1428. CPPUNIT_ASSERT_EQUAL((size_t)7, result.size());
  1429. for(size_t i = 0; i < 7; ++i) {
  1430. CPPUNIT_ASSERT_EQUAL(i, result[i]);
  1431. }
  1432. result.clear();
  1433. util::parsePrioritizePieceRange(result, "", entries, pieceLength);
  1434. CPPUNIT_ASSERT(result.empty());
  1435. }
  1436. void UtilTest::testApplyDir()
  1437. {
  1438. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir("", "pred"));
  1439. CPPUNIT_ASSERT_EQUAL(std::string("/pred"), util::applyDir("/", "pred"));
  1440. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir(".", "pred"));
  1441. CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred"));
  1442. }
  1443. void UtilTest::testFixTaintedBasename()
  1444. {
  1445. CPPUNIT_ASSERT_EQUAL(std::string("a%2Fb"), util::fixTaintedBasename("a/b"));
  1446. #ifdef __MINGW32__
  1447. CPPUNIT_ASSERT_EQUAL(std::string("a%5Cb"), util::fixTaintedBasename("a\\b"));
  1448. #else // !__MINGW32__
  1449. CPPUNIT_ASSERT_EQUAL(std::string("a\\b"), util::fixTaintedBasename("a\\b"));
  1450. #endif // !__MINGW32__
  1451. }
  1452. void UtilTest::testIsNumericHost()
  1453. {
  1454. CPPUNIT_ASSERT(util::isNumericHost("192.168.0.1"));
  1455. CPPUNIT_ASSERT(!util::isNumericHost("aria2.sf.net"));
  1456. CPPUNIT_ASSERT(util::isNumericHost("::1"));
  1457. }
  1458. void UtilTest::testDetectDirTraversal()
  1459. {
  1460. CPPUNIT_ASSERT(util::detectDirTraversal("/foo"));
  1461. CPPUNIT_ASSERT(util::detectDirTraversal("./foo"));
  1462. CPPUNIT_ASSERT(util::detectDirTraversal("../foo"));
  1463. CPPUNIT_ASSERT(util::detectDirTraversal("foo/../bar"));
  1464. CPPUNIT_ASSERT(util::detectDirTraversal("foo/./bar"));
  1465. CPPUNIT_ASSERT(util::detectDirTraversal("foo/."));
  1466. CPPUNIT_ASSERT(util::detectDirTraversal("foo/.."));
  1467. CPPUNIT_ASSERT(util::detectDirTraversal("."));
  1468. CPPUNIT_ASSERT(util::detectDirTraversal(".."));
  1469. CPPUNIT_ASSERT(util::detectDirTraversal("/"));
  1470. CPPUNIT_ASSERT(util::detectDirTraversal("foo/"));
  1471. CPPUNIT_ASSERT(util::detectDirTraversal("\t"));
  1472. CPPUNIT_ASSERT(!util::detectDirTraversal("foo/bar"));
  1473. CPPUNIT_ASSERT(!util::detectDirTraversal("foo"));
  1474. }
  1475. void UtilTest::testEscapePath()
  1476. {
  1477. CPPUNIT_ASSERT_EQUAL(std::string("foo%00bar%00%01"),
  1478. util::escapePath(std::string("foo")+(char)0x00+
  1479. std::string("bar")+(char)0x00+
  1480. (char)0x01));
  1481. #ifdef __MINGW32__
  1482. CPPUNIT_ASSERT_EQUAL(std::string("foo%5Cbar"), util::escapePath("foo\\bar"));
  1483. #else // !__MINGW32__
  1484. CPPUNIT_ASSERT_EQUAL(std::string("foo\\bar"), util::escapePath("foo\\bar"));
  1485. #endif // !__MINGW32__
  1486. }
  1487. void UtilTest::testInSameCidrBlock()
  1488. {
  1489. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 16));
  1490. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 17));
  1491. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "192.168.0.1", 32));
  1492. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.0.1", "192.168.0.0", 32));
  1493. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "10.0.0.1", 0));
  1494. CPPUNIT_ASSERT(util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 28));
  1495. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 29));
  1496. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "192.168.0.1", 8));
  1497. }
  1498. void UtilTest::testIsUtf8String()
  1499. {
  1500. CPPUNIT_ASSERT(util::isUtf8("ascii"));
  1501. // "Hello World" in Japanese UTF-8
  1502. CPPUNIT_ASSERT(util::isUtf8
  1503. (util::fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
  1504. // "World" in Shift_JIS
  1505. CPPUNIT_ASSERT(!util::isUtf8(util::fromHex("90a28a")+"E"));
  1506. // UTF8-2
  1507. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("c280")));
  1508. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("dfbf")));
  1509. // UTF8-3
  1510. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e0a080")));
  1511. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e0bf80")));
  1512. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e18080")));
  1513. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ec8080")));
  1514. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ed8080")));
  1515. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ed9f80")));
  1516. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ee8080")));
  1517. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ef8080")));
  1518. // UTF8-4
  1519. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f0908080")));
  1520. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f0bf8080")));
  1521. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f1808080")));
  1522. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f3808080")));
  1523. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f4808080")));
  1524. CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f48f8080")));
  1525. CPPUNIT_ASSERT(util::isUtf8(""));
  1526. CPPUNIT_ASSERT(!util::isUtf8(util::fromHex("00")));
  1527. }
  1528. void UtilTest::testNextParam()
  1529. {
  1530. std::string s1 = " :a : b=c :d=b::::g::";
  1531. std::pair<std::string::iterator, bool> r;
  1532. std::string name, value;
  1533. r = util::nextParam(name, value, s1.begin(), s1.end(), ':');
  1534. CPPUNIT_ASSERT(r.second);
  1535. CPPUNIT_ASSERT_EQUAL(std::string("a"), name);
  1536. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1537. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1538. CPPUNIT_ASSERT(r.second);
  1539. CPPUNIT_ASSERT_EQUAL(std::string("b"), name);
  1540. CPPUNIT_ASSERT_EQUAL(std::string("c"), value);
  1541. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1542. CPPUNIT_ASSERT(r.second);
  1543. CPPUNIT_ASSERT_EQUAL(std::string("d"), name);
  1544. CPPUNIT_ASSERT_EQUAL(std::string("b"), value);
  1545. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1546. CPPUNIT_ASSERT(r.second);
  1547. CPPUNIT_ASSERT_EQUAL(std::string("g"), name);
  1548. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1549. std::string s2 = "";
  1550. r = util::nextParam(name, value, s2.begin(), s2.end(), ':');
  1551. CPPUNIT_ASSERT(!r.second);
  1552. std::string s3 = " ";
  1553. r = util::nextParam(name, value, s3.begin(), s3.end(), ':');
  1554. CPPUNIT_ASSERT(!r.second);
  1555. std::string s4 = ":::";
  1556. r = util::nextParam(name, value, s4.begin(), s4.end(), ':');
  1557. CPPUNIT_ASSERT(!r.second);
  1558. }
  1559. void UtilTest::testNoProxyDomainMatch()
  1560. {
  1561. CPPUNIT_ASSERT(util::noProxyDomainMatch("localhost", "localhost"));
  1562. CPPUNIT_ASSERT(util::noProxyDomainMatch("192.168.0.1", "192.168.0.1"));
  1563. CPPUNIT_ASSERT(util::noProxyDomainMatch("www.example.org", ".example.org"));
  1564. CPPUNIT_ASSERT(!util::noProxyDomainMatch("www.example.org", "example.org"));
  1565. CPPUNIT_ASSERT(!util::noProxyDomainMatch("192.168.0.1", "0.1"));
  1566. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "example.com"));
  1567. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org"));
  1568. }
  1569. } // namespace aria2