aria2/test/DHTGetPeersReplyMessageTest.cc
Tatsuhiro Tsujikawa 9202fe23c8 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 05:46:45 +00:00

98 lines
2.8 KiB
C++

#include "DHTGetPeersReplyMessage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "DHTNode.h"
#include "DHTUtil.h"
#include "Exception.h"
#include "Util.h"
#include "DHTBucket.h"
#include "PeerMessageUtil.h"
#include "Peer.h"
#include "bencode.h"
namespace aria2 {
class DHTGetPeersReplyMessageTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DHTGetPeersReplyMessageTest);
CPPUNIT_TEST(testGetBencodedMessage);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
void testGetBencodedMessage();
};
CPPUNIT_TEST_SUITE_REGISTRATION(DHTGetPeersReplyMessageTest);
void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
{
SharedHandle<DHTNode> localNode(new DHTNode());
SharedHandle<DHTNode> remoteNode(new DHTNode());
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
std::string token = "token";
DHTGetPeersReplyMessage msg(localNode, remoteNode, token, transactionID);
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];
for(size_t i = 0; i < DHTBucket::K; ++i) {
nodes[i].reset(new DHTNode());
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
nodes[i]->setPort(6881+i);
unsigned char buf[6];
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]));
std::string msgbody = msg.getBencodedMessage();
rDict["nodes"] = compactNodeInfo;
CPPUNIT_ASSERT_EQUAL(Util::urlencode(bencode::encode(dict)),
Util::urlencode(msgbody));
}
rDict.removeKey("nodes");
{
std::deque<SharedHandle<Peer> > peers;
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));
valuesList << bencode::BDE(buffer, sizeof(buffer));
peers.push_back(peer);
}
rDict["values"] = valuesList;
msg.setValues(peers);
std::string msgbody = msg.getBencodedMessage();
CPPUNIT_ASSERT_EQUAL(Util::urlencode(bencode::encode(dict)),
Util::urlencode(msgbody));
}
}
} // namespace aria2