BittorrentHelperTest.cc 37 KB

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