UtilTest2.cc 32 KB

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