DefaultBtContextTest.cc 15 KB

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