BittorrentHelperTest.cc 32 KB

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