DefaultBtContextTest.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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((int32_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((long long int)384,
  115. btContext.getTotalLength());
  116. }
  117. void DefaultBtContextTest::testGetTotalLengthSingle() {
  118. DefaultBtContext btContext;
  119. btContext.load("single.torrent");
  120. CPPUNIT_ASSERT_EQUAL((long long int)384,
  121. btContext.getTotalLength());
  122. }
  123. void DefaultBtContextTest::testGetFileModeMulti() {
  124. DefaultBtContext btContext;
  125. btContext.load("test.torrent");
  126. CPPUNIT_ASSERT_EQUAL(BtContext::MULTI,
  127. btContext.getFileMode());
  128. }
  129. void DefaultBtContextTest::testGetFileModeSingle() {
  130. DefaultBtContext btContext;
  131. btContext.load("single.torrent");
  132. CPPUNIT_ASSERT_EQUAL(BtContext::SINGLE,
  133. btContext.getFileMode());
  134. }
  135. void DefaultBtContextTest::testGetNameMulti() {
  136. DefaultBtContext btContext;
  137. btContext.load("test.torrent");
  138. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"),
  139. btContext.getName());
  140. }
  141. void DefaultBtContextTest::testGetNameSingle() {
  142. DefaultBtContext btContext;
  143. btContext.load("single.torrent");
  144. CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"),
  145. btContext.getName());
  146. }
  147. void DefaultBtContextTest::testGetAnnounceTier() {
  148. DefaultBtContext btContext;
  149. btContext.load("single.torrent");
  150. std::deque<SharedHandle<AnnounceTier> > tiers = btContext.getAnnounceTiers();
  151. // There is 1 tier.
  152. CPPUNIT_ASSERT_EQUAL((size_t)1,
  153. tiers.size());
  154. std::deque<SharedHandle<AnnounceTier> >::iterator itr = tiers.begin();
  155. SharedHandle<AnnounceTier> tier1 = *itr;
  156. CPPUNIT_ASSERT_EQUAL((size_t)1,
  157. tier1->urls.size());
  158. CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.com/announce.php"),
  159. tier1->urls.at(0));
  160. }
  161. void DefaultBtContextTest::testGetAnnounceTierAnnounceList() {
  162. DefaultBtContext btContext;
  163. btContext.load("test.torrent");
  164. std::deque<SharedHandle<AnnounceTier> > tiers = btContext.getAnnounceTiers();
  165. // There are 3 tiers.
  166. CPPUNIT_ASSERT_EQUAL((size_t)3,
  167. tiers.size());
  168. SharedHandle<AnnounceTier> tier1 = tiers.at(0);
  169. CPPUNIT_ASSERT_EQUAL((size_t)1,
  170. tier1->urls.size());
  171. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  172. tier1->urls.at(0));
  173. SharedHandle<AnnounceTier> tier2 = tiers.at(1);
  174. CPPUNIT_ASSERT_EQUAL((size_t)1,
  175. tier2->urls.size());
  176. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
  177. tier2->urls.at(0));
  178. SharedHandle<AnnounceTier> tier3 = tiers.at(2);
  179. CPPUNIT_ASSERT_EQUAL((size_t)1,
  180. tier3->urls.size());
  181. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"),
  182. tier3->urls.at(0));
  183. }
  184. void DefaultBtContextTest::testGetPieceLength() {
  185. DefaultBtContext btContext;
  186. btContext.load("test.torrent");
  187. CPPUNIT_ASSERT_EQUAL((int32_t)128,
  188. 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(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 pieces = 1000;
  208. int fastSetSize = 10;
  209. DefaultBtContext btContext;
  210. btContext.setInfoHash(infoHash);
  211. btContext.setNumPieces(pieces);
  212. std::deque<int32_t> fastSet = btContext.computeFastSet(ipaddr, fastSetSize);
  213. //for_each(fastSet.begin(), fastSet.end(), Printer());
  214. //cerr << endl;
  215. int ans1[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
  216. std::deque<int32_t> ansSet1(&ans1[0], &ans1[10]);
  217. CPPUNIT_ASSERT(equal(fastSet.begin(), fastSet.end(), ansSet1.begin()));
  218. ipaddr = "10.0.0.1";
  219. fastSet = btContext.computeFastSet(ipaddr, fastSetSize);
  220. int ans2[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
  221. std::deque<int32_t> ansSet2(&ans2[0], &ans2[10]);
  222. CPPUNIT_ASSERT(equal(fastSet.begin(), fastSet.end(), ansSet2.begin()));
  223. }
  224. void DefaultBtContextTest::testGetFileEntries_multiFileUrlList() {
  225. DefaultBtContext btContext;
  226. btContext.load("url-list-multiFile.torrent");
  227. // This is multi-file torrent.
  228. std::deque<SharedHandle<FileEntry> > fileEntries = btContext.getFileEntries();
  229. // There are 2 file entries.
  230. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  231. std::deque<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin();
  232. SharedHandle<FileEntry> fileEntry1 = *itr;
  233. CPPUNIT_ASSERT_EQUAL(std::string("aria2/src/aria2c"),
  234. fileEntry1->getPath());
  235. std::deque<std::string> uris1 = fileEntry1->getAssociatedUris();
  236. CPPUNIT_ASSERT_EQUAL((size_t)2, uris1.size());
  237. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test/aria2/src/aria2c"),
  238. uris1[0]);
  239. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test/aria2/src/aria2c"),
  240. uris1[1]);
  241. itr++;
  242. SharedHandle<FileEntry> fileEntry2 = *itr;
  243. CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.2.2.tar.bz2"),
  244. fileEntry2->getPath());
  245. std::deque<std::string> uris2 = fileEntry2->getAssociatedUris();
  246. CPPUNIT_ASSERT_EQUAL((size_t)2, uris2.size());
  247. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test/aria2-0.2.2.tar.bz2"),
  248. uris2[0]);
  249. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test/aria2-0.2.2.tar.bz2"),
  250. uris2[1]);
  251. }
  252. void DefaultBtContextTest::testGetFileEntries_singleFileUrlList() {
  253. DefaultBtContext btContext;
  254. btContext.load("url-list-singleFile.torrent");
  255. // This is multi-file torrent.
  256. std::deque<SharedHandle<FileEntry> > fileEntries = btContext.getFileEntries();
  257. // There are 1 file entries.
  258. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  259. SharedHandle<FileEntry> fileEntry1 = fileEntries.front();
  260. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  261. fileEntry1->getPath());
  262. std::deque<std::string> uris1 = fileEntry1->getAssociatedUris();
  263. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  264. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"),
  265. uris1[0]);
  266. }
  267. void DefaultBtContextTest::testLoadFromMemory()
  268. {
  269. 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";
  270. DefaultBtContext btContext;
  271. btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
  272. std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  273. CPPUNIT_ASSERT_EQUAL((int32_t)20, btContext.getInfoHashLength());
  274. CPPUNIT_ASSERT_EQUAL(correctHash, Util::toHex(btContext.getInfoHash(),
  275. btContext.getInfoHashLength()));
  276. }
  277. void DefaultBtContextTest::testLoadFromMemory_somethingMissing()
  278. {
  279. // pieces missing
  280. try {
  281. std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee";
  282. DefaultBtContext btContext;
  283. btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
  284. CPPUNIT_FAIL("exception must be thrown.");
  285. } catch(Exception* e) {
  286. std::cerr << *e << std::endl;
  287. delete e;
  288. }
  289. }
  290. void DefaultBtContextTest::testGetNodes()
  291. {
  292. {
  293. std::string memory =
  294. "d5:nodesl"
  295. "l11:192.168.0.1i6881ee"
  296. "l11:192.168.0.24:6882e"
  297. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  298. "12:piece lengthi262144e"
  299. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  300. "ee";
  301. DefaultBtContext btContext;
  302. btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
  303. const std::deque<std::pair<std::string, uint16_t> >& nodes =
  304. btContext.getNodes();
  305. CPPUNIT_ASSERT_EQUAL((size_t)2, nodes.size());
  306. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), nodes[0].first);
  307. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, nodes[0].second);
  308. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[1].first);
  309. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[1].second);
  310. }
  311. {
  312. // empty hostname
  313. std::string memory =
  314. "d5:nodesl"
  315. "l1: i6881ee"
  316. "l11:192.168.0.24:6882e"
  317. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  318. "12:piece lengthi262144e"
  319. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  320. "ee";
  321. DefaultBtContext btContext;
  322. btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
  323. const std::deque<std::pair<std::string, uint16_t> >& nodes =
  324. btContext.getNodes();
  325. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  326. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0].first);
  327. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[0].second);
  328. }
  329. {
  330. // bad port
  331. std::string memory =
  332. "d5:nodesl"
  333. "l11:192.168.0.11:xe"
  334. "l11:192.168.0.24:6882e"
  335. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  336. "12:piece lengthi262144e"
  337. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  338. "ee";
  339. DefaultBtContext btContext;
  340. btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
  341. const std::deque<std::pair<std::string, uint16_t> >& nodes =
  342. btContext.getNodes();
  343. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  344. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0].first);
  345. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[0].second);
  346. }
  347. {
  348. // port missing
  349. std::string memory =
  350. "d5:nodesl"
  351. "l11:192.168.0.1e"
  352. "l11:192.168.0.24:6882e"
  353. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  354. "12:piece lengthi262144e"
  355. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  356. "ee";
  357. DefaultBtContext btContext;
  358. btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
  359. const std::deque<std::pair<std::string, uint16_t> >& nodes =
  360. btContext.getNodes();
  361. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  362. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0].first);
  363. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[0].second);
  364. }
  365. {
  366. // nodes is not a list
  367. std::string memory =
  368. "d5:nodes"
  369. "l11:192.168.0.1e"
  370. "4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  371. "12:piece lengthi262144e"
  372. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  373. "ee";
  374. DefaultBtContext btContext;
  375. btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
  376. const std::deque<std::pair<std::string, uint16_t> >& nodes =
  377. btContext.getNodes();
  378. CPPUNIT_ASSERT_EQUAL((size_t)0, nodes.size());
  379. }
  380. {
  381. // the element of node is not Data
  382. std::string memory =
  383. "d5:nodesl"
  384. "ll11:192.168.0.1i6881eee"
  385. "l11:192.168.0.24:6882e"
  386. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  387. "12:piece lengthi262144e"
  388. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  389. "ee";
  390. DefaultBtContext btContext;
  391. btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
  392. const std::deque<std::pair<std::string, uint16_t> >& nodes =
  393. btContext.getNodes();
  394. CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size());
  395. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0].first);
  396. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[0].second);
  397. }
  398. }
  399. } // namespace aria2