BittorrentHelperTest.cc 23 KB

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