BittorrentHelperTest.cc 37 KB

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