BittorrentHelperTest.cc 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. #include "bittorrent_helper.h"
  2. #include <cstring>
  3. #include <iostream>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. #include "DownloadContext.h"
  6. #include "util.h"
  7. #include "RecoverableException.h"
  8. #include "AnnounceTier.h"
  9. #include "FixedNumberRandomizer.h"
  10. #include "FileEntry.h"
  11. #include "array_fun.h"
  12. #include "a2netcompat.h"
  13. #include "bencode2.h"
  14. #include "TestUtil.h"
  15. #include "base32.h"
  16. #include "Option.h"
  17. #include "prefs.h"
  18. namespace aria2 {
  19. namespace bittorrent {
  20. class BittorrentHelperTest : public CppUnit::TestFixture {
  21. CPPUNIT_TEST_SUITE(BittorrentHelperTest);
  22. CPPUNIT_TEST(testGetInfoHash);
  23. CPPUNIT_TEST(testGetPieceHash);
  24. CPPUNIT_TEST(testGetFileEntries);
  25. CPPUNIT_TEST(testGetTotalLength);
  26. CPPUNIT_TEST(testGetFileEntriesSingle);
  27. CPPUNIT_TEST(testGetTotalLengthSingle);
  28. CPPUNIT_TEST(testGetFileModeMulti);
  29. CPPUNIT_TEST(testGetFileModeSingle);
  30. CPPUNIT_TEST(testGetNameMulti);
  31. CPPUNIT_TEST(testGetNameSingle);
  32. CPPUNIT_TEST(testOverrideName);
  33. CPPUNIT_TEST(testGetAnnounceTier);
  34. CPPUNIT_TEST(testGetAnnounceTierAnnounceList);
  35. CPPUNIT_TEST(testGetPieceLength);
  36. CPPUNIT_TEST(testGetInfoHashAsString);
  37. CPPUNIT_TEST(testGetPeerId);
  38. CPPUNIT_TEST(testGetPeerAgent);
  39. CPPUNIT_TEST(testComputeFastSet);
  40. CPPUNIT_TEST(testGetFileEntries_multiFileUrlList);
  41. CPPUNIT_TEST(testGetFileEntries_singleFileUrlList);
  42. CPPUNIT_TEST(testGetFileEntries_singleFileUrlListEndsWithSlash);
  43. CPPUNIT_TEST(testLoadFromMemory);
  44. CPPUNIT_TEST(testLoadFromMemory_somethingMissing);
  45. CPPUNIT_TEST(testLoadFromMemory_overrideName);
  46. CPPUNIT_TEST(testLoadFromMemory_multiFileDirTraversal);
  47. CPPUNIT_TEST(testLoadFromMemory_singleFileDirTraversal);
  48. CPPUNIT_TEST(testLoadFromMemory_multiFileNonUtf8Path);
  49. CPPUNIT_TEST(testLoadFromMemory_singleFileNonUtf8Path);
  50. CPPUNIT_TEST(testGetNodes);
  51. CPPUNIT_TEST(testGetBasePath);
  52. CPPUNIT_TEST(testSetFileFilter_single);
  53. CPPUNIT_TEST(testSetFileFilter_multi);
  54. CPPUNIT_TEST(testUTF8Torrent);
  55. CPPUNIT_TEST(testEtc);
  56. CPPUNIT_TEST(testCheckBitfield);
  57. CPPUNIT_TEST(testMetadata);
  58. CPPUNIT_TEST(testParseMagnet);
  59. CPPUNIT_TEST(testParseMagnet_base32);
  60. CPPUNIT_TEST(testMetadata2Torrent);
  61. CPPUNIT_TEST(testTorrent2Magnet);
  62. CPPUNIT_TEST(testExtractPeerFromString);
  63. CPPUNIT_TEST(testExtractPeerFromList);
  64. CPPUNIT_TEST(testExtract2PeersFromList);
  65. CPPUNIT_TEST(testPackcompact);
  66. CPPUNIT_TEST(testUnpackcompact);
  67. CPPUNIT_TEST(testRemoveAnnounceUri);
  68. CPPUNIT_TEST(testAddAnnounceUri);
  69. CPPUNIT_TEST(testAdjustAnnounceUri);
  70. CPPUNIT_TEST_SUITE_END();
  71. public:
  72. std::shared_ptr<Option> option_;
  73. void setUp()
  74. {
  75. option_.reset(new Option());
  76. option_->put(PREF_DIR, ".");
  77. }
  78. void testGetInfoHash();
  79. void testGetPieceHash();
  80. void testGetFileEntries();
  81. void testGetTotalLength();
  82. void testGetFileEntriesSingle();
  83. void testGetTotalLengthSingle();
  84. void testGetFileModeMulti();
  85. void testGetFileModeSingle();
  86. void testGetNameMulti();
  87. void testGetNameSingle();
  88. void testOverrideName();
  89. void testGetAnnounceTier();
  90. void testGetAnnounceTierAnnounceList();
  91. void testGetPieceLength();
  92. void testGetInfoHashAsString();
  93. void testGetPeerId();
  94. void testGetPeerAgent();
  95. void testComputeFastSet();
  96. void testGetFileEntries_multiFileUrlList();
  97. void testGetFileEntries_singleFileUrlList();
  98. void testGetFileEntries_singleFileUrlListEndsWithSlash();
  99. void testLoadFromMemory();
  100. void testLoadFromMemory_somethingMissing();
  101. void testLoadFromMemory_overrideName();
  102. void testLoadFromMemory_multiFileDirTraversal();
  103. void testLoadFromMemory_singleFileDirTraversal();
  104. void testLoadFromMemory_multiFileNonUtf8Path();
  105. void testLoadFromMemory_singleFileNonUtf8Path();
  106. void testGetNodes();
  107. void testGetBasePath();
  108. void testSetFileFilter_single();
  109. void testSetFileFilter_multi();
  110. void testUTF8Torrent();
  111. void testEtc();
  112. void testCheckBitfield();
  113. void testMetadata();
  114. void testParseMagnet();
  115. void testParseMagnet_base32();
  116. void testMetadata2Torrent();
  117. void testTorrent2Magnet();
  118. void testExtractPeerFromString();
  119. void testExtractPeerFromList();
  120. void testExtract2PeersFromList();
  121. void testPackcompact();
  122. void testUnpackcompact();
  123. void testRemoveAnnounceUri();
  124. void testAddAnnounceUri();
  125. void testAdjustAnnounceUri();
  126. };
  127. CPPUNIT_TEST_SUITE_REGISTRATION(BittorrentHelperTest);
  128. void BittorrentHelperTest::testGetInfoHash()
  129. {
  130. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  131. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  132. std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  133. CPPUNIT_ASSERT_EQUAL(correctHash, bittorrent::getInfoHashString(dctx));
  134. }
  135. void BittorrentHelperTest::testGetPieceHash()
  136. {
  137. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  138. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  139. CPPUNIT_ASSERT_EQUAL(std::string("AAAAAAAAAAAAAAAAAAAA"),
  140. dctx->getPieceHash(0));
  141. CPPUNIT_ASSERT_EQUAL(std::string("BBBBBBBBBBBBBBBBBBBB"),
  142. dctx->getPieceHash(1));
  143. CPPUNIT_ASSERT_EQUAL(std::string("CCCCCCCCCCCCCCCCCCCC"),
  144. dctx->getPieceHash(2));
  145. CPPUNIT_ASSERT_EQUAL(std::string(""), dctx->getPieceHash(3));
  146. CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashType());
  147. }
  148. void BittorrentHelperTest::testGetFileEntries()
  149. {
  150. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  151. option_->put(PREF_MAX_CONNECTION_PER_SERVER, "10");
  152. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  153. // This is multi-file torrent.
  154. std::vector<std::shared_ptr<FileEntry>> fileEntries = dctx->getFileEntries();
  155. // There are 2 file entries.
  156. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  157. std::vector<std::shared_ptr<FileEntry>>::iterator itr = fileEntries.begin();
  158. std::shared_ptr<FileEntry> fileEntry1 = *itr;
  159. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2/src/aria2c"),
  160. fileEntry1->getPath());
  161. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test/aria2/src/aria2c"),
  162. fileEntry1->getOriginalName());
  163. CPPUNIT_ASSERT_EQUAL(10, fileEntry1->getMaxConnectionPerServer());
  164. itr++;
  165. std::shared_ptr<FileEntry> fileEntry2 = *itr;
  166. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2-0.2.2.tar.bz2"),
  167. fileEntry2->getPath());
  168. CPPUNIT_ASSERT_EQUAL(10, fileEntry2->getMaxConnectionPerServer());
  169. }
  170. void BittorrentHelperTest::testGetFileEntriesSingle()
  171. {
  172. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  173. option_->put(PREF_MAX_CONNECTION_PER_SERVER, "10");
  174. load(A2_TEST_DIR "/single.torrent", dctx, option_);
  175. // This is multi-file torrent.
  176. std::vector<std::shared_ptr<FileEntry>> fileEntries = dctx->getFileEntries();
  177. // There is 1 file entry.
  178. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  179. std::vector<std::shared_ptr<FileEntry>>::iterator itr = fileEntries.begin();
  180. std::shared_ptr<FileEntry> fileEntry1 = *itr;
  181. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"),
  182. fileEntry1->getPath());
  183. CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"),
  184. fileEntry1->getOriginalName());
  185. CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"),
  186. fileEntry1->getSuffixPath());
  187. CPPUNIT_ASSERT_EQUAL(10, fileEntry1->getMaxConnectionPerServer());
  188. }
  189. void BittorrentHelperTest::testGetTotalLength()
  190. {
  191. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  192. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  193. CPPUNIT_ASSERT_EQUAL((int64_t)384LL, dctx->getTotalLength());
  194. }
  195. void BittorrentHelperTest::testGetTotalLengthSingle()
  196. {
  197. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  198. load(A2_TEST_DIR "/single.torrent", dctx, option_);
  199. CPPUNIT_ASSERT_EQUAL((int64_t)384LL, dctx->getTotalLength());
  200. }
  201. void BittorrentHelperTest::testGetFileModeMulti()
  202. {
  203. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  204. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  205. CPPUNIT_ASSERT_EQUAL(BT_FILE_MODE_MULTI, getTorrentAttrs(dctx)->mode);
  206. }
  207. void BittorrentHelperTest::testGetFileModeSingle()
  208. {
  209. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  210. load(A2_TEST_DIR "/single.torrent", dctx, option_);
  211. CPPUNIT_ASSERT_EQUAL(BT_FILE_MODE_SINGLE, getTorrentAttrs(dctx)->mode);
  212. }
  213. void BittorrentHelperTest::testGetNameMulti()
  214. {
  215. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  216. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  217. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"), getTorrentAttrs(dctx)->name);
  218. }
  219. void BittorrentHelperTest::testGetNameSingle()
  220. {
  221. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  222. load(A2_TEST_DIR "/single.torrent", dctx, option_);
  223. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"),
  224. dctx->getBasePath());
  225. CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"),
  226. getTorrentAttrs(dctx)->name);
  227. }
  228. void BittorrentHelperTest::testOverrideName()
  229. {
  230. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  231. load(A2_TEST_DIR "/test.torrent", dctx, option_, "aria2-override.name");
  232. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-override.name"),
  233. dctx->getBasePath());
  234. CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"),
  235. getTorrentAttrs(dctx)->name);
  236. }
  237. void BittorrentHelperTest::testGetAnnounceTier()
  238. {
  239. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  240. load(A2_TEST_DIR "/single.torrent", dctx, option_);
  241. auto attrs = getTorrentAttrs(dctx);
  242. // There is 1 tier.
  243. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList.size());
  244. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[0].size());
  245. CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.com/announce.php"),
  246. attrs->announceList[0][0]);
  247. }
  248. void BittorrentHelperTest::testGetAnnounceTierAnnounceList()
  249. {
  250. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  251. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  252. auto attrs = getTorrentAttrs(dctx);
  253. // There are 3 tiers.
  254. CPPUNIT_ASSERT_EQUAL((size_t)3, attrs->announceList.size());
  255. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[0].size());
  256. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  257. attrs->announceList[0][0]);
  258. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[1].size());
  259. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
  260. attrs->announceList[1][0]);
  261. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[2].size());
  262. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"),
  263. attrs->announceList[2][0]);
  264. }
  265. void BittorrentHelperTest::testGetPieceLength()
  266. {
  267. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  268. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  269. CPPUNIT_ASSERT_EQUAL(128, dctx->getPieceLength());
  270. }
  271. void BittorrentHelperTest::testGetInfoHashAsString()
  272. {
  273. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  274. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  275. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  276. getInfoHashString(dctx));
  277. }
  278. void BittorrentHelperTest::testGetPeerId()
  279. {
  280. std::string peerId = generatePeerId("aria2-");
  281. CPPUNIT_ASSERT(peerId.find("aria2-") == 0);
  282. CPPUNIT_ASSERT_EQUAL((size_t)20, peerId.size());
  283. }
  284. void BittorrentHelperTest::testGetPeerAgent()
  285. {
  286. setStaticPeerAgent("");
  287. std::string peerAgent = generateStaticPeerAgent("aria2/-1.-1.-1");
  288. CPPUNIT_ASSERT_EQUAL(std::string("aria2/-1.-1.-1"), peerAgent);
  289. CPPUNIT_ASSERT_EQUAL(std::string("aria2/-1.-1.-1"),
  290. bittorrent::getStaticPeerAgent());
  291. }
  292. void BittorrentHelperTest::testComputeFastSet()
  293. {
  294. std::string ipaddr = "192.168.0.1";
  295. unsigned char infoHash[20];
  296. memset(infoHash, 0, sizeof(infoHash));
  297. infoHash[0] = 0xff;
  298. int fastSetSize = 10;
  299. size_t numPieces = 1000;
  300. {
  301. auto fastSet = computeFastSet(ipaddr, numPieces, infoHash, fastSetSize);
  302. size_t ans[] = {686, 459, 278, 200, 404, 834, 64, 203, 760, 950};
  303. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), std::begin(ans)));
  304. }
  305. ipaddr = "10.0.0.1";
  306. {
  307. auto fastSet = computeFastSet(ipaddr, numPieces, infoHash, fastSetSize);
  308. size_t ans[] = {568, 188, 466, 452, 550, 662, 109, 226, 398, 11};
  309. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), std::begin(ans)));
  310. }
  311. // See when pieces < fastSetSize
  312. numPieces = 9;
  313. {
  314. auto fastSet = computeFastSet(ipaddr, numPieces, infoHash, fastSetSize);
  315. size_t ans[] = {8, 6, 7, 5, 1, 4, 0, 2, 3};
  316. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), std::begin(ans)));
  317. }
  318. }
  319. void BittorrentHelperTest::testGetFileEntries_multiFileUrlList()
  320. {
  321. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  322. load(A2_TEST_DIR "/url-list-multiFile.torrent", dctx, option_);
  323. // This is multi-file torrent.
  324. const std::vector<std::shared_ptr<FileEntry>>& fileEntries =
  325. dctx->getFileEntries();
  326. // There are 2 file entries.
  327. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  328. std::vector<std::shared_ptr<FileEntry>>::const_iterator itr =
  329. fileEntries.begin();
  330. const std::shared_ptr<FileEntry>& fileEntry1 = *itr;
  331. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test@/aria2@/src@/aria2c@"),
  332. fileEntry1->getPath());
  333. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test@/aria2@/src@/aria2c@"),
  334. fileEntry1->getSuffixPath());
  335. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  336. CPPUNIT_ASSERT_EQUAL((size_t)2, uris1.size());
  337. CPPUNIT_ASSERT_EQUAL(
  338. std::string(
  339. "http://localhost/dist/aria2-test%40/aria2%40/src%40/aria2c%40"),
  340. uris1[0]);
  341. CPPUNIT_ASSERT_EQUAL(
  342. std::string("http://mirror/dist/aria2-test%40/aria2%40/src%40/aria2c%40"),
  343. uris1[1]);
  344. ++itr;
  345. const std::shared_ptr<FileEntry>& fileEntry2 = *itr;
  346. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test@/aria2-0.2.2.tar.bz2"),
  347. fileEntry2->getPath());
  348. const std::deque<std::string>& uris2 = fileEntry2->getRemainingUris();
  349. CPPUNIT_ASSERT_EQUAL((size_t)2, uris2.size());
  350. CPPUNIT_ASSERT_EQUAL(
  351. std::string("http://localhost/dist/aria2-test%40/aria2-0.2.2.tar.bz2"),
  352. uris2[0]);
  353. CPPUNIT_ASSERT_EQUAL(
  354. std::string("http://mirror/dist/aria2-test%40/aria2-0.2.2.tar.bz2"),
  355. uris2[1]);
  356. }
  357. void BittorrentHelperTest::testGetFileEntries_singleFileUrlList()
  358. {
  359. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  360. load(A2_TEST_DIR "/url-list-singleFile.torrent", dctx, option_);
  361. // This is single-file torrent.
  362. const std::vector<std::shared_ptr<FileEntry>>& fileEntries =
  363. dctx->getFileEntries();
  364. // There are 1 file entries.
  365. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  366. const std::shared_ptr<FileEntry>& fileEntry1 = fileEntries.front();
  367. CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"), fileEntry1->getPath());
  368. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  369. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  370. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"),
  371. uris1[0]);
  372. }
  373. void BittorrentHelperTest::testGetFileEntries_singleFileUrlListEndsWithSlash()
  374. {
  375. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  376. load(A2_TEST_DIR "/url-list-singleFileEndsWithSlash.torrent", dctx, option_);
  377. // This is single-file torrent.
  378. const std::vector<std::shared_ptr<FileEntry>>& fileEntries =
  379. dctx->getFileEntries();
  380. // There are 1 file entries.
  381. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  382. const std::shared_ptr<FileEntry>& fileEntry1 = fileEntries.front();
  383. CPPUNIT_ASSERT_EQUAL(std::string("./aria2@.tar.bz2"), fileEntry1->getPath());
  384. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  385. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  386. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2%40.tar.bz2"),
  387. uris1[0]);
  388. }
  389. void BittorrentHelperTest::testLoadFromMemory_multiFileNonUtf8Path()
  390. {
  391. auto path = List::g();
  392. path->append("path");
  393. path->append(fromHex("90a28a") + "E");
  394. auto file = Dict::g();
  395. file->put("length", Integer::g(1_k));
  396. file->put("path", std::move(path));
  397. auto files = List::g();
  398. files->append(std::move(file));
  399. auto info = Dict::g();
  400. info->put("files", std::move(files));
  401. info->put("piece length", Integer::g(1_k));
  402. info->put("pieces", "01234567890123456789");
  403. info->put("name", fromHex("1b") + "$B%O%m!<" + fromHex("1b") + "(B");
  404. Dict dict;
  405. dict.put("info", std::move(info));
  406. auto dctx = std::make_shared<DownloadContext>();
  407. loadFromMemory(bencode2::encode(&dict), dctx, option_, "default");
  408. auto& fe = dctx->getFirstFileEntry();
  409. CPPUNIT_ASSERT_EQUAL(
  410. std::string("./%1B%24B%25O%25m%21%3C%1B%28B/path/%90%A2%8AE"),
  411. fe->getPath());
  412. CPPUNIT_ASSERT_EQUAL(
  413. std::string("%1B%24B%25O%25m%21%3C%1B%28B/path/%90%A2%8AE"),
  414. fe->getSuffixPath());
  415. CPPUNIT_ASSERT_EQUAL(std::string("./%1B%24B%25O%25m%21%3C%1B%28B"),
  416. dctx->getBasePath());
  417. }
  418. void BittorrentHelperTest::testLoadFromMemory_singleFileNonUtf8Path()
  419. {
  420. auto info = Dict::g();
  421. info->put("piece length", Integer::g(1_k));
  422. info->put("pieces", "01234567890123456789");
  423. info->put("name", fromHex("90a28a") + "E");
  424. info->put("length", Integer::g(1_k));
  425. Dict dict;
  426. dict.put("info", std::move(info));
  427. auto dctx = std::make_shared<DownloadContext>();
  428. loadFromMemory(bencode2::encode(&dict), dctx, option_, "default");
  429. const std::shared_ptr<FileEntry>& fe = dctx->getFirstFileEntry();
  430. CPPUNIT_ASSERT_EQUAL(std::string("./%90%A2%8AE"), fe->getPath());
  431. CPPUNIT_ASSERT_EQUAL(std::string("%90%A2%8AE"), fe->getSuffixPath());
  432. }
  433. void BittorrentHelperTest::testLoadFromMemory()
  434. {
  435. std::string memory = "d8:announce36:http://aria.rednoah.com/"
  436. "announce.php13:announce-listll16:http://tracker1 "
  437. "el15:http://tracker2el15:http://"
  438. "tracker3ee7:comment17:REDNOAH.COM RULES13:creation "
  439. "datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:"
  440. "aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2."
  441. "2.tar.bz2eee4:name10:aria2-test12:piece "
  442. "lengthi128e6:pieces60:"
  443. "AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCC"
  444. "CCCCCee";
  445. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  446. loadFromMemory(memory, dctx, option_, "default");
  447. std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  448. CPPUNIT_ASSERT_EQUAL(correctHash, getInfoHashString(dctx));
  449. }
  450. void BittorrentHelperTest::testLoadFromMemory_somethingMissing()
  451. {
  452. // pieces missing
  453. try {
  454. std::string memory = "d8:announce36:http://aria.rednoah.com/"
  455. "announce.php4:infod4:name13:aria2.tar.bz26:"
  456. "lengthi262144eee";
  457. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  458. loadFromMemory(memory, dctx, option_, "default");
  459. CPPUNIT_FAIL("exception must be thrown.");
  460. }
  461. catch (Exception& e) {
  462. // OK
  463. }
  464. }
  465. void BittorrentHelperTest::testLoadFromMemory_overrideName()
  466. {
  467. std::string memory = "d8:announce36:http://aria.rednoah.com/"
  468. "announce.php13:announce-listll16:http://tracker1 "
  469. "el15:http://tracker2el15:http://"
  470. "tracker3ee7:comment17:REDNOAH.COM RULES13:creation "
  471. "datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:"
  472. "aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2."
  473. "2.tar.bz2eee4:name10:aria2-test12:piece "
  474. "lengthi128e6:pieces60:"
  475. "AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCC"
  476. "CCCCCee";
  477. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  478. loadFromMemory(memory, dctx, option_, "default", "aria2-override.name");
  479. CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"),
  480. getTorrentAttrs(dctx)->name);
  481. }
  482. void BittorrentHelperTest::testLoadFromMemory_multiFileDirTraversal()
  483. {
  484. std::string memory = "d8:announce27:http://example.com/"
  485. "announce4:infod5:filesld6:lengthi262144e4:pathl7:../"
  486. "dir14:dir28:file.imgeee4:name14:../name1/name212:piece "
  487. "lengthi262144e6:pieces20:00000000000000000000ee";
  488. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  489. try {
  490. loadFromMemory(memory, dctx, option_, "default");
  491. CPPUNIT_FAIL("Exception must be thrown.");
  492. }
  493. catch (RecoverableException& e) {
  494. // success
  495. }
  496. }
  497. void BittorrentHelperTest::testLoadFromMemory_singleFileDirTraversal()
  498. {
  499. std::string memory = "d8:announce27:http://example.com/"
  500. "announce4:infod4:name14:../name1/"
  501. "name26:lengthi262144e12:piece "
  502. "lengthi262144e6:pieces20:00000000000000000000ee";
  503. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  504. try {
  505. loadFromMemory(memory, dctx, option_, "default");
  506. }
  507. catch (RecoverableException& e) {
  508. // success
  509. }
  510. }
  511. void BittorrentHelperTest::testGetNodes()
  512. {
  513. {
  514. std::string memory = "d5:nodesl"
  515. "l11:192.168.0.1i6881ee"
  516. "l11:192.168.0.2i6882ee"
  517. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  518. "12:piece lengthi262144e"
  519. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  520. "ee";
  521. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  522. loadFromMemory(memory, dctx, option_, "default");
  523. auto attrs = getTorrentAttrs(dctx);
  524. CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->nodes.size());
  525. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), attrs->nodes[0].first);
  526. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, attrs->nodes[0].second);
  527. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[1].first);
  528. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[1].second);
  529. }
  530. {
  531. // empty hostname
  532. std::string memory = "d5:nodesl"
  533. "l1: i6881ee"
  534. "l11:192.168.0.2i6882ee"
  535. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  536. "12:piece lengthi262144e"
  537. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  538. "ee";
  539. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  540. loadFromMemory(memory, dctx, option_, "default");
  541. auto attrs = getTorrentAttrs(dctx);
  542. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  543. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  544. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  545. }
  546. {
  547. // bad port
  548. std::string memory = "d5:nodesl"
  549. "l11:192.168.0.11:xe"
  550. "l11:192.168.0.2i6882ee"
  551. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  552. "12:piece lengthi262144e"
  553. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  554. "ee";
  555. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  556. loadFromMemory(memory, dctx, option_, "default");
  557. auto attrs = getTorrentAttrs(dctx);
  558. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  559. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  560. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  561. }
  562. {
  563. // port missing
  564. std::string memory = "d5:nodesl"
  565. "l11:192.168.0.1e"
  566. "l11:192.168.0.2i6882ee"
  567. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  568. "12:piece lengthi262144e"
  569. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  570. "ee";
  571. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  572. loadFromMemory(memory, dctx, option_, "default");
  573. auto attrs = getTorrentAttrs(dctx);
  574. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  575. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  576. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  577. }
  578. {
  579. // nodes is not a list
  580. std::string memory = "d5:nodes"
  581. "l11:192.168.0.1e"
  582. "4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  583. "12:piece lengthi262144e"
  584. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  585. "ee";
  586. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  587. loadFromMemory(memory, dctx, option_, "default");
  588. auto attrs = getTorrentAttrs(dctx);
  589. CPPUNIT_ASSERT_EQUAL((size_t)0, attrs->nodes.size());
  590. }
  591. {
  592. // the element of node is not Data
  593. std::string memory = "d5:nodesl"
  594. "ll11:192.168.0.1i6881eee"
  595. "l11:192.168.0.2i6882ee"
  596. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  597. "12:piece lengthi262144e"
  598. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  599. "ee";
  600. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  601. loadFromMemory(memory, dctx, option_, "default");
  602. auto attrs = getTorrentAttrs(dctx);
  603. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  604. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  605. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  606. }
  607. }
  608. void BittorrentHelperTest::testGetBasePath()
  609. {
  610. std::shared_ptr<DownloadContext> singleCtx(new DownloadContext());
  611. load(A2_TEST_DIR "/single.torrent", singleCtx, option_);
  612. singleCtx->setFilePathWithIndex(1, "new-path");
  613. CPPUNIT_ASSERT_EQUAL(std::string("new-path"), singleCtx->getBasePath());
  614. option_->put(PREF_DIR, "downloads");
  615. std::shared_ptr<DownloadContext> multiCtx(new DownloadContext());
  616. load(A2_TEST_DIR "/test.torrent", multiCtx, option_);
  617. CPPUNIT_ASSERT_EQUAL(std::string("downloads/aria2-test"),
  618. multiCtx->getBasePath());
  619. }
  620. void BittorrentHelperTest::testSetFileFilter_single()
  621. {
  622. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  623. load(A2_TEST_DIR "/single.torrent", dctx, option_);
  624. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  625. dctx->setFileFilter(SegList<int>());
  626. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  627. dctx->setFileFilter(util::parseIntSegments("1,2"));
  628. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  629. // For single file torrent, file is always selected whatever range
  630. // is passed.
  631. dctx->setFileFilter(util::parseIntSegments("2,3"));
  632. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  633. }
  634. void BittorrentHelperTest::testSetFileFilter_multi()
  635. {
  636. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  637. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  638. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  639. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  640. dctx->setFileFilter(SegList<int>());
  641. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  642. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  643. dctx->setFileFilter(util::parseIntSegments("2,3"));
  644. CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
  645. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  646. dctx->setFileFilter(util::parseIntSegments("3,4"));
  647. CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
  648. CPPUNIT_ASSERT(!dctx->getFileEntries()[1]->isRequested());
  649. dctx->setFileFilter(util::parseIntSegments("1,2"));
  650. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  651. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  652. }
  653. void BittorrentHelperTest::testUTF8Torrent()
  654. {
  655. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  656. load(A2_TEST_DIR "/utf8.torrent", dctx, option_);
  657. CPPUNIT_ASSERT_EQUAL(std::string("name in utf-8"),
  658. getTorrentAttrs(dctx)->name);
  659. CPPUNIT_ASSERT_EQUAL(std::string("./name in utf-8/path in utf-8"),
  660. dctx->getFirstFileEntry()->getPath());
  661. CPPUNIT_ASSERT_EQUAL(std::string("This is utf8 comment."),
  662. getTorrentAttrs(dctx)->comment);
  663. }
  664. void BittorrentHelperTest::testEtc()
  665. {
  666. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  667. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  668. CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
  669. getTorrentAttrs(dctx)->comment);
  670. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), getTorrentAttrs(dctx)->createdBy);
  671. CPPUNIT_ASSERT_EQUAL((time_t)1123456789, getTorrentAttrs(dctx)->creationDate);
  672. }
  673. void BittorrentHelperTest::testCheckBitfield()
  674. {
  675. unsigned char bitfield[] = {0xff, 0xe0};
  676. checkBitfield(bitfield, sizeof(bitfield), 11);
  677. try {
  678. checkBitfield(bitfield, sizeof(bitfield), 17);
  679. CPPUNIT_FAIL("exception must be thrown.");
  680. }
  681. catch (RecoverableException& e) {
  682. // success
  683. }
  684. // Change last byte
  685. bitfield[1] = 0xf0;
  686. try {
  687. checkBitfield(bitfield, sizeof(bitfield), 11);
  688. CPPUNIT_FAIL("exception must be thrown.");
  689. }
  690. catch (RecoverableException& e) {
  691. // success
  692. }
  693. }
  694. void BittorrentHelperTest::testMetadata()
  695. {
  696. auto dctx = std::make_shared<DownloadContext>();
  697. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  698. std::string torrentData = readFile(A2_TEST_DIR "/test.torrent");
  699. auto tr = bencode2::decode(torrentData);
  700. auto infoDic = downcast<Dict>(tr)->get("info");
  701. std::string metadata = bencode2::encode(infoDic);
  702. auto attrs = getTorrentAttrs(dctx);
  703. CPPUNIT_ASSERT(metadata == attrs->metadata);
  704. CPPUNIT_ASSERT_EQUAL(metadata.size(), attrs->metadataSize);
  705. }
  706. void BittorrentHelperTest::testParseMagnet()
  707. {
  708. std::string magnet =
  709. "magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c&dn=aria2"
  710. "&tr=http://tracker1&tr=http://tracker2";
  711. auto attrs = bittorrent::parseMagnet(magnet);
  712. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  713. util::toHex(attrs->infoHash));
  714. CPPUNIT_ASSERT_EQUAL(std::string("[METADATA]aria2"), attrs->name);
  715. CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->announceList.size());
  716. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  717. attrs->announceList[0][0]);
  718. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
  719. attrs->announceList[1][0]);
  720. magnet = "magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c";
  721. attrs = bittorrent::parseMagnet(magnet);
  722. CPPUNIT_ASSERT_EQUAL(
  723. std::string("[METADATA]248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  724. attrs->name);
  725. CPPUNIT_ASSERT(attrs->announceList.empty());
  726. magnet = "magnet:?xt=urn:sha1:7899bdb90a026c746f3cbc10839dd9b2a2a3e985&"
  727. "xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c";
  728. attrs = bittorrent::parseMagnet(magnet);
  729. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  730. util::toHex(attrs->infoHash));
  731. }
  732. void BittorrentHelperTest::testParseMagnet_base32()
  733. {
  734. std::string infoHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  735. std::string base32InfoHash = base32::encode(fromHex(infoHash));
  736. std::string magnet = "magnet:?xt=urn:btih:" + base32InfoHash + "&dn=aria2";
  737. auto attrs = bittorrent::parseMagnet(magnet);
  738. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  739. util::toHex(attrs->infoHash));
  740. }
  741. void BittorrentHelperTest::testMetadata2Torrent()
  742. {
  743. TorrentAttribute attrs;
  744. std::string metadata = "METADATA";
  745. CPPUNIT_ASSERT_EQUAL(std::string("d4:infoMETADATAe"),
  746. metadata2Torrent(metadata, &attrs));
  747. attrs.announceList.push_back(std::vector<std::string>());
  748. attrs.announceList[0].push_back("http://localhost/announce");
  749. CPPUNIT_ASSERT_EQUAL(std::string("d"
  750. "13:announce-list"
  751. "ll25:http://localhost/announceee"
  752. "4:infoMETADATA"
  753. "e"),
  754. metadata2Torrent(metadata, &attrs));
  755. }
  756. void BittorrentHelperTest::testTorrent2Magnet()
  757. {
  758. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  759. load(A2_TEST_DIR "/test.torrent", dctx, option_);
  760. CPPUNIT_ASSERT_EQUAL(
  761. std::string("magnet:?xt=urn:btih:248D0A1CD08284299DE78D5C1ED359BB46717D8C"
  762. "&dn=aria2-test"
  763. "&tr=http%3A%2F%2Ftracker1"
  764. "&tr=http%3A%2F%2Ftracker2"
  765. "&tr=http%3A%2F%2Ftracker3"),
  766. torrent2Magnet(getTorrentAttrs(dctx)));
  767. }
  768. void BittorrentHelperTest::testExtractPeerFromString()
  769. {
  770. std::string hextext = "100210354527354678541237324732171ae1";
  771. hextext += "20010db8bd0501d2288a1fc0000110ee1ae2";
  772. std::string peersstr = "36:" + fromHex(hextext);
  773. auto str = bencode2::decode(peersstr);
  774. std::deque<std::shared_ptr<Peer>> peers;
  775. extractPeer(str.get(), AF_INET6, std::back_inserter(peers));
  776. CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
  777. CPPUNIT_ASSERT_EQUAL(std::string("1002:1035:4527:3546:7854:1237:3247:3217"),
  778. peers[0]->getIPAddress());
  779. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peers[0]->getPort());
  780. CPPUNIT_ASSERT_EQUAL(std::string("2001:db8:bd05:1d2:288a:1fc0:1:10ee"),
  781. peers[1]->getIPAddress());
  782. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, peers[1]->getPort());
  783. hextext = "c0a800011ae1";
  784. hextext += "c0a800021ae2";
  785. peersstr = "12:" + fromHex(hextext);
  786. str = bencode2::decode(peersstr);
  787. peers.clear();
  788. extractPeer(str.get(), AF_INET, std::back_inserter(peers));
  789. CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
  790. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peers[0]->getIPAddress());
  791. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peers[0]->getPort());
  792. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peers[1]->getIPAddress());
  793. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, peers[1]->getPort());
  794. }
  795. void BittorrentHelperTest::testExtractPeerFromList()
  796. {
  797. std::string peersString =
  798. "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
  799. "4:porti2006eeee";
  800. auto dict = bencode2::decode(peersString);
  801. std::deque<std::shared_ptr<Peer>> peers;
  802. extractPeer(downcast<Dict>(dict)->get("peers"), AF_INET,
  803. std::back_inserter(peers));
  804. CPPUNIT_ASSERT_EQUAL((size_t)1, peers.size());
  805. auto& peer = *peers.begin();
  806. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer->getIPAddress());
  807. CPPUNIT_ASSERT_EQUAL((uint16_t)2006, peer->getPort());
  808. }
  809. void BittorrentHelperTest::testExtract2PeersFromList()
  810. {
  811. std::string peersString =
  812. "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
  813. "4:porti65535eed2:ip11:192.168.0.27:peer id20:aria2-00000000000000"
  814. "4:porti2007eeee";
  815. auto dict = bencode2::decode(peersString);
  816. std::deque<std::shared_ptr<Peer>> peers;
  817. extractPeer(downcast<Dict>(dict)->get("peers"), AF_INET,
  818. std::back_inserter(peers));
  819. CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
  820. auto& peer = *peers.begin();
  821. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer->getIPAddress());
  822. CPPUNIT_ASSERT_EQUAL((uint16_t)65535, peer->getPort());
  823. peer = *(peers.begin() + 1);
  824. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peer->getIPAddress());
  825. CPPUNIT_ASSERT_EQUAL((uint16_t)2007, peer->getPort());
  826. }
  827. void BittorrentHelperTest::testPackcompact()
  828. {
  829. unsigned char compact[COMPACT_LEN_IPV6];
  830. CPPUNIT_ASSERT_EQUAL(
  831. (size_t)18,
  832. packcompact(compact, "1002:1035:4527:3546:7854:1237:3247:3217", 6881));
  833. CPPUNIT_ASSERT_EQUAL(std::string("100210354527354678541237324732171ae1"),
  834. util::toHex(compact, 18));
  835. CPPUNIT_ASSERT_EQUAL((size_t)6, packcompact(compact, "192.168.0.1", 6881));
  836. CPPUNIT_ASSERT_EQUAL(std::string("c0a800011ae1"), util::toHex(compact, 6));
  837. CPPUNIT_ASSERT_EQUAL((size_t)0, packcompact(compact, "badaddr", 6881));
  838. }
  839. void BittorrentHelperTest::testUnpackcompact()
  840. {
  841. unsigned char compact6[] = {0x10, 0x02, 0x10, 0x35, 0x45, 0x27,
  842. 0x35, 0x46, 0x78, 0x54, 0x12, 0x37,
  843. 0x32, 0x47, 0x32, 0x17, 0x1A, 0xE1};
  844. std::pair<std::string, uint16_t> p = unpackcompact(compact6, AF_INET6);
  845. CPPUNIT_ASSERT_EQUAL(std::string("1002:1035:4527:3546:7854:1237:3247:3217"),
  846. p.first);
  847. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, p.second);
  848. unsigned char compact[] = {0xC0, 0xa8, 0x00, 0x01, 0x1A, 0xE1};
  849. p = unpackcompact(compact, AF_INET);
  850. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), p.first);
  851. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, p.second);
  852. }
  853. void BittorrentHelperTest::testRemoveAnnounceUri()
  854. {
  855. TorrentAttribute attrs;
  856. std::vector<std::string> tier1;
  857. tier1.push_back("http://host1/announce");
  858. std::vector<std::string> tier2;
  859. tier2.push_back("http://host2/announce");
  860. tier2.push_back("http://host3/announce");
  861. attrs.announceList.push_back(tier1);
  862. attrs.announceList.push_back(tier2);
  863. std::vector<std::string> removeUris;
  864. removeUris.push_back(tier1[0]);
  865. removeUris.push_back(tier2[0]);
  866. removeAnnounceUri(&attrs, removeUris);
  867. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList.size());
  868. CPPUNIT_ASSERT_EQUAL(std::string("http://host3/announce"),
  869. attrs.announceList[0][0]);
  870. removeUris.clear();
  871. removeUris.push_back("*");
  872. removeAnnounceUri(&attrs, removeUris);
  873. CPPUNIT_ASSERT(attrs.announceList.empty());
  874. }
  875. void BittorrentHelperTest::testAddAnnounceUri()
  876. {
  877. TorrentAttribute attrs;
  878. std::vector<std::string> addUris;
  879. addUris.push_back("http://host1/announce");
  880. addUris.push_back("http://host2/announce");
  881. addAnnounceUri(&attrs, addUris);
  882. CPPUNIT_ASSERT_EQUAL((size_t)2, attrs.announceList.size());
  883. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[0].size());
  884. CPPUNIT_ASSERT_EQUAL(std::string("http://host1/announce"),
  885. attrs.announceList[0][0]);
  886. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[1].size());
  887. CPPUNIT_ASSERT_EQUAL(std::string("http://host2/announce"),
  888. attrs.announceList[1][0]);
  889. }
  890. void BittorrentHelperTest::testAdjustAnnounceUri()
  891. {
  892. TorrentAttribute attrs;
  893. std::vector<std::string> tier1;
  894. tier1.push_back("http://host1/announce");
  895. std::vector<std::string> tier2;
  896. tier2.push_back("http://host2/announce");
  897. tier2.push_back("http://host3/announce");
  898. attrs.announceList.push_back(tier1);
  899. attrs.announceList.push_back(tier2);
  900. std::shared_ptr<Option> option(new Option());
  901. option->put(PREF_BT_TRACKER, "http://host1/announce,http://host4/announce");
  902. option->put(PREF_BT_EXCLUDE_TRACKER,
  903. "http://host1/announce,http://host2/announce");
  904. adjustAnnounceUri(&attrs, option);
  905. CPPUNIT_ASSERT_EQUAL((size_t)3, attrs.announceList.size());
  906. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[0].size());
  907. CPPUNIT_ASSERT_EQUAL(std::string("http://host3/announce"),
  908. attrs.announceList[0][0]);
  909. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[1].size());
  910. CPPUNIT_ASSERT_EQUAL(std::string("http://host1/announce"),
  911. attrs.announceList[1][0]);
  912. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[2].size());
  913. CPPUNIT_ASSERT_EQUAL(std::string("http://host4/announce"),
  914. attrs.announceList[2][0]);
  915. }
  916. } // namespace bittorrent
  917. } // namespace aria2