2006-12-24 06:25:21 +00:00
|
|
|
#include "BtInterestedMessage.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include <cstring>
|
2008-11-03 06:49:02 +00:00
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2009-09-29 14:52:42 +00:00
|
|
|
#include "bittorrent_helper.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
#include "Peer.h"
|
2008-11-03 12:04:57 +00:00
|
|
|
#include "MockPeerStorage.h"
|
2013-01-11 05:20:34 +00:00
|
|
|
#include "SocketBuffer.h"
|
2008-11-03 06:49:02 +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 BtInterestedMessageTest : public CppUnit::TestFixture {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtInterestedMessageTest);
|
|
|
|
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(testToString);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
2015-12-27 09:39:47 +00:00
|
|
|
|
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 testToString();
|
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtInterestedMessageTest);
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtInterestedMessageTest::testCreate()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
unsigned char msg[5];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 2);
|
2015-12-27 09:39:47 +00:00
|
|
|
std::shared_ptr<BtInterestedMessage> pm(
|
|
|
|
BtInterestedMessage::create(&msg[4], 1));
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)2, pm->getId());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
// case: payload size is wrong
|
|
|
|
try {
|
|
|
|
unsigned char msg[6];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 2);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtInterestedMessage::create(&msg[4], 2);
|
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[5];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 3);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtInterestedMessage::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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtInterestedMessageTest::testCreateMessage()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
BtInterestedMessage msg;
|
|
|
|
unsigned char data[5];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(data, sizeof(data), 1, 2);
|
2016-05-15 14:40:09 +00: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 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtInterestedMessageTest::testDoReceivedAction()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
BtInterestedMessage msg;
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Peer> peer(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
|
|
|
msg.setPeer(peer);
|
2008-11-03 06:49:02 +00:00
|
|
|
|
2013-06-22 15:01:40 +00:00
|
|
|
auto peerStorage = make_unique<MockPeerStorage>();
|
2008-11-03 12:04:57 +00:00
|
|
|
|
2013-06-22 15:01:40 +00:00
|
|
|
msg.setPeerStorage(peerStorage.get());
|
2008-11-03 12:04:57 +00:00
|
|
|
|
2008-02-09 17:14:40 +00:00
|
|
|
CPPUNIT_ASSERT(!peer->peerInterested());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg.doReceivedAction();
|
2008-02-09 17:14:40 +00:00
|
|
|
CPPUNIT_ASSERT(peer->peerInterested());
|
2016-06-05 02:30:43 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted());
|
2008-11-03 12:04:57 +00:00
|
|
|
|
|
|
|
peer->amChoking(false);
|
|
|
|
msg.doReceivedAction();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtInterestedMessageTest::testToString()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
BtInterestedMessage msg;
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("interested"), msg.toString());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|