aria2/test/DHTPeerAnnounceEntryTest.cc
Tatsuhiro Tsujikawa 7cf589e784 2008-11-03 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Made BtRegistry non-static object. Now DownloadEngine has a
	reference to its object.
	* src/AbstractBtMessage.cc
	* src/ActivePeerConnectionCommand.cc
	* src/ActivePeerConnectionCommand.h
	* src/BtContextAwareCommand.cc
	* src/BtContextAwareCommand.h
	* src/BtExtendedMessage.cc
	* src/BtExtendedMessage.h
	* src/BtInterestedMessage.cc
	* src/BtInterestedMessage.h
	* src/BtNotInterestedMessage.cc
	* src/BtNotInterestedMessage.h
	* src/BtRegistry.cc
	* src/BtRegistry.h
	* src/BtSeederStateChoke.cc
	* src/BtSetup.cc
	* src/DHTGetPeersCommand.cc
	* src/DHTGetPeersCommand.h
	* src/DHTPeerAnnounceEntry.cc
	* src/DHTPeerAnnounceEntry.h
	* src/DHTPeerAnnounceStorage.cc
	* src/DHTPeerAnnounceStorage.h
	* src/DHTPeerLookupTask.cc
	* src/DHTPeerLookupTask.h
	* src/DHTSetup.cc
	* src/DHTSetup.h
	* src/DHTTaskFactory.h
	* src/DHTTaskFactoryImpl.cc
	* src/DHTTaskFactoryImpl.h
	* src/DefaultBtAnnounce.cc
	* src/DefaultBtInteractive.cc
	* src/DefaultBtInteractive.h
	* src/DefaultBtMessageDispatcher.cc
	* src/DefaultBtMessageDispatcher.h
	* src/DefaultBtMessageFactory.cc
	* src/DefaultBtMessageFactory.h
	* src/DefaultBtMessageReceiver.cc
	* src/DefaultBtProgressInfoFile.cc
	* src/DefaultBtProgressInfoFile.h
	* src/DefaultBtRequestFactory.cc
	* src/DefaultBtRequestFactory.h
	* src/DefaultExtensionMessageFactory.cc
	* src/DefaultExtensionMessageFactory.h
	* src/DefaultPeerStorage.cc
	* src/DefaultPeerStorage.h
	* src/DownloadEngine.cc
	* src/DownloadEngine.h
	* src/InitiatorMSEHandshakeCommand.cc
	* src/InitiatorMSEHandshakeCommand.h
	* src/MSEHandshake.cc
	* src/MSEHandshake.h
	* src/Makefile.am
	* src/Makefile.in
	* src/PeerChokeCommand.cc
	* src/PeerChokeCommand.h
	* src/PeerInitiateConnectionCommand.cc
	* src/PeerInitiateConnectionCommand.h
	* src/PeerInteractionCommand.cc
	* src/PeerInteractionCommand.h
	* src/PeerObject.h
	* src/PeerReceiveHandshakeCommand.cc
	* src/PeerReceiveHandshakeCommand.h
	* src/ReceiverMSEHandshakeCommand.cc
	* src/RequestGroup.cc
	* src/RequestGroup.h
	* src/RequestGroupMan.cc
	* src/RequestGroupMan.h
	* src/SeedCheckCommand.cc
	* src/SeedCheckCommand.h
	* src/ShareRatioSeedCriteria.h
	* src/SimpleBtMessage.cc
	* src/TrackerWatcherCommand.cc
	* src/TrackerWatcherCommand.h
	* src/UTPexExtensionMessage.cc
	* src/UTPexExtensionMessage.h
	* test/BtCancelMessageTest.cc
	* test/BtChokeMessageTest.cc
	* test/BtExtendedMessageTest.cc
	* test/BtInterestedMessageTest.cc
	* test/BtNotInterestedMessageTest.cc
	* test/BtPieceMessageTest.cc
	* test/BtPortMessageTest.cc
	* test/BtRegistryTest.cc
	* test/BtRejectMessageTest.cc
	* test/BtRequestMessageTest.cc
	* test/DHTPeerAnnounceEntryTest.cc
	* test/DHTPeerAnnounceStorageTest.cc
	* test/DefaultBtMessageDispatcherTest.cc
	* test/DefaultBtMessageFactoryTest.cc
	* test/DefaultBtProgressInfoFileTest.cc
	* test/DefaultBtRequestFactoryTest.cc
	* test/DefaultExtensionMessageFactoryTest.cc
	* test/MSEHandshakeTest.cc
	* test/MockDHTTaskFactory.h
	* test/ShareRatioSeedCriteriaTest.cc
	* test/UTPexExtensionMessageTest.cc
2008-11-03 06:49:02 +00:00

140 lines
4.4 KiB
C++

#include "DHTPeerAnnounceEntry.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>
#include "Exception.h"
#include "Util.h"
#include "MockPeerStorage.h"
#include "FileEntry.h"
namespace aria2 {
class DHTPeerAnnounceEntryTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DHTPeerAnnounceEntryTest);
CPPUNIT_TEST(testRemoveStalePeerAddrEntry);
CPPUNIT_TEST(testEmpty);
CPPUNIT_TEST(testAddPeerAddrEntry);
CPPUNIT_TEST(testGetPeers);
CPPUNIT_TEST_SUITE_END();
public:
void testRemoveStalePeerAddrEntry();
void testEmpty();
void testAddPeerAddrEntry();
void testGetPeers();
};
CPPUNIT_TEST_SUITE_REGISTRATION(DHTPeerAnnounceEntryTest);
void DHTPeerAnnounceEntryTest::testRemoveStalePeerAddrEntry()
{
unsigned char infohash[DHT_ID_LENGTH];
memset(infohash, 0xff, DHT_ID_LENGTH);
DHTPeerAnnounceEntry entry(infohash);
entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881));
entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.2", 6882, Time(0)));
entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.3", 6883));
entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.4", 6884, Time(0)));
entry.removeStalePeerAddrEntry(10);
CPPUNIT_ASSERT_EQUAL((size_t)2, entry.countPeerAddrEntry());
const std::deque<PeerAddrEntry>& peerAddrEntries = entry.getPeerAddrEntries();
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peerAddrEntries[0].getIPAddress());
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.3"), peerAddrEntries[1].getIPAddress());
}
void DHTPeerAnnounceEntryTest::testEmpty()
{
unsigned char infohash[DHT_ID_LENGTH];
memset(infohash, 0xff, DHT_ID_LENGTH);
{
DHTPeerAnnounceEntry entry(infohash);
entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881));
CPPUNIT_ASSERT(!entry.empty());
}
{
DHTPeerAnnounceEntry entry(infohash);
entry.setPeerStorage(SharedHandle<PeerStorage>(new MockPeerStorage()));
CPPUNIT_ASSERT(!entry.empty());
}
{
DHTPeerAnnounceEntry entry(infohash);
CPPUNIT_ASSERT(entry.empty());
}
}
void DHTPeerAnnounceEntryTest::testAddPeerAddrEntry()
{
unsigned char infohash[DHT_ID_LENGTH];
memset(infohash, 0xff, DHT_ID_LENGTH);
DHTPeerAnnounceEntry entry(infohash);
entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881, Time(0)));
entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6882));
CPPUNIT_ASSERT_EQUAL((size_t)2, entry.countPeerAddrEntry());
entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881));
CPPUNIT_ASSERT_EQUAL((size_t)2, entry.countPeerAddrEntry());
CPPUNIT_ASSERT(0 != entry.getPeerAddrEntries()[0].getLastUpdated().getTime());
}
void DHTPeerAnnounceEntryTest::testGetPeers()
{
unsigned char infohash[DHT_ID_LENGTH];
memset(infohash, 0xff, DHT_ID_LENGTH);
SharedHandle<MockPeerStorage> peerStorage(new MockPeerStorage());
{
SharedHandle<Peer> activePeers[2];
activePeers[0].reset(new Peer("192.168.0.3", 6883));
activePeers[1].reset(new Peer("192.168.0.4", 6884));
peerStorage->setActivePeers(std::deque<SharedHandle<Peer> >(&activePeers[0],
&activePeers[2]));
}
DHTPeerAnnounceEntry entry(infohash);
{
std::deque<SharedHandle<Peer> > peers;
entry.getPeers(peers);
CPPUNIT_ASSERT_EQUAL((size_t)0, peers.size());
}
entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881, Time(0)));
entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.2", 6882));
{
std::deque<SharedHandle<Peer> > peers;
entry.getPeers(peers);
CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peers[0]->ipaddr);
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peers[0]->port);
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peers[1]->ipaddr);
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, peers[1]->port);
}
entry.setPeerStorage(peerStorage);
{
std::deque<SharedHandle<Peer> > peers;
entry.getPeers(peers);
CPPUNIT_ASSERT_EQUAL((size_t)4, peers.size());
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peers[0]->ipaddr);
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peers[0]->port);
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peers[1]->ipaddr);
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, peers[1]->port);
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.3"), peers[2]->ipaddr);
CPPUNIT_ASSERT_EQUAL((uint16_t)6883, peers[2]->port);
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.4"), peers[3]->ipaddr);
CPPUNIT_ASSERT_EQUAL((uint16_t)6884, peers[3]->port);
}
}
} // namespace aria2