2006-12-24 06:25:21 +00:00
|
|
|
#include "BtAllowedFastMessage.h"
|
|
|
|
#include "PeerMessageUtil.h"
|
|
|
|
#include "Util.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Peer.h"
|
|
|
|
#include <cstring>
|
2006-12-24 06:25:21 +00:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
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);
|
|
|
|
CPPUNIT_TEST(testGetMessage);
|
|
|
|
CPPUNIT_TEST(testDoReceivedAction);
|
|
|
|
CPPUNIT_TEST(testOnSendComplete);
|
|
|
|
CPPUNIT_TEST(testToString);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void testCreate();
|
|
|
|
void testGetMessage();
|
|
|
|
void testDoReceivedAction();
|
|
|
|
void testOnSendComplete();
|
|
|
|
void testToString();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtAllowedFastMessageTest);
|
|
|
|
|
|
|
|
void BtAllowedFastMessageTest::testCreate() {
|
|
|
|
unsigned char msg[9];
|
|
|
|
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 5, 17);
|
|
|
|
PeerMessageUtil::setIntParam(&msg[5], 12345);
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<BtAllowedFastMessage> pm = BtAllowedFastMessage::create(&msg[4], 5);
|
2007-01-28 14:18:35 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((int8_t)17, pm->getId());
|
2007-07-23 14:09:46 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)12345, pm->getIndex());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
// case: payload size is wrong
|
|
|
|
try {
|
|
|
|
unsigned char msg[10];
|
|
|
|
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 6, 17);
|
|
|
|
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];
|
|
|
|
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 5, 18);
|
|
|
|
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(...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtAllowedFastMessageTest::testGetMessage() {
|
|
|
|
BtAllowedFastMessage msg;
|
|
|
|
msg.setIndex(12345);
|
|
|
|
unsigned char data[9];
|
|
|
|
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 5, 17);
|
|
|
|
PeerMessageUtil::setIntParam(&data[5], 12345);
|
|
|
|
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 9) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtAllowedFastMessageTest::testDoReceivedAction() {
|
|
|
|
BtAllowedFastMessage msg;
|
|
|
|
msg.setIndex(1);
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<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);
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<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));
|
|
|
|
msg.onSendComplete();
|
|
|
|
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
|