DefaultBtContextTest.cc 19 KB

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