DefaultBtContextTest.cc 15 KB

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