BittorrentHelperTest.cc 24 KB

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