BittorrentHelperTest.cc 24 KB

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