| 31 | CPPUNIT_TEST_SUITE_REGISTRATION(DHTFindNodeReplyMessageTest); |
| 32 | |
| 33 | void DHTFindNodeReplyMessageTest::testGetBencodedMessage() |
| 34 | { |
| 35 | std::shared_ptr<DHTNode> localNode(new DHTNode()); |
| 36 | std::shared_ptr<DHTNode> remoteNode(new DHTNode()); |
| 37 | |
| 38 | unsigned char tid[DHT_TRANSACTION_ID_LENGTH]; |
| 39 | util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH); |
| 40 | std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]); |
| 41 | |
| 42 | DHTFindNodeReplyMessage msg(AF_INET, localNode, remoteNode, transactionID); |
| 43 | msg.setVersion("A200"); |
| 44 | std::string compactNodeInfo; |
| 45 | std::shared_ptr<DHTNode> nodes[8]; |
| 46 | for (size_t i = 0; i < DHTBucket::K; ++i) { |
| 47 | nodes[i].reset(new DHTNode()); |
| 48 | nodes[i]->setIPAddress("192.168.0." + util::uitos(i + 1)); |
| 49 | nodes[i]->setPort(6881 + i); |
| 50 | |
| 51 | unsigned char buf[COMPACT_LEN_IPV6]; |
| 52 | CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV4, |
| 53 | bittorrent::packcompact(buf, nodes[i]->getIPAddress(), |
| 54 | nodes[i]->getPort())); |
| 55 | compactNodeInfo += |
| 56 | std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH]) + |
| 57 | std::string(&buf[0], &buf[COMPACT_LEN_IPV4]); |
| 58 | } |
| 59 | msg.setClosestKNodes( |
| 60 | std::vector<std::shared_ptr<DHTNode>>(&nodes[0], &nodes[DHTBucket::K])); |
| 61 | |
| 62 | std::string msgbody = msg.getBencodedMessage(); |
| 63 | |
| 64 | Dict dict; |
| 65 | dict.put("t", transactionID); |
| 66 | dict.put("v", "A200"); |
| 67 | dict.put("y", "r"); |
| 68 | auto rDict = Dict::g(); |
| 69 | rDict->put("id", String::g(localNode->getID(), DHT_ID_LENGTH)); |
| 70 | rDict->put("nodes", compactNodeInfo); |
| 71 | dict.put("r", std::move(rDict)); |
| 72 | |
| 73 | CPPUNIT_ASSERT_EQUAL(bencode2::encode(&dict), msgbody); |
| 74 | } |
| 75 | |
| 76 | void DHTFindNodeReplyMessageTest::testGetBencodedMessage6() |
| 77 | { |
nothing calls this directly
no test coverage detected