2006-12-24 06:25:21 +00:00
|
|
|
#include "BtSuggestPieceMessage.h"
|
2009-09-29 14:52:42 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include <cstring>
|
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-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
class BtSuggestPieceMessageTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtSuggestPieceMessageTest);
|
|
|
|
CPPUNIT_TEST(testCreate);
|
2010-03-04 16:24:03 +00:00
|
|
|
CPPUNIT_TEST(testCreateMessage);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_TEST(testToString);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void testCreate();
|
2010-03-04 16:24:03 +00:00
|
|
|
void testCreateMessage();
|
2006-12-24 06:25:21 +00:00
|
|
|
void testToString();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtSuggestPieceMessageTest);
|
|
|
|
|
|
|
|
void BtSuggestPieceMessageTest::testCreate() {
|
|
|
|
unsigned char msg[9];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 13);
|
|
|
|
bittorrent::setIntParam(&msg[5], 12345);
|
2013-06-30 07:55:15 +00:00
|
|
|
auto pm = BtSuggestPieceMessage::create(&msg[4], 5);
|
|
|
|
CPPUNIT_ASSERT(BtSuggestPieceMessage::ID == pm->getId());
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
// case: payload size is wrong
|
|
|
|
try {
|
|
|
|
unsigned char msg[10];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 6, 13);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtSuggestPieceMessage::create(&msg[4], 2);
|
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[9];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 14);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtSuggestPieceMessage::create(&msg[4], 1);
|
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 BtSuggestPieceMessageTest::testCreateMessage() {
|
2006-12-24 06:25:21 +00:00
|
|
|
BtSuggestPieceMessage msg;
|
|
|
|
msg.setIndex(12345);
|
|
|
|
unsigned char data[9];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(data, sizeof(data), 5, 13);
|
|
|
|
bittorrent::setIntParam(&data[5], 12345);
|
2010-03-04 16:24:03 +00:00
|
|
|
unsigned char* rawmsg = msg.createMessage();
|
|
|
|
CPPUNIT_ASSERT(memcmp(rawmsg, data, 9) == 0);
|
|
|
|
delete [] rawmsg;
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BtSuggestPieceMessageTest::testToString() {
|
|
|
|
BtSuggestPieceMessage msg;
|
|
|
|
msg.setIndex(12345);
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("suggest piece index=12345"),
|
2010-01-05 16:01:46 +00:00
|
|
|
msg.toString());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|