UtilTest.cc 60 KB

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