BittorrentHelperTest.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. SharedHandle<DownloadContext> dctx(new DownloadContext());
  234. CPPUNIT_ASSERT_EQUAL
  235. (std::string("-aria-AAAAAAAAAAAAAA"),
  236. generatePeerId("-aria-",
  237. SharedHandle<Randomizer>(new FixedNumberRandomizer())));
  238. }
  239. void BittorrentHelperTest::testComputeFastSet()
  240. {
  241. std::string ipaddr = "192.168.0.1";
  242. unsigned char infoHash[20];
  243. memset(infoHash, 0, sizeof(infoHash));
  244. infoHash[0] = 0xff;
  245. int fastSetSize = 10;
  246. size_t numPieces = 1000;
  247. {
  248. std::vector<size_t> fastSet;
  249. computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
  250. size_t ans[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
  251. std::vector<size_t> ansSet(&ans[0], &ans[arrayLength(ans)]);
  252. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
  253. }
  254. ipaddr = "10.0.0.1";
  255. {
  256. std::vector<size_t> fastSet;
  257. computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
  258. size_t ans[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
  259. std::vector<size_t> ansSet(&ans[0], &ans[arrayLength(ans)]);
  260. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
  261. }
  262. // See when pieces < fastSetSize
  263. numPieces = 9;
  264. {
  265. std::vector<size_t> fastSet;
  266. computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
  267. size_t ans[] = { 8, 6, 7, 5, 1, 4, 0, 2, 3 };
  268. std::vector<size_t> ansSet(&ans[0], &ans[arrayLength(ans)]);
  269. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
  270. }
  271. }
  272. void BittorrentHelperTest::testGetFileEntries_multiFileUrlList() {
  273. SharedHandle<DownloadContext> dctx(new DownloadContext());
  274. load("url-list-multiFile.torrent", dctx);
  275. // This is multi-file torrent.
  276. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  277. dctx->getFileEntries();
  278. // There are 2 file entries.
  279. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  280. std::vector<SharedHandle<FileEntry> >::const_iterator itr =
  281. fileEntries.begin();
  282. const SharedHandle<FileEntry>& fileEntry1 = *itr;
  283. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2/src/aria2c"),
  284. fileEntry1->getPath());
  285. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  286. CPPUNIT_ASSERT_EQUAL((size_t)2, uris1.size());
  287. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test/aria2/src/aria2c"),
  288. uris1[0]);
  289. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test/aria2/src/aria2c"),
  290. uris1[1]);
  291. ++itr;
  292. const SharedHandle<FileEntry>& fileEntry2 = *itr;
  293. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2-0.2.2.tar.bz2"),
  294. fileEntry2->getPath());
  295. const std::deque<std::string>& uris2 = fileEntry2->getRemainingUris();
  296. CPPUNIT_ASSERT_EQUAL((size_t)2, uris2.size());
  297. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test/aria2-0.2.2.tar.bz2"),
  298. uris2[0]);
  299. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test/aria2-0.2.2.tar.bz2"),
  300. uris2[1]);
  301. }
  302. void BittorrentHelperTest::testGetFileEntries_singleFileUrlList() {
  303. SharedHandle<DownloadContext> dctx(new DownloadContext());
  304. load("url-list-singleFile.torrent", dctx);
  305. // This is single-file torrent.
  306. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  307. dctx->getFileEntries();
  308. // There are 1 file entries.
  309. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  310. const SharedHandle<FileEntry>& fileEntry1 = fileEntries.front();
  311. CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"),
  312. fileEntry1->getPath());
  313. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  314. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  315. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"),
  316. uris1[0]);
  317. }
  318. void BittorrentHelperTest::testGetFileEntries_singleFileUrlListEndsWithSlash() {
  319. SharedHandle<DownloadContext> dctx(new DownloadContext());
  320. load("url-list-singleFileEndsWithSlash.torrent", dctx);
  321. // This is single-file torrent.
  322. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  323. dctx->getFileEntries();
  324. // There are 1 file entries.
  325. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  326. const SharedHandle<FileEntry>& fileEntry1 = fileEntries.front();
  327. CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"),
  328. fileEntry1->getPath());
  329. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  330. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  331. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"),
  332. uris1[0]);
  333. }
  334. void BittorrentHelperTest::testLoadFromMemory()
  335. {
  336. 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";
  337. SharedHandle<DownloadContext> dctx(new DownloadContext());
  338. loadFromMemory(memory, dctx, "default");
  339. std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  340. CPPUNIT_ASSERT_EQUAL(correctHash, getInfoHashString(dctx));
  341. }
  342. void BittorrentHelperTest::testLoadFromMemory_somethingMissing()
  343. {
  344. // pieces missing
  345. try {
  346. std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee";
  347. SharedHandle<DownloadContext> dctx(new DownloadContext());
  348. loadFromMemory(memory, dctx, "default");
  349. CPPUNIT_FAIL("exception must be thrown.");
  350. } catch(Exception& e) {
  351. // OK
  352. }
  353. }
  354. void BittorrentHelperTest::testLoadFromMemory_overrideName()
  355. {
  356. 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";
  357. SharedHandle<DownloadContext> dctx(new DownloadContext());
  358. loadFromMemory(memory, dctx, "default", "aria2-override.name");
  359. CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"), getName(dctx));
  360. }
  361. void BittorrentHelperTest::testLoadFromMemory_joinPathMulti()
  362. {
  363. std::string memory =
  364. "d8:announce27:http://example.com/announce4:infod5:filesld6:lengthi262144e4:pathl7:../dir14:dir28:file.imgeee4:name14:../name1/name212:piece lengthi262144e6:pieces20:00000000000000000000ee";
  365. SharedHandle<DownloadContext> dctx(new DownloadContext());
  366. dctx->setDir("/tmp");
  367. loadFromMemory(memory, dctx, "default");
  368. // remove ".." element
  369. CPPUNIT_ASSERT_EQUAL(std::string("../name1/name2"), getName(dctx));
  370. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/name1/dir1/dir2/file.img"),
  371. dctx->getFirstFileEntry()->getPath());
  372. }
  373. void BittorrentHelperTest::testLoadFromMemory_joinPathSingle()
  374. {
  375. std::string memory =
  376. "d8:announce27:http://example.com/announce4:infod4:name14:../name1/name26:lengthi262144e12:piece lengthi262144e6:pieces20:00000000000000000000ee";
  377. SharedHandle<DownloadContext> dctx(new DownloadContext());
  378. dctx->setDir("/tmp");
  379. loadFromMemory(memory, dctx, "default");
  380. CPPUNIT_ASSERT_EQUAL(std::string("../name1/name2"), getName(dctx));
  381. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/name1/name2"),
  382. dctx->getFirstFileEntry()->getPath());
  383. }
  384. void BittorrentHelperTest::testGetNodes()
  385. {
  386. {
  387. std::string memory =
  388. "d5:nodesl"
  389. "l11:192.168.0.1i6881ee"
  390. "l11:192.168.0.2i6882ee"
  391. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  392. "12:piece lengthi262144e"
  393. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  394. "ee";
  395. SharedHandle<DownloadContext> dctx(new DownloadContext());
  396. loadFromMemory(memory, dctx, "default");
  397. const BDE& nodes = getNodes(dctx);
  398. CPPUNIT_ASSERT_EQUAL((size_t)2, nodes.size());
  399. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), nodes[0][HOSTNAME].s());
  400. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, (uint16_t)nodes[0][PORT].i());
  401. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[1][HOSTNAME].s());
  402. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[1][PORT].i());
  403. }
  404. {
  405. // empty hostname
  406. std::string memory =
  407. "d5:nodesl"
  408. "l1: i6881ee"
  409. "l11:192.168.0.2i6882ee"
  410. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  411. "12:piece lengthi262144e"
  412. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  413. "ee";
  414. SharedHandle<DownloadContext> dctx(new DownloadContext());
  415. loadFromMemory(memory, dctx, "default");
  416. const BDE& nodes = getNodes(dctx);
  417. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  418. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0][HOSTNAME].s());
  419. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[0][PORT].i());
  420. }
  421. {
  422. // bad port
  423. std::string memory =
  424. "d5:nodesl"
  425. "l11:192.168.0.11:xe"
  426. "l11:192.168.0.2i6882ee"
  427. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  428. "12:piece lengthi262144e"
  429. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  430. "ee";
  431. SharedHandle<DownloadContext> dctx(new DownloadContext());
  432. loadFromMemory(memory, dctx, "default");
  433. const BDE& nodes = getNodes(dctx);
  434. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  435. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0][HOSTNAME].s());
  436. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[0][PORT].i());
  437. }
  438. {
  439. // port missing
  440. std::string memory =
  441. "d5:nodesl"
  442. "l11:192.168.0.1e"
  443. "l11:192.168.0.2i6882ee"
  444. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  445. "12:piece lengthi262144e"
  446. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  447. "ee";
  448. SharedHandle<DownloadContext> dctx(new DownloadContext());
  449. loadFromMemory(memory, dctx, "default");
  450. const BDE& nodes = getNodes(dctx);
  451. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  452. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0][HOSTNAME].s());
  453. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[0][PORT].i());
  454. }
  455. {
  456. // nodes is not a list
  457. std::string memory =
  458. "d5:nodes"
  459. "l11:192.168.0.1e"
  460. "4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  461. "12:piece lengthi262144e"
  462. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  463. "ee";
  464. SharedHandle<DownloadContext> dctx(new DownloadContext());
  465. loadFromMemory(memory, dctx, "default");
  466. CPPUNIT_ASSERT_EQUAL((size_t)0, getNodes(dctx).size());
  467. }
  468. {
  469. // the element of node is not Data
  470. std::string memory =
  471. "d5:nodesl"
  472. "ll11:192.168.0.1i6881eee"
  473. "l11:192.168.0.2i6882ee"
  474. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  475. "12:piece lengthi262144e"
  476. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  477. "ee";
  478. SharedHandle<DownloadContext> dctx(new DownloadContext());
  479. loadFromMemory(memory, dctx, "default");
  480. const BDE& nodes = getNodes(dctx);
  481. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  482. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0][HOSTNAME].s());
  483. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[0][PORT].i());
  484. }
  485. }
  486. void BittorrentHelperTest::testGetBasePath()
  487. {
  488. SharedHandle<DownloadContext> singleCtx(new DownloadContext());
  489. load("single.torrent", singleCtx);
  490. singleCtx->setFilePathWithIndex(1, "new-path");
  491. CPPUNIT_ASSERT_EQUAL(std::string("new-path"), singleCtx->getBasePath());
  492. SharedHandle<DownloadContext> multiCtx(new DownloadContext());
  493. multiCtx->setDir("downloads");
  494. load("test.torrent", multiCtx);
  495. CPPUNIT_ASSERT_EQUAL(std::string("downloads/aria2-test"),
  496. multiCtx->getBasePath());
  497. }
  498. void BittorrentHelperTest::testSetFileFilter_single()
  499. {
  500. SharedHandle<DownloadContext> dctx(new DownloadContext());
  501. load("single.torrent", dctx);
  502. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  503. dctx->setFileFilter(Util::parseIntRange(""));
  504. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  505. dctx->setFileFilter(Util::parseIntRange("1"));
  506. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  507. // For single file torrent, file is always selected whatever range
  508. // is passed.
  509. dctx->setFileFilter(Util::parseIntRange("2"));
  510. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  511. }
  512. void BittorrentHelperTest::testSetFileFilter_multi()
  513. {
  514. SharedHandle<DownloadContext> dctx(new DownloadContext());
  515. load("test.torrent", dctx);
  516. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  517. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  518. dctx->setFileFilter(Util::parseIntRange(""));
  519. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  520. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  521. dctx->setFileFilter(Util::parseIntRange("2"));
  522. CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
  523. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  524. dctx->setFileFilter(Util::parseIntRange("3"));
  525. CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
  526. CPPUNIT_ASSERT(!dctx->getFileEntries()[1]->isRequested());
  527. dctx->setFileFilter(Util::parseIntRange("1,2"));
  528. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  529. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  530. }
  531. void BittorrentHelperTest::testUTF8Torrent()
  532. {
  533. SharedHandle<DownloadContext> dctx(new DownloadContext());
  534. load("utf8.torrent", dctx);
  535. CPPUNIT_ASSERT_EQUAL(std::string("name in utf-8"), getName(dctx));
  536. CPPUNIT_ASSERT_EQUAL(std::string("./name in utf-8/path in utf-8"),
  537. dctx->getFirstFileEntry()->getPath());
  538. CPPUNIT_ASSERT_EQUAL(std::string("This is utf8 comment."),
  539. dctx->getAttribute(BITTORRENT)[COMMENT].s());
  540. }
  541. void BittorrentHelperTest::testMetaData()
  542. {
  543. SharedHandle<DownloadContext> dctx(new DownloadContext());
  544. load("test.torrent", dctx);
  545. CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
  546. dctx->getAttribute(BITTORRENT)[COMMENT].s());
  547. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  548. dctx->getAttribute(BITTORRENT)[CREATED_BY].s());
  549. CPPUNIT_ASSERT_EQUAL((int64_t)1123456789,
  550. dctx->getAttribute(BITTORRENT)[CREATION_DATE].i());
  551. }
  552. void BittorrentHelperTest::testCreatecompact()
  553. {
  554. unsigned char compact[6];
  555. // Note: bittorrent::createcompact() on linux can handle IPv4-mapped
  556. // addresses like `ffff::127.0.0.1', but on cygwin, it doesn't.
  557. CPPUNIT_ASSERT(createcompact(compact, "127.0.0.1", 6881));
  558. std::pair<std::string, uint16_t> p = unpackcompact(compact);
  559. CPPUNIT_ASSERT_EQUAL(std::string("127.0.0.1"), p.first);
  560. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, p.second);
  561. }
  562. void BittorrentHelperTest::testCheckBitfield()
  563. {
  564. unsigned char bitfield[] = { 0xff, 0xe0 };
  565. checkBitfield(bitfield, sizeof(bitfield), 11);
  566. try {
  567. checkBitfield(bitfield, sizeof(bitfield), 17);
  568. CPPUNIT_FAIL("exception must be thrown.");
  569. } catch(RecoverableException& e) {
  570. // success
  571. }
  572. // Change last byte
  573. bitfield[1] = 0xf0;
  574. try {
  575. checkBitfield(bitfield, sizeof(bitfield), 11);
  576. CPPUNIT_FAIL("exception must be thrown.");
  577. } catch(RecoverableException& e) {
  578. // success
  579. }
  580. }
  581. } // namespace bittorrent
  582. } // namespace aria2