DHTFindNodeReplyMessageTest.cc 3.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "DHTFindNodeReplyMessage.h"

#include <cppunit/extensions/HelperMacros.h>

#include "DHTNode.h"
#include "Exception.h"
#include "util.h"
#include "DHTBucket.h"
#include "bittorrent_helper.h"
#include "bencode2.h"

namespace aria2 {

14
class DHTFindNodeReplyMessageTest : public CppUnit::TestFixture {
15 16 17 18 19

  CPPUNIT_TEST_SUITE(DHTFindNodeReplyMessageTest);
  CPPUNIT_TEST(testGetBencodedMessage);
  CPPUNIT_TEST(testGetBencodedMessage6);
  CPPUNIT_TEST_SUITE_END();
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34
public:
  void setUp() {}

  void tearDown() {}

  void testGetBencodedMessage();

  void testGetBencodedMessage6();
};

CPPUNIT_TEST_SUITE_REGISTRATION(DHTFindNodeReplyMessageTest);

void DHTFindNodeReplyMessageTest::testGetBencodedMessage()
{
35 36
  std::shared_ptr<DHTNode> localNode(new DHTNode());
  std::shared_ptr<DHTNode> remoteNode(new DHTNode());
37 38 39 40 41 42 43 44

  unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
  util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
  std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);

  DHTFindNodeReplyMessage msg(AF_INET, localNode, remoteNode, transactionID);
  msg.setVersion("A200");
  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 49
    nodes[i]->setIPAddress("192.168.0." + util::uitos(i + 1));
    nodes[i]->setPort(6881 + i);
50 51

    unsigned char buf[COMPACT_LEN_IPV6];
52 53 54
    CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV4,
                         bittorrent::packcompact(buf, nodes[i]->getIPAddress(),
                                                 nodes[i]->getPort()));
55
    compactNodeInfo +=
56 57
        std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH]) +
        std::string(&buf[0], &buf[COMPACT_LEN_IPV4]);
58
  }
59 60
  msg.setClosestKNodes(
      std::vector<std::shared_ptr<DHTNode>>(&nodes[0], &nodes[DHTBucket::K]));
61 62 63 64 65 66 67

  std::string msgbody = msg.getBencodedMessage();

  Dict dict;
  dict.put("t", transactionID);
  dict.put("v", "A200");
  dict.put("y", "r");
68
  auto rDict = Dict::g();
69 70
  rDict->put("id", String::g(localNode->getID(), DHT_ID_LENGTH));
  rDict->put("nodes", compactNodeInfo);
71
  dict.put("r", std::move(rDict));
72 73 74 75 76 77

  CPPUNIT_ASSERT_EQUAL(bencode2::encode(&dict), msgbody);
}

void DHTFindNodeReplyMessageTest::testGetBencodedMessage6()
{
78 79
  std::shared_ptr<DHTNode> localNode(new DHTNode());
  std::shared_ptr<DHTNode> remoteNode(new DHTNode());
80 81 82 83 84 85 86 87

  unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
  util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
  std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);

  DHTFindNodeReplyMessage msg(AF_INET6, localNode, remoteNode, transactionID);
  msg.setVersion("A200");
  std::string compactNodeInfo;
88
  std::shared_ptr<DHTNode> nodes[8];
89
  for (size_t i = 0; i < DHTBucket::K; ++i) {
90
    nodes[i].reset(new DHTNode());
91 92
    nodes[i]->setIPAddress("2001::000" + util::uitos(i + 1));
    nodes[i]->setPort(6881 + i);
93 94

    unsigned char buf[COMPACT_LEN_IPV6];
95 96 97
    CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV6,
                         bittorrent::packcompact(buf, nodes[i]->getIPAddress(),
                                                 nodes[i]->getPort()));
98
    compactNodeInfo +=
99 100
        std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH]) +
        std::string(&buf[0], &buf[COMPACT_LEN_IPV6]);
101
  }
102 103
  msg.setClosestKNodes(
      std::vector<std::shared_ptr<DHTNode>>(&nodes[0], &nodes[DHTBucket::K]));
104 105 106 107 108 109 110

  std::string msgbody = msg.getBencodedMessage();

  Dict dict;
  dict.put("t", transactionID);
  dict.put("v", "A200");
  dict.put("y", "r");
111
  auto rDict = Dict::g();
112 113
  rDict->put("id", String::g(localNode->getID(), DHT_ID_LENGTH));
  rDict->put("nodes6", compactNodeInfo);
114
  dict.put("r", std::move(rDict));
115 116 117 118 119

  CPPUNIT_ASSERT_EQUAL(bencode2::encode(&dict), msgbody);
}

} // namespace aria2