diff --git a/ChangeLog b/ChangeLog index bb5090a8..ef450c03 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,46 @@ +2008-12-14 Tatsuhiro Tsujikawa + + 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 Added tests the case where length < 0 is specified. diff --git a/src/DHTAbstractMessage.cc b/src/DHTAbstractMessage.cc index 3a25ef63..41c67f27 100644 --- a/src/DHTAbstractMessage.cc +++ b/src/DHTAbstractMessage.cc @@ -37,14 +37,12 @@ #include #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 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() diff --git a/src/DHTAbstractMessage.h b/src/DHTAbstractMessage.h index fc0203bb..ee7d4988 100644 --- a/src/DHTAbstractMessage.h +++ b/src/DHTAbstractMessage.h @@ -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(); diff --git a/src/DHTAnnouncePeerMessage.cc b/src/DHTAnnouncePeerMessage.cc index 21422c43..31d4d4ad 100644 --- a/src/DHTAnnouncePeerMessage.cc +++ b/src/DHTAnnouncePeerMessage.cc @@ -33,9 +33,10 @@ */ /* copyright --> */ #include "DHTAnnouncePeerMessage.h" + +#include + #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 +#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(_localNode->getID()), - DHT_ID_LENGTH)); - a->put(INFO_HASH, new Data(reinterpret_cast(_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 diff --git a/src/DHTAnnouncePeerMessage.h b/src/DHTAnnouncePeerMessage.h index ec00c6f1..8d38ea53 100644 --- a/src/DHTAnnouncePeerMessage.h +++ b/src/DHTAnnouncePeerMessage.h @@ -69,7 +69,7 @@ public: virtual void doReceivedAction(); - virtual Dictionary* getArgument(); + virtual bencode::BDE getArgument(); virtual std::string getMessageType() const; diff --git a/src/DHTAnnouncePeerReplyMessage.cc b/src/DHTAnnouncePeerReplyMessage.cc index edd99641..2e2b9b64 100644 --- a/src/DHTAnnouncePeerReplyMessage.cc +++ b/src/DHTAnnouncePeerReplyMessage.cc @@ -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 diff --git a/src/DHTAnnouncePeerReplyMessage.h b/src/DHTAnnouncePeerReplyMessage.h index 98e50682..967a3088 100644 --- a/src/DHTAnnouncePeerReplyMessage.h +++ b/src/DHTAnnouncePeerReplyMessage.h @@ -49,7 +49,7 @@ public: virtual void doReceivedAction(); - virtual Dictionary* getResponse(); + virtual bencode::BDE getResponse(); virtual std::string getMessageType() const; diff --git a/src/DHTFindNodeMessage.cc b/src/DHTFindNodeMessage.cc index f5b0ab2c..47a36667 100644 --- a/src/DHTFindNodeMessage.cc +++ b/src/DHTFindNodeMessage.cc @@ -33,15 +33,16 @@ */ /* copyright --> */ #include "DHTFindNodeMessage.h" + +#include + #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 +#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(_localNode->getID()), - DHT_ID_LENGTH)); - a->put(TARGET_NODE, new Data(reinterpret_cast(_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 diff --git a/src/DHTFindNodeMessage.h b/src/DHTFindNodeMessage.h index 95486ac8..11162787 100644 --- a/src/DHTFindNodeMessage.h +++ b/src/DHTFindNodeMessage.h @@ -55,7 +55,7 @@ public: virtual void doReceivedAction(); - virtual Dictionary* getArgument(); + virtual bencode::BDE getArgument(); virtual std::string getMessageType() const; diff --git a/src/DHTFindNodeReplyMessage.cc b/src/DHTFindNodeReplyMessage.cc index 47f06db1..0ae1d74c 100644 --- a/src/DHTFindNodeReplyMessage.cc +++ b/src/DHTFindNodeReplyMessage.cc @@ -33,17 +33,18 @@ */ /* copyright --> */ #include "DHTFindNodeReplyMessage.h" + +#include + #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 +#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 >::const_iterator i = _closestKNodes.begin(); i != _closestKNodes.end(); ++i) { + for(std::deque >::const_iterator i = + _closestKNodes.begin(); + i != _closestKNodes.end() && offset < DHTBucket::K*26; ++i) { SharedHandle 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 diff --git a/src/DHTFindNodeReplyMessage.h b/src/DHTFindNodeReplyMessage.h index c3ef632c..ab84d3ed 100644 --- a/src/DHTFindNodeReplyMessage.h +++ b/src/DHTFindNodeReplyMessage.h @@ -54,7 +54,7 @@ public: virtual void doReceivedAction(); - virtual Dictionary* getResponse(); + virtual bencode::BDE getResponse(); virtual std::string getMessageType() const; diff --git a/src/DHTGetPeersMessage.cc b/src/DHTGetPeersMessage.cc index 5a4a6291..6532dae5 100644 --- a/src/DHTGetPeersMessage.cc +++ b/src/DHTGetPeersMessage.cc @@ -33,9 +33,10 @@ */ /* copyright --> */ #include "DHTGetPeersMessage.h" + +#include + #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 +#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 diff --git a/src/DHTGetPeersMessage.h b/src/DHTGetPeersMessage.h index 20ed3474..237020e8 100644 --- a/src/DHTGetPeersMessage.h +++ b/src/DHTGetPeersMessage.h @@ -63,7 +63,7 @@ public: virtual void doReceivedAction(); - virtual Dictionary* getArgument(); + virtual bencode::BDE getArgument(); virtual std::string getMessageType() const; diff --git a/src/DHTGetPeersReplyMessage.cc b/src/DHTGetPeersReplyMessage.cc index 6c0fb423..a53aa56e 100644 --- a/src/DHTGetPeersReplyMessage.cc +++ b/src/DHTGetPeersReplyMessage.cc @@ -33,11 +33,11 @@ */ /* copyright --> */ #include "DHTGetPeersReplyMessage.h" + +#include + #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 +#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 >::const_iterator i = _values.begin(); i != _values.end(); ++i) { - const SharedHandle& 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 >::const_iterator i = _closestKNodes.begin(); i != _closestKNodes.end(); ++i) { + for(std::deque >::const_iterator i = + _closestKNodes.begin(); + i != _closestKNodes.end() && offset < DHTBucket::K*26; ++i) { SharedHandle 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 >::const_iterator i = _values.begin(); + i != _values.end(); ++i) { + const SharedHandle& 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 diff --git a/src/DHTGetPeersReplyMessage.h b/src/DHTGetPeersReplyMessage.h index 5b526546..de77d0d0 100644 --- a/src/DHTGetPeersReplyMessage.h +++ b/src/DHTGetPeersReplyMessage.h @@ -36,9 +36,11 @@ #define _D_DHT_GET_PEERS_REPLY_MESSAGE_H_ #include "DHTResponseMessage.h" -#include "DHTConstants.h" + #include +#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; diff --git a/src/DHTMessageFactory.h b/src/DHTMessageFactory.h index 157e7322..5db6bfe0 100644 --- a/src/DHTMessageFactory.h +++ b/src/DHTMessageFactory.h @@ -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 - createQueryMessage(const Dictionary* d, + createQueryMessage(const bencode::BDE& dict, const std::string& ipaddr, uint16_t port) = 0; virtual SharedHandle createResponseMessage(const std::string& messageType, - const Dictionary* d, + const bencode::BDE& dict, const std::string& ipaddr, uint16_t port) = 0; virtual SharedHandle diff --git a/src/DHTMessageFactoryImpl.cc b/src/DHTMessageFactoryImpl.cc index f1985335..80845a43 100644 --- a/src/DHTMessageFactoryImpl.cc +++ b/src/DHTMessageFactoryImpl.cc @@ -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(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(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(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(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 DHTMessageFactoryImpl::createQueryMessage(const Dictionary* d, - const std::string& ipaddr, - uint16_t port) +SharedHandle 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 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 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(port->toInt()), - token->toString(), transactionID); + const bencode::BDE& token = getString(aDict, DHTAnnouncePeerMessage::TOKEN); + return createAnnouncePeerMessage(remoteNode, infoHash.uc(), + static_cast(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 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 remoteNode = getRemoteNode(id->getData(), ipaddr, port); + SharedHandle 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(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(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 -DHTMessageFactoryImpl::createFindNodeReplyMessage(const SharedHandle& remoteNode, - const Dictionary* d, - const std::string& transactionID) +DHTMessageFactoryImpl::createFindNodeReplyMessage +(const SharedHandle& remoteNode, + const bencode::BDE& dict, + const std::string& transactionID) { - const Data* nodesData = getData(getDictionary(d, DHTResponseMessage::R), DHTFindNodeReplyMessage::NODES); - std::deque > nodes = extractNodes(nodesData->getData(), nodesData->getLen()); + const bencode::BDE& nodesData = + getString(getDictionary(dict, DHTResponseMessage::R), + DHTFindNodeReplyMessage::NODES); + std::deque > nodes = extractNodes(nodesData.uc(), + nodesData.s().size()); return createFindNodeReplyMessage(remoteNode, nodes, transactionID); } @@ -344,15 +373,18 @@ DHTMessageFactoryImpl::createGetPeersMessage(const SharedHandle& remote } SharedHandle -DHTMessageFactoryImpl::createGetPeersReplyMessageWithNodes(const SharedHandle& remoteNode, - const Dictionary* d, - const std::string& transactionID) +DHTMessageFactoryImpl::createGetPeersReplyMessageWithNodes +(const SharedHandle& 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 > 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 > 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& r } SharedHandle -DHTMessageFactoryImpl::createGetPeersReplyMessageWithValues(const SharedHandle& remoteNode, - const Dictionary* d, - const std::string& transactionID) +DHTMessageFactoryImpl::createGetPeersReplyMessageWithValues +(const SharedHandle& 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 > peers; - for(std::deque::const_iterator i = valuesList->getList().begin(); i != valuesList->getList().end(); ++i) { - const Data* data = dynamic_cast(*i); - if(data && data->getLen() == 6) { - std::pair 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 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); } diff --git a/src/DHTMessageFactoryImpl.h b/src/DHTMessageFactoryImpl.h index 57986c8b..4e666a26 100644 --- a/src/DHTMessageFactoryImpl.h +++ b/src/DHTMessageFactoryImpl.h @@ -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 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 > extractNodes(const unsigned char* src, size_t length); @@ -83,12 +82,12 @@ public: virtual ~DHTMessageFactoryImpl(); virtual SharedHandle - createQueryMessage(const Dictionary* d, + createQueryMessage(const bencode::BDE& dict, const std::string& ipaddr, uint16_t port); virtual SharedHandle createResponseMessage(const std::string& messageType, - const Dictionary* d, + const bencode::BDE& dict, const std::string& ipaddr, uint16_t port); virtual SharedHandle @@ -107,7 +106,7 @@ public: SharedHandle createFindNodeReplyMessage(const SharedHandle& remoteNode, - const Dictionary* d, + const bencode::BDE& dict, const std::string& transactionID); @@ -129,7 +128,7 @@ public: SharedHandle createGetPeersReplyMessageWithNodes(const SharedHandle& remoteNode, - const Dictionary* d, + const bencode::BDE& dict, const std::string& transactionID); virtual SharedHandle @@ -140,7 +139,7 @@ public: SharedHandle createGetPeersReplyMessageWithValues(const SharedHandle& remoteNode, - const Dictionary* d, + const bencode::BDE& dict, const std::string& transactionID); virtual SharedHandle diff --git a/src/DHTMessageReceiver.cc b/src/DHTMessageReceiver.cc index ad624127..15669a0a 100644 --- a/src/DHTMessageReceiver.cc +++ b/src/DHTMessageReceiver.cc @@ -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 DHTMessageReceiver::receiveMessage() } try { bool isReply = false; - MetaEntryHandle msgroot(MetaFileUtil::bdecoding(data, length)); - const Dictionary* d = dynamic_cast(msgroot.get()); - if(d) { - const Data* y = dynamic_cast(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 message; SharedHandle callback; if(isReply) { - std::pair, SharedHandle > p = _tracker->messageArrived(d, remoteAddr, remotePort); + std::pair, SharedHandle > p = + _tracker->messageArrived(dict, remoteAddr, remotePort); message = p.first; callback = p.second; if(message.isNull()) { @@ -104,7 +103,7 @@ SharedHandle 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."); diff --git a/src/DHTMessageTracker.cc b/src/DHTMessageTracker.cc index 3d0d5db5..a64e5a9b 100644 --- a/src/DHTMessageTracker.cc +++ b/src/DHTMessageTracker.cc @@ -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& message, cons } std::pair, SharedHandle > -DHTMessageTracker::messageArrived(const Dictionary* d, +DHTMessageTracker::messageArrived(const bencode::BDE& dict, const std::string& ipaddr, uint16_t port) { - const Data* tid = dynamic_cast(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 >::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 >::iterator i = + _entries.begin(); i != _entries.end(); ++i) { + if((*i)->match(tid.s(), ipaddr, port)) { SharedHandle entry = *i; _entries.erase(i); _logger->debug("Tracker entry found."); SharedHandle targetNode = entry->getTargetNode(); SharedHandle message = - _factory->createResponseMessage(entry->getMessageType(), d, + _factory->createResponseMessage(entry->getMessageType(), dict, targetNode->getIPAddress(), targetNode->getPort()); diff --git a/src/DHTMessageTracker.h b/src/DHTMessageTracker.h index 890bcd5c..218d988b 100644 --- a/src/DHTMessageTracker.h +++ b/src/DHTMessageTracker.h @@ -36,11 +36,13 @@ #define _D_DHT_MESSAGE_TRACKER_H_ #include "common.h" -#include "SharedHandle.h" -#include "a2time.h" + #include #include +#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()); std::pair, SharedHandle > - 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(); diff --git a/src/DHTPingMessage.cc b/src/DHTPingMessage.cc index 9e396625..c68324a3 100644 --- a/src/DHTPingMessage.cc +++ b/src/DHTPingMessage.cc @@ -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(_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 diff --git a/src/DHTPingMessage.h b/src/DHTPingMessage.h index 8b458cbc..4479555d 100644 --- a/src/DHTPingMessage.h +++ b/src/DHTPingMessage.h @@ -50,7 +50,7 @@ public: virtual void doReceivedAction(); - virtual Dictionary* getArgument(); + virtual bencode::BDE getArgument(); virtual std::string getMessageType() const; diff --git a/src/DHTPingReplyMessage.cc b/src/DHTPingReplyMessage.cc index b4c75984..2d04364b 100644 --- a/src/DHTPingReplyMessage.cc +++ b/src/DHTPingReplyMessage.cc @@ -33,11 +33,12 @@ */ /* copyright --> */ #include "DHTPingReplyMessage.h" -#include "DHTNode.h" -#include "Dictionary.h" -#include "Data.h" + #include +#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 diff --git a/src/DHTPingReplyMessage.h b/src/DHTPingReplyMessage.h index 5c4130cf..e0c41de5 100644 --- a/src/DHTPingReplyMessage.h +++ b/src/DHTPingReplyMessage.h @@ -53,7 +53,7 @@ public: virtual void doReceivedAction(); - virtual Dictionary* getResponse(); + virtual bencode::BDE getResponse(); virtual std::string getMessageType() const; diff --git a/src/DHTQueryMessage.cc b/src/DHTQueryMessage.cc index 895a2dc5..dc891e59 100644 --- a/src/DHTQueryMessage.cc +++ b/src/DHTQueryMessage.cc @@ -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 diff --git a/src/DHTQueryMessage.h b/src/DHTQueryMessage.h index dc9cb22f..955bb9e8 100644 --- a/src/DHTQueryMessage.h +++ b/src/DHTQueryMessage.h @@ -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; diff --git a/src/DHTResponseMessage.cc b/src/DHTResponseMessage.cc index b9cd374b..6cb03037 100644 --- a/src/DHTResponseMessage.cc +++ b/src/DHTResponseMessage.cc @@ -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 diff --git a/src/DHTResponseMessage.h b/src/DHTResponseMessage.h index 8a2ecc79..1ab80a95 100644 --- a/src/DHTResponseMessage.h +++ b/src/DHTResponseMessage.h @@ -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; diff --git a/test/DHTAnnouncePeerMessageTest.cc b/test/DHTAnnouncePeerMessageTest.cc index eddd3d2c..d0ebf705 100644 --- a/test/DHTAnnouncePeerMessageTest.cc +++ b/test/DHTAnnouncePeerMessageTest.cc @@ -1,16 +1,16 @@ #include "DHTAnnouncePeerMessage.h" + +#include + #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 +#include "bencode.h" namespace aria2 { @@ -60,21 +60,18 @@ void DHTAnnouncePeerMessageTest::testGetBencodedMessage() std::string msgbody = msg.getBencodedMessage(); - SharedHandle 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)); } diff --git a/test/DHTAnnouncePeerReplyMessageTest.cc b/test/DHTAnnouncePeerReplyMessageTest.cc index 8ccfac51..c73612f6 100644 --- a/test/DHTAnnouncePeerReplyMessageTest.cc +++ b/test/DHTAnnouncePeerReplyMessageTest.cc @@ -1,12 +1,12 @@ #include "DHTAnnouncePeerReplyMessage.h" + +#include + #include "DHTNode.h" #include "DHTUtil.h" -#include "BencodeVisitor.h" -#include "Dictionary.h" -#include "Data.h" #include "Exception.h" #include "Util.h" -#include +#include "bencode.h" namespace aria2 { @@ -39,17 +39,14 @@ void DHTAnnouncePeerReplyMessageTest::testGetBencodedMessage() std::string msgbody = msg.getBencodedMessage(); - SharedHandle 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 diff --git a/test/DHTFindNodeMessageTest.cc b/test/DHTFindNodeMessageTest.cc index b544fa6c..d26a0af9 100644 --- a/test/DHTFindNodeMessageTest.cc +++ b/test/DHTFindNodeMessageTest.cc @@ -1,16 +1,16 @@ #include "DHTFindNodeMessage.h" + +#include + #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 +#include "bencode.h" namespace aria2 { @@ -61,19 +61,16 @@ void DHTFindNodeMessageTest::testGetBencodedMessage() std::string msgbody = msg.getBencodedMessage(); - SharedHandle 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() diff --git a/test/DHTFindNodeReplyMessageTest.cc b/test/DHTFindNodeReplyMessageTest.cc index cab09ad1..d4f3290e 100644 --- a/test/DHTFindNodeReplyMessageTest.cc +++ b/test/DHTFindNodeReplyMessageTest.cc @@ -1,14 +1,14 @@ #include "DHTFindNodeReplyMessage.h" + +#include + #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 +#include "bencode.h" namespace aria2 { @@ -56,18 +56,15 @@ void DHTFindNodeReplyMessageTest::testGetBencodedMessage() std::string msgbody = msg.getBencodedMessage(); - SharedHandle 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 diff --git a/test/DHTGetPeersMessageTest.cc b/test/DHTGetPeersMessageTest.cc index 8b514d7b..003183b4 100644 --- a/test/DHTGetPeersMessageTest.cc +++ b/test/DHTGetPeersMessageTest.cc @@ -1,9 +1,9 @@ #include "DHTGetPeersMessage.h" + +#include + #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 +#include "bencode.h" namespace aria2 { @@ -80,19 +80,16 @@ void DHTGetPeersMessageTest::testGetBencodedMessage() std::string msgbody = msg.getBencodedMessage(); - SharedHandle 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)); } diff --git a/test/DHTGetPeersReplyMessageTest.cc b/test/DHTGetPeersReplyMessageTest.cc index 89fd2def..f43e8ae0 100644 --- a/test/DHTGetPeersReplyMessageTest.cc +++ b/test/DHTGetPeersReplyMessageTest.cc @@ -1,16 +1,15 @@ #include "DHTGetPeersReplyMessage.h" + +#include + #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 +#include "bencode.h" namespace aria2 { @@ -43,14 +42,13 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage() DHTGetPeersReplyMessage msg(localNode, remoteNode, token, transactionID); - SharedHandle 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 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 >(&nodes[0], &nodes[DHTBucket::K])); + msg.setClosestKNodes + (std::deque >(&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 > 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(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)); } } diff --git a/test/DHTMessageFactoryImplTest.cc b/test/DHTMessageFactoryImplTest.cc index ca62b239..72d535f9 100644 --- a/test/DHTMessageFactoryImplTest.cc +++ b/test/DHTMessageFactoryImplTest.cc @@ -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 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 m - (dynamic_pointer_cast(factory->createQueryMessage(d.get(), "192.168.0.1", 6881))); + (dynamic_pointer_cast + (factory->createQueryMessage(dict, "192.168.0.1", 6881))); SharedHandle remoteNode(new DHTNode(remoteNodeID)); remoteNode->setIPAddress("192.168.0.1"); remoteNode->setPort(6881); @@ -103,12 +102,12 @@ void DHTMessageFactoryImplTest::testCreatePingMessage() void DHTMessageFactoryImplTest::testCreatePingReplyMessage() { - SharedHandle 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 remoteNode(new DHTNode(remoteNodeID)); remoteNode->setIPAddress("192.168.0.1"); @@ -116,7 +115,7 @@ void DHTMessageFactoryImplTest::testCreatePingReplyMessage() SharedHandle m (dynamic_pointer_cast - (factory->createResponseMessage("ping", d.get(), + (factory->createResponseMessage("ping", dict, remoteNode->getIPAddress(), remoteNode->getPort()))); @@ -128,19 +127,20 @@ void DHTMessageFactoryImplTest::testCreatePingReplyMessage() void DHTMessageFactoryImplTest::testCreateFindNodeMessage() { - SharedHandle 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 m - (dynamic_pointer_cast(factory->createQueryMessage(d.get(), "192.168.0.1", 6881))); + (dynamic_pointer_cast + (factory->createQueryMessage(dict, "192.168.0.1", 6881))); SharedHandle 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 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 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 remoteNode(new DHTNode(remoteNodeID)); remoteNode->setIPAddress("192.168.0.1"); @@ -183,7 +184,7 @@ void DHTMessageFactoryImplTest::testCreateFindNodeReplyMessage() SharedHandle m (dynamic_pointer_cast - (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 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 m - (dynamic_pointer_cast(factory->createQueryMessage(d.get(), "192.168.0.1", 6881))); + (dynamic_pointer_cast + (factory->createQueryMessage(dict, "192.168.0.1", 6881))); SharedHandle 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 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 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 remoteNode(new DHTNode(remoteNodeID)); remoteNode->setIPAddress("192.168.0.1"); @@ -257,7 +260,7 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_nodes() SharedHandle m (dynamic_pointer_cast - (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 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 > 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(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 remoteNode(new DHTNode(remoteNodeID)); remoteNode->setIPAddress("192.168.0.1"); @@ -303,7 +306,7 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_values() SharedHandle m (dynamic_pointer_cast - (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 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 m - (dynamic_pointer_cast(factory->createQueryMessage(d.get(), "192.168.0.1", 6882))); + (dynamic_pointer_cast + (factory->createQueryMessage(dict, "192.168.0.1", 6882))); SharedHandle remoteNode(new DHTNode(remoteNodeID)); remoteNode->setIPAddress("192.168.0.1"); remoteNode->setPort(6882); @@ -359,12 +363,12 @@ void DHTMessageFactoryImplTest::testCreateAnnouncePeerMessage() void DHTMessageFactoryImplTest::testCreateAnnouncePeerReplyMessage() { - SharedHandle 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 remoteNode(new DHTNode(remoteNodeID)); remoteNode->setIPAddress("192.168.0.1"); @@ -372,7 +376,7 @@ void DHTMessageFactoryImplTest::testCreateAnnouncePeerReplyMessage() SharedHandle m (dynamic_pointer_cast - (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 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 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."); diff --git a/test/DHTMessageTrackerTest.cc b/test/DHTMessageTrackerTest.cc index 9a0a144a..5866a8bd 100644 --- a/test/DHTMessageTrackerTest.cc +++ b/test/DHTMessageTrackerTest.cc @@ -1,16 +1,16 @@ #include "DHTMessageTracker.h" + +#include + #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 +#include "bencode.h" namespace aria2 { @@ -76,11 +76,12 @@ void DHTMessageTrackerTest::testMessageArrived() tracker.addMessage(m3); { - SharedHandle res(new Dictionary()); - res->put("t", new Data(m2->getTransactionID())); + bencode::BDE resDict = bencode::BDE::dict(); + resDict["t"] = m2->getTransactionID(); std::pair, SharedHandle > p = - tracker.messageArrived(res.get(), m2->getRemoteNode()->getIPAddress(), m2->getRemoteNode()->getPort()); + tracker.messageArrived(resDict, m2->getRemoteNode()->getIPAddress(), + m2->getRemoteNode()->getPort()); SharedHandle reply = p.first; CPPUNIT_ASSERT(!reply.isNull()); @@ -89,10 +90,12 @@ void DHTMessageTrackerTest::testMessageArrived() CPPUNIT_ASSERT_EQUAL((size_t)2, tracker.countEntry()); } { - SharedHandle res(new Dictionary()); - res->put("t", new Data(m3->getTransactionID())); + bencode::BDE resDict = bencode::BDE::dict(); + resDict["t"] = m3->getTransactionID(); - std::pair, SharedHandle > p = tracker.messageArrived(res.get(), m3->getRemoteNode()->getIPAddress(), m3->getRemoteNode()->getPort()); + std::pair, SharedHandle > p = + tracker.messageArrived(resDict, m3->getRemoteNode()->getIPAddress(), + m3->getRemoteNode()->getPort()); SharedHandle reply = p.first; CPPUNIT_ASSERT(!reply.isNull()); @@ -100,10 +103,11 @@ void DHTMessageTrackerTest::testMessageArrived() CPPUNIT_ASSERT_EQUAL((size_t)1, tracker.countEntry()); } { - SharedHandle res(new Dictionary()); - res->put("t", new Data(m1->getTransactionID())); + bencode::BDE resDict = bencode::BDE::dict(); + resDict["t"] = m1->getTransactionID(); - std::pair, SharedHandle > p = tracker.messageArrived(res.get(), "192.168.1.100", 6889); + std::pair, SharedHandle > p = + tracker.messageArrived(resDict, "192.168.1.100", 6889); SharedHandle reply = p.first; CPPUNIT_ASSERT(reply.isNull()); diff --git a/test/DHTPingMessageTest.cc b/test/DHTPingMessageTest.cc index d931c3b7..aa1c621c 100644 --- a/test/DHTPingMessageTest.cc +++ b/test/DHTPingMessageTest.cc @@ -1,15 +1,15 @@ #include "DHTPingMessage.h" + +#include + #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 +#include "bencode.h" namespace aria2 { @@ -57,18 +57,15 @@ void DHTPingMessageTest::testGetBencodedMessage() std::string msgbody = msg.getBencodedMessage(); - SharedHandle 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() diff --git a/test/DHTPingReplyMessageTest.cc b/test/DHTPingReplyMessageTest.cc index b39fa1c3..2e65cebd 100644 --- a/test/DHTPingReplyMessageTest.cc +++ b/test/DHTPingReplyMessageTest.cc @@ -1,12 +1,12 @@ #include "DHTPingReplyMessage.h" + +#include + #include "DHTNode.h" #include "DHTUtil.h" -#include "BencodeVisitor.h" -#include "Dictionary.h" -#include "Data.h" #include "Exception.h" #include "Util.h" -#include +#include "bencode.h" namespace aria2 { @@ -42,17 +42,14 @@ void DHTPingReplyMessageTest::testGetBencodedMessage() std::string msgbody = msg.getBencodedMessage(); - SharedHandle 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 diff --git a/test/MockDHTMessageFactory.h b/test/MockDHTMessageFactory.h index 8cfcacf1..807bc354 100644 --- a/test/MockDHTMessageFactory.h +++ b/test/MockDHTMessageFactory.h @@ -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 - createQueryMessage(const Dictionary* d, + createQueryMessage(const bencode::BDE& dict, const std::string& ipaddr, uint16_t port) { return SharedHandle(); @@ -26,7 +25,7 @@ public: virtual SharedHandle createResponseMessage(const std::string& messageType, - const Dictionary* d, + const bencode::BDE& dict, const std::string& ipaddr, uint16_t port) { SharedHandle remoteNode(new DHTNode()); @@ -34,8 +33,7 @@ public: remoteNode->setIPAddress(ipaddr); remoteNode->setPort(port); SharedHandle m - (new MockDHTMessage(_localNode, remoteNode, - reinterpret_cast(d->get("t"))->toString())); + (new MockDHTMessage(_localNode, remoteNode, dict["t"].s())); m->setReply(true); return m; }