DefaultBtContextTest.cc 15 KB

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