DHTFindNodeReplyMessageTest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "DHTFindNodeReplyMessage.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "DHTNode.h"
  4. #include "Exception.h"
  5. #include "util.h"
  6. #include "DHTBucket.h"
  7. #include "bittorrent_helper.h"
  8. #include "bencode2.h"
  9. namespace aria2 {
  10. class DHTFindNodeReplyMessageTest : public CppUnit::TestFixture {
  11. CPPUNIT_TEST_SUITE(DHTFindNodeReplyMessageTest);
  12. CPPUNIT_TEST(testGetBencodedMessage);
  13. CPPUNIT_TEST(testGetBencodedMessage6);
  14. CPPUNIT_TEST_SUITE_END();
  15. public:
  16. void setUp() {}
  17. void tearDown() {}
  18. void testGetBencodedMessage();
  19. void testGetBencodedMessage6();
  20. };
  21. CPPUNIT_TEST_SUITE_REGISTRATION(DHTFindNodeReplyMessageTest);
  22. void DHTFindNodeReplyMessageTest::testGetBencodedMessage()
  23. {
  24. std::shared_ptr<DHTNode> localNode(new DHTNode());
  25. std::shared_ptr<DHTNode> remoteNode(new DHTNode());
  26. unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
  27. util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
  28. std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
  29. DHTFindNodeReplyMessage msg(AF_INET, localNode, remoteNode, transactionID);
  30. msg.setVersion("A200");
  31. std::string compactNodeInfo;
  32. std::shared_ptr<DHTNode> nodes[8];
  33. for (size_t i = 0; i < DHTBucket::K; ++i) {
  34. nodes[i].reset(new DHTNode());
  35. nodes[i]->setIPAddress("192.168.0." + util::uitos(i + 1));
  36. nodes[i]->setPort(6881 + i);
  37. unsigned char buf[COMPACT_LEN_IPV6];
  38. CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV4,
  39. bittorrent::packcompact(buf, nodes[i]->getIPAddress(),
  40. nodes[i]->getPort()));
  41. compactNodeInfo +=
  42. std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH]) +
  43. std::string(&buf[0], &buf[COMPACT_LEN_IPV4]);
  44. }
  45. msg.setClosestKNodes(
  46. std::vector<std::shared_ptr<DHTNode>>(&nodes[0], &nodes[DHTBucket::K]));
  47. std::string msgbody = msg.getBencodedMessage();
  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("nodes", compactNodeInfo);
  55. dict.put("r", std::move(rDict));
  56. CPPUNIT_ASSERT_EQUAL(bencode2::encode(&dict), msgbody);
  57. }
  58. void DHTFindNodeReplyMessageTest::testGetBencodedMessage6()
  59. {
  60. std::shared_ptr<DHTNode> localNode(new DHTNode());
  61. std::shared_ptr<DHTNode> remoteNode(new DHTNode());
  62. unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
  63. util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
  64. std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
  65. DHTFindNodeReplyMessage msg(AF_INET6, localNode, remoteNode, transactionID);
  66. msg.setVersion("A200");
  67. std::string compactNodeInfo;
  68. std::shared_ptr<DHTNode> nodes[8];
  69. for (size_t i = 0; i < DHTBucket::K; ++i) {
  70. nodes[i].reset(new DHTNode());
  71. nodes[i]->setIPAddress("2001::000" + util::uitos(i + 1));
  72. nodes[i]->setPort(6881 + i);
  73. unsigned char buf[COMPACT_LEN_IPV6];
  74. CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV6,
  75. bittorrent::packcompact(buf, nodes[i]->getIPAddress(),
  76. nodes[i]->getPort()));
  77. compactNodeInfo +=
  78. std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH]) +
  79. std::string(&buf[0], &buf[COMPACT_LEN_IPV6]);
  80. }
  81. msg.setClosestKNodes(
  82. std::vector<std::shared_ptr<DHTNode>>(&nodes[0], &nodes[DHTBucket::K]));
  83. std::string msgbody = msg.getBencodedMessage();
  84. Dict dict;
  85. dict.put("t", transactionID);
  86. dict.put("v", "A200");
  87. dict.put("y", "r");
  88. auto rDict = Dict::g();
  89. rDict->put("id", String::g(localNode->getID(), DHT_ID_LENGTH));
  90. rDict->put("nodes6", compactNodeInfo);
  91. dict.put("r", std::move(rDict));
  92. CPPUNIT_ASSERT_EQUAL(bencode2::encode(&dict), msgbody);
  93. }
  94. } // namespace aria2