BittorrentHelperTest.cc 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. #include "bittorrent_helper.h"
  2. #include <cstring>
  3. #include <iostream>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. #include "DownloadContext.h"
  6. #include "util.h"
  7. #include "RecoverableException.h"
  8. #include "AnnounceTier.h"
  9. #include "FixedNumberRandomizer.h"
  10. #include "FileEntry.h"
  11. #include "array_fun.h"
  12. #include "a2netcompat.h"
  13. #include "bencode2.h"
  14. #include "TestUtil.h"
  15. #include "base32.h"
  16. #include "Option.h"
  17. #include "prefs.h"
  18. namespace aria2 {
  19. namespace bittorrent {
  20. class BittorrentHelperTest:public CppUnit::TestFixture {
  21. CPPUNIT_TEST_SUITE(BittorrentHelperTest);
  22. CPPUNIT_TEST(testGetInfoHash);
  23. CPPUNIT_TEST(testGetPieceHash);
  24. CPPUNIT_TEST(testGetFileEntries);
  25. CPPUNIT_TEST(testGetTotalLength);
  26. CPPUNIT_TEST(testGetFileEntriesSingle);
  27. CPPUNIT_TEST(testGetTotalLengthSingle);
  28. CPPUNIT_TEST(testGetFileModeMulti);
  29. CPPUNIT_TEST(testGetFileModeSingle);
  30. CPPUNIT_TEST(testGetNameMulti);
  31. CPPUNIT_TEST(testGetNameSingle);
  32. CPPUNIT_TEST(testOverrideName);
  33. CPPUNIT_TEST(testGetAnnounceTier);
  34. CPPUNIT_TEST(testGetAnnounceTierAnnounceList);
  35. CPPUNIT_TEST(testGetPieceLength);
  36. CPPUNIT_TEST(testGetInfoHashAsString);
  37. CPPUNIT_TEST(testGetPeerId);
  38. CPPUNIT_TEST(testComputeFastSet);
  39. CPPUNIT_TEST(testGetFileEntries_multiFileUrlList);
  40. CPPUNIT_TEST(testGetFileEntries_singleFileUrlList);
  41. CPPUNIT_TEST(testGetFileEntries_singleFileUrlListEndsWithSlash);
  42. CPPUNIT_TEST(testLoadFromMemory);
  43. CPPUNIT_TEST(testLoadFromMemory_somethingMissing);
  44. CPPUNIT_TEST(testLoadFromMemory_overrideName);
  45. CPPUNIT_TEST(testLoadFromMemory_multiFileDirTraversal);
  46. CPPUNIT_TEST(testLoadFromMemory_singleFileDirTraversal);
  47. CPPUNIT_TEST(testLoadFromMemory_multiFileNonUtf8Path);
  48. CPPUNIT_TEST(testLoadFromMemory_singleFileNonUtf8Path);
  49. CPPUNIT_TEST(testGetNodes);
  50. CPPUNIT_TEST(testGetBasePath);
  51. CPPUNIT_TEST(testSetFileFilter_single);
  52. CPPUNIT_TEST(testSetFileFilter_multi);
  53. CPPUNIT_TEST(testUTF8Torrent);
  54. CPPUNIT_TEST(testEtc);
  55. CPPUNIT_TEST(testCheckBitfield);
  56. CPPUNIT_TEST(testMetadata);
  57. CPPUNIT_TEST(testParseMagnet);
  58. CPPUNIT_TEST(testParseMagnet_base32);
  59. CPPUNIT_TEST(testMetadata2Torrent);
  60. CPPUNIT_TEST(testTorrent2Magnet);
  61. CPPUNIT_TEST(testExtractPeerFromString);
  62. CPPUNIT_TEST(testExtractPeerFromList);
  63. CPPUNIT_TEST(testExtract2PeersFromList);
  64. CPPUNIT_TEST(testPackcompact);
  65. CPPUNIT_TEST(testUnpackcompact);
  66. CPPUNIT_TEST(testRemoveAnnounceUri);
  67. CPPUNIT_TEST(testAddAnnounceUri);
  68. CPPUNIT_TEST(testAdjustAnnounceUri);
  69. CPPUNIT_TEST_SUITE_END();
  70. public:
  71. SharedHandle<Option> option_;
  72. void setUp()
  73. {
  74. option_.reset(new Option());
  75. option_->put(PREF_DIR, ".");
  76. }
  77. void testGetInfoHash();
  78. void testGetPieceHash();
  79. void testGetFileEntries();
  80. void testGetTotalLength();
  81. void testGetFileEntriesSingle();
  82. void testGetTotalLengthSingle();
  83. void testGetFileModeMulti();
  84. void testGetFileModeSingle();
  85. void testGetNameMulti();
  86. void testGetNameSingle();
  87. void testOverrideName();
  88. void testGetAnnounceTier();
  89. void testGetAnnounceTierAnnounceList();
  90. void testGetPieceLength();
  91. void testGetInfoHashAsString();
  92. void testGetPeerId();
  93. void testComputeFastSet();
  94. void testGetFileEntries_multiFileUrlList();
  95. void testGetFileEntries_singleFileUrlList();
  96. void testGetFileEntries_singleFileUrlListEndsWithSlash();
  97. void testLoadFromMemory();
  98. void testLoadFromMemory_somethingMissing();
  99. void testLoadFromMemory_overrideName();
  100. void testLoadFromMemory_multiFileDirTraversal();
  101. void testLoadFromMemory_singleFileDirTraversal();
  102. void testLoadFromMemory_multiFileNonUtf8Path();
  103. void testLoadFromMemory_singleFileNonUtf8Path();
  104. void testGetNodes();
  105. void testGetBasePath();
  106. void testSetFileFilter_single();
  107. void testSetFileFilter_multi();
  108. void testUTF8Torrent();
  109. void testEtc();
  110. void testCheckBitfield();
  111. void testMetadata();
  112. void testParseMagnet();
  113. void testParseMagnet_base32();
  114. void testMetadata2Torrent();
  115. void testTorrent2Magnet();
  116. void testExtractPeerFromString();
  117. void testExtractPeerFromList();
  118. void testExtract2PeersFromList();
  119. void testPackcompact();
  120. void testUnpackcompact();
  121. void testRemoveAnnounceUri();
  122. void testAddAnnounceUri();
  123. void testAdjustAnnounceUri();
  124. };
  125. CPPUNIT_TEST_SUITE_REGISTRATION(BittorrentHelperTest);
  126. void BittorrentHelperTest::testGetInfoHash() {
  127. SharedHandle<DownloadContext> dctx(new DownloadContext());
  128. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  129. std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  130. CPPUNIT_ASSERT_EQUAL(correctHash, bittorrent::getInfoHashString(dctx));
  131. }
  132. void BittorrentHelperTest::testGetPieceHash() {
  133. SharedHandle<DownloadContext> dctx(new DownloadContext());
  134. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  135. CPPUNIT_ASSERT_EQUAL(std::string("AAAAAAAAAAAAAAAAAAAA"),
  136. dctx->getPieceHash(0));
  137. CPPUNIT_ASSERT_EQUAL(std::string("BBBBBBBBBBBBBBBBBBBB"),
  138. dctx->getPieceHash(1));
  139. CPPUNIT_ASSERT_EQUAL(std::string("CCCCCCCCCCCCCCCCCCCC"),
  140. dctx->getPieceHash(2));
  141. CPPUNIT_ASSERT_EQUAL(std::string(""),
  142. dctx->getPieceHash(3));
  143. CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashType());
  144. }
  145. void BittorrentHelperTest::testGetFileEntries() {
  146. SharedHandle<DownloadContext> dctx(new DownloadContext());
  147. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  148. // This is multi-file torrent.
  149. std::vector<SharedHandle<FileEntry> > fileEntries = dctx->getFileEntries();
  150. // There are 2 file entries.
  151. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  152. std::vector<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin();
  153. SharedHandle<FileEntry> fileEntry1 = *itr;
  154. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2/src/aria2c"),
  155. fileEntry1->getPath());
  156. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test/aria2/src/aria2c"),
  157. fileEntry1->getOriginalName());
  158. itr++;
  159. SharedHandle<FileEntry> fileEntry2 = *itr;
  160. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2-0.2.2.tar.bz2"),
  161. fileEntry2->getPath());
  162. }
  163. void BittorrentHelperTest::testGetFileEntriesSingle() {
  164. SharedHandle<DownloadContext> dctx(new DownloadContext());
  165. load(A2_TEST_DIR"/single.torrent", dctx, option_);
  166. // This is multi-file torrent.
  167. std::vector<SharedHandle<FileEntry> > fileEntries = dctx->getFileEntries();
  168. // There is 1 file entry.
  169. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  170. std::vector<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin();
  171. SharedHandle<FileEntry> fileEntry1 = *itr;
  172. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"),
  173. fileEntry1->getPath());
  174. CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"),
  175. fileEntry1->getOriginalName());
  176. }
  177. void BittorrentHelperTest::testGetTotalLength() {
  178. SharedHandle<DownloadContext> dctx(new DownloadContext());
  179. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  180. CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, dctx->getTotalLength());
  181. }
  182. void BittorrentHelperTest::testGetTotalLengthSingle() {
  183. SharedHandle<DownloadContext> dctx(new DownloadContext());
  184. load(A2_TEST_DIR"/single.torrent", dctx, option_);
  185. CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, dctx->getTotalLength());
  186. }
  187. void BittorrentHelperTest::testGetFileModeMulti() {
  188. SharedHandle<DownloadContext> dctx(new DownloadContext());
  189. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  190. CPPUNIT_ASSERT_EQUAL(MULTI, getTorrentAttrs(dctx)->mode);
  191. }
  192. void BittorrentHelperTest::testGetFileModeSingle() {
  193. SharedHandle<DownloadContext> dctx(new DownloadContext());
  194. load(A2_TEST_DIR"/single.torrent", dctx, option_);
  195. CPPUNIT_ASSERT_EQUAL(SINGLE, getTorrentAttrs(dctx)->mode);
  196. }
  197. void BittorrentHelperTest::testGetNameMulti() {
  198. SharedHandle<DownloadContext> dctx(new DownloadContext());
  199. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  200. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"), getTorrentAttrs(dctx)->name);
  201. }
  202. void BittorrentHelperTest::testGetNameSingle() {
  203. SharedHandle<DownloadContext> dctx(new DownloadContext());
  204. load(A2_TEST_DIR"/single.torrent", dctx, option_);
  205. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"),
  206. dctx->getBasePath());
  207. CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"),
  208. getTorrentAttrs(dctx)->name);
  209. }
  210. void BittorrentHelperTest::testOverrideName()
  211. {
  212. SharedHandle<DownloadContext> dctx(new DownloadContext());
  213. load(A2_TEST_DIR"/test.torrent", dctx, option_, "aria2-override.name");
  214. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-override.name"),
  215. dctx->getBasePath());
  216. CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"),
  217. getTorrentAttrs(dctx)->name);
  218. }
  219. void BittorrentHelperTest::testGetAnnounceTier() {
  220. SharedHandle<DownloadContext> dctx(new DownloadContext());
  221. load(A2_TEST_DIR"/single.torrent", dctx, option_);
  222. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  223. // There is 1 tier.
  224. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList.size());
  225. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[0].size());
  226. CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.com/announce.php"),
  227. attrs->announceList[0][0]);
  228. }
  229. void BittorrentHelperTest::testGetAnnounceTierAnnounceList() {
  230. SharedHandle<DownloadContext> dctx(new DownloadContext());
  231. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  232. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  233. // There are 3 tiers.
  234. CPPUNIT_ASSERT_EQUAL((size_t)3, attrs->announceList.size());
  235. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[0].size());
  236. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  237. attrs->announceList[0][0]);
  238. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[1].size());
  239. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
  240. attrs->announceList[1][0]);
  241. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[2].size());
  242. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"),
  243. attrs->announceList[2][0]);
  244. }
  245. void BittorrentHelperTest::testGetPieceLength() {
  246. SharedHandle<DownloadContext> dctx(new DownloadContext());
  247. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  248. CPPUNIT_ASSERT_EQUAL((size_t)128, dctx->getPieceLength());
  249. }
  250. void BittorrentHelperTest::testGetInfoHashAsString() {
  251. SharedHandle<DownloadContext> dctx(new DownloadContext());
  252. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  253. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  254. getInfoHashString(dctx));
  255. }
  256. void BittorrentHelperTest::testGetPeerId() {
  257. std::string peerId = generatePeerId("aria2-");
  258. CPPUNIT_ASSERT(peerId.find("aria2-") == 0);
  259. CPPUNIT_ASSERT_EQUAL((size_t)20, peerId.size());
  260. }
  261. void BittorrentHelperTest::testComputeFastSet()
  262. {
  263. std::string ipaddr = "192.168.0.1";
  264. unsigned char infoHash[20];
  265. memset(infoHash, 0, sizeof(infoHash));
  266. infoHash[0] = 0xff;
  267. int fastSetSize = 10;
  268. size_t numPieces = 1000;
  269. {
  270. std::vector<size_t> fastSet;
  271. computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
  272. size_t ans[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
  273. std::vector<size_t> ansSet(vbegin(ans), vend(ans));
  274. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
  275. }
  276. ipaddr = "10.0.0.1";
  277. {
  278. std::vector<size_t> fastSet;
  279. computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
  280. size_t ans[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
  281. std::vector<size_t> ansSet(vbegin(ans), vend(ans));
  282. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
  283. }
  284. // See when pieces < fastSetSize
  285. numPieces = 9;
  286. {
  287. std::vector<size_t> fastSet;
  288. computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
  289. size_t ans[] = { 8, 6, 7, 5, 1, 4, 0, 2, 3 };
  290. std::vector<size_t> ansSet(vbegin(ans), vend(ans));
  291. CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
  292. }
  293. }
  294. void BittorrentHelperTest::testGetFileEntries_multiFileUrlList() {
  295. SharedHandle<DownloadContext> dctx(new DownloadContext());
  296. load(A2_TEST_DIR"/url-list-multiFile.torrent", dctx, option_);
  297. // This is multi-file torrent.
  298. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  299. dctx->getFileEntries();
  300. // There are 2 file entries.
  301. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  302. std::vector<SharedHandle<FileEntry> >::const_iterator itr =
  303. fileEntries.begin();
  304. const SharedHandle<FileEntry>& fileEntry1 = *itr;
  305. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test@/aria2@/src@/aria2c@"),
  306. fileEntry1->getPath());
  307. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  308. CPPUNIT_ASSERT_EQUAL((size_t)2, uris1.size());
  309. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test%40/aria2%40/src%40/aria2c%40"),
  310. uris1[0]);
  311. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test%40/aria2%40/src%40/aria2c%40"),
  312. uris1[1]);
  313. ++itr;
  314. const SharedHandle<FileEntry>& fileEntry2 = *itr;
  315. CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test@/aria2-0.2.2.tar.bz2"),
  316. fileEntry2->getPath());
  317. const std::deque<std::string>& uris2 = fileEntry2->getRemainingUris();
  318. CPPUNIT_ASSERT_EQUAL((size_t)2, uris2.size());
  319. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test%40/aria2-0.2.2.tar.bz2"),
  320. uris2[0]);
  321. CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test%40/aria2-0.2.2.tar.bz2"),
  322. uris2[1]);
  323. }
  324. void BittorrentHelperTest::testGetFileEntries_singleFileUrlList() {
  325. SharedHandle<DownloadContext> dctx(new DownloadContext());
  326. load(A2_TEST_DIR"/url-list-singleFile.torrent", dctx, option_);
  327. // This is single-file torrent.
  328. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  329. dctx->getFileEntries();
  330. // There are 1 file entries.
  331. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  332. const SharedHandle<FileEntry>& fileEntry1 = fileEntries.front();
  333. CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"),
  334. fileEntry1->getPath());
  335. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  336. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  337. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"),
  338. uris1[0]);
  339. }
  340. void BittorrentHelperTest::testGetFileEntries_singleFileUrlListEndsWithSlash() {
  341. SharedHandle<DownloadContext> dctx(new DownloadContext());
  342. load(A2_TEST_DIR"/url-list-singleFileEndsWithSlash.torrent", dctx, option_);
  343. // This is single-file torrent.
  344. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  345. dctx->getFileEntries();
  346. // There are 1 file entries.
  347. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  348. const SharedHandle<FileEntry>& fileEntry1 = fileEntries.front();
  349. CPPUNIT_ASSERT_EQUAL(std::string("./aria2@.tar.bz2"),
  350. fileEntry1->getPath());
  351. const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
  352. CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
  353. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2%40.tar.bz2"),
  354. uris1[0]);
  355. }
  356. void BittorrentHelperTest::testLoadFromMemory_multiFileNonUtf8Path()
  357. {
  358. SharedHandle<List> path = List::g();
  359. path->append("path");
  360. path->append(util::fromHex("90a28a")+"E");
  361. SharedHandle<Dict> file = Dict::g();
  362. file->put("length", Integer::g(1024));
  363. file->put("path", path);
  364. SharedHandle<List> files = List::g();
  365. files->append(file);
  366. SharedHandle<Dict> info = Dict::g();
  367. info->put("files", files);
  368. info->put("piece length", Integer::g(1024));
  369. info->put("pieces", "01234567890123456789");
  370. info->put("name", util::fromHex("1b")+"$B%O%m!<"+util::fromHex("1b")+"(B");
  371. Dict dict;
  372. dict.put("info", info);
  373. SharedHandle<DownloadContext> dctx(new DownloadContext());
  374. loadFromMemory(bencode2::encode(&dict), dctx, option_, "default");
  375. const SharedHandle<FileEntry>& fe = dctx->getFirstFileEntry();
  376. CPPUNIT_ASSERT_EQUAL
  377. (std::string("./%1B%24B%25O%25m%21%3C%1B%28B/path/%90%A2%8AE"),
  378. fe->getPath());
  379. CPPUNIT_ASSERT_EQUAL
  380. (std::string("./%1B%24B%25O%25m%21%3C%1B%28B"), dctx->getBasePath());
  381. }
  382. void BittorrentHelperTest::testLoadFromMemory_singleFileNonUtf8Path()
  383. {
  384. SharedHandle<Dict> info = Dict::g();
  385. info->put("piece length", Integer::g(1024));
  386. info->put("pieces", "01234567890123456789");
  387. info->put("name", util::fromHex("90a28a")+"E");
  388. info->put("length", Integer::g(1024));
  389. Dict dict;
  390. dict.put("info", info);
  391. SharedHandle<DownloadContext> dctx(new DownloadContext());
  392. loadFromMemory(bencode2::encode(&dict), dctx, option_, "default");
  393. const SharedHandle<FileEntry>& fe = dctx->getFirstFileEntry();
  394. CPPUNIT_ASSERT_EQUAL(std::string("./%90%A2%8AE"), fe->getPath());
  395. }
  396. void BittorrentHelperTest::testLoadFromMemory()
  397. {
  398. 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";
  399. SharedHandle<DownloadContext> dctx(new DownloadContext());
  400. loadFromMemory(memory, dctx, option_, "default");
  401. std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  402. CPPUNIT_ASSERT_EQUAL(correctHash, getInfoHashString(dctx));
  403. }
  404. void BittorrentHelperTest::testLoadFromMemory_somethingMissing()
  405. {
  406. // pieces missing
  407. try {
  408. std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee";
  409. SharedHandle<DownloadContext> dctx(new DownloadContext());
  410. loadFromMemory(memory, dctx, option_, "default");
  411. CPPUNIT_FAIL("exception must be thrown.");
  412. } catch(Exception& e) {
  413. // OK
  414. }
  415. }
  416. void BittorrentHelperTest::testLoadFromMemory_overrideName()
  417. {
  418. 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";
  419. SharedHandle<DownloadContext> dctx(new DownloadContext());
  420. loadFromMemory(memory, dctx, option_, "default", "aria2-override.name");
  421. CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"),
  422. getTorrentAttrs(dctx)->name);
  423. }
  424. void BittorrentHelperTest::testLoadFromMemory_multiFileDirTraversal()
  425. {
  426. std::string memory =
  427. "d8:announce27:http://example.com/announce4:infod5:filesld6:lengthi262144e4:pathl7:../dir14:dir28:file.imgeee4:name14:../name1/name212:piece lengthi262144e6:pieces20:00000000000000000000ee";
  428. SharedHandle<DownloadContext> dctx(new DownloadContext());
  429. try {
  430. loadFromMemory(memory, dctx, option_, "default");
  431. CPPUNIT_FAIL("Exception must be thrown.");
  432. } catch(RecoverableException& e) {
  433. // success
  434. }
  435. }
  436. void BittorrentHelperTest::testLoadFromMemory_singleFileDirTraversal()
  437. {
  438. std::string memory =
  439. "d8:announce27:http://example.com/announce4:infod4:name14:../name1/name26:lengthi262144e12:piece lengthi262144e6:pieces20:00000000000000000000ee";
  440. SharedHandle<DownloadContext> dctx(new DownloadContext());
  441. try {
  442. loadFromMemory(memory, dctx, option_, "default");
  443. } catch(RecoverableException& e) {
  444. // success
  445. }
  446. }
  447. void BittorrentHelperTest::testGetNodes()
  448. {
  449. {
  450. std::string memory =
  451. "d5:nodesl"
  452. "l11:192.168.0.1i6881ee"
  453. "l11:192.168.0.2i6882ee"
  454. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  455. "12:piece lengthi262144e"
  456. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  457. "ee";
  458. SharedHandle<DownloadContext> dctx(new DownloadContext());
  459. loadFromMemory(memory, dctx, option_, "default");
  460. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  461. CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->nodes.size());
  462. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), attrs->nodes[0].first);
  463. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, attrs->nodes[0].second);
  464. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[1].first);
  465. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[1].second);
  466. }
  467. {
  468. // empty hostname
  469. std::string memory =
  470. "d5:nodesl"
  471. "l1: i6881ee"
  472. "l11:192.168.0.2i6882ee"
  473. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  474. "12:piece lengthi262144e"
  475. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  476. "ee";
  477. SharedHandle<DownloadContext> dctx(new DownloadContext());
  478. loadFromMemory(memory, dctx, option_, "default");
  479. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  480. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  481. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  482. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  483. }
  484. {
  485. // bad port
  486. std::string memory =
  487. "d5:nodesl"
  488. "l11:192.168.0.11:xe"
  489. "l11:192.168.0.2i6882ee"
  490. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  491. "12:piece lengthi262144e"
  492. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  493. "ee";
  494. SharedHandle<DownloadContext> dctx(new DownloadContext());
  495. loadFromMemory(memory, dctx, option_, "default");
  496. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  497. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  498. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  499. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  500. }
  501. {
  502. // port missing
  503. std::string memory =
  504. "d5:nodesl"
  505. "l11:192.168.0.1e"
  506. "l11:192.168.0.2i6882ee"
  507. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  508. "12:piece lengthi262144e"
  509. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  510. "ee";
  511. SharedHandle<DownloadContext> dctx(new DownloadContext());
  512. loadFromMemory(memory, dctx, option_, "default");
  513. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  514. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  515. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  516. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  517. }
  518. {
  519. // nodes is not a list
  520. std::string memory =
  521. "d5:nodes"
  522. "l11:192.168.0.1e"
  523. "4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  524. "12:piece lengthi262144e"
  525. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  526. "ee";
  527. SharedHandle<DownloadContext> dctx(new DownloadContext());
  528. loadFromMemory(memory, dctx, option_, "default");
  529. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  530. CPPUNIT_ASSERT_EQUAL((size_t)0, attrs->nodes.size());
  531. }
  532. {
  533. // the element of node is not Data
  534. std::string memory =
  535. "d5:nodesl"
  536. "ll11:192.168.0.1i6881eee"
  537. "l11:192.168.0.2i6882ee"
  538. "e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
  539. "12:piece lengthi262144e"
  540. "6:pieces20:AAAAAAAAAAAAAAAAAAAA"
  541. "ee";
  542. SharedHandle<DownloadContext> dctx(new DownloadContext());
  543. loadFromMemory(memory, dctx, option_, "default");
  544. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  545. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
  546. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
  547. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
  548. }
  549. }
  550. void BittorrentHelperTest::testGetBasePath()
  551. {
  552. SharedHandle<DownloadContext> singleCtx(new DownloadContext());
  553. load(A2_TEST_DIR"/single.torrent", singleCtx, option_);
  554. singleCtx->setFilePathWithIndex(1, "new-path");
  555. CPPUNIT_ASSERT_EQUAL(std::string("new-path"), singleCtx->getBasePath());
  556. option_->put(PREF_DIR, "downloads");
  557. SharedHandle<DownloadContext> multiCtx(new DownloadContext());
  558. load(A2_TEST_DIR"/test.torrent", multiCtx, option_);
  559. CPPUNIT_ASSERT_EQUAL(std::string("downloads/aria2-test"),
  560. multiCtx->getBasePath());
  561. }
  562. void BittorrentHelperTest::testSetFileFilter_single()
  563. {
  564. SharedHandle<DownloadContext> dctx(new DownloadContext());
  565. load(A2_TEST_DIR"/single.torrent", dctx, option_);
  566. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  567. SegList<int> sgl;
  568. dctx->setFileFilter(sgl);
  569. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  570. sgl.clear();
  571. sgl.add(1, 2);
  572. dctx->setFileFilter(sgl);
  573. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  574. // For single file torrent, file is always selected whatever range
  575. // is passed.
  576. sgl.clear();
  577. sgl.add(2, 3);
  578. dctx->setFileFilter(sgl);
  579. CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
  580. }
  581. void BittorrentHelperTest::testSetFileFilter_multi()
  582. {
  583. SharedHandle<DownloadContext> dctx(new DownloadContext());
  584. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  585. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  586. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  587. SegList<int> sgl;
  588. dctx->setFileFilter(sgl);
  589. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  590. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  591. sgl.add(2, 3);
  592. dctx->setFileFilter(sgl);
  593. CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
  594. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  595. sgl.clear();
  596. sgl.add(3, 4);
  597. dctx->setFileFilter(sgl);
  598. CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
  599. CPPUNIT_ASSERT(!dctx->getFileEntries()[1]->isRequested());
  600. sgl.clear();
  601. util::parseIntSegments(sgl, "1,2");
  602. dctx->setFileFilter(sgl);
  603. CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
  604. CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
  605. }
  606. void BittorrentHelperTest::testUTF8Torrent()
  607. {
  608. SharedHandle<DownloadContext> dctx(new DownloadContext());
  609. load(A2_TEST_DIR"/utf8.torrent", dctx, option_);
  610. CPPUNIT_ASSERT_EQUAL(std::string("name in utf-8"),
  611. getTorrentAttrs(dctx)->name);
  612. CPPUNIT_ASSERT_EQUAL(std::string("./name in utf-8/path in utf-8"),
  613. dctx->getFirstFileEntry()->getPath());
  614. CPPUNIT_ASSERT_EQUAL(std::string("This is utf8 comment."),
  615. getTorrentAttrs(dctx)->comment);
  616. }
  617. void BittorrentHelperTest::testEtc()
  618. {
  619. SharedHandle<DownloadContext> dctx(new DownloadContext());
  620. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  621. CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
  622. getTorrentAttrs(dctx)->comment);
  623. CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
  624. getTorrentAttrs(dctx)->createdBy);
  625. CPPUNIT_ASSERT_EQUAL((time_t)1123456789,
  626. getTorrentAttrs(dctx)->creationDate);
  627. }
  628. void BittorrentHelperTest::testCheckBitfield()
  629. {
  630. unsigned char bitfield[] = { 0xff, 0xe0 };
  631. checkBitfield(bitfield, sizeof(bitfield), 11);
  632. try {
  633. checkBitfield(bitfield, sizeof(bitfield), 17);
  634. CPPUNIT_FAIL("exception must be thrown.");
  635. } catch(RecoverableException& e) {
  636. // success
  637. }
  638. // Change last byte
  639. bitfield[1] = 0xf0;
  640. try {
  641. checkBitfield(bitfield, sizeof(bitfield), 11);
  642. CPPUNIT_FAIL("exception must be thrown.");
  643. } catch(RecoverableException& e) {
  644. // success
  645. }
  646. }
  647. void BittorrentHelperTest::testMetadata() {
  648. SharedHandle<DownloadContext> dctx(new DownloadContext());
  649. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  650. std::string torrentData = readFile(A2_TEST_DIR"/test.torrent");
  651. SharedHandle<ValueBase> tr = bencode2::decode(torrentData);
  652. SharedHandle<ValueBase> infoDic = downcast<Dict>(tr)->get("info");
  653. std::string metadata = bencode2::encode(infoDic);
  654. SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
  655. CPPUNIT_ASSERT(metadata == attrs->metadata);
  656. CPPUNIT_ASSERT_EQUAL(metadata.size(), attrs->metadataSize);
  657. }
  658. void BittorrentHelperTest::testParseMagnet()
  659. {
  660. std::string magnet =
  661. "magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c&dn=aria2"
  662. "&tr=http://tracker1&tr=http://tracker2";
  663. SharedHandle<TorrentAttribute> attrs = bittorrent::parseMagnet(magnet);
  664. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  665. util::toHex(attrs->infoHash));
  666. CPPUNIT_ASSERT_EQUAL(std::string("[METADATA]aria2"), attrs->name);
  667. CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->announceList.size());
  668. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  669. attrs->announceList[0][0]);
  670. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
  671. attrs->announceList[1][0]);
  672. magnet = "magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c";
  673. attrs = bittorrent::parseMagnet(magnet);
  674. CPPUNIT_ASSERT_EQUAL
  675. (std::string("[METADATA]248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  676. attrs->name);
  677. CPPUNIT_ASSERT(attrs->announceList.empty());
  678. magnet = "magnet:?xt=urn:sha1:7899bdb90a026c746f3cbc10839dd9b2a2a3e985&"
  679. "xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c";
  680. attrs = bittorrent::parseMagnet(magnet);
  681. CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  682. util::toHex(attrs->infoHash));
  683. }
  684. void BittorrentHelperTest::testParseMagnet_base32()
  685. {
  686. std::string infoHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  687. std::string base32InfoHash = base32::encode(util::fromHex(infoHash));
  688. std::string magnet = "magnet:?xt=urn:btih:"+base32InfoHash+"&dn=aria2";
  689. SharedHandle<TorrentAttribute> attrs = bittorrent::parseMagnet(magnet);
  690. CPPUNIT_ASSERT_EQUAL
  691. (std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  692. util::toHex(attrs->infoHash));
  693. }
  694. void BittorrentHelperTest::testMetadata2Torrent()
  695. {
  696. std::string metadata = "METADATA";
  697. SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
  698. CPPUNIT_ASSERT_EQUAL
  699. (std::string("d4:infoMETADATAe"), metadata2Torrent(metadata, attrs));
  700. attrs->announceList.push_back(std::vector<std::string>());
  701. attrs->announceList[0].push_back("http://localhost/announce");
  702. CPPUNIT_ASSERT_EQUAL
  703. (std::string("d"
  704. "13:announce-list"
  705. "ll25:http://localhost/announceee"
  706. "4:infoMETADATA"
  707. "e"),
  708. metadata2Torrent(metadata, attrs));
  709. }
  710. void BittorrentHelperTest::testTorrent2Magnet()
  711. {
  712. SharedHandle<DownloadContext> dctx(new DownloadContext());
  713. load(A2_TEST_DIR"/test.torrent", dctx, option_);
  714. CPPUNIT_ASSERT_EQUAL
  715. (std::string("magnet:?xt=urn:btih:248D0A1CD08284299DE78D5C1ED359BB46717D8C"
  716. "&dn=aria2-test"
  717. "&tr=http%3A%2F%2Ftracker1"
  718. "&tr=http%3A%2F%2Ftracker2"
  719. "&tr=http%3A%2F%2Ftracker3"),
  720. torrent2Magnet(getTorrentAttrs(dctx)));
  721. }
  722. void BittorrentHelperTest::testExtractPeerFromString()
  723. {
  724. std::string hextext = "100210354527354678541237324732171ae1";
  725. hextext += "20010db8bd0501d2288a1fc0000110ee1ae2";
  726. std::string peersstr = "36:"+util::fromHex(hextext);
  727. SharedHandle<ValueBase> str = bencode2::decode(peersstr);
  728. std::deque<SharedHandle<Peer> > peers;
  729. extractPeer(str, AF_INET6, std::back_inserter(peers));
  730. CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
  731. CPPUNIT_ASSERT_EQUAL(std::string("1002:1035:4527:3546:7854:1237:3247:3217"),
  732. peers[0]->getIPAddress());
  733. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peers[0]->getPort());
  734. CPPUNIT_ASSERT_EQUAL(std::string("2001:db8:bd05:1d2:288a:1fc0:1:10ee"),
  735. peers[1]->getIPAddress());
  736. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, peers[1]->getPort());
  737. hextext = "c0a800011ae1";
  738. hextext += "c0a800021ae2";
  739. peersstr = "12:"+util::fromHex(hextext);
  740. str = bencode2::decode(peersstr);
  741. peers.clear();
  742. extractPeer(str, AF_INET, std::back_inserter(peers));
  743. CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
  744. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peers[0]->getIPAddress());
  745. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peers[0]->getPort());
  746. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peers[1]->getIPAddress());
  747. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, peers[1]->getPort());
  748. }
  749. void BittorrentHelperTest::testExtractPeerFromList()
  750. {
  751. std::string peersString =
  752. "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
  753. "4:porti2006eeee";
  754. SharedHandle<ValueBase> dict = bencode2::decode(peersString);
  755. std::deque<SharedHandle<Peer> > peers;
  756. extractPeer(downcast<Dict>(dict)->get("peers"), AF_INET, std::back_inserter(peers));
  757. CPPUNIT_ASSERT_EQUAL((size_t)1, peers.size());
  758. SharedHandle<Peer> peer = *peers.begin();
  759. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer->getIPAddress());
  760. CPPUNIT_ASSERT_EQUAL((uint16_t)2006, peer->getPort());
  761. }
  762. void BittorrentHelperTest::testExtract2PeersFromList()
  763. {
  764. std::string peersString =
  765. "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
  766. "4:porti65535eed2:ip11:192.168.0.27:peer id20:aria2-00000000000000"
  767. "4:porti2007eeee";
  768. SharedHandle<ValueBase> dict = bencode2::decode(peersString);
  769. std::deque<SharedHandle<Peer> > peers;
  770. extractPeer(downcast<Dict>(dict)->get("peers"), AF_INET, std::back_inserter(peers));
  771. CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
  772. SharedHandle<Peer> peer = *peers.begin();
  773. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer->getIPAddress());
  774. CPPUNIT_ASSERT_EQUAL((uint16_t)65535, peer->getPort());
  775. peer = *(peers.begin()+1);
  776. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peer->getIPAddress());
  777. CPPUNIT_ASSERT_EQUAL((uint16_t)2007, peer->getPort());
  778. }
  779. void BittorrentHelperTest::testPackcompact()
  780. {
  781. unsigned char compact[COMPACT_LEN_IPV6];
  782. CPPUNIT_ASSERT_EQUAL(18,
  783. packcompact(compact,
  784. "1002:1035:4527:3546:7854:1237:3247:3217",
  785. 6881));
  786. CPPUNIT_ASSERT_EQUAL(std::string("100210354527354678541237324732171ae1"),
  787. util::toHex(compact, 18));
  788. CPPUNIT_ASSERT_EQUAL(6, packcompact(compact, "192.168.0.1", 6881));
  789. CPPUNIT_ASSERT_EQUAL(std::string("c0a800011ae1"), util::toHex(compact, 6));
  790. CPPUNIT_ASSERT_EQUAL(0, packcompact(compact, "badaddr", 6881));
  791. }
  792. void BittorrentHelperTest::testUnpackcompact()
  793. {
  794. unsigned char compact6[] = {
  795. 0x10, 0x02, 0x10, 0x35, 0x45, 0x27, 0x35, 0x46,
  796. 0x78, 0x54, 0x12, 0x37, 0x32, 0x47, 0x32, 0x17,
  797. 0x1A, 0xE1 };
  798. std::pair<std::string, uint16_t> p =
  799. unpackcompact(compact6, AF_INET6);
  800. CPPUNIT_ASSERT_EQUAL(std::string("1002:1035:4527:3546:7854:1237:3247:3217"),
  801. p.first);
  802. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, p.second);
  803. unsigned char compact[] = { 0xC0, 0xa8, 0x00, 0x01, 0x1A, 0xE1 };
  804. p = unpackcompact(compact, AF_INET);
  805. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), p.first);
  806. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, p.second);
  807. }
  808. void BittorrentHelperTest::testRemoveAnnounceUri()
  809. {
  810. SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
  811. std::vector<std::string> tier1;
  812. tier1.push_back("http://host1/announce");
  813. std::vector<std::string> tier2;
  814. tier2.push_back("http://host2/announce");
  815. tier2.push_back("http://host3/announce");
  816. attrs->announceList.push_back(tier1);
  817. attrs->announceList.push_back(tier2);
  818. std::vector<std::string> removeUris;
  819. removeUris.push_back(tier1[0]);
  820. removeUris.push_back(tier2[0]);
  821. removeAnnounceUri(attrs, removeUris);
  822. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList.size());
  823. CPPUNIT_ASSERT_EQUAL(std::string("http://host3/announce"),
  824. attrs->announceList[0][0]);
  825. removeUris.clear();
  826. removeUris.push_back("*");
  827. removeAnnounceUri(attrs, removeUris);
  828. CPPUNIT_ASSERT(attrs->announceList.empty());
  829. }
  830. void BittorrentHelperTest::testAddAnnounceUri()
  831. {
  832. SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
  833. std::vector<std::string> addUris;
  834. addUris.push_back("http://host1/announce");
  835. addUris.push_back("http://host2/announce");
  836. addAnnounceUri(attrs, addUris);
  837. CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->announceList.size());
  838. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[0].size());
  839. CPPUNIT_ASSERT_EQUAL(std::string("http://host1/announce"),
  840. attrs->announceList[0][0]);
  841. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[1].size());
  842. CPPUNIT_ASSERT_EQUAL(std::string("http://host2/announce"),
  843. attrs->announceList[1][0]);
  844. }
  845. void BittorrentHelperTest::testAdjustAnnounceUri()
  846. {
  847. SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
  848. std::vector<std::string> tier1;
  849. tier1.push_back("http://host1/announce");
  850. std::vector<std::string> tier2;
  851. tier2.push_back("http://host2/announce");
  852. tier2.push_back("http://host3/announce");
  853. attrs->announceList.push_back(tier1);
  854. attrs->announceList.push_back(tier2);
  855. SharedHandle<Option> option(new Option());
  856. option->put(PREF_BT_TRACKER, "http://host1/announce,http://host4/announce");
  857. option->put(PREF_BT_EXCLUDE_TRACKER,
  858. "http://host1/announce,http://host2/announce");
  859. adjustAnnounceUri(attrs, option);
  860. CPPUNIT_ASSERT_EQUAL((size_t)3, attrs->announceList.size());
  861. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[0].size());
  862. CPPUNIT_ASSERT_EQUAL(std::string("http://host3/announce"),
  863. attrs->announceList[0][0]);
  864. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[1].size());
  865. CPPUNIT_ASSERT_EQUAL(std::string("http://host1/announce"),
  866. attrs->announceList[1][0]);
  867. CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[2].size());
  868. CPPUNIT_ASSERT_EQUAL(std::string("http://host4/announce"),
  869. attrs->announceList[2][0]);
  870. }
  871. } // namespace bittorrent
  872. } // namespace aria2