UtilTest.cc 63 KB

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