DefaultBtContextTest.cc 19 KB

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