BittorrentHelperTest.cc 22 KB

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