2006-12-24 06:25:21 +00:00
|
|
|
#include "BtRequestMessage.h"
|
|
|
|
#include "PeerMessageUtil.h"
|
|
|
|
#include "MockBtContext.h"
|
|
|
|
#include "MockBtMessage.h"
|
|
|
|
#include "MockPieceStorage.h"
|
|
|
|
#include "MockBtMessageFactory.h"
|
|
|
|
#include "MockBtMessageDispatcher.h"
|
|
|
|
#include "DefaultBtContext.h"
|
|
|
|
#include "BtAbortOutstandingRequestEvent.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Peer.h"
|
|
|
|
#include "BtRegistry.h"
|
|
|
|
#include "PeerObject.h"
|
|
|
|
#include "BtMessageReceiver.h"
|
|
|
|
#include "BtRequestFactory.h"
|
|
|
|
#include "PeerConnection.h"
|
|
|
|
#include "ExtensionMessageFactory.h"
|
|
|
|
#include "FileEntry.h"
|
2008-04-20 00:50:22 +00:00
|
|
|
#include "BtHandshakeMessage.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include <cstring>
|
2006-12-24 06:25:21 +00:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
class BtRequestMessageTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtRequestMessageTest);
|
|
|
|
CPPUNIT_TEST(testCreate);
|
|
|
|
CPPUNIT_TEST(testGetMessage);
|
|
|
|
CPPUNIT_TEST(testDoReceivedAction_hasPieceAndAmNotChoking);
|
|
|
|
CPPUNIT_TEST(testDoReceivedAction_hasPieceAndAmChokingAndFastExtensionEnabled);
|
|
|
|
CPPUNIT_TEST(testDoReceivedAction_hasPieceAndAmChokingAndFastExtensionDisabled);
|
|
|
|
CPPUNIT_TEST(testDoReceivedAction_doesntHavePieceAndFastExtensionEnabled);
|
|
|
|
CPPUNIT_TEST(testDoReceivedAction_doesntHavePieceAndFastExtensionDisabled);
|
|
|
|
CPPUNIT_TEST(testHandleAbortRequestEvent);
|
|
|
|
CPPUNIT_TEST(testHandleAbortRequestEvent_indexNoMatch);
|
|
|
|
CPPUNIT_TEST(testHandleAbortRequestEvent_alreadyInvalidated);
|
|
|
|
CPPUNIT_TEST(testHandleAbortRequestEvent_sendingInProgress);
|
|
|
|
CPPUNIT_TEST(testToString);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
void testCreate();
|
|
|
|
void testGetMessage();
|
|
|
|
void testDoReceivedAction_hasPieceAndAmNotChoking();
|
|
|
|
void testDoReceivedAction_hasPieceAndAmChokingAndFastExtensionEnabled();
|
|
|
|
void testDoReceivedAction_hasPieceAndAmChokingAndFastExtensionDisabled();
|
|
|
|
void testDoReceivedAction_doesntHavePieceAndFastExtensionEnabled();
|
|
|
|
void testDoReceivedAction_doesntHavePieceAndFastExtensionDisabled();
|
|
|
|
void testHandleAbortRequestEvent();
|
|
|
|
void testHandleAbortRequestEvent_indexNoMatch();
|
|
|
|
void testHandleAbortRequestEvent_alreadyInvalidated();
|
|
|
|
void testHandleAbortRequestEvent_sendingInProgress();
|
|
|
|
void testToString();
|
|
|
|
|
|
|
|
class MockPieceStorage2 : public MockPieceStorage {
|
|
|
|
public:
|
2008-03-08 08:04:28 +00:00
|
|
|
virtual bool hasPiece(size_t index) {
|
2006-12-24 06:25:21 +00:00
|
|
|
return index == 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MockBtMessage2 : public MockBtMessage {
|
|
|
|
public:
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string type;
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t index;
|
|
|
|
uint32_t begin;
|
|
|
|
size_t length;
|
2006-12-24 06:25:21 +00:00
|
|
|
public:
|
2008-03-09 12:24:01 +00:00
|
|
|
MockBtMessage2(std::string type, size_t index, uint32_t begin, size_t length):type(type), index(index), begin(begin), length(length) {}
|
2006-12-24 06:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef SharedHandle<MockBtMessage2> MockBtMessage2Handle;
|
|
|
|
|
|
|
|
class MockBtMessageFactory2 : public MockBtMessageFactory {
|
|
|
|
public:
|
2008-02-08 15:53:45 +00:00
|
|
|
virtual SharedHandle<BtMessage>
|
2008-03-09 12:24:01 +00:00
|
|
|
createPieceMessage(size_t index, uint32_t begin, size_t length) {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> btMsg(new MockBtMessage2("piece", index, begin, length));
|
2006-12-24 06:25:21 +00:00
|
|
|
return btMsg;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
virtual SharedHandle<BtMessage>
|
2008-03-09 12:24:01 +00:00
|
|
|
createRejectMessage(size_t index, uint32_t begin, size_t length) {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> btMsg(new MockBtMessage2("reject", index, begin, length));
|
2006-12-24 06:25:21 +00:00
|
|
|
return btMsg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef SharedHandle<MockBtMessageFactory2> MockBtMessageFactory2Handle;
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<Peer> peer;
|
|
|
|
SharedHandle<MockBtMessageDispatcher> dispatcher;
|
|
|
|
SharedHandle<BtRequestMessage> msg;
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
void setUp() {
|
2007-10-11 16:58:24 +00:00
|
|
|
BtRegistry::unregisterAll();
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtContext> btContext(new MockBtContext());
|
2006-12-24 06:25:21 +00:00
|
|
|
btContext->setInfoHash((const unsigned char*)"12345678901234567890");
|
|
|
|
btContext->setPieceLength(16*1024);
|
|
|
|
btContext->setTotalLength(256*1024);
|
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockPieceStorage> pieceStorage(new MockPieceStorage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(),
|
|
|
|
pieceStorage);
|
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
peer.reset(new Peer("host", 6969));
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->allocateSessionResource(btContext->getPieceLength(),
|
|
|
|
btContext->getTotalLength());
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<PeerObjectCluster> cluster(new PeerObjectCluster());
|
2006-12-24 06:25:21 +00:00
|
|
|
BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(),
|
2008-04-20 00:50:22 +00:00
|
|
|
cluster);
|
|
|
|
SharedHandle<PeerObject> po(new PeerObject());
|
|
|
|
PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), po);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
dispatcher.reset(new MockBtMessageDispatcher());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
PEER_OBJECT(btContext, peer)->btMessageDispatcher = dispatcher;
|
2008-04-20 00:50:22 +00:00
|
|
|
PEER_OBJECT(btContext, peer)->btMessageFactory.reset(new MockBtMessageFactory2());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
msg.reset(new BtRequestMessage());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg->setBtContext(btContext);
|
|
|
|
msg->setPeer(peer);
|
|
|
|
msg->setIndex(1);
|
|
|
|
msg->setBegin(16);
|
|
|
|
msg->setLength(32);
|
|
|
|
msg->setBlockIndex(2);
|
2007-01-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To decrease CPU usage in bittorrent download, calculation
results in
BitfieldMan were cached and realtime fetching PeerObject was
removed
with WeakHandle introduced. Option values are set to the objects
by setter before download begins.
* src/DefaultBtRequestFactory.cc: Use messageFactory member.
* src/DefaultBtRequestFactory.h
(dispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(messageFactory): New variable.
(setBtMessageDispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(setBtMessageFactory): New function.
* src/DefaultBtMessageDispatcher.cc:
(sendMessages): Use maxUploadSpeedLimit instead of fetching the
value
from Option.
(checkRequestSlotAndDoNecessaryThing): Use requestTimeout
instead of
feating the value from Option.
Use messageFactory member.
* src/PeerInteractionCommand.cc
(PeerInteractionCommand): Added maxDownloadSpeedLimit.
Add reverse dependencies to factory object.
Set maxUploadSpeedLimit and requestTimeout and messageFactory to
dispatcher.
Set messageFactory to receiver.
Set keepAliveInterval and maxDownloadSpeedLimit and
messageFactory to
btInteractive.
Set receiver to peerObject.
Set maxDownloadSpeedLimit to this.
(executeInternal): Use maxDownloadSpeedLimit member.
* src/BtChokeMessage.cc
(doReceivedAction): Use dispatcher, requestFactory member.
(onSendComplete): Use dispatcher member.
* src/PeerInteractionCommand.h
(maxDownloadSpeedLimit): New variable.
* src/DefaultBtMessageReceiver.h
(peerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(messageFactory): New variable.
(setPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(getPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.cc
(initiateHandshake): Use messageFactory member.
(addBitfieldMessageToQueue): Use messageFactory member.
(addAllowedFastMessageToQueue): Use messageFactory member.
(decideChoking): Use messageFactory member.
(checkHave): Use messageFactory member.
(sendKeepAlive): Use keepAliveInterval, messageFactory member.
(receiveMessages): Use maxDownloadSpeedLimit member.
(decideInterest): Use messageFactory member.
* src/BtRequestMessage.cc
(doReceivedAction): Use messageFactory, dispatcher member.
(onQueued): Use dispatcher member.
* src/BtPieceMessage.cc
(doReceivedAction): Use dispatcher member.
(send): Use peerConnection member.
(onWrongPiece): Use requestFactory member.
(handleChokingEvent): Use messageFactory, dispatcher member.
(handleCancelSendingPieceEvent): Use messageFactory, dispatcher
member.
* src/BtMessageDispatcher.h
(BtMessageDispatcherWeakHandle): New type definition.
* src/SimpleBtMessage.cc
(send): Use peerConnection member.
* src/BtRejectMessage.cc
(doReceivedAction): Use dispatcher member.
* src/DefaultBtMessageDispatcher.h
(Option.h): Removed include.
(messageFactory): New variable.
(option): Removed.
(maxUploadSpeedLimit): New variable.
(requestTimeout): New variable.
(DefaultBtMessageDispatcher): Removed option.
Added maxUploadSpeedLimit, requestTimeout.
(setOption): Removed.
(getOption): Removed.
(setMaxUploadSpeedLimit): New function.
(setRequestTimeout): New function.
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.h
(btMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(btRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(peerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(messageFactory): New variable.
(option): Removed.
(keepAliveInterval): New variable.
(maxDownloadSpeedLimit): New variable.
(DefaultBtInteractive): Added keepAliveInterval,
maxDownloadSpeedLimit.
(setBtMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(setBtRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(setPeerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(setOption): Removed.
(setKeepAliveInterval): New function.
(setMaxDownloadSpeedLimit): New function.
(setBtMessageFactory): New function.
* src/BitfieldMan.h
(cachedNumMissingBlock): New variable.
(cachedNumFilteredBlock): New variable.
(cachedCompletedLength): New variable.
(cachedFilteredComletedLength): New variable.
(cachedFilteredTotalLength): New variable.
(countMissingBlockNow): New function.
(countFilteredBlockNow): New function.
(getFilteredTotalLengthNow): New function.
(getCompletedLengthNow): New function.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/AbstractBtMessage.h
(BtMessageDispatcher.h): New include.
(PeerConnection.h): New include.
(BtRequestFactory.h): New include
(BtMessageFactory.h): New include.
(dispatcher): New variable.
(messageFactory): New variable.
(peerConnection: New variable.
(setBtMessageDispatcher): New function.
(setPeerConnection): New function.
(setBtMessageFactory): New function.
(setBtRequestFactory): New function.
* src/DefaultBtMessageFactory.cc
(setCommonProperty): Set dispatcher, requestFactory, this,
peerConnection to msg.
* src/BtRegistry.h
(BT_MESSAGE_RECEIVER): New macro.
* src/PeerConnection.h
(PeerConnectionWeakHandle): New type definition.
* src/BtMessageFactory.h
(BtMessageFactoryWeakHandle): New type definition.
* src/BitfieldMan.cc
(BitfieldMan): Added cachedNumMissingBlock,
cachedNumFilteredBlock,
cachedCompletedLength, cachedFilteredComletedLength,
cachedFilteredTotalLength.
Call updateCache().
(countMissingBlock): Return cachedNumMissingBlock.
(countMissingBlockNow): New function.
(countBlock): Return cachedNumFilteredBlock if filterEnabled is
true.
(countFilteredBlockNow): New function.
(setBit): Call updateCache().
(unsetBit): Call updateCache().
(setBitfield): Call updateCache().
(clearAllBit): Call updateCache().
(setAllBit): Use setBitInternal instead of setBit.
Call updateCache().
(addFilter): Call updateCache().
(enableFilter): Call updateCache().
(disableFilter): Call updateCache().
(clearFilter): Call updateCache().
(getFilteredTotalLength): Return cachedFilteredTotalLength.
(getFilteredTotalLengthNow): New function.
(getCompletedLength): Return cachedCompletedLength.
(getCompletedLengthNow): New function.
(getFilteredCompletedLength): Return
cachedFilteredComletedLength.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/BtMessageReceiver.h
(BtMessageReceiverWeakHandle): New type definition.
* src/DefaultBtMessageReceiver.cc
(receiveHandshake): Use messageFactory member.
(sendHandshake): Use messageFactory member.
(receiveMessage): Use messageFactory member.
* src/DefaultBtMessageFactory.h
(dispatcher): New variable.
(requestFactory): New variable.
(peerConnection): New variablle.
(setBtMessageDispatcher): New function.
(setBtRequestFactory): New function.
(setPeerConnection): New function.
* src/SharedHandle.h
(RefCount): New class.
(WeakHandle): New class.
* src/PeerObject.h
(BtMessageReceiver.h): New include.
(PeerObject): Added btMessageReceiver.
(btMessageReceiver): New variable.
* src/Util.cc
(countBit): Simplified.
* src/BtCancelMessage.cc
(doReceivedAction): Use dispatcher member.
* src/BtRequestFactory.h
(BtRequestFactoryWeakHandle): New type definition.
* src/PeerStorage.h
(downloadSpeed): int -> uint32_t
(uploadSpeed): int -> uint32_t
(sessionDownloadLength): long long int -> uint64_t
(sessionUploadLength): long long int -> uint64_t
2007-01-16 15:20:26 +00:00
|
|
|
msg->setBtMessageDispatcher(dispatcher);
|
|
|
|
msg->setBtMessageFactory(BT_MESSAGE_FACTORY(btContext, peer));
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtRequestMessageTest);
|
|
|
|
|
|
|
|
void BtRequestMessageTest::testCreate() {
|
|
|
|
unsigned char msg[17];
|
|
|
|
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 6);
|
|
|
|
PeerMessageUtil::setIntParam(&msg[5], 12345);
|
|
|
|
PeerMessageUtil::setIntParam(&msg[9], 256);
|
|
|
|
PeerMessageUtil::setIntParam(&msg[13], 1024);
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<BtRequestMessage> pm = BtRequestMessage::create(&msg[4], 13);
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)6, pm->getId());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint32_t)256, pm->getBegin());
|
2008-07-31 12:28:12 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pm->getLength());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
// case: payload size is wrong
|
|
|
|
try {
|
|
|
|
unsigned char msg[18];
|
|
|
|
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 14, 6);
|
|
|
|
BtRequestMessage::create(&msg[4], 14);
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2006-12-24 06:25:21 +00:00
|
|
|
} catch(...) {
|
|
|
|
}
|
|
|
|
// case: id is wrong
|
|
|
|
try {
|
|
|
|
unsigned char msg[17];
|
|
|
|
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 7);
|
|
|
|
BtRequestMessage::create(&msg[4], 13);
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2006-12-24 06:25:21 +00:00
|
|
|
} catch(...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtRequestMessageTest::testGetMessage() {
|
|
|
|
BtRequestMessage msg;
|
|
|
|
msg.setIndex(12345);
|
|
|
|
msg.setBegin(256);
|
|
|
|
msg.setLength(1024);
|
|
|
|
unsigned char data[17];
|
|
|
|
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 13, 6);
|
|
|
|
PeerMessageUtil::setIntParam(&data[5], 12345);
|
|
|
|
PeerMessageUtil::setIntParam(&data[9], 256);
|
|
|
|
PeerMessageUtil::setIntParam(&data[13], 1024);
|
|
|
|
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmNotChoking() {
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->amChoking(false);
|
2006-12-24 06:25:21 +00:00
|
|
|
msg->doReceivedAction();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, dispatcher->messageQueue.size());
|
|
|
|
MockBtMessage2* pieceMsg = (MockBtMessage2*)dispatcher->messageQueue.front().get();
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("piece"), pieceMsg->type);
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, pieceMsg->index);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint32_t)16, pieceMsg->begin);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)32, pieceMsg->length);
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmChokingAndFastExtensionEnabled() {
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->amChoking(true);
|
2006-12-24 06:25:21 +00:00
|
|
|
peer->setFastExtensionEnabled(true);
|
|
|
|
msg->doReceivedAction();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, dispatcher->messageQueue.size());
|
|
|
|
MockBtMessage2* pieceMsg = (MockBtMessage2*)dispatcher->messageQueue.front().get();
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("reject"), pieceMsg->type);
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, pieceMsg->index);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint32_t)16, pieceMsg->begin);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)32, pieceMsg->length);
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmChokingAndFastExtensionDisabled() {
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->amChoking(true);
|
2006-12-24 06:25:21 +00:00
|
|
|
msg->doReceivedAction();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, dispatcher->messageQueue.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtRequestMessageTest::testDoReceivedAction_doesntHavePieceAndFastExtensionEnabled() {
|
|
|
|
msg->setIndex(2);
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->amChoking(false);
|
2006-12-24 06:25:21 +00:00
|
|
|
peer->setFastExtensionEnabled(true);
|
|
|
|
msg->doReceivedAction();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, dispatcher->messageQueue.size());
|
|
|
|
MockBtMessage2* pieceMsg = (MockBtMessage2*)dispatcher->messageQueue.front().get();
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("reject"), pieceMsg->type);
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, pieceMsg->index);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint32_t)16, pieceMsg->begin);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)32, pieceMsg->length);
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BtRequestMessageTest::testDoReceivedAction_doesntHavePieceAndFastExtensionDisabled() {
|
|
|
|
msg->setIndex(2);
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->amChoking(false);
|
2006-12-24 06:25:21 +00:00
|
|
|
msg->doReceivedAction();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, dispatcher->messageQueue.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtRequestMessageTest::testHandleAbortRequestEvent() {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<Piece> piece(new Piece(1, 16*1024));
|
|
|
|
SharedHandle<BtAbortOutstandingRequestEvent> event
|
|
|
|
(new BtAbortOutstandingRequestEvent(piece));
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT(!msg->isInvalidate());
|
|
|
|
msg->handleEvent(event);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(msg->isInvalidate());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtRequestMessageTest::testHandleAbortRequestEvent_indexNoMatch() {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<Piece> piece(new Piece(2, 16*1024));
|
|
|
|
SharedHandle<BtAbortOutstandingRequestEvent> event
|
|
|
|
(new BtAbortOutstandingRequestEvent(piece));
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT(!msg->isInvalidate());
|
|
|
|
CPPUNIT_ASSERT(!msg->isSendingInProgress());
|
|
|
|
msg->handleEvent(event);
|
|
|
|
CPPUNIT_ASSERT(!msg->isInvalidate());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtRequestMessageTest::testHandleAbortRequestEvent_alreadyInvalidated() {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<Piece> piece(new Piece(1, 16*1024));
|
|
|
|
SharedHandle<BtAbortOutstandingRequestEvent> event
|
|
|
|
(new BtAbortOutstandingRequestEvent(piece));
|
2006-12-24 06:25:21 +00:00
|
|
|
msg->setInvalidate(true);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(msg->isInvalidate());
|
|
|
|
CPPUNIT_ASSERT(!msg->isSendingInProgress());
|
|
|
|
msg->handleEvent(event);
|
|
|
|
CPPUNIT_ASSERT(msg->isInvalidate());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtRequestMessageTest::testHandleAbortRequestEvent_sendingInProgress() {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<Piece> piece(new Piece(1, 16*1024));
|
|
|
|
SharedHandle<BtAbortOutstandingRequestEvent> event
|
|
|
|
(new BtAbortOutstandingRequestEvent(piece));
|
2006-12-24 06:25:21 +00:00
|
|
|
msg->setSendingInProgress(true);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(!msg->isInvalidate());
|
|
|
|
CPPUNIT_ASSERT(msg->isSendingInProgress());
|
|
|
|
msg->handleEvent(event);
|
|
|
|
CPPUNIT_ASSERT(!msg->isInvalidate());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtRequestMessageTest::testToString() {
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("request index=1, begin=16, length=32"),
|
2006-12-24 06:25:21 +00:00
|
|
|
msg->toString());
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|