2009-06-28 10:37:15 +00:00
|
|
|
#include "bittorrent_helper.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
#include "DownloadContext.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "RecoverableException.h"
|
|
|
|
#include "AnnounceTier.h"
|
|
|
|
#include "FixedNumberRandomizer.h"
|
|
|
|
#include "FileEntry.h"
|
|
|
|
#include "array_fun.h"
|
|
|
|
#include "messageDigest.h"
|
2009-09-29 14:52:42 +00:00
|
|
|
#include "a2netcompat.h"
|
2010-06-18 14:47:09 +00:00
|
|
|
#include "bencode2.h"
|
2009-11-20 15:42:25 +00:00
|
|
|
#include "TestUtil.h"
|
2009-11-23 15:59:31 +00:00
|
|
|
#include "base32.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
namespace bittorrent {
|
|
|
|
|
|
|
|
class BittorrentHelperTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BittorrentHelperTest);
|
|
|
|
CPPUNIT_TEST(testGetInfoHash);
|
|
|
|
CPPUNIT_TEST(testGetPieceHash);
|
|
|
|
CPPUNIT_TEST(testGetFileEntries);
|
|
|
|
CPPUNIT_TEST(testGetTotalLength);
|
|
|
|
CPPUNIT_TEST(testGetFileEntriesSingle);
|
|
|
|
CPPUNIT_TEST(testGetTotalLengthSingle);
|
|
|
|
CPPUNIT_TEST(testGetFileModeMulti);
|
|
|
|
CPPUNIT_TEST(testGetFileModeSingle);
|
|
|
|
CPPUNIT_TEST(testGetNameMulti);
|
|
|
|
CPPUNIT_TEST(testGetNameSingle);
|
|
|
|
CPPUNIT_TEST(testOverrideName);
|
|
|
|
CPPUNIT_TEST(testGetAnnounceTier);
|
|
|
|
CPPUNIT_TEST(testGetAnnounceTierAnnounceList);
|
|
|
|
CPPUNIT_TEST(testGetPieceLength);
|
|
|
|
CPPUNIT_TEST(testGetInfoHashAsString);
|
|
|
|
CPPUNIT_TEST(testGetPeerId);
|
|
|
|
CPPUNIT_TEST(testComputeFastSet);
|
|
|
|
CPPUNIT_TEST(testGetFileEntries_multiFileUrlList);
|
|
|
|
CPPUNIT_TEST(testGetFileEntries_singleFileUrlList);
|
|
|
|
CPPUNIT_TEST(testGetFileEntries_singleFileUrlListEndsWithSlash);
|
|
|
|
CPPUNIT_TEST(testLoadFromMemory);
|
|
|
|
CPPUNIT_TEST(testLoadFromMemory_somethingMissing);
|
|
|
|
CPPUNIT_TEST(testLoadFromMemory_overrideName);
|
2010-02-27 08:22:58 +00:00
|
|
|
CPPUNIT_TEST(testLoadFromMemory_multiFileDirTraversal);
|
|
|
|
CPPUNIT_TEST(testLoadFromMemory_singleFileDirTraversal);
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_TEST(testGetNodes);
|
|
|
|
CPPUNIT_TEST(testGetBasePath);
|
|
|
|
CPPUNIT_TEST(testSetFileFilter_single);
|
|
|
|
CPPUNIT_TEST(testSetFileFilter_multi);
|
|
|
|
CPPUNIT_TEST(testUTF8Torrent);
|
2009-11-20 15:42:25 +00:00
|
|
|
CPPUNIT_TEST(testEtc);
|
2009-09-29 14:52:42 +00:00
|
|
|
CPPUNIT_TEST(testCreatecompact);
|
|
|
|
CPPUNIT_TEST(testCheckBitfield);
|
2009-11-20 15:42:25 +00:00
|
|
|
CPPUNIT_TEST(testMetadata);
|
2009-11-24 14:47:42 +00:00
|
|
|
CPPUNIT_TEST(testParseMagnet);
|
|
|
|
CPPUNIT_TEST(testParseMagnet_base32);
|
2009-11-28 09:58:49 +00:00
|
|
|
CPPUNIT_TEST(testMetadata2Torrent);
|
2009-12-10 12:52:59 +00:00
|
|
|
CPPUNIT_TEST(testTorrent2Magnet);
|
2010-06-20 12:12:18 +00:00
|
|
|
CPPUNIT_TEST(testExtractPeerFromList);
|
|
|
|
CPPUNIT_TEST(testExtract2PeersFromList);
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
public:
|
|
|
|
void setUp() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void testGetInfoHash();
|
|
|
|
void testGetPieceHash();
|
|
|
|
void testGetFileEntries();
|
|
|
|
void testGetTotalLength();
|
|
|
|
void testGetFileEntriesSingle();
|
|
|
|
void testGetTotalLengthSingle();
|
|
|
|
void testGetFileModeMulti();
|
|
|
|
void testGetFileModeSingle();
|
|
|
|
void testGetNameMulti();
|
|
|
|
void testGetNameSingle();
|
|
|
|
void testOverrideName();
|
|
|
|
void testGetAnnounceTier();
|
|
|
|
void testGetAnnounceTierAnnounceList();
|
|
|
|
void testGetPieceLength();
|
|
|
|
void testGetInfoHashAsString();
|
|
|
|
void testGetPeerId();
|
|
|
|
void testComputeFastSet();
|
|
|
|
void testGetFileEntries_multiFileUrlList();
|
|
|
|
void testGetFileEntries_singleFileUrlList();
|
|
|
|
void testGetFileEntries_singleFileUrlListEndsWithSlash();
|
|
|
|
void testLoadFromMemory();
|
|
|
|
void testLoadFromMemory_somethingMissing();
|
|
|
|
void testLoadFromMemory_overrideName();
|
2010-02-27 08:22:58 +00:00
|
|
|
void testLoadFromMemory_multiFileDirTraversal();
|
|
|
|
void testLoadFromMemory_singleFileDirTraversal();
|
2009-06-28 10:37:15 +00:00
|
|
|
void testGetNodes();
|
|
|
|
void testGetBasePath();
|
|
|
|
void testSetFileFilter_single();
|
|
|
|
void testSetFileFilter_multi();
|
|
|
|
void testUTF8Torrent();
|
2009-11-20 15:42:25 +00:00
|
|
|
void testEtc();
|
2009-09-29 14:52:42 +00:00
|
|
|
void testCreatecompact();
|
|
|
|
void testCheckBitfield();
|
2009-11-20 15:42:25 +00:00
|
|
|
void testMetadata();
|
2009-11-24 14:47:42 +00:00
|
|
|
void testParseMagnet();
|
|
|
|
void testParseMagnet_base32();
|
2009-11-28 09:58:49 +00:00
|
|
|
void testMetadata2Torrent();
|
2009-12-10 12:52:59 +00:00
|
|
|
void testTorrent2Magnet();
|
2010-06-20 12:12:18 +00:00
|
|
|
void testExtractPeerFromList();
|
|
|
|
void testExtract2PeersFromList();
|
2009-06-28 10:37:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BittorrentHelperTest);
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetInfoHash() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
|
|
|
|
|
|
|
std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(correctHash, bittorrent::getInfoHashString(dctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetPieceHash() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(util::toHex("AAAAAAAAAAAAAAAAAAAA", 20),
|
2010-01-05 16:01:46 +00:00
|
|
|
dctx->getPieceHash(0));
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(util::toHex("BBBBBBBBBBBBBBBBBBBB", 20),
|
2010-01-05 16:01:46 +00:00
|
|
|
dctx->getPieceHash(1));
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(util::toHex("CCCCCCCCCCCCCCCCCCCC", 20),
|
2010-01-05 16:01:46 +00:00
|
|
|
dctx->getPieceHash(2));
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""),
|
2010-01-05 16:01:46 +00:00
|
|
|
dctx->getPieceHash(3));
|
2009-06-28 10:37:15 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(MessageDigestContext::SHA1, dctx->getPieceHashAlgo());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetFileEntries() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
|
|
|
// This is multi-file torrent.
|
|
|
|
std::vector<SharedHandle<FileEntry> > fileEntries = dctx->getFileEntries();
|
|
|
|
// There are 2 file entries.
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
|
|
|
|
std::vector<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin();
|
|
|
|
|
|
|
|
SharedHandle<FileEntry> fileEntry1 = *itr;
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2/src/aria2c"),
|
2010-01-05 16:01:46 +00:00
|
|
|
fileEntry1->getPath());
|
2010-02-26 15:37:08 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2-test/aria2/src/aria2c"),
|
|
|
|
fileEntry1->getOriginalName());
|
2009-06-28 10:37:15 +00:00
|
|
|
itr++;
|
|
|
|
SharedHandle<FileEntry> fileEntry2 = *itr;
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2-0.2.2.tar.bz2"),
|
2010-01-05 16:01:46 +00:00
|
|
|
fileEntry2->getPath());
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetFileEntriesSingle() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("single.torrent", dctx);
|
|
|
|
// This is multi-file torrent.
|
|
|
|
std::vector<SharedHandle<FileEntry> > fileEntries = dctx->getFileEntries();
|
|
|
|
// There is 1 file entry.
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
|
|
|
|
std::vector<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin();
|
|
|
|
|
|
|
|
SharedHandle<FileEntry> fileEntry1 = *itr;
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"),
|
2010-01-05 16:01:46 +00:00
|
|
|
fileEntry1->getPath());
|
2010-02-26 15:37:08 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"),
|
|
|
|
fileEntry1->getOriginalName());
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetTotalLength() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, dctx->getTotalLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetTotalLengthSingle() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("single.torrent", dctx);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, dctx->getTotalLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetFileModeMulti() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(MULTI, getTorrentAttrs(dctx)->mode);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetFileModeSingle() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("single.torrent", dctx);
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(SINGLE, getTorrentAttrs(dctx)->mode);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetNameMulti() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"), getTorrentAttrs(dctx)->name);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetNameSingle() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("single.torrent", dctx);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"),
|
2010-01-05 16:01:46 +00:00
|
|
|
dctx->getBasePath());
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"),
|
|
|
|
getTorrentAttrs(dctx)->name);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testOverrideName()
|
|
|
|
{
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx, "aria2-override.name");
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./aria2-override.name"),
|
2010-01-05 16:01:46 +00:00
|
|
|
dctx->getBasePath());
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"),
|
|
|
|
getTorrentAttrs(dctx)->name);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetAnnounceTier() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("single.torrent", dctx);
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
|
2009-06-28 10:37:15 +00:00
|
|
|
// There is 1 tier.
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList.size());
|
2009-06-28 10:37:15 +00:00
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[0].size());
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.com/announce.php"),
|
2010-06-18 14:47:09 +00:00
|
|
|
attrs->announceList[0][0]);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetAnnounceTierAnnounceList() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
|
2009-06-28 10:37:15 +00:00
|
|
|
// There are 3 tiers.
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, attrs->announceList.size());
|
2009-06-28 10:37:15 +00:00
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[0].size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
|
|
|
|
attrs->announceList[0][0]);
|
2009-06-28 10:37:15 +00:00
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[1].size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
|
|
|
|
attrs->announceList[1][0]);
|
2009-06-28 10:37:15 +00:00
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[2].size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"),
|
|
|
|
attrs->announceList[2][0]);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetPieceLength() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)128, dctx->getPieceLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetInfoHashAsString() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
|
2010-01-05 16:01:46 +00:00
|
|
|
getInfoHashString(dctx));
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetPeerId() {
|
2009-10-05 13:04:06 +00:00
|
|
|
std::string peerId = generatePeerId("aria2-");
|
|
|
|
CPPUNIT_ASSERT(peerId.find("aria2-") == 0);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)20, peerId.size());
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testComputeFastSet()
|
|
|
|
{
|
|
|
|
std::string ipaddr = "192.168.0.1";
|
|
|
|
unsigned char infoHash[20];
|
|
|
|
memset(infoHash, 0, sizeof(infoHash));
|
|
|
|
infoHash[0] = 0xff;
|
|
|
|
int fastSetSize = 10;
|
|
|
|
size_t numPieces = 1000;
|
|
|
|
{
|
|
|
|
std::vector<size_t> fastSet;
|
|
|
|
computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
|
|
|
|
size_t ans[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
|
2010-03-25 13:51:10 +00:00
|
|
|
std::vector<size_t> ansSet(vbegin(ans), vend(ans));
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
|
|
|
|
}
|
|
|
|
ipaddr = "10.0.0.1";
|
|
|
|
{
|
|
|
|
std::vector<size_t> fastSet;
|
|
|
|
computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
|
|
|
|
size_t ans[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
|
2010-03-25 13:51:10 +00:00
|
|
|
std::vector<size_t> ansSet(vbegin(ans), vend(ans));
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
|
|
|
|
}
|
|
|
|
// See when pieces < fastSetSize
|
|
|
|
numPieces = 9;
|
|
|
|
{
|
|
|
|
std::vector<size_t> fastSet;
|
|
|
|
computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
|
|
|
|
size_t ans[] = { 8, 6, 7, 5, 1, 4, 0, 2, 3 };
|
2010-03-25 13:51:10 +00:00
|
|
|
std::vector<size_t> ansSet(vbegin(ans), vend(ans));
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetFileEntries_multiFileUrlList() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("url-list-multiFile.torrent", dctx);
|
|
|
|
// This is multi-file torrent.
|
|
|
|
const std::vector<SharedHandle<FileEntry> >& fileEntries =
|
|
|
|
dctx->getFileEntries();
|
|
|
|
// There are 2 file entries.
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
|
|
|
|
std::vector<SharedHandle<FileEntry> >::const_iterator itr =
|
|
|
|
fileEntries.begin();
|
|
|
|
|
|
|
|
const SharedHandle<FileEntry>& fileEntry1 = *itr;
|
2010-04-03 04:02:14 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test@/aria2@/src@/aria2c@"),
|
2010-01-05 16:01:46 +00:00
|
|
|
fileEntry1->getPath());
|
2009-06-28 10:37:15 +00:00
|
|
|
const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, uris1.size());
|
2010-04-03 04:02:14 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test%40/aria2%40/src%40/aria2c%40"),
|
2010-01-05 16:01:46 +00:00
|
|
|
uris1[0]);
|
2010-04-03 04:02:14 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test%40/aria2%40/src%40/aria2c%40"),
|
2010-01-05 16:01:46 +00:00
|
|
|
uris1[1]);
|
2009-06-28 10:37:15 +00:00
|
|
|
|
|
|
|
++itr;
|
|
|
|
const SharedHandle<FileEntry>& fileEntry2 = *itr;
|
2010-04-03 04:02:14 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test@/aria2-0.2.2.tar.bz2"),
|
2010-01-05 16:01:46 +00:00
|
|
|
fileEntry2->getPath());
|
2009-06-28 10:37:15 +00:00
|
|
|
const std::deque<std::string>& uris2 = fileEntry2->getRemainingUris();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, uris2.size());
|
2010-04-03 04:02:14 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test%40/aria2-0.2.2.tar.bz2"),
|
2010-01-05 16:01:46 +00:00
|
|
|
uris2[0]);
|
2010-04-03 04:02:14 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test%40/aria2-0.2.2.tar.bz2"),
|
2010-01-05 16:01:46 +00:00
|
|
|
uris2[1]);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetFileEntries_singleFileUrlList() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("url-list-singleFile.torrent", dctx);
|
|
|
|
// This is single-file torrent.
|
|
|
|
const std::vector<SharedHandle<FileEntry> >& fileEntries =
|
|
|
|
dctx->getFileEntries();
|
|
|
|
// There are 1 file entries.
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
|
|
|
|
|
|
|
|
const SharedHandle<FileEntry>& fileEntry1 = fileEntries.front();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"),
|
2010-01-05 16:01:46 +00:00
|
|
|
fileEntry1->getPath());
|
2009-06-28 10:37:15 +00:00
|
|
|
const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"),
|
2010-01-05 16:01:46 +00:00
|
|
|
uris1[0]);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetFileEntries_singleFileUrlListEndsWithSlash() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("url-list-singleFileEndsWithSlash.torrent", dctx);
|
|
|
|
// This is single-file torrent.
|
|
|
|
const std::vector<SharedHandle<FileEntry> >& fileEntries =
|
|
|
|
dctx->getFileEntries();
|
|
|
|
// There are 1 file entries.
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
|
|
|
|
|
|
|
|
const SharedHandle<FileEntry>& fileEntry1 = fileEntries.front();
|
2010-04-03 04:02:14 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./aria2@.tar.bz2"),
|
2010-01-05 16:01:46 +00:00
|
|
|
fileEntry1->getPath());
|
2009-06-28 10:37:15 +00:00
|
|
|
const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size());
|
2010-04-03 04:02:14 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2%40.tar.bz2"),
|
2010-01-05 16:01:46 +00:00
|
|
|
uris1[0]);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testLoadFromMemory()
|
|
|
|
{
|
|
|
|
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";
|
|
|
|
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
loadFromMemory(memory, dctx, "default");
|
|
|
|
|
|
|
|
std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(correctHash, getInfoHashString(dctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testLoadFromMemory_somethingMissing()
|
|
|
|
{
|
|
|
|
// pieces missing
|
|
|
|
try {
|
|
|
|
std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee";
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
loadFromMemory(memory, dctx, "default");
|
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
|
|
|
} catch(Exception& e) {
|
|
|
|
// OK
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testLoadFromMemory_overrideName()
|
|
|
|
{
|
|
|
|
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";
|
|
|
|
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
loadFromMemory(memory, dctx, "default", "aria2-override.name");
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"),
|
|
|
|
getTorrentAttrs(dctx)->name);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 08:22:58 +00:00
|
|
|
void BittorrentHelperTest::testLoadFromMemory_multiFileDirTraversal()
|
2009-06-28 10:37:15 +00:00
|
|
|
{
|
|
|
|
std::string memory =
|
|
|
|
"d8:announce27:http://example.com/announce4:infod5:filesld6:lengthi262144e4:pathl7:../dir14:dir28:file.imgeee4:name14:../name1/name212:piece lengthi262144e6:pieces20:00000000000000000000ee";
|
|
|
|
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
dctx->setDir("/tmp");
|
2010-02-27 08:22:58 +00:00
|
|
|
try {
|
|
|
|
loadFromMemory(memory, dctx, "default");
|
|
|
|
CPPUNIT_FAIL("Exception must be thrown.");
|
|
|
|
} catch(RecoverableException& e) {
|
|
|
|
// success
|
|
|
|
}
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 08:22:58 +00:00
|
|
|
void BittorrentHelperTest::testLoadFromMemory_singleFileDirTraversal()
|
2009-06-28 10:37:15 +00:00
|
|
|
{
|
|
|
|
std::string memory =
|
|
|
|
"d8:announce27:http://example.com/announce4:infod4:name14:../name1/name26:lengthi262144e12:piece lengthi262144e6:pieces20:00000000000000000000ee";
|
|
|
|
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
dctx->setDir("/tmp");
|
2010-02-27 08:22:58 +00:00
|
|
|
try {
|
|
|
|
loadFromMemory(memory, dctx, "default");
|
|
|
|
} catch(RecoverableException& e) {
|
|
|
|
// success
|
|
|
|
}
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetNodes()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::string memory =
|
|
|
|
"d5:nodesl"
|
|
|
|
"l11:192.168.0.1i6881ee"
|
|
|
|
"l11:192.168.0.2i6882ee"
|
|
|
|
"e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
|
|
|
|
"12:piece lengthi262144e"
|
|
|
|
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
|
|
|
"ee";
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
loadFromMemory(memory, dctx, "default");
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->nodes.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), attrs->nodes[0].first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, attrs->nodes[0].second);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[1].first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[1].second);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// empty hostname
|
|
|
|
std::string memory =
|
|
|
|
"d5:nodesl"
|
|
|
|
"l1: i6881ee"
|
|
|
|
"l11:192.168.0.2i6882ee"
|
|
|
|
"e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
|
|
|
|
"12:piece lengthi262144e"
|
|
|
|
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
|
|
|
"ee";
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
loadFromMemory(memory, dctx, "default");
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// bad port
|
|
|
|
std::string memory =
|
|
|
|
"d5:nodesl"
|
|
|
|
"l11:192.168.0.11:xe"
|
|
|
|
"l11:192.168.0.2i6882ee"
|
|
|
|
"e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
|
|
|
|
"12:piece lengthi262144e"
|
|
|
|
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
|
|
|
"ee";
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
loadFromMemory(memory, dctx, "default");
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// port missing
|
|
|
|
std::string memory =
|
|
|
|
"d5:nodesl"
|
|
|
|
"l11:192.168.0.1e"
|
|
|
|
"l11:192.168.0.2i6882ee"
|
|
|
|
"e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
|
|
|
|
"12:piece lengthi262144e"
|
|
|
|
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
|
|
|
"ee";
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
loadFromMemory(memory, dctx, "default");
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// nodes is not a list
|
|
|
|
std::string memory =
|
|
|
|
"d5:nodes"
|
|
|
|
"l11:192.168.0.1e"
|
|
|
|
"4:infod4:name13:aria2.tar.bz26:lengthi262144e"
|
|
|
|
"12:piece lengthi262144e"
|
|
|
|
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
|
|
|
"ee";
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
loadFromMemory(memory, dctx, "default");
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, attrs->nodes.size());
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// the element of node is not Data
|
|
|
|
std::string memory =
|
|
|
|
"d5:nodesl"
|
|
|
|
"ll11:192.168.0.1i6881eee"
|
|
|
|
"l11:192.168.0.2i6882ee"
|
|
|
|
"e4:infod4:name13:aria2.tar.bz26:lengthi262144e"
|
|
|
|
"12:piece lengthi262144e"
|
|
|
|
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
|
|
|
"ee";
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
loadFromMemory(memory, dctx, "default");
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testGetBasePath()
|
|
|
|
{
|
|
|
|
SharedHandle<DownloadContext> singleCtx(new DownloadContext());
|
|
|
|
load("single.torrent", singleCtx);
|
|
|
|
singleCtx->setFilePathWithIndex(1, "new-path");
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("new-path"), singleCtx->getBasePath());
|
|
|
|
|
|
|
|
SharedHandle<DownloadContext> multiCtx(new DownloadContext());
|
|
|
|
multiCtx->setDir("downloads");
|
|
|
|
load("test.torrent", multiCtx);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("downloads/aria2-test"),
|
2010-01-05 16:01:46 +00:00
|
|
|
multiCtx->getBasePath());
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testSetFileFilter_single()
|
|
|
|
{
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("single.torrent", dctx);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
|
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
dctx->setFileFilter(util::parseIntRange(""));
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
|
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
dctx->setFileFilter(util::parseIntRange("1"));
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
|
|
|
|
|
|
|
|
// For single file torrent, file is always selected whatever range
|
|
|
|
// is passed.
|
2009-10-22 15:09:00 +00:00
|
|
|
dctx->setFileFilter(util::parseIntRange("2"));
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testSetFileFilter_multi()
|
|
|
|
{
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
|
|
|
|
CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
|
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
dctx->setFileFilter(util::parseIntRange(""));
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
|
|
|
|
CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
|
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
dctx->setFileFilter(util::parseIntRange("2"));
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
|
|
|
|
CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
|
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
dctx->setFileFilter(util::parseIntRange("3"));
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested());
|
|
|
|
CPPUNIT_ASSERT(!dctx->getFileEntries()[1]->isRequested());
|
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
dctx->setFileFilter(util::parseIntRange("1,2"));
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested());
|
|
|
|
CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testUTF8Torrent()
|
|
|
|
{
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("utf8.torrent", dctx);
|
2010-06-18 14:47:09 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("name in utf-8"),
|
|
|
|
getTorrentAttrs(dctx)->name);
|
2009-06-28 10:37:15 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./name in utf-8/path in utf-8"),
|
2010-01-05 16:01:46 +00:00
|
|
|
dctx->getFirstFileEntry()->getPath());
|
2009-08-19 14:09:19 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("This is utf8 comment."),
|
2010-06-18 14:47:09 +00:00
|
|
|
getTorrentAttrs(dctx)->comment);
|
2009-08-19 14:09:19 +00:00
|
|
|
}
|
|
|
|
|
2009-11-20 15:42:25 +00:00
|
|
|
void BittorrentHelperTest::testEtc()
|
2009-08-19 14:09:19 +00:00
|
|
|
{
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
|
2010-06-18 14:47:09 +00:00
|
|
|
getTorrentAttrs(dctx)->comment);
|
2009-08-19 14:09:19 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
|
2010-06-18 14:47:09 +00:00
|
|
|
getTorrentAttrs(dctx)->createdBy);
|
2009-08-19 14:09:19 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((int64_t)1123456789,
|
2010-06-18 14:47:09 +00:00
|
|
|
getTorrentAttrs(dctx)->creationDate);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
2009-09-29 14:52:42 +00:00
|
|
|
void BittorrentHelperTest::testCreatecompact()
|
|
|
|
{
|
|
|
|
unsigned char compact[6];
|
|
|
|
// Note: bittorrent::createcompact() on linux can handle IPv4-mapped
|
|
|
|
// addresses like `ffff::127.0.0.1', but on cygwin, it doesn't.
|
|
|
|
CPPUNIT_ASSERT(createcompact(compact, "127.0.0.1", 6881));
|
|
|
|
|
|
|
|
std::pair<std::string, uint16_t> p = unpackcompact(compact);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("127.0.0.1"), p.first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, p.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testCheckBitfield()
|
|
|
|
{
|
|
|
|
unsigned char bitfield[] = { 0xff, 0xe0 };
|
|
|
|
checkBitfield(bitfield, sizeof(bitfield), 11);
|
|
|
|
try {
|
|
|
|
checkBitfield(bitfield, sizeof(bitfield), 17);
|
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
|
|
|
} catch(RecoverableException& e) {
|
|
|
|
// success
|
|
|
|
}
|
|
|
|
// Change last byte
|
|
|
|
bitfield[1] = 0xf0;
|
|
|
|
try {
|
|
|
|
checkBitfield(bitfield, sizeof(bitfield), 11);
|
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
|
|
|
} catch(RecoverableException& e) {
|
|
|
|
// success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-20 15:42:25 +00:00
|
|
|
void BittorrentHelperTest::testMetadata() {
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
|
|
|
std::string torrentData = readFile("test.torrent");
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<ValueBase> tr = bencode2::decode(torrentData);
|
|
|
|
SharedHandle<ValueBase> infoDic = asDict(tr)->get("info");
|
|
|
|
std::string metadata = bencode2::encode(infoDic);
|
|
|
|
SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
|
|
|
|
CPPUNIT_ASSERT(metadata == attrs->metadata);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(metadata.size(), attrs->metadataSize);
|
2009-11-20 15:42:25 +00:00
|
|
|
}
|
|
|
|
|
2009-11-24 14:47:42 +00:00
|
|
|
void BittorrentHelperTest::testParseMagnet()
|
2009-11-22 14:35:35 +00:00
|
|
|
{
|
|
|
|
std::string magnet =
|
2009-11-24 14:47:42 +00:00
|
|
|
"magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c&dn=aria2"
|
|
|
|
"&tr=http://tracker1&tr=http://tracker2";
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> attrs = bittorrent::parseMagnet(magnet);
|
2009-11-22 14:35:35 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
|
2010-06-18 14:47:09 +00:00
|
|
|
util::toHex(attrs->infoHash));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("[METADATA]aria2"), attrs->name);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->announceList.size());
|
2009-11-24 14:47:42 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
|
2010-06-18 14:47:09 +00:00
|
|
|
attrs->announceList[0][0]);
|
2009-11-24 14:47:42 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
|
2010-06-18 14:47:09 +00:00
|
|
|
attrs->announceList[1][0]);
|
2009-11-22 14:35:35 +00:00
|
|
|
|
|
|
|
magnet = "magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c";
|
2009-11-24 14:47:42 +00:00
|
|
|
attrs = bittorrent::parseMagnet(magnet);
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("[METADATA]248d0a1cd08284299de78d5c1ed359bb46717d8c"),
|
2010-06-18 14:47:09 +00:00
|
|
|
attrs->name);
|
|
|
|
CPPUNIT_ASSERT(attrs->announceList.empty());
|
2010-03-12 15:00:28 +00:00
|
|
|
|
|
|
|
magnet = "magnet:?xt=urn:sha1:7899bdb90a026c746f3cbc10839dd9b2a2a3e985&"
|
|
|
|
"xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c";
|
|
|
|
attrs = bittorrent::parseMagnet(magnet);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
|
2010-06-18 14:47:09 +00:00
|
|
|
util::toHex(attrs->infoHash));
|
2009-11-22 14:35:35 +00:00
|
|
|
}
|
|
|
|
|
2009-11-24 14:47:42 +00:00
|
|
|
void BittorrentHelperTest::testParseMagnet_base32()
|
2009-11-23 15:59:31 +00:00
|
|
|
{
|
|
|
|
std::string infoHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
|
|
|
|
std::string base32InfoHash = base32::encode(util::fromHex(infoHash));
|
|
|
|
std::string magnet = "magnet:?xt=urn:btih:"+base32InfoHash+"&dn=aria2";
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> attrs = bittorrent::parseMagnet(magnet);
|
2009-11-24 14:47:42 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
|
2010-06-18 14:47:09 +00:00
|
|
|
util::toHex(attrs->infoHash));
|
2009-11-23 15:59:31 +00:00
|
|
|
}
|
|
|
|
|
2009-11-28 09:58:49 +00:00
|
|
|
void BittorrentHelperTest::testMetadata2Torrent()
|
|
|
|
{
|
|
|
|
std::string metadata = "METADATA";
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
|
2009-11-28 09:58:49 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("d4:infoMETADATAe"), metadata2Torrent(metadata, attrs));
|
2010-06-18 14:47:09 +00:00
|
|
|
attrs->announceList.push_back(std::vector<std::string>());
|
|
|
|
attrs->announceList[0].push_back("http://localhost/announce");
|
2009-11-28 09:58:49 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("d"
|
2010-01-05 16:01:46 +00:00
|
|
|
"13:announce-list"
|
|
|
|
"ll25:http://localhost/announceee"
|
|
|
|
"4:infoMETADATA"
|
|
|
|
"e"),
|
2009-11-28 09:58:49 +00:00
|
|
|
metadata2Torrent(metadata, attrs));
|
|
|
|
}
|
|
|
|
|
2009-12-10 12:52:59 +00:00
|
|
|
void BittorrentHelperTest::testTorrent2Magnet()
|
|
|
|
{
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
load("test.torrent", dctx);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
2009-12-18 06:32:14 +00:00
|
|
|
(std::string("magnet:?xt=urn:btih:248D0A1CD08284299DE78D5C1ED359BB46717D8C"
|
2010-01-05 16:01:46 +00:00
|
|
|
"&dn=aria2-test"
|
|
|
|
"&tr=http%3A%2F%2Ftracker1"
|
|
|
|
"&tr=http%3A%2F%2Ftracker2"
|
|
|
|
"&tr=http%3A%2F%2Ftracker3"),
|
2010-06-18 14:47:09 +00:00
|
|
|
torrent2Magnet(getTorrentAttrs(dctx)));
|
2009-12-10 12:52:59 +00:00
|
|
|
}
|
|
|
|
|
2010-06-20 12:12:18 +00:00
|
|
|
void BittorrentHelperTest::testExtractPeerFromList()
|
|
|
|
{
|
|
|
|
std::string peersString =
|
|
|
|
"d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
|
|
|
|
"4:porti2006eeee";
|
|
|
|
|
|
|
|
SharedHandle<ValueBase> dict = bencode2::decode(peersString);
|
|
|
|
|
|
|
|
std::deque<SharedHandle<Peer> > peers;
|
|
|
|
extractPeer(asDict(dict)->get("peers"), std::back_inserter(peers));
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, peers.size());
|
|
|
|
SharedHandle<Peer> peer = *peers.begin();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer->getIPAddress());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)2006, peer->getPort());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BittorrentHelperTest::testExtract2PeersFromList()
|
|
|
|
{
|
|
|
|
std::string peersString =
|
|
|
|
"d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
|
|
|
|
"4:porti65535eed2:ip11:192.168.0.27:peer id20:aria2-00000000000000"
|
|
|
|
"4:porti2007eeee";
|
|
|
|
|
|
|
|
SharedHandle<ValueBase> dict = bencode2::decode(peersString);
|
|
|
|
|
|
|
|
std::deque<SharedHandle<Peer> > peers;
|
|
|
|
extractPeer(asDict(dict)->get("peers"), std::back_inserter(peers));
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
|
|
|
|
SharedHandle<Peer> peer = *peers.begin();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer->getIPAddress());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)65535, peer->getPort());
|
|
|
|
|
|
|
|
peer = *(peers.begin()+1);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peer->getIPAddress());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)2007, peer->getPort());
|
|
|
|
}
|
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
} // namespace bittorrent
|
|
|
|
|
|
|
|
} // namespace aria2
|