UtilTest.cc 61 KB

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