UtilTest2.cc 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. #include "util.h"
  2. #include <cmath>
  3. #include <cstring>
  4. #include <string>
  5. #include <iostream>
  6. #include <cppunit/extensions/HelperMacros.h>
  7. #include "FixedNumberRandomizer.h"
  8. #include "DlAbortEx.h"
  9. #include "BitfieldMan.h"
  10. #include "ByteArrayDiskWriter.h"
  11. #include "FileEntry.h"
  12. #include "File.h"
  13. #include "array_fun.h"
  14. #include "BufferedFile.h"
  15. #include "TestUtil.h"
  16. #include "SocketCore.h"
  17. namespace aria2 {
  18. class UtilTest2 : public CppUnit::TestFixture {
  19. CPPUNIT_TEST_SUITE(UtilTest2);
  20. CPPUNIT_TEST(testToUpper);
  21. CPPUNIT_TEST(testToLower);
  22. CPPUNIT_TEST(testUppercase);
  23. CPPUNIT_TEST(testLowercase);
  24. CPPUNIT_TEST(testPercentDecode);
  25. CPPUNIT_TEST(testGetRealSize);
  26. CPPUNIT_TEST(testAbbrevSize);
  27. CPPUNIT_TEST(testToStream);
  28. CPPUNIT_TEST(testIsNumber);
  29. CPPUNIT_TEST(testIsLowercase);
  30. CPPUNIT_TEST(testIsUppercase);
  31. CPPUNIT_TEST(testMkdirs);
  32. CPPUNIT_TEST(testConvertBitfield);
  33. CPPUNIT_TEST(testParseIntSegments);
  34. CPPUNIT_TEST(testParseIntSegments_invalidRange);
  35. CPPUNIT_TEST(testParseIntNoThrow);
  36. CPPUNIT_TEST(testParseUIntNoThrow);
  37. CPPUNIT_TEST(testParseLLIntNoThrow);
  38. CPPUNIT_TEST(testToString_binaryStream);
  39. CPPUNIT_TEST(testItos);
  40. CPPUNIT_TEST(testUitos);
  41. CPPUNIT_TEST(testNtoh64);
  42. CPPUNIT_TEST(testPercentEncode);
  43. CPPUNIT_TEST(testPercentEncodeMini);
  44. CPPUNIT_TEST(testHtmlEscape);
  45. CPPUNIT_TEST(testJoinPath);
  46. CPPUNIT_TEST(testParseIndexPath);
  47. CPPUNIT_TEST(testCreateIndexPaths);
  48. CPPUNIT_TEST(testGenerateRandomData);
  49. CPPUNIT_TEST(testFromHex);
  50. CPPUNIT_TEST(testParsePrioritizePieceRange);
  51. CPPUNIT_TEST(testApplyDir);
  52. CPPUNIT_TEST(testFixTaintedBasename);
  53. CPPUNIT_TEST(testIsNumericHost);
  54. CPPUNIT_TEST(testDetectDirTraversal);
  55. CPPUNIT_TEST(testEscapePath);
  56. CPPUNIT_TEST(testInSameCidrBlock);
  57. CPPUNIT_TEST(testIsUtf8String);
  58. CPPUNIT_TEST(testNextParam);
  59. CPPUNIT_TEST(testNoProxyDomainMatch);
  60. CPPUNIT_TEST(testInPrivateAddress);
  61. CPPUNIT_TEST(testSecfmt);
  62. CPPUNIT_TEST(testTlsHostnameMatch);
  63. CPPUNIT_TEST(testParseDoubleNoThrow);
  64. CPPUNIT_TEST_SUITE_END();
  65. private:
  66. public:
  67. void setUp() {}
  68. void testToUpper();
  69. void testToLower();
  70. void testUppercase();
  71. void testLowercase();
  72. void testPercentDecode();
  73. void testGetRealSize();
  74. void testAbbrevSize();
  75. void testToStream();
  76. void testIsNumber();
  77. void testIsLowercase();
  78. void testIsUppercase();
  79. void testMkdirs();
  80. void testConvertBitfield();
  81. void testParseIntSegments();
  82. void testParseIntSegments_invalidRange();
  83. void testParseIntNoThrow();
  84. void testParseUIntNoThrow();
  85. void testParseLLIntNoThrow();
  86. void testToString_binaryStream();
  87. void testItos();
  88. void testUitos();
  89. void testNtoh64();
  90. void testPercentEncode();
  91. void testPercentEncodeMini();
  92. void testHtmlEscape();
  93. void testJoinPath();
  94. void testParseIndexPath();
  95. void testCreateIndexPaths();
  96. void testGenerateRandomData();
  97. void testFromHex();
  98. void testParsePrioritizePieceRange();
  99. void testApplyDir();
  100. void testFixTaintedBasename();
  101. void testIsNumericHost();
  102. void testDetectDirTraversal();
  103. void testEscapePath();
  104. void testInSameCidrBlock();
  105. void testIsUtf8String();
  106. void testNextParam();
  107. void testNoProxyDomainMatch();
  108. void testInPrivateAddress();
  109. void testSecfmt();
  110. void testTlsHostnameMatch();
  111. void testParseDoubleNoThrow();
  112. };
  113. CPPUNIT_TEST_SUITE_REGISTRATION(UtilTest2);
  114. class Printer {
  115. public:
  116. template <class T> void operator()(T t) { std::cerr << t << ", "; }
  117. };
  118. void UtilTest2::testToUpper()
  119. {
  120. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  121. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  122. CPPUNIT_ASSERT_EQUAL(upp, util::toUpper(src));
  123. }
  124. void UtilTest2::testToLower()
  125. {
  126. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  127. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  128. CPPUNIT_ASSERT_EQUAL(upp, util::toLower(src));
  129. }
  130. void UtilTest2::testUppercase()
  131. {
  132. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  133. std::string ans = "608CABC0F2FA18C260CAFD974516865C772363D5";
  134. util::uppercase(src);
  135. CPPUNIT_ASSERT_EQUAL(ans, src);
  136. }
  137. void UtilTest2::testLowercase()
  138. {
  139. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  140. std::string ans = "608cabc0f2fa18c260cafd974516865c772363d5";
  141. util::lowercase(src);
  142. CPPUNIT_ASSERT_EQUAL(ans, src);
  143. }
  144. void UtilTest2::testPercentDecode()
  145. {
  146. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  147. CPPUNIT_ASSERT_EQUAL(
  148. std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  149. util::percentDecode(src.begin(), src.end()));
  150. std::string src2 = "aria2+aria2";
  151. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"),
  152. util::percentDecode(src2.begin(), src2.end()));
  153. std::string src3 = "%5t%20";
  154. CPPUNIT_ASSERT_EQUAL(std::string("%5t "),
  155. util::percentDecode(src3.begin(), src3.end()));
  156. std::string src4 = "%";
  157. CPPUNIT_ASSERT_EQUAL(std::string("%"),
  158. util::percentDecode(src4.begin(), src4.end()));
  159. std::string src5 = "%3";
  160. CPPUNIT_ASSERT_EQUAL(std::string("%3"),
  161. util::percentDecode(src5.begin(), src5.end()));
  162. std::string src6 = "%2f";
  163. CPPUNIT_ASSERT_EQUAL(std::string("/"),
  164. util::percentDecode(src6.begin(), src6.end()));
  165. }
  166. void UtilTest2::testGetRealSize()
  167. {
  168. CPPUNIT_ASSERT_EQUAL((int64_t)4_g, util::getRealSize("4096M"));
  169. CPPUNIT_ASSERT_EQUAL((int64_t)1_k, util::getRealSize("1K"));
  170. CPPUNIT_ASSERT_EQUAL((int64_t)4_g, util::getRealSize("4096m"));
  171. CPPUNIT_ASSERT_EQUAL((int64_t)1_k, util::getRealSize("1k"));
  172. try {
  173. util::getRealSize("");
  174. CPPUNIT_FAIL("exception must be thrown.");
  175. }
  176. catch (Exception& e) {
  177. std::cerr << e.stackTrace();
  178. }
  179. try {
  180. util::getRealSize("foo");
  181. CPPUNIT_FAIL("exception must be thrown.");
  182. }
  183. catch (Exception& e) {
  184. std::cerr << e.stackTrace();
  185. }
  186. try {
  187. util::getRealSize("-1");
  188. CPPUNIT_FAIL("exception must be thrown.");
  189. }
  190. catch (Exception& e) {
  191. std::cerr << e.stackTrace();
  192. }
  193. try {
  194. util::getRealSize("9223372036854775807K");
  195. CPPUNIT_FAIL("exception must be thrown.");
  196. }
  197. catch (Exception& e) {
  198. std::cerr << e.stackTrace();
  199. }
  200. try {
  201. util::getRealSize("9223372036854775807M");
  202. CPPUNIT_FAIL("exception must be thrown.");
  203. }
  204. catch (Exception& e) {
  205. std::cerr << e.stackTrace();
  206. }
  207. }
  208. void UtilTest2::testAbbrevSize()
  209. {
  210. CPPUNIT_ASSERT_EQUAL(std::string("8,589,934,591Gi"),
  211. util::abbrevSize(9223372036854775807LL));
  212. CPPUNIT_ASSERT_EQUAL(std::string("4.0Gi"), util::abbrevSize(4_g));
  213. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1_k));
  214. CPPUNIT_ASSERT_EQUAL(std::string("0.9Ki"), util::abbrevSize(1023));
  215. CPPUNIT_ASSERT_EQUAL(std::string("511"), util::abbrevSize(511));
  216. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::abbrevSize(0));
  217. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), util::abbrevSize(1127));
  218. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), util::abbrevSize(1572864));
  219. }
  220. void UtilTest2::testToStream()
  221. {
  222. std::ostringstream os;
  223. std::shared_ptr<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  224. std::shared_ptr<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  225. std::deque<std::shared_ptr<FileEntry>> entries;
  226. entries.push_back(f1);
  227. entries.push_back(f2);
  228. const char* filename = A2_TEST_OUT_DIR "/aria2_UtilTest2_testToStream";
  229. BufferedFile fp(filename, BufferedFile::WRITE);
  230. util::toStream(entries.begin(), entries.end(), fp);
  231. fp.close();
  232. CPPUNIT_ASSERT_EQUAL(std::string("Files:\n"
  233. "idx|path/length\n"
  234. "===+======================================="
  235. "====================================\n"
  236. " 1|aria2.tar.bz2\n"
  237. " |12KiB (12,300)\n"
  238. "---+---------------------------------------"
  239. "------------------------------------\n"
  240. " 2|aria2.txt\n"
  241. " |556B (556)\n"
  242. "---+---------------------------------------"
  243. "------------------------------------\n"),
  244. readFile(filename));
  245. }
  246. void UtilTest2::testIsNumber()
  247. {
  248. std::string s = "000";
  249. CPPUNIT_ASSERT_EQUAL(true, util::isNumber(s.begin(), s.end()));
  250. s = "a";
  251. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  252. s = "0a";
  253. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  254. s = "";
  255. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  256. s = " ";
  257. CPPUNIT_ASSERT_EQUAL(false, util::isNumber(s.begin(), s.end()));
  258. }
  259. void UtilTest2::testIsLowercase()
  260. {
  261. std::string s = "alpha";
  262. CPPUNIT_ASSERT_EQUAL(true, util::isLowercase(s.begin(), s.end()));
  263. s = "Alpha";
  264. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  265. s = "1alpha";
  266. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  267. s = "";
  268. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  269. s = " ";
  270. CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(s.begin(), s.end()));
  271. }
  272. void UtilTest2::testIsUppercase()
  273. {
  274. std::string s = "ALPHA";
  275. CPPUNIT_ASSERT_EQUAL(true, util::isUppercase(s.begin(), s.end()));
  276. s = "Alpha";
  277. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  278. s = "1ALPHA";
  279. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  280. s = "";
  281. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  282. s = " ";
  283. CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(s.begin(), s.end()));
  284. }
  285. void UtilTest2::testMkdirs()
  286. {
  287. std::string dir = A2_TEST_OUT_DIR "/aria2-UtilTest2-testMkdirs";
  288. File d(dir);
  289. if (d.exists()) {
  290. CPPUNIT_ASSERT(d.remove());
  291. }
  292. CPPUNIT_ASSERT(!d.exists());
  293. util::mkdirs(dir);
  294. CPPUNIT_ASSERT(d.isDir());
  295. std::string file = A2_TEST_DIR "/UtilTest2.cc";
  296. File f(file);
  297. CPPUNIT_ASSERT(f.isFile());
  298. try {
  299. util::mkdirs(file);
  300. CPPUNIT_FAIL("exception must be thrown.");
  301. }
  302. catch (DlAbortEx& ex) {
  303. std::cerr << ex.stackTrace() << std::endl;
  304. }
  305. }
  306. void UtilTest2::testConvertBitfield()
  307. {
  308. BitfieldMan srcBitfield(384_k, 256_k * 256 + 1);
  309. BitfieldMan destBitfield(512_k, srcBitfield.getTotalLength());
  310. srcBitfield.setAllBit();
  311. srcBitfield.unsetBit(2); // <- range [768, 1152)
  312. // which corresponds to the index [1,2] in destBitfield
  313. util::convertBitfield(&destBitfield, &srcBitfield);
  314. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  315. util::toHex(destBitfield.getBitfield(),
  316. destBitfield.getBitfieldLength()));
  317. }
  318. void UtilTest2::testParseIntSegments()
  319. {
  320. {
  321. auto sgl = util::parseIntSegments("1,3-8,10");
  322. CPPUNIT_ASSERT(sgl.hasNext());
  323. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  324. CPPUNIT_ASSERT(sgl.hasNext());
  325. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  326. CPPUNIT_ASSERT(sgl.hasNext());
  327. CPPUNIT_ASSERT_EQUAL(4, sgl.next());
  328. CPPUNIT_ASSERT(sgl.hasNext());
  329. CPPUNIT_ASSERT_EQUAL(5, sgl.next());
  330. CPPUNIT_ASSERT(sgl.hasNext());
  331. CPPUNIT_ASSERT_EQUAL(6, sgl.next());
  332. CPPUNIT_ASSERT(sgl.hasNext());
  333. CPPUNIT_ASSERT_EQUAL(7, sgl.next());
  334. CPPUNIT_ASSERT(sgl.hasNext());
  335. CPPUNIT_ASSERT_EQUAL(8, sgl.next());
  336. CPPUNIT_ASSERT(sgl.hasNext());
  337. CPPUNIT_ASSERT_EQUAL(10, sgl.next());
  338. CPPUNIT_ASSERT(!sgl.hasNext());
  339. CPPUNIT_ASSERT_EQUAL(0, sgl.next());
  340. }
  341. {
  342. auto sgl = util::parseIntSegments(",,,1,,,3,,,");
  343. CPPUNIT_ASSERT_EQUAL(1, sgl.next());
  344. CPPUNIT_ASSERT_EQUAL(3, sgl.next());
  345. CPPUNIT_ASSERT(!sgl.hasNext());
  346. }
  347. }
  348. void UtilTest2::testParseIntSegments_invalidRange()
  349. {
  350. try {
  351. auto sgl = util::parseIntSegments("-1");
  352. CPPUNIT_FAIL("exception must be thrown.");
  353. }
  354. catch (Exception& e) {
  355. }
  356. try {
  357. auto sgl = util::parseIntSegments("1-");
  358. CPPUNIT_FAIL("exception must be thrown.");
  359. }
  360. catch (Exception& e) {
  361. }
  362. try {
  363. auto sgl = util::parseIntSegments("2147483648");
  364. CPPUNIT_FAIL("exception must be thrown.");
  365. }
  366. catch (Exception& e) {
  367. }
  368. try {
  369. auto sgl = util::parseIntSegments("2147483647-2147483648");
  370. CPPUNIT_FAIL("exception must be thrown.");
  371. }
  372. catch (Exception& e) {
  373. }
  374. try {
  375. auto sgl = util::parseIntSegments("1-2x");
  376. CPPUNIT_FAIL("exception must be thrown.");
  377. }
  378. catch (Exception& e) {
  379. }
  380. try {
  381. auto sgl = util::parseIntSegments("3x-4");
  382. CPPUNIT_FAIL("exception must be thrown.");
  383. }
  384. catch (Exception& e) {
  385. }
  386. }
  387. void UtilTest2::testParseIntNoThrow()
  388. {
  389. std::string s;
  390. int32_t n;
  391. s = " -1 ";
  392. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s));
  393. CPPUNIT_ASSERT_EQUAL((int32_t)-1, n);
  394. s = "2147483647";
  395. CPPUNIT_ASSERT(util::parseIntNoThrow(n, s));
  396. CPPUNIT_ASSERT_EQUAL((int32_t)2147483647, n);
  397. s = "2147483648";
  398. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  399. s = "-2147483649";
  400. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  401. s = "12x";
  402. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  403. s = "";
  404. CPPUNIT_ASSERT(!util::parseIntNoThrow(n, s));
  405. }
  406. void UtilTest2::testParseUIntNoThrow()
  407. {
  408. std::string s;
  409. uint32_t n;
  410. s = " 2147483647 ";
  411. CPPUNIT_ASSERT(util::parseUIntNoThrow(n, s));
  412. CPPUNIT_ASSERT_EQUAL((uint32_t)INT32_MAX, n);
  413. s = "2147483648";
  414. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s));
  415. s = "-1";
  416. CPPUNIT_ASSERT(!util::parseUIntNoThrow(n, s));
  417. }
  418. void UtilTest2::testParseLLIntNoThrow()
  419. {
  420. std::string s;
  421. int64_t n;
  422. s = " 9223372036854775807 ";
  423. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s));
  424. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MAX, n);
  425. s = "9223372036854775808";
  426. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s));
  427. s = "-9223372036854775808";
  428. CPPUNIT_ASSERT(util::parseLLIntNoThrow(n, s));
  429. CPPUNIT_ASSERT_EQUAL((int64_t)INT64_MIN, n);
  430. s = "-9223372036854775809";
  431. CPPUNIT_ASSERT(!util::parseLLIntNoThrow(n, s));
  432. }
  433. void UtilTest2::testToString_binaryStream()
  434. {
  435. std::shared_ptr<DiskWriter> dw(new ByteArrayDiskWriter());
  436. std::string data(16_k + 256, 'a');
  437. dw->initAndOpenFile();
  438. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  439. std::string readData = util::toString(dw);
  440. CPPUNIT_ASSERT_EQUAL(data, readData);
  441. }
  442. void UtilTest2::testItos()
  443. {
  444. {
  445. int i = 0;
  446. CPPUNIT_ASSERT_EQUAL(std::string("0"), util::itos(i));
  447. }
  448. {
  449. int i = 100;
  450. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i, true));
  451. }
  452. {
  453. int i = 100;
  454. CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i));
  455. }
  456. {
  457. int i = 12345;
  458. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), util::itos(i, true));
  459. }
  460. {
  461. int i = 12345;
  462. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::itos(i));
  463. }
  464. {
  465. int i = -12345;
  466. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), util::itos(i, true));
  467. }
  468. {
  469. int64_t i = INT64_MAX;
  470. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"),
  471. util::itos(i, true));
  472. }
  473. {
  474. int64_t i = INT64_MIN;
  475. CPPUNIT_ASSERT_EQUAL(std::string("-9,223,372,036,854,775,808"),
  476. util::itos(i, true));
  477. }
  478. }
  479. void UtilTest2::testUitos()
  480. {
  481. {
  482. uint16_t i = 12345;
  483. CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::uitos(i));
  484. }
  485. {
  486. int16_t i = -12345;
  487. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), util::uitos(i));
  488. }
  489. }
  490. void UtilTest2::testNtoh64()
  491. {
  492. uint64_t x = 0xff00ff00ee00ee00LL;
  493. #ifdef WORDS_BIGENDIAN
  494. CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
  495. CPPUNIT_ASSERT_EQUAL(x, hton64(x));
  496. #else // !WORDS_BIGENDIAN
  497. uint64_t y = 0x00ee00ee00ff00ffLL;
  498. CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
  499. CPPUNIT_ASSERT_EQUAL(x, hton64(y));
  500. #endif // !WORDS_BIGENDIAN
  501. }
  502. void UtilTest2::testPercentEncode()
  503. {
  504. CPPUNIT_ASSERT_EQUAL(
  505. std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
  506. util::percentEncode(":/?#[]@!%&'()*+,;="));
  507. std::string unreserved = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  508. "abcdefghijklmnopqrstuvwxyz"
  509. "0123456789"
  510. "-._~";
  511. CPPUNIT_ASSERT_EQUAL(unreserved, util::percentEncode(unreserved));
  512. CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::percentEncode("1^A "));
  513. }
  514. void UtilTest2::testPercentEncodeMini()
  515. {
  516. CPPUNIT_ASSERT_EQUAL(std::string("%80"),
  517. util::percentEncodeMini({(char)0x80}));
  518. }
  519. void UtilTest2::testHtmlEscape()
  520. {
  521. CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),
  522. util::htmlEscape("aria2<>\"'util"));
  523. }
  524. void UtilTest2::testJoinPath()
  525. {
  526. const std::string dir1dir2file[] = {"dir1", "dir2", "file"};
  527. CPPUNIT_ASSERT_EQUAL(
  528. std::string("dir1/dir2/file"),
  529. util::joinPath(std::begin(dir1dir2file), std::end(dir1dir2file)));
  530. const std::string dirparentfile[] = {"dir", "..", "file"};
  531. CPPUNIT_ASSERT_EQUAL(
  532. std::string("file"),
  533. util::joinPath(std::begin(dirparentfile), std::end(dirparentfile)));
  534. const std::string dirparentparentfile[] = {"dir", "..", "..", "file"};
  535. CPPUNIT_ASSERT_EQUAL(std::string("file"),
  536. util::joinPath(std::begin(dirparentparentfile),
  537. std::end(dirparentparentfile)));
  538. const std::string dirdotfile[] = {"dir", ".", "file"};
  539. CPPUNIT_ASSERT_EQUAL(
  540. std::string("dir/file"),
  541. util::joinPath(std::begin(dirdotfile), std::end(dirdotfile)));
  542. const std::string empty[] = {};
  543. CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(&empty[0], &empty[0]));
  544. const std::string parentdot[] = {"..", "."};
  545. CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(std::begin(parentdot),
  546. std::end(parentdot)));
  547. }
  548. void UtilTest2::testParseIndexPath()
  549. {
  550. std::pair<size_t, std::string> p = util::parseIndexPath("1=foo");
  551. CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
  552. CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
  553. try {
  554. util::parseIndexPath("1X=foo");
  555. CPPUNIT_FAIL("exception must be thrown.");
  556. }
  557. catch (Exception& e) {
  558. // success
  559. }
  560. try {
  561. util::parseIndexPath("1=");
  562. CPPUNIT_FAIL("exception must be thrown.");
  563. }
  564. catch (Exception& e) {
  565. // success
  566. }
  567. }
  568. void UtilTest2::testCreateIndexPaths()
  569. {
  570. std::stringstream in("1=/tmp/myfile\n"
  571. "100=/myhome/mypicture.png\n");
  572. std::vector<std::pair<size_t, std::string>> m = util::createIndexPaths(in);
  573. CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
  574. CPPUNIT_ASSERT_EQUAL((size_t)1, m[0].first);
  575. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[0].second);
  576. CPPUNIT_ASSERT_EQUAL((size_t)100, m[1].first);
  577. CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[1].second);
  578. }
  579. void UtilTest2::testGenerateRandomData()
  580. {
  581. using namespace std;
  582. // Simple sanity check
  583. unsigned char data1[25];
  584. memset(data1, 0, sizeof(data1));
  585. util::generateRandomData(data1, sizeof(data1));
  586. unsigned char data2[25];
  587. memset(data2, 0, sizeof(data2));
  588. util::generateRandomData(data2, sizeof(data2));
  589. CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
  590. // Simple stddev/mean tests
  591. map<uint8_t, size_t> counts;
  592. uint8_t bytes[1 << 20];
  593. for (auto i = 0; i < 10; ++i) {
  594. util::generateRandomData(bytes, sizeof(bytes));
  595. for (auto b : bytes) {
  596. counts[b]++;
  597. }
  598. }
  599. CPPUNIT_ASSERT_MESSAGE("Should see all kinds of bytes", counts.size() == 256);
  600. double sum =
  601. accumulate(counts.begin(), counts.end(), 0.0,
  602. [](double total, const decltype(counts)::value_type& elem) {
  603. return total + elem.second;
  604. });
  605. double mean = sum / counts.size();
  606. vector<double> diff(counts.size());
  607. transform(counts.begin(), counts.end(), diff.begin(),
  608. [&](const decltype(counts)::value_type& elem) -> double {
  609. return (double)elem.second - mean;
  610. });
  611. double sq_sum = inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
  612. double stddev = sqrt(sq_sum / counts.size());
  613. cout << "stddev: " << fixed << stddev << endl;
  614. CPPUNIT_ASSERT_MESSAGE("stddev makes sense (lower)", stddev <= 320);
  615. CPPUNIT_ASSERT_MESSAGE("stddev makes sense (upper)", stddev >= 100);
  616. }
  617. void UtilTest2::testFromHex()
  618. {
  619. std::string src;
  620. std::string dest;
  621. src = "0011fF";
  622. dest = util::fromHex(src.begin(), src.end());
  623. CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
  624. CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
  625. CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
  626. CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
  627. src = "0011f";
  628. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  629. src = "001g";
  630. CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
  631. }
  632. void UtilTest2::testParsePrioritizePieceRange()
  633. {
  634. // piece index
  635. // 0 1 2 3 4 5 6 7
  636. // | | | |
  637. // file1 | | |
  638. // | | |
  639. // file2 | |
  640. // file3 |
  641. // | |
  642. // file4 |
  643. constexpr size_t pieceLength = 1_k;
  644. std::vector<std::shared_ptr<FileEntry>> entries(4,
  645. std::shared_ptr<FileEntry>());
  646. entries[0].reset(new FileEntry("file1", 1024, 0));
  647. entries[1].reset(new FileEntry("file2", 2560, entries[0]->getLastOffset()));
  648. entries[2].reset(new FileEntry("file3", 0, entries[1]->getLastOffset()));
  649. entries[3].reset(new FileEntry("file4", 3584, entries[2]->getLastOffset()));
  650. std::vector<size_t> result;
  651. util::parsePrioritizePieceRange(result, "head=1", entries, pieceLength);
  652. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  653. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  654. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  655. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  656. result.clear();
  657. util::parsePrioritizePieceRange(result, "tail=1", entries, pieceLength);
  658. CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
  659. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  660. CPPUNIT_ASSERT_EQUAL((size_t)3, result[1]);
  661. CPPUNIT_ASSERT_EQUAL((size_t)6, result[2]);
  662. result.clear();
  663. util::parsePrioritizePieceRange(result, "head=1K", entries, pieceLength);
  664. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  665. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  666. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  667. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  668. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  669. result.clear();
  670. util::parsePrioritizePieceRange(result, "head", entries, pieceLength, 1_k);
  671. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  672. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  673. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  674. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  675. CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
  676. result.clear();
  677. util::parsePrioritizePieceRange(result, "tail=1K", entries, pieceLength);
  678. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  679. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  680. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  681. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  682. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  683. result.clear();
  684. util::parsePrioritizePieceRange(result, "tail", entries, pieceLength, 1_k);
  685. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  686. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  687. CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
  688. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  689. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  690. result.clear();
  691. util::parsePrioritizePieceRange(result, "head=1,tail=1", entries,
  692. pieceLength);
  693. CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
  694. CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
  695. CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
  696. CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
  697. CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
  698. result.clear();
  699. util::parsePrioritizePieceRange(result, "head=300M,tail=300M", entries,
  700. pieceLength);
  701. CPPUNIT_ASSERT_EQUAL((size_t)7, result.size());
  702. for (size_t i = 0; i < 7; ++i) {
  703. CPPUNIT_ASSERT_EQUAL(i, result[i]);
  704. }
  705. result.clear();
  706. util::parsePrioritizePieceRange(result, "", entries, pieceLength);
  707. CPPUNIT_ASSERT(result.empty());
  708. }
  709. void UtilTest2::testApplyDir()
  710. {
  711. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir("", "pred"));
  712. CPPUNIT_ASSERT_EQUAL(std::string("/pred"), util::applyDir("/", "pred"));
  713. CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir(".", "pred"));
  714. CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred"));
  715. }
  716. void UtilTest2::testFixTaintedBasename()
  717. {
  718. CPPUNIT_ASSERT_EQUAL(std::string("a%2Fb"), util::fixTaintedBasename("a/b"));
  719. #ifdef __MINGW32__
  720. CPPUNIT_ASSERT_EQUAL(std::string("a%5Cb"), util::fixTaintedBasename("a\\b"));
  721. #else // !__MINGW32__
  722. CPPUNIT_ASSERT_EQUAL(std::string("a\\b"), util::fixTaintedBasename("a\\b"));
  723. #endif // !__MINGW32__
  724. }
  725. void UtilTest2::testIsNumericHost()
  726. {
  727. CPPUNIT_ASSERT(util::isNumericHost("192.168.0.1"));
  728. CPPUNIT_ASSERT(!util::isNumericHost("aria2.sf.net"));
  729. CPPUNIT_ASSERT(util::isNumericHost("::1"));
  730. }
  731. void UtilTest2::testDetectDirTraversal()
  732. {
  733. CPPUNIT_ASSERT(util::detectDirTraversal("/foo"));
  734. CPPUNIT_ASSERT(util::detectDirTraversal("./foo"));
  735. CPPUNIT_ASSERT(util::detectDirTraversal("../foo"));
  736. CPPUNIT_ASSERT(util::detectDirTraversal("foo/../bar"));
  737. CPPUNIT_ASSERT(util::detectDirTraversal("foo/./bar"));
  738. CPPUNIT_ASSERT(util::detectDirTraversal("foo/."));
  739. CPPUNIT_ASSERT(util::detectDirTraversal("foo/.."));
  740. CPPUNIT_ASSERT(util::detectDirTraversal("."));
  741. CPPUNIT_ASSERT(util::detectDirTraversal(".."));
  742. CPPUNIT_ASSERT(util::detectDirTraversal("/"));
  743. CPPUNIT_ASSERT(util::detectDirTraversal("foo/"));
  744. CPPUNIT_ASSERT(util::detectDirTraversal("\t"));
  745. CPPUNIT_ASSERT(!util::detectDirTraversal("foo/bar"));
  746. CPPUNIT_ASSERT(!util::detectDirTraversal("foo"));
  747. }
  748. void UtilTest2::testEscapePath()
  749. {
  750. CPPUNIT_ASSERT_EQUAL(std::string("foo%00bar%00%01"),
  751. util::escapePath(std::string("foo") + (char)0x00 +
  752. std::string("bar") + (char)0x00 +
  753. (char)0x01));
  754. #ifdef __MINGW32__
  755. CPPUNIT_ASSERT_EQUAL(std::string("foo%5Cbar"), util::escapePath("foo\\bar"));
  756. #else // !__MINGW32__
  757. CPPUNIT_ASSERT_EQUAL(std::string("foo\\bar"), util::escapePath("foo\\bar"));
  758. #endif // !__MINGW32__
  759. }
  760. void UtilTest2::testInSameCidrBlock()
  761. {
  762. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 16));
  763. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.128.1", "192.168.0.1", 17));
  764. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "192.168.0.1", 32));
  765. CPPUNIT_ASSERT(!util::inSameCidrBlock("192.168.0.1", "192.168.0.0", 32));
  766. CPPUNIT_ASSERT(util::inSameCidrBlock("192.168.0.1", "10.0.0.1", 0));
  767. CPPUNIT_ASSERT(util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 28));
  768. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "2001:db0::2:2", 29));
  769. CPPUNIT_ASSERT(!util::inSameCidrBlock("2001:db8::2:1", "192.168.0.1", 8));
  770. }
  771. void UtilTest2::testIsUtf8String()
  772. {
  773. CPPUNIT_ASSERT(util::isUtf8("ascii"));
  774. // "Hello World" in Japanese UTF-8
  775. CPPUNIT_ASSERT(
  776. util::isUtf8(fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
  777. // "World" in Shift_JIS
  778. CPPUNIT_ASSERT(!util::isUtf8(fromHex("90a28a") + "E"));
  779. // UTF8-2
  780. CPPUNIT_ASSERT(util::isUtf8(fromHex("c280")));
  781. CPPUNIT_ASSERT(util::isUtf8(fromHex("dfbf")));
  782. // UTF8-3
  783. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0a080")));
  784. CPPUNIT_ASSERT(util::isUtf8(fromHex("e0bf80")));
  785. CPPUNIT_ASSERT(util::isUtf8(fromHex("e18080")));
  786. CPPUNIT_ASSERT(util::isUtf8(fromHex("ec8080")));
  787. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed8080")));
  788. CPPUNIT_ASSERT(util::isUtf8(fromHex("ed9f80")));
  789. CPPUNIT_ASSERT(util::isUtf8(fromHex("ee8080")));
  790. CPPUNIT_ASSERT(util::isUtf8(fromHex("ef8080")));
  791. // UTF8-4
  792. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0908080")));
  793. CPPUNIT_ASSERT(util::isUtf8(fromHex("f0bf8080")));
  794. CPPUNIT_ASSERT(util::isUtf8(fromHex("f1808080")));
  795. CPPUNIT_ASSERT(util::isUtf8(fromHex("f3808080")));
  796. CPPUNIT_ASSERT(util::isUtf8(fromHex("f4808080")));
  797. CPPUNIT_ASSERT(util::isUtf8(fromHex("f48f8080")));
  798. CPPUNIT_ASSERT(util::isUtf8(""));
  799. CPPUNIT_ASSERT(!util::isUtf8(fromHex("00")));
  800. }
  801. void UtilTest2::testNextParam()
  802. {
  803. std::string s1 = " :a : b=c :d=b::::g::";
  804. std::pair<std::string::iterator, bool> r;
  805. std::string name, value;
  806. r = util::nextParam(name, value, s1.begin(), s1.end(), ':');
  807. CPPUNIT_ASSERT(r.second);
  808. CPPUNIT_ASSERT_EQUAL(std::string("a"), name);
  809. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  810. r = util::nextParam(name, value, r.first, s1.end(), ':');
  811. CPPUNIT_ASSERT(r.second);
  812. CPPUNIT_ASSERT_EQUAL(std::string("b"), name);
  813. CPPUNIT_ASSERT_EQUAL(std::string("c"), value);
  814. r = util::nextParam(name, value, r.first, s1.end(), ':');
  815. CPPUNIT_ASSERT(r.second);
  816. CPPUNIT_ASSERT_EQUAL(std::string("d"), name);
  817. CPPUNIT_ASSERT_EQUAL(std::string("b"), value);
  818. r = util::nextParam(name, value, r.first, s1.end(), ':');
  819. CPPUNIT_ASSERT(r.second);
  820. CPPUNIT_ASSERT_EQUAL(std::string("g"), name);
  821. CPPUNIT_ASSERT_EQUAL(std::string(), value);
  822. std::string s2 = "";
  823. r = util::nextParam(name, value, s2.begin(), s2.end(), ':');
  824. CPPUNIT_ASSERT(!r.second);
  825. std::string s3 = " ";
  826. r = util::nextParam(name, value, s3.begin(), s3.end(), ':');
  827. CPPUNIT_ASSERT(!r.second);
  828. std::string s4 = ":::";
  829. r = util::nextParam(name, value, s4.begin(), s4.end(), ':');
  830. CPPUNIT_ASSERT(!r.second);
  831. }
  832. void UtilTest2::testNoProxyDomainMatch()
  833. {
  834. CPPUNIT_ASSERT(util::noProxyDomainMatch("localhost", "localhost"));
  835. CPPUNIT_ASSERT(util::noProxyDomainMatch("192.168.0.1", "192.168.0.1"));
  836. CPPUNIT_ASSERT(util::noProxyDomainMatch("www.example.org", ".example.org"));
  837. CPPUNIT_ASSERT(!util::noProxyDomainMatch("www.example.org", "example.org"));
  838. CPPUNIT_ASSERT(!util::noProxyDomainMatch("192.168.0.1", "0.1"));
  839. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "example.com"));
  840. CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org"));
  841. }
  842. void UtilTest2::testInPrivateAddress()
  843. {
  844. CPPUNIT_ASSERT(!util::inPrivateAddress("localhost"));
  845. CPPUNIT_ASSERT(util::inPrivateAddress("192.168.0.1"));
  846. // Only checks prefix..
  847. CPPUNIT_ASSERT(util::inPrivateAddress("10."));
  848. CPPUNIT_ASSERT(!util::inPrivateAddress("172."));
  849. CPPUNIT_ASSERT(!util::inPrivateAddress("172.15.0.0"));
  850. CPPUNIT_ASSERT(util::inPrivateAddress("172.16.0.0"));
  851. CPPUNIT_ASSERT(util::inPrivateAddress("172.31.0.0"));
  852. CPPUNIT_ASSERT(!util::inPrivateAddress("172.32.0.0"));
  853. }
  854. void UtilTest2::testSecfmt()
  855. {
  856. CPPUNIT_ASSERT_EQUAL(std::string("0s"), util::secfmt(0));
  857. CPPUNIT_ASSERT_EQUAL(std::string("1s"), util::secfmt(1));
  858. CPPUNIT_ASSERT_EQUAL(std::string("9s"), util::secfmt(9));
  859. CPPUNIT_ASSERT_EQUAL(std::string("10s"), util::secfmt(10));
  860. CPPUNIT_ASSERT_EQUAL(std::string("1m"), util::secfmt(60));
  861. CPPUNIT_ASSERT_EQUAL(std::string("1m59s"), util::secfmt(119));
  862. CPPUNIT_ASSERT_EQUAL(std::string("2m"), util::secfmt(120));
  863. CPPUNIT_ASSERT_EQUAL(std::string("59m59s"), util::secfmt(3599));
  864. CPPUNIT_ASSERT_EQUAL(std::string("1h"), util::secfmt(3600));
  865. }
  866. void UtilTest2::testTlsHostnameMatch()
  867. {
  868. CPPUNIT_ASSERT(util::tlsHostnameMatch("Foo.com", "foo.com"));
  869. CPPUNIT_ASSERT(util::tlsHostnameMatch("*.a.com", "foo.a.com"));
  870. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*.a.com", "bar.foo.a.com"));
  871. CPPUNIT_ASSERT(!util::tlsHostnameMatch("f*.com", "foo.com"));
  872. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*.com", "bar.com"));
  873. CPPUNIT_ASSERT(util::tlsHostnameMatch("com", "com"));
  874. CPPUNIT_ASSERT(!util::tlsHostnameMatch("foo.*", "foo.com"));
  875. CPPUNIT_ASSERT(util::tlsHostnameMatch("a.foo.com", "A.foo.com"));
  876. CPPUNIT_ASSERT(!util::tlsHostnameMatch("a.foo.com", "b.foo.com"));
  877. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*a.foo.com", "a.foo.com"));
  878. CPPUNIT_ASSERT(util::tlsHostnameMatch("*a.foo.com", "ba.foo.com"));
  879. CPPUNIT_ASSERT(!util::tlsHostnameMatch("a*.foo.com", "a.foo.com"));
  880. CPPUNIT_ASSERT(util::tlsHostnameMatch("a*.foo.com", "ab.foo.com"));
  881. CPPUNIT_ASSERT(!util::tlsHostnameMatch("foo.b*z.foo.com", "foo.baz.foo.com"));
  882. CPPUNIT_ASSERT(util::tlsHostnameMatch("B*z.foo.com", "bAZ.Foo.com"));
  883. CPPUNIT_ASSERT(!util::tlsHostnameMatch("b*z.foo.com", "bz.foo.com"));
  884. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*", "foo"));
  885. CPPUNIT_ASSERT(!util::tlsHostnameMatch("*", ""));
  886. CPPUNIT_ASSERT(util::tlsHostnameMatch("", ""));
  887. CPPUNIT_ASSERT(!util::tlsHostnameMatch("xn--*.a.b", "xn--c.a.b"));
  888. }
  889. void UtilTest2::testParseDoubleNoThrow()
  890. {
  891. double n;
  892. CPPUNIT_ASSERT(util::parseDoubleNoThrow(n, " 123 "));
  893. CPPUNIT_ASSERT_EQUAL(123., n);
  894. CPPUNIT_ASSERT(util::parseDoubleNoThrow(n, "3.14"));
  895. CPPUNIT_ASSERT_EQUAL(3.14, n);
  896. CPPUNIT_ASSERT(util::parseDoubleNoThrow(n, "-3.14"));
  897. CPPUNIT_ASSERT_EQUAL(-3.14, n);
  898. CPPUNIT_ASSERT(!util::parseDoubleNoThrow(n, ""));
  899. CPPUNIT_ASSERT(!util::parseDoubleNoThrow(n, "123x"));
  900. }
  901. } // namespace aria2