#include "DHTGetPeersMessage.h" #include #include "DHTNode.h" #include "Exception.h" #include "util.h" #include "MockDHTMessageFactory.h" #include "MockDHTMessage.h" #include "MockDHTMessageDispatcher.h" #include "DHTTokenTracker.h" #include "DHTPeerAnnounceStorage.h" #include "DHTRoutingTable.h" #include "bencode2.h" #include "GroupId.h" #include "DownloadContext.h" #include "Option.h" #include "RequestGroup.h" #include "BtRegistry.h" #include "TorrentAttribute.h" namespace aria2 { class DHTGetPeersMessageTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(DHTGetPeersMessageTest); CPPUNIT_TEST(testGetBencodedMessage); CPPUNIT_TEST(testDoReceivedAction); CPPUNIT_TEST_SUITE_END(); public: std::shared_ptr localNode_; std::shared_ptr remoteNode_; void setUp() { localNode_ = std::make_shared(); remoteNode_ = std::make_shared(); } void tearDown() {} void testGetBencodedMessage(); void testDoReceivedAction(); class MockDHTMessageFactory2 : public MockDHTMessageFactory { public: virtual std::unique_ptr createGetPeersReplyMessage( const std::shared_ptr& remoteNode, std::vector> closestKNodes, std::vector> peers, const std::string& token, const std::string& transactionID) CXX11_OVERRIDE { auto m = make_unique( AF_INET, localNode_, remoteNode, token, transactionID); m->setClosestKNodes(closestKNodes); m->setValues(peers); return m; } }; }; CPPUNIT_TEST_SUITE_REGISTRATION(DHTGetPeersMessageTest); void DHTGetPeersMessageTest::testGetBencodedMessage() { unsigned char tid[DHT_TRANSACTION_ID_LENGTH]; util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH); std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]); unsigned char infoHash[DHT_ID_LENGTH]; util::generateRandomData(infoHash, DHT_ID_LENGTH); DHTGetPeersMessage msg(localNode_, remoteNode_, infoHash, transactionID); msg.setVersion("A200"); std::string msgbody = msg.getBencodedMessage(); Dict dict; dict.put("t", transactionID); dict.put("v", "A200"); dict.put("y", "q"); dict.put("q", "get_peers"); auto aDict = Dict::g(); aDict->put("id", String::g(localNode_->getID(), DHT_ID_LENGTH)); aDict->put("info_hash", String::g(infoHash, DHT_ID_LENGTH)); dict.put("a", std::move(aDict)); CPPUNIT_ASSERT_EQUAL(util::percentEncode(bencode2::encode(&dict)), util::percentEncode(msgbody)); } void DHTGetPeersMessageTest::testDoReceivedAction() { remoteNode_->setIPAddress("192.168.0.1"); remoteNode_->setPort(6881); unsigned char tid[DHT_TRANSACTION_ID_LENGTH]; util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH); std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]); unsigned char infoHash[DHT_ID_LENGTH]; util::generateRandomData(infoHash, DHT_ID_LENGTH); DHTTokenTracker tokenTracker; MockDHTMessageDispatcher dispatcher; MockDHTMessageFactory2 factory; factory.setLocalNode(localNode_); DHTRoutingTable routingTable(localNode_); auto torrentAttrs = std::make_shared(); torrentAttrs->infoHash = std::string(infoHash, infoHash + DHT_ID_LENGTH); auto dctx = std::make_shared(); dctx->setAttribute(CTX_ATTR_BT, torrentAttrs); auto option = std::make_shared