BittorrentHelperTest.cc 27 KB

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