BittorrentHelperTest.cc 37 KB

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