aria2/test/BtExtendedMessageTest.cc

122 lines
3.5 KiB
C++
Raw Normal View History

2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> Added uTorrent compatible Peer Exchange. * src/BencodeVisitor.{h, cc} * test/BencodeVisitorTest.cc * src/BtConstants.h * src/BtContext.h: Added 'private' flag. * src/BtExtendedMessage.{h, cc} * test/BtExtendedMessageTest.cc * src/BtHandshakeMessage.{h, cc}: Set extended messaging bit in reserved field. * test/BtHandshakeMessageTest.cc * src/BtMessageFactory.h * src/BtRegistry.h * src/BtRuntime.h: This class holds default extension message IDs for aria2. By default, aria2 uses ID 8 for ut_pex. * src/DefaultBtContext.cc * src/DefaultBtInteractive.{h, cc}: This class holds _utPexEnabled. When it is true, aria2 enables ut_pex. This value is set by PeerInteractionCommand. * src/DefaultBtMessageFactory.{h, cc} * test/DefaultBtMessageFactoryTest.cc * src/DefaultBtMessageReceiver.cc: Moved the code of fast extension handling to DefaultBtInteractive class. * src/DefaultExtensionMessageFactory.{h, cc} * test/DefaultExtensionMessageFactoryTest.cc * src/DefaultPeerStorage.cc: Returns false if a peer is already in the container(peers and incomingPeers. The equality is determined by Peer::id). * test/DefaultPeerStorageTest.cc * src/ExtensionMessage.h * test/MockExtensionMessage.h * src/ExtensionMessageFactory.h * test/MockExtensionMessageFactory.h * src/HandshakeExtensionMessage.{h, cc} * test/HandshakeExtensionMessageTest.cc * src/MetaEntry.h * src/Peer.{h, cc} * src/PeerInteractionCommand.cc * src/PeerReceiveHandshakeCommand.cc: Evaluate the return value of addIncomingPeer. * src/PeerMessageUtil.{h, cc} * src/PeerObject.h * src/UTPexExtensionMessage.{h, cc} * test/UTPexExtensionMessageTest.cc * src/message.h * src/prefs.h Fixed the bug that returns incomplete data when it contains null character. A convenient constructor was also added. * src/Data.{h, cc} Rewritten. * src/CompactPeerListProcessor.cc Fixed typos. * src/message.h * src/MetaFileUtil.cc
2007-12-22 03:57:55 +00:00
#include "BtExtendedMessage.h"
#include "PeerMessageUtil.h"
#include "MockBtContext.h"
#include "MockExtensionMessageFactory.h"
#include <cppunit/extensions/HelperMacros.h>
using namespace std;
class BtExtendedMessageTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(BtExtendedMessageTest);
CPPUNIT_TEST(testCreate);
CPPUNIT_TEST(testGetMessage);
CPPUNIT_TEST(testDoReceivedAction);
CPPUNIT_TEST(testToString);
CPPUNIT_TEST_SUITE_END();
private:
public:
void setUp()
{
BtRegistry::unregisterAll();
}
void tearDown()
{
BtRegistry::unregisterAll();
}
void testCreate();
void testGetMessage();
void testDoReceivedAction();
void testToString();
};
CPPUNIT_TEST_SUITE_REGISTRATION(BtExtendedMessageTest);
void BtExtendedMessageTest::testCreate() {
PeerHandle peer = new Peer("192.168.0.1", 6969);
2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> Added uTorrent compatible Peer Exchange. * src/BencodeVisitor.{h, cc} * test/BencodeVisitorTest.cc * src/BtConstants.h * src/BtContext.h: Added 'private' flag. * src/BtExtendedMessage.{h, cc} * test/BtExtendedMessageTest.cc * src/BtHandshakeMessage.{h, cc}: Set extended messaging bit in reserved field. * test/BtHandshakeMessageTest.cc * src/BtMessageFactory.h * src/BtRegistry.h * src/BtRuntime.h: This class holds default extension message IDs for aria2. By default, aria2 uses ID 8 for ut_pex. * src/DefaultBtContext.cc * src/DefaultBtInteractive.{h, cc}: This class holds _utPexEnabled. When it is true, aria2 enables ut_pex. This value is set by PeerInteractionCommand. * src/DefaultBtMessageFactory.{h, cc} * test/DefaultBtMessageFactoryTest.cc * src/DefaultBtMessageReceiver.cc: Moved the code of fast extension handling to DefaultBtInteractive class. * src/DefaultExtensionMessageFactory.{h, cc} * test/DefaultExtensionMessageFactoryTest.cc * src/DefaultPeerStorage.cc: Returns false if a peer is already in the container(peers and incomingPeers. The equality is determined by Peer::id). * test/DefaultPeerStorageTest.cc * src/ExtensionMessage.h * test/MockExtensionMessage.h * src/ExtensionMessageFactory.h * test/MockExtensionMessageFactory.h * src/HandshakeExtensionMessage.{h, cc} * test/HandshakeExtensionMessageTest.cc * src/MetaEntry.h * src/Peer.{h, cc} * src/PeerInteractionCommand.cc * src/PeerReceiveHandshakeCommand.cc: Evaluate the return value of addIncomingPeer. * src/PeerMessageUtil.{h, cc} * src/PeerObject.h * src/UTPexExtensionMessage.{h, cc} * test/UTPexExtensionMessageTest.cc * src/message.h * src/prefs.h Fixed the bug that returns incomplete data when it contains null character. A convenient constructor was also added. * src/Data.{h, cc} Rewritten. * src/CompactPeerListProcessor.cc Fixed typos. * src/message.h * src/MetaFileUtil.cc
2007-12-22 03:57:55 +00:00
peer->setExtension("charlie", 1);
MockBtContextHandle ctx = new MockBtContext();
unsigned char infohash[20];
memset(infohash, 0, sizeof(infohash));
ctx->setInfoHash(infohash);
MockExtensionMessageFactoryHandle exmsgFactory = new MockExtensionMessageFactory();
BtRegistry::registerPeerObjectCluster(ctx->getInfoHashAsString(), new PeerObjectCluster());
PeerObjectHandle peerObject = new PeerObject();
peerObject->extensionMessageFactory = exmsgFactory;
PEER_OBJECT_CLUSTER(ctx)->registerHandle(peer->getId(), peerObject);
// payload:{4:name3:foo}->11bytes
string payload = "4:name3:foo";
char msg[17];// 6+11bytes
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 13, 20);
msg[5] = 1; // Set dummy extended message ID 1
memcpy(msg+6, payload.c_str(), payload.size());
BtExtendedMessageHandle pm = BtExtendedMessage::create(ctx,
peer,
&msg[4], 13);
CPPUNIT_ASSERT_EQUAL((int8_t)20, pm->getId());
// case: payload size is wrong
try {
char msg[5];
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 1, 20);
BtExtendedMessage::create(ctx, peer, &msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
cerr << *e << endl;
delete e;
}
// case: id is wrong
try {
char msg[6];
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 2, 21);
BtExtendedMessage::create(ctx, peer, &msg[4], 2);
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
cerr << *e << endl;
delete e;
}
}
void BtExtendedMessageTest::testGetMessage() {
string payload = "4:name3:foo";
uint8_t extendedMessageID = 1;
MockExtensionMessageHandle exmsg =
new MockExtensionMessage("charlie", extendedMessageID,
payload.c_str(),
payload.size());
BtExtendedMessage msg(exmsg);
char data[17];
PeerMessageUtil::createPeerMessageString((unsigned char*)data, sizeof(data), 13, 20);
*(data+5) = extendedMessageID;
memcpy(data+6, payload.c_str(), payload.size());
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);
}
void BtExtendedMessageTest::testDoReceivedAction() {
MockExtensionMessageHandle exmsg =
new MockExtensionMessage("charlie", 1, "", 0);
BtExtendedMessage msg(exmsg);
msg.doReceivedAction();
CPPUNIT_ASSERT(exmsg->_doReceivedActionCalled);
}
void BtExtendedMessageTest::testToString() {
string payload = "4:name3:foo";
uint8_t extendedMessageID = 1;
MockExtensionMessageHandle exmsg =
new MockExtensionMessage("charlie", extendedMessageID,
payload.c_str(),
payload.size());
BtExtendedMessage msg(exmsg);
CPPUNIT_ASSERT_EQUAL(string("extended charlie"), msg.toString());
}