aria2/test/BtPortMessageTest.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

172 lines
5.1 KiB
C++

#include "BtPortMessage.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "Util.h"
#include "array_fun.h"
#include "Peer.h"
#include "DHTNode.h"
#include "DHTRoutingTable.h"
#include "MockDHTTask.h"
#include "MockDHTTaskFactory.h"
#include "MockDHTTaskQueue.h"
namespace aria2 {
class BtPortMessageTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(BtPortMessageTest);
CPPUNIT_TEST(testCreate);
CPPUNIT_TEST(testToString);
CPPUNIT_TEST(testGetMessage);
CPPUNIT_TEST(testDoReceivedAction);
CPPUNIT_TEST(testDoReceivedAction_bootstrap);
CPPUNIT_TEST_SUITE_END();
private:
public:
void setUp() {
}
void testCreate();
void testToString();
void testGetMessage();
void testDoReceivedAction();
void testDoReceivedAction_bootstrap();
class MockDHTTaskFactory2:public MockDHTTaskFactory {
public:
virtual SharedHandle<DHTTask>
createPingTask(const SharedHandle<DHTNode>& remoteNode, size_t numRetry)
{
return SharedHandle<DHTTask>(new MockDHTTask(remoteNode));
}
virtual SharedHandle<DHTTask>
createNodeLookupTask(const unsigned char* targetID)
{
SharedHandle<MockDHTTask> task(new MockDHTTask(SharedHandle<DHTNode>()));
task->setTargetID(targetID);
return task;
}
};
};
CPPUNIT_TEST_SUITE_REGISTRATION(BtPortMessageTest);
void BtPortMessageTest::testCreate() {
unsigned char msg[7];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 3, 9);
PeerMessageUtil::setShortIntParam(&msg[5], 12345);
SharedHandle<BtPortMessage> pm = BtPortMessage::create(&msg[4], 3);
CPPUNIT_ASSERT_EQUAL((uint8_t)9, pm->getId());
CPPUNIT_ASSERT_EQUAL((uint16_t)12345, pm->getPort());
// case: payload size is wrong
try {
unsigned char msg[8];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 4, 9);
BtPortMessage::create(&msg[4], 4);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
}
// case: id is wrong
try {
unsigned char msg[7];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 3, 10);
BtPortMessage::create(&msg[4], 3);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
}
}
void BtPortMessageTest::testToString() {
BtPortMessage msg(1);
CPPUNIT_ASSERT_EQUAL(std::string("port port=1"), msg.toString());
}
void BtPortMessageTest::testGetMessage() {
BtPortMessage msg(6881);
unsigned char data[7];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 3, 9);
PeerMessageUtil::setShortIntParam(&data[5], 6881);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 7) == 0);
}
void BtPortMessageTest::testDoReceivedAction()
{
unsigned char nodeID[DHT_ID_LENGTH];
memset(nodeID, 0, DHT_ID_LENGTH);
SharedHandle<DHTNode> localNode(new DHTNode(nodeID));
// 9 nodes to create at least 2 buckets.
SharedHandle<DHTNode> nodes[9];
for(size_t i = 0; i < arrayLength(nodes); ++i) {
memset(nodeID, 0, DHT_ID_LENGTH);
nodeID[DHT_ID_LENGTH-1] = i+1;
nodes[i].reset(new DHTNode(nodeID));
}
DHTRoutingTable routingTable(localNode);
for(size_t i = 0; i < arrayLength(nodes); ++i) {
routingTable.addNode(nodes[i]);
}
SharedHandle<Peer> peer(new Peer("192.168.0.1", 6881));
BtPortMessage msg(6881);
MockDHTTaskQueue taskQueue;
MockDHTTaskFactory2 taskFactory;
msg.setLocalNode(localNode);
msg.setRoutingTable(WeakHandle<DHTRoutingTable>(&routingTable));
msg.setTaskQueue(WeakHandle<DHTTaskQueue>(&taskQueue));
msg.setTaskFactory(WeakHandle<DHTTaskFactory>(&taskFactory));
msg.setPeer(peer);
msg.doReceivedAction();
CPPUNIT_ASSERT_EQUAL((size_t)1, taskQueue._immediateTaskQueue.size());
SharedHandle<MockDHTTask> task
(dynamic_pointer_cast<MockDHTTask>(taskQueue._immediateTaskQueue[0]));
SharedHandle<DHTNode> node = task->_remoteNode;
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), node->getIPAddress());
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, node->getPort());
}
void BtPortMessageTest::testDoReceivedAction_bootstrap()
{
unsigned char nodeID[DHT_ID_LENGTH];
memset(nodeID, 0, DHT_ID_LENGTH);
nodeID[0] = 0xff;
SharedHandle<DHTNode> localNode(new DHTNode(nodeID));
DHTRoutingTable routingTable(localNode); // no nodes , 1 bucket.
SharedHandle<Peer> peer(new Peer("192.168.0.1", 6881));
BtPortMessage msg(6881);
MockDHTTaskQueue taskQueue;
MockDHTTaskFactory2 taskFactory;
msg.setLocalNode(localNode);
msg.setRoutingTable(WeakHandle<DHTRoutingTable>(&routingTable));
msg.setTaskQueue(WeakHandle<DHTTaskQueue>(&taskQueue));
msg.setTaskFactory(WeakHandle<DHTTaskFactory>(&taskFactory));
msg.setPeer(peer);
msg.doReceivedAction();
CPPUNIT_ASSERT_EQUAL((size_t)2, taskQueue._immediateTaskQueue.size());
SharedHandle<MockDHTTask> task
(dynamic_pointer_cast<MockDHTTask>(taskQueue._immediateTaskQueue[0]));
SharedHandle<DHTNode> node(task->_remoteNode);
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), node->getIPAddress());
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, node->getPort());
SharedHandle<MockDHTTask> task2
(dynamic_pointer_cast<MockDHTTask>(taskQueue._immediateTaskQueue[1]));
CPPUNIT_ASSERT(memcmp(nodeID, task2->_targetID, DHT_ID_LENGTH) == 0);
}
} // namespace aria2