2006-12-24 14:25:21 +08:00
|
|
|
#include "BtHaveAllMessage.h"
|
2009-07-06 23:22:06 +08:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2009-09-29 22:52:42 +08:00
|
|
|
#include "bittorrent_helper.h"
|
2008-02-08 23:53:45 +08:00
|
|
|
#include "Peer.h"
|
2008-05-11 09:22:32 +08:00
|
|
|
#include "MockPieceStorage.h"
|
2009-07-06 23:22:06 +08:00
|
|
|
#include "DlAbortEx.h"
|
2010-06-11 20:49:14 +08:00
|
|
|
#include "FileEntry.h"
|
2006-12-24 14:25:21 +08:00
|
|
|
|
2008-02-08 23:53:45 +08:00
|
|
|
namespace aria2 {
|
2006-12-24 14:25:21 +08:00
|
|
|
|
2015-12-27 17:39:47 +08:00
|
|
|
class BtHaveAllMessageTest : public CppUnit::TestFixture {
|
2006-12-24 14:25:21 +08:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtHaveAllMessageTest);
|
|
|
|
CPPUNIT_TEST(testCreate);
|
2010-03-05 00:24:03 +08:00
|
|
|
CPPUNIT_TEST(testCreateMessage);
|
2006-12-24 14:25:21 +08:00
|
|
|
CPPUNIT_TEST(testDoReceivedAction);
|
2009-07-06 23:22:06 +08:00
|
|
|
CPPUNIT_TEST(testDoReceivedAction_goodByeSeeder);
|
2006-12-24 14:25:21 +08:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2015-12-27 17:39:47 +08:00
|
|
|
private:
|
2006-12-24 14:25:21 +08:00
|
|
|
public:
|
2015-12-27 17:39:47 +08:00
|
|
|
void setUp() {}
|
2006-12-24 14:25:21 +08:00
|
|
|
|
|
|
|
void testCreate();
|
2010-03-05 00:24:03 +08:00
|
|
|
void testCreateMessage();
|
2006-12-24 14:25:21 +08:00
|
|
|
void testDoReceivedAction();
|
2009-07-06 23:22:06 +08:00
|
|
|
void testDoReceivedAction_goodByeSeeder();
|
2006-12-24 14:25:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtHaveAllMessageTest);
|
|
|
|
|
2015-12-27 17:39:47 +08:00
|
|
|
void BtHaveAllMessageTest::testCreate()
|
|
|
|
{
|
2006-12-24 14:25:21 +08:00
|
|
|
unsigned char msg[5];
|
2009-09-29 22:52:42 +08:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 14);
|
2013-06-22 00:10:38 +08:00
|
|
|
std::shared_ptr<BtHaveAllMessage> pm(BtHaveAllMessage::create(&msg[4], 1));
|
2008-03-09 20:24:01 +08:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)14, pm->getId());
|
2006-12-24 14:25:21 +08:00
|
|
|
|
|
|
|
// case: payload size is wrong
|
|
|
|
try {
|
|
|
|
unsigned char msg[6];
|
2009-09-29 22:52:42 +08:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 14);
|
2006-12-24 14:25:21 +08:00
|
|
|
BtHaveAllMessage::create(&msg[4], 2);
|
2007-08-09 22:54:36 +08:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2015-12-27 17:39:47 +08:00
|
|
|
}
|
|
|
|
catch (...) {
|
2006-12-24 14:25:21 +08:00
|
|
|
}
|
|
|
|
// case: id is wrong
|
|
|
|
try {
|
|
|
|
unsigned char msg[5];
|
2009-09-29 22:52:42 +08:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 15);
|
2006-12-24 14:25:21 +08:00
|
|
|
BtHaveAllMessage::create(&msg[4], 1);
|
2007-08-09 22:54:36 +08:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2015-12-27 17:39:47 +08:00
|
|
|
}
|
|
|
|
catch (...) {
|
2006-12-24 14:25:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 17:39:47 +08:00
|
|
|
void BtHaveAllMessageTest::testCreateMessage()
|
|
|
|
{
|
2006-12-24 14:25:21 +08:00
|
|
|
BtHaveAllMessage msg;
|
|
|
|
unsigned char data[5];
|
2009-09-29 22:52:42 +08:00
|
|
|
bittorrent::createPeerMessageString(data, sizeof(data), 1, 14);
|
2016-05-15 22:40:09 +08:00
|
|
|
auto rawmsg = msg.createMessage();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
|
|
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
2006-12-24 14:25:21 +08:00
|
|
|
}
|
|
|
|
|
2015-12-27 17:39:47 +08:00
|
|
|
void BtHaveAllMessageTest::testDoReceivedAction()
|
|
|
|
{
|
2006-12-24 14:25:21 +08:00
|
|
|
BtHaveAllMessage msg;
|
2013-06-22 00:10:38 +08:00
|
|
|
std::shared_ptr<Peer> peer(new Peer("host", 6969));
|
2015-06-21 17:04:30 +08:00
|
|
|
peer->allocateSessionResource(16_k, 256_k);
|
2006-12-24 14:25:21 +08:00
|
|
|
peer->setFastExtensionEnabled(true);
|
|
|
|
msg.setPeer(peer);
|
2013-06-22 23:01:40 +08:00
|
|
|
auto pieceStorage = make_unique<MockPieceStorage>();
|
|
|
|
msg.setPieceStorage(pieceStorage.get());
|
2008-05-11 09:22:32 +08:00
|
|
|
|
2006-12-24 14:25:21 +08:00
|
|
|
msg.doReceivedAction();
|
2012-10-01 22:52:22 +08:00
|
|
|
|
2006-12-24 14:25:21 +08:00
|
|
|
CPPUNIT_ASSERT(peer->isSeeder());
|
2012-10-01 22:52:22 +08:00
|
|
|
|
2006-12-24 14:25:21 +08:00
|
|
|
peer->setFastExtensionEnabled(false);
|
2012-10-01 22:52:22 +08:00
|
|
|
|
2006-12-24 14:25:21 +08:00
|
|
|
try {
|
|
|
|
msg.doReceivedAction();
|
2007-08-09 22:54:36 +08:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2015-12-27 17:39:47 +08:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
}
|
2006-12-24 14:25:21 +08:00
|
|
|
}
|
2008-02-08 23:53:45 +08:00
|
|
|
|
2009-07-06 23:22:06 +08:00
|
|
|
void BtHaveAllMessageTest::testDoReceivedAction_goodByeSeeder()
|
|
|
|
{
|
|
|
|
BtHaveAllMessage msg;
|
2013-06-22 00:10:38 +08:00
|
|
|
std::shared_ptr<Peer> peer(new Peer("ip", 6000));
|
2015-06-21 17:04:30 +08:00
|
|
|
peer->allocateSessionResource(1_k, 1_k);
|
2009-07-06 23:22:06 +08:00
|
|
|
peer->setFastExtensionEnabled(true);
|
|
|
|
msg.setPeer(peer);
|
2013-06-22 23:01:40 +08:00
|
|
|
auto pieceStorage = make_unique<MockPieceStorage>();
|
|
|
|
msg.setPieceStorage(pieceStorage.get());
|
2009-07-06 23:22:06 +08:00
|
|
|
|
|
|
|
pieceStorage->setDownloadFinished(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
msg.doReceivedAction();
|
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2015-12-27 17:39:47 +08:00
|
|
|
}
|
|
|
|
catch (DlAbortEx& e) {
|
2009-07-06 23:22:06 +08:00
|
|
|
// success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 23:53:45 +08:00
|
|
|
} // namespace aria2
|