aria2/test/AnnounceListTest.cc
Tatsuhiro Tsujikawa 49b4cfbfcd 2009-04-27 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Put BDE in aria2 namespace. BDE will also be used to store
	xml-rpc request.
	* src/AnnounceList.cc
	* src/AnnounceList.h
	* src/BDE.cc: Added
	* src/BDE.h: Added
	* src/DHTAbstractMessage.cc
	* src/DHTAbstractMessage.h
	* src/DHTAnnouncePeerMessage.cc
	* src/DHTAnnouncePeerMessage.h
	* src/DHTAnnouncePeerReplyMessage.cc
	* src/DHTAnnouncePeerReplyMessage.h
	* src/DHTFindNodeMessage.cc
	* src/DHTFindNodeMessage.h
	* src/DHTFindNodeReplyMessage.cc
	* src/DHTFindNodeReplyMessage.h
	* src/DHTGetPeersMessage.cc
	* src/DHTGetPeersMessage.h
	* src/DHTGetPeersReplyMessage.cc
	* src/DHTGetPeersReplyMessage.h
	* src/DHTMessageFactory.h
	* src/DHTMessageFactoryImpl.cc
	* src/DHTMessageFactoryImpl.h
	* src/DHTMessageReceiver.cc
	* src/DHTMessageTracker.cc
	* src/DHTMessageTracker.h
	* src/DHTPingMessage.cc
	* src/DHTPingMessage.h
	* src/DHTPingReplyMessage.cc
	* src/DHTPingReplyMessage.h
	* src/DHTQueryMessage.cc
	* src/DHTQueryMessage.h
	* src/DHTResponseMessage.cc
	* src/DHTResponseMessage.h
	* src/DefaultBtAnnounce.cc
	* src/DefaultBtContext.cc
	* src/DefaultBtContext.h
	* src/HandshakeExtensionMessage.cc
	* src/Makefile.am
	* src/PeerListProcessor.h
	* src/UTPexExtensionMessage.cc
	* src/bencode.cc
	* src/bencode.h
	* test/AnnounceListTest.cc
	* test/BDETest.cc: Added
	* test/BencodeTest.cc
	* test/DHTAnnouncePeerMessageTest.cc
	* test/DHTAnnouncePeerReplyMessageTest.cc
	* test/DHTFindNodeMessageTest.cc
	* test/DHTFindNodeReplyMessageTest.cc
	* test/DHTGetPeersMessageTest.cc
	* test/DHTGetPeersReplyMessageTest.cc
	* test/DHTMessageFactoryImplTest.cc
	* test/DHTMessageTrackerTest.cc
	* test/DHTPingMessageTest.cc
	* test/DHTPingReplyMessageTest.cc
	* test/Makefile.am
	* test/MockDHTMessageFactory.h
	* test/PeerListProcessorTest.cc
2009-04-27 11:45:22 +00:00

315 lines
12 KiB
C++

#include "AnnounceList.h"
#include <cppunit/extensions/HelperMacros.h>
#include "Exception.h"
#include "bencode.h"
namespace aria2 {
class AnnounceListTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(AnnounceListTest);
CPPUNIT_TEST(testSingleElementList);
CPPUNIT_TEST(testMultiElementList);
CPPUNIT_TEST(testSingleAndMulti);
CPPUNIT_TEST(testNoGroup);
CPPUNIT_TEST(testEvent);
CPPUNIT_TEST(testNextEventIfAfterStarted);
CPPUNIT_TEST(testCountStoppedAllowedTier);
CPPUNIT_TEST(testCountCompletedAllowedTier);
CPPUNIT_TEST(testMoveToStoppedAllowedTier);
CPPUNIT_TEST(testMoveToCompletedAllowedTier);
CPPUNIT_TEST_SUITE_END();
private:
public:
void setUp() {
}
void testSingleElementList();
void testMultiElementList();
void testSingleAndMulti();
void testNoGroup();
void testEvent();
void testNextEventIfAfterStarted();
void testCountStoppedAllowedTier();
void testCountCompletedAllowedTier();
void testMoveToStoppedAllowedTier();
void testMoveToCompletedAllowedTier();
};
CPPUNIT_TEST_SUITE_REGISTRATION( AnnounceListTest );
void AnnounceListTest::testSingleElementList() {
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
const BDE announcesList = bencode::decode(peersString);
// ANNOUNCE_LIST
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
AnnounceList announceList(announcesList);
CPPUNIT_ASSERT(!announceList.allTiersFailed());
std::string url = announceList.getAnnounce();
std::string event = announceList.getEventString();
CPPUNIT_ASSERT_EQUAL(std::string("tracker1"), url);
CPPUNIT_ASSERT_EQUAL(std::string("started"), event);
announceList.announceFailure();
url = announceList.getAnnounce();
CPPUNIT_ASSERT_EQUAL(std::string("tracker2"), url);
announceList.announceFailure();
url = announceList.getAnnounce();
CPPUNIT_ASSERT_EQUAL(std::string("tracker3"), url);
announceList.announceFailure();
CPPUNIT_ASSERT(announceList.allTiersFailed());
announceList.resetTier();
CPPUNIT_ASSERT(!announceList.allTiersFailed());
// back to the first list
url = announceList.getAnnounce();
event = announceList.getEventString();
CPPUNIT_ASSERT_EQUAL(std::string("tracker1"), url);
CPPUNIT_ASSERT_EQUAL(std::string("started"), event);
announceList.announceFailure();
url = announceList.getAnnounce();
event = announceList.getEventString();
CPPUNIT_ASSERT_EQUAL(std::string("tracker2"), url);
CPPUNIT_ASSERT_EQUAL(std::string("started"), event);
announceList.announceSuccess();
// back to the first list because announce to tracker2 succeeded.
url = announceList.getAnnounce();
event = announceList.getEventString();
CPPUNIT_ASSERT_EQUAL(std::string("tracker1"), url);
CPPUNIT_ASSERT_EQUAL(std::string("started"), event);
announceList.announceFailure();
url = announceList.getAnnounce();
event = announceList.getEventString();
CPPUNIT_ASSERT_EQUAL(std::string("tracker2"), url);
CPPUNIT_ASSERT_EQUAL(std::string(""), event);
}
void AnnounceListTest::testMultiElementList() {
std::string peersString = "ll8:tracker18:tracker28:tracker3ee";
const BDE announcesList = bencode::decode(peersString);
// ANNOUNCE_LIST
// [ [ tracker1, tracker2, tracker3 ] ]
AnnounceList announceList(announcesList);
CPPUNIT_ASSERT(!announceList.allTiersFailed());
std::string url = announceList.getAnnounce();
CPPUNIT_ASSERT_EQUAL(std::string("tracker1"), url);
announceList.announceFailure();
url = announceList.getAnnounce();
CPPUNIT_ASSERT_EQUAL(std::string("tracker2"), url);
announceList.announceSuccess();
url = announceList.getAnnounce();
// tracker2 returns because tracker2 is now first.
CPPUNIT_ASSERT_EQUAL(std::string("tracker2"), url);
announceList.announceFailure();
url = announceList.getAnnounce();
CPPUNIT_ASSERT_EQUAL(std::string("tracker1"), url);
announceList.announceFailure();
url = announceList.getAnnounce();
CPPUNIT_ASSERT_EQUAL(std::string("tracker3"), url);
announceList.announceFailure();
CPPUNIT_ASSERT(announceList.allTiersFailed());
announceList.resetTier();
CPPUNIT_ASSERT(!announceList.allTiersFailed());
// back to the first list because there is no other list.
url = announceList.getAnnounce();
CPPUNIT_ASSERT_EQUAL(std::string("tracker2"), url);
}
void AnnounceListTest::testSingleAndMulti() {
std::string peersString = "ll8:tracker18:tracker2el8:tracker3ee";
const BDE announcesList = bencode::decode(peersString);
// ANNOUNCE_LIST
// [ [ tracker1, tracker2 ], [ tracker3 ] ]
AnnounceList announceList(announcesList);
std::string url = announceList.getAnnounce();
CPPUNIT_ASSERT_EQUAL(std::string("tracker1"), url);
announceList.announceSuccess();
url = announceList.getAnnounce();
CPPUNIT_ASSERT_EQUAL(std::string("tracker1"), url);
announceList.announceFailure();
url = announceList.getAnnounce();
CPPUNIT_ASSERT_EQUAL(std::string("tracker2"), url);
announceList.announceFailure();
url = announceList.getAnnounce();
CPPUNIT_ASSERT_EQUAL(std::string("tracker3"), url);
announceList.announceSuccess();
url = announceList.getAnnounce();
// tracker1 returns because after the announce to tracker3 succeeds, list
// pointer points to the first list.
CPPUNIT_ASSERT_EQUAL(std::string("tracker1"), url);
}
void AnnounceListTest::testNoGroup() {
std::string peersString = "llee";
const BDE announcesList = bencode::decode(peersString);
AnnounceList announceList(announcesList);
CPPUNIT_ASSERT(announceList.countTier() == 0);
}
void AnnounceListTest::testNextEventIfAfterStarted() {
std::string peersString = "ll8:tracker1ee";
const BDE announcesList = bencode::decode(peersString);
// ANNOUNCE_LIST
// [ [ tracker1 ] ]
AnnounceList announceList(announcesList);
announceList.setEvent(AnnounceTier::STOPPED);
announceList.announceFailure();
announceList.resetTier();
CPPUNIT_ASSERT_EQUAL(std::string(""), announceList.getEventString());
CPPUNIT_ASSERT_EQUAL(AnnounceTier::HALTED, announceList.getEvent());
announceList.setEvent(AnnounceTier::COMPLETED);
announceList.announceFailure();
announceList.resetTier();
CPPUNIT_ASSERT_EQUAL(std::string(""), announceList.getEventString());
CPPUNIT_ASSERT_EQUAL(AnnounceTier::SEEDING, announceList.getEvent());
}
void AnnounceListTest::testEvent() {
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
const BDE announcesList = bencode::decode(peersString);
// ANNOUNCE_LIST
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
AnnounceList announceList(announcesList);
announceList.setEvent(AnnounceTier::STOPPED);
announceList.announceSuccess();
CPPUNIT_ASSERT_EQUAL(std::string(""), announceList.getEventString());
CPPUNIT_ASSERT_EQUAL(AnnounceTier::HALTED, announceList.getEvent());
announceList.setEvent(AnnounceTier::COMPLETED);
announceList.announceSuccess();
CPPUNIT_ASSERT_EQUAL(std::string(""), announceList.getEventString());
CPPUNIT_ASSERT_EQUAL(AnnounceTier::SEEDING, announceList.getEvent());
announceList.setEvent(AnnounceTier::STARTED_AFTER_COMPLETION);
CPPUNIT_ASSERT_EQUAL(std::string("started"), announceList.getEventString());
announceList.announceSuccess();
CPPUNIT_ASSERT_EQUAL(AnnounceTier::SEEDING, announceList.getEvent());
}
void AnnounceListTest::testCountStoppedAllowedTier() {
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
const BDE announcesList = bencode::decode(peersString);
// ANNOUNCE_LIST
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
AnnounceList announceList(announcesList);
CPPUNIT_ASSERT_EQUAL((size_t)0, announceList.countStoppedAllowedTier());
announceList.setEvent(AnnounceTier::STARTED);
CPPUNIT_ASSERT_EQUAL((size_t)0, announceList.countStoppedAllowedTier());
announceList.setEvent(AnnounceTier::STARTED_AFTER_COMPLETION);
CPPUNIT_ASSERT_EQUAL((size_t)0, announceList.countStoppedAllowedTier());
announceList.setEvent(AnnounceTier::HALTED);
CPPUNIT_ASSERT_EQUAL((size_t)0, announceList.countStoppedAllowedTier());
announceList.setEvent(AnnounceTier::DOWNLOADING);
CPPUNIT_ASSERT_EQUAL((size_t)1, announceList.countStoppedAllowedTier());
announceList.setEvent(AnnounceTier::STOPPED);
CPPUNIT_ASSERT_EQUAL((size_t)1, announceList.countStoppedAllowedTier());
announceList.setEvent(AnnounceTier::COMPLETED);
CPPUNIT_ASSERT_EQUAL((size_t)1, announceList.countStoppedAllowedTier());
announceList.setEvent(AnnounceTier::SEEDING);
CPPUNIT_ASSERT_EQUAL((size_t)1, announceList.countStoppedAllowedTier());
}
void AnnounceListTest::testCountCompletedAllowedTier() {
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
const BDE announcesList = bencode::decode(peersString);
// ANNOUNCE_LIST
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
AnnounceList announceList(announcesList);
CPPUNIT_ASSERT_EQUAL((size_t)0, announceList.countCompletedAllowedTier());
announceList.setEvent(AnnounceTier::STARTED);
CPPUNIT_ASSERT_EQUAL((size_t)0, announceList.countCompletedAllowedTier());
announceList.setEvent(AnnounceTier::STARTED_AFTER_COMPLETION);
CPPUNIT_ASSERT_EQUAL((size_t)0, announceList.countCompletedAllowedTier());
announceList.setEvent(AnnounceTier::STOPPED);
CPPUNIT_ASSERT_EQUAL((size_t)0, announceList.countCompletedAllowedTier());
announceList.setEvent(AnnounceTier::SEEDING);
CPPUNIT_ASSERT_EQUAL((size_t)0, announceList.countCompletedAllowedTier());
announceList.setEvent(AnnounceTier::HALTED);
CPPUNIT_ASSERT_EQUAL((size_t)0, announceList.countCompletedAllowedTier());
announceList.setEvent(AnnounceTier::DOWNLOADING);
CPPUNIT_ASSERT_EQUAL((size_t)1, announceList.countCompletedAllowedTier());
announceList.setEvent(AnnounceTier::COMPLETED);
CPPUNIT_ASSERT_EQUAL((size_t)1, announceList.countCompletedAllowedTier());
}
std::deque<std::string> createUrls(const std::string& url) {
std::deque<std::string> urls;
urls.push_back(url);
return urls;
}
void AnnounceListTest::testMoveToStoppedAllowedTier() {
SharedHandle<AnnounceTier> t1(new AnnounceTier(createUrls("tracker1")));
SharedHandle<AnnounceTier> t2(new AnnounceTier(createUrls("tracker2")));
t2->event = AnnounceTier::COMPLETED;
SharedHandle<AnnounceTier> t3(new AnnounceTier(createUrls("tracker3")));
std::deque<SharedHandle<AnnounceTier> > tiers;
tiers.push_back(t1);
tiers.push_back(t2);
tiers.push_back(t3);
AnnounceList announceList(tiers);
CPPUNIT_ASSERT(!announceList.currentTierAcceptsStoppedEvent());
CPPUNIT_ASSERT_EQUAL(std::string("tracker1"), announceList.getAnnounce());
announceList.moveToStoppedAllowedTier();
CPPUNIT_ASSERT(announceList.currentTierAcceptsStoppedEvent());
CPPUNIT_ASSERT_EQUAL(std::string("tracker2"), announceList.getAnnounce());
announceList.announceFailure();
CPPUNIT_ASSERT(!announceList.currentTierAcceptsStoppedEvent());
CPPUNIT_ASSERT_EQUAL(std::string("tracker3"), announceList.getAnnounce());
// test wrapped search
announceList.moveToStoppedAllowedTier();
CPPUNIT_ASSERT_EQUAL(std::string("tracker2"), announceList.getAnnounce());
}
void AnnounceListTest::testMoveToCompletedAllowedTier() {
SharedHandle<AnnounceTier> t1(new AnnounceTier(createUrls("tracker1")));
SharedHandle<AnnounceTier> t2(new AnnounceTier(createUrls("tracker2")));
t2->event = AnnounceTier::COMPLETED;
SharedHandle<AnnounceTier> t3(new AnnounceTier(createUrls("tracker3")));
std::deque<SharedHandle<AnnounceTier> > tiers;
tiers.push_back(t1);
tiers.push_back(t2);
tiers.push_back(t3);
AnnounceList announceList(tiers);
CPPUNIT_ASSERT(!announceList.currentTierAcceptsCompletedEvent());
CPPUNIT_ASSERT_EQUAL(std::string("tracker1"), announceList.getAnnounce());
announceList.moveToStoppedAllowedTier();
CPPUNIT_ASSERT(announceList.currentTierAcceptsCompletedEvent());
CPPUNIT_ASSERT_EQUAL(std::string("tracker2"), announceList.getAnnounce());
announceList.announceFailure();
CPPUNIT_ASSERT(!announceList.currentTierAcceptsCompletedEvent());
CPPUNIT_ASSERT_EQUAL(std::string("tracker3"), announceList.getAnnounce());
// test wrapped search
announceList.moveToStoppedAllowedTier();
CPPUNIT_ASSERT_EQUAL(std::string("tracker2"), announceList.getAnnounce());
}
} // namespace aria2