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
This commit is contained in:
Tatsuhiro Tsujikawa 2007-12-22 03:57:55 +00:00
parent c25f4cffc0
commit 286f34cb3f
61 changed files with 2487 additions and 100 deletions

View file

@ -0,0 +1,187 @@
#include "UTPexExtensionMessage.h"
#include "Peer.h"
#include "a2netcompat.h"
#include "Util.h"
#include "PeerMessageUtil.h"
#include "BtRegistry.h"
#include "MockBtContext.h"
#include "MockPeerStorage.h"
#include <cppunit/extensions/HelperMacros.h>
class UTPexExtensionMessageTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(UTPexExtensionMessageTest);
CPPUNIT_TEST(testGetExtensionMessageID);
CPPUNIT_TEST(testGetExtensionName);
CPPUNIT_TEST(testGetBencodedData);
CPPUNIT_TEST(testToString);
CPPUNIT_TEST(testDoReceivedAction);
CPPUNIT_TEST(testCreate);
CPPUNIT_TEST_SUITE_END();
private:
MockBtContextHandle _btContext;
public:
UTPexExtensionMessageTest():_btContext(0) {}
void setUp()
{
BtRegistry::unregisterAll();
MockBtContextHandle btContext = new MockBtContext();
unsigned char infohash[20];
memset(infohash, 0, sizeof(infohash));
btContext->setInfoHash(infohash);
_btContext = btContext;
MockPeerStorageHandle peerStorage = new MockPeerStorage();
BtRegistry::registerPeerStorage(_btContext->getInfoHashAsString(),
peerStorage);
}
void tearDown()
{
BtRegistry::unregisterAll();
}
void testGetExtensionMessageID();
void testGetExtensionName();
void testGetBencodedData();
void testToString();
void testDoReceivedAction();
void testCreate();
};
CPPUNIT_TEST_SUITE_REGISTRATION(UTPexExtensionMessageTest);
void UTPexExtensionMessageTest::testGetExtensionMessageID()
{
UTPexExtensionMessage msg(1);
CPPUNIT_ASSERT_EQUAL((uint8_t)1, msg.getExtensionMessageID());
}
void UTPexExtensionMessageTest::testGetExtensionName()
{
UTPexExtensionMessage msg(1);
CPPUNIT_ASSERT_EQUAL(string("ut_pex"), msg.getExtensionName());
}
void UTPexExtensionMessageTest::testGetBencodedData()
{
UTPexExtensionMessage msg(1);
PeerHandle p1 = new Peer("192.168.0.1", 6881, 1, 1);
p1->setAllBitfield();
msg.addFreshPeer(p1);// added seeder, check add.f flag
PeerHandle p2 = new Peer("10.1.1.2", 9999, 1, 1);
msg.addFreshPeer(p2);
PeerHandle p3 = new Peer("192.168.0.2", 6882, 1, 1);
msg.addDroppedPeer(p3);
PeerHandle p4 = new Peer("10.1.1.3", 10000, 1, 1);
msg.addDroppedPeer(p4);
char c1[6];
char c2[6];
char c3[6];
char c4[6];
PeerMessageUtil::createcompact(c1, p1->ipaddr, p1->port);
PeerMessageUtil::createcompact(c2, p2->ipaddr, p2->port);
PeerMessageUtil::createcompact(c3, p3->ipaddr, p3->port);
PeerMessageUtil::createcompact(c4, p4->ipaddr, p4->port);
string expected = "d5:added12:"+
string(&c1[0], &c1[6])+string(&c2[0], &c2[6])+
"7:added.f2:207:dropped12:"+
string(&c3[0], &c3[6])+string(&c4[0], &c4[6])+
"e";
string bd = msg.getBencodedData();
CPPUNIT_ASSERT_EQUAL(Util::urlencode(expected),
Util::urlencode(bd));
}
void UTPexExtensionMessageTest::testToString()
{
UTPexExtensionMessage msg(1);
PeerHandle p1 = new Peer("192.168.0.1", 6881, 1, 1);
p1->setAllBitfield();
msg.addFreshPeer(p1);// added seeder, check add.f flag
PeerHandle p2 = new Peer("10.1.1.2", 9999, 1, 1);
msg.addFreshPeer(p2);
PeerHandle p3 = new Peer("192.168.0.2", 6882, 1, 1);
msg.addDroppedPeer(p3);
PeerHandle p4 = new Peer("10.1.1.3", 10000, 1, 1);
msg.addDroppedPeer(p4);
CPPUNIT_ASSERT_EQUAL(string("ut_pex added=2, dropped=2"), msg.toString());
}
void UTPexExtensionMessageTest::testDoReceivedAction()
{
UTPexExtensionMessage msg(1);
PeerHandle p1 = new Peer("192.168.0.1", 6881, 1, 1);
p1->setAllBitfield();
msg.addFreshPeer(p1);// added seeder, check add.f flag
PeerHandle p2 = new Peer("10.1.1.2", 9999, 1, 1);
msg.addFreshPeer(p2);
PeerHandle p3 = new Peer("192.168.0.2", 6882, 1, 1);
msg.addDroppedPeer(p3);
PeerHandle p4 = new Peer("10.1.1.3", 10000, 1, 1);
msg.addDroppedPeer(p4);
msg.setBtContext(_btContext);
msg.doReceivedAction();
CPPUNIT_ASSERT_EQUAL((size_t)2, PEER_STORAGE(_btContext)->getPeers().size());
{
PeerHandle p = PEER_STORAGE(_btContext)->getPeers()[0];
CPPUNIT_ASSERT_EQUAL(string("192.168.0.1"), p->ipaddr);
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, p->port);
}
{
PeerHandle p = PEER_STORAGE(_btContext)->getPeers()[1];
CPPUNIT_ASSERT_EQUAL(string("10.1.1.2"), p->ipaddr);
CPPUNIT_ASSERT_EQUAL((uint16_t)9999, p->port);
}
}
void UTPexExtensionMessageTest::testCreate()
{
_btContext->setPieceLength(256*1024);
_btContext->setTotalLength(1024*1024);
char c1[6];
char c2[6];
char c3[6];
char c4[6];
PeerMessageUtil::createcompact(c1, "192.168.0.1", 6881);
PeerMessageUtil::createcompact(c2, "10.1.1.2", 9999);
PeerMessageUtil::createcompact(c3, "192.168.0.2", 6882);
PeerMessageUtil::createcompact(c4, "10.1.1.3",10000);
char id[1] = { 1 };
string data = string(&id[0], &id[1])+"d5:added12:"+
string(&c1[0], &c1[6])+string(&c2[0], &c2[6])+
"7:added.f2:207:dropped12:"+
string(&c3[0], &c3[6])+string(&c4[0], &c4[6])+
"e";
UTPexExtensionMessageHandle msg =
UTPexExtensionMessage::create(_btContext, data.c_str(), data.size());
CPPUNIT_ASSERT_EQUAL((uint8_t)1, msg->getExtensionMessageID());
CPPUNIT_ASSERT_EQUAL((size_t)2, msg->getFreshPeers().size());
CPPUNIT_ASSERT_EQUAL(string("192.168.0.1"), msg->getFreshPeers()[0]->ipaddr);
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, msg->getFreshPeers()[0]->port);
CPPUNIT_ASSERT_EQUAL(string("10.1.1.2"), msg->getFreshPeers()[1]->ipaddr);
CPPUNIT_ASSERT_EQUAL((uint16_t)9999, msg->getFreshPeers()[1]->port);
CPPUNIT_ASSERT_EQUAL((size_t)2, msg->getDroppedPeers().size());
CPPUNIT_ASSERT_EQUAL(string("192.168.0.2"), msg->getDroppedPeers()[0]->ipaddr);
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, msg->getDroppedPeers()[0]->port);
CPPUNIT_ASSERT_EQUAL(string("10.1.1.3"), msg->getDroppedPeers()[1]->ipaddr);
CPPUNIT_ASSERT_EQUAL((uint16_t)10000, msg->getDroppedPeers()[1]->port);
try {
// 0 length data
string in = "";
UTPexExtensionMessage::create(_btContext, in.c_str(), in.size());
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
cerr << *e << endl;
delete e;
}
}