aria2/test/BtCancelMessageTest.cc
Tatsuhiro Tsujikawa c064a2cd9e 2008-02-10 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Extract the Peer class's member variables, which are only needed 
after
	PeerInteractionCommand, into PeerSessionResource class.
	This class is instantiated in PeerInteractionCommand class's 
ctor and
	released in its dtor.
	This will make Peer class lightweight and uses less memory for 
peers
	which are not connected and wait in the queue.
	* src/PeerChokeCommand.cc
	* src/PeerSessionResource.{h, cc}
	* src/PeerInteractionCommand.cc
	* src/PeerAbstractCommand.cc: Note: 0 is given to onAbort() 
function.
	* src/DefaultBtInteractive.cc
	* src/BtPieceMessage.cc
	* src/BtInterestedMessage.cc
	* src/BtUnchokeMessage.cc
	* src/DefaultPeerStorage.{h, cc}
	* src/PeerInitiateConnectionCommand.cc
	* src/ActivePeerConnectionCommand.cc
	* src/BtNotInterestedMessage.cc
	* src/DefaultBtMessageDispatcher.cc
	* src/BtChokeMessage.cc
	* src/BtRequestMessage.cc
	* src/Peer.{h, cc}
	* src/BtRegistry.h
	* src/TrackerWatcherCommand.cc
	* src/PeerReceiveHandshakeCommand.cc
	* test/BtExtendedMessageTest.cc
	* test/BtAllowedFastMessageTest.cc
	* test/BtCancelMessageTest.cc
	* test/DefaultPieceStorageTest.cc
	* test/BtBitfieldMessageTest.cc
	* test/BtHaveMessageTest.cc
	* test/BtNotInterestedMessageTest.cc
	* test/BtRequestMessageTest.cc
	* test/PeerSessionResourceTest.cc
	* test/DefaultBtMessageDispatcherTest.cc
	* test/PeerTest.cc
	* test/BtInterestedMessageTest.cc
	* test/BtRejectMessageTest.cc
	* test/BtChokeMessageTest.cc
	* test/DefaultPeerStorageTest.cc
	* test/BtHaveNoneMessageTest.cc
	* test/BtHaveAllMessageTest.cc
	* test/DefaultExtensionMessageFactoryTest.cc
	* test/BtUnchokeMessageTest.cc
	* test/DefaultBtMessageFactoryTest.cc
	* test/HandshakeExtensionMessageTest.cc
	* test/UTPexExtensionMessageTest.cc
	* test/DefaultBtRequestFactoryTest.cc
	* test/BtPieceMessageTest.cc
	
	Removed typedef PeerStats.
	* src/PeerStat.h
	* src/SegmentMan.cc
2008-02-09 17:14:40 +00:00

129 lines
3.8 KiB
C++

#include "BtCancelMessage.h"
#include "PeerMessageUtil.h"
#include "MockBtMessageDispatcher.h"
#include "MockBtContext.h"
#include "Peer.h"
#include "FileEntry.h"
#include "PeerObject.h"
#include "Piece.h"
#include "BtRegistry.h"
#include "BtMessageFactory.h"
#include "BtRequestFactory.h"
#include "BtMessageReceiver.h"
#include "PeerConnection.h"
#include "ExtensionMessageFactory.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>
namespace aria2 {
class BtCancelMessageTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(BtCancelMessageTest);
CPPUNIT_TEST(testCreate);
CPPUNIT_TEST(testGetMessage);
CPPUNIT_TEST(testDoReceivedAction);
CPPUNIT_TEST_SUITE_END();
private:
public:
BtCancelMessageTest():peer(0), btContext(0) {}
SharedHandle<Peer> peer;
SharedHandle<MockBtContext> btContext;
void setUp() {
BtRegistry::unregisterAll();
peer = new Peer("host", 6969);
btContext = new MockBtContext();
btContext->setInfoHash((const unsigned char*)"12345678901234567890");
BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(),
new PeerObjectCluster());
PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), new PeerObject());
}
void testCreate();
void testGetMessage();
void testDoReceivedAction();
class MockBtMessageDispatcher2 : public MockBtMessageDispatcher {
public:
int32_t index;
int32_t begin;
int32_t length;
public:
MockBtMessageDispatcher2():index(0),
begin(0),
length(0) {}
virtual void doCancelSendingPieceAction(int32_t index, int32_t begin, int32_t length) {
this->index = index;
this->begin = begin;
this->length = length;
}
};
};
CPPUNIT_TEST_SUITE_REGISTRATION(BtCancelMessageTest);
void BtCancelMessageTest::testCreate() {
unsigned char msg[17];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 8);
PeerMessageUtil::setIntParam(&msg[5], 12345);
PeerMessageUtil::setIntParam(&msg[9], 256);
PeerMessageUtil::setIntParam(&msg[13], 1024);
SharedHandle<BtCancelMessage> pm = BtCancelMessage::create(&msg[4], 13);
CPPUNIT_ASSERT_EQUAL((int8_t)8, pm->getId());
CPPUNIT_ASSERT_EQUAL((int32_t)12345, pm->getIndex());
CPPUNIT_ASSERT_EQUAL((int32_t)256, pm->getBegin());
CPPUNIT_ASSERT_EQUAL((int32_t)1024, pm->getLength());
// case: payload size is wrong
try {
unsigned char msg[18];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 14, 8);
BtCancelMessage::create(&msg[4], 14);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
}
// case: id is wrong
try {
unsigned char msg[17];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 9);
BtCancelMessage::create(&msg[4], 13);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
}
}
void BtCancelMessageTest::testGetMessage() {
BtCancelMessage msg;
msg.setIndex(12345);
msg.setBegin(256);
msg.setLength(1024);
unsigned char data[17];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 13, 8);
PeerMessageUtil::setIntParam(&data[5], 12345);
PeerMessageUtil::setIntParam(&data[9], 256);
PeerMessageUtil::setIntParam(&data[13], 1024);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);
}
void BtCancelMessageTest::testDoReceivedAction() {
BtCancelMessage msg;
msg.setIndex(1);
msg.setBegin(2*16*1024);
msg.setLength(16*1024);
msg.setBtContext(btContext);
msg.setPeer(peer);
SharedHandle<MockBtMessageDispatcher2> dispatcher = new MockBtMessageDispatcher2();
msg.setBtMessageDispatcher(dispatcher);
msg.doReceivedAction();
CPPUNIT_ASSERT_EQUAL(msg.getIndex(), dispatcher->index);
CPPUNIT_ASSERT_EQUAL(msg.getBegin(), dispatcher->begin);
CPPUNIT_ASSERT_EQUAL(msg.getLength(), dispatcher->length);
}
} // namespace aria2