2006-12-24 06:25:21 +00:00
|
|
|
#include "BtCancelMessage.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 "MockBtMessageDispatcher.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Peer.h"
|
|
|
|
#include "FileEntry.h"
|
|
|
|
#include "Piece.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
class BtCancelMessageTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtCancelMessageTest);
|
|
|
|
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_SUITE_END();
|
|
|
|
private:
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Peer> peer;
|
2008-04-20 00:50:22 +00:00
|
|
|
public:
|
2006-12-24 06:25:21 +00:00
|
|
|
void setUp() {
|
2008-04-20 00:50:22 +00:00
|
|
|
peer.reset(new Peer("host", 6969));
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void testCreate();
|
2010-03-04 16:24:03 +00:00
|
|
|
void testCreateMessage();
|
2006-12-24 06:25:21 +00:00
|
|
|
void testDoReceivedAction();
|
|
|
|
|
|
|
|
class MockBtMessageDispatcher2 : public MockBtMessageDispatcher {
|
|
|
|
public:
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t index;
|
2011-12-07 15:51:20 +00:00
|
|
|
int32_t begin;
|
|
|
|
int32_t length;
|
2006-12-24 06:25:21 +00:00
|
|
|
public:
|
|
|
|
MockBtMessageDispatcher2():index(0),
|
2010-01-05 16:01:46 +00:00
|
|
|
begin(0),
|
|
|
|
length(0) {}
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2011-12-07 15:51:20 +00:00
|
|
|
virtual void doCancelSendingPieceAction
|
2013-07-06 06:54:03 +00:00
|
|
|
(size_t index, int32_t begin, int32_t length) CXX11_OVERRIDE {
|
2006-12-24 06:25:21 +00:00
|
|
|
this->index = index;
|
|
|
|
this->begin = begin;
|
|
|
|
this->length = length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtCancelMessageTest);
|
|
|
|
|
|
|
|
void BtCancelMessageTest::testCreate() {
|
|
|
|
unsigned char msg[17];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 8);
|
|
|
|
bittorrent::setIntParam(&msg[5], 12345);
|
|
|
|
bittorrent::setIntParam(&msg[9], 256);
|
|
|
|
bittorrent::setIntParam(&msg[13], 1024);
|
2013-06-30 07:55:15 +00:00
|
|
|
auto pm = BtCancelMessage::create(&msg[4], 13);
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)8, pm->getId());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
|
2011-12-07 15:51:20 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(256, pm->getBegin());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(1024, 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, 8);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtCancelMessage::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];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 9);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtCancelMessage::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(...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-04 16:24:03 +00:00
|
|
|
void BtCancelMessageTest::testCreateMessage() {
|
2006-12-24 06:25:21 +00:00
|
|
|
BtCancelMessage msg;
|
|
|
|
msg.setIndex(12345);
|
|
|
|
msg.setBegin(256);
|
|
|
|
msg.setLength(1024);
|
|
|
|
unsigned char data[17];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(data, sizeof(data), 13, 8);
|
|
|
|
bittorrent::setIntParam(&data[5], 12345);
|
|
|
|
bittorrent::setIntParam(&data[9], 256);
|
|
|
|
bittorrent::setIntParam(&data[13], 1024);
|
2010-03-04 16:24:03 +00:00
|
|
|
unsigned char* rawmsg = msg.createMessage();
|
|
|
|
CPPUNIT_ASSERT(memcmp(rawmsg, data, 17) == 0);
|
|
|
|
delete [] rawmsg;
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BtCancelMessageTest::testDoReceivedAction() {
|
|
|
|
BtCancelMessage msg;
|
|
|
|
msg.setIndex(1);
|
|
|
|
msg.setBegin(2*16*1024);
|
|
|
|
msg.setLength(16*1024);
|
|
|
|
msg.setPeer(peer);
|
2013-06-30 07:55:15 +00:00
|
|
|
auto dispatcher = make_unique<MockBtMessageDispatcher2>();
|
2010-11-12 12:48:48 +00:00
|
|
|
msg.setBtMessageDispatcher(dispatcher.get());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
msg.doReceivedAction();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(msg.getIndex(), dispatcher->index);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(msg.getBegin(), dispatcher->begin);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(msg.getLength(), dispatcher->length);
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|