UtilTest.cc 60 KB

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