BittorrentHelperTest.cc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. auto fastSet = computeFastSet(ipaddr, numPieces, infoHash, fastSetSize);
  278. size_t ans[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
  279. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), std::begin(ans)));
  280. }
  281. ipaddr = "10.0.0.1";
  282. {
  283. auto fastSet = computeFastSet(ipaddr, numPieces, infoHash, fastSetSize);
  284. size_t ans[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
  285. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), std::begin(ans)));
  286. }
  287. // See when pieces < fastSetSize
  288. numPieces = 9;
  289. {
  290. auto fastSet = computeFastSet(ipaddr, numPieces, infoHash, fastSetSize);
  291. size_t ans[] = { 8, 6, 7, 5, 1, 4, 0, 2, 3 };
  292. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), std::begin(ans)));
  293. }
  294. }
  295. void BittorrentHelperTest::testGetFileEntries_multiFileUrlList() {
  296. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  297. load(A2_TEST_DIR"/url-list-multiFile.torrent", dctx, option_);
  298. // This is multi-file torrent.
  299. const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
  300. dctx->getFileEntries();
  301. // There are 2 file entries.
  302. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  303. std::vector<std::shared_ptr<FileEntry> >::const_iterator itr =
  304. fileEntries.begin();
  305. const std::shared_ptr<FileEntry>& fileEntry1 = *itr;
  306. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test@/aria2@/src@/aria2c@"),
  307. fileEntry1->getPath());
  308. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test@/aria2@/src@/aria2c@"),
  309. fileEntry1->getSuffixPath());
  310. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  311. CPPUNIT_ASSERT_EQUAL((size_t)2, uris1.size());
  312. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test%40/aria2%40/src%40/aria2c%40"),
  313. uris1[0]);
  314. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test%40/aria2%40/src%40/aria2c%40"),
  315. uris1[1]);
  316. ++itr;
  317. const std::shared_ptr<FileEntry>& fileEntry2 = *itr;
  318. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test@/aria2-0.2.2.tar.bz2"),
  319. fileEntry2->getPath());
  320. const std::deque<std::string>& uris2 = fileEntry2->getRemainingUris();
  321. CPPUNIT_ASSERT_EQUAL((size_t)2, uris2.size());
  322. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test%40/aria2-0.2.2.tar.bz2"),
  323. uris2[0]);
  324. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test%40/aria2-0.2.2.tar.bz2"),
  325. uris2[1]);
  326. }
  327. void BittorrentHelperTest::testGetFileEntries_singleFileUrlList() {
  328. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  329. load(A2_TEST_DIR"/url-list-singleFile.torrent", dctx, option_);
  330. // This is single-file torrent.
  331. const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
  332. dctx->getFileEntries();
  333. // There are 1 file entries.
  334. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  335. const std::shared_ptr<FileEntry>& fileEntry1 = fileEntries.front();
  336. CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"),
  337. fileEntry1->getPath());
  338. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  339. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  340. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"),
  341. uris1[0]);
  342. }
  343. void BittorrentHelperTest::testGetFileEntries_singleFileUrlListEndsWithSlash() {
  344. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  345. load(A2_TEST_DIR"/url-list-singleFileEndsWithSlash.torrent", dctx, option_);
  346. // This is single-file torrent.
  347. const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
  348. dctx->getFileEntries();
  349. // There are 1 file entries.
  350. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  351. const std::shared_ptr<FileEntry>& fileEntry1 = fileEntries.front();
  352. CPPUNIT_ASSERT_EQUAL(std::string("./aria2@.tar.bz2"),
  353. fileEntry1->getPath());
  354. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  355. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  356. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2%40.tar.bz2"),
  357. uris1[0]);
  358. }
  359. void BittorrentHelperTest::testLoadFromMemory_multiFileNonUtf8Path()
  360. {
  361. auto path = List::g();
  362. path->append("path");
  363. path->append(fromHex("90a28a")+"E");
  364. auto file = Dict::g();
  365. file->put("length", Integer::g(1024));
  366. file->put("path", std::move(path));
  367. auto files = List::g();
  368. files->append(std::move(file));
  369. auto info = Dict::g();
  370. info->put("files", std::move(files));
  371. info->put("piece length", Integer::g(1024));
  372. info->put("pieces", "01234567890123456789");
  373. info->put("name", fromHex("1b")+"$B%O%m!<"+fromHex("1b")+"(B");
  374. Dict dict;
  375. dict.put("info", std::move(info));
  376. auto dctx = std::make_shared<DownloadContext>();
  377. loadFromMemory(bencode2::encode(&dict), dctx, option_, "default");
  378. auto& fe = dctx->getFirstFileEntry();
  379. CPPUNIT_ASSERT_EQUAL
  380. (std::string("./%1B%24B%25O%25m%21%3C%1B%28B/path/%90%A2%8AE"),
  381. fe->getPath());
  382. CPPUNIT_ASSERT_EQUAL
  383. (std::string("%1B%24B%25O%25m%21%3C%1B%28B/path/%90%A2%8AE"),
  384. fe->getSuffixPath());
  385. CPPUNIT_ASSERT_EQUAL
  386. (std::string("./%1B%24B%25O%25m%21%3C%1B%28B"), dctx->getBasePath());
  387. }
  388. void BittorrentHelperTest::testLoadFromMemory_singleFileNonUtf8Path()
  389. {
  390. auto info = Dict::g();
  391. info->put("piece length", Integer::g(1024));
  392. info->put("pieces", "01234567890123456789");
  393. info->put("name", fromHex("90a28a")+"E");
  394. info->put("length", Integer::g(1024));
  395. Dict dict;
  396. dict.put("info", std::move(info));
  397. auto dctx = std::make_shared<DownloadContext>();
  398. loadFromMemory(bencode2::encode(&dict), dctx, option_, "default");
  399. const std::shared_ptr<FileEntry>& fe = dctx->getFirstFileEntry();
  400. CPPUNIT_ASSERT_EQUAL(std::string("./%90%A2%8AE"), fe->getPath());
  401. CPPUNIT_ASSERT_EQUAL(std::string("%90%A2%8AE"), fe->getSuffixPath());
  402. }
  403. void BittorrentHelperTest::testLoadFromMemory()
  404. {
  405. 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";
  406. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  407. loadFromMemory(memory, dctx, option_, "default");
  408. std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  409. CPPUNIT_ASSERT_EQUAL(correctHash, getInfoHashString(dctx));
  410. }
  411. void BittorrentHelperTest::testLoadFromMemory_somethingMissing()
  412. {
  413. // pieces missing
  414. try {
  415. std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee";
  416. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  417. loadFromMemory(memory, dctx, option_, "default");
  418. CPPUNIT_FAIL("exception must be thrown.");
  419. } catch(Exception& e) {
  420. // OK
  421. }
  422. }
  423. void BittorrentHelperTest::testLoadFromMemory_overrideName()
  424. {
  425. 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";
  426. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  427. loadFromMemory(memory, dctx, option_, "default", "aria2-override.name");
  428. CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"),
  429. getTorrentAttrs(dctx)->name);
  430. }
  431. void BittorrentHelperTest::testLoadFromMemory_multiFileDirTraversal()
  432. {
  433. std::string memory =
  434. "d8:announce27:http://example.com/announce4:infod5:filesld6:lengthi262144e4:pathl7:../dir14:dir28:file.imgeee4:name14:../name1/name212:piece lengthi262144e6:pieces20:00000000000000000000ee";
  435. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  436. try {
  437. loadFromMemory(memory, dctx, option_, "default");
  438. CPPUNIT_FAIL("Exception must be thrown.");
  439. } catch(RecoverableException& e) {
  440. // success
  441. }
  442. }
  443. void BittorrentHelperTest::testLoadFromMemory_singleFileDirTraversal()
  444. {
  445. std::string memory =
  446. "d8:announce27:http://example.com/announce4:infod4:name14:../name1/name26:lengthi262144e12:piece lengthi262144e6:pieces20:00000000000000000000ee";
  447. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  448. try {
  449. loadFromMemory(memory, dctx, option_, "default");
  450. } catch(RecoverableException& e) {
  451. // success
  452. }
  453. }
  454. void BittorrentHelperTest::testGetNodes()
  455. {
  456. {
  457. std::string memory =
  458. "d5:nodesl"
  459. "l11:192.168.0.1i6881ee"
  460. "l11:192.168.0.2i6882ee"
  461. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  462. "12:piece lengthi262144e"
  463. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  464. "ee";
  465. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  466. loadFromMemory(memory, dctx, option_, "default");
  467. auto attrs = getTorrentAttrs(dctx);
  468. CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->nodes.size());
  469. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), attrs->nodes[0].first);
  470. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, attrs->nodes[0].second);
  471. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[1].first);
  472. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[1].second);
  473. }
  474. {
  475. // empty hostname
  476. std::string memory =
  477. "d5:nodesl"
  478. "l1: i6881ee"
  479. "l11:192.168.0.2i6882ee"
  480. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  481. "12:piece lengthi262144e"
  482. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  483. "ee";
  484. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  485. loadFromMemory(memory, dctx, option_, "default");
  486. auto attrs = getTorrentAttrs(dctx);
  487. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  488. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  489. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  490. }
  491. {
  492. // bad port
  493. std::string memory =
  494. "d5:nodesl"
  495. "l11:192.168.0.11:xe"
  496. "l11:192.168.0.2i6882ee"
  497. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  498. "12:piece lengthi262144e"
  499. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  500. "ee";
  501. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  502. loadFromMemory(memory, dctx, option_, "default");
  503. auto attrs = getTorrentAttrs(dctx);
  504. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  505. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  506. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  507. }
  508. {
  509. // port missing
  510. std::string memory =
  511. "d5:nodesl"
  512. "l11:192.168.0.1e"
  513. "l11:192.168.0.2i6882ee"
  514. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  515. "12:piece lengthi262144e"
  516. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  517. "ee";
  518. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  519. loadFromMemory(memory, dctx, option_, "default");
  520. auto attrs = getTorrentAttrs(dctx);
  521. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  522. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  523. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  524. }
  525. {
  526. // nodes is not a list
  527. std::string memory =
  528. "d5:nodes"
  529. "l11:192.168.0.1e"
  530. "4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  531. "12:piece lengthi262144e"
  532. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  533. "ee";
  534. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  535. loadFromMemory(memory, dctx, option_, "default");
  536. auto attrs = getTorrentAttrs(dctx);
  537. CPPUNIT_ASSERT_EQUAL((size_t)0, attrs->nodes.size());
  538. }
  539. {
  540. // the element of node is not Data
  541. std::string memory =
  542. "d5:nodesl"
  543. "ll11:192.168.0.1i6881eee"
  544. "l11:192.168.0.2i6882ee"
  545. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  546. "12:piece lengthi262144e"
  547. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  548. "ee";
  549. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  550. loadFromMemory(memory, dctx, option_, "default");
  551. auto attrs = getTorrentAttrs(dctx);
  552. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  553. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  554. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  555. }
  556. }
  557. void BittorrentHelperTest::testGetBasePath()
  558. {
  559. std::shared_ptr<DownloadContext> singleCtx(new DownloadContext());
  560. load(A2_TEST_DIR"/single.torrent", singleCtx, option_);
  561. singleCtx->setFilePathWithIndex(1, "new-path");
  562. CPPUNIT_ASSERT_EQUAL(std::string("new-path"), singleCtx->getBasePath());
  563. option_->put(PREF_DIR, "downloads");
  564. std::shared_ptr<DownloadContext> multiCtx(new DownloadContext());
  565. load(A2_TEST_DIR"/test.torrent", multiCtx, option_);
  566. CPPUNIT_ASSERT_EQUAL(std::string("downloads/aria2-test"),
  567. multiCtx->getBasePath());
  568. }
  569. void BittorrentHelperTest::testSetFileFilter_single()
  570. {
  571. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  572. load(A2_TEST_DIR"/single.torrent", dctx, option_);
  573. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  574. dctx->setFileFilter(SegList<int>());
  575. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  576. dctx->setFileFilter(util::parseIntSegments("1,2"));
  577. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  578. // For single file torrent, file is always selected whatever range
  579. // is passed.
  580. dctx->setFileFilter(util::parseIntSegments("2,3"));
  581. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  582. }
  583. void BittorrentHelperTest::testSetFileFilter_multi()
  584. {
  585. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  586. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  587. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  588. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  589. dctx->setFileFilter(SegList<int>());
  590. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  591. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  592. dctx->setFileFilter(util::parseIntSegments("2,3"));
  593. CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
  594. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  595. dctx->setFileFilter(util::parseIntSegments("3,4"));
  596. CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
  597. CPPUNIT_ASSERT(!dctx->getFileEntries()[1]->isRequested());
  598. dctx->setFileFilter(util::parseIntSegments("1,2"));
  599. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  600. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  601. }
  602. void BittorrentHelperTest::testUTF8Torrent()
  603. {
  604. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  605. load(A2_TEST_DIR"/utf8.torrent", dctx, option_);
  606. CPPUNIT_ASSERT_EQUAL(std::string("name in utf-8"),
  607. getTorrentAttrs(dctx)->name);
  608. CPPUNIT_ASSERT_EQUAL(std::string("./name in utf-8/path in utf-8"),
  609. dctx->getFirstFileEntry()->getPath());
  610. CPPUNIT_ASSERT_EQUAL(std::string("This is utf8 comment."),
  611. getTorrentAttrs(dctx)->comment);
  612. }
  613. void BittorrentHelperTest::testEtc()
  614. {
  615. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  616. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  617. CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
  618. getTorrentAttrs(dctx)->comment);
  619. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  620. getTorrentAttrs(dctx)->createdBy);
  621. CPPUNIT_ASSERT_EQUAL((time_t)1123456789,
  622. getTorrentAttrs(dctx)->creationDate);
  623. }
  624. void BittorrentHelperTest::testCheckBitfield()
  625. {
  626. unsigned char bitfield[] = { 0xff, 0xe0 };
  627. checkBitfield(bitfield, sizeof(bitfield), 11);
  628. try {
  629. checkBitfield(bitfield, sizeof(bitfield), 17);
  630. CPPUNIT_FAIL("exception must be thrown.");
  631. } catch(RecoverableException& e) {
  632. // success
  633. }
  634. // Change last byte
  635. bitfield[1] = 0xf0;
  636. try {
  637. checkBitfield(bitfield, sizeof(bitfield), 11);
  638. CPPUNIT_FAIL("exception must be thrown.");
  639. } catch(RecoverableException& e) {
  640. // success
  641. }
  642. }
  643. void BittorrentHelperTest::testMetadata() {
  644. auto dctx = std::make_shared<DownloadContext>();
  645. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  646. std::string torrentData = readFile(A2_TEST_DIR"/test.torrent");
  647. auto tr = bencode2::decode(torrentData);
  648. auto infoDic = downcast<Dict>(tr)->get("info");
  649. std::string metadata = bencode2::encode(infoDic);
  650. auto attrs = getTorrentAttrs(dctx);
  651. CPPUNIT_ASSERT(metadata == attrs->metadata);
  652. CPPUNIT_ASSERT_EQUAL(metadata.size(), attrs->metadataSize);
  653. }
  654. void BittorrentHelperTest::testParseMagnet()
  655. {
  656. std::string magnet =
  657. "magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c&dn=aria2"
  658. "&tr=http://tracker1&tr=http://tracker2";
  659. auto attrs = bittorrent::parseMagnet(magnet);
  660. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  661. util::toHex(attrs->infoHash));
  662. CPPUNIT_ASSERT_EQUAL(std::string("[METADATA]aria2"), attrs->name);
  663. CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->announceList.size());
  664. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  665. attrs->announceList[0][0]);
  666. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
  667. attrs->announceList[1][0]);
  668. magnet = "magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c";
  669. attrs = bittorrent::parseMagnet(magnet);
  670. CPPUNIT_ASSERT_EQUAL
  671. (std::string("[METADATA]248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  672. attrs->name);
  673. CPPUNIT_ASSERT(attrs->announceList.empty());
  674. magnet = "magnet:?xt=urn:sha1:7899bdb90a026c746f3cbc10839dd9b2a2a3e985&"
  675. "xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c";
  676. attrs = bittorrent::parseMagnet(magnet);
  677. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  678. util::toHex(attrs->infoHash));
  679. }
  680. void BittorrentHelperTest::testParseMagnet_base32()
  681. {
  682. std::string infoHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  683. std::string base32InfoHash = base32::encode(fromHex(infoHash));
  684. std::string magnet = "magnet:?xt=urn:btih:"+base32InfoHash+"&dn=aria2";
  685. auto attrs = bittorrent::parseMagnet(magnet);
  686. CPPUNIT_ASSERT_EQUAL
  687. (std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  688. util::toHex(attrs->infoHash));
  689. }
  690. void BittorrentHelperTest::testMetadata2Torrent()
  691. {
  692. TorrentAttribute attrs;
  693. std::string metadata = "METADATA";
  694. CPPUNIT_ASSERT_EQUAL
  695. (std::string("d4:infoMETADATAe"), metadata2Torrent(metadata, &attrs));
  696. attrs.announceList.push_back(std::vector<std::string>());
  697. attrs.announceList[0].push_back("http://localhost/announce");
  698. CPPUNIT_ASSERT_EQUAL
  699. (std::string("d"
  700. "13:announce-list"
  701. "ll25:http://localhost/announceee"
  702. "4:infoMETADATA"
  703. "e"),
  704. metadata2Torrent(metadata, &attrs));
  705. }
  706. void BittorrentHelperTest::testTorrent2Magnet()
  707. {
  708. std::shared_ptr<DownloadContext> dctx(new DownloadContext());
  709. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  710. CPPUNIT_ASSERT_EQUAL
  711. (std::string("magnet:?xt=urn:btih:248D0A1CD08284299DE78D5C1ED359BB46717D8C"
  712. "&dn=aria2-test"
  713. "&tr=http%3A%2F%2Ftracker1"
  714. "&tr=http%3A%2F%2Ftracker2"
  715. "&tr=http%3A%2F%2Ftracker3"),
  716. torrent2Magnet(getTorrentAttrs(dctx)));
  717. }
  718. void BittorrentHelperTest::testExtractPeerFromString()
  719. {
  720. std::string hextext = "100210354527354678541237324732171ae1";
  721. hextext += "20010db8bd0501d2288a1fc0000110ee1ae2";
  722. std::string peersstr = "36:"+fromHex(hextext);
  723. auto str = bencode2::decode(peersstr);
  724. std::deque<std::shared_ptr<Peer>> peers;
  725. extractPeer(str.get(), AF_INET6, std::back_inserter(peers));
  726. CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
  727. CPPUNIT_ASSERT_EQUAL(std::string("1002:1035:4527:3546:7854:1237:3247:3217"),
  728. peers[0]->getIPAddress());
  729. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peers[0]->getPort());
  730. CPPUNIT_ASSERT_EQUAL(std::string("2001:db8:bd05:1d2:288a:1fc0:1:10ee"),
  731. peers[1]->getIPAddress());
  732. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, peers[1]->getPort());
  733. hextext = "c0a800011ae1";
  734. hextext += "c0a800021ae2";
  735. peersstr = "12:"+fromHex(hextext);
  736. str = bencode2::decode(peersstr);
  737. peers.clear();
  738. extractPeer(str.get(), AF_INET, std::back_inserter(peers));
  739. CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
  740. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peers[0]->getIPAddress());
  741. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peers[0]->getPort());
  742. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peers[1]->getIPAddress());
  743. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, peers[1]->getPort());
  744. }
  745. void BittorrentHelperTest::testExtractPeerFromList()
  746. {
  747. std::string peersString =
  748. "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
  749. "4:porti2006eeee";
  750. auto dict = bencode2::decode(peersString);
  751. std::deque<std::shared_ptr<Peer>> peers;
  752. extractPeer(downcast<Dict>(dict)->get("peers"), AF_INET, std::back_inserter(peers));
  753. CPPUNIT_ASSERT_EQUAL((size_t)1, peers.size());
  754. auto& peer = *peers.begin();
  755. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer->getIPAddress());
  756. CPPUNIT_ASSERT_EQUAL((uint16_t)2006, peer->getPort());
  757. }
  758. void BittorrentHelperTest::testExtract2PeersFromList()
  759. {
  760. std::string peersString =
  761. "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
  762. "4:porti65535eed2:ip11:192.168.0.27:peer id20:aria2-00000000000000"
  763. "4:porti2007eeee";
  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)2, 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)65535, peer->getPort());
  771. peer = *(peers.begin()+1);
  772. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peer->getIPAddress());
  773. CPPUNIT_ASSERT_EQUAL((uint16_t)2007, peer->getPort());
  774. }
  775. void BittorrentHelperTest::testPackcompact()
  776. {
  777. unsigned char compact[COMPACT_LEN_IPV6];
  778. CPPUNIT_ASSERT_EQUAL(18,
  779. packcompact(compact,
  780. "1002:1035:4527:3546:7854:1237:3247:3217",
  781. 6881));
  782. CPPUNIT_ASSERT_EQUAL(std::string("100210354527354678541237324732171ae1"),
  783. util::toHex(compact, 18));
  784. CPPUNIT_ASSERT_EQUAL(6, packcompact(compact, "192.168.0.1", 6881));
  785. CPPUNIT_ASSERT_EQUAL(std::string("c0a800011ae1"), util::toHex(compact, 6));
  786. CPPUNIT_ASSERT_EQUAL(0, packcompact(compact, "badaddr", 6881));
  787. }
  788. void BittorrentHelperTest::testUnpackcompact()
  789. {
  790. unsigned char compact6[] = {
  791. 0x10, 0x02, 0x10, 0x35, 0x45, 0x27, 0x35, 0x46,
  792. 0x78, 0x54, 0x12, 0x37, 0x32, 0x47, 0x32, 0x17,
  793. 0x1A, 0xE1 };
  794. std::pair<std::string, uint16_t> p =
  795. unpackcompact(compact6, AF_INET6);
  796. CPPUNIT_ASSERT_EQUAL(std::string("1002:1035:4527:3546:7854:1237:3247:3217"),
  797. p.first);
  798. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, p.second);
  799. unsigned char compact[] = { 0xC0, 0xa8, 0x00, 0x01, 0x1A, 0xE1 };
  800. p = unpackcompact(compact, AF_INET);
  801. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), p.first);
  802. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, p.second);
  803. }
  804. void BittorrentHelperTest::testRemoveAnnounceUri()
  805. {
  806. TorrentAttribute attrs;
  807. std::vector<std::string> tier1;
  808. tier1.push_back("http://host1/announce");
  809. std::vector<std::string> tier2;
  810. tier2.push_back("http://host2/announce");
  811. tier2.push_back("http://host3/announce");
  812. attrs.announceList.push_back(tier1);
  813. attrs.announceList.push_back(tier2);
  814. std::vector<std::string> removeUris;
  815. removeUris.push_back(tier1[0]);
  816. removeUris.push_back(tier2[0]);
  817. removeAnnounceUri(&attrs, removeUris);
  818. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList.size());
  819. CPPUNIT_ASSERT_EQUAL(std::string("http://host3/announce"),
  820. attrs.announceList[0][0]);
  821. removeUris.clear();
  822. removeUris.push_back("*");
  823. removeAnnounceUri(&attrs, removeUris);
  824. CPPUNIT_ASSERT(attrs.announceList.empty());
  825. }
  826. void BittorrentHelperTest::testAddAnnounceUri()
  827. {
  828. TorrentAttribute attrs;
  829. std::vector<std::string> addUris;
  830. addUris.push_back("http://host1/announce");
  831. addUris.push_back("http://host2/announce");
  832. addAnnounceUri(&attrs, addUris);
  833. CPPUNIT_ASSERT_EQUAL((size_t)2, attrs.announceList.size());
  834. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[0].size());
  835. CPPUNIT_ASSERT_EQUAL(std::string("http://host1/announce"),
  836. attrs.announceList[0][0]);
  837. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[1].size());
  838. CPPUNIT_ASSERT_EQUAL(std::string("http://host2/announce"),
  839. attrs.announceList[1][0]);
  840. }
  841. void BittorrentHelperTest::testAdjustAnnounceUri()
  842. {
  843. TorrentAttribute attrs;
  844. std::vector<std::string> tier1;
  845. tier1.push_back("http://host1/announce");
  846. std::vector<std::string> tier2;
  847. tier2.push_back("http://host2/announce");
  848. tier2.push_back("http://host3/announce");
  849. attrs.announceList.push_back(tier1);
  850. attrs.announceList.push_back(tier2);
  851. std::shared_ptr<Option> option(new Option());
  852. option->put(PREF_BT_TRACKER, "http://host1/announce,http://host4/announce");
  853. option->put(PREF_BT_EXCLUDE_TRACKER,
  854. "http://host1/announce,http://host2/announce");
  855. adjustAnnounceUri(&attrs, option);
  856. CPPUNIT_ASSERT_EQUAL((size_t)3, attrs.announceList.size());
  857. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[0].size());
  858. CPPUNIT_ASSERT_EQUAL(std::string("http://host3/announce"),
  859. attrs.announceList[0][0]);
  860. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[1].size());
  861. CPPUNIT_ASSERT_EQUAL(std::string("http://host1/announce"),
  862. attrs.announceList[1][0]);
  863. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[2].size());
  864. CPPUNIT_ASSERT_EQUAL(std::string("http://host4/announce"),
  865. attrs.announceList[2][0]);
  866. }
  867. } // namespace bittorrent
  868. } // namespace aria2