2006-12-24 06:25:21 +00:00
|
|
|
#include "BtRejectMessage.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2009-09-29 14:52:42 +00:00
|
|
|
#include "bittorrent_helper.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
#include "Peer.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "FileEntry.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
#include "MockBtMessageDispatcher.h"
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
class BtRejectMessageTest : public CppUnit::TestFixture {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtRejectMessageTest);
|
|
|
|
CPPUNIT_TEST(testCreate);
|
2010-03-04 16:24:03 +00:00
|
|
|
CPPUNIT_TEST(testCreateMessage);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_TEST(testDoReceivedAction);
|
|
|
|
CPPUNIT_TEST(testDoReceivedActionNoMatch);
|
|
|
|
CPPUNIT_TEST(testDoReceivedActionFastExtensionDisabled);
|
|
|
|
CPPUNIT_TEST(testToString);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
private:
|
2006-12-24 06:25:21 +00:00
|
|
|
public:
|
|
|
|
void testCreate();
|
2010-03-04 16:24:03 +00:00
|
|
|
void testCreateMessage();
|
2006-12-24 06:25:21 +00:00
|
|
|
void testDoReceivedAction();
|
|
|
|
void testDoReceivedActionNoMatch();
|
|
|
|
void testDoReceivedActionFastExtensionDisabled();
|
|
|
|
void testToString();
|
|
|
|
|
|
|
|
class MockBtMessageDispatcher2 : public MockBtMessageDispatcher {
|
|
|
|
public:
|
2013-06-29 15:31:50 +00:00
|
|
|
std::unique_ptr<RequestSlot> slot;
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void setRequestSlot(std::unique_ptr<RequestSlot> s) { slot = std::move(s); }
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
virtual const RequestSlot*
|
|
|
|
getOutstandingRequest(size_t index, int32_t begin,
|
|
|
|
int32_t length) CXX11_OVERRIDE
|
2013-07-06 06:54:03 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (slot && slot->getIndex() == index && slot->getBegin() == begin &&
|
|
|
|
slot->getLength() == length) {
|
2013-06-29 15:31:50 +00:00
|
|
|
return slot.get();
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-06-29 15:31:50 +00:00
|
|
|
return nullptr;
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-06 06:54:03 +00:00
|
|
|
virtual void removeOutstandingRequest(const RequestSlot* s) CXX11_OVERRIDE
|
2013-06-29 15:31:50 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (slot->getIndex() == s->getIndex() &&
|
|
|
|
slot->getBegin() == s->getBegin() &&
|
|
|
|
slot->getLength() == s->getLength()) {
|
2013-06-29 15:31:50 +00:00
|
|
|
slot.reset();
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
typedef std::shared_ptr<MockBtMessageDispatcher2>
|
|
|
|
MockBtMessageDispatcher2Handle;
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Peer> peer;
|
|
|
|
std::shared_ptr<MockBtMessageDispatcher2> dispatcher;
|
|
|
|
std::shared_ptr<BtRejectMessage> msg;
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void setUp()
|
|
|
|
{
|
2008-04-20 00:50:22 +00:00
|
|
|
peer.reset(new Peer("host", 6969));
|
2015-06-21 09:04:30 +00:00
|
|
|
peer->allocateSessionResource(1_k, 1_m);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
dispatcher.reset(new MockBtMessageDispatcher2());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
msg.reset(new BtRejectMessage());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg->setPeer(peer);
|
|
|
|
msg->setIndex(1);
|
|
|
|
msg->setBegin(16);
|
|
|
|
msg->setLength(32);
|
2010-11-12 12:48:48 +00:00
|
|
|
msg->setBtMessageDispatcher(dispatcher.get());
|
2008-04-20 00:50:22 +00:00
|
|
|
}
|
2006-12-24 06:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtRejectMessageTest);
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtRejectMessageTest::testCreate()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
unsigned char msg[17];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 16);
|
|
|
|
bittorrent::setIntParam(&msg[5], 12345);
|
|
|
|
bittorrent::setIntParam(&msg[9], 256);
|
2015-06-21 09:04:30 +00:00
|
|
|
bittorrent::setIntParam(&msg[13], 1_k);
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<BtRejectMessage> pm(BtRejectMessage::create(&msg[4], 13));
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)16, pm->getId());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
|
2011-12-07 15:51:20 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(256, pm->getBegin());
|
2015-06-21 09:04:30 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)1_k, pm->getLength());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
// case: payload size is wrong
|
|
|
|
try {
|
|
|
|
unsigned char msg[18];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 14, 16);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtRejectMessage::create(&msg[4], 14);
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
// case: id is wrong
|
|
|
|
try {
|
|
|
|
unsigned char msg[17];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 17);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtRejectMessage::create(&msg[4], 13);
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtRejectMessageTest::testCreateMessage()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
BtRejectMessage msg;
|
|
|
|
msg.setIndex(12345);
|
|
|
|
msg.setBegin(256);
|
2015-06-21 09:04:30 +00:00
|
|
|
msg.setLength(1_k);
|
2006-12-24 06:25:21 +00:00
|
|
|
unsigned char data[17];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(data, sizeof(data), 13, 16);
|
|
|
|
bittorrent::setIntParam(&data[5], 12345);
|
|
|
|
bittorrent::setIntParam(&data[9], 256);
|
2015-06-21 09:04:30 +00:00
|
|
|
bittorrent::setIntParam(&data[13], 1_k);
|
2016-05-15 14:40:09 +00:00
|
|
|
auto rawmsg = msg.createMessage();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)17, rawmsg.size());
|
|
|
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtRejectMessageTest::testDoReceivedAction()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
peer->setFastExtensionEnabled(true);
|
2013-06-29 15:31:50 +00:00
|
|
|
dispatcher->setRequestSlot(make_unique<RequestSlot>(1, 16, 32, 2));
|
2012-10-01 14:52:22 +00:00
|
|
|
|
2013-06-29 15:31:50 +00:00
|
|
|
CPPUNIT_ASSERT(dispatcher->getOutstandingRequest(1, 16, 32));
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
msg->doReceivedAction();
|
|
|
|
|
2013-06-29 15:31:50 +00:00
|
|
|
CPPUNIT_ASSERT(!dispatcher->getOutstandingRequest(1, 16, 32));
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtRejectMessageTest::testDoReceivedActionNoMatch()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
peer->setFastExtensionEnabled(true);
|
2013-06-29 15:31:50 +00:00
|
|
|
dispatcher->setRequestSlot(make_unique<RequestSlot>(2, 16, 32, 2));
|
2012-10-01 14:52:22 +00:00
|
|
|
|
2013-06-29 15:31:50 +00:00
|
|
|
CPPUNIT_ASSERT(dispatcher->getOutstandingRequest(2, 16, 32));
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
msg->doReceivedAction();
|
|
|
|
|
2013-06-29 15:31:50 +00:00
|
|
|
CPPUNIT_ASSERT(dispatcher->getOutstandingRequest(2, 16, 32));
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtRejectMessageTest::testDoReceivedActionFastExtensionDisabled()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
RequestSlot slot(1, 16, 32, 2);
|
2013-06-29 15:31:50 +00:00
|
|
|
dispatcher->setRequestSlot(make_unique<RequestSlot>(1, 16, 32, 2));
|
2012-10-01 14:52:22 +00:00
|
|
|
|
2013-06-29 15:31:50 +00:00
|
|
|
CPPUNIT_ASSERT(dispatcher->getOutstandingRequest(1, 16, 32));
|
2006-12-24 06:25:21 +00:00
|
|
|
try {
|
|
|
|
msg->doReceivedAction();
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
}
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtRejectMessageTest::testToString()
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("reject index=1, begin=16, length=32"),
|
2010-01-05 16:01:46 +00:00
|
|
|
msg->toString());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|