MCPcopy Create free account
hub / github.com/aria2/aria2 / testCreateGetPeersReplyMessage

Method testCreateGetPeersReplyMessage

test/DHTMessageFactoryImplTest.cc:270–334  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

268}
269
270void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage()
271{
272 try {
273 Dict dict;
274 dict.put("t", String::g(transactionID, DHT_TRANSACTION_ID_LENGTH));
275 dict.put("y", "r");
276 auto rDict = Dict::g();
277 rDict->put("id", String::g(remoteNodeID, DHT_ID_LENGTH));
278 std::string compactNodeInfo;
279 std::shared_ptr<DHTNode> nodes[8];
280 for (size_t i = 0; i < DHTBucket::K; ++i) {
281 nodes[i] = std::make_shared<DHTNode>();
282 nodes[i]->setIPAddress("192.168.0." + util::uitos(i + 1));
283 nodes[i]->setPort(6881 + i);
284
285 unsigned char buf[COMPACT_LEN_IPV6];
286 CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV4, bittorrent::packcompact(
287 buf, nodes[i]->getIPAddress(),
288 nodes[i]->getPort()));
289 compactNodeInfo += std::string(&nodes[i]->getID()[0],
290 &nodes[i]->getID()[DHT_ID_LENGTH]) +
291 std::string(&buf[0], &buf[COMPACT_LEN_IPV4]);
292 }
293 rDict->put("nodes", compactNodeInfo);
294
295 std::deque<std::shared_ptr<Peer>> peers;
296 auto valuesList = List::g();
297 for (size_t i = 0; i < 4; ++i) {
298 auto peer =
299 std::make_shared<Peer>("192.168.0." + util::uitos(i + 1), 6881 + i);
300 unsigned char buffer[COMPACT_LEN_IPV6];
301 CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV4,
302 bittorrent::packcompact(buffer, peer->getIPAddress(),
303 peer->getPort()));
304 valuesList->append(String::g(buffer, COMPACT_LEN_IPV4));
305 peers.push_back(peer);
306 }
307 rDict->put("values", std::move(valuesList));
308 rDict->put("token", "token");
309 dict.put("r", std::move(rDict));
310
311 auto r = factory->createResponseMessage("get_peers", &dict,
312 remoteNode_->getIPAddress(),
313 remoteNode_->getPort());
314 auto m = dynamic_cast<DHTGetPeersReplyMessage*>(r.get());
315
316 CPPUNIT_ASSERT(*localNode == *m->getLocalNode());
317 CPPUNIT_ASSERT(*remoteNode_ == *m->getRemoteNode());
318 CPPUNIT_ASSERT_EQUAL(std::string("token"), m->getToken());
319 CPPUNIT_ASSERT_EQUAL((size_t)DHTBucket::K, m->getClosestKNodes().size());
320 CPPUNIT_ASSERT(*nodes[0] == *m->getClosestKNodes()[0]);
321 CPPUNIT_ASSERT(*nodes[7] == *m->getClosestKNodes()[7]);
322 CPPUNIT_ASSERT_EQUAL((size_t)4, m->getValues().size());
323 for (int i = 0; i < 4; ++i) {
324 CPPUNIT_ASSERT_EQUAL(peers[i]->getIPAddress(),
325 m->getValues()[i]->getIPAddress());
326 CPPUNIT_ASSERT_EQUAL(peers[i]->getPort(), m->getValues()[i]->getPort());
327 }

Callers

nothing calls this directly

Calls 15

uitosFunction · 0.85
packcompactFunction · 0.85
toHexFunction · 0.85
setIPAddressMethod · 0.80
getIDMethod · 0.80
push_backMethod · 0.80
getRemoteNodeMethod · 0.80
getClosestKNodesMethod · 0.80
stackTraceMethod · 0.80
putMethod · 0.45
setPortMethod · 0.45
getPortMethod · 0.45

Tested by

no test coverage detected