BittorrentHelperTest.cc 37 KB

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