2008-12-14 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Use BDE instead of Dictionary/List/Data.
	* src/DHTAbstractMessage.cc
	* src/DHTAbstractMessage.h
	* src/DHTAnnouncePeerMessage.cc
	* src/DHTAnnouncePeerMessage.h
	* src/DHTAnnouncePeerReplyMessage.cc
	* src/DHTAnnouncePeerReplyMessage.h
	* src/DHTFindNodeMessage.cc
	* src/DHTFindNodeMessage.h
	* src/DHTFindNodeReplyMessage.cc
	* src/DHTFindNodeReplyMessage.h
	* src/DHTGetPeersMessage.cc
	* src/DHTGetPeersMessage.h
	* src/DHTGetPeersReplyMessage.cc
	* src/DHTGetPeersReplyMessage.h
	* src/DHTMessageFactory.h
	* src/DHTMessageFactoryImpl.cc
	* src/DHTMessageFactoryImpl.h
	* src/DHTMessageReceiver.cc
	* src/DHTMessageTracker.cc
	* src/DHTMessageTracker.h
	* src/DHTPingMessage.cc
	* src/DHTPingMessage.h
	* src/DHTPingReplyMessage.cc
	* src/DHTPingReplyMessage.h
	* src/DHTQueryMessage.cc
	* src/DHTQueryMessage.h
	* src/DHTResponseMessage.cc
	* src/DHTResponseMessage.h
	* test/DHTAnnouncePeerMessageTest.cc
	* test/DHTAnnouncePeerReplyMessageTest.cc
	* test/DHTFindNodeMessageTest.cc
	* test/DHTFindNodeReplyMessageTest.cc
	* test/DHTGetPeersMessageTest.cc
	* test/DHTGetPeersReplyMessageTest.cc
	* test/DHTMessageFactoryImplTest.cc
	* test/DHTMessageTrackerTest.cc
	* test/DHTPingMessageTest.cc
	* test/DHTPingReplyMessageTest.cc
	* test/MockDHTMessageFactory.h
This commit is contained in:
Tatsuhiro Tsujikawa 2008-12-14 05:46:45 +00:00
parent 057132cd5f
commit 9202fe23c8
40 changed files with 575 additions and 510 deletions

View file

@ -1,3 +1,46 @@
2008-12-14 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Use BDE instead of Dictionary/List/Data.
* src/DHTAbstractMessage.cc
* src/DHTAbstractMessage.h
* src/DHTAnnouncePeerMessage.cc
* src/DHTAnnouncePeerMessage.h
* src/DHTAnnouncePeerReplyMessage.cc
* src/DHTAnnouncePeerReplyMessage.h
* src/DHTFindNodeMessage.cc
* src/DHTFindNodeMessage.h
* src/DHTFindNodeReplyMessage.cc
* src/DHTFindNodeReplyMessage.h
* src/DHTGetPeersMessage.cc
* src/DHTGetPeersMessage.h
* src/DHTGetPeersReplyMessage.cc
* src/DHTGetPeersReplyMessage.h
* src/DHTMessageFactory.h
* src/DHTMessageFactoryImpl.cc
* src/DHTMessageFactoryImpl.h
* src/DHTMessageReceiver.cc
* src/DHTMessageTracker.cc
* src/DHTMessageTracker.h
* src/DHTPingMessage.cc
* src/DHTPingMessage.h
* src/DHTPingReplyMessage.cc
* src/DHTPingReplyMessage.h
* src/DHTQueryMessage.cc
* src/DHTQueryMessage.h
* src/DHTResponseMessage.cc
* src/DHTResponseMessage.h
* test/DHTAnnouncePeerMessageTest.cc
* test/DHTAnnouncePeerReplyMessageTest.cc
* test/DHTFindNodeMessageTest.cc
* test/DHTFindNodeReplyMessageTest.cc
* test/DHTGetPeersMessageTest.cc
* test/DHTGetPeersReplyMessageTest.cc
* test/DHTMessageFactoryImplTest.cc
* test/DHTMessageTrackerTest.cc
* test/DHTPingMessageTest.cc
* test/DHTPingReplyMessageTest.cc
* test/MockDHTMessageFactory.h
2008-12-14 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added tests the case where length < 0 is specified.

View file

@ -37,14 +37,12 @@
#include <cassert>
#include "DHTNode.h"
#include "BencodeVisitor.h"
#include "DHTConnection.h"
#include "Dictionary.h"
#include "Data.h"
#include "DHTMessageDispatcher.h"
#include "DHTMessageFactory.h"
#include "DHTRoutingTable.h"
#include "DHTMessageCallback.h"
#include "bencode.h"
namespace aria2 {
@ -57,14 +55,11 @@ DHTAbstractMessage::~DHTAbstractMessage() {}
std::string DHTAbstractMessage::getBencodedMessage()
{
SharedHandle<Dictionary> msg(new Dictionary());
msg->put(DHTMessage::T, new Data(_transactionID));
msg->put(DHTMessage::Y, new Data(getType()));
fillMessage(msg.get());
BencodeVisitor v;
msg->accept(&v);
return v.getBencodedData();
bencode::BDE msgDict = bencode::BDE::dict();
msgDict[T] = _transactionID;
msgDict[Y] = getType();
fillMessage(msgDict);
return bencode::encode(msgDict);
}
bool DHTAbstractMessage::send()

View file

@ -40,7 +40,9 @@
namespace aria2 {
class Dictionary;
namespace bencode {
class BDE;
} // namespace bencode
class DHTConnection;
class DHTMessageDispatcher;
class DHTMessageFactory;
@ -66,7 +68,7 @@ public:
virtual std::string getType() const = 0;
virtual void fillMessage(Dictionary* message) = 0;
virtual void fillMessage(bencode::BDE& msgDict) = 0;
std::string getBencodedMessage();

View file

@ -33,9 +33,10 @@
*/
/* copyright --> */
#include "DHTAnnouncePeerMessage.h"
#include <cstring>
#include "DHTNode.h"
#include "Data.h"
#include "Dictionary.h"
#include "DHTRoutingTable.h"
#include "DHTMessageFactory.h"
#include "DHTMessageDispatcher.h"
@ -46,7 +47,7 @@
#include "DlAbortEx.h"
#include "BtConstants.h"
#include "StringFormat.h"
#include <cstring>
#include "bencode.h"
namespace aria2 {
@ -83,17 +84,14 @@ void DHTAnnouncePeerMessage::doReceivedAction()
_dispatcher->addMessageToQueue(reply);
}
Dictionary* DHTAnnouncePeerMessage::getArgument()
bencode::BDE DHTAnnouncePeerMessage::getArgument()
{
Dictionary* a = new Dictionary();
a->put(DHTMessage::ID, new Data(reinterpret_cast<const char*>(_localNode->getID()),
DHT_ID_LENGTH));
a->put(INFO_HASH, new Data(reinterpret_cast<const char*>(_infoHash),
DHT_ID_LENGTH));
a->put(PORT, new Data(Util::uitos(_tcpPort), true));
a->put(TOKEN, new Data(_token));
return a;
bencode::BDE aDict = bencode::BDE::dict();
aDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
aDict[INFO_HASH] = bencode::BDE(_infoHash, DHT_ID_LENGTH);
aDict[PORT] = _tcpPort;
aDict[TOKEN] = _token;
return aDict;
}
std::string DHTAnnouncePeerMessage::getMessageType() const

View file

@ -69,7 +69,7 @@ public:
virtual void doReceivedAction();
virtual Dictionary* getArgument();
virtual bencode::BDE getArgument();
virtual std::string getMessageType() const;

View file

@ -34,8 +34,7 @@
/* copyright --> */
#include "DHTAnnouncePeerReplyMessage.h"
#include "DHTNode.h"
#include "Dictionary.h"
#include "Data.h"
#include "bencode.h"
namespace aria2 {
@ -50,11 +49,11 @@ DHTAnnouncePeerReplyMessage::~DHTAnnouncePeerReplyMessage() {}
void DHTAnnouncePeerReplyMessage::doReceivedAction() {}
Dictionary* DHTAnnouncePeerReplyMessage::getResponse()
bencode::BDE DHTAnnouncePeerReplyMessage::getResponse()
{
Dictionary* r = new Dictionary();
r->put(DHTMessage::ID, new Data(_localNode->getID(), DHT_ID_LENGTH));
return r;
bencode::BDE rDict = bencode::BDE::dict();
rDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
return rDict;
}
std::string DHTAnnouncePeerReplyMessage::getMessageType() const

View file

@ -49,7 +49,7 @@ public:
virtual void doReceivedAction();
virtual Dictionary* getResponse();
virtual bencode::BDE getResponse();
virtual std::string getMessageType() const;

View file

@ -33,15 +33,16 @@
*/
/* copyright --> */
#include "DHTFindNodeMessage.h"
#include <cstring>
#include "DHTNode.h"
#include "Data.h"
#include "Dictionary.h"
#include "DHTRoutingTable.h"
#include "DHTMessageFactory.h"
#include "DHTMessageDispatcher.h"
#include "DHTMessageCallback.h"
#include "Util.h"
#include <cstring>
#include "bencode.h"
namespace aria2 {
@ -69,14 +70,12 @@ void DHTFindNodeMessage::doReceivedAction()
_dispatcher->addMessageToQueue(reply);
}
Dictionary* DHTFindNodeMessage::getArgument()
bencode::BDE DHTFindNodeMessage::getArgument()
{
Dictionary* a = new Dictionary();
a->put(DHTMessage::ID, new Data(reinterpret_cast<const char*>(_localNode->getID()),
DHT_ID_LENGTH));
a->put(TARGET_NODE, new Data(reinterpret_cast<const char*>(_targetNodeID),
DHT_ID_LENGTH));
return a;
bencode::BDE aDict = bencode::BDE::dict();
aDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
aDict[TARGET_NODE] = bencode::BDE(_targetNodeID, DHT_ID_LENGTH);
return aDict;
}
std::string DHTFindNodeMessage::getMessageType() const

View file

@ -55,7 +55,7 @@ public:
virtual void doReceivedAction();
virtual Dictionary* getArgument();
virtual bencode::BDE getArgument();
virtual std::string getMessageType() const;

View file

@ -33,17 +33,18 @@
*/
/* copyright --> */
#include "DHTFindNodeReplyMessage.h"
#include <cstring>
#include "DHTNode.h"
#include "DHTBucket.h"
#include "Data.h"
#include "Dictionary.h"
#include "DHTRoutingTable.h"
#include "DHTMessageFactory.h"
#include "DHTMessageDispatcher.h"
#include "DHTMessageCallback.h"
#include "PeerMessageUtil.h"
#include "Util.h"
#include <cstring>
#include "bencode.h"
namespace aria2 {
@ -67,22 +68,25 @@ void DHTFindNodeReplyMessage::doReceivedAction()
}
}
Dictionary* DHTFindNodeReplyMessage::getResponse()
bencode::BDE DHTFindNodeReplyMessage::getResponse()
{
Dictionary* a = new Dictionary();
a->put(DHTMessage::ID, new Data(_localNode->getID(), DHT_ID_LENGTH));
bencode::BDE aDict = bencode::BDE::dict();
aDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
size_t offset = 0;
unsigned char buffer[DHTBucket::K*26];
// TODO if _closestKNodes.size() > DHTBucket::K ??
for(std::deque<SharedHandle<DHTNode> >::const_iterator i = _closestKNodes.begin(); i != _closestKNodes.end(); ++i) {
for(std::deque<SharedHandle<DHTNode> >::const_iterator i =
_closestKNodes.begin();
i != _closestKNodes.end() && offset < DHTBucket::K*26; ++i) {
SharedHandle<DHTNode> node = *i;
memcpy(buffer+offset, node->getID(), DHT_ID_LENGTH);
if(PeerMessageUtil::createcompact(buffer+20+offset, node->getIPAddress(), node->getPort())) {
if(PeerMessageUtil::createcompact(buffer+20+offset, node->getIPAddress(),
node->getPort())) {
offset += 26;
}
}
a->put(NODES, new Data(buffer, offset));
return a;
aDict[NODES] = bencode::BDE(buffer, offset);
return aDict;
}
std::string DHTFindNodeReplyMessage::getMessageType() const

View file

@ -54,7 +54,7 @@ public:
virtual void doReceivedAction();
virtual Dictionary* getResponse();
virtual bencode::BDE getResponse();
virtual std::string getMessageType() const;

View file

@ -33,9 +33,10 @@
*/
/* copyright --> */
#include "DHTGetPeersMessage.h"
#include <cstring>
#include "DHTNode.h"
#include "Data.h"
#include "Dictionary.h"
#include "DHTRoutingTable.h"
#include "DHTMessageFactory.h"
#include "DHTMessageDispatcher.h"
@ -45,7 +46,7 @@
#include "Peer.h"
#include "DHTTokenTracker.h"
#include "Util.h"
#include <cstring>
#include "bencode.h"
namespace aria2 {
@ -87,12 +88,12 @@ void DHTGetPeersMessage::doReceivedAction()
_dispatcher->addMessageToQueue(reply);
}
Dictionary* DHTGetPeersMessage::getArgument()
bencode::BDE DHTGetPeersMessage::getArgument()
{
Dictionary* a = new Dictionary();
a->put(DHTMessage::ID, new Data(_localNode->getID(), DHT_ID_LENGTH));
a->put(INFO_HASH, new Data(_infoHash, DHT_ID_LENGTH));
return a;
bencode::BDE aDict = bencode::BDE::dict();
aDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
aDict[INFO_HASH] = bencode::BDE(_infoHash, DHT_ID_LENGTH);
return aDict;
}
std::string DHTGetPeersMessage::getMessageType() const

View file

@ -63,7 +63,7 @@ public:
virtual void doReceivedAction();
virtual Dictionary* getArgument();
virtual bencode::BDE getArgument();
virtual std::string getMessageType() const;

View file

@ -33,11 +33,11 @@
*/
/* copyright --> */
#include "DHTGetPeersReplyMessage.h"
#include <cstring>
#include "DHTNode.h"
#include "DHTBucket.h"
#include "Data.h"
#include "Dictionary.h"
#include "List.h"
#include "DHTRoutingTable.h"
#include "DHTMessageFactory.h"
#include "DHTMessageDispatcher.h"
@ -46,7 +46,7 @@
#include "Peer.h"
#include "DHTUtil.h"
#include "Util.h"
#include <cstring>
#include "bencode.h"
namespace aria2 {
@ -72,34 +72,37 @@ void DHTGetPeersReplyMessage::doReceivedAction()
// Returned peers and nodes are handled in DHTPeerLookupTask.
}
Dictionary* DHTGetPeersReplyMessage::getResponse()
bencode::BDE DHTGetPeersReplyMessage::getResponse()
{
Dictionary* r = new Dictionary();
r->put(DHTMessage::ID, new Data(_localNode->getID(), DHT_ID_LENGTH));
r->put(TOKEN, new Data(_token));
if(_values.size()) {
List* valuesList = new List();
r->put(VALUES, valuesList);
for(std::deque<SharedHandle<Peer> >::const_iterator i = _values.begin(); i != _values.end(); ++i) {
const SharedHandle<Peer>& peer = *i;
unsigned char buffer[6];
if(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port)) {
valuesList->add(new Data(buffer, sizeof(buffer)));
}
}
} else {
bencode::BDE rDict = bencode::BDE::dict();
rDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
rDict[TOKEN] = _token;
if(_values.empty()) {
size_t offset = 0;
unsigned char buffer[DHTBucket::K*26];
for(std::deque<SharedHandle<DHTNode> >::const_iterator i = _closestKNodes.begin(); i != _closestKNodes.end(); ++i) {
for(std::deque<SharedHandle<DHTNode> >::const_iterator i =
_closestKNodes.begin();
i != _closestKNodes.end() && offset < DHTBucket::K*26; ++i) {
SharedHandle<DHTNode> node = *i;
memcpy(buffer+offset, node->getID(), DHT_ID_LENGTH);
if(PeerMessageUtil::createcompact(buffer+20+offset, node->getIPAddress(), node->getPort())) {
offset += 26;
}
}
r->put(NODES, new Data(buffer, offset));
rDict[NODES] = bencode::BDE(buffer, offset);
} else {
bencode::BDE valuesList = bencode::BDE::list();
for(std::deque<SharedHandle<Peer> >::const_iterator i = _values.begin();
i != _values.end(); ++i) {
const SharedHandle<Peer>& peer = *i;
unsigned char buffer[6];
if(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port)) {
valuesList << bencode::BDE(buffer, sizeof(buffer));
}
}
rDict[VALUES] = valuesList;
}
return r;
return rDict;
}
std::string DHTGetPeersReplyMessage::getMessageType() const

View file

@ -36,9 +36,11 @@
#define _D_DHT_GET_PEERS_REPLY_MESSAGE_H_
#include "DHTResponseMessage.h"
#include "DHTConstants.h"
#include <deque>
#include "DHTConstants.h"
namespace aria2 {
class Peer;
@ -62,7 +64,7 @@ public:
virtual void doReceivedAction();
virtual Dictionary* getResponse();
virtual bencode::BDE getResponse();
virtual std::string getMessageType() const;

View file

@ -48,19 +48,21 @@ namespace aria2 {
class DHTMessage;
class DHTNode;
class Peer;
class Dictionary;
namespace bencode {
class BDE;
} // namespace bencode
class DHTMessageFactory {
public:
virtual ~DHTMessageFactory() {}
virtual SharedHandle<DHTMessage>
createQueryMessage(const Dictionary* d,
createQueryMessage(const bencode::BDE& dict,
const std::string& ipaddr, uint16_t port) = 0;
virtual SharedHandle<DHTMessage>
createResponseMessage(const std::string& messageType,
const Dictionary* d,
const bencode::BDE& dict,
const std::string& ipaddr, uint16_t port) = 0;
virtual SharedHandle<DHTMessage>

View file

@ -39,9 +39,6 @@
#include "LogFactory.h"
#include "DlAbortEx.h"
#include "Data.h"
#include "Dictionary.h"
#include "List.h"
#include "DHTNode.h"
#include "DHTRoutingTable.h"
#include "DHTPingMessage.h"
@ -64,6 +61,7 @@
#include "Peer.h"
#include "Logger.h"
#include "StringFormat.h"
#include "bencode.h"
namespace aria2 {
@ -84,10 +82,23 @@ DHTMessageFactoryImpl::getRemoteNode(const unsigned char* id, const std::string&
return node;
}
static const Dictionary* getDictionary(const Dictionary* d, const std::string& key)
static const bencode::BDE& getDictionary(const bencode::BDE& dict,
const std::string& key)
{
const Dictionary* c = dynamic_cast<const Dictionary*>(d->get(key));
if(c) {
const bencode::BDE& d = dict[key];
if(d.isDict()) {
return d;
} else {
throw DlAbortEx
(StringFormat("Malformed DHT message. Missing %s", key.c_str()).str());
}
}
static const bencode::BDE& getString(const bencode::BDE& dict,
const std::string& key)
{
const bencode::BDE& c = dict[key];
if(c.isString()) {
return c;
} else {
throw DlAbortEx
@ -95,10 +106,11 @@ static const Dictionary* getDictionary(const Dictionary* d, const std::string& k
}
}
static const Data* getData(const Dictionary* d, const std::string& key)
static const bencode::BDE& getInteger(const bencode::BDE& dict,
const std::string& key)
{
const Data* c = dynamic_cast<const Data*>(d->get(key));
if(c) {
const bencode::BDE& c = dict[key];
if(c.isInteger()) {
return c;
} else {
throw DlAbortEx
@ -106,22 +118,35 @@ static const Data* getData(const Dictionary* d, const std::string& key)
}
}
static const Data* getData(const List* l, size_t index)
static const bencode::BDE& getString(const bencode::BDE& list, size_t index)
{
const Data* c = dynamic_cast<const Data*>(l->getList()[index]);
if(c) {
const bencode::BDE& c = list[index];
if(c.isString()) {
return c;
} else {
throw DlAbortEx
(StringFormat("Malformed DHT message. element[%u] is not Data.",
(StringFormat("Malformed DHT message. element[%u] is not String.",
index).str());
}
}
static const List* getList(const Dictionary* d, const std::string& key)
static const bencode::BDE& getInteger(const bencode::BDE& list, size_t index)
{
const List* l = dynamic_cast<const List*>(d->get(key));
if(l) {
const bencode::BDE& c = list[index];
if(c.isInteger()) {
return c;
} else {
throw DlAbortEx
(StringFormat("Malformed DHT message. element[%u] is not Integer.",
index).str());
}
}
static const bencode::BDE& getList(const bencode::BDE& dict,
const std::string& key)
{
const bencode::BDE& l = dict[key];
if(l.isList()) {
return l;
} else {
throw DlAbortEx
@ -129,121 +154,121 @@ static const List* getList(const Dictionary* d, const std::string& key)
}
}
void DHTMessageFactoryImpl::validateID(const Data* id) const
void DHTMessageFactoryImpl::validateID(const bencode::BDE& id) const
{
if(id->getLen() != DHT_ID_LENGTH) {
if(id.s().size() != DHT_ID_LENGTH) {
throw DlAbortEx
(StringFormat("Malformed DHT message. Invalid ID length. Expected:%d, Actual:%d", DHT_ID_LENGTH, id->getLen()).str());
(StringFormat("Malformed DHT message. Invalid ID length."
" Expected:%d, Actual:%d",
DHT_ID_LENGTH, id.s().size()).str());
}
}
void DHTMessageFactoryImpl::validatePort(const Data* i) const
void DHTMessageFactoryImpl::validatePort(const bencode::BDE& i) const
{
if(!i->isNumber()) {
bencode::BDE::Integer port = i.i();
if(!(0 < port && port < UINT16_MAX)) {
throw DlAbortEx
(StringFormat("Malformed DHT message. Invalid port=%s",
Util::toHex(i->toString()).c_str()).str());
}
uint32_t port = i->toInt();
if(UINT16_MAX < port) {
throw DlAbortEx
(StringFormat("Malformed DHT message. Invalid port=%u", port).str());
Util::itos(port).c_str()).str());
}
}
SharedHandle<DHTMessage> DHTMessageFactoryImpl::createQueryMessage(const Dictionary* d,
const std::string& ipaddr,
uint16_t port)
SharedHandle<DHTMessage> DHTMessageFactoryImpl::createQueryMessage
(const bencode::BDE& dict,
const std::string& ipaddr,
uint16_t port)
{
const Data* q = getData(d, DHTQueryMessage::Q);
const Data* t = getData(d, DHTMessage::T);
const Data* y = getData(d, DHTMessage::Y);
const Dictionary* a = getDictionary(d, DHTQueryMessage::A);
if(y->toString() != DHTQueryMessage::Q) {
const bencode::BDE& messageType = getString(dict, DHTQueryMessage::Q);
const bencode::BDE& transactionID = getString(dict, DHTMessage::T);
const bencode::BDE& y = getString(dict, DHTMessage::Y);
const bencode::BDE& aDict = getDictionary(dict, DHTQueryMessage::A);
if(y.s() != DHTQueryMessage::Q) {
throw DlAbortEx("Malformed DHT message. y != q");
}
const Data* id = getData(getDictionary(d, DHTQueryMessage::A), DHTMessage::ID);
const bencode::BDE& id = getString(aDict, DHTMessage::ID);
validateID(id);
SharedHandle<DHTNode> remoteNode = getRemoteNode(id->getData(), ipaddr, port);
std::string messageType = q->toString();
std::string transactionID = t->toString();
if(messageType == DHTPingMessage::PING) {
return createPingMessage(remoteNode, transactionID);
} else if(messageType == DHTFindNodeMessage::FIND_NODE) {
const Data* targetNodeID = getData(a, DHTFindNodeMessage::TARGET_NODE);
SharedHandle<DHTNode> remoteNode = getRemoteNode(id.uc(), ipaddr, port);
if(messageType.s() == DHTPingMessage::PING) {
return createPingMessage(remoteNode, transactionID.s());
} else if(messageType.s() == DHTFindNodeMessage::FIND_NODE) {
const bencode::BDE& targetNodeID =
getString(aDict, DHTFindNodeMessage::TARGET_NODE);
validateID(targetNodeID);
return createFindNodeMessage(remoteNode, targetNodeID->getData(),
transactionID);
} else if(messageType == DHTGetPeersMessage::GET_PEERS) {
const Data* infoHash = getData(a, DHTGetPeersMessage::INFO_HASH);
return createFindNodeMessage(remoteNode, targetNodeID.uc(),
transactionID.s());
} else if(messageType.s() == DHTGetPeersMessage::GET_PEERS) {
const bencode::BDE& infoHash =
getString(aDict, DHTGetPeersMessage::INFO_HASH);
validateID(infoHash);
return createGetPeersMessage(remoteNode,
infoHash->getData(), transactionID);
} else if(messageType == DHTAnnouncePeerMessage::ANNOUNCE_PEER) {
const Data* infoHash = getData(a, DHTAnnouncePeerMessage::INFO_HASH);
infoHash.uc(), transactionID.s());
} else if(messageType.s() == DHTAnnouncePeerMessage::ANNOUNCE_PEER) {
const bencode::BDE& infoHash =
getString(aDict, DHTAnnouncePeerMessage::INFO_HASH);
validateID(infoHash);
const Data* port = getData(a, DHTAnnouncePeerMessage::PORT);
const bencode::BDE& port = getInteger(aDict, DHTAnnouncePeerMessage::PORT);
validatePort(port);
const Data* token = getData(a, DHTAnnouncePeerMessage::TOKEN);
return createAnnouncePeerMessage(remoteNode, infoHash->getData(),
static_cast<uint16_t>(port->toInt()),
token->toString(), transactionID);
const bencode::BDE& token = getString(aDict, DHTAnnouncePeerMessage::TOKEN);
return createAnnouncePeerMessage(remoteNode, infoHash.uc(),
static_cast<uint16_t>(port.i()),
token.s(), transactionID.s());
} else {
throw DlAbortEx
(StringFormat("Unsupported message type: %s", messageType.c_str()).str());
(StringFormat("Unsupported message type: %s",
messageType.s().c_str()).str());
}
}
SharedHandle<DHTMessage>
DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType,
const Dictionary* d,
const bencode::BDE& dict,
const std::string& ipaddr,
uint16_t port)
{
const Data* t = getData(d, DHTMessage::T);
const Data* y = getData(d, DHTMessage::Y);
if(y->toString() == DHTUnknownMessage::E) {
const bencode::BDE& transactionID = getString(dict, DHTMessage::T);
const bencode::BDE& y = getString(dict, DHTMessage::Y);
if(y.s() == DHTUnknownMessage::E) {
// for now, just report error message arrived and throw exception.
const List* e = getList(d, DHTUnknownMessage::E);
if(e->getList().size() == 2) {
const bencode::BDE& e = getList(dict, DHTUnknownMessage::E);
if(e.size() == 2) {
_logger->info("Received Error DHT message. code=%s, msg=%s",
Util::urlencode(getData(e, 0)->toString()).c_str(),
Util::urlencode(getData(e, 1)->toString()).c_str());
Util::itos(getInteger(e, 0).i()).c_str(),
Util::urlencode(getString(e, 1).s()).c_str());
} else {
_logger->debug("e doesn't have 2 elements.");
}
throw DlAbortEx("Received Error DHT message.");
} else if(y->toString() != DHTResponseMessage::R) {
} else if(y.s() != DHTResponseMessage::R) {
throw DlAbortEx
(StringFormat("Malformed DHT message. y != r: y=%s",
Util::urlencode(y->toString()).c_str()).str());
Util::urlencode(y.s()).c_str()).str());
}
const Dictionary* r = getDictionary(d, DHTResponseMessage::R);
const Data* id = getData(r, DHTMessage::ID);
const bencode::BDE& rDict = getDictionary(dict, DHTResponseMessage::R);
const bencode::BDE& id = getString(rDict, DHTMessage::ID);
validateID(id);
SharedHandle<DHTNode> remoteNode = getRemoteNode(id->getData(), ipaddr, port);
SharedHandle<DHTNode> remoteNode = getRemoteNode(id.uc(), ipaddr, port);
std::string transactionID = t->toString();
if(messageType == DHTPingReplyMessage::PING) {
return createPingReplyMessage(remoteNode,
id->getData(), transactionID);
return createPingReplyMessage(remoteNode, id.uc(), transactionID.s());
} else if(messageType == DHTFindNodeReplyMessage::FIND_NODE) {
return createFindNodeReplyMessage(remoteNode, d, transactionID);
return createFindNodeReplyMessage(remoteNode, dict, transactionID.s());
} else if(messageType == DHTGetPeersReplyMessage::GET_PEERS) {
const List* values =
dynamic_cast<const List*>(r->get(DHTGetPeersReplyMessage::VALUES));
if(values) {
return createGetPeersReplyMessageWithValues(remoteNode, d, transactionID);
const bencode::BDE& valuesList = rDict[DHTGetPeersReplyMessage::VALUES];
if(valuesList.isList()) {
return createGetPeersReplyMessageWithValues(remoteNode, dict,
transactionID.s());
} else {
const Data* nodes = dynamic_cast<const Data*>(r->get(DHTGetPeersReplyMessage::NODES));
if(nodes) {
return createGetPeersReplyMessageWithNodes(remoteNode, d, transactionID);
const bencode::BDE& nodes = rDict[DHTGetPeersReplyMessage::NODES];
if(nodes.isString()) {
return createGetPeersReplyMessageWithNodes(remoteNode, dict,
transactionID.s());
} else {
throw DlAbortEx("Malformed DHT message: missing nodes/values");
}
}
} else if(messageType == DHTAnnouncePeerReplyMessage::ANNOUNCE_PEER) {
return createAnnouncePeerReplyMessage(remoteNode, transactionID);
return createAnnouncePeerReplyMessage(remoteNode, transactionID.s());
} else {
throw DlAbortEx
(StringFormat("Unsupported message type: %s", messageType.c_str()).str());
@ -319,12 +344,16 @@ DHTMessageFactoryImpl::extractNodes(const unsigned char* src, size_t length)
}
SharedHandle<DHTMessage>
DHTMessageFactoryImpl::createFindNodeReplyMessage(const SharedHandle<DHTNode>& remoteNode,
const Dictionary* d,
const std::string& transactionID)
DHTMessageFactoryImpl::createFindNodeReplyMessage
(const SharedHandle<DHTNode>& remoteNode,
const bencode::BDE& dict,
const std::string& transactionID)
{
const Data* nodesData = getData(getDictionary(d, DHTResponseMessage::R), DHTFindNodeReplyMessage::NODES);
std::deque<SharedHandle<DHTNode> > nodes = extractNodes(nodesData->getData(), nodesData->getLen());
const bencode::BDE& nodesData =
getString(getDictionary(dict, DHTResponseMessage::R),
DHTFindNodeReplyMessage::NODES);
std::deque<SharedHandle<DHTNode> > nodes = extractNodes(nodesData.uc(),
nodesData.s().size());
return createFindNodeReplyMessage(remoteNode, nodes, transactionID);
}
@ -344,15 +373,18 @@ DHTMessageFactoryImpl::createGetPeersMessage(const SharedHandle<DHTNode>& remote
}
SharedHandle<DHTMessage>
DHTMessageFactoryImpl::createGetPeersReplyMessageWithNodes(const SharedHandle<DHTNode>& remoteNode,
const Dictionary* d,
const std::string& transactionID)
DHTMessageFactoryImpl::createGetPeersReplyMessageWithNodes
(const SharedHandle<DHTNode>& remoteNode,
const bencode::BDE& dict,
const std::string& transactionID)
{
const Dictionary* r = getDictionary(d, DHTResponseMessage::R);
const Data* nodesData = getData(r, DHTGetPeersReplyMessage::NODES);
std::deque<SharedHandle<DHTNode> > nodes = extractNodes(nodesData->getData(), nodesData->getLen());
const Data* token = getData(r, DHTGetPeersReplyMessage::TOKEN);
return createGetPeersReplyMessage(remoteNode, nodes, token->toString(),
const bencode::BDE& rDict = getDictionary(dict, DHTResponseMessage::R);
const bencode::BDE& nodesData = getString(rDict,
DHTGetPeersReplyMessage::NODES);
std::deque<SharedHandle<DHTNode> > nodes = extractNodes(nodesData.uc(),
nodesData.s().size());
const bencode::BDE& token = getString(rDict, DHTGetPeersReplyMessage::TOKEN);
return createGetPeersReplyMessage(remoteNode, nodes, token.s(),
transactionID);
}
@ -370,23 +402,27 @@ DHTMessageFactoryImpl::createGetPeersReplyMessage(const SharedHandle<DHTNode>& r
}
SharedHandle<DHTMessage>
DHTMessageFactoryImpl::createGetPeersReplyMessageWithValues(const SharedHandle<DHTNode>& remoteNode,
const Dictionary* d,
const std::string& transactionID)
DHTMessageFactoryImpl::createGetPeersReplyMessageWithValues
(const SharedHandle<DHTNode>& remoteNode,
const bencode::BDE& dict,
const std::string& transactionID)
{
const Dictionary* r = getDictionary(d, DHTResponseMessage::R);
const List* valuesList = getList(r, DHTGetPeersReplyMessage::VALUES);
const bencode::BDE& rDict = getDictionary(dict, DHTResponseMessage::R);
const bencode::BDE& valuesList = getList(rDict,
DHTGetPeersReplyMessage::VALUES);
std::deque<SharedHandle<Peer> > peers;
for(std::deque<MetaEntry*>::const_iterator i = valuesList->getList().begin(); i != valuesList->getList().end(); ++i) {
const Data* data = dynamic_cast<const Data*>(*i);
if(data && data->getLen() == 6) {
std::pair<std::string, uint16_t> addr = PeerMessageUtil::unpackcompact(data->getData());
for(bencode::BDE::List::const_iterator i = valuesList.listBegin();
i != valuesList.listEnd(); ++i) {
const bencode::BDE& data = *i;
if(data.isString() && data.s().size() == 6) {
std::pair<std::string, uint16_t> addr =
PeerMessageUtil::unpackcompact(data.uc());
PeerHandle peer(new Peer(addr.first, addr.second));
peers.push_back(peer);
}
}
const Data* token = getData(r, DHTGetPeersReplyMessage::TOKEN);
return createGetPeersReplyMessage(remoteNode, peers, token->toString(),
const bencode::BDE& token = getString(rDict, DHTGetPeersReplyMessage::TOKEN);
return createGetPeersReplyMessage(remoteNode, peers, token.s(),
transactionID);
}

View file

@ -46,7 +46,6 @@ class DHTRoutingTable;
class DHTPeerAnnounceStorage;
class DHTTokenTracker;
class Logger;
class Data;
class DHTMessage;
class DHTAbstractMessage;
@ -69,9 +68,9 @@ private:
// search node in routingTable. If it is not found, create new one.
SharedHandle<DHTNode> getRemoteNode(const unsigned char* id, const std::string& ipaddr, uint16_t port) const;
void validateID(const Data* id) const;
void validateID(const bencode::BDE& id) const;
void validatePort(const Data* i) const;
void validatePort(const bencode::BDE& i) const;
std::deque<SharedHandle<DHTNode> > extractNodes(const unsigned char* src, size_t length);
@ -83,12 +82,12 @@ public:
virtual ~DHTMessageFactoryImpl();
virtual SharedHandle<DHTMessage>
createQueryMessage(const Dictionary* d,
createQueryMessage(const bencode::BDE& dict,
const std::string& ipaddr, uint16_t port);
virtual SharedHandle<DHTMessage>
createResponseMessage(const std::string& messageType,
const Dictionary* d,
const bencode::BDE& dict,
const std::string& ipaddr, uint16_t port);
virtual SharedHandle<DHTMessage>
@ -107,7 +106,7 @@ public:
SharedHandle<DHTMessage>
createFindNodeReplyMessage(const SharedHandle<DHTNode>& remoteNode,
const Dictionary* d,
const bencode::BDE& dict,
const std::string& transactionID);
@ -129,7 +128,7 @@ public:
SharedHandle<DHTMessage>
createGetPeersReplyMessageWithNodes(const SharedHandle<DHTNode>& remoteNode,
const Dictionary* d,
const bencode::BDE& dict,
const std::string& transactionID);
virtual SharedHandle<DHTMessage>
@ -140,7 +139,7 @@ public:
SharedHandle<DHTMessage>
createGetPeersReplyMessageWithValues(const SharedHandle<DHTNode>& remoteNode,
const Dictionary* d,
const bencode::BDE& dict,
const std::string& transactionID);
virtual SharedHandle<DHTMessage>

View file

@ -46,13 +46,11 @@
#include "DHTRoutingTable.h"
#include "DHTNode.h"
#include "DHTMessageCallback.h"
#include "Dictionary.h"
#include "Data.h"
#include "MetaFileUtil.h"
#include "DlAbortEx.h"
#include "LogFactory.h"
#include "Logger.h"
#include "Util.h"
#include "bencode.h"
namespace aria2 {
@ -76,27 +74,28 @@ SharedHandle<DHTMessage> DHTMessageReceiver::receiveMessage()
}
try {
bool isReply = false;
MetaEntryHandle msgroot(MetaFileUtil::bdecoding(data, length));
const Dictionary* d = dynamic_cast<const Dictionary*>(msgroot.get());
if(d) {
const Data* y = dynamic_cast<const Data*>(d->get(DHTMessage::Y));
if(y) {
if(y->toString() == DHTResponseMessage::R ||
y->toString() == DHTUnknownMessage::E) {
const bencode::BDE dict = bencode::decode(data, length);
if(dict.isDict()) {
const bencode::BDE& y = dict[DHTMessage::Y];
if(y.isString()) {
if(y.s() == DHTResponseMessage::R || y.s() == DHTUnknownMessage::E) {
isReply = true;
}
} else {
_logger->info("Malformed DHT message. Missing 'y' key. From:%s:%u", remoteAddr.c_str(), remotePort);
_logger->info("Malformed DHT message. Missing 'y' key. From:%s:%u",
remoteAddr.c_str(), remotePort);
return handleUnknownMessage(data, sizeof(data), remoteAddr, remotePort);
}
} else {
_logger->info("Malformed DHT message. This is not a bencoded directory. From:%s:%u", remoteAddr.c_str(), remotePort);
_logger->info("Malformed DHT message. This is not a bencoded directory."
" From:%s:%u", remoteAddr.c_str(), remotePort);
return handleUnknownMessage(data, sizeof(data), remoteAddr, remotePort);
}
SharedHandle<DHTMessage> message;
SharedHandle<DHTMessageCallback> callback;
if(isReply) {
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p = _tracker->messageArrived(d, remoteAddr, remotePort);
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
_tracker->messageArrived(dict, remoteAddr, remotePort);
message = p.first;
callback = p.second;
if(message.isNull()) {
@ -104,7 +103,7 @@ SharedHandle<DHTMessage> DHTMessageReceiver::receiveMessage()
return handleUnknownMessage(data, sizeof(data), remoteAddr, remotePort);
}
} else {
message = _factory->createQueryMessage(d, remoteAddr, remotePort);
message = _factory->createQueryMessage(dict, remoteAddr, remotePort);
if(message->getLocalNode() == message->getRemoteNode()) {
// drop message from localnode
_logger->info("Recieved DHT message from localnode.");

View file

@ -45,11 +45,10 @@
#include "Util.h"
#include "LogFactory.h"
#include "Logger.h"
#include "Dictionary.h"
#include "Data.h"
#include "DlAbortEx.h"
#include "DHTConstants.h"
#include "StringFormat.h"
#include "bencode.h"
namespace aria2 {
@ -70,26 +69,26 @@ void DHTMessageTracker::addMessage(const SharedHandle<DHTMessage>& message, cons
}
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> >
DHTMessageTracker::messageArrived(const Dictionary* d,
DHTMessageTracker::messageArrived(const bencode::BDE& dict,
const std::string& ipaddr, uint16_t port)
{
const Data* tid = dynamic_cast<const Data*>(d->get(DHTMessage::T));
if(!tid) {
const bencode::BDE& tid = dict[DHTMessage::T];
if(!tid.isString()) {
throw DlAbortEx(StringFormat("Malformed DHT message. From:%s:%u",
ipaddr.c_str(), port).str());
}
_logger->debug("Searching tracker entry for TransactionID=%s, Remote=%s:%u",
Util::toHex(tid->toString()).c_str(), ipaddr.c_str(), port);
for(std::deque<SharedHandle<DHTMessageTrackerEntry> >::iterator i = _entries.begin();
i != _entries.end(); ++i) {
if((*i)->match(tid->toString(), ipaddr, port)) {
Util::toHex(tid.s()).c_str(), ipaddr.c_str(), port);
for(std::deque<SharedHandle<DHTMessageTrackerEntry> >::iterator i =
_entries.begin(); i != _entries.end(); ++i) {
if((*i)->match(tid.s(), ipaddr, port)) {
SharedHandle<DHTMessageTrackerEntry> entry = *i;
_entries.erase(i);
_logger->debug("Tracker entry found.");
SharedHandle<DHTNode> targetNode = entry->getTargetNode();
SharedHandle<DHTMessage> message =
_factory->createResponseMessage(entry->getMessageType(), d,
_factory->createResponseMessage(entry->getMessageType(), dict,
targetNode->getIPAddress(),
targetNode->getPort());

View file

@ -36,11 +36,13 @@
#define _D_DHT_MESSAGE_TRACKER_H_
#include "common.h"
#include "SharedHandle.h"
#include "a2time.h"
#include <utility>
#include <deque>
#include "SharedHandle.h"
#include "a2time.h"
namespace aria2 {
class DHTMessage;
@ -49,7 +51,9 @@ class DHTRoutingTable;
class DHTMessageFactory;
class DHTMessageTrackerEntry;
class Logger;
class Dictionary;
namespace bencode {
class BDE;
} // namespace bencode
class DHTMessageTracker {
private:
@ -75,7 +79,8 @@ public:
SharedHandle<DHTMessageCallback>());
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> >
messageArrived(const Dictionary* d, const std::string& ipaddr, uint16_t port);
messageArrived(const bencode::BDE& dict,
const std::string& ipaddr, uint16_t port);
void handleTimeout();

View file

@ -34,12 +34,11 @@
/* copyright --> */
#include "DHTPingMessage.h"
#include "DHTNode.h"
#include "Dictionary.h"
#include "Data.h"
#include "DHTConstants.h"
#include "DHTMessageDispatcher.h"
#include "DHTMessageFactory.h"
#include "DHTMessageCallback.h"
#include "bencode.h"
namespace aria2 {
@ -59,12 +58,11 @@ void DHTPingMessage::doReceivedAction()
_dispatcher->addMessageToQueue(reply);
}
Dictionary* DHTPingMessage::getArgument()
bencode::BDE DHTPingMessage::getArgument()
{
Dictionary* a = new Dictionary();
a->put(DHTMessage::ID, new Data(reinterpret_cast<const char*>(_localNode->getID()),
DHT_ID_LENGTH));
return a;
bencode::BDE aDict = bencode::BDE::dict();
aDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
return aDict;
}
std::string DHTPingMessage::getMessageType() const

View file

@ -50,7 +50,7 @@ public:
virtual void doReceivedAction();
virtual Dictionary* getArgument();
virtual bencode::BDE getArgument();
virtual std::string getMessageType() const;

View file

@ -33,11 +33,12 @@
*/
/* copyright --> */
#include "DHTPingReplyMessage.h"
#include "DHTNode.h"
#include "Dictionary.h"
#include "Data.h"
#include <cstring>
#include "DHTNode.h"
#include "bencode.h"
namespace aria2 {
const std::string DHTPingReplyMessage::PING("ping");
@ -55,11 +56,11 @@ DHTPingReplyMessage::~DHTPingReplyMessage() {}
void DHTPingReplyMessage::doReceivedAction() {}
Dictionary* DHTPingReplyMessage::getResponse()
bencode::BDE DHTPingReplyMessage::getResponse()
{
Dictionary* r = new Dictionary();
r->put(DHTMessage::ID, new Data(_id, DHT_ID_LENGTH));
return r;
bencode::BDE rDict = bencode::BDE::dict();
rDict[DHTMessage::ID] = bencode::BDE(_id, DHT_ID_LENGTH);
return rDict;
}
std::string DHTPingReplyMessage::getMessageType() const

View file

@ -53,7 +53,7 @@ public:
virtual void doReceivedAction();
virtual Dictionary* getResponse();
virtual bencode::BDE getResponse();
virtual std::string getMessageType() const;

View file

@ -35,8 +35,7 @@
#include "DHTQueryMessage.h"
#include "DHTNode.h"
#include "Util.h"
#include "Dictionary.h"
#include "Data.h"
#include "bencode.h"
namespace aria2 {
@ -56,10 +55,10 @@ std::string DHTQueryMessage::getType() const
return Q;
}
void DHTQueryMessage::fillMessage(Dictionary* message)
void DHTQueryMessage::fillMessage(bencode::BDE& msgDict)
{
message->put(Q, new Data(getMessageType()));
message->put(A, getArgument());
msgDict[Q] = getMessageType();
msgDict[A] = getArgument();
}
bool DHTQueryMessage::isReply() const

View file

@ -52,9 +52,9 @@ public:
virtual std::string getType() const;
virtual void fillMessage(Dictionary* message);
virtual void fillMessage(bencode::BDE& msgDict);
virtual Dictionary* getArgument() = 0;
virtual bencode::BDE getArgument() = 0;
virtual bool isReply() const;

View file

@ -35,8 +35,7 @@
#include "DHTResponseMessage.h"
#include "DHTNode.h"
#include "Util.h"
#include "Dictionary.h"
#include "Data.h"
#include "bencode.h"
namespace aria2 {
@ -54,9 +53,9 @@ std::string DHTResponseMessage::getType() const
return R;
}
void DHTResponseMessage::fillMessage(Dictionary* message)
void DHTResponseMessage::fillMessage(bencode::BDE& msgDict)
{
message->put(R, getResponse());
msgDict[R] = getResponse();
}
bool DHTResponseMessage::isReply() const

View file

@ -52,9 +52,9 @@ public:
virtual std::string getType() const;
virtual void fillMessage(Dictionary* message);
virtual void fillMessage(bencode::BDE& msgDict);
virtual Dictionary* getResponse() = 0;
virtual bencode::BDE getResponse() = 0;
virtual bool isReply() const;

View file

@ -1,16 +1,16 @@
#include "DHTAnnouncePeerMessage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "DHTNode.h"
#include "DHTUtil.h"
#include "BencodeVisitor.h"
#include "Dictionary.h"
#include "Data.h"
#include "Exception.h"
#include "Util.h"
#include "MockDHTMessageFactory.h"
#include "MockDHTMessageDispatcher.h"
#include "MockDHTMessage.h"
#include "DHTPeerAnnounceStorage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "bencode.h"
namespace aria2 {
@ -60,21 +60,18 @@ void DHTAnnouncePeerMessageTest::testGetBencodedMessage()
std::string msgbody = msg.getBencodedMessage();
SharedHandle<Dictionary> cm(new Dictionary());
cm->put("t", new Data(transactionID));
cm->put("y", new Data("q"));
cm->put("q", new Data("announce_peer"));
Dictionary* a = new Dictionary();
cm->put("a", a);
a->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
a->put("info_hash", new Data(infoHash, DHT_ID_LENGTH));
a->put("port", new Data(Util::uitos(port), true));
a->put("token", new Data(token));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = transactionID;
dict["y"] = bencode::BDE("q");
dict["q"] = bencode::BDE("announce_peer");
bencode::BDE aDict = bencode::BDE::dict();
aDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
aDict["info_hash"] = bencode::BDE(infoHash, DHT_ID_LENGTH);
aDict["port"] = port;
aDict["token"] = token;
dict["a"] = aDict;
BencodeVisitor v;
cm->accept(&v);
CPPUNIT_ASSERT_EQUAL(Util::urlencode(v.getBencodedData()),
CPPUNIT_ASSERT_EQUAL(Util::urlencode(bencode::encode(dict)),
Util::urlencode(msgbody));
}

View file

@ -1,12 +1,12 @@
#include "DHTAnnouncePeerReplyMessage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "DHTNode.h"
#include "DHTUtil.h"
#include "BencodeVisitor.h"
#include "Dictionary.h"
#include "Data.h"
#include "Exception.h"
#include "Util.h"
#include <cppunit/extensions/HelperMacros.h>
#include "bencode.h"
namespace aria2 {
@ -39,17 +39,14 @@ void DHTAnnouncePeerReplyMessageTest::testGetBencodedMessage()
std::string msgbody = msg.getBencodedMessage();
SharedHandle<Dictionary> cm(new Dictionary());
cm->put("t", new Data(transactionID));
cm->put("y", new Data("r"));
Dictionary* r = new Dictionary();
cm->put("r", r);
r->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = transactionID;
dict["y"] = bencode::BDE("r");
bencode::BDE rDict = bencode::BDE::dict();
rDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
dict["r"] = rDict;
BencodeVisitor v;
cm->accept(&v);
CPPUNIT_ASSERT_EQUAL(v.getBencodedData(), msgbody);
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
}
} // namespace aria2

View file

@ -1,16 +1,16 @@
#include "DHTFindNodeMessage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "DHTNode.h"
#include "DHTUtil.h"
#include "BencodeVisitor.h"
#include "Dictionary.h"
#include "Data.h"
#include "Exception.h"
#include "Util.h"
#include "MockDHTMessageFactory.h"
#include "MockDHTMessage.h"
#include "MockDHTMessageDispatcher.h"
#include "DHTRoutingTable.h"
#include <cppunit/extensions/HelperMacros.h>
#include "bencode.h"
namespace aria2 {
@ -61,19 +61,16 @@ void DHTFindNodeMessageTest::testGetBencodedMessage()
std::string msgbody = msg.getBencodedMessage();
SharedHandle<Dictionary> cm(new Dictionary());
cm->put("t", new Data(transactionID));
cm->put("y", new Data("q"));
cm->put("q", new Data("find_node"));
Dictionary* a = new Dictionary();
cm->put("a", a);
a->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
a->put("target", new Data(targetNode->getID(), DHT_ID_LENGTH));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = transactionID;
dict["y"] = bencode::BDE("q");
dict["q"] = bencode::BDE("find_node");
bencode::BDE aDict = bencode::BDE::dict();
aDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
aDict["target"] = bencode::BDE(targetNode->getID(), DHT_ID_LENGTH);
dict["a"] = aDict;
BencodeVisitor v;
cm->accept(&v);
CPPUNIT_ASSERT_EQUAL(v.getBencodedData(), msgbody);
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
}
void DHTFindNodeMessageTest::testDoReceivedAction()

View file

@ -1,14 +1,14 @@
#include "DHTFindNodeReplyMessage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "DHTNode.h"
#include "DHTUtil.h"
#include "BencodeVisitor.h"
#include "Dictionary.h"
#include "Data.h"
#include "Exception.h"
#include "Util.h"
#include "DHTBucket.h"
#include "PeerMessageUtil.h"
#include <cppunit/extensions/HelperMacros.h>
#include "bencode.h"
namespace aria2 {
@ -56,18 +56,15 @@ void DHTFindNodeReplyMessageTest::testGetBencodedMessage()
std::string msgbody = msg.getBencodedMessage();
SharedHandle<Dictionary> cm(new Dictionary());
cm->put("t", new Data(transactionID));
cm->put("y", new Data("r"));
Dictionary* r = new Dictionary();
cm->put("r", r);
r->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
r->put("nodes", new Data(compactNodeInfo));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = transactionID;
dict["y"] = bencode::BDE("r");
bencode::BDE rDict = bencode::BDE::dict();
rDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
rDict["nodes"] = compactNodeInfo;
dict["r"] = rDict;
BencodeVisitor v;
cm->accept(&v);
CPPUNIT_ASSERT_EQUAL(v.getBencodedData(), msgbody);
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
}
} // namespace aria2

View file

@ -1,9 +1,9 @@
#include "DHTGetPeersMessage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "DHTNode.h"
#include "DHTUtil.h"
#include "BencodeVisitor.h"
#include "Dictionary.h"
#include "Data.h"
#include "Exception.h"
#include "Util.h"
#include "MockDHTMessageFactory.h"
@ -12,7 +12,7 @@
#include "DHTTokenTracker.h"
#include "DHTPeerAnnounceStorage.h"
#include "DHTRoutingTable.h"
#include <cppunit/extensions/HelperMacros.h>
#include "bencode.h"
namespace aria2 {
@ -80,19 +80,16 @@ void DHTGetPeersMessageTest::testGetBencodedMessage()
std::string msgbody = msg.getBencodedMessage();
SharedHandle<Dictionary> cm(new Dictionary());
cm->put("t", new Data(transactionID));
cm->put("y", new Data("q"));
cm->put("q", new Data("get_peers"));
Dictionary* a = new Dictionary();
cm->put("a", a);
a->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
a->put("info_hash", new Data(infoHash, DHT_ID_LENGTH));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = transactionID;
dict["y"] = bencode::BDE("q");
dict["q"] = bencode::BDE("get_peers");
bencode::BDE aDict = bencode::BDE::dict();
aDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
aDict["info_hash"] = bencode::BDE(infoHash, DHT_ID_LENGTH);
dict["a"] = aDict;
BencodeVisitor v;
cm->accept(&v);
CPPUNIT_ASSERT_EQUAL(Util::urlencode(v.getBencodedData()),
CPPUNIT_ASSERT_EQUAL(Util::urlencode(bencode::encode(dict)),
Util::urlencode(msgbody));
}

View file

@ -1,16 +1,15 @@
#include "DHTGetPeersReplyMessage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "DHTNode.h"
#include "DHTUtil.h"
#include "BencodeVisitor.h"
#include "Dictionary.h"
#include "Data.h"
#include "Exception.h"
#include "Util.h"
#include "DHTBucket.h"
#include "PeerMessageUtil.h"
#include "List.h"
#include "Peer.h"
#include <cppunit/extensions/HelperMacros.h>
#include "bencode.h"
namespace aria2 {
@ -43,14 +42,13 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
DHTGetPeersReplyMessage msg(localNode, remoteNode, token, transactionID);
SharedHandle<Dictionary> cm(new Dictionary());
cm->put("t", new Data(transactionID));
cm->put("y", new Data("r"));
Dictionary* r = new Dictionary();
cm->put("r", r);
r->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
r->put("token", new Data(token));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = transactionID;
dict["y"] = bencode::BDE("r");
bencode::BDE rDict = bencode::BDE::dict();
rDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
rDict["token"] = token;
dict["r"] = rDict;
{
std::string compactNodeInfo;
SharedHandle<DHTNode> nodes[8];
@ -60,40 +58,39 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
nodes[i]->setPort(6881+i);
unsigned char buf[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
CPPUNIT_ASSERT(PeerMessageUtil::createcompact
(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
compactNodeInfo +=
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
std::string(&buf[0], &buf[sizeof(buf)]);
}
msg.setClosestKNodes(std::deque<SharedHandle<DHTNode> >(&nodes[0], &nodes[DHTBucket::K]));
msg.setClosestKNodes
(std::deque<SharedHandle<DHTNode> >(&nodes[0], &nodes[DHTBucket::K]));
std::string msgbody = msg.getBencodedMessage();
r->put("nodes", new Data(compactNodeInfo));
rDict["nodes"] = compactNodeInfo;
BencodeVisitor v;
cm->accept(&v);
CPPUNIT_ASSERT_EQUAL(Util::urlencode(v.getBencodedData()),
CPPUNIT_ASSERT_EQUAL(Util::urlencode(bencode::encode(dict)),
Util::urlencode(msgbody));
}
r->remove("nodes");
rDict.removeKey("nodes");
{
std::deque<SharedHandle<Peer> > peers;
List* values = new List();
r->put("values", values);
bencode::BDE valuesList = bencode::BDE::list();
for(size_t i = 0; i < 4; ++i) {
SharedHandle<Peer> peer(new Peer("192.168.0."+Util::uitos(i+1), 6881+i));
unsigned char buffer[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port));
values->add(new Data(buffer, sizeof(buffer)));
valuesList << bencode::BDE(buffer, sizeof(buffer));
peers.push_back(peer);
}
rDict["values"] = valuesList;
msg.setValues(peers);
std::string msgbody = msg.getBencodedMessage();
BencodeVisitor v;
cm->accept(&v);
CPPUNIT_ASSERT_EQUAL(Util::urlencode(v.getBencodedData()),
CPPUNIT_ASSERT_EQUAL(Util::urlencode(bencode::encode(dict)),
Util::urlencode(msgbody));
}
}

View file

@ -9,9 +9,6 @@
#include "Util.h"
#include "DHTNode.h"
#include "DHTRoutingTable.h"
#include "Dictionary.h"
#include "Data.h"
#include "List.h"
#include "Peer.h"
#include "PeerMessageUtil.h"
#include "DHTBucket.h"
@ -23,6 +20,7 @@
#include "DHTGetPeersReplyMessage.h"
#include "DHTAnnouncePeerMessage.h"
#include "DHTAnnouncePeerReplyMessage.h"
#include "bencode.h"
namespace aria2 {
@ -81,16 +79,17 @@ CPPUNIT_TEST_SUITE_REGISTRATION(DHTMessageFactoryImplTest);
void DHTMessageFactoryImplTest::testCreatePingMessage()
{
SharedHandle<Dictionary> d(new Dictionary());
d->put("t", new Data(transactionID, DHT_TRANSACTION_ID_LENGTH));
d->put("y", new Data("q"));
d->put("q", new Data("ping"));
Dictionary* a = new Dictionary();
a->put("id", new Data(remoteNodeID, DHT_ID_LENGTH));
d->put("a", a);
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
dict["y"] = bencode::BDE("q");
dict["q"] = bencode::BDE("ping");
bencode::BDE aDict = bencode::BDE::dict();
aDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
dict["a"] = aDict;
SharedHandle<DHTPingMessage> m
(dynamic_pointer_cast<DHTPingMessage>(factory->createQueryMessage(d.get(), "192.168.0.1", 6881)));
(dynamic_pointer_cast<DHTPingMessage>
(factory->createQueryMessage(dict, "192.168.0.1", 6881)));
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
remoteNode->setIPAddress("192.168.0.1");
remoteNode->setPort(6881);
@ -103,12 +102,12 @@ void DHTMessageFactoryImplTest::testCreatePingMessage()
void DHTMessageFactoryImplTest::testCreatePingReplyMessage()
{
SharedHandle<Dictionary> d(new Dictionary());
d->put("t", new Data(transactionID, DHT_TRANSACTION_ID_LENGTH));
d->put("y", new Data("r"));
Dictionary* r = new Dictionary();
r->put("id", new Data(remoteNodeID, DHT_ID_LENGTH));
d->put("r", r);
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
dict["y"] = bencode::BDE("r");
bencode::BDE rDict = bencode::BDE::dict();
rDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
dict["r"] = rDict;
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
remoteNode->setIPAddress("192.168.0.1");
@ -116,7 +115,7 @@ void DHTMessageFactoryImplTest::testCreatePingReplyMessage()
SharedHandle<DHTPingReplyMessage> m
(dynamic_pointer_cast<DHTPingReplyMessage>
(factory->createResponseMessage("ping", d.get(),
(factory->createResponseMessage("ping", dict,
remoteNode->getIPAddress(),
remoteNode->getPort())));
@ -128,19 +127,20 @@ void DHTMessageFactoryImplTest::testCreatePingReplyMessage()
void DHTMessageFactoryImplTest::testCreateFindNodeMessage()
{
SharedHandle<Dictionary> d(new Dictionary());
d->put("t", new Data(transactionID, DHT_TRANSACTION_ID_LENGTH));
d->put("y", new Data("q"));
d->put("q", new Data("find_node"));
Dictionary* a = new Dictionary();
a->put("id", new Data(remoteNodeID, DHT_ID_LENGTH));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
dict["y"] = bencode::BDE("q");
dict["q"] = bencode::BDE("find_node");
bencode::BDE aDict = bencode::BDE::dict();
aDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
unsigned char targetNodeID[DHT_ID_LENGTH];
memset(targetNodeID, 0x11, DHT_ID_LENGTH);
a->put("target", new Data(targetNodeID, DHT_ID_LENGTH));
d->put("a", a);
aDict["target"] = bencode::BDE(targetNodeID, DHT_ID_LENGTH);
dict["a"] = aDict;
SharedHandle<DHTFindNodeMessage> m
(dynamic_pointer_cast<DHTFindNodeMessage>(factory->createQueryMessage(d.get(), "192.168.0.1", 6881)));
(dynamic_pointer_cast<DHTFindNodeMessage>
(factory->createQueryMessage(dict, "192.168.0.1", 6881)));
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
remoteNode->setIPAddress("192.168.0.1");
remoteNode->setPort(6881);
@ -156,11 +156,11 @@ void DHTMessageFactoryImplTest::testCreateFindNodeMessage()
void DHTMessageFactoryImplTest::testCreateFindNodeReplyMessage()
{
try {
SharedHandle<Dictionary> d(new Dictionary());
d->put("t", new Data(transactionID, DHT_TRANSACTION_ID_LENGTH));
d->put("y", new Data("r"));
Dictionary* r = new Dictionary();
r->put("id", new Data(remoteNodeID, DHT_ID_LENGTH));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
dict["y"] = bencode::BDE("r");
bencode::BDE rDict = bencode::BDE::dict();
rDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
std::string compactNodeInfo;
SharedHandle<DHTNode> nodes[8];
for(size_t i = 0; i < DHTBucket::K; ++i) {
@ -169,13 +169,14 @@ void DHTMessageFactoryImplTest::testCreateFindNodeReplyMessage()
nodes[i]->setPort(6881+i);
unsigned char buf[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
CPPUNIT_ASSERT(PeerMessageUtil::createcompact
(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
compactNodeInfo +=
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
std::string(&buf[0], &buf[sizeof(buf)]);
}
r->put("nodes", new Data(compactNodeInfo));
d->put("r", r);
rDict["nodes"] = compactNodeInfo;
dict["r"] = rDict;
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
remoteNode->setIPAddress("192.168.0.1");
@ -183,7 +184,7 @@ void DHTMessageFactoryImplTest::testCreateFindNodeReplyMessage()
SharedHandle<DHTFindNodeReplyMessage> m
(dynamic_pointer_cast<DHTFindNodeReplyMessage>
(factory->createResponseMessage("find_node", d.get(),
(factory->createResponseMessage("find_node", dict,
remoteNode->getIPAddress(),
remoteNode->getPort())));
@ -201,19 +202,20 @@ void DHTMessageFactoryImplTest::testCreateFindNodeReplyMessage()
void DHTMessageFactoryImplTest::testCreateGetPeersMessage()
{
SharedHandle<Dictionary> d(new Dictionary());
d->put("t", new Data(transactionID, DHT_TRANSACTION_ID_LENGTH));
d->put("y", new Data("q"));
d->put("q", new Data("get_peers"));
Dictionary* a = new Dictionary();
a->put("id", new Data(remoteNodeID, DHT_ID_LENGTH));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
dict["y"] = bencode::BDE("q");
dict["q"] = bencode::BDE("get_peers");
bencode::BDE aDict = bencode::BDE::dict();
aDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
unsigned char infoHash[DHT_ID_LENGTH];
memset(infoHash, 0x11, DHT_ID_LENGTH);
a->put("info_hash", new Data(infoHash, DHT_ID_LENGTH));
d->put("a", a);
aDict["info_hash"] = bencode::BDE(infoHash, DHT_ID_LENGTH);
dict["a"] = aDict;
SharedHandle<DHTGetPeersMessage> m
(dynamic_pointer_cast<DHTGetPeersMessage>(factory->createQueryMessage(d.get(), "192.168.0.1", 6881)));
(dynamic_pointer_cast<DHTGetPeersMessage>
(factory->createQueryMessage(dict, "192.168.0.1", 6881)));
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
remoteNode->setIPAddress("192.168.0.1");
remoteNode->setPort(6881);
@ -229,11 +231,11 @@ void DHTMessageFactoryImplTest::testCreateGetPeersMessage()
void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_nodes()
{
try {
SharedHandle<Dictionary> d(new Dictionary());
d->put("t", new Data(transactionID, DHT_TRANSACTION_ID_LENGTH));
d->put("y", new Data("r"));
Dictionary* r = new Dictionary();
r->put("id", new Data(remoteNodeID, DHT_ID_LENGTH));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
dict["y"] = bencode::BDE("r");
bencode::BDE rDict = bencode::BDE::dict();
rDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
std::string compactNodeInfo;
SharedHandle<DHTNode> nodes[8];
for(size_t i = 0; i < DHTBucket::K; ++i) {
@ -242,14 +244,15 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_nodes()
nodes[i]->setPort(6881+i);
unsigned char buf[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
CPPUNIT_ASSERT(PeerMessageUtil::createcompact
(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
compactNodeInfo +=
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
std::string(&buf[0], &buf[sizeof(buf)]);
}
r->put("nodes", new Data(compactNodeInfo));
r->put("token", new Data("token"));
d->put("r", r);
rDict["nodes"] = compactNodeInfo;
rDict["token"] = bencode::BDE("token");
dict["r"] = rDict;
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
remoteNode->setIPAddress("192.168.0.1");
@ -257,7 +260,7 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_nodes()
SharedHandle<DHTGetPeersReplyMessage> m
(dynamic_pointer_cast<DHTGetPeersReplyMessage>
(factory->createResponseMessage("get_peers", d.get(),
(factory->createResponseMessage("get_peers", dict,
remoteNode->getIPAddress(),
remoteNode->getPort())));
@ -277,25 +280,25 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_nodes()
void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_values()
{
try {
SharedHandle<Dictionary> d(new Dictionary());
d->put("t", new Data(transactionID, DHT_TRANSACTION_ID_LENGTH));
d->put("y", new Data("r"));
Dictionary* r = new Dictionary();
r->put("id", new Data(remoteNodeID, DHT_ID_LENGTH));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
dict["y"] = bencode::BDE("r");
bencode::BDE rDict = bencode::BDE::dict();
rDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
std::deque<SharedHandle<Peer> > peers;
List* values = new List();
r->put("values", values);
bencode::BDE valuesList = bencode::BDE::list();
for(size_t i = 0; i < 4; ++i) {
SharedHandle<Peer> peer(new Peer("192.168.0."+Util::uitos(i+1), 6881+i));
unsigned char buffer[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port));
values->add(new Data(buffer, sizeof(buffer)));
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr,
peer->port));
valuesList << bencode::BDE(buffer, sizeof(buffer));
peers.push_back(peer);
}
r->put("values", values);
r->put("token", new Data("token"));
d->put("r", r);
rDict["values"] = valuesList;
rDict["token"] = bencode::BDE("token");
dict["r"] = rDict;
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
remoteNode->setIPAddress("192.168.0.1");
@ -303,7 +306,7 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_values()
SharedHandle<DHTGetPeersReplyMessage> m
(dynamic_pointer_cast<DHTGetPeersReplyMessage>
(factory->createResponseMessage("get_peers", d.get(),
(factory->createResponseMessage("get_peers", dict,
remoteNode->getIPAddress(),
remoteNode->getPort())));
@ -323,23 +326,24 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_values()
void DHTMessageFactoryImplTest::testCreateAnnouncePeerMessage()
{
try {
SharedHandle<Dictionary> d(new Dictionary());
d->put("t", new Data(transactionID, DHT_TRANSACTION_ID_LENGTH));
d->put("y", new Data("q"));
d->put("q", new Data("announce_peer"));
Dictionary* a = new Dictionary();
a->put("id", new Data(remoteNodeID, DHT_ID_LENGTH));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
dict["y"] = bencode::BDE("q");
dict["q"] = bencode::BDE("announce_peer");
bencode::BDE aDict = bencode::BDE::dict();
aDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
unsigned char infoHash[DHT_ID_LENGTH];
memset(infoHash, 0x11, DHT_ID_LENGTH);
a->put("info_hash", new Data(infoHash, DHT_ID_LENGTH));
aDict["info_hash"] = bencode::BDE(infoHash, DHT_ID_LENGTH);
std::string token = "ffff";
uint16_t port = 6881;
a->put("port", new Data(Util::uitos(port), true));
a->put("token", new Data(token));
d->put("a", a);
aDict["port"] = port;
aDict["token"] = token;
dict["a"] = aDict;
SharedHandle<DHTAnnouncePeerMessage> m
(dynamic_pointer_cast<DHTAnnouncePeerMessage>(factory->createQueryMessage(d.get(), "192.168.0.1", 6882)));
(dynamic_pointer_cast<DHTAnnouncePeerMessage>
(factory->createQueryMessage(dict, "192.168.0.1", 6882)));
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
remoteNode->setIPAddress("192.168.0.1");
remoteNode->setPort(6882);
@ -359,12 +363,12 @@ void DHTMessageFactoryImplTest::testCreateAnnouncePeerMessage()
void DHTMessageFactoryImplTest::testCreateAnnouncePeerReplyMessage()
{
SharedHandle<Dictionary> d(new Dictionary());
d->put("t", new Data(transactionID, DHT_TRANSACTION_ID_LENGTH));
d->put("y", new Data("r"));
Dictionary* r = new Dictionary();
r->put("id", new Data(remoteNodeID, DHT_ID_LENGTH));
d->put("r", r);
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
dict["y"] = bencode::BDE("r");
bencode::BDE rDict = bencode::BDE::dict();
rDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
dict["r"] = rDict;
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
remoteNode->setIPAddress("192.168.0.1");
@ -372,7 +376,7 @@ void DHTMessageFactoryImplTest::testCreateAnnouncePeerReplyMessage()
SharedHandle<DHTAnnouncePeerReplyMessage> m
(dynamic_pointer_cast<DHTAnnouncePeerReplyMessage>
(factory->createResponseMessage("announce_peer", d.get(),
(factory->createResponseMessage("announce_peer", dict,
remoteNode->getIPAddress(),
remoteNode->getPort())));
@ -384,20 +388,20 @@ void DHTMessageFactoryImplTest::testCreateAnnouncePeerReplyMessage()
void DHTMessageFactoryImplTest::testReceivedErrorMessage()
{
SharedHandle<Dictionary> d(new Dictionary());
d->put("t", new Data(transactionID, DHT_TRANSACTION_ID_LENGTH));
d->put("y", new Data("e"));
List* l = new List();
l->add(new Data("404"));
l->add(new Data("Not found"));
d->put("e", l);
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
dict["y"] = bencode::BDE("e");
bencode::BDE list = bencode::BDE::list();
list << 404;
list << bencode::BDE("Not found");
dict["e"] = list;
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
remoteNode->setIPAddress("192.168.0.1");
remoteNode->setPort(6881);
try {
factory->createResponseMessage("announce_peer", d.get(),
factory->createResponseMessage("announce_peer", dict,
remoteNode->getIPAddress(),
remoteNode->getPort());
CPPUNIT_FAIL("exception must be thrown.");

View file

@ -1,16 +1,16 @@
#include "DHTMessageTracker.h"
#include <cppunit/extensions/HelperMacros.h>
#include "Exception.h"
#include "Util.h"
#include "MockDHTMessage.h"
#include "MockDHTMessageCallback.h"
#include "DHTNode.h"
#include "MetaEntry.h"
#include "DHTMessageTrackerEntry.h"
#include "DHTRoutingTable.h"
#include "MockDHTMessageFactory.h"
#include "Dictionary.h"
#include "Data.h"
#include <cppunit/extensions/HelperMacros.h>
#include "bencode.h"
namespace aria2 {
@ -76,11 +76,12 @@ void DHTMessageTrackerTest::testMessageArrived()
tracker.addMessage(m3);
{
SharedHandle<Dictionary> res(new Dictionary());
res->put("t", new Data(m2->getTransactionID()));
bencode::BDE resDict = bencode::BDE::dict();
resDict["t"] = m2->getTransactionID();
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
tracker.messageArrived(res.get(), m2->getRemoteNode()->getIPAddress(), m2->getRemoteNode()->getPort());
tracker.messageArrived(resDict, m2->getRemoteNode()->getIPAddress(),
m2->getRemoteNode()->getPort());
SharedHandle<DHTMessage> reply = p.first;
CPPUNIT_ASSERT(!reply.isNull());
@ -89,10 +90,12 @@ void DHTMessageTrackerTest::testMessageArrived()
CPPUNIT_ASSERT_EQUAL((size_t)2, tracker.countEntry());
}
{
SharedHandle<Dictionary> res(new Dictionary());
res->put("t", new Data(m3->getTransactionID()));
bencode::BDE resDict = bencode::BDE::dict();
resDict["t"] = m3->getTransactionID();
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p = tracker.messageArrived(res.get(), m3->getRemoteNode()->getIPAddress(), m3->getRemoteNode()->getPort());
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
tracker.messageArrived(resDict, m3->getRemoteNode()->getIPAddress(),
m3->getRemoteNode()->getPort());
SharedHandle<DHTMessage> reply = p.first;
CPPUNIT_ASSERT(!reply.isNull());
@ -100,10 +103,11 @@ void DHTMessageTrackerTest::testMessageArrived()
CPPUNIT_ASSERT_EQUAL((size_t)1, tracker.countEntry());
}
{
SharedHandle<Dictionary> res(new Dictionary());
res->put("t", new Data(m1->getTransactionID()));
bencode::BDE resDict = bencode::BDE::dict();
resDict["t"] = m1->getTransactionID();
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p = tracker.messageArrived(res.get(), "192.168.1.100", 6889);
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
tracker.messageArrived(resDict, "192.168.1.100", 6889);
SharedHandle<DHTMessage> reply = p.first;
CPPUNIT_ASSERT(reply.isNull());

View file

@ -1,15 +1,15 @@
#include "DHTPingMessage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "DHTNode.h"
#include "DHTUtil.h"
#include "BencodeVisitor.h"
#include "Dictionary.h"
#include "Data.h"
#include "Exception.h"
#include "Util.h"
#include "MockDHTMessageFactory.h"
#include "MockDHTMessageDispatcher.h"
#include "MockDHTMessage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "bencode.h"
namespace aria2 {
@ -57,18 +57,15 @@ void DHTPingMessageTest::testGetBencodedMessage()
std::string msgbody = msg.getBencodedMessage();
SharedHandle<Dictionary> cm(new Dictionary());
cm->put("t", new Data(transactionID));
cm->put("y", new Data("q"));
cm->put("q", new Data("ping"));
Dictionary* a = new Dictionary();
cm->put("a", a);
a->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = transactionID;
dict["y"] = bencode::BDE("q");
dict["q"] = bencode::BDE("ping");
bencode::BDE aDict = bencode::BDE::dict();
aDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
dict["a"] = aDict;
BencodeVisitor v;
cm->accept(&v);
CPPUNIT_ASSERT_EQUAL(v.getBencodedData(), msgbody);
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
}
void DHTPingMessageTest::testDoReceivedAction()

View file

@ -1,12 +1,12 @@
#include "DHTPingReplyMessage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "DHTNode.h"
#include "DHTUtil.h"
#include "BencodeVisitor.h"
#include "Dictionary.h"
#include "Data.h"
#include "Exception.h"
#include "Util.h"
#include <cppunit/extensions/HelperMacros.h>
#include "bencode.h"
namespace aria2 {
@ -42,17 +42,14 @@ void DHTPingReplyMessageTest::testGetBencodedMessage()
std::string msgbody = msg.getBencodedMessage();
SharedHandle<Dictionary> cm(new Dictionary());
cm->put("t", new Data(transactionID));
cm->put("y", new Data("r"));
Dictionary* r = new Dictionary();
cm->put("r", r);
r->put("id", new Data(id, DHT_ID_LENGTH));
bencode::BDE dict = bencode::BDE::dict();
dict["t"] = transactionID;
dict["y"] = bencode::BDE("r");
bencode::BDE rDict = bencode::BDE::dict();
rDict["id"] = bencode::BDE(id, DHT_ID_LENGTH);
dict["r"] = rDict;
BencodeVisitor v;
cm->accept(&v);
CPPUNIT_ASSERT_EQUAL(v.getBencodedData(), msgbody);
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
}
} // namespace aria2

View file

@ -4,8 +4,7 @@
#include "DHTMessageFactory.h"
#include "DHTNode.h"
#include "MockDHTMessage.h"
#include "Dictionary.h"
#include "Data.h"
#include "bencode.h"
namespace aria2 {
@ -18,7 +17,7 @@ public:
virtual ~MockDHTMessageFactory() {}
virtual SharedHandle<DHTMessage>
createQueryMessage(const Dictionary* d,
createQueryMessage(const bencode::BDE& dict,
const std::string& ipaddr, uint16_t port)
{
return SharedHandle<DHTMessage>();
@ -26,7 +25,7 @@ public:
virtual SharedHandle<DHTMessage>
createResponseMessage(const std::string& messageType,
const Dictionary* d,
const bencode::BDE& dict,
const std::string& ipaddr, uint16_t port)
{
SharedHandle<DHTNode> remoteNode(new DHTNode());
@ -34,8 +33,7 @@ public:
remoteNode->setIPAddress(ipaddr);
remoteNode->setPort(port);
SharedHandle<MockDHTMessage> m
(new MockDHTMessage(_localNode, remoteNode,
reinterpret_cast<const Data*>(d->get("t"))->toString()));
(new MockDHTMessage(_localNode, remoteNode, dict["t"].s()));
m->setReply(true);
return m;
}