aria2/test/DefaultBtMessageFactoryTest.cc
Tatsuhiro Tsujikawa 286f34cb3f 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

78 lines
2.3 KiB
C++

#include "DefaultBtMessageFactory.h"
#include "Peer.h"
#include "PeerMessageUtil.h"
#include "BtRegistry.h"
#include "MockBtContext.h"
#include "MockExtensionMessageFactory.h"
#include "BtExtendedMessage.h"
#include <cppunit/extensions/HelperMacros.h>
class DefaultBtMessageFactoryTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DefaultBtMessageFactoryTest);
CPPUNIT_TEST(testCreateBtMessage_BtExtendedMessage);
CPPUNIT_TEST_SUITE_END();
private:
MockBtContextHandle _btContext;
PeerHandle _peer;
public:
DefaultBtMessageFactoryTest():_btContext(0), _peer(0) {}
void setUp()
{
BtRegistry::unregisterAll();
MockBtContextHandle btContext = new MockBtContext();
unsigned char infohash[20];
memset(infohash, 0, sizeof(infohash));
btContext->setInfoHash(infohash);
_btContext = btContext;
_peer = new Peer("192.168.0.1", 6969, 16*1024, 256*1024);
_peer->setExtendedMessagingEnabled(true);
MockExtensionMessageFactoryHandle exmsgFactory = new MockExtensionMessageFactory();
BtRegistry::registerPeerObjectCluster(_btContext->getInfoHashAsString(),
new PeerObjectCluster());
PeerObjectHandle peerObject = new PeerObject();
peerObject->extensionMessageFactory = exmsgFactory;
PEER_OBJECT_CLUSTER(_btContext)->registerHandle(_peer->getId(), peerObject);
}
void tearDown()
{
BtRegistry::unregisterAll();
}
void testCreateBtMessage_BtExtendedMessage();
};
CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtMessageFactoryTest);
void DefaultBtMessageFactoryTest::testCreateBtMessage_BtExtendedMessage()
{
DefaultBtMessageFactory factory;
factory.setBtContext(_btContext);
factory.setPeer(_peer);
// 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 m = factory.createBtMessage((const unsigned char*)msg+4, sizeof(msg));
try {
// disable extended messaging
_peer->setExtendedMessagingEnabled(false);
factory.createBtMessage((const unsigned char*)msg+4, sizeof(msg));
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
cerr << *e << endl;
delete e;
}
}