BittorrentHelperTest.cc 26 KB

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