2006-12-24 06:25:21 +00:00
|
|
|
#include "BtHaveNoneMessage.h"
|
2009-09-29 14:52:42 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include <cstring>
|
2009-09-29 14:52:42 +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"
|
|
|
|
#include "Peer.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 BtHaveNoneMessageTest : public CppUnit::TestFixture {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtHaveNoneMessageTest);
|
|
|
|
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
|
|
|
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();
|
|
|
|
void testToString();
|
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtHaveNoneMessageTest);
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtHaveNoneMessageTest::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, 15);
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<BtHaveNoneMessage> pm(BtHaveNoneMessage::create(&msg[4], 1));
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)15, 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, 15);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtHaveNoneMessage::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, 16);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtHaveNoneMessage::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 BtHaveNoneMessageTest::testCreateMessage()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
BtHaveNoneMessage msg;
|
|
|
|
unsigned char data[5];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(data, sizeof(data), 1, 15);
|
2010-03-04 16:24:03 +00:00
|
|
|
unsigned char* rawmsg = msg.createMessage();
|
|
|
|
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
2015-12-27 09:39:47 +00:00
|
|
|
delete[] rawmsg;
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtHaveNoneMessageTest::testDoReceivedAction()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
BtHaveNoneMessage 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
|
|
|
peer->setFastExtensionEnabled(true);
|
|
|
|
msg.setPeer(peer);
|
|
|
|
msg.doReceivedAction();
|
|
|
|
|
|
|
|
peer->setFastExtensionEnabled(false);
|
|
|
|
try {
|
|
|
|
msg.doReceivedAction();
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("an 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 BtHaveNoneMessageTest::testToString()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
BtHaveNoneMessage msg;
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("have none"), msg.toString());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|