UtilTest.cc 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  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(testParseContentDisposition);
  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(testMkdirs);
  50. CPPUNIT_TEST(testConvertBitfield);
  51. CPPUNIT_TEST(testParseIntSegments);
  52. CPPUNIT_TEST(testParseIntSegments_invalidRange);
  53. CPPUNIT_TEST(testParseIntNoThrow);
  54. CPPUNIT_TEST(testParseUIntNoThrow);
  55. CPPUNIT_TEST(testParseLLIntNoThrow);
  56. CPPUNIT_TEST(testToString_binaryStream);
  57. CPPUNIT_TEST(testItos);
  58. CPPUNIT_TEST(testUitos);
  59. CPPUNIT_TEST(testNtoh64);
  60. CPPUNIT_TEST(testPercentEncode);
  61. CPPUNIT_TEST(testHtmlEscape);
  62. CPPUNIT_TEST(testJoinPath);
  63. CPPUNIT_TEST(testParseIndexPath);
  64. CPPUNIT_TEST(testCreateIndexPaths);
  65. CPPUNIT_TEST(testGenerateRandomData);
  66. CPPUNIT_TEST(testFromHex);
  67. CPPUNIT_TEST(testParsePrioritizePieceRange);
  68. CPPUNIT_TEST(testApplyDir);
  69. CPPUNIT_TEST(testFixTaintedBasename);
  70. CPPUNIT_TEST(testIsNumericHost);
  71. CPPUNIT_TEST(testDetectDirTraversal);
  72. CPPUNIT_TEST(testEscapePath);
  73. CPPUNIT_TEST(testInSameCidrBlock);
  74. CPPUNIT_TEST(testIsUtf8String);
  75. CPPUNIT_TEST(testNextParam);
  76. CPPUNIT_TEST(testNoProxyDomainMatch);
  77. CPPUNIT_TEST(testInPrivateAddress);
  78. CPPUNIT_TEST(testSecfmt);
  79. CPPUNIT_TEST(testTlsHostnameMatch);
  80. CPPUNIT_TEST_SUITE_END();
  81. private:
  82. public:
  83. void setUp() {
  84. }
  85. void testStrip();
  86. void testStripIter();
  87. void testLstripIter();
  88. void testLstripIter_char();
  89. void testDivide();
  90. void testSplit();
  91. void testSplitIter();
  92. void testSplitIterM();
  93. void testStreq();
  94. void testStrieq();
  95. void testStrifind();
  96. void testEndsWith();
  97. void testIendsWith();
  98. void testReplace();
  99. void testStartsWith();
  100. void testIstartsWith();
  101. // may be moved to other helper class in the future.
  102. void testGetContentDispositionFilename();
  103. void testParseContentDisposition();
  104. void testToUpper();
  105. void testToLower();
  106. void testUppercase();
  107. void testLowercase();
  108. void testPercentDecode();
  109. void testGetRealSize();
  110. void testAbbrevSize();
  111. void testToStream();
  112. void testIsNumber();
  113. void testIsLowercase();
  114. void testIsUppercase();
  115. void testMkdirs();
  116. void testConvertBitfield();
  117. void testParseIntSegments();
  118. void testParseIntSegments_invalidRange();
  119. void testParseIntNoThrow();
  120. void testParseUIntNoThrow();
  121. void testParseLLIntNoThrow();
  122. void testToString_binaryStream();
  123. void testItos();
  124. void testUitos();
  125. void testNtoh64();
  126. void testPercentEncode();
  127. void testHtmlEscape();
  128. void testJoinPath();
  129. void testParseIndexPath();
  130. void testCreateIndexPaths();
  131. void testGenerateRandomData();
  132. void testFromHex();
  133. void testParsePrioritizePieceRange();
  134. void testApplyDir();
  135. void testFixTaintedBasename();
  136. void testIsNumericHost();
  137. void testDetectDirTraversal();
  138. void testEscapePath();
  139. void testInSameCidrBlock();
  140. void testIsUtf8String();
  141. void testNextParam();
  142. void testNoProxyDomainMatch();
  143. void testInPrivateAddress();
  144. void testSecfmt();
  145. void testTlsHostnameMatch();
  146. };
  147. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  148. void UtilTest::testStrip()
  149. {
  150. std::string str1 = "aria2";
  151. CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2"));
  152. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2"));
  153. CPPUNIT_ASSERT_EQUAL(str1, util::strip("aria2 "));
  154. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
  155. CPPUNIT_ASSERT_EQUAL(str1, util::strip(" aria2 "));
  156. std::string str2 = "aria2 debut";
  157. CPPUNIT_ASSERT_EQUAL(str2, util::strip("aria2 debut"));
  158. CPPUNIT_ASSERT_EQUAL(str2, util::strip(" aria2 debut "));
  159. std::string str3 = "";
  160. CPPUNIT_ASSERT_EQUAL(str3, util::strip(""));
  161. CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
  162. CPPUNIT_ASSERT_EQUAL(str3, util::strip(" "));
  163. std::string str4 = "A";
  164. CPPUNIT_ASSERT_EQUAL(str4, util::strip("A"));
  165. CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
  166. CPPUNIT_ASSERT_EQUAL(str4, util::strip(" A "));
  167. }
  168. void UtilTest::testStripIter()
  169. {
  170. Scip p;
  171. std::string str1 = "aria2";
  172. std::string s = "aria2";
  173. p = util::stripIter(s.begin(), s.end());
  174. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  175. s = " aria2";
  176. p = util::stripIter(s.begin(), s.end());
  177. CPPUNIT_ASSERT_EQUAL(str1, std::string(p.first, p.second));
  178. 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. std::string str2 = "aria2 debut";
  188. s = "aria2 debut";
  189. p = util::stripIter(s.begin(), s.end());
  190. CPPUNIT_ASSERT_EQUAL(str2, std::string(p.first, p.second));
  191. s = " aria2 debut ";
  192. p = util::stripIter(s.begin(), s.end());
  193. CPPUNIT_ASSERT_EQUAL(str2, std::string(p.first, p.second));
  194. std::string str3 = "";
  195. s = "";
  196. p = util::stripIter(s.begin(), s.end());
  197. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  198. s = " ";
  199. p = util::stripIter(s.begin(), s.end());
  200. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  201. s = " ";
  202. p = util::stripIter(s.begin(), s.end());
  203. CPPUNIT_ASSERT_EQUAL(str3, std::string(p.first, p.second));
  204. std::string str4 = "A";
  205. s = "A";
  206. p = util::stripIter(s.begin(), s.end());
  207. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  208. s = " A ";
  209. p = util::stripIter(s.begin(), s.end());
  210. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  211. s = " A ";
  212. p = util::stripIter(s.begin(), s.end());
  213. CPPUNIT_ASSERT_EQUAL(str4, std::string(p.first, p.second));
  214. }
  215. void UtilTest::testLstripIter()
  216. {
  217. std::string::iterator r;
  218. std::string s = "foo";
  219. r = util::lstripIter(s.begin(), s.end());
  220. CPPUNIT_ASSERT_EQUAL(std::string("foo"), std::string(r, s.end()));
  221. s = " foo bar ";
  222. r = util::lstripIter(s.begin(), s.end());
  223. CPPUNIT_ASSERT_EQUAL(std::string("foo bar "), std::string(r, s.end()));
  224. s = "f";
  225. r = util::lstripIter(s.begin(), s.end());
  226. CPPUNIT_ASSERT_EQUAL(std::string("f"), std::string(r, s.end()));
  227. s = "foo ";
  228. r = util::lstripIter(s.begin(), s.end());
  229. CPPUNIT_ASSERT_EQUAL(std::string("foo "), std::string(r, s.end()));
  230. }
  231. void UtilTest::testLstripIter_char()
  232. {
  233. std::string::iterator r;
  234. std::string s = "foo";
  235. r = util::lstripIter(s.begin(), s.end(), '$');
  236. CPPUNIT_ASSERT_EQUAL(std::string("foo"), std::string(r, s.end()));
  237. s = "$$foo$bar$$";
  238. r = util::lstripIter(s.begin(), s.end(), '$');
  239. CPPUNIT_ASSERT_EQUAL(std::string("foo$bar$$"), std::string(r, s.end()));
  240. s = "f";
  241. r = util::lstripIter(s.begin(), s.end(), '$');
  242. CPPUNIT_ASSERT_EQUAL(std::string("f"), std::string(r, s.end()));
  243. s = "foo$$";
  244. r = util::lstripIter(s.begin(), s.end(), '$');
  245. CPPUNIT_ASSERT_EQUAL(std::string("foo$$"), std::string(r, s.end()));
  246. }
  247. void UtilTest::testDivide() {
  248. std::pair<Sip, Sip> p1;
  249. std::string s = "name=value";
  250. util::divide(p1, s.begin(), s.end(), '=');
  251. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  252. std::string(p1.first.first, p1.first.second));
  253. CPPUNIT_ASSERT_EQUAL(std::string("value"),
  254. std::string(p1.second.first, p1.second.second));
  255. 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 = "=value";
  262. util::divide(p1, s.begin(), s.end(), '=');
  263. CPPUNIT_ASSERT_EQUAL(std::string(""),
  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 = "name=";
  268. util::divide(p1, s.begin(), s.end(), '=');
  269. CPPUNIT_ASSERT_EQUAL(std::string("name"),
  270. std::string(p1.first.first, p1.first.second));
  271. CPPUNIT_ASSERT_EQUAL(std::string(""),
  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. }
  280. void UtilTest::testSplit() {
  281. std::vector<std::string> v;
  282. std::string s = "k1; k2;; k3";
  283. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true);
  284. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  285. std::vector<std::string>::iterator itr = v.begin();
  286. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  287. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  288. CPPUNIT_ASSERT_EQUAL(std::string("k3"), *itr++);
  289. v.clear();
  290. s = "k1; k2; k3";
  291. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  292. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  293. 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 = "k=v";
  299. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  300. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  301. itr = v.begin();
  302. CPPUNIT_ASSERT_EQUAL(std::string("k=v"), *itr++);
  303. v.clear();
  304. s = ";;k1;;k2;";
  305. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  306. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  307. itr = v.begin();
  308. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  309. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  310. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  311. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  312. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  313. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  314. v.clear();
  315. s = ";;k1;;k2;";
  316. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  317. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  318. itr = v.begin();
  319. CPPUNIT_ASSERT_EQUAL(std::string("k1"), *itr++);
  320. CPPUNIT_ASSERT_EQUAL(std::string("k2"), *itr++);
  321. v.clear();
  322. s = "k; ";
  323. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  324. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  325. itr = v.begin();
  326. CPPUNIT_ASSERT_EQUAL(std::string("k"), *itr++);
  327. CPPUNIT_ASSERT_EQUAL(std::string(" "), *itr++);
  328. v.clear();
  329. s = " ";
  330. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true, true);
  331. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  332. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  333. v.clear();
  334. s = " ";
  335. util::split(s.begin(), s.end(), std::back_inserter(v), ';', true);
  336. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  337. v.clear();
  338. s = " ";
  339. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  340. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  341. CPPUNIT_ASSERT_EQUAL(std::string(" "), v[0]);
  342. v.clear();
  343. s = ";";
  344. util::split(s.begin(), s.end(), std::back_inserter(v), ';');
  345. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  346. v.clear();
  347. s = ";";
  348. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  349. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  350. itr = v.begin();
  351. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  352. CPPUNIT_ASSERT_EQUAL(std::string(""), *itr++);
  353. v.clear();
  354. s = "";
  355. util::split(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  356. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  357. CPPUNIT_ASSERT_EQUAL(std::string(""), v[0]);
  358. }
  359. void UtilTest::testSplitIter() {
  360. std::vector<Scip> v;
  361. std::string s = "k1; k2;; k3";
  362. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true);
  363. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  364. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  365. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  366. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  367. v.clear();
  368. s = "k1; k2; k3";
  369. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  370. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  371. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  372. CPPUNIT_ASSERT_EQUAL(std::string(" k2"),
  373. std::string(v[1].first, v[1].second));
  374. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  375. std::string(v[2].first, v[2].second));
  376. v.clear();
  377. s = "k=v";
  378. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  379. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  380. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  381. std::string(v[0].first, v[0].second));
  382. v.clear();
  383. s = ";;k1;;k2;";
  384. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  385. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  386. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  387. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  388. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  389. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  390. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  391. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  392. v.clear();
  393. s = ";;k1;;k2;";
  394. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  395. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  396. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  397. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  398. v.clear();
  399. s = "k; ";
  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("k"), std::string(v[0].first, v[0].second));
  403. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  404. v.clear();
  405. s = " ";
  406. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true, true);
  407. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  408. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  409. v.clear();
  410. s = " ";
  411. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', true);
  412. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  413. v.clear();
  414. s = " ";
  415. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  416. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  417. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[0].first, v[0].second));
  418. v.clear();
  419. s = ";";
  420. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';');
  421. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  422. v.clear();
  423. s = ";";
  424. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  425. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  426. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  427. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  428. v.clear();
  429. s = "";
  430. util::splitIter(s.begin(), s.end(), std::back_inserter(v), ';', false, true);
  431. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  432. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  433. }
  434. void UtilTest::testSplitIterM() {
  435. const char d[] = ";";
  436. const char md[] = "; ";
  437. std::vector<Scip> v;
  438. std::string s = "k1; k2;; k3";
  439. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true);
  440. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  441. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  442. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  443. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  444. v.clear();
  445. s = "k1; k2; k3";
  446. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  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"),
  450. std::string(v[1].first, v[1].second));
  451. CPPUNIT_ASSERT_EQUAL(std::string(" k3"),
  452. 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), md);
  456. CPPUNIT_ASSERT_EQUAL((size_t)3, v.size());
  457. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  458. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  459. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[2].first, v[2].second));
  460. v.clear();
  461. s = "k1; k2; k3;";
  462. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), md, false, true);
  463. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  464. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  465. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  466. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[2].first, v[2].second));
  467. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  468. CPPUNIT_ASSERT_EQUAL(std::string("k3"), std::string(v[4].first, v[4].second));
  469. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  470. v.clear();
  471. s = "k=v";
  472. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  473. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  474. CPPUNIT_ASSERT_EQUAL(std::string("k=v"),
  475. std::string(v[0].first, v[0].second));
  476. v.clear();
  477. s = ";;k1;;k2;";
  478. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  479. CPPUNIT_ASSERT_EQUAL((size_t)6, v.size());
  480. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  481. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  482. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[2].first, v[2].second));
  483. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[3].first, v[3].second));
  484. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[4].first, v[4].second));
  485. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[5].first, v[5].second));
  486. v.clear();
  487. s = ";;k1;;k2;";
  488. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  489. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  490. CPPUNIT_ASSERT_EQUAL(std::string("k1"), std::string(v[0].first, v[0].second));
  491. CPPUNIT_ASSERT_EQUAL(std::string("k2"), std::string(v[1].first, v[1].second));
  492. v.clear();
  493. s = "k; ";
  494. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  495. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  496. CPPUNIT_ASSERT_EQUAL(std::string("k"), std::string(v[0].first, v[0].second));
  497. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[1].first, v[1].second));
  498. v.clear();
  499. s = " ";
  500. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true, true);
  501. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  502. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  503. v.clear();
  504. s = " ";
  505. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, true);
  506. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  507. v.clear();
  508. s = " ";
  509. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  510. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  511. CPPUNIT_ASSERT_EQUAL(std::string(" "), std::string(v[0].first, v[0].second));
  512. v.clear();
  513. s = ";";
  514. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d);
  515. CPPUNIT_ASSERT_EQUAL((size_t)0, v.size());
  516. v.clear();
  517. s = ";";
  518. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  519. CPPUNIT_ASSERT_EQUAL((size_t)2, v.size());
  520. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  521. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[1].first, v[1].second));
  522. v.clear();
  523. s = "";
  524. util::splitIterM(s.begin(), s.end(), std::back_inserter(v), d, false, true);
  525. CPPUNIT_ASSERT_EQUAL((size_t)1, v.size());
  526. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(v[0].first, v[0].second));
  527. }
  528. void UtilTest::testEndsWith() {
  529. std::string target = "abcdefg";
  530. std::string part = "fg";
  531. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  532. part.begin(), part.end()));
  533. target = "abdefg";
  534. part = "g";
  535. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  536. part.begin(), part.end()));
  537. target = "abdefg";
  538. part = "eg";
  539. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  540. part.begin(), part.end()));
  541. target = "g";
  542. part = "eg";
  543. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  544. part.begin(), part.end()));
  545. target = "g";
  546. part = "g";
  547. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  548. part.begin(), part.end()));
  549. target = "g";
  550. part = "";
  551. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  552. part.begin(), part.end()));
  553. target = "";
  554. part = "";
  555. CPPUNIT_ASSERT(util::endsWith(target.begin(), target.end(),
  556. part.begin(), part.end()));
  557. target = "";
  558. part = "g";
  559. CPPUNIT_ASSERT(!util::endsWith(target.begin(), target.end(),
  560. part.begin(), part.end()));
  561. }
  562. void UtilTest::testIendsWith() {
  563. std::string target = "abcdefg";
  564. std::string part = "Fg";
  565. CPPUNIT_ASSERT(util::iendsWith(target.begin(), target.end(),
  566. part.begin(), part.end()));
  567. target = "abdefg";
  568. part = "ef";
  569. CPPUNIT_ASSERT(!util::iendsWith(target.begin(), target.end(),
  570. part.begin(), part.end()));
  571. }
  572. void UtilTest::testStreq()
  573. {
  574. std::string s1, s2;
  575. s1 = "foo";
  576. s2 = "foo";
  577. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  578. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.c_str()));
  579. s2 = "fooo";
  580. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  581. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  582. s2 = "fo";
  583. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  584. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  585. s2 = "";
  586. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  587. CPPUNIT_ASSERT(!util::streq(s1.begin(), s1.end(), s2.c_str()));
  588. s1 = "";
  589. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  590. CPPUNIT_ASSERT(util::streq(s1.begin(), s1.end(), s2.c_str()));
  591. }
  592. void UtilTest::testStrieq()
  593. {
  594. std::string s1, s2;
  595. s1 = "foo";
  596. s2 = "foo";
  597. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  598. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  599. s1 = "FoO";
  600. s2 = "fOo";
  601. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  602. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  603. s2 = "fooo";
  604. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  605. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  606. s2 = "fo";
  607. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  608. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  609. s2 = "";
  610. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  611. CPPUNIT_ASSERT(!util::strieq(s1.begin(), s1.end(), s2.c_str()));
  612. s1 = "";
  613. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.begin(), s2.end()));
  614. CPPUNIT_ASSERT(util::strieq(s1.begin(), s1.end(), s2.c_str()));
  615. }
  616. void UtilTest::testStrifind()
  617. {
  618. std::string s1, s2;
  619. s1 = "yamagakani mukashi wo toheba hARU no tuki";
  620. s2 = "HaRu";
  621. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  622. != s1.end());
  623. s2 = "aki";
  624. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  625. == s1.end());
  626. s1 = "h";
  627. s2 = "HH";
  628. CPPUNIT_ASSERT(util::strifind(s1.begin(), s1.end(), s2.begin(), s2.end())
  629. == s1.end());
  630. }
  631. void UtilTest::testReplace() {
  632. CPPUNIT_ASSERT_EQUAL(std::string("abc\n"), util::replace("abc\r\n", "\r", ""));
  633. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc\r\n", "\r\n", ""));
  634. CPPUNIT_ASSERT_EQUAL(std::string(""), util::replace("", "\r\n", ""));
  635. CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc", "", "a"));
  636. CPPUNIT_ASSERT_EQUAL(std::string("xbc"), util::replace("abc", "a", "x"));
  637. }
  638. void UtilTest::testStartsWith() {
  639. std::string target;
  640. std::string part;
  641. target = "abcdefg";
  642. part = "abc";
  643. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  644. part.begin(), part.end()));
  645. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  646. target = "abcdefg";
  647. part = "abx";
  648. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  649. part.begin(), part.end()));
  650. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
  651. target = "abcdefg";
  652. part = "bcd";
  653. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  654. part.begin(), part.end()));
  655. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
  656. target = "";
  657. part = "a";
  658. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(),
  659. part.begin(), part.end()));
  660. CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.c_str()));
  661. target = "";
  662. part = "";
  663. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  664. part.begin(), part.end()));
  665. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  666. target = "a";
  667. part = "";
  668. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  669. part.begin(), part.end()));
  670. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  671. target = "a";
  672. part = "a";
  673. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(),
  674. part.begin(), part.end()));
  675. CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.c_str()));
  676. }
  677. void UtilTest::testIstartsWith() {
  678. std::string target;
  679. std::string part;
  680. target = "abcdefg";
  681. part = "aBc";
  682. CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(),
  683. part.begin(), part.end()));
  684. CPPUNIT_ASSERT(util::istartsWith(target.begin(), target.end(), part.c_str()));
  685. target = "abcdefg";
  686. part = "abx";
  687. CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(),
  688. part.begin(), part.end()));
  689. CPPUNIT_ASSERT(!util::istartsWith(target.begin(), target.end(), part.c_str()));
  690. }
  691. void UtilTest::testGetContentDispositionFilename() {
  692. std::string val;
  693. val = "attachment; filename=\"aria2.tar.bz2\"";
  694. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  695. util::getContentDispositionFilename(val));
  696. val = "attachment; filename=\"\"";
  697. CPPUNIT_ASSERT_EQUAL(std::string(""),
  698. util::getContentDispositionFilename(val));
  699. val = "attachment; filename=\"";
  700. CPPUNIT_ASSERT_EQUAL(std::string(""),
  701. util::getContentDispositionFilename(val));
  702. val = "attachment; filename= \" aria2.tar.bz2 \"";
  703. CPPUNIT_ASSERT_EQUAL(std::string(" aria2.tar.bz2 "),
  704. util::getContentDispositionFilename(val));
  705. val = "attachment; filename=dir/file";
  706. CPPUNIT_ASSERT_EQUAL(std::string(""),
  707. util::getContentDispositionFilename(val));
  708. val = "attachment; filename=dir\\file";
  709. CPPUNIT_ASSERT_EQUAL(std::string(""),
  710. util::getContentDispositionFilename(val));
  711. val = "attachment; filename=\"dir/file\"";
  712. CPPUNIT_ASSERT_EQUAL(std::string(""),
  713. util::getContentDispositionFilename(val));
  714. val = "attachment; filename=\"dir\\\\file\"";
  715. CPPUNIT_ASSERT_EQUAL(std::string(""),
  716. util::getContentDispositionFilename(val));
  717. val = "attachment; filename=\"/etc/passwd\"";
  718. CPPUNIT_ASSERT_EQUAL(std::string(""),
  719. util::getContentDispositionFilename(val));
  720. val = "attachment; filename=\"..\"";
  721. CPPUNIT_ASSERT_EQUAL(std::string(""),
  722. util::getContentDispositionFilename(val));
  723. val = "attachment; filename=..";
  724. CPPUNIT_ASSERT_EQUAL(std::string(""),
  725. util::getContentDispositionFilename(val));
  726. // Unescaping %2E%2E%2F produces "../". But since we won't unescape,
  727. // we just accept it as is.
  728. val = "attachment; filename=\"%2E%2E%2Ffoo.html\"";
  729. CPPUNIT_ASSERT_EQUAL(std::string("%2E%2E%2Ffoo.html"),
  730. util::getContentDispositionFilename(val));
  731. // iso-8859-1 string will be converted to utf-8.
  732. val = "attachment; filename*=iso-8859-1''foo-%E4.html";
  733. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  734. util::getContentDispositionFilename(val));
  735. val = "attachment; filename*= UTF-8''foo-%c3%a4.html";
  736. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  737. util::getContentDispositionFilename(val));
  738. // iso-8859-1 string will be converted to utf-8.
  739. val = "attachment; filename=\"foo-%E4.html\"";
  740. val = util::percentDecode(val.begin(), val.end());
  741. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  742. util::getContentDispositionFilename(val));
  743. }
  744. void UtilTest::testParseContentDisposition() {
  745. char dest[1024];
  746. size_t destlen = sizeof(dest);
  747. const char *cs;
  748. size_t cslen;
  749. std::string val;
  750. // test cases from http://greenbytes.de/tech/tc2231/
  751. // inlonly
  752. val = "inline";
  753. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  754. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  755. // inlonlyquoted
  756. val = "\"inline\"";
  757. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  758. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  759. // inlwithasciifilename
  760. val = "inline; filename=\"foo.html\"";
  761. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  762. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  763. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  764. std::string(&dest[0], &dest[8]));
  765. // inlwithfnattach
  766. val = "inline; filename=\"Not an attachment!\"";
  767. CPPUNIT_ASSERT_EQUAL((ssize_t)18, util::parse_content_disposition
  768. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  769. CPPUNIT_ASSERT_EQUAL(std::string("Not an attachment!"),
  770. std::string(&dest[0], &dest[18]));
  771. // inlwithasciifilenamepdf
  772. val = "inline; filename=\"foo.pdf\"";
  773. CPPUNIT_ASSERT_EQUAL((ssize_t)7, util::parse_content_disposition
  774. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  775. CPPUNIT_ASSERT_EQUAL(std::string("foo.pdf"),
  776. std::string(&dest[0], &dest[7]));
  777. // attwithasciifilename25
  778. val = "attachment; filename=\"0000000000111111111122222\"";
  779. CPPUNIT_ASSERT_EQUAL((ssize_t)25, util::parse_content_disposition
  780. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  781. CPPUNIT_ASSERT_EQUAL(std::string("0000000000111111111122222"),
  782. std::string(&dest[0], &dest[25]));
  783. // attwithasciifilename35
  784. val = "attachment; filename=\"00000000001111111111222222222233333\"";
  785. CPPUNIT_ASSERT_EQUAL((ssize_t)35, util::parse_content_disposition
  786. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  787. CPPUNIT_ASSERT_EQUAL(std::string("00000000001111111111222222222233333"),
  788. std::string(&dest[0], &dest[35]));
  789. // attwithasciifnescapedchar
  790. val = "attachment; filename=\"f\\oo.html\"";
  791. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  792. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  793. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  794. std::string(&dest[0], &dest[8]));
  795. // attwithasciifnescapedquote
  796. val = "attachment; filename=\"\\\"quoting\\\" tested.html\"";
  797. CPPUNIT_ASSERT_EQUAL((ssize_t)21, util::parse_content_disposition
  798. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  799. CPPUNIT_ASSERT_EQUAL(std::string("\"quoting\" tested.html"),
  800. std::string(&dest[0], &dest[21]));
  801. // attwithquotedsemicolon
  802. val = "attachment; filename=\"Here's a semicolon;.html\"";
  803. CPPUNIT_ASSERT_EQUAL((ssize_t)24, util::parse_content_disposition
  804. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  805. CPPUNIT_ASSERT_EQUAL(std::string("Here's a semicolon;.html"),
  806. std::string(&dest[0], &dest[24]));
  807. // attwithfilenameandextparam
  808. val = "attachment; foo=\"bar\"; filename=\"foo.html\"";
  809. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  810. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  811. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  812. std::string(&dest[0], &dest[8]));
  813. // attwithfilenameandextparamescaped
  814. val = "attachment; foo=\"\\\"\\\\\";filename=\"foo.html\"";
  815. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  816. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  817. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  818. std::string(&dest[0], &dest[8]));
  819. // attwithasciifilenameucase
  820. val = "attachment; FILENAME=\"foo.html\"";
  821. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  822. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  823. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  824. std::string(&dest[0], &dest[8]));
  825. // attwithasciifilenamenq
  826. val = "attachment; filename=foo.html";
  827. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  828. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  829. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  830. std::string(&dest[0], &dest[8]));
  831. // attwithtokfncommanq
  832. val = "attachment; filename=foo,bar.html";
  833. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  834. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  835. // attwithasciifilenamenqs
  836. val = "attachment; filename=foo.html ;";
  837. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  838. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  839. // attemptyparam
  840. val = "attachment; ;filename=foo";
  841. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  842. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  843. // attwithasciifilenamenqws
  844. val = "attachment; filename=foo bar.html";
  845. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  846. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  847. // attwithfntokensq
  848. val = "attachment; filename='foo.bar'";
  849. CPPUNIT_ASSERT_EQUAL((ssize_t)9, util::parse_content_disposition
  850. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  851. CPPUNIT_ASSERT_EQUAL(std::string("'foo.bar'"),
  852. std::string(&dest[0], &dest[9]));
  853. // attwithisofnplain
  854. // attachment; filename="foo-ä.html"
  855. val = "attachment; filename=\"foo-%E4.html\"";
  856. val = util::percentDecode(val.begin(), val.end());
  857. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  858. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  859. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  860. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[10])));
  861. // attwithutf8fnplain
  862. // attachment; filename="foo-ä.html"
  863. val = "attachment; filename=\"foo-%C3%A4.html\"";
  864. val = util::percentDecode(val.begin(), val.end());
  865. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  866. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  867. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  868. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[11])));
  869. // attwithfnrawpctenca
  870. val = "attachment; filename=\"foo-%41.html\"";
  871. CPPUNIT_ASSERT_EQUAL((ssize_t)12, util::parse_content_disposition
  872. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  873. CPPUNIT_ASSERT_EQUAL(std::string("foo-%41.html"),
  874. std::string(&dest[0], &dest[12]));
  875. // attwithfnusingpct
  876. val = "attachment; filename=\"50%.html\"";
  877. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  878. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  879. CPPUNIT_ASSERT_EQUAL(std::string("50%.html"),
  880. std::string(&dest[0], &dest[8]));
  881. // attwithfnrawpctencaq
  882. val = "attachment; filename=\"foo-%\\41.html\"";
  883. CPPUNIT_ASSERT_EQUAL((ssize_t)12, util::parse_content_disposition
  884. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  885. CPPUNIT_ASSERT_EQUAL(std::string("foo-%41.html"),
  886. std::string(&dest[0], &dest[12]));
  887. // attwithnamepct
  888. val = "attachment; name=\"foo-%41.html\"";
  889. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  890. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  891. // attwithfilenamepctandiso
  892. // attachment; filename="ä-%41.html"
  893. val = "attachment; filename=\"%E4-%2541.html\"";
  894. val = util::percentDecode(val.begin(), val.end());
  895. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  896. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  897. CPPUNIT_ASSERT_EQUAL(std::string("ä-%41.html"),
  898. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[10])));
  899. // attwithfnrawpctenclong
  900. val = "attachment; filename=\"foo-%c3%a4-%e2%82%ac.html\"";
  901. CPPUNIT_ASSERT_EQUAL((ssize_t)25, util::parse_content_disposition
  902. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  903. CPPUNIT_ASSERT_EQUAL(std::string("foo-%c3%a4-%e2%82%ac.html"),
  904. std::string(&dest[0], &dest[25]));
  905. // attwithasciifilenamews1
  906. val = "attachment; filename =\"foo.html\"";
  907. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  908. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  909. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  910. std::string(&dest[0], &dest[8]));
  911. // attwith2filenames
  912. val = "attachment; filename=\"foo.html\"; filename=\"bar.html\"";
  913. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  914. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  915. // attfnbrokentoken
  916. val = "attachment; filename=foo[1](2).html";
  917. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  918. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  919. // attfnbrokentokeniso
  920. val = "attachment; filename=foo-%E4.html";
  921. val = util::percentDecode(val.begin(), val.end());
  922. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  923. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  924. // attfnbrokentokenutf
  925. // attachment; filename=foo-ä.html
  926. val = "attachment; filename=foo-ä.html";
  927. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  928. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  929. // attmissingdisposition
  930. val = "filename=foo.html";
  931. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  932. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  933. // attmissingdisposition2
  934. val = "x=y; filename=foo.html";
  935. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  936. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  937. // attmissingdisposition3
  938. val = "\"foo; filename=bar;baz\"; filename=qux";
  939. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  940. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  941. // attmissingdisposition4
  942. val = "filename=foo.html, filename=bar.html";
  943. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  944. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  945. // emptydisposition
  946. val = "; filename=foo.html";
  947. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  948. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  949. // doublecolon
  950. val = ": inline; attachment; filename=foo.html";
  951. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  952. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  953. // attandinline
  954. val = "inline; attachment; filename=foo.html";
  955. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  956. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  957. // attandinline2
  958. val = "attachment; inline; filename=foo.html";
  959. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  960. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  961. // attbrokenquotedfn
  962. val = "attachment; filename=\"foo.html\".txt";
  963. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  964. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  965. // attbrokenquotedfn2
  966. val = "attachment; filename=\"bar";
  967. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  968. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  969. // attbrokenquotedfn3
  970. val = "attachment; filename=foo\"bar;baz\"qux";
  971. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  972. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  973. // attmultinstances
  974. val = "attachment; filename=foo.html, attachment; filename=bar.html";
  975. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  976. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  977. // attmissingdelim
  978. val = "attachment; foo=foo filename=bar";
  979. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  980. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  981. // attmissingdelim2
  982. val = "attachment; filename=bar foo=foo ";
  983. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  984. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  985. // attmissingdelim3
  986. val = "attachment filename=bar";
  987. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  988. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  989. // attreversed
  990. val = "filename=foo.html; attachment";
  991. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  992. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  993. // attconfusedparam
  994. val = "attachment; xfilename=foo.html";
  995. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  996. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  997. // attabspath
  998. val = "attachment; filename=\"/foo.html\"";
  999. CPPUNIT_ASSERT_EQUAL((ssize_t)9, util::parse_content_disposition
  1000. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1001. CPPUNIT_ASSERT_EQUAL(std::string("/foo.html"),
  1002. std::string(&dest[0], &dest[9]));
  1003. // attabspathwin
  1004. val = "attachment; filename=\"\\\\foo.html\"";
  1005. CPPUNIT_ASSERT_EQUAL((ssize_t)9, util::parse_content_disposition
  1006. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1007. CPPUNIT_ASSERT_EQUAL(std::string("\\foo.html"),
  1008. std::string(&dest[0], &dest[9]));
  1009. // attcdate
  1010. val = "attachment; creation-date=\"Wed, 12 Feb 1997 16:29:51 -0500\"";
  1011. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1012. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1013. // dispext
  1014. val = "foobar";
  1015. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1016. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1017. // dispextbadfn
  1018. val = "attachment; example=\"filename=example.txt\"";
  1019. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1020. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1021. // attwithisofn2231iso
  1022. val = "attachment; filename*=iso-8859-1''foo-%E4.html";
  1023. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  1024. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1025. CPPUNIT_ASSERT_EQUAL(std::string("iso-8859-1"), std::string(cs, cslen));
  1026. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1027. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[10])));
  1028. // attwithfn2231utf8
  1029. val = "attachment; filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html";
  1030. CPPUNIT_ASSERT_EQUAL((ssize_t)15, util::parse_content_disposition
  1031. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1032. CPPUNIT_ASSERT_EQUAL(std::string("UTF-8"), std::string(cs, cslen));
  1033. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä-€.html"),
  1034. std::string(&dest[0], &dest[15]));
  1035. // attwithfn2231noc
  1036. val = "attachment; filename*=''foo-%c3%a4-%e2%82%ac.html";
  1037. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1038. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1039. // attwithfn2231utf8comp
  1040. val = "attachment; filename*=UTF-8''foo-a%cc%88.html";
  1041. CPPUNIT_ASSERT_EQUAL((ssize_t)12, util::parse_content_disposition
  1042. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1043. val = "foo-a%cc%88.html";
  1044. CPPUNIT_ASSERT_EQUAL(std::string(util::percentDecode(val.begin(),
  1045. val.end())),
  1046. std::string(&dest[0], &dest[12]));
  1047. // attwithfn2231utf8-bad
  1048. val = "attachment; filename*=iso-8859-1''foo-%c3%a4-%e2%82%ac.html";
  1049. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1050. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1051. // attwithfn2231iso-bad
  1052. val = "attachment; filename*=utf-8''foo-%E4.html";
  1053. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1054. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1055. // attwithfn2231ws1
  1056. val = "attachment; filename *=UTF-8''foo-%c3%a4.html";
  1057. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1058. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1059. // attwithfn2231ws2
  1060. val = "attachment; filename*= UTF-8''foo-%c3%a4.html";
  1061. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  1062. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1063. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1064. std::string(&dest[0], &dest[11]));
  1065. // attwithfn2231ws3
  1066. val = "attachment; filename* =UTF-8''foo-%c3%a4.html";
  1067. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  1068. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1069. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1070. std::string(&dest[0], &dest[11]));
  1071. // attwithfn2231quot
  1072. val = "attachment; filename*=\"UTF-8''foo-%c3%a4.html\"";
  1073. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1074. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1075. // attwithfn2231quot2
  1076. val = "attachment; filename*=\"foo%20bar.html\"";
  1077. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1078. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1079. // attwithfn2231singleqmissing
  1080. val = "attachment; filename*=UTF-8'foo-%c3%a4.html";
  1081. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1082. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1083. // attwithfn2231nbadpct1
  1084. val = "attachment; filename*=UTF-8''foo%";
  1085. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1086. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1087. // attwithfn2231nbadpct2
  1088. val = "attachment; filename*=UTF-8''f%oo.html";
  1089. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1090. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1091. // attwithfn2231dpct
  1092. val = "attachment; filename*=UTF-8''A-%2541.html";
  1093. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  1094. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1095. CPPUNIT_ASSERT_EQUAL(std::string("A-%41.html"),
  1096. std::string(&dest[0], &dest[10]));
  1097. // attwithfn2231abspathdisguised
  1098. val = "attachment; filename*=UTF-8''%5cfoo.html";
  1099. CPPUNIT_ASSERT_EQUAL((ssize_t)9, util::parse_content_disposition
  1100. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1101. CPPUNIT_ASSERT_EQUAL(std::string("\\foo.html"),
  1102. std::string(&dest[0], &dest[9]));
  1103. // attfnboth
  1104. val = "attachment; filename=\"foo-ae.html\"; filename*=UTF-8''foo-%c3%a4.html";
  1105. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  1106. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1107. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1108. std::string(&dest[0], &dest[11]));
  1109. // attfnboth2
  1110. val = "attachment; filename*=UTF-8''foo-%c3%a4.html; filename=\"foo-ae.html\"";
  1111. CPPUNIT_ASSERT_EQUAL((ssize_t)11, util::parse_content_disposition
  1112. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1113. CPPUNIT_ASSERT_EQUAL(std::string("foo-ä.html"),
  1114. std::string(&dest[0], &dest[11]));
  1115. // attfnboth3
  1116. val = "attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4; filename*=ISO-8859-1''currency-sign%3d%a4";
  1117. CPPUNIT_ASSERT_EQUAL((ssize_t)15, util::parse_content_disposition
  1118. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1119. CPPUNIT_ASSERT_EQUAL(std::string("ISO-8859-1"), std::string(cs, cslen));
  1120. CPPUNIT_ASSERT_EQUAL(std::string("currency-sign=¤"),
  1121. util::iso8859p1ToUtf8(std::string(&dest[0], &dest[15])));
  1122. // attnewandfn
  1123. val = "attachment; foobar=x; filename=\"foo.html\"";
  1124. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1125. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1126. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1127. std::string(&dest[0], &dest[8]));
  1128. // attrfc2047token
  1129. val = "attachment; filename==?ISO-8859-1?Q?foo-=E4.html?=";
  1130. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1131. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1132. // attrfc2047quoted
  1133. val = "attachment; filename=\"=?ISO-8859-1?Q?foo-=E4.html?=\"";
  1134. CPPUNIT_ASSERT_EQUAL((ssize_t)29, util::parse_content_disposition
  1135. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1136. CPPUNIT_ASSERT_EQUAL(std::string("=?ISO-8859-1?Q?foo-=E4.html?="),
  1137. std::string(&dest[0], &dest[29]));
  1138. // aria2 original testcases
  1139. // zero-length filename. token cannot be empty, so this is invalid.
  1140. val = "attachment; filename=";
  1141. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1142. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1143. // zero-length filename. quoted-string can be empty string, so this
  1144. // is ok.
  1145. val = "attachment; filename=\"\"";
  1146. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1147. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1148. // empty value is not allowed
  1149. val = "attachment; filename=;";
  1150. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1151. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1152. // / is not valid char in token.
  1153. val = "attachment; filename=dir/file";
  1154. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, util::parse_content_disposition
  1155. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1156. // value-chars is *(pct-encoded / attr-char), so empty string is
  1157. // allowed.
  1158. val = "attachment; filename*=UTF-8''";
  1159. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1160. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1161. CPPUNIT_ASSERT_EQUAL(std::string("UTF-8"), std::string(cs, cslen));
  1162. val = "attachment; filename*=UTF-8''; filename=foo";
  1163. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1164. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1165. CPPUNIT_ASSERT_EQUAL(std::string("UTF-8"), std::string(cs, cslen));
  1166. val = "attachment; filename*=UTF-8'' ; filename=foo";
  1167. CPPUNIT_ASSERT_EQUAL((ssize_t)0, util::parse_content_disposition
  1168. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1169. CPPUNIT_ASSERT_EQUAL(std::string("UTF-8"), std::string(cs, cslen));
  1170. // with language
  1171. val = "attachment; filename*=UTF-8'japanese'konnichiwa";
  1172. CPPUNIT_ASSERT_EQUAL((ssize_t)10, util::parse_content_disposition
  1173. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1174. CPPUNIT_ASSERT_EQUAL(std::string("konnichiwa"),
  1175. std::string(&dest[0], &dest[10]));
  1176. // lws before and after "="
  1177. val = "attachment; filename = foo.html";
  1178. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1179. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1180. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1181. std::string(&dest[0], &dest[8]));
  1182. // lws before and after "=" with quoted-string
  1183. val = "attachment; filename = \"foo.html\"";
  1184. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1185. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1186. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1187. std::string(&dest[0], &dest[8]));
  1188. // lws after parm
  1189. val = "attachment; filename=foo.html ";
  1190. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1191. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1192. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1193. std::string(&dest[0], &dest[8]));
  1194. val = "attachment; filename=foo.html ; hello=world";
  1195. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1196. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1197. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1198. std::string(&dest[0], &dest[8]));
  1199. val = "attachment; filename=\"foo.html\" ";
  1200. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1201. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1202. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1203. std::string(&dest[0], &dest[8]));
  1204. val = "attachment; filename=\"foo.html\" ; hello=world";
  1205. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1206. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1207. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1208. std::string(&dest[0], &dest[8]));
  1209. val = "attachment; filename*=UTF-8''foo.html ; hello=world";
  1210. CPPUNIT_ASSERT_EQUAL((ssize_t)8, util::parse_content_disposition
  1211. (dest, destlen, &cs, &cslen, val.c_str(), val.size()));
  1212. CPPUNIT_ASSERT_EQUAL(std::string("foo.html"),
  1213. std::string(&dest[0], &dest[8]));
  1214. }
  1215. class Printer {
  1216. public:
  1217. template<class T>
  1218. void operator()(T t) {
  1219. std::cerr << t << ", ";
  1220. }
  1221. };
  1222. void UtilTest::testToUpper() {
  1223. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  1224. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  1225. CPPUNIT_ASSERT_EQUAL(upp, util::toUpper(src));
  1226. }
  1227. void UtilTest::testToLower() {
  1228. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  1229. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  1230. CPPUNIT_ASSERT_EQUAL(upp, util::toLower(src));
  1231. }
  1232. void UtilTest::testUppercase() {
  1233. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  1234. std::string ans = "608CABC0F2FA18C260CAFD974516865C772363D5";
  1235. util::uppercase(src);
  1236. CPPUNIT_ASSERT_EQUAL(ans, src);
  1237. }
  1238. void UtilTest::testLowercase() {
  1239. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  1240. std::string ans = "608cabc0f2fa18c260cafd974516865c772363d5";
  1241. util::lowercase(src);
  1242. CPPUNIT_ASSERT_EQUAL(ans, src);
  1243. }
  1244. void UtilTest::testPercentDecode() {
  1245. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  1246. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  1247. util::percentDecode(src.begin(), src.end()));
  1248. std::string src2 = "aria2+aria2";
  1249. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"),
  1250. util::percentDecode(src2.begin(), src2.end()));
  1251. std::string src3 = "%5t%20";
  1252. CPPUNIT_ASSERT_EQUAL(std::string("%5t "),
  1253. util::percentDecode(src3.begin(), src3.end()));
  1254. std::string src4 = "%";
  1255. CPPUNIT_ASSERT_EQUAL(std::string("%"),
  1256. util::percentDecode(src4.begin(), src4.end()));
  1257. std::string src5 = "%3";
  1258. CPPUNIT_ASSERT_EQUAL(std::string("%3"),
  1259. util::percentDecode(src5.begin(), src5.end()));
  1260. std::string src6 = "%2f";
  1261. CPPUNIT_ASSERT_EQUAL(std::string("/"),
  1262. util::percentDecode(src6.begin(), src6.end()));
  1263. }
  1264. void UtilTest::testGetRealSize()
  1265. {
  1266. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096M"));
  1267. CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1K"));
  1268. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096m"));
  1269. CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1k"));
  1270. try {
  1271. util::getRealSize("");
  1272. CPPUNIT_FAIL("exception must be thrown.");
  1273. } catch(Exception& e) {
  1274. std::cerr << e.stackTrace();
  1275. }
  1276. try {
  1277. util::getRealSize("foo");
  1278. CPPUNIT_FAIL("exception must be thrown.");
  1279. } catch(Exception& e) {
  1280. std::cerr << e.stackTrace();
  1281. }
  1282. try {
  1283. util::getRealSize("-1");
  1284. CPPUNIT_FAIL("exception must be thrown.");
  1285. } catch(Exception& e) {
  1286. std::cerr << e.stackTrace();
  1287. }
  1288. try {
  1289. util::getRealSize("9223372036854775807K");
  1290. CPPUNIT_FAIL("exception must be thrown.");
  1291. } catch(Exception& e) {
  1292. std::cerr << e.stackTrace();
  1293. }
  1294. try {
  1295. util::getRealSize("9223372036854775807M");
  1296. CPPUNIT_FAIL("exception must be thrown.");
  1297. } catch(Exception& e) {
  1298. std::cerr << e.stackTrace();
  1299. }
  1300. }
  1301. void UtilTest::testAbbrevSize()
  1302. {
  1303. CPPUNIT_ASSERT_EQUAL(std::string("8,589,934,591Gi"),
  1304. util::abbrevSize(9223372036854775807LL));
  1305. CPPUNIT_ASSERT_EQUAL(std::string("4.0Gi"), util::abbrevSize(4294967296LL));
  1306. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1024));
  1307. CPPUNIT_ASSERT_EQUAL(std::string("0.9Ki"), util::abbrevSize(1023));
  1308. CPPUNIT_ASSERT_EQUAL(std::string("511"), util::abbrevSize(511));
  1309. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::abbrevSize(0));
  1310. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), util::abbrevSize(1127));
  1311. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), util::abbrevSize(1572864));
  1312. }
  1313. void UtilTest::testToStream()
  1314. {
  1315. std::ostringstream os;
  1316. std::shared_ptr<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  1317. std::shared_ptr<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  1318. std::deque<std::shared_ptr<FileEntry> > entries;
  1319. entries.push_back(f1);
  1320. entries.push_back(f2);
  1321. const char* filename = A2_TEST_OUT_DIR"/aria2_UtilTest_testToStream";
  1322. BufferedFile fp(filename, BufferedFile::WRITE);
  1323. util::toStream(entries.begin(), entries.end(), fp);
  1324. fp.close();
  1325. CPPUNIT_ASSERT_EQUAL(
  1326. std::string("Files:\n"
  1327. "idx|path/length\n"
  1328. "===+===========================================================================\n"
  1329. " 1|aria2.tar.bz2\n"
  1330. " |12KiB (12,300)\n"
  1331. "---+---------------------------------------------------------------------------\n"
  1332. " 2|aria2.txt\n"
  1333. " |556B (556)\n"
  1334. "---+---------------------------------------------------------------------------\n"),
  1335. readFile(filename));
  1336. }
  1337. void UtilTest::testIsNumber()
  1338. {
  1339. std::string s = "000";
  1340. CPPUNIT_ASSERT_EQUAL(true, util::isNumber(s.begin(),s.end()));
  1341. s = "a";
  1342. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  1343. s = "0a";
  1344. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  1345. s = "";
  1346. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  1347. s = " ";
  1348. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  1349. }
  1350. void UtilTest::testIsLowercase()
  1351. {
  1352. std::string s = "alpha";
  1353. CPPUNIT_ASSERT_EQUAL(true, util::isLowercase(s.begin(), s.end()));
  1354. s = "Alpha";
  1355. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  1356. s = "1alpha";
  1357. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  1358. s = "";
  1359. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  1360. s = " ";
  1361. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  1362. }
  1363. void UtilTest::testIsUppercase()
  1364. {
  1365. std::string s = "ALPHA";
  1366. CPPUNIT_ASSERT_EQUAL(true, util::isUppercase(s.begin(), s.end()));
  1367. s = "Alpha";
  1368. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  1369. s = "1ALPHA";
  1370. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  1371. s = "";
  1372. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  1373. s = " ";
  1374. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  1375. }
  1376. void UtilTest::testMkdirs()
  1377. {
  1378. std::string dir = A2_TEST_OUT_DIR"/aria2-UtilTest-testMkdirs";
  1379. File d(dir);
  1380. if(d.exists()) {
  1381. CPPUNIT_ASSERT(d.remove());
  1382. }
  1383. CPPUNIT_ASSERT(!d.exists());
  1384. util::mkdirs(dir);
  1385. CPPUNIT_ASSERT(d.isDir());
  1386. std::string file = A2_TEST_DIR"/UtilTest.cc";
  1387. File f(file);
  1388. CPPUNIT_ASSERT(f.isFile());
  1389. try {
  1390. util::mkdirs(file);
  1391. CPPUNIT_FAIL("exception must be thrown.");
  1392. } catch(DlAbortEx& ex) {
  1393. std::cerr << ex.stackTrace() << std::endl;
  1394. }
  1395. }
  1396. void UtilTest::testConvertBitfield()
  1397. {
  1398. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  1399. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  1400. srcBitfield.setAllBit();
  1401. srcBitfield.unsetBit(2);// <- range [768, 1152)
  1402. // which corresponds to the index [1,2] in destBitfield
  1403. util::convertBitfield(&destBitfield, &srcBitfield);
  1404. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  1405. util::toHex(destBitfield.getBitfield(),
  1406. destBitfield.getBitfieldLength()));
  1407. }
  1408. void UtilTest::testParseIntSegments()
  1409. {
  1410. SegList<int> sgl;
  1411. util::parseIntSegments(sgl, "1,3-8,10");
  1412. CPPUNIT_ASSERT(sgl.hasNext());
  1413. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  1414. CPPUNIT_ASSERT(sgl.hasNext());
  1415. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  1416. CPPUNIT_ASSERT(sgl.hasNext());
  1417. CPPUNIT_ASSERT_EQUAL(4, sgl.next());
  1418. CPPUNIT_ASSERT(sgl.hasNext());
  1419. CPPUNIT_ASSERT_EQUAL(5, sgl.next());
  1420. CPPUNIT_ASSERT(sgl.hasNext());
  1421. CPPUNIT_ASSERT_EQUAL(6, sgl.next());
  1422. CPPUNIT_ASSERT(sgl.hasNext());
  1423. CPPUNIT_ASSERT_EQUAL(7, sgl.next());
  1424. CPPUNIT_ASSERT(sgl.hasNext());
  1425. CPPUNIT_ASSERT_EQUAL(8, sgl.next());
  1426. CPPUNIT_ASSERT(sgl.hasNext());
  1427. CPPUNIT_ASSERT_EQUAL(10, sgl.next());
  1428. CPPUNIT_ASSERT(!sgl.hasNext());
  1429. CPPUNIT_ASSERT_EQUAL(0, sgl.next());
  1430. sgl.clear();
  1431. util::parseIntSegments(sgl, ",,,1,,,3,,,");
  1432. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  1433. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  1434. CPPUNIT_ASSERT(!sgl.hasNext());
  1435. }
  1436. void UtilTest::testParseIntSegments_invalidRange()
  1437. {
  1438. try {
  1439. SegList<int> sgl;
  1440. util::parseIntSegments(sgl, "-1");
  1441. CPPUNIT_FAIL("exception must be thrown.");
  1442. } catch(Exception& e) {
  1443. }
  1444. try {
  1445. SegList<int> sgl;
  1446. util::parseIntSegments(sgl, "1-");
  1447. CPPUNIT_FAIL("exception must be thrown.");
  1448. } catch(Exception& e) {
  1449. }
  1450. try {
  1451. SegList<int> sgl;
  1452. util::parseIntSegments(sgl, "2147483648");
  1453. CPPUNIT_FAIL("exception must be thrown.");
  1454. } catch(Exception& e) {
  1455. }
  1456. try {
  1457. SegList<int> sgl;
  1458. util::parseIntSegments(sgl, "2147483647-2147483648");
  1459. CPPUNIT_FAIL("exception must be thrown.");
  1460. } catch(Exception& e) {
  1461. }
  1462. try {
  1463. SegList<int> sgl;
  1464. util::parseIntSegments(sgl, "1-2x");
  1465. CPPUNIT_FAIL("exception must be thrown.");
  1466. } catch(Exception& e) {
  1467. }
  1468. try {
  1469. SegList<int> sgl;
  1470. util::parseIntSegments(sgl, "3x-4");
  1471. CPPUNIT_FAIL("exception must be thrown.");
  1472. } catch(Exception& e) {
  1473. }
  1474. }
  1475. void UtilTest::testParseIntNoThrow()
  1476. {
  1477. std::string s;
  1478. int32_t n;
  1479. s = " -1 ";
  1480. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s));
  1481. CPPUNIT_ASSERT_EQUAL((int32_t)-1, n);
  1482. s = "2147483647";
  1483. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s));
  1484. CPPUNIT_ASSERT_EQUAL((int32_t)2147483647, n);
  1485. s = "2147483648";
  1486. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1487. s = "-2147483649";
  1488. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1489. s = "12x";
  1490. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1491. s = "";
  1492. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  1493. }
  1494. void UtilTest::testParseUIntNoThrow()
  1495. {
  1496. std::string s;
  1497. uint32_t n;
  1498. s = " 2147483647 ";
  1499. CPPUNIT_ASSERT(util::parseUIntNoThrow(n, s));
  1500. CPPUNIT_ASSERT_EQUAL((uint32_t)INT32_MAX, n);
  1501. s = "2147483648";
  1502. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s));
  1503. s = "-1";
  1504. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s));
  1505. }
  1506. void UtilTest::testParseLLIntNoThrow()
  1507. {
  1508. std::string s;
  1509. int64_t n;
  1510. s = " 9223372036854775807 ";
  1511. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s));
  1512. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MAX, n);
  1513. s = "9223372036854775808";
  1514. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s));
  1515. s = "-9223372036854775808";
  1516. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s));
  1517. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MIN, n);
  1518. s = "-9223372036854775809";
  1519. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s));
  1520. }
  1521. void UtilTest::testToString_binaryStream()
  1522. {
  1523. std::shared_ptr<DiskWriter> dw(new ByteArrayDiskWriter());
  1524. std::string data(16*1024+256, 'a');
  1525. dw->initAndOpenFile();
  1526. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  1527. std::string readData = util::toString(dw);
  1528. CPPUNIT_ASSERT_EQUAL(data, readData);
  1529. }
  1530. void UtilTest::testItos()
  1531. {
  1532. {
  1533. int i = 0;
  1534. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::itos(i));
  1535. }
  1536. {
  1537. int i = 100;
  1538. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i, true));
  1539. }
  1540. {
  1541. int i = 100;
  1542. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i));
  1543. }
  1544. {
  1545. int i = 12345;
  1546. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), util::itos(i, true));
  1547. }
  1548. {
  1549. int i = 12345;
  1550. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::itos(i));
  1551. }
  1552. {
  1553. int i = -12345;
  1554. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), util::itos(i, true));
  1555. }
  1556. {
  1557. int64_t i = INT64_MAX;
  1558. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"),
  1559. util::itos(i, true));
  1560. }
  1561. {
  1562. int64_t i = INT64_MIN;
  1563. CPPUNIT_ASSERT_EQUAL(std::string("-9,223,372,036,854,775,808"),
  1564. util::itos(i, true));
  1565. }
  1566. }
  1567. void UtilTest::testUitos()
  1568. {
  1569. {
  1570. uint16_t i = 12345;
  1571. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::uitos(i));
  1572. }
  1573. {
  1574. int16_t i = -12345;
  1575. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), util::uitos(i));
  1576. }
  1577. }
  1578. void UtilTest::testNtoh64()
  1579. {
  1580. uint64_t x = 0xff00ff00ee00ee00LL;
  1581. #ifdef WORDS_BIGENDIAN
  1582. CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
  1583. CPPUNIT_ASSERT_EQUAL(x, hton64(x));
  1584. #else // !WORDS_BIGENDIAN
  1585. uint64_t y = 0x00ee00ee00ff00ffLL;
  1586. CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
  1587. CPPUNIT_ASSERT_EQUAL(x, hton64(y));
  1588. #endif // !WORDS_BIGENDIAN
  1589. }
  1590. void UtilTest::testPercentEncode()
  1591. {
  1592. CPPUNIT_ASSERT_EQUAL
  1593. (std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
  1594. util::percentEncode(":/?#[]@!%&'()*+,;="));
  1595. std::string unreserved =
  1596. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1597. "abcdefghijklmnopqrstuvwxyz"
  1598. "0123456789"
  1599. "-._~";
  1600. CPPUNIT_ASSERT_EQUAL(unreserved, util::percentEncode(unreserved));
  1601. CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::percentEncode("1^A "));
  1602. }
  1603. void UtilTest::testHtmlEscape()
  1604. {
  1605. CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),
  1606. util::htmlEscape("aria2<>\"'util"));
  1607. }
  1608. void UtilTest::testJoinPath()
  1609. {
  1610. const std::string dir1dir2file[] = { "dir1", "dir2", "file" };
  1611. CPPUNIT_ASSERT_EQUAL
  1612. (std::string("dir1/dir2/file"),
  1613. util::joinPath(std::begin(dir1dir2file), std::end(dir1dir2file)));
  1614. const std::string dirparentfile[] = { "dir", "..", "file" };
  1615. CPPUNIT_ASSERT_EQUAL
  1616. (std::string("file"),
  1617. util::joinPath(std::begin(dirparentfile), std::end(dirparentfile)));
  1618. const std::string dirparentparentfile[] = { "dir", "..", "..", "file" };
  1619. CPPUNIT_ASSERT_EQUAL
  1620. (std::string("file"),
  1621. util::joinPath(std::begin(dirparentparentfile),
  1622. std::end(dirparentparentfile)));
  1623. const std::string dirdotfile[] = { "dir", ".", "file" };
  1624. CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
  1625. util::joinPath(std::begin(dirdotfile),
  1626. std::end(dirdotfile)));
  1627. const std::string empty[] = {};
  1628. CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(&empty[0], &empty[0]));
  1629. const std::string parentdot[] = { "..", "." };
  1630. CPPUNIT_ASSERT_EQUAL(std::string(""),
  1631. util::joinPath(std::begin(parentdot),
  1632. std::end(parentdot)));
  1633. }
  1634. void UtilTest::testParseIndexPath()
  1635. {
  1636. std::pair<size_t, std::string> p = util::parseIndexPath("1=foo");
  1637. CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
  1638. CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
  1639. try {
  1640. util::parseIndexPath("1X=foo");
  1641. CPPUNIT_FAIL("exception must be thrown.");
  1642. } catch(Exception& e) {
  1643. // success
  1644. }
  1645. try {
  1646. util::parseIndexPath("1=");
  1647. CPPUNIT_FAIL("exception must be thrown.");
  1648. } catch(Exception& e) {
  1649. // success
  1650. }
  1651. }
  1652. void UtilTest::testCreateIndexPaths()
  1653. {
  1654. std::stringstream in
  1655. ("1=/tmp/myfile\n"
  1656. "100=/myhome/mypicture.png\n");
  1657. std::vector<std::pair<size_t, std::string> > m = util::createIndexPaths(in);
  1658. CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
  1659. CPPUNIT_ASSERT_EQUAL((size_t)1, m[0].first);
  1660. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[0].second);
  1661. CPPUNIT_ASSERT_EQUAL((size_t)100, m[1].first);
  1662. CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[1].second);
  1663. }
  1664. void UtilTest::testGenerateRandomData()
  1665. {
  1666. unsigned char data1[20];
  1667. util::generateRandomData(data1, sizeof(data1));
  1668. unsigned char data2[20];
  1669. util::generateRandomData(data2, sizeof(data2));
  1670. CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
  1671. }
  1672. void UtilTest::testFromHex()
  1673. {
  1674. std::string src;
  1675. std::string dest;
  1676. src = "0011fF";
  1677. dest = util::fromHex(src.begin(), src.end());
  1678. CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
  1679. CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
  1680. CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
  1681. CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
  1682. src = "0011f";
  1683. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  1684. src = "001g";
  1685. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  1686. }
  1687. void UtilTest::testParsePrioritizePieceRange()
  1688. {
  1689. // piece index
  1690. // 0 1 2 3 4 5 6 7
  1691. // | | | |
  1692. // file1 | | |
  1693. // | | |
  1694. // file2 | |
  1695. // file3 |
  1696. // | |
  1697. // file4 |
  1698. size_t pieceLength = 1024;
  1699. std::vector<std::shared_ptr<FileEntry> > entries(4, std::shared_ptr<FileEntry>());
  1700. entries[0].reset(new FileEntry("file1", 1024, 0));
  1701. entries[1].reset(new FileEntry("file2",2560,entries[0]->getLastOffset()));
  1702. entries[2].reset(new FileEntry("file3",0,entries[1]->getLastOffset()));
  1703. entries[3].reset(new FileEntry("file4",3584,entries[2]->getLastOffset()));
  1704. std::vector<size_t> result;
  1705. util::parsePrioritizePieceRange(result, "head=1", entries, pieceLength);
  1706. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1707. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1708. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1709. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1710. result.clear();
  1711. util::parsePrioritizePieceRange(result, "tail=1", entries, pieceLength);
  1712. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  1713. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1714. CPPUNIT_ASSERT_EQUAL((size_t)3, result[1]);
  1715. CPPUNIT_ASSERT_EQUAL((size_t)6, result[2]);
  1716. result.clear();
  1717. util::parsePrioritizePieceRange(result, "head=1K", entries, pieceLength);
  1718. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1719. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1720. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1721. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1722. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1723. result.clear();
  1724. util::parsePrioritizePieceRange(result, "head", entries, pieceLength, 1024);
  1725. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1726. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1727. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1728. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1729. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  1730. result.clear();
  1731. util::parsePrioritizePieceRange(result, "tail=1K", entries, pieceLength);
  1732. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1733. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1734. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1735. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1736. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1737. result.clear();
  1738. util::parsePrioritizePieceRange(result, "tail", entries, pieceLength, 1024);
  1739. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1740. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1741. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  1742. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1743. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1744. result.clear();
  1745. util::parsePrioritizePieceRange(result, "head=1,tail=1", entries, pieceLength);
  1746. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  1747. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  1748. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  1749. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  1750. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  1751. result.clear();
  1752. util::parsePrioritizePieceRange(result, "head=300M,tail=300M",
  1753. entries, pieceLength);
  1754. CPPUNIT_ASSERT_EQUAL((size_t)7, result.size());
  1755. for(size_t i = 0; i < 7; ++i) {
  1756. CPPUNIT_ASSERT_EQUAL(i, result[i]);
  1757. }
  1758. result.clear();
  1759. util::parsePrioritizePieceRange(result, "", entries, pieceLength);
  1760. CPPUNIT_ASSERT(result.empty());
  1761. }
  1762. void UtilTest::testApplyDir()
  1763. {
  1764. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir("", "pred"));
  1765. CPPUNIT_ASSERT_EQUAL(std::string("/pred"), util::applyDir("/", "pred"));
  1766. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir(".", "pred"));
  1767. CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred"));
  1768. }
  1769. void UtilTest::testFixTaintedBasename()
  1770. {
  1771. CPPUNIT_ASSERT_EQUAL(std::string("a%2Fb"), util::fixTaintedBasename("a/b"));
  1772. #ifdef __MINGW32__
  1773. CPPUNIT_ASSERT_EQUAL(std::string("a%5Cb"), util::fixTaintedBasename("a\\b"));
  1774. #else // !__MINGW32__
  1775. CPPUNIT_ASSERT_EQUAL(std::string("a\\b"), util::fixTaintedBasename("a\\b"));
  1776. #endif // !__MINGW32__
  1777. }
  1778. void UtilTest::testIsNumericHost()
  1779. {
  1780. CPPUNIT_ASSERT(util::isNumericHost("192.168.0.1"));
  1781. CPPUNIT_ASSERT(!util::isNumericHost("aria2.sf.net"));
  1782. CPPUNIT_ASSERT(util::isNumericHost("::1"));
  1783. }
  1784. void UtilTest::testDetectDirTraversal()
  1785. {
  1786. CPPUNIT_ASSERT(util::detectDirTraversal("/foo"));
  1787. CPPUNIT_ASSERT(util::detectDirTraversal("./foo"));
  1788. CPPUNIT_ASSERT(util::detectDirTraversal("../foo"));
  1789. CPPUNIT_ASSERT(util::detectDirTraversal("foo/../bar"));
  1790. CPPUNIT_ASSERT(util::detectDirTraversal("foo/./bar"));
  1791. CPPUNIT_ASSERT(util::detectDirTraversal("foo/."));
  1792. CPPUNIT_ASSERT(util::detectDirTraversal("foo/.."));
  1793. CPPUNIT_ASSERT(util::detectDirTraversal("."));
  1794. CPPUNIT_ASSERT(util::detectDirTraversal(".."));
  1795. CPPUNIT_ASSERT(util::detectDirTraversal("/"));
  1796. CPPUNIT_ASSERT(util::detectDirTraversal("foo/"));
  1797. CPPUNIT_ASSERT(util::detectDirTraversal("\t"));
  1798. CPPUNIT_ASSERT(!util::detectDirTraversal("foo/bar"));
  1799. CPPUNIT_ASSERT(!util::detectDirTraversal("foo"));
  1800. }
  1801. void UtilTest::testEscapePath()
  1802. {
  1803. CPPUNIT_ASSERT_EQUAL(std::string("foo%00bar%00%01"),
  1804. util::escapePath(std::string("foo")+(char)0x00+
  1805. std::string("bar")+(char)0x00+
  1806. (char)0x01));
  1807. #ifdef __MINGW32__
  1808. CPPUNIT_ASSERT_EQUAL(std::string("foo%5Cbar"), util::escapePath("foo\\bar"));
  1809. #else // !__MINGW32__
  1810. CPPUNIT_ASSERT_EQUAL(std::string("foo\\bar"), util::escapePath("foo\\bar"));
  1811. #endif // !__MINGW32__
  1812. }
  1813. void UtilTest::testInSameCidrBlock()
  1814. {
  1815. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 16));
  1816. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 17));
  1817. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "192.168.0.1", 32));
  1818. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.0.1", "192.168.0.0", 32));
  1819. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "10.0.0.1", 0));
  1820. CPPUNIT_ASSERT(util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 28));
  1821. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 29));
  1822. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "192.168.0.1", 8));
  1823. }
  1824. void UtilTest::testIsUtf8String()
  1825. {
  1826. CPPUNIT_ASSERT(util::isUtf8("ascii"));
  1827. // "Hello World" in Japanese UTF-8
  1828. CPPUNIT_ASSERT(util::isUtf8
  1829. (fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
  1830. // "World" in Shift_JIS
  1831. CPPUNIT_ASSERT(!util::isUtf8(fromHex("90a28a")+"E"));
  1832. // UTF8-2
  1833. CPPUNIT_ASSERT(util::isUtf8(fromHex("c280")));
  1834. CPPUNIT_ASSERT(util::isUtf8(fromHex("dfbf")));
  1835. // UTF8-3
  1836. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0a080")));
  1837. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0bf80")));
  1838. CPPUNIT_ASSERT(util::isUtf8(fromHex("e18080")));
  1839. CPPUNIT_ASSERT(util::isUtf8(fromHex("ec8080")));
  1840. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed8080")));
  1841. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed9f80")));
  1842. CPPUNIT_ASSERT(util::isUtf8(fromHex("ee8080")));
  1843. CPPUNIT_ASSERT(util::isUtf8(fromHex("ef8080")));
  1844. // UTF8-4
  1845. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0908080")));
  1846. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0bf8080")));
  1847. CPPUNIT_ASSERT(util::isUtf8(fromHex("f1808080")));
  1848. CPPUNIT_ASSERT(util::isUtf8(fromHex("f3808080")));
  1849. CPPUNIT_ASSERT(util::isUtf8(fromHex("f4808080")));
  1850. CPPUNIT_ASSERT(util::isUtf8(fromHex("f48f8080")));
  1851. CPPUNIT_ASSERT(util::isUtf8(""));
  1852. CPPUNIT_ASSERT(!util::isUtf8(fromHex("00")));
  1853. }
  1854. void UtilTest::testNextParam()
  1855. {
  1856. std::string s1 = " :a : b=c :d=b::::g::";
  1857. std::pair<std::string::iterator, bool> r;
  1858. std::string name, value;
  1859. r = util::nextParam(name, value, s1.begin(), s1.end(), ':');
  1860. CPPUNIT_ASSERT(r.second);
  1861. CPPUNIT_ASSERT_EQUAL(std::string("a"), name);
  1862. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1863. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1864. CPPUNIT_ASSERT(r.second);
  1865. CPPUNIT_ASSERT_EQUAL(std::string("b"), name);
  1866. CPPUNIT_ASSERT_EQUAL(std::string("c"), value);
  1867. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1868. CPPUNIT_ASSERT(r.second);
  1869. CPPUNIT_ASSERT_EQUAL(std::string("d"), name);
  1870. CPPUNIT_ASSERT_EQUAL(std::string("b"), value);
  1871. r = util::nextParam(name, value, r.first, s1.end(), ':');
  1872. CPPUNIT_ASSERT(r.second);
  1873. CPPUNIT_ASSERT_EQUAL(std::string("g"), name);
  1874. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  1875. std::string s2 = "";
  1876. r = util::nextParam(name, value, s2.begin(), s2.end(), ':');
  1877. CPPUNIT_ASSERT(!r.second);
  1878. std::string s3 = " ";
  1879. r = util::nextParam(name, value, s3.begin(), s3.end(), ':');
  1880. CPPUNIT_ASSERT(!r.second);
  1881. std::string s4 = ":::";
  1882. r = util::nextParam(name, value, s4.begin(), s4.end(), ':');
  1883. CPPUNIT_ASSERT(!r.second);
  1884. }
  1885. void UtilTest::testNoProxyDomainMatch()
  1886. {
  1887. CPPUNIT_ASSERT(util::noProxyDomainMatch("localhost", "localhost"));
  1888. CPPUNIT_ASSERT(util::noProxyDomainMatch("192.168.0.1", "192.168.0.1"));
  1889. CPPUNIT_ASSERT(util::noProxyDomainMatch("www.example.org", ".example.org"));
  1890. CPPUNIT_ASSERT(!util::noProxyDomainMatch("www.example.org", "example.org"));
  1891. CPPUNIT_ASSERT(!util::noProxyDomainMatch("192.168.0.1", "0.1"));
  1892. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "example.com"));
  1893. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org"));
  1894. }
  1895. void UtilTest::testInPrivateAddress()
  1896. {
  1897. CPPUNIT_ASSERT(!util::inPrivateAddress("localhost"));
  1898. CPPUNIT_ASSERT(util::inPrivateAddress("192.168.0.1"));
  1899. // Only checks prefix..
  1900. CPPUNIT_ASSERT(util::inPrivateAddress("10."));
  1901. CPPUNIT_ASSERT(!util::inPrivateAddress("172."));
  1902. CPPUNIT_ASSERT(!util::inPrivateAddress("172.15.0.0"));
  1903. CPPUNIT_ASSERT(util::inPrivateAddress("172.16.0.0"));
  1904. CPPUNIT_ASSERT(util::inPrivateAddress("172.31.0.0"));
  1905. CPPUNIT_ASSERT(!util::inPrivateAddress("172.32.0.0"));
  1906. }
  1907. void UtilTest::testSecfmt()
  1908. {
  1909. CPPUNIT_ASSERT_EQUAL(std::string("0s"), util::secfmt(0));
  1910. CPPUNIT_ASSERT_EQUAL(std::string("1s"), util::secfmt(1));
  1911. CPPUNIT_ASSERT_EQUAL(std::string("9s"), util::secfmt(9));
  1912. CPPUNIT_ASSERT_EQUAL(std::string("10s"), util::secfmt(10));
  1913. CPPUNIT_ASSERT_EQUAL(std::string("1m"), util::secfmt(60));
  1914. CPPUNIT_ASSERT_EQUAL(std::string("1m59s"), util::secfmt(119));
  1915. CPPUNIT_ASSERT_EQUAL(std::string("2m"), util::secfmt(120));
  1916. CPPUNIT_ASSERT_EQUAL(std::string("59m59s"), util::secfmt(3599));
  1917. CPPUNIT_ASSERT_EQUAL(std::string("1h"), util::secfmt(3600));
  1918. }
  1919. void UtilTest::testTlsHostnameMatch()
  1920. {
  1921. CPPUNIT_ASSERT(util::tlsHostnameMatch("Foo.com", "foo.com"));
  1922. CPPUNIT_ASSERT(util::tlsHostnameMatch("*.a.com", "foo.a.com"));
  1923. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*.a.com", "bar.foo.a.com"));
  1924. CPPUNIT_ASSERT(!util::tlsHostnameMatch("f*.com", "foo.com"));
  1925. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*.com", "bar.com"));
  1926. CPPUNIT_ASSERT(util::tlsHostnameMatch("com", "com"));
  1927. CPPUNIT_ASSERT(!util::tlsHostnameMatch("foo.*", "foo.com"));
  1928. CPPUNIT_ASSERT(util::tlsHostnameMatch("a.foo.com", "A.foo.com"));
  1929. CPPUNIT_ASSERT(!util::tlsHostnameMatch("a.foo.com", "b.foo.com"));
  1930. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*a.foo.com", "a.foo.com"));
  1931. CPPUNIT_ASSERT(util::tlsHostnameMatch("*a.foo.com", "ba.foo.com"));
  1932. CPPUNIT_ASSERT(!util::tlsHostnameMatch("a*.foo.com", "a.foo.com"));
  1933. CPPUNIT_ASSERT(util::tlsHostnameMatch("a*.foo.com", "ab.foo.com"));
  1934. CPPUNIT_ASSERT(!util::tlsHostnameMatch("foo.b*z.foo.com", "foo.baz.foo.com"));
  1935. CPPUNIT_ASSERT(util::tlsHostnameMatch("B*z.foo.com", "bAZ.Foo.com"));
  1936. CPPUNIT_ASSERT(!util::tlsHostnameMatch("b*z.foo.com", "bz.foo.com"));
  1937. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*", "foo"));
  1938. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*", ""));
  1939. CPPUNIT_ASSERT(util::tlsHostnameMatch("", ""));
  1940. CPPUNIT_ASSERT(!util::tlsHostnameMatch("xn--*.a.b", "xn--c.a.b"));
  1941. }
  1942. } // namespace aria2