UtilTest.cc 60 KB

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