aria2/test/DHTPingMessageTest.cc

102 lines
3.0 KiB
C++
Raw Normal View History

#include "DHTPingMessage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "DHTNode.h"
#include "Exception.h"
#include "util.h"
#include "MockDHTMessageFactory.h"
#include "MockDHTMessageDispatcher.h"
#include "MockDHTMessage.h"
#include "bencode2.h"
namespace aria2 {
class DHTPingMessageTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DHTPingMessageTest);
CPPUNIT_TEST(testGetBencodedMessage);
CPPUNIT_TEST(testDoReceivedAction);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
void testGetBencodedMessage();
void testDoReceivedAction();
class MockDHTMessageFactory2:public MockDHTMessageFactory {
public:
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
virtual SharedHandle<DHTResponseMessage>
createPingReplyMessage(const SharedHandle<DHTNode>& remoteNode,
const unsigned char* remoteNodeID,
const std::string& transactionID)
{
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
return SharedHandle<MockDHTResponseMessage>
(new MockDHTResponseMessage(localNode_, remoteNode, "ping_reply",
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
transactionID));
}
};
};
CPPUNIT_TEST_SUITE_REGISTRATION(DHTPingMessageTest);
void DHTPingMessageTest::testGetBencodedMessage()
{
SharedHandle<DHTNode> localNode(new DHTNode());
SharedHandle<DHTNode> remoteNode(new DHTNode());
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
DHTPingMessage msg(localNode, remoteNode, transactionID);
msg.setVersion("A200");
std::string msgbody = msg.getBencodedMessage();
Dict dict;
dict.put("t", transactionID);
dict.put("v", "A200");
dict.put("y", "q");
dict.put("q", "ping");
SharedHandle<Dict> aDict = Dict::g();
aDict->put("id", String::g(localNode->getID(), DHT_ID_LENGTH));
dict.put("a", aDict);
CPPUNIT_ASSERT_EQUAL(bencode2::encode(&dict), msgbody);
}
void DHTPingMessageTest::testDoReceivedAction()
{
SharedHandle<DHTNode> localNode(new DHTNode());
SharedHandle<DHTNode> remoteNode(new DHTNode());
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
MockDHTMessageDispatcher dispatcher;
MockDHTMessageFactory2 factory;
factory.setLocalNode(localNode);
DHTPingMessage msg(localNode, remoteNode, transactionID);
msg.setMessageDispatcher(WeakHandle<DHTMessageDispatcher>(&dispatcher));
msg.setMessageFactory(WeakHandle<DHTMessageFactory>(&factory));
msg.doReceivedAction();
CPPUNIT_ASSERT_EQUAL((size_t)1, dispatcher.messageQueue_.size());
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
SharedHandle<MockDHTResponseMessage> m
(dynamic_pointer_cast<MockDHTResponseMessage>
(dispatcher.messageQueue_[0].message_));
CPPUNIT_ASSERT(localNode == m->getLocalNode());
CPPUNIT_ASSERT(remoteNode == m->getRemoteNode());
CPPUNIT_ASSERT_EQUAL(std::string("ping_reply"), m->getMessageType());
CPPUNIT_ASSERT_EQUAL(msg.getTransactionID(), m->getTransactionID());
}
} // namespace aria2