2006-12-24 06:25:21 +00:00
|
|
|
#include "BtAllowedFastMessage.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"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2009-09-29 14:52:42 +00:00
|
|
|
#include "Peer.h"
|
2013-01-11 05:20:34 +00:00
|
|
|
#include "SocketBuffer.h"
|
2009-09-29 14:52:42 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
class BtAllowedFastMessageTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtAllowedFastMessageTest);
|
|
|
|
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(testOnSendComplete);
|
|
|
|
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 testDoReceivedAction();
|
|
|
|
void testOnSendComplete();
|
|
|
|
void testToString();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtAllowedFastMessageTest);
|
|
|
|
|
|
|
|
void BtAllowedFastMessageTest::testCreate() {
|
|
|
|
unsigned char msg[9];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 17);
|
|
|
|
bittorrent::setIntParam(&msg[5], 12345);
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<BtAllowedFastMessage> pm
|
2012-09-27 14:40:25 +00:00
|
|
|
(BtAllowedFastMessage::create(&msg[4], 5));
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)17, pm->getId());
|
|
|
|
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, 17);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtAllowedFastMessage::create(&msg[4], 6);
|
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, 18);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtAllowedFastMessage::create(&msg[4], 5);
|
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 BtAllowedFastMessageTest::testCreateMessage() {
|
2006-12-24 06:25:21 +00:00
|
|
|
BtAllowedFastMessage msg;
|
|
|
|
msg.setIndex(12345);
|
|
|
|
unsigned char data[9];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(data, sizeof(data), 5, 17);
|
|
|
|
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 BtAllowedFastMessageTest::testDoReceivedAction() {
|
|
|
|
BtAllowedFastMessage msg;
|
|
|
|
msg.setIndex(1);
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Peer> peer(new Peer("localhost", 6969));
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->allocateSessionResource(1024, 1024*1024);
|
2006-12-24 06:25:21 +00:00
|
|
|
peer->setFastExtensionEnabled(true);
|
|
|
|
msg.setPeer(peer);
|
|
|
|
CPPUNIT_ASSERT(!peer->isInPeerAllowedIndexSet(1));
|
|
|
|
msg.doReceivedAction();
|
|
|
|
CPPUNIT_ASSERT(peer->isInPeerAllowedIndexSet(1));
|
|
|
|
|
|
|
|
peer->setFastExtensionEnabled(false);
|
|
|
|
try {
|
|
|
|
msg.doReceivedAction();
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2006-12-24 06:25:21 +00:00
|
|
|
} catch(...) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtAllowedFastMessageTest::testOnSendComplete() {
|
|
|
|
BtAllowedFastMessage msg;
|
|
|
|
msg.setIndex(1);
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Peer> peer(new Peer("localhost", 6969));
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->allocateSessionResource(1024, 1024*1024);
|
2006-12-24 06:25:21 +00:00
|
|
|
peer->setFastExtensionEnabled(true);
|
|
|
|
msg.setPeer(peer);
|
|
|
|
CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1));
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<ProgressUpdate> pu(msg.getProgressUpdate());
|
2013-01-11 05:20:34 +00:00
|
|
|
pu->update(0, true);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_ASSERT(peer->isInAmAllowedIndexSet(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtAllowedFastMessageTest::testToString() {
|
|
|
|
BtAllowedFastMessage msg;
|
|
|
|
msg.setIndex(1);
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("allowed fast index=1"), msg.toString());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|