2006-12-24 06:25:21 +00:00
|
|
|
#include "BtBitfieldMessage.h"
|
2009-07-06 15:22:06 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2009-09-29 14:52:42 +00:00
|
|
|
#include "bittorrent_helper.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Peer.h"
|
2008-05-11 01:22:32 +00:00
|
|
|
#include "MockPieceStorage.h"
|
2009-07-06 15:22:06 +00:00
|
|
|
#include "DlAbortEx.h"
|
2010-06-11 12:49:14 +00:00
|
|
|
#include "FileEntry.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
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
class BtBitfieldMessageTest : public CppUnit::TestFixture {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtBitfieldMessageTest);
|
|
|
|
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);
|
2009-07-06 15:22:06 +00:00
|
|
|
CPPUNIT_TEST(testDoReceivedAction_goodByeSeeder);
|
2006-12-24 06:25:21 +00:00
|
|
|
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:
|
2015-12-27 09:39:47 +00:00
|
|
|
void setUp() {}
|
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();
|
2009-07-06 15:22:06 +00:00
|
|
|
void testDoReceivedAction_goodByeSeeder();
|
2006-12-24 06:25:21 +00:00
|
|
|
void testToString();
|
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtBitfieldMessageTest);
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtBitfieldMessageTest::testCreate()
|
|
|
|
{
|
|
|
|
unsigned char msg[5 + 2];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 3, 5);
|
2006-12-24 06:25:21 +00:00
|
|
|
unsigned char bitfield[2];
|
|
|
|
memset(bitfield, 0xff, sizeof(bitfield));
|
|
|
|
memcpy(&msg[5], bitfield, sizeof(bitfield));
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<BtBitfieldMessage> pm(BtBitfieldMessage::create(&msg[4], 3));
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)5, pm->getId());
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_ASSERT(memcmp(bitfield, pm->getBitfield(), sizeof(bitfield)) == 0);
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, pm->getBitfieldLength());
|
2006-12-24 06:25:21 +00:00
|
|
|
// case: payload size is wrong
|
|
|
|
try {
|
|
|
|
unsigned char msg[5];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 5);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtBitfieldMessage::create(&msg[4], 1);
|
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 {
|
2015-12-27 09:39:47 +00:00
|
|
|
unsigned char msg[5 + 2];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 3, 6);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtBitfieldMessage::create(&msg[4], 3);
|
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 BtBitfieldMessageTest::testCreateMessage()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
BtBitfieldMessage msg;
|
|
|
|
unsigned char bitfield[2];
|
|
|
|
memset(bitfield, 0xff, sizeof(bitfield));
|
|
|
|
msg.setBitfield(bitfield, sizeof(bitfield));
|
2015-12-27 09:39:47 +00:00
|
|
|
unsigned char data[5 + 2];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(data, sizeof(data), 3, 5);
|
2006-12-24 06:25:21 +00:00
|
|
|
memcpy(&data[5], bitfield, sizeof(bitfield));
|
2010-03-04 16:24:03 +00:00
|
|
|
unsigned char* rawmsg = msg.createMessage();
|
|
|
|
CPPUNIT_ASSERT(memcmp(rawmsg, data, 7) == 0);
|
2015-12-27 09:39:47 +00:00
|
|
|
delete[] rawmsg;
|
2010-03-04 16:24:03 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)7, msg.getMessageLength());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtBitfieldMessageTest::testDoReceivedAction()
|
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Peer> peer(new Peer("host1", 6969));
|
2015-06-21 09:04:30 +00:00
|
|
|
peer->allocateSessionResource(16_k, 256_k);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtBitfieldMessage msg;
|
|
|
|
msg.setPeer(peer);
|
2013-06-22 15:01:40 +00:00
|
|
|
auto pieceStorage = make_unique<MockPieceStorage>();
|
|
|
|
msg.setPieceStorage(pieceStorage.get());
|
2015-12-27 09:39:47 +00:00
|
|
|
unsigned char bitfield[] = {0xff, 0xff};
|
2006-12-24 06:25:21 +00:00
|
|
|
msg.setBitfield(bitfield, sizeof(bitfield));
|
2012-09-27 14:40:25 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(
|
|
|
|
std::string("0000"),
|
|
|
|
util::toHex(peer->getBitfield(), peer->getBitfieldLength()));
|
2006-12-24 06:25:21 +00:00
|
|
|
msg.doReceivedAction();
|
2015-12-27 09:39:47 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(
|
|
|
|
std::string("ffff"),
|
|
|
|
util::toHex(peer->getBitfield(), peer->getBitfieldLength()));
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2009-07-06 15:22:06 +00:00
|
|
|
void BtBitfieldMessageTest::testDoReceivedAction_goodByeSeeder()
|
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Peer> peer(new Peer("ip", 6000));
|
2015-06-21 09:04:30 +00:00
|
|
|
peer->allocateSessionResource(1_k, 1_k);
|
2009-07-06 15:22:06 +00:00
|
|
|
BtBitfieldMessage msg;
|
|
|
|
msg.setPeer(peer);
|
2013-06-22 15:01:40 +00:00
|
|
|
auto pieceStorage = make_unique<MockPieceStorage>();
|
|
|
|
msg.setPieceStorage(pieceStorage.get());
|
2015-12-27 09:39:47 +00:00
|
|
|
unsigned char bitfield[] = {0x00};
|
2009-07-06 15:22:06 +00:00
|
|
|
msg.setBitfield(bitfield, sizeof(bitfield));
|
|
|
|
|
|
|
|
// peer is not seeder and client have not completed download
|
|
|
|
msg.doReceivedAction();
|
|
|
|
|
|
|
|
pieceStorage->setDownloadFinished(true);
|
|
|
|
|
|
|
|
// client completed download, but peer is not seeder
|
|
|
|
msg.doReceivedAction();
|
|
|
|
|
|
|
|
pieceStorage->setDownloadFinished(false);
|
|
|
|
bitfield[0] = 0x80;
|
|
|
|
msg.setBitfield(bitfield, sizeof(bitfield));
|
|
|
|
|
|
|
|
// peer is seeder, but client have not completed download
|
|
|
|
msg.doReceivedAction();
|
|
|
|
|
|
|
|
pieceStorage->setDownloadFinished(true);
|
|
|
|
try {
|
|
|
|
msg.doReceivedAction();
|
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
catch (DlAbortEx& e) {
|
2009-07-06 15:22:06 +00:00
|
|
|
// success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtBitfieldMessageTest::testToString()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
BtBitfieldMessage msg;
|
2015-12-27 09:39:47 +00:00
|
|
|
unsigned char bitfield[] = {0xff, 0xff};
|
2006-12-24 06:25:21 +00:00
|
|
|
msg.setBitfield(bitfield, sizeof(bitfield));
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("bitfield ffff"), msg.toString());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|