BittorrentHelperTest.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 "messageDigest.h"
  13. #include "a2netcompat.h"
  14. #include "bencode2.h"
  15. #include "TestUtil.h"
  16. #include "base32.h"
  17. namespace aria2 {
  18. namespace bittorrent {
  19. class BittorrentHelperTest:public CppUnit::TestFixture {
  20. CPPUNIT_TEST_SUITE(BittorrentHelperTest);
  21. CPPUNIT_TEST(testGetInfoHash);
  22. CPPUNIT_TEST(testGetPieceHash);
  23. CPPUNIT_TEST(testGetFileEntries);
  24. CPPUNIT_TEST(testGetTotalLength);
  25. CPPUNIT_TEST(testGetFileEntriesSingle);
  26. CPPUNIT_TEST(testGetTotalLengthSingle);
  27. CPPUNIT_TEST(testGetFileModeMulti);
  28. CPPUNIT_TEST(testGetFileModeSingle);
  29. CPPUNIT_TEST(testGetNameMulti);
  30. CPPUNIT_TEST(testGetNameSingle);
  31. CPPUNIT_TEST(testOverrideName);
  32. CPPUNIT_TEST(testGetAnnounceTier);
  33. CPPUNIT_TEST(testGetAnnounceTierAnnounceList);
  34. CPPUNIT_TEST(testGetPieceLength);
  35. CPPUNIT_TEST(testGetInfoHashAsString);
  36. CPPUNIT_TEST(testGetPeerId);
  37. CPPUNIT_TEST(testComputeFastSet);
  38. CPPUNIT_TEST(testGetFileEntries_multiFileUrlList);
  39. CPPUNIT_TEST(testGetFileEntries_singleFileUrlList);
  40. CPPUNIT_TEST(testGetFileEntries_singleFileUrlListEndsWithSlash);
  41. CPPUNIT_TEST(testLoadFromMemory);
  42. CPPUNIT_TEST(testLoadFromMemory_somethingMissing);
  43. CPPUNIT_TEST(testLoadFromMemory_overrideName);
  44. CPPUNIT_TEST(testLoadFromMemory_multiFileDirTraversal);
  45. CPPUNIT_TEST(testLoadFromMemory_singleFileDirTraversal);
  46. CPPUNIT_TEST(testGetNodes);
  47. CPPUNIT_TEST(testGetBasePath);
  48. CPPUNIT_TEST(testSetFileFilter_single);
  49. CPPUNIT_TEST(testSetFileFilter_multi);
  50. CPPUNIT_TEST(testUTF8Torrent);
  51. CPPUNIT_TEST(testEtc);
  52. CPPUNIT_TEST(testCreatecompact);
  53. CPPUNIT_TEST(testCheckBitfield);
  54. CPPUNIT_TEST(testMetadata);
  55. CPPUNIT_TEST(testParseMagnet);
  56. CPPUNIT_TEST(testParseMagnet_base32);
  57. CPPUNIT_TEST(testMetadata2Torrent);
  58. CPPUNIT_TEST(testTorrent2Magnet);
  59. CPPUNIT_TEST(testExtractPeerFromList);
  60. CPPUNIT_TEST(testExtract2PeersFromList);
  61. CPPUNIT_TEST_SUITE_END();
  62. public:
  63. void setUp() {
  64. }
  65. void testGetInfoHash();
  66. void testGetPieceHash();
  67. void testGetFileEntries();
  68. void testGetTotalLength();
  69. void testGetFileEntriesSingle();
  70. void testGetTotalLengthSingle();
  71. void testGetFileModeMulti();
  72. void testGetFileModeSingle();
  73. void testGetNameMulti();
  74. void testGetNameSingle();
  75. void testOverrideName();
  76. void testGetAnnounceTier();
  77. void testGetAnnounceTierAnnounceList();
  78. void testGetPieceLength();
  79. void testGetInfoHashAsString();
  80. void testGetPeerId();
  81. void testComputeFastSet();
  82. void testGetFileEntries_multiFileUrlList();
  83. void testGetFileEntries_singleFileUrlList();
  84. void testGetFileEntries_singleFileUrlListEndsWithSlash();
  85. void testLoadFromMemory();
  86. void testLoadFromMemory_somethingMissing();
  87. void testLoadFromMemory_overrideName();
  88. void testLoadFromMemory_multiFileDirTraversal();
  89. void testLoadFromMemory_singleFileDirTraversal();
  90. void testGetNodes();
  91. void testGetBasePath();
  92. void testSetFileFilter_single();
  93. void testSetFileFilter_multi();
  94. void testUTF8Torrent();
  95. void testEtc();
  96. void testCreatecompact();
  97. void testCheckBitfield();
  98. void testMetadata();
  99. void testParseMagnet();
  100. void testParseMagnet_base32();
  101. void testMetadata2Torrent();
  102. void testTorrent2Magnet();
  103. void testExtractPeerFromList();
  104. void testExtract2PeersFromList();
  105. };
  106. CPPUNIT_TEST_SUITE_REGISTRATION(BittorrentHelperTest);
  107. void BittorrentHelperTest::testGetInfoHash() {
  108. SharedHandle<DownloadContext> dctx(new DownloadContext());
  109. load("test.torrent", dctx);
  110. std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  111. CPPUNIT_ASSERT_EQUAL(correctHash, bittorrent::getInfoHashString(dctx));
  112. }
  113. void BittorrentHelperTest::testGetPieceHash() {
  114. SharedHandle<DownloadContext> dctx(new DownloadContext());
  115. load("test.torrent", dctx);
  116. CPPUNIT_ASSERT_EQUAL(util::toHex("AAAAAAAAAAAAAAAAAAAA", 20),
  117. dctx->getPieceHash(0));
  118. CPPUNIT_ASSERT_EQUAL(util::toHex("BBBBBBBBBBBBBBBBBBBB", 20),
  119. dctx->getPieceHash(1));
  120. CPPUNIT_ASSERT_EQUAL(util::toHex("CCCCCCCCCCCCCCCCCCCC", 20),
  121. dctx->getPieceHash(2));
  122. CPPUNIT_ASSERT_EQUAL(std::string(""),
  123. dctx->getPieceHash(3));
  124. CPPUNIT_ASSERT_EQUAL(MessageDigestContext::SHA1, dctx->getPieceHashAlgo());
  125. }
  126. void BittorrentHelperTest::testGetFileEntries() {
  127. SharedHandle<DownloadContext> dctx(new DownloadContext());
  128. load("test.torrent", dctx);
  129. // This is multi-file torrent.
  130. std::vector<SharedHandle<FileEntry> > fileEntries = dctx->getFileEntries();
  131. // There are 2 file entries.
  132. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  133. std::vector<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin();
  134. SharedHandle<FileEntry> fileEntry1 = *itr;
  135. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2/src/aria2c"),
  136. fileEntry1->getPath());
  137. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test/aria2/src/aria2c"),
  138. fileEntry1->getOriginalName());
  139. itr++;
  140. SharedHandle<FileEntry> fileEntry2 = *itr;
  141. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2-0.2.2.tar.bz2"),
  142. fileEntry2->getPath());
  143. }
  144. void BittorrentHelperTest::testGetFileEntriesSingle() {
  145. SharedHandle<DownloadContext> dctx(new DownloadContext());
  146. load("single.torrent", dctx);
  147. // This is multi-file torrent.
  148. std::vector<SharedHandle<FileEntry> > fileEntries = dctx->getFileEntries();
  149. // There is 1 file entry.
  150. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  151. std::vector<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin();
  152. SharedHandle<FileEntry> fileEntry1 = *itr;
  153. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"),
  154. fileEntry1->getPath());
  155. CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"),
  156. fileEntry1->getOriginalName());
  157. }
  158. void BittorrentHelperTest::testGetTotalLength() {
  159. SharedHandle<DownloadContext> dctx(new DownloadContext());
  160. load("test.torrent", dctx);
  161. CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, dctx->getTotalLength());
  162. }
  163. void BittorrentHelperTest::testGetTotalLengthSingle() {
  164. SharedHandle<DownloadContext> dctx(new DownloadContext());
  165. load("single.torrent", dctx);
  166. CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, dctx->getTotalLength());
  167. }
  168. void BittorrentHelperTest::testGetFileModeMulti() {
  169. SharedHandle<DownloadContext> dctx(new DownloadContext());
  170. load("test.torrent", dctx);
  171. CPPUNIT_ASSERT_EQUAL(MULTI, getTorrentAttrs(dctx)->mode);
  172. }
  173. void BittorrentHelperTest::testGetFileModeSingle() {
  174. SharedHandle<DownloadContext> dctx(new DownloadContext());
  175. load("single.torrent", dctx);
  176. CPPUNIT_ASSERT_EQUAL(SINGLE, getTorrentAttrs(dctx)->mode);
  177. }
  178. void BittorrentHelperTest::testGetNameMulti() {
  179. SharedHandle<DownloadContext> dctx(new DownloadContext());
  180. load("test.torrent", dctx);
  181. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"), getTorrentAttrs(dctx)->name);
  182. }
  183. void BittorrentHelperTest::testGetNameSingle() {
  184. SharedHandle<DownloadContext> dctx(new DownloadContext());
  185. load("single.torrent", dctx);
  186. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"),
  187. dctx->getBasePath());
  188. CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"),
  189. getTorrentAttrs(dctx)->name);
  190. }
  191. void BittorrentHelperTest::testOverrideName()
  192. {
  193. SharedHandle<DownloadContext> dctx(new DownloadContext());
  194. load("test.torrent", dctx, "aria2-override.name");
  195. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-override.name"),
  196. dctx->getBasePath());
  197. CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"),
  198. getTorrentAttrs(dctx)->name);
  199. }
  200. void BittorrentHelperTest::testGetAnnounceTier() {
  201. SharedHandle<DownloadContext> dctx(new DownloadContext());
  202. load("single.torrent", dctx);
  203. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  204. // There is 1 tier.
  205. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList.size());
  206. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[0].size());
  207. CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.com/announce.php"),
  208. attrs->announceList[0][0]);
  209. }
  210. void BittorrentHelperTest::testGetAnnounceTierAnnounceList() {
  211. SharedHandle<DownloadContext> dctx(new DownloadContext());
  212. load("test.torrent", dctx);
  213. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  214. // There are 3 tiers.
  215. CPPUNIT_ASSERT_EQUAL((size_t)3, attrs->announceList.size());
  216. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[0].size());
  217. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  218. attrs->announceList[0][0]);
  219. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[1].size());
  220. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
  221. attrs->announceList[1][0]);
  222. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[2].size());
  223. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"),
  224. attrs->announceList[2][0]);
  225. }
  226. void BittorrentHelperTest::testGetPieceLength() {
  227. SharedHandle<DownloadContext> dctx(new DownloadContext());
  228. load("test.torrent", dctx);
  229. CPPUNIT_ASSERT_EQUAL((size_t)128, dctx->getPieceLength());
  230. }
  231. void BittorrentHelperTest::testGetInfoHashAsString() {
  232. SharedHandle<DownloadContext> dctx(new DownloadContext());
  233. load("test.torrent", dctx);
  234. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  235. getInfoHashString(dctx));
  236. }
  237. void BittorrentHelperTest::testGetPeerId() {
  238. std::string peerId = generatePeerId("aria2-");
  239. CPPUNIT_ASSERT(peerId.find("aria2-") == 0);
  240. CPPUNIT_ASSERT_EQUAL((size_t)20, peerId.size());
  241. }
  242. void BittorrentHelperTest::testComputeFastSet()
  243. {
  244. std::string ipaddr = "192.168.0.1";
  245. unsigned char infoHash[20];
  246. memset(infoHash, 0, sizeof(infoHash));
  247. infoHash[0] = 0xff;
  248. int fastSetSize = 10;
  249. size_t numPieces = 1000;
  250. {
  251. std::vector<size_t> fastSet;
  252. computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
  253. size_t ans[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
  254. std::vector<size_t> ansSet(vbegin(ans), vend(ans));
  255. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
  256. }
  257. ipaddr = "10.0.0.1";
  258. {
  259. std::vector<size_t> fastSet;
  260. computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
  261. size_t ans[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
  262. std::vector<size_t> ansSet(vbegin(ans), vend(ans));
  263. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
  264. }
  265. // See when pieces < fastSetSize
  266. numPieces = 9;
  267. {
  268. std::vector<size_t> fastSet;
  269. computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
  270. size_t ans[] = { 8, 6, 7, 5, 1, 4, 0, 2, 3 };
  271. std::vector<size_t> ansSet(vbegin(ans), vend(ans));
  272. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
  273. }
  274. }
  275. void BittorrentHelperTest::testGetFileEntries_multiFileUrlList() {
  276. SharedHandle<DownloadContext> dctx(new DownloadContext());
  277. load("url-list-multiFile.torrent", dctx);
  278. // This is multi-file torrent.
  279. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  280. dctx->getFileEntries();
  281. // There are 2 file entries.
  282. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  283. std::vector<SharedHandle<FileEntry> >::const_iterator itr =
  284. fileEntries.begin();
  285. const SharedHandle<FileEntry>& fileEntry1 = *itr;
  286. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test@/aria2@/src@/aria2c@"),
  287. fileEntry1->getPath());
  288. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  289. CPPUNIT_ASSERT_EQUAL((size_t)2, uris1.size());
  290. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test%40/aria2%40/src%40/aria2c%40"),
  291. uris1[0]);
  292. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test%40/aria2%40/src%40/aria2c%40"),
  293. uris1[1]);
  294. ++itr;
  295. const SharedHandle<FileEntry>& fileEntry2 = *itr;
  296. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test@/aria2-0.2.2.tar.bz2"),
  297. fileEntry2->getPath());
  298. const std::deque<std::string>& uris2 = fileEntry2->getRemainingUris();
  299. CPPUNIT_ASSERT_EQUAL((size_t)2, uris2.size());
  300. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test%40/aria2-0.2.2.tar.bz2"),
  301. uris2[0]);
  302. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test%40/aria2-0.2.2.tar.bz2"),
  303. uris2[1]);
  304. }
  305. void BittorrentHelperTest::testGetFileEntries_singleFileUrlList() {
  306. SharedHandle<DownloadContext> dctx(new DownloadContext());
  307. load("url-list-singleFile.torrent", dctx);
  308. // This is single-file torrent.
  309. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  310. dctx->getFileEntries();
  311. // There are 1 file entries.
  312. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  313. const SharedHandle<FileEntry>& fileEntry1 = fileEntries.front();
  314. CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"),
  315. fileEntry1->getPath());
  316. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  317. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  318. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"),
  319. uris1[0]);
  320. }
  321. void BittorrentHelperTest::testGetFileEntries_singleFileUrlListEndsWithSlash() {
  322. SharedHandle<DownloadContext> dctx(new DownloadContext());
  323. load("url-list-singleFileEndsWithSlash.torrent", dctx);
  324. // This is single-file torrent.
  325. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  326. dctx->getFileEntries();
  327. // There are 1 file entries.
  328. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  329. const SharedHandle<FileEntry>& fileEntry1 = fileEntries.front();
  330. CPPUNIT_ASSERT_EQUAL(std::string("./aria2@.tar.bz2"),
  331. fileEntry1->getPath());
  332. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  333. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  334. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2%40.tar.bz2"),
  335. uris1[0]);
  336. }
  337. void BittorrentHelperTest::testLoadFromMemory()
  338. {
  339. 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";
  340. SharedHandle<DownloadContext> dctx(new DownloadContext());
  341. loadFromMemory(memory, dctx, "default");
  342. std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  343. CPPUNIT_ASSERT_EQUAL(correctHash, getInfoHashString(dctx));
  344. }
  345. void BittorrentHelperTest::testLoadFromMemory_somethingMissing()
  346. {
  347. // pieces missing
  348. try {
  349. std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee";
  350. SharedHandle<DownloadContext> dctx(new DownloadContext());
  351. loadFromMemory(memory, dctx, "default");
  352. CPPUNIT_FAIL("exception must be thrown.");
  353. } catch(Exception& e) {
  354. // OK
  355. }
  356. }
  357. void BittorrentHelperTest::testLoadFromMemory_overrideName()
  358. {
  359. 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";
  360. SharedHandle<DownloadContext> dctx(new DownloadContext());
  361. loadFromMemory(memory, dctx, "default", "aria2-override.name");
  362. CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"),
  363. getTorrentAttrs(dctx)->name);
  364. }
  365. void BittorrentHelperTest::testLoadFromMemory_multiFileDirTraversal()
  366. {
  367. std::string memory =
  368. "d8:announce27:http://example.com/announce4:infod5:filesld6:lengthi262144e4:pathl7:../dir14:dir28:file.imgeee4:name14:../name1/name212:piece lengthi262144e6:pieces20:00000000000000000000ee";
  369. SharedHandle<DownloadContext> dctx(new DownloadContext());
  370. dctx->setDir("/tmp");
  371. try {
  372. loadFromMemory(memory, dctx, "default");
  373. CPPUNIT_FAIL("Exception must be thrown.");
  374. } catch(RecoverableException& e) {
  375. // success
  376. }
  377. }
  378. void BittorrentHelperTest::testLoadFromMemory_singleFileDirTraversal()
  379. {
  380. std::string memory =
  381. "d8:announce27:http://example.com/announce4:infod4:name14:../name1/name26:lengthi262144e12:piece lengthi262144e6:pieces20:00000000000000000000ee";
  382. SharedHandle<DownloadContext> dctx(new DownloadContext());
  383. dctx->setDir("/tmp");
  384. try {
  385. loadFromMemory(memory, dctx, "default");
  386. } catch(RecoverableException& e) {
  387. // success
  388. }
  389. }
  390. void BittorrentHelperTest::testGetNodes()
  391. {
  392. {
  393. std::string memory =
  394. "d5:nodesl"
  395. "l11:192.168.0.1i6881ee"
  396. "l11:192.168.0.2i6882ee"
  397. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  398. "12:piece lengthi262144e"
  399. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  400. "ee";
  401. SharedHandle<DownloadContext> dctx(new DownloadContext());
  402. loadFromMemory(memory, dctx, "default");
  403. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  404. CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->nodes.size());
  405. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), attrs->nodes[0].first);
  406. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, attrs->nodes[0].second);
  407. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[1].first);
  408. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[1].second);
  409. }
  410. {
  411. // empty hostname
  412. std::string memory =
  413. "d5:nodesl"
  414. "l1: i6881ee"
  415. "l11:192.168.0.2i6882ee"
  416. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  417. "12:piece lengthi262144e"
  418. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  419. "ee";
  420. SharedHandle<DownloadContext> dctx(new DownloadContext());
  421. loadFromMemory(memory, dctx, "default");
  422. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  423. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  424. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  425. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  426. }
  427. {
  428. // bad port
  429. std::string memory =
  430. "d5:nodesl"
  431. "l11:192.168.0.11:xe"
  432. "l11:192.168.0.2i6882ee"
  433. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  434. "12:piece lengthi262144e"
  435. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  436. "ee";
  437. SharedHandle<DownloadContext> dctx(new DownloadContext());
  438. loadFromMemory(memory, dctx, "default");
  439. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  440. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  441. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  442. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  443. }
  444. {
  445. // port missing
  446. std::string memory =
  447. "d5:nodesl"
  448. "l11:192.168.0.1e"
  449. "l11:192.168.0.2i6882ee"
  450. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  451. "12:piece lengthi262144e"
  452. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  453. "ee";
  454. SharedHandle<DownloadContext> dctx(new DownloadContext());
  455. loadFromMemory(memory, dctx, "default");
  456. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  457. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  458. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  459. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  460. }
  461. {
  462. // nodes is not a list
  463. std::string memory =
  464. "d5:nodes"
  465. "l11:192.168.0.1e"
  466. "4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  467. "12:piece lengthi262144e"
  468. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  469. "ee";
  470. SharedHandle<DownloadContext> dctx(new DownloadContext());
  471. loadFromMemory(memory, dctx, "default");
  472. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  473. CPPUNIT_ASSERT_EQUAL((size_t)0, attrs->nodes.size());
  474. }
  475. {
  476. // the element of node is not Data
  477. std::string memory =
  478. "d5:nodesl"
  479. "ll11:192.168.0.1i6881eee"
  480. "l11:192.168.0.2i6882ee"
  481. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  482. "12:piece lengthi262144e"
  483. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  484. "ee";
  485. SharedHandle<DownloadContext> dctx(new DownloadContext());
  486. loadFromMemory(memory, dctx, "default");
  487. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  488. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  489. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  490. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  491. }
  492. }
  493. void BittorrentHelperTest::testGetBasePath()
  494. {
  495. SharedHandle<DownloadContext> singleCtx(new DownloadContext());
  496. load("single.torrent", singleCtx);
  497. singleCtx->setFilePathWithIndex(1, "new-path");
  498. CPPUNIT_ASSERT_EQUAL(std::string("new-path"), singleCtx->getBasePath());
  499. SharedHandle<DownloadContext> multiCtx(new DownloadContext());
  500. multiCtx->setDir("downloads");
  501. load("test.torrent", multiCtx);
  502. CPPUNIT_ASSERT_EQUAL(std::string("downloads/aria2-test"),
  503. multiCtx->getBasePath());
  504. }
  505. void BittorrentHelperTest::testSetFileFilter_single()
  506. {
  507. SharedHandle<DownloadContext> dctx(new DownloadContext());
  508. load("single.torrent", dctx);
  509. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  510. dctx->setFileFilter(util::parseIntRange(""));
  511. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  512. dctx->setFileFilter(util::parseIntRange("1"));
  513. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  514. // For single file torrent, file is always selected whatever range
  515. // is passed.
  516. dctx->setFileFilter(util::parseIntRange("2"));
  517. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  518. }
  519. void BittorrentHelperTest::testSetFileFilter_multi()
  520. {
  521. SharedHandle<DownloadContext> dctx(new DownloadContext());
  522. load("test.torrent", dctx);
  523. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  524. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  525. dctx->setFileFilter(util::parseIntRange(""));
  526. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  527. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  528. dctx->setFileFilter(util::parseIntRange("2"));
  529. CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
  530. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  531. dctx->setFileFilter(util::parseIntRange("3"));
  532. CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
  533. CPPUNIT_ASSERT(!dctx->getFileEntries()[1]->isRequested());
  534. dctx->setFileFilter(util::parseIntRange("1,2"));
  535. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  536. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  537. }
  538. void BittorrentHelperTest::testUTF8Torrent()
  539. {
  540. SharedHandle<DownloadContext> dctx(new DownloadContext());
  541. load("utf8.torrent", dctx);
  542. CPPUNIT_ASSERT_EQUAL(std::string("name in utf-8"),
  543. getTorrentAttrs(dctx)->name);
  544. CPPUNIT_ASSERT_EQUAL(std::string("./name in utf-8/path in utf-8"),
  545. dctx->getFirstFileEntry()->getPath());
  546. CPPUNIT_ASSERT_EQUAL(std::string("This is utf8 comment."),
  547. getTorrentAttrs(dctx)->comment);
  548. }
  549. void BittorrentHelperTest::testEtc()
  550. {
  551. SharedHandle<DownloadContext> dctx(new DownloadContext());
  552. load("test.torrent", dctx);
  553. CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
  554. getTorrentAttrs(dctx)->comment);
  555. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  556. getTorrentAttrs(dctx)->createdBy);
  557. CPPUNIT_ASSERT_EQUAL((time_t)1123456789,
  558. getTorrentAttrs(dctx)->creationDate);
  559. }
  560. void BittorrentHelperTest::testCreatecompact()
  561. {
  562. unsigned char compact[6];
  563. // Note: bittorrent::createcompact() on linux can handle IPv4-mapped
  564. // addresses like `ffff::127.0.0.1', but on cygwin, it doesn't.
  565. CPPUNIT_ASSERT(createcompact(compact, "127.0.0.1", 6881));
  566. std::pair<std::string, uint16_t> p = unpackcompact(compact);
  567. CPPUNIT_ASSERT_EQUAL(std::string("127.0.0.1"), p.first);
  568. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, p.second);
  569. }
  570. void BittorrentHelperTest::testCheckBitfield()
  571. {
  572. unsigned char bitfield[] = { 0xff, 0xe0 };
  573. checkBitfield(bitfield, sizeof(bitfield), 11);
  574. try {
  575. checkBitfield(bitfield, sizeof(bitfield), 17);
  576. CPPUNIT_FAIL("exception must be thrown.");
  577. } catch(RecoverableException& e) {
  578. // success
  579. }
  580. // Change last byte
  581. bitfield[1] = 0xf0;
  582. try {
  583. checkBitfield(bitfield, sizeof(bitfield), 11);
  584. CPPUNIT_FAIL("exception must be thrown.");
  585. } catch(RecoverableException& e) {
  586. // success
  587. }
  588. }
  589. void BittorrentHelperTest::testMetadata() {
  590. SharedHandle<DownloadContext> dctx(new DownloadContext());
  591. load("test.torrent", dctx);
  592. std::string torrentData = readFile("test.torrent");
  593. SharedHandle<ValueBase> tr = bencode2::decode(torrentData);
  594. SharedHandle<ValueBase> infoDic = asDict(tr)->get("info");
  595. std::string metadata = bencode2::encode(infoDic);
  596. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  597. CPPUNIT_ASSERT(metadata == attrs->metadata);
  598. CPPUNIT_ASSERT_EQUAL(metadata.size(), attrs->metadataSize);
  599. }
  600. void BittorrentHelperTest::testParseMagnet()
  601. {
  602. std::string magnet =
  603. "magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c&dn=aria2"
  604. "&tr=http://tracker1&tr=http://tracker2";
  605. SharedHandle<TorrentAttribute> attrs = bittorrent::parseMagnet(magnet);
  606. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  607. util::toHex(attrs->infoHash));
  608. CPPUNIT_ASSERT_EQUAL(std::string("[METADATA]aria2"), attrs->name);
  609. CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->announceList.size());
  610. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  611. attrs->announceList[0][0]);
  612. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
  613. attrs->announceList[1][0]);
  614. magnet = "magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c";
  615. attrs = bittorrent::parseMagnet(magnet);
  616. CPPUNIT_ASSERT_EQUAL
  617. (std::string("[METADATA]248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  618. attrs->name);
  619. CPPUNIT_ASSERT(attrs->announceList.empty());
  620. magnet = "magnet:?xt=urn:sha1:7899bdb90a026c746f3cbc10839dd9b2a2a3e985&"
  621. "xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c";
  622. attrs = bittorrent::parseMagnet(magnet);
  623. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  624. util::toHex(attrs->infoHash));
  625. }
  626. void BittorrentHelperTest::testParseMagnet_base32()
  627. {
  628. std::string infoHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  629. std::string base32InfoHash = base32::encode(util::fromHex(infoHash));
  630. std::string magnet = "magnet:?xt=urn:btih:"+base32InfoHash+"&dn=aria2";
  631. SharedHandle<TorrentAttribute> attrs = bittorrent::parseMagnet(magnet);
  632. CPPUNIT_ASSERT_EQUAL
  633. (std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  634. util::toHex(attrs->infoHash));
  635. }
  636. void BittorrentHelperTest::testMetadata2Torrent()
  637. {
  638. std::string metadata = "METADATA";
  639. SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
  640. CPPUNIT_ASSERT_EQUAL
  641. (std::string("d4:infoMETADATAe"), metadata2Torrent(metadata, attrs));
  642. attrs->announceList.push_back(std::vector<std::string>());
  643. attrs->announceList[0].push_back("http://localhost/announce");
  644. CPPUNIT_ASSERT_EQUAL
  645. (std::string("d"
  646. "13:announce-list"
  647. "ll25:http://localhost/announceee"
  648. "4:infoMETADATA"
  649. "e"),
  650. metadata2Torrent(metadata, attrs));
  651. }
  652. void BittorrentHelperTest::testTorrent2Magnet()
  653. {
  654. SharedHandle<DownloadContext> dctx(new DownloadContext());
  655. load("test.torrent", dctx);
  656. CPPUNIT_ASSERT_EQUAL
  657. (std::string("magnet:?xt=urn:btih:248D0A1CD08284299DE78D5C1ED359BB46717D8C"
  658. "&dn=aria2-test"
  659. "&tr=http%3A%2F%2Ftracker1"
  660. "&tr=http%3A%2F%2Ftracker2"
  661. "&tr=http%3A%2F%2Ftracker3"),
  662. torrent2Magnet(getTorrentAttrs(dctx)));
  663. }
  664. void BittorrentHelperTest::testExtractPeerFromList()
  665. {
  666. std::string peersString =
  667. "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
  668. "4:porti2006eeee";
  669. SharedHandle<ValueBase> dict = bencode2::decode(peersString);
  670. std::deque<SharedHandle<Peer> > peers;
  671. extractPeer(asDict(dict)->get("peers"), std::back_inserter(peers));
  672. CPPUNIT_ASSERT_EQUAL((size_t)1, peers.size());
  673. SharedHandle<Peer> peer = *peers.begin();
  674. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer->getIPAddress());
  675. CPPUNIT_ASSERT_EQUAL((uint16_t)2006, peer->getPort());
  676. }
  677. void BittorrentHelperTest::testExtract2PeersFromList()
  678. {
  679. std::string peersString =
  680. "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
  681. "4:porti65535eed2:ip11:192.168.0.27:peer id20:aria2-00000000000000"
  682. "4:porti2007eeee";
  683. SharedHandle<ValueBase> dict = bencode2::decode(peersString);
  684. std::deque<SharedHandle<Peer> > peers;
  685. extractPeer(asDict(dict)->get("peers"), std::back_inserter(peers));
  686. CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
  687. SharedHandle<Peer> peer = *peers.begin();
  688. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer->getIPAddress());
  689. CPPUNIT_ASSERT_EQUAL((uint16_t)65535, peer->getPort());
  690. peer = *(peers.begin()+1);
  691. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peer->getIPAddress());
  692. CPPUNIT_ASSERT_EQUAL((uint16_t)2007, peer->getPort());
  693. }
  694. } // namespace bittorrent
  695. } // namespace aria2