BittorrentHelperTest.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. namespace aria2 {
  17. namespace bittorrent {
  18. class BittorrentHelperTest:public CppUnit::TestFixture {
  19. CPPUNIT_TEST_SUITE(BittorrentHelperTest);
  20. CPPUNIT_TEST(testGetInfoHash);
  21. CPPUNIT_TEST(testGetPieceHash);
  22. CPPUNIT_TEST(testGetFileEntries);
  23. CPPUNIT_TEST(testGetTotalLength);
  24. CPPUNIT_TEST(testGetFileEntriesSingle);
  25. CPPUNIT_TEST(testGetTotalLengthSingle);
  26. CPPUNIT_TEST(testGetFileModeMulti);
  27. CPPUNIT_TEST(testGetFileModeSingle);
  28. CPPUNIT_TEST(testGetNameMulti);
  29. CPPUNIT_TEST(testGetNameSingle);
  30. CPPUNIT_TEST(testOverrideName);
  31. CPPUNIT_TEST(testGetAnnounceTier);
  32. CPPUNIT_TEST(testGetAnnounceTierAnnounceList);
  33. CPPUNIT_TEST(testGetPieceLength);
  34. CPPUNIT_TEST(testGetInfoHashAsString);
  35. CPPUNIT_TEST(testGetPeerId);
  36. CPPUNIT_TEST(testComputeFastSet);
  37. CPPUNIT_TEST(testGetFileEntries_multiFileUrlList);
  38. CPPUNIT_TEST(testGetFileEntries_singleFileUrlList);
  39. CPPUNIT_TEST(testGetFileEntries_singleFileUrlListEndsWithSlash);
  40. CPPUNIT_TEST(testLoadFromMemory);
  41. CPPUNIT_TEST(testLoadFromMemory_somethingMissing);
  42. CPPUNIT_TEST(testLoadFromMemory_overrideName);
  43. CPPUNIT_TEST(testLoadFromMemory_joinPathMulti);
  44. CPPUNIT_TEST(testLoadFromMemory_joinPathSingle);
  45. CPPUNIT_TEST(testGetNodes);
  46. CPPUNIT_TEST(testGetBasePath);
  47. CPPUNIT_TEST(testSetFileFilter_single);
  48. CPPUNIT_TEST(testSetFileFilter_multi);
  49. CPPUNIT_TEST(testUTF8Torrent);
  50. CPPUNIT_TEST(testEtc);
  51. CPPUNIT_TEST(testCreatecompact);
  52. CPPUNIT_TEST(testCheckBitfield);
  53. CPPUNIT_TEST(testMetadata);
  54. CPPUNIT_TEST(testParseMagnetLink);
  55. CPPUNIT_TEST_SUITE_END();
  56. public:
  57. void setUp() {
  58. }
  59. void testGetInfoHash();
  60. void testGetPieceHash();
  61. void testGetFileEntries();
  62. void testGetTotalLength();
  63. void testGetFileEntriesSingle();
  64. void testGetTotalLengthSingle();
  65. void testGetFileModeMulti();
  66. void testGetFileModeSingle();
  67. void testGetNameMulti();
  68. void testGetNameSingle();
  69. void testOverrideName();
  70. void testGetAnnounceTier();
  71. void testGetAnnounceTierAnnounceList();
  72. void testGetPieceLength();
  73. void testGetInfoHashAsString();
  74. void testGetPeerId();
  75. void testComputeFastSet();
  76. void testGetFileEntries_multiFileUrlList();
  77. void testGetFileEntries_singleFileUrlList();
  78. void testGetFileEntries_singleFileUrlListEndsWithSlash();
  79. void testLoadFromMemory();
  80. void testLoadFromMemory_somethingMissing();
  81. void testLoadFromMemory_overrideName();
  82. void testLoadFromMemory_joinPathMulti();
  83. void testLoadFromMemory_joinPathSingle();
  84. void testGetNodes();
  85. void testGetBasePath();
  86. void testSetFileFilter_single();
  87. void testSetFileFilter_multi();
  88. void testUTF8Torrent();
  89. void testEtc();
  90. void testCreatecompact();
  91. void testCheckBitfield();
  92. void testMetadata();
  93. void testParseMagnetLink();
  94. };
  95. CPPUNIT_TEST_SUITE_REGISTRATION(BittorrentHelperTest);
  96. static const BDE& getAnnounceList(const SharedHandle<DownloadContext>& dctx)
  97. {
  98. return dctx->getAttribute(BITTORRENT)[ANNOUNCE_LIST];
  99. }
  100. static const std::string& getMode(const SharedHandle<DownloadContext>& dctx)
  101. {
  102. return dctx->getAttribute(BITTORRENT)[MODE].s();
  103. }
  104. static const std::string& getName(const SharedHandle<DownloadContext>& dctx)
  105. {
  106. return dctx->getAttribute(BITTORRENT)[NAME].s();
  107. }
  108. static const BDE& getNodes(const SharedHandle<DownloadContext>& dctx)
  109. {
  110. return dctx->getAttribute(BITTORRENT)[NODES];
  111. }
  112. void BittorrentHelperTest::testGetInfoHash() {
  113. SharedHandle<DownloadContext> dctx(new DownloadContext());
  114. load("test.torrent", dctx);
  115. std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  116. CPPUNIT_ASSERT_EQUAL(correctHash, bittorrent::getInfoHashString(dctx));
  117. }
  118. void BittorrentHelperTest::testGetPieceHash() {
  119. SharedHandle<DownloadContext> dctx(new DownloadContext());
  120. load("test.torrent", dctx);
  121. CPPUNIT_ASSERT_EQUAL(util::toHex("AAAAAAAAAAAAAAAAAAAA", 20),
  122. dctx->getPieceHash(0));
  123. CPPUNIT_ASSERT_EQUAL(util::toHex("BBBBBBBBBBBBBBBBBBBB", 20),
  124. dctx->getPieceHash(1));
  125. CPPUNIT_ASSERT_EQUAL(util::toHex("CCCCCCCCCCCCCCCCCCCC", 20),
  126. dctx->getPieceHash(2));
  127. CPPUNIT_ASSERT_EQUAL(std::string(""),
  128. dctx->getPieceHash(3));
  129. CPPUNIT_ASSERT_EQUAL(MessageDigestContext::SHA1, dctx->getPieceHashAlgo());
  130. }
  131. void BittorrentHelperTest::testGetFileEntries() {
  132. SharedHandle<DownloadContext> dctx(new DownloadContext());
  133. load("test.torrent", dctx);
  134. // This is multi-file torrent.
  135. std::vector<SharedHandle<FileEntry> > fileEntries = dctx->getFileEntries();
  136. // There are 2 file entries.
  137. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  138. std::vector<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin();
  139. SharedHandle<FileEntry> fileEntry1 = *itr;
  140. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2/src/aria2c"),
  141. fileEntry1->getPath());
  142. itr++;
  143. SharedHandle<FileEntry> fileEntry2 = *itr;
  144. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2-0.2.2.tar.bz2"),
  145. fileEntry2->getPath());
  146. }
  147. void BittorrentHelperTest::testGetFileEntriesSingle() {
  148. SharedHandle<DownloadContext> dctx(new DownloadContext());
  149. load("single.torrent", dctx);
  150. // This is multi-file torrent.
  151. std::vector<SharedHandle<FileEntry> > fileEntries = dctx->getFileEntries();
  152. // There is 1 file entry.
  153. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  154. std::vector<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin();
  155. SharedHandle<FileEntry> fileEntry1 = *itr;
  156. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"),
  157. fileEntry1->getPath());
  158. }
  159. void BittorrentHelperTest::testGetTotalLength() {
  160. SharedHandle<DownloadContext> dctx(new DownloadContext());
  161. load("test.torrent", dctx);
  162. CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, dctx->getTotalLength());
  163. }
  164. void BittorrentHelperTest::testGetTotalLengthSingle() {
  165. SharedHandle<DownloadContext> dctx(new DownloadContext());
  166. load("single.torrent", dctx);
  167. CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, dctx->getTotalLength());
  168. }
  169. void BittorrentHelperTest::testGetFileModeMulti() {
  170. SharedHandle<DownloadContext> dctx(new DownloadContext());
  171. load("test.torrent", dctx);
  172. CPPUNIT_ASSERT_EQUAL(MULTI, getMode(dctx));
  173. }
  174. void BittorrentHelperTest::testGetFileModeSingle() {
  175. SharedHandle<DownloadContext> dctx(new DownloadContext());
  176. load("single.torrent", dctx);
  177. CPPUNIT_ASSERT_EQUAL(SINGLE, getMode(dctx));
  178. }
  179. void BittorrentHelperTest::testGetNameMulti() {
  180. SharedHandle<DownloadContext> dctx(new DownloadContext());
  181. load("test.torrent", dctx);
  182. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"), getName(dctx));
  183. }
  184. void BittorrentHelperTest::testGetNameSingle() {
  185. SharedHandle<DownloadContext> dctx(new DownloadContext());
  186. load("single.torrent", dctx);
  187. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"),
  188. dctx->getBasePath());
  189. CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"), getName(dctx));
  190. }
  191. void BittorrentHelperTest::testOverrideName()
  192. {
  193. SharedHandle<DownloadContext> dctx(new DownloadContext());
  194. load("test.torrent", dctx, "aria2-override.name");
  195. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-override.name"),
  196. dctx->getBasePath());
  197. CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"), getName(dctx));
  198. }
  199. void BittorrentHelperTest::testGetAnnounceTier() {
  200. SharedHandle<DownloadContext> dctx(new DownloadContext());
  201. load("single.torrent", dctx);
  202. const BDE& announceList = getAnnounceList(dctx);
  203. // There is 1 tier.
  204. CPPUNIT_ASSERT_EQUAL((size_t)1, announceList.size());
  205. const BDE& tier = announceList[0];
  206. CPPUNIT_ASSERT_EQUAL((size_t)1, tier.size());
  207. CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.com/announce.php"),
  208. util::trim(tier[0].s()));
  209. }
  210. void BittorrentHelperTest::testGetAnnounceTierAnnounceList() {
  211. SharedHandle<DownloadContext> dctx(new DownloadContext());
  212. load("test.torrent", dctx);
  213. const BDE& announceList = getAnnounceList(dctx);
  214. // There are 3 tiers.
  215. CPPUNIT_ASSERT_EQUAL((size_t)3, announceList.size());
  216. const BDE& tier1 = announceList[0];
  217. CPPUNIT_ASSERT_EQUAL((size_t)1, tier1.size());
  218. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  219. util::trim(tier1[0].s()));
  220. const BDE& tier2 = announceList[1];
  221. CPPUNIT_ASSERT_EQUAL((size_t)1, tier2.size());
  222. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"), tier2[0].s());
  223. const BDE& tier3 = announceList[2];
  224. CPPUNIT_ASSERT_EQUAL((size_t)1, tier3.size());
  225. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"), tier3[0].s());
  226. }
  227. void BittorrentHelperTest::testGetPieceLength() {
  228. SharedHandle<DownloadContext> dctx(new DownloadContext());
  229. load("test.torrent", dctx);
  230. CPPUNIT_ASSERT_EQUAL((size_t)128, dctx->getPieceLength());
  231. }
  232. void BittorrentHelperTest::testGetInfoHashAsString() {
  233. SharedHandle<DownloadContext> dctx(new DownloadContext());
  234. load("test.torrent", dctx);
  235. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  236. getInfoHashString(dctx));
  237. }
  238. void BittorrentHelperTest::testGetPeerId() {
  239. std::string peerId = generatePeerId("aria2-");
  240. CPPUNIT_ASSERT(peerId.find("aria2-") == 0);
  241. CPPUNIT_ASSERT_EQUAL((size_t)20, peerId.size());
  242. }
  243. void BittorrentHelperTest::testComputeFastSet()
  244. {
  245. std::string ipaddr = "192.168.0.1";
  246. unsigned char infoHash[20];
  247. memset(infoHash, 0, sizeof(infoHash));
  248. infoHash[0] = 0xff;
  249. int fastSetSize = 10;
  250. size_t numPieces = 1000;
  251. {
  252. std::vector<size_t> fastSet;
  253. computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
  254. size_t ans[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
  255. std::vector<size_t> ansSet(&ans[0], &ans[arrayLength(ans)]);
  256. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
  257. }
  258. ipaddr = "10.0.0.1";
  259. {
  260. std::vector<size_t> fastSet;
  261. computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
  262. size_t ans[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
  263. std::vector<size_t> ansSet(&ans[0], &ans[arrayLength(ans)]);
  264. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
  265. }
  266. // See when pieces < fastSetSize
  267. numPieces = 9;
  268. {
  269. std::vector<size_t> fastSet;
  270. computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
  271. size_t ans[] = { 8, 6, 7, 5, 1, 4, 0, 2, 3 };
  272. std::vector<size_t> ansSet(&ans[0], &ans[arrayLength(ans)]);
  273. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
  274. }
  275. }
  276. void BittorrentHelperTest::testGetFileEntries_multiFileUrlList() {
  277. SharedHandle<DownloadContext> dctx(new DownloadContext());
  278. load("url-list-multiFile.torrent", dctx);
  279. // This is multi-file torrent.
  280. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  281. dctx->getFileEntries();
  282. // There are 2 file entries.
  283. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  284. std::vector<SharedHandle<FileEntry> >::const_iterator itr =
  285. fileEntries.begin();
  286. const SharedHandle<FileEntry>& fileEntry1 = *itr;
  287. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2/src/aria2c"),
  288. fileEntry1->getPath());
  289. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  290. CPPUNIT_ASSERT_EQUAL((size_t)2, uris1.size());
  291. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test/aria2/src/aria2c"),
  292. uris1[0]);
  293. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test/aria2/src/aria2c"),
  294. uris1[1]);
  295. ++itr;
  296. const SharedHandle<FileEntry>& fileEntry2 = *itr;
  297. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2-0.2.2.tar.bz2"),
  298. fileEntry2->getPath());
  299. const std::deque<std::string>& uris2 = fileEntry2->getRemainingUris();
  300. CPPUNIT_ASSERT_EQUAL((size_t)2, uris2.size());
  301. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test/aria2-0.2.2.tar.bz2"),
  302. uris2[0]);
  303. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test/aria2-0.2.2.tar.bz2"),
  304. uris2[1]);
  305. }
  306. void BittorrentHelperTest::testGetFileEntries_singleFileUrlList() {
  307. SharedHandle<DownloadContext> dctx(new DownloadContext());
  308. load("url-list-singleFile.torrent", dctx);
  309. // This is single-file torrent.
  310. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  311. dctx->getFileEntries();
  312. // There are 1 file entries.
  313. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  314. const SharedHandle<FileEntry>& fileEntry1 = fileEntries.front();
  315. CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"),
  316. fileEntry1->getPath());
  317. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  318. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  319. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"),
  320. uris1[0]);
  321. }
  322. void BittorrentHelperTest::testGetFileEntries_singleFileUrlListEndsWithSlash() {
  323. SharedHandle<DownloadContext> dctx(new DownloadContext());
  324. load("url-list-singleFileEndsWithSlash.torrent", dctx);
  325. // This is single-file torrent.
  326. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  327. dctx->getFileEntries();
  328. // There are 1 file entries.
  329. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  330. const SharedHandle<FileEntry>& fileEntry1 = fileEntries.front();
  331. CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"),
  332. fileEntry1->getPath());
  333. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  334. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  335. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"),
  336. uris1[0]);
  337. }
  338. void BittorrentHelperTest::testLoadFromMemory()
  339. {
  340. 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";
  341. SharedHandle<DownloadContext> dctx(new DownloadContext());
  342. loadFromMemory(memory, dctx, "default");
  343. std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  344. CPPUNIT_ASSERT_EQUAL(correctHash, getInfoHashString(dctx));
  345. }
  346. void BittorrentHelperTest::testLoadFromMemory_somethingMissing()
  347. {
  348. // pieces missing
  349. try {
  350. std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee";
  351. SharedHandle<DownloadContext> dctx(new DownloadContext());
  352. loadFromMemory(memory, dctx, "default");
  353. CPPUNIT_FAIL("exception must be thrown.");
  354. } catch(Exception& e) {
  355. // OK
  356. }
  357. }
  358. void BittorrentHelperTest::testLoadFromMemory_overrideName()
  359. {
  360. 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";
  361. SharedHandle<DownloadContext> dctx(new DownloadContext());
  362. loadFromMemory(memory, dctx, "default", "aria2-override.name");
  363. CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"), getName(dctx));
  364. }
  365. void BittorrentHelperTest::testLoadFromMemory_joinPathMulti()
  366. {
  367. std::string memory =
  368. "d8:announce27:http://example.com/announce4:infod5:filesld6:lengthi262144e4:pathl7:../dir14:dir28:file.imgeee4:name14:../name1/name212:piece lengthi262144e6:pieces20:00000000000000000000ee";
  369. SharedHandle<DownloadContext> dctx(new DownloadContext());
  370. dctx->setDir("/tmp");
  371. loadFromMemory(memory, dctx, "default");
  372. // remove ".." element
  373. CPPUNIT_ASSERT_EQUAL(std::string("../name1/name2"), getName(dctx));
  374. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/name1/dir1/dir2/file.img"),
  375. dctx->getFirstFileEntry()->getPath());
  376. }
  377. void BittorrentHelperTest::testLoadFromMemory_joinPathSingle()
  378. {
  379. std::string memory =
  380. "d8:announce27:http://example.com/announce4:infod4:name14:../name1/name26:lengthi262144e12:piece lengthi262144e6:pieces20:00000000000000000000ee";
  381. SharedHandle<DownloadContext> dctx(new DownloadContext());
  382. dctx->setDir("/tmp");
  383. loadFromMemory(memory, dctx, "default");
  384. CPPUNIT_ASSERT_EQUAL(std::string("../name1/name2"), getName(dctx));
  385. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/name1/name2"),
  386. dctx->getFirstFileEntry()->getPath());
  387. }
  388. void BittorrentHelperTest::testGetNodes()
  389. {
  390. {
  391. std::string memory =
  392. "d5:nodesl"
  393. "l11:192.168.0.1i6881ee"
  394. "l11:192.168.0.2i6882ee"
  395. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  396. "12:piece lengthi262144e"
  397. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  398. "ee";
  399. SharedHandle<DownloadContext> dctx(new DownloadContext());
  400. loadFromMemory(memory, dctx, "default");
  401. const BDE& nodes = getNodes(dctx);
  402. CPPUNIT_ASSERT_EQUAL((size_t)2, nodes.size());
  403. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), nodes[0][HOSTNAME].s());
  404. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, (uint16_t)nodes[0][PORT].i());
  405. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[1][HOSTNAME].s());
  406. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[1][PORT].i());
  407. }
  408. {
  409. // empty hostname
  410. std::string memory =
  411. "d5:nodesl"
  412. "l1: i6881ee"
  413. "l11:192.168.0.2i6882ee"
  414. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  415. "12:piece lengthi262144e"
  416. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  417. "ee";
  418. SharedHandle<DownloadContext> dctx(new DownloadContext());
  419. loadFromMemory(memory, dctx, "default");
  420. const BDE& nodes = getNodes(dctx);
  421. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  422. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0][HOSTNAME].s());
  423. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[0][PORT].i());
  424. }
  425. {
  426. // bad port
  427. std::string memory =
  428. "d5:nodesl"
  429. "l11:192.168.0.11:xe"
  430. "l11:192.168.0.2i6882ee"
  431. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  432. "12:piece lengthi262144e"
  433. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  434. "ee";
  435. SharedHandle<DownloadContext> dctx(new DownloadContext());
  436. loadFromMemory(memory, dctx, "default");
  437. const BDE& nodes = getNodes(dctx);
  438. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  439. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0][HOSTNAME].s());
  440. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[0][PORT].i());
  441. }
  442. {
  443. // port missing
  444. std::string memory =
  445. "d5:nodesl"
  446. "l11:192.168.0.1e"
  447. "l11:192.168.0.2i6882ee"
  448. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  449. "12:piece lengthi262144e"
  450. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  451. "ee";
  452. SharedHandle<DownloadContext> dctx(new DownloadContext());
  453. loadFromMemory(memory, dctx, "default");
  454. const BDE& nodes = getNodes(dctx);
  455. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  456. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0][HOSTNAME].s());
  457. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[0][PORT].i());
  458. }
  459. {
  460. // nodes is not a list
  461. std::string memory =
  462. "d5:nodes"
  463. "l11:192.168.0.1e"
  464. "4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  465. "12:piece lengthi262144e"
  466. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  467. "ee";
  468. SharedHandle<DownloadContext> dctx(new DownloadContext());
  469. loadFromMemory(memory, dctx, "default");
  470. CPPUNIT_ASSERT_EQUAL((size_t)0, getNodes(dctx).size());
  471. }
  472. {
  473. // the element of node is not Data
  474. std::string memory =
  475. "d5:nodesl"
  476. "ll11:192.168.0.1i6881eee"
  477. "l11:192.168.0.2i6882ee"
  478. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  479. "12:piece lengthi262144e"
  480. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  481. "ee";
  482. SharedHandle<DownloadContext> dctx(new DownloadContext());
  483. loadFromMemory(memory, dctx, "default");
  484. const BDE& nodes = getNodes(dctx);
  485. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  486. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0][HOSTNAME].s());
  487. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[0][PORT].i());
  488. }
  489. }
  490. void BittorrentHelperTest::testGetBasePath()
  491. {
  492. SharedHandle<DownloadContext> singleCtx(new DownloadContext());
  493. load("single.torrent", singleCtx);
  494. singleCtx->setFilePathWithIndex(1, "new-path");
  495. CPPUNIT_ASSERT_EQUAL(std::string("new-path"), singleCtx->getBasePath());
  496. SharedHandle<DownloadContext> multiCtx(new DownloadContext());
  497. multiCtx->setDir("downloads");
  498. load("test.torrent", multiCtx);
  499. CPPUNIT_ASSERT_EQUAL(std::string("downloads/aria2-test"),
  500. multiCtx->getBasePath());
  501. }
  502. void BittorrentHelperTest::testSetFileFilter_single()
  503. {
  504. SharedHandle<DownloadContext> dctx(new DownloadContext());
  505. load("single.torrent", dctx);
  506. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  507. dctx->setFileFilter(util::parseIntRange(""));
  508. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  509. dctx->setFileFilter(util::parseIntRange("1"));
  510. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  511. // For single file torrent, file is always selected whatever range
  512. // is passed.
  513. dctx->setFileFilter(util::parseIntRange("2"));
  514. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  515. }
  516. void BittorrentHelperTest::testSetFileFilter_multi()
  517. {
  518. SharedHandle<DownloadContext> dctx(new DownloadContext());
  519. load("test.torrent", dctx);
  520. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  521. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  522. dctx->setFileFilter(util::parseIntRange(""));
  523. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  524. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  525. dctx->setFileFilter(util::parseIntRange("2"));
  526. CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
  527. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  528. dctx->setFileFilter(util::parseIntRange("3"));
  529. CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
  530. CPPUNIT_ASSERT(!dctx->getFileEntries()[1]->isRequested());
  531. dctx->setFileFilter(util::parseIntRange("1,2"));
  532. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  533. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  534. }
  535. void BittorrentHelperTest::testUTF8Torrent()
  536. {
  537. SharedHandle<DownloadContext> dctx(new DownloadContext());
  538. load("utf8.torrent", dctx);
  539. CPPUNIT_ASSERT_EQUAL(std::string("name in utf-8"), getName(dctx));
  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. dctx->getAttribute(BITTORRENT)[COMMENT].s());
  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. dctx->getAttribute(BITTORRENT)[COMMENT].s());
  551. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  552. dctx->getAttribute(BITTORRENT)[CREATED_BY].s());
  553. CPPUNIT_ASSERT_EQUAL((int64_t)1123456789,
  554. dctx->getAttribute(BITTORRENT)[CREATION_DATE].i());
  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. BDE tr = bencode::decode(torrentData);
  590. BDE infoDic = tr["info"];
  591. std::string metadata = bencode::encode(infoDic);
  592. const BDE& attrs = dctx->getAttribute(bittorrent::BITTORRENT);
  593. CPPUNIT_ASSERT(metadata == attrs[bittorrent::METADATA].s());
  594. CPPUNIT_ASSERT_EQUAL(metadata.size(),
  595. (size_t)attrs[bittorrent::METADATA_SIZE].i());
  596. }
  597. void BittorrentHelperTest::testParseMagnetLink()
  598. {
  599. SharedHandle<DownloadContext> dctx(new DownloadContext());
  600. std::string magnet =
  601. "magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c&dn=aria2";
  602. bittorrent::parseMagnetLink(magnet, dctx);
  603. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  604. bittorrent::getInfoHashString(dctx));
  605. BDE attrs = dctx->getAttribute(bittorrent::BITTORRENT);
  606. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), attrs[bittorrent::NAME].s());
  607. magnet = "magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c";
  608. bittorrent::parseMagnetLink(magnet, dctx);
  609. attrs = dctx->getAttribute(bittorrent::BITTORRENT);
  610. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  611. attrs[bittorrent::NAME].s());
  612. }
  613. } // namespace bittorrent
  614. } // namespace aria2