DefaultBtContextTest.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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(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. std::deque<size_t> fastSet = btContext.computeFastSet(ipaddr, fastSetSize);
  198. //for_each(fastSet.begin(), fastSet.end(), Printer());
  199. //cerr << endl;
  200. size_t ans1[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
  201. std::deque<size_t> ansSet1(&ans1[0], &ans1[10]);
  202. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet1.begin()));
  203. ipaddr = "10.0.0.1";
  204. fastSet = btContext.computeFastSet(ipaddr, fastSetSize);
  205. size_t ans2[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
  206. std::deque<size_t> ansSet2(&ans2[0], &ans2[10]);
  207. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet2.begin()));
  208. }
  209. void DefaultBtContextTest::testGetFileEntries_multiFileUrlList() {
  210. DefaultBtContext btContext;
  211. btContext.load("url-list-multiFile.torrent");
  212. // This is multi-file torrent.
  213. std::deque<SharedHandle<FileEntry> > fileEntries = btContext.getFileEntries();
  214. // There are 2 file entries.
  215. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  216. std::deque<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin();
  217. SharedHandle<FileEntry> fileEntry1 = *itr;
  218. CPPUNIT_ASSERT_EQUAL(std::string("aria2/src/aria2c"),
  219. fileEntry1->getPath());
  220. std::deque<std::string> uris1 = fileEntry1->getAssociatedUris();
  221. CPPUNIT_ASSERT_EQUAL((size_t)2, uris1.size());
  222. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test/aria2/src/aria2c"),
  223. uris1[0]);
  224. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test/aria2/src/aria2c"),
  225. uris1[1]);
  226. itr++;
  227. SharedHandle<FileEntry> fileEntry2 = *itr;
  228. CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.2.2.tar.bz2"),
  229. fileEntry2->getPath());
  230. std::deque<std::string> uris2 = fileEntry2->getAssociatedUris();
  231. CPPUNIT_ASSERT_EQUAL((size_t)2, uris2.size());
  232. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test/aria2-0.2.2.tar.bz2"),
  233. uris2[0]);
  234. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test/aria2-0.2.2.tar.bz2"),
  235. uris2[1]);
  236. }
  237. void DefaultBtContextTest::testGetFileEntries_singleFileUrlList() {
  238. DefaultBtContext btContext;
  239. btContext.load("url-list-singleFile.torrent");
  240. // This is multi-file torrent.
  241. std::deque<SharedHandle<FileEntry> > fileEntries = btContext.getFileEntries();
  242. // There are 1 file entries.
  243. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  244. SharedHandle<FileEntry> fileEntry1 = fileEntries.front();
  245. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  246. fileEntry1->getPath());
  247. std::deque<std::string> uris1 = fileEntry1->getAssociatedUris();
  248. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  249. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"),
  250. uris1[0]);
  251. }
  252. void DefaultBtContextTest::testLoadFromMemory()
  253. {
  254. 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";
  255. DefaultBtContext btContext;
  256. btContext.loadFromMemory(memory, "default");
  257. std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  258. CPPUNIT_ASSERT_EQUAL((size_t)20, btContext.getInfoHashLength());
  259. CPPUNIT_ASSERT_EQUAL(correctHash, Util::toHex(btContext.getInfoHash(),
  260. btContext.getInfoHashLength()));
  261. }
  262. void DefaultBtContextTest::testLoadFromMemory_somethingMissing()
  263. {
  264. // pieces missing
  265. try {
  266. std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee";
  267. DefaultBtContext btContext;
  268. btContext.loadFromMemory(memory, "default");
  269. CPPUNIT_FAIL("exception must be thrown.");
  270. } catch(Exception* e) {
  271. std::cerr << *e << std::endl;
  272. delete e;
  273. }
  274. }
  275. void DefaultBtContextTest::testGetNodes()
  276. {
  277. {
  278. std::string memory =
  279. "d5:nodesl"
  280. "l11:192.168.0.1i6881ee"
  281. "l11:192.168.0.24:6882e"
  282. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  283. "12:piece lengthi262144e"
  284. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  285. "ee";
  286. DefaultBtContext btContext;
  287. btContext.loadFromMemory(memory, "default");
  288. const std::deque<std::pair<std::string, uint16_t> >& nodes =
  289. btContext.getNodes();
  290. CPPUNIT_ASSERT_EQUAL((size_t)2, nodes.size());
  291. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), nodes[0].first);
  292. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, nodes[0].second);
  293. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[1].first);
  294. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[1].second);
  295. }
  296. {
  297. // empty hostname
  298. std::string memory =
  299. "d5:nodesl"
  300. "l1: i6881ee"
  301. "l11:192.168.0.24:6882e"
  302. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  303. "12:piece lengthi262144e"
  304. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  305. "ee";
  306. DefaultBtContext btContext;
  307. btContext.loadFromMemory(memory, "default");
  308. const std::deque<std::pair<std::string, uint16_t> >& nodes =
  309. btContext.getNodes();
  310. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  311. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0].first);
  312. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[0].second);
  313. }
  314. {
  315. // bad port
  316. std::string memory =
  317. "d5:nodesl"
  318. "l11:192.168.0.11:xe"
  319. "l11:192.168.0.24:6882e"
  320. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  321. "12:piece lengthi262144e"
  322. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  323. "ee";
  324. DefaultBtContext btContext;
  325. btContext.loadFromMemory(memory, "default");
  326. const std::deque<std::pair<std::string, uint16_t> >& nodes =
  327. btContext.getNodes();
  328. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  329. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0].first);
  330. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[0].second);
  331. }
  332. {
  333. // port missing
  334. std::string memory =
  335. "d5:nodesl"
  336. "l11:192.168.0.1e"
  337. "l11:192.168.0.24:6882e"
  338. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  339. "12:piece lengthi262144e"
  340. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  341. "ee";
  342. DefaultBtContext btContext;
  343. btContext.loadFromMemory(memory, "default");
  344. const std::deque<std::pair<std::string, uint16_t> >& nodes =
  345. btContext.getNodes();
  346. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  347. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0].first);
  348. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[0].second);
  349. }
  350. {
  351. // nodes is not a list
  352. std::string memory =
  353. "d5:nodes"
  354. "l11:192.168.0.1e"
  355. "4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  356. "12:piece lengthi262144e"
  357. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  358. "ee";
  359. DefaultBtContext btContext;
  360. btContext.loadFromMemory(memory, "default");
  361. const std::deque<std::pair<std::string, uint16_t> >& nodes =
  362. btContext.getNodes();
  363. CPPUNIT_ASSERT_EQUAL((size_t)0, nodes.size());
  364. }
  365. {
  366. // the element of node is not Data
  367. std::string memory =
  368. "d5:nodesl"
  369. "ll11:192.168.0.1i6881eee"
  370. "l11:192.168.0.24:6882e"
  371. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  372. "12:piece lengthi262144e"
  373. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  374. "ee";
  375. DefaultBtContext btContext;
  376. btContext.loadFromMemory(memory, "default");
  377. const std::deque<std::pair<std::string, uint16_t> >& nodes =
  378. btContext.getNodes();
  379. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  380. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0].first);
  381. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[0].second);
  382. }
  383. }
  384. } // namespace aria2