diff --git a/ChangeLog b/ChangeLog index dfbcfc6f..51fd7a6d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,45 @@ +2007-11-04 Tatsuhiro Tsujikawa + + Fixed: the listen port sent to the tracker is wrong. If aria2 fails + to open listen port, then remove the port number from the tracker + request. + * src/DefaultBtAnnounce.cc + * test/DefaultBtAnnounceTest.cc + * test/MockPieceStorage.h + + Inject randomizer to DefaultBtAnnounce and DefaultBtContext to make + them more testable. + * src/DefaultBtAnnounce.{h, cc} + * src/DefaultBtContext.{h, cc} + * src/Util.{h, cc} + * test/DefaultBtAnnounceTest.cc + * test/DefaultBtContextTest.cc + * test/UtilTest.cc + + Added 'B' to upload bytes readout. + * src/ConsoleStatCalc.cc + + Now the listen port for BitTorrent download is opened when it is + needed. + * src/DownloadEngineFactory.cc + * src/BtSetup.{h, cc} + * src/PeerListenCommand.{h, cc} + + Now an exception thrown while parsing tracker response is now logged. + If DlAbortEx is catched, then btAnnounce->resetAnnounce() immediately + called, which means no retry is made in this case, assuming a tracker + has a problem. + * src/TrackerWatcherCommand.cc + + Fixed: downloading a file whose length is unkown fails. + * src/DownloadCommand.cc + + Simplified prepareForNextSegment() + * src/DownloadCommand.cc + + Updated + * po/POTFILES.in + 2007-11-03 Tatsuhiro Tsujikawa Re-implemented a file listing for Metalink, which was dropped while diff --git a/TODO b/TODO index 8a1c79ea..de39b6b4 100644 --- a/TODO +++ b/TODO @@ -51,9 +51,6 @@ -- remaining features to be implemented for 0.12.0 release * Reimplement ChecksumCommand(validation using 1 checksum for 1 file) * Implement duplicate download checking in Bt -* Add PeerListenCommand to DownloadEngine only when it is really necessary. * improve --metalink-location field * Use content-type for PostDownloadHandler -* Torrent information * Fix SleepCommand to catch halt signal - diff --git a/po/POTFILES b/po/POTFILES index 94d3ba3a..ea613793 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -5,4 +5,5 @@ ../src/MultiUrlRequestInfo.cc \ ../src/RequestGroupMan.cc \ ../src/Util.cc \ - ../src/version_usage.cc + ../src/version_usage.cc \ + ../src/BtSetup.cc diff --git a/po/POTFILES.in b/po/POTFILES.in index bb62783f..8dc58467 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -6,3 +6,4 @@ src/MultiUrlRequestInfo.cc src/RequestGroupMan.cc src/Util.cc src/version_usage.cc +src/BtSetup.cc diff --git a/src/BtSetup.cc b/src/BtSetup.cc index d9a70fbb..845e1a49 100644 --- a/src/BtSetup.cc +++ b/src/BtSetup.cc @@ -42,6 +42,7 @@ #include "SeedCheckCommand.h" #include "PeerChokeCommand.h" #include "ActivePeerConnectionCommand.h" +#include "PeerListenCommand.h" #include "UnionSeedCriteria.h" #include "TimeSeedCriteria.h" #include "ShareRatioSeedCriteria.h" @@ -49,6 +50,11 @@ #include "DefaultBtProgressInfoFile.h" #include "CUIDCounter.h" #include "prefs.h" +#include "LogFactory.h" +#include "Logger.h" +#include "Util.h" + +BtSetup::BtSetup():_logger(LogFactory::getInstance()) {} Commands BtSetup::setup(RequestGroup* requestGroup, DownloadEngine* e, @@ -92,6 +98,24 @@ Commands BtSetup::setup(RequestGroup* requestGroup, unionCri)); } + if(PeerListenCommand::getNumInstance() == 0) { + PeerListenCommand* listenCommand = PeerListenCommand::getInstance(e); + int32_t port; + int32_t listenPort = option->getAsInt(PREF_LISTEN_PORT); + if(listenPort == -1) { + port = listenCommand->bindPort(6881, 6999); + } else { + port = listenCommand->bindPort(listenPort, listenPort); + } + if(port == -1) { + _logger->error(_("Errors occurred while binding port.\n")); + delete listenCommand; + } else { + BT_RUNTIME(btContext)->setListenPort(port); + commands.push_back(listenCommand); + } + } + BT_RUNTIME(btContext)->setReady(true); return commands; } diff --git a/src/BtSetup.h b/src/BtSetup.h index 12636eee..e44bc5f4 100644 --- a/src/BtSetup.h +++ b/src/BtSetup.h @@ -37,6 +37,7 @@ #include "common.h" +class Logger; class RequestGroup; class DownloadEngine; class Option; @@ -44,7 +45,11 @@ class Command; extern typedef deque Commands; class BtSetup { +private: + const Logger* _logger; public: + BtSetup(); + Commands setup(RequestGroup* requestGroup, DownloadEngine* e, const Option* option); diff --git a/src/ConsoleStatCalc.cc b/src/ConsoleStatCalc.cc index 1dad09aa..5d3c9bf5 100644 --- a/src/ConsoleStatCalc.cc +++ b/src/ConsoleStatCalc.cc @@ -101,7 +101,7 @@ ConsoleStatCalc::calculateStat(const RequestGroupManHandle& requestGroupMan, cout << " " << "UP:" << fixed << setprecision(2) << stat.getUploadSpeed()/1024.0 << "KiB/s" - << "(" << Util::abbrevSize(stat.getAllTimeUploadLength()) << ")"; + << "(" << Util::abbrevSize(stat.getAllTimeUploadLength()) << "B)"; } if(eta > 0) { cout << " " diff --git a/src/DefaultBtAnnounce.cc b/src/DefaultBtAnnounce.cc index 9345eb7d..b9b53256 100644 --- a/src/DefaultBtAnnounce.cc +++ b/src/DefaultBtAnnounce.cc @@ -44,6 +44,7 @@ #include "prefs.h" #include "DlAbortEx.h" #include "message.h" +#include "SimpleRandomizer.h" DefaultBtAnnounce::DefaultBtAnnounce(BtContextHandle btContext, const Option* option): @@ -56,20 +57,22 @@ DefaultBtAnnounce::DefaultBtAnnounce(BtContextHandle btContext, announceList(btContext->getAnnounceTiers()), trackerNumTry(0), option(option), + logger(LogFactory::getInstance()), + _randomizer(SimpleRandomizer::getInstance()), btRuntime(BT_RUNTIME(btContext)), pieceStorage(PIECE_STORAGE(btContext)), peerStorage(PEER_STORAGE(btContext)) { prevAnnounceTime.setTimeInSec(0); - key = generateKey(); - logger = LogFactory::getInstance(); + generateKey(); } DefaultBtAnnounce::~DefaultBtAnnounce() { } -string DefaultBtAnnounce::generateKey() const { - return Util::randomAlpha(8); +void DefaultBtAnnounce::generateKey() +{ + key = Util::randomAlpha(8, _randomizer); } bool DefaultBtAnnounce::isDefaultAnnounceReady() { @@ -125,7 +128,6 @@ string DefaultBtAnnounce::getAnnounceUrl() { "info_hash="+Util::torrentUrlencode(btContext->getInfoHash(), btContext->getInfoHashLength())+"&"+ "peer_id="+Util::torrentUrlencode(btContext->getPeerId(), 20)+"&"+ - "port="+Util::itos(btRuntime->getListenPort())+"&"+ "uploaded="+Util::llitos(stat.getSessionUploadLength())+"&"+ "downloaded="+Util::llitos(stat.getSessionDownloadLength())+"&"+ "left="+Util::llitos(left)+"&"+ @@ -133,6 +135,9 @@ string DefaultBtAnnounce::getAnnounceUrl() { "key="+key+"&"+ "numwant="+Util::itos(numWant)+"&"+ "no_peer_id=1"; + if(btRuntime->getListenPort() > 0) { + url += string("&")+"port="+Util::itos(btRuntime->getListenPort()); + } string event = announceList.getEventString(); if(!event.empty()) { url += string("&")+"event="+event; @@ -171,7 +176,7 @@ void DefaultBtAnnounce::resetAnnounce() { void DefaultBtAnnounce::processAnnounceResponse(const char* trackerResponse, - size_t trackerResponseLength) + size_t trackerResponseLength) { SharedHandle entry(MetaFileUtil::bdecoding(trackerResponse, trackerResponseLength)); @@ -237,3 +242,8 @@ bool DefaultBtAnnounce::noMoreAnnounce() { void DefaultBtAnnounce::shuffleAnnounce() { announceList.shuffle(); } + +void DefaultBtAnnounce::setRandomizer(const RandomizerHandle& randomizer) +{ + _randomizer = randomizer; +} diff --git a/src/DefaultBtAnnounce.h b/src/DefaultBtAnnounce.h index 03a873ff..774f0699 100644 --- a/src/DefaultBtAnnounce.h +++ b/src/DefaultBtAnnounce.h @@ -45,6 +45,9 @@ #include "PieceStorage.h" #include "PeerStorage.h" +class Randomizer; +extern typedef SharedHandle RandomizerHandle; + #define DEFAULT_ANNOUNCE_INTERVAL 1800 class DefaultBtAnnounce : public BtAnnounce { @@ -62,6 +65,7 @@ private: int32_t trackerNumTry; const Option* option; Logger* logger; + RandomizerHandle _randomizer; BtRuntimeHandle btRuntime; PieceStorageHandle pieceStorage; PeerStorageHandle peerStorage; @@ -111,7 +115,9 @@ public: virtual void shuffleAnnounce(); - string generateKey() const; + void generateKey(); + + void setRandomizer(const RandomizerHandle& randomizer); }; #endif // _D_DEFAULT_BT_ANNOUNCE_H_ diff --git a/src/DefaultBtContext.cc b/src/DefaultBtContext.cc index cda84ec7..9fda2358 100644 --- a/src/DefaultBtContext.cc +++ b/src/DefaultBtContext.cc @@ -43,15 +43,18 @@ #include "MessageDigestHelper.h" #include "a2netcompat.h" #include "AnnounceTier.h" +#include "SimpleRandomizer.h" #include -DefaultBtContext::DefaultBtContext():_peerIdPrefix("-aria2-"), _ownerRequestGroup(0) {} +DefaultBtContext::DefaultBtContext():_peerIdPrefix("-aria2-"), + _randomizer(SimpleRandomizer::getInstance()), + _ownerRequestGroup(0) {} DefaultBtContext::~DefaultBtContext() {} string DefaultBtContext::generatePeerId() const { string peerId = _peerIdPrefix; - peerId += Util::randomAlpha(20-_peerIdPrefix.size()); + peerId += Util::randomAlpha(20-_peerIdPrefix.size(), _randomizer); if(peerId.size() > 20) { peerId.erase(20); } @@ -342,3 +345,7 @@ ostream& operator<<(ostream& o, const DefaultBtContext& ctx) return o; } +void DefaultBtContext::setRandomizer(const RandomizerHandle& randomizer) +{ + _randomizer = randomizer; +} diff --git a/src/DefaultBtContext.h b/src/DefaultBtContext.h index 9efb37c8..3dfd81c4 100644 --- a/src/DefaultBtContext.h +++ b/src/DefaultBtContext.h @@ -39,6 +39,10 @@ #include "Dictionary.h" #include "Data.h" #include "List.h" + +class Randomizer; +extern typedef SharedHandle RandomizerHandle; + #define INFO_HASH_LENGTH 20 #define PIECE_HASH_LENGTH 20 @@ -58,6 +62,7 @@ private: string peerId; string _peerIdPrefix; AnnounceTiers announceTiers; + RandomizerHandle _randomizer; RequestGroup* _ownerRequestGroup; @@ -150,6 +155,8 @@ private: _ownerRequestGroup = owner; } + void setRandomizer(const RandomizerHandle& randomizer); + friend ostream& operator<<(ostream& o, const DefaultBtContext& ctx); }; diff --git a/src/DownloadCommand.cc b/src/DownloadCommand.cc index b9ea50e2..9e4cb943 100644 --- a/src/DownloadCommand.cc +++ b/src/DownloadCommand.cc @@ -88,7 +88,7 @@ bool DownloadCommand::executeInternal() { int32_t BUFSIZE = 16*1024; char buf[BUFSIZE]; int32_t bufSize; - if(segment->getLength()-segment->getWrittenLength() < BUFSIZE) { + if(segment->getLength() > 0 && segment->getLength()-segment->getWrittenLength() < BUFSIZE) { bufSize = segment->getLength()-segment->getWrittenLength(); } else { bufSize = BUFSIZE; @@ -157,31 +157,16 @@ bool DownloadCommand::prepareForNextSegment() { */ return true; } else { - // Merge segment with next segment, if segment.index+1 == nextSegment.index - SegmentHandle tempSegment = _segments.front(); - while(1) { - SegmentHandle nextSegment = - _requestGroup->getSegmentMan()->getSegment(cuid, - tempSegment->getIndex()+1); - if(nextSegment.isNull()) { - break; - } else { - if(nextSegment->getWrittenLength() > 0) { - return prepareForRetry(0); - } - nextSegment->updateWrittenLength(tempSegment->getOverflowLength()); - if(nextSegment->complete()) { - validatePieceHash(nextSegment); - tempSegment = nextSegment; - } else { - e->commands.push_back(this); - return false; - } - } + SegmentHandle nextSegment = + _requestGroup->getSegmentMan()->getSegment(cuid, + tempSegment->getIndex()+1); + if(!nextSegment.isNull() && nextSegment->getWrittenLength() == 0) { + e->commands.push_back(this); + return false; + } else { + return prepareForRetry(0); } - - return prepareForRetry(0); } } diff --git a/src/DownloadEngineFactory.cc b/src/DownloadEngineFactory.cc index b3905981..87d0a65c 100644 --- a/src/DownloadEngineFactory.cc +++ b/src/DownloadEngineFactory.cc @@ -49,7 +49,6 @@ #include "FileAllocationDispatcherCommand.h" #include "AutoSaveCommand.h" #include "HaveEraseCommand.h" -#include "PeerListenCommand.h" DownloadEngineFactory::DownloadEngineFactory(): _logger(LogFactory::getInstance()) {} @@ -84,23 +83,5 @@ DownloadEngineFactory::newDownloadEngine(Option* op, e->commands.push_back(new AutoSaveCommand(CUIDCounterSingletonHolder::instance()->newID(), e.get(), op->getAsInt(PREF_AUTO_SAVE_INTERVAL))); e->commands.push_back(new HaveEraseCommand(CUIDCounterSingletonHolder::instance()->newID(), e.get(), 10)); - PeerListenCommand* listenCommand = - new PeerListenCommand(CUIDCounterSingletonHolder::instance()->newID(), e.get()); - int32_t port; - int32_t listenPort = op->getAsInt(PREF_LISTEN_PORT); - if(listenPort == -1) { - port = listenCommand->bindPort(6881, 6999); - } else { - port = listenCommand->bindPort(listenPort, listenPort); - } - if(port == -1) { - _logger->error(_("Errors occurred while binding port.\n")); - delete listenCommand; - } else { - op->put(PREF_LISTEN_PORT, Util::itos(port).c_str()); - e->commands.push_back(listenCommand); - } - //btRuntime->setListenPort(port); - return e; } diff --git a/src/PeerListenCommand.cc b/src/PeerListenCommand.cc index f8162c40..023b81f8 100644 --- a/src/PeerListenCommand.cc +++ b/src/PeerListenCommand.cc @@ -41,14 +41,26 @@ #include "message.h" #include "PeerReceiveHandshakeCommand.h" +int32_t PeerListenCommand::__numInstance = 0; + +PeerListenCommand* PeerListenCommand::__instance = 0; + PeerListenCommand::PeerListenCommand(int32_t cuid, DownloadEngine* e): Command(cuid), e(e), - _lowestSpeedLimit(20*1024) {} + _lowestSpeedLimit(20*1024) +{ + ++__numInstance; +} -PeerListenCommand::~PeerListenCommand() {} +PeerListenCommand::~PeerListenCommand() +{ + --__numInstance; +} -int32_t PeerListenCommand::bindPort(int32_t portRangeStart, int32_t portRangeEnd) { +int32_t PeerListenCommand::bindPort(int32_t portRangeStart, + int32_t portRangeEnd) +{ if(portRangeStart > portRangeEnd) { return -1; } @@ -103,3 +115,11 @@ bool PeerListenCommand::execute() { e->commands.push_back(this); return false; } + +PeerListenCommand* PeerListenCommand::getInstance(DownloadEngine* e) +{ + if(__numInstance == 0) { + __instance = new PeerListenCommand(CUIDCounterSingletonHolder::instance()->newID(), e); + } + return __instance; +} diff --git a/src/PeerListenCommand.h b/src/PeerListenCommand.h index 5997dfee..aba8cf00 100644 --- a/src/PeerListenCommand.h +++ b/src/PeerListenCommand.h @@ -45,6 +45,11 @@ private: DownloadEngine* e; SocketHandle socket; int32_t _lowestSpeedLimit; + + static int32_t __numInstance; + + static PeerListenCommand* __instance; + public: PeerListenCommand(int32_t cuid, DownloadEngine* e); @@ -58,6 +63,14 @@ public: { _lowestSpeedLimit = speed; } + + static PeerListenCommand* getInstance(DownloadEngine* e); + + static int32_t getNumInstance() + { + return __numInstance; + } + }; #endif // _D_PEER_LISTEN_COMMAND_H_ diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc index 600c727b..64afe78e 100644 --- a/src/TrackerWatcherCommand.cc +++ b/src/TrackerWatcherCommand.cc @@ -82,11 +82,17 @@ bool TrackerWatcherCommand::execute() { btAnnounce->announceSuccess(); btAnnounce->resetAnnounce(); } catch(DlAbortEx* ex) { + logger->error(EX_EXCEPTION_CAUGHT, ex); + delete ex; + btAnnounce->announceFailure(); + btAnnounce->resetAnnounce(); + } catch(DlRetryEx* ex) { + logger->error(EX_EXCEPTION_CAUGHT, ex); + delete ex; btAnnounce->announceFailure(); if(btAnnounce->isAllAnnounceFailed()) { btAnnounce->resetAnnounce(); } - delete ex; } _trackerRequestGroup = 0; } else if(_trackerRequestGroup->getNumCommand() == 0){ diff --git a/src/Util.cc b/src/Util.cc index f7eb4367..92fd7cce 100644 --- a/src/Util.cc +++ b/src/Util.cc @@ -35,7 +35,7 @@ #include "Util.h" #include "File.h" #include "message.h" -#include "SimpleRandomizer.h" +#include "Randomizer.h" #include "a2netcompat.h" #include "a2time.h" #include @@ -522,11 +522,11 @@ int32_t Util::countBit(uint32_t n) { nbits[(n >> 24)&0xffu]; } -string Util::randomAlpha(int32_t length) { +string Util::randomAlpha(int32_t length, const RandomizerHandle& randomizer) { static char *random_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; string str; for(int32_t i = 0; i < length; i++) { - int32_t index = SimpleRandomizer::getInstance()->getRandomNumber(strlen(random_chars)); + int32_t index = randomizer->getRandomNumber(strlen(random_chars)); str += random_chars[index]; } return str; diff --git a/src/Util.h b/src/Util.h index f57c228b..ac3cfabd 100644 --- a/src/Util.h +++ b/src/Util.h @@ -43,6 +43,9 @@ #include #include +class Randomizer; +extern typedef SharedHandle RandomizerHandle; + #define STRTOLL(X) strtoll(X, (char**)NULL, 10) #define START_INDEX(OFFSET, PIECE_LENGTH) ((OFFSET)/(PIECE_LENGTH)) @@ -112,7 +115,7 @@ public: static int32_t countBit(uint32_t n); - static string randomAlpha(int32_t length); + static string randomAlpha(int32_t length, const RandomizerHandle& randomizer); static string toUpper(const string& src); diff --git a/test/DefaultBtAnnounceTest.cc b/test/DefaultBtAnnounceTest.cc index 782cfad2..47276fde 100644 --- a/test/DefaultBtAnnounceTest.cc +++ b/test/DefaultBtAnnounceTest.cc @@ -3,6 +3,12 @@ #include "Option.h" #include "Util.h" #include "Exception.h" +#include "MockBtContext.h" +#include "MockPieceStorage.h" +#include "MockPeerStorage.h" +#include "BtRuntime.h" +#include "AnnounceTier.h" +#include "FixedNumberRandomizer.h" #include using namespace std; @@ -11,27 +17,161 @@ class DefaultBtAnnounceTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(DefaultBtAnnounceTest); CPPUNIT_TEST(testIsDefaultAnnounceReady); + CPPUNIT_TEST(testGetAnnounceUrl); + CPPUNIT_TEST(testNoMoreAnnounce); CPPUNIT_TEST_SUITE_END(); private: - BtContextHandle btContext; - Option* option; + MockBtContextHandle _btContext; + MockPieceStorageHandle _pieceStorage; + MockPeerStorageHandle _peerStorage; + BtRuntimeHandle _btRuntime; + Option* _option; public: - DefaultBtAnnounceTest():btContext(0) {} + DefaultBtAnnounceTest():_btContext(0), + _pieceStorage(0), + _peerStorage(0), + _btRuntime(0) {} void setUp() { - btContext = BtContextHandle(new DefaultBtContext()); - btContext->load("test.torrent"); - option = new Option(); + _option = new Option(); + + int64_t totalLength = 4*1024*1024; + int32_t pieceLength = 256*1024; + + static const unsigned char infoHash[] = { 0x01, 0x23, 0x45, 0x67, + 0x89, 0xab, 0xcd, 0xef, + 0x01, 0x23, 0x45, 0x67, + 0x89, 0xab, 0xcd, 0xef, + 0x01, 0x23, 0x45, 0x67 }; + + string peerId = "-aria2-ultrafastdltl"; + + _btContext = new MockBtContext(); + _btContext->setInfoHash(infoHash); + _btContext->setTotalLength(totalLength); + _btContext->setPieceLength(pieceLength); + _btContext->setPeerId((const unsigned char*)peerId.c_str()); + + _pieceStorage = new MockPieceStorage(); + _pieceStorage->setTotalLength(totalLength); + _pieceStorage->setCompletedLength(pieceLength*10); + + _peerStorage = new MockPeerStorage(); + TransferStat stat; + stat.setSessionDownloadLength(pieceLength*5); + stat.setSessionUploadLength(pieceLength*6); + _peerStorage->setStat(stat); + + _btRuntime = new BtRuntime(); + _btRuntime->setListenPort(6989); + } + + void tearDown() + { + delete _option; } void testIsDefaultAnnounceReady(); + void testGetAnnounceUrl(); + void testNoMoreAnnounce(); }; CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtAnnounceTest); void DefaultBtAnnounceTest::testIsDefaultAnnounceReady() { - DefaultBtAnnounce btAnnounce(btContext, option); + DefaultBtAnnounce btAnnounce(_btContext, _option); CPPUNIT_ASSERT(btAnnounce.isDefaultAnnounceReady()); } + +void DefaultBtAnnounceTest::testNoMoreAnnounce() +{ + string trackerURI1 = "http://localhost/announce"; + Strings uris1; + uris1.push_back(trackerURI1); + AnnounceTierHandle announceTier1 = new AnnounceTier(uris1); + + string trackerURI2 = "http://backup/announce"; + Strings uris2; + uris2.push_back(trackerURI2); + AnnounceTierHandle announceTier2 = new AnnounceTier(uris2); + + + _btContext->addAnnounceTier(announceTier1); + _btContext->addAnnounceTier(announceTier2); + + DefaultBtAnnounce btAnnounce(_btContext, _option); + btAnnounce.setPieceStorage(_pieceStorage); + btAnnounce.setPeerStorage(_peerStorage); + btAnnounce.setBtRuntime(_btRuntime); + btAnnounce.setRandomizer(new FixedNumberRandomizer()); + btAnnounce.generateKey(); + + CPPUNIT_ASSERT_EQUAL(string("http://localhost/announce?info_hash=%01%23Eg%89%ab%cd%ef%01%23Eg%89%ab%cd%ef%01%23Eg&peer_id=%2daria2%2dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=AAAAAAAA&numwant=50&no_peer_id=1&port=6989&event=started"), btAnnounce.getAnnounceUrl()); + + btAnnounce.announceSuccess(); + + CPPUNIT_ASSERT_EQUAL(string("http://localhost/announce?info_hash=%01%23Eg%89%ab%cd%ef%01%23Eg%89%ab%cd%ef%01%23Eg&peer_id=%2daria2%2dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=AAAAAAAA&numwant=50&no_peer_id=1&port=6989"), btAnnounce.getAnnounceUrl()); + + btAnnounce.announceFailure(); + + CPPUNIT_ASSERT_EQUAL(string("http://backup/announce?info_hash=%01%23Eg%89%ab%cd%ef%01%23Eg%89%ab%cd%ef%01%23Eg&peer_id=%2daria2%2dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=AAAAAAAA&numwant=50&no_peer_id=1&port=6989&event=started"), btAnnounce.getAnnounceUrl()); + + btAnnounce.announceSuccess(); + + _pieceStorage->setAllDownloadFinished(true); + + CPPUNIT_ASSERT_EQUAL(string("http://localhost/announce?info_hash=%01%23Eg%89%ab%cd%ef%01%23Eg%89%ab%cd%ef%01%23Eg&peer_id=%2daria2%2dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=AAAAAAAA&numwant=50&no_peer_id=1&port=6989&event=completed"), btAnnounce.getAnnounceUrl()); + + btAnnounce.announceSuccess(); + + CPPUNIT_ASSERT_EQUAL(string("http://backup/announce?info_hash=%01%23Eg%89%ab%cd%ef%01%23Eg%89%ab%cd%ef%01%23Eg&peer_id=%2daria2%2dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=AAAAAAAA&numwant=50&no_peer_id=1&port=6989&event=completed"), btAnnounce.getAnnounceUrl()); + + btAnnounce.announceSuccess(); + + _btRuntime->setHalt(true); + + CPPUNIT_ASSERT_EQUAL(string("http://localhost/announce?info_hash=%01%23Eg%89%ab%cd%ef%01%23Eg%89%ab%cd%ef%01%23Eg&peer_id=%2daria2%2dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=AAAAAAAA&numwant=0&no_peer_id=1&port=6989&event=stopped"), btAnnounce.getAnnounceUrl()); + + btAnnounce.announceSuccess(); + + CPPUNIT_ASSERT_EQUAL(string("http://backup/announce?info_hash=%01%23Eg%89%ab%cd%ef%01%23Eg%89%ab%cd%ef%01%23Eg&peer_id=%2daria2%2dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=AAAAAAAA&numwant=0&no_peer_id=1&port=6989&event=stopped"), btAnnounce.getAnnounceUrl()); + + btAnnounce.announceSuccess(); +} + +void DefaultBtAnnounceTest::testGetAnnounceUrl() +{ + string trackerURI = "http://localhost/announce"; + Strings uris; + uris.push_back(trackerURI); + AnnounceTierHandle announceTier = new AnnounceTier(uris); + + _btContext->addAnnounceTier(announceTier); + + DefaultBtAnnounce btAnnounce(_btContext, _option); + btAnnounce.setPieceStorage(_pieceStorage); + btAnnounce.setPeerStorage(_peerStorage); + btAnnounce.setBtRuntime(_btRuntime); + btAnnounce.setRandomizer(new FixedNumberRandomizer()); + btAnnounce.generateKey(); + + CPPUNIT_ASSERT_EQUAL(string("http://localhost/announce?info_hash=%01%23Eg%89%ab%cd%ef%01%23Eg%89%ab%cd%ef%01%23Eg&peer_id=%2daria2%2dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=AAAAAAAA&numwant=50&no_peer_id=1&port=6989&event=started"), btAnnounce.getAnnounceUrl()); + + btAnnounce.announceSuccess(); + + CPPUNIT_ASSERT_EQUAL(string("http://localhost/announce?info_hash=%01%23Eg%89%ab%cd%ef%01%23Eg%89%ab%cd%ef%01%23Eg&peer_id=%2daria2%2dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=AAAAAAAA&numwant=50&no_peer_id=1&port=6989"), btAnnounce.getAnnounceUrl()); + + btAnnounce.announceSuccess(); + + _pieceStorage->setAllDownloadFinished(true); + + CPPUNIT_ASSERT_EQUAL(string("http://localhost/announce?info_hash=%01%23Eg%89%ab%cd%ef%01%23Eg%89%ab%cd%ef%01%23Eg&peer_id=%2daria2%2dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=AAAAAAAA&numwant=50&no_peer_id=1&port=6989&event=completed"), btAnnounce.getAnnounceUrl()); + + btAnnounce.announceSuccess(); + + _btRuntime->setHalt(true); + + CPPUNIT_ASSERT_EQUAL(string("http://localhost/announce?info_hash=%01%23Eg%89%ab%cd%ef%01%23Eg%89%ab%cd%ef%01%23Eg&peer_id=%2daria2%2dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=AAAAAAAA&numwant=0&no_peer_id=1&port=6989&event=stopped"), btAnnounce.getAnnounceUrl()); +} diff --git a/test/DefaultBtContextTest.cc b/test/DefaultBtContextTest.cc index 6b3f5be2..0a77fb42 100644 --- a/test/DefaultBtContextTest.cc +++ b/test/DefaultBtContextTest.cc @@ -2,6 +2,7 @@ #include "Util.h" #include "Exception.h" #include "AnnounceTier.h" +#include "FixedNumberRandomizer.h" #include using namespace std; @@ -229,7 +230,8 @@ void DefaultBtContextTest::testGetInfoHashAsString() { void DefaultBtContextTest::testGetPeerId() { DefaultBtContext btContext; - Util::torrentUrlencode(btContext.getPeerId(), 20); + btContext.setRandomizer(new FixedNumberRandomizer()); + CPPUNIT_ASSERT_EQUAL(string("%2daria2%2dAAAAAAAAAAAAA"), Util::torrentUrlencode(btContext.getPeerId(), 20)); } void DefaultBtContextTest::testComputeFastSet() diff --git a/test/MockPieceStorage.h b/test/MockPieceStorage.h index e52624cb..4f7175cc 100644 --- a/test/MockPieceStorage.h +++ b/test/MockPieceStorage.h @@ -18,8 +18,18 @@ private: DiskAdaptorHandle diskAdaptor; Integers pieceLengthList; Pieces inFlightPieces; + bool _allDownloadFinished; public: - MockPieceStorage():diskAdaptor(0) {} + MockPieceStorage():totalLength(0), + filteredTotalLength(0), + completedLength(0), + filteredCompletedLength(0), + bitfieldMan(0), + selectiveDownloadingMode(false), + endGame(false), + diskAdaptor(0), + _allDownloadFinished(false) {} + virtual ~MockPieceStorage() {} virtual bool hasMissingPiece(const PeerHandle& peer) { @@ -108,7 +118,12 @@ public: } virtual bool allDownloadFinished() { - return false; + return _allDownloadFinished; + } + + void setAllDownloadFinished(bool f) + { + _allDownloadFinished = f; } virtual void initStorage() {} diff --git a/test/UtilTest.cc b/test/UtilTest.cc index dc81be8d..dd12423e 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -1,4 +1,5 @@ #include "Util.h" +#include "FixedNumberRandomizer.h" #include #include @@ -240,7 +241,8 @@ public: }; void UtilTest::testRandomAlpha() { - CPPUNIT_ASSERT_EQUAL((size_t)8, Util::randomAlpha(8).size()); + string s = Util::randomAlpha(8, new FixedNumberRandomizer()); + CPPUNIT_ASSERT_EQUAL(string("AAAAAAAA"), s); } void UtilTest::testToUpper() {