aria2/test/DHTMessageTrackerTest.cc

106 lines
3.2 KiB
C++
Raw Normal View History

#include "DHTMessageTracker.h"
#include <cppunit/extensions/HelperMacros.h>
#include "Exception.h"
#include "util.h"
#include "MockDHTMessage.h"
#include "MockDHTMessageCallback.h"
#include "DHTNode.h"
#include "DHTMessageTrackerEntry.h"
#include "DHTRoutingTable.h"
#include "MockDHTMessageFactory.h"
namespace aria2 {
class DHTMessageTrackerTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DHTMessageTrackerTest);
CPPUNIT_TEST(testMessageArrived);
CPPUNIT_TEST(testHandleTimeout);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
void testMessageArrived();
void testHandleTimeout();
};
CPPUNIT_TEST_SUITE_REGISTRATION(DHTMessageTrackerTest);
void DHTMessageTrackerTest::testMessageArrived()
{
SharedHandle<DHTNode> localNode(new DHTNode());
SharedHandle<DHTRoutingTable> routingTable(new DHTRoutingTable(localNode));
SharedHandle<MockDHTMessageFactory> factory(new MockDHTMessageFactory());
factory->setLocalNode(localNode);
SharedHandle<MockDHTMessage> m1(new MockDHTMessage(localNode,
SharedHandle<DHTNode>(new DHTNode())));
SharedHandle<MockDHTMessage> m2(new MockDHTMessage(localNode,
SharedHandle<DHTNode>(new DHTNode())));
SharedHandle<MockDHTMessage> m3(new MockDHTMessage(localNode,
SharedHandle<DHTNode>(new DHTNode())));
m1->getRemoteNode()->setIPAddress("192.168.0.1");
m1->getRemoteNode()->setPort(6881);
m2->getRemoteNode()->setIPAddress("192.168.0.2");
m2->getRemoteNode()->setPort(6882);
m3->getRemoteNode()->setIPAddress("192.168.0.3");
m3->getRemoteNode()->setPort(6883);
DHTMessageTracker tracker;
tracker.setRoutingTable(routingTable);
tracker.setMessageFactory(factory);
tracker.addMessage(m1, DHT_MESSAGE_TIMEOUT);
2010-06-13 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Rewritten DHTMessageCallback using Visitor pattern. Eliminated dynamic_pointer_cast. * src/DHTMessageCallbackImpl.cc: Removed * src/DHTMessageReceiver.cc * src/DHTAbstractNodeLookupTask.h * src/DHTAnnouncePeerReplyMessage.h * src/DHTReplaceNodeTask.h * src/DHTFindNodeReplyMessage.cc * src/DHTGetPeersReplyMessage.h * src/DHTPeerLookupTask.h * src/DHTMessageCallbackImpl.h: Removed * src/DHTMessageFactory.h * src/DHTNodeLookupTaskCallback.h * src/DHTMessageTracker.h * src/DHTMessageCallbackListener.h: Removed * src/DHTGetPeersReplyMessage.cc * src/DHTMessageCallback.h * src/DHTAnnouncePeerReplyMessage.cc * src/DHTNodeLookupTask.h * src/DHTReplaceNodeTask.cc * src/DHTPeerLookupTaskCallback.cc * src/DHTMessageTracker.cc * src/DHTPingReplyMessage.cc * src/DHTPingTask.cc * src/DHTMessageFactoryImpl.h * src/Makefile.am * src/DHTNodeLookupTask.cc * src/DHTPeerLookupTaskCallback.h * src/DHTPeerLookupTask.cc * src/DHTMessageReceiver.h * src/DHTMessageFactoryImpl.cc * src/DHTResponseMessage.h * src/DHTFindNodeReplyMessage.h * src/DHTPingReplyMessageCallback.h * src/Makefile.in * src/DHTBucketRefreshTask.cc * src/DHTNodeLookupTaskCallback.cc * src/DHTPingTask.h * src/DHTPingReplyMessage.h * src/DHTAbstractNodeLookupTask.cc: Removed * test/DHTMessageTrackerTest.cc * test/DHTPingMessageTest.cc * test/DHTGetPeersMessageTest.cc * test/MockDHTMessage.h * test/MockDHTMessageFactory.h * test/DHTFindNodeMessageTest.cc * test/MockDHTMessageCallback.h * test/DHTAnnouncePeerMessageTest.cc
2010-06-13 11:25:21 +00:00
tracker.addMessage(m2, DHT_MESSAGE_TIMEOUT);
tracker.addMessage(m3, DHT_MESSAGE_TIMEOUT);
{
Dict resDict;
resDict.put("t", m2->getTransactionID());
2012-10-01 14:52:22 +00:00
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
tracker.messageArrived(&resDict, m2->getRemoteNode()->getIPAddress(),
m2->getRemoteNode()->getPort());
SharedHandle<DHTMessage> reply = p.first;
CPPUNIT_ASSERT(reply);
CPPUNIT_ASSERT(!tracker.getEntryFor(m2));
CPPUNIT_ASSERT_EQUAL((size_t)2, tracker.countEntry());
}
{
Dict resDict;
resDict.put("t", m3->getTransactionID());
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
tracker.messageArrived(&resDict, m3->getRemoteNode()->getIPAddress(),
m3->getRemoteNode()->getPort());
SharedHandle<DHTMessage> reply = p.first;
CPPUNIT_ASSERT(reply);
CPPUNIT_ASSERT(!tracker.getEntryFor(m3));
CPPUNIT_ASSERT_EQUAL((size_t)1, tracker.countEntry());
}
{
Dict resDict;
resDict.put("t", m1->getTransactionID());
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
tracker.messageArrived(&resDict, "192.168.1.100", 6889);
SharedHandle<DHTMessage> reply = p.first;
CPPUNIT_ASSERT(!reply);
}
}
void DHTMessageTrackerTest::testHandleTimeout()
{
}
} // namespace aria2