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

Method testGetBencodedMessage

test/DHTGetPeersReplyMessageTest.cc:34–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32CPPUNIT_TEST_SUITE_REGISTRATION(DHTGetPeersReplyMessageTest);
33
34void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
35{
36 std::shared_ptr<DHTNode> localNode(new DHTNode());
37 std::shared_ptr<DHTNode> remoteNode(new DHTNode());
38
39 unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
40 util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
41 std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
42
43 std::string token = "token";
44
45 DHTGetPeersReplyMessage msg(AF_INET, localNode, remoteNode, token,
46 transactionID);
47 msg.setVersion("A200");
48 Dict dict;
49 dict.put("t", transactionID);
50 dict.put("v", "A200");
51 dict.put("y", "r");
52 auto rDict = Dict::g();
53 rDict->put("id", String::g(localNode->getID(), DHT_ID_LENGTH));
54 rDict->put("token", token);
55 {
56 std::string compactNodeInfo;
57 std::shared_ptr<DHTNode> nodes[8];
58 for (size_t i = 0; i < DHTBucket::K; ++i) {
59 nodes[i].reset(new DHTNode());
60 nodes[i]->setIPAddress("192.168.0." + util::uitos(i + 1));
61 nodes[i]->setPort(6881 + i);
62
63 unsigned char buf[COMPACT_LEN_IPV6];
64 CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV4, bittorrent::packcompact(
65 buf, nodes[i]->getIPAddress(),
66 nodes[i]->getPort()));
67 compactNodeInfo += std::string(&nodes[i]->getID()[0],
68 &nodes[i]->getID()[DHT_ID_LENGTH]) +
69 std::string(&buf[0], &buf[COMPACT_LEN_IPV4]);
70 }
71 msg.setClosestKNodes(
72 std::vector<std::shared_ptr<DHTNode>>(&nodes[0], &nodes[DHTBucket::K]));
73 rDict->put("nodes", compactNodeInfo);
74
75 std::vector<std::shared_ptr<Peer>> peers;
76 auto valuesList = List::g();
77 for (size_t i = 0; i < 4; ++i) {
78 auto peer =
79 std::make_shared<Peer>("192.168.0." + util::uitos(i + 1), 6881 + i);
80 unsigned char buffer[COMPACT_LEN_IPV6];
81 CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV4,
82 bittorrent::packcompact(buffer, peer->getIPAddress(),
83 peer->getPort()));
84 valuesList->append(String::g(buffer, COMPACT_LEN_IPV4));
85 peers.push_back(peer);
86 }
87 msg.setValues(peers);
88 rDict->put("values", std::move(valuesList));
89 dict.put("r", std::move(rDict));
90
91 std::string msgbody = msg.getBencodedMessage();

Callers

nothing calls this directly

Calls 15

generateRandomDataFunction · 0.85
uitosFunction · 0.85
packcompactFunction · 0.85
percentEncodeFunction · 0.85
getIDMethod · 0.80
setIPAddressMethod · 0.80
push_backMethod · 0.80
setValuesMethod · 0.80
getBencodedMessageMethod · 0.80
encodeFunction · 0.50
setVersionMethod · 0.45
putMethod · 0.45

Tested by

no test coverage detected