BittorrentHelperTest.cc 27 KB

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