BittorrentHelperTest.cc 28 KB

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