DefaultBtContextTest.cc 15 KB

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