BittorrentHelperTest.cc 28 KB

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