DHTFindNodeReplyMessageTest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, bittorrent::packcompact
  39. (buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
  40. compactNodeInfo +=
  41. std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
  42. std::string(&buf[0], &buf[COMPACT_LEN_IPV4]);
  43. }
  44. msg.setClosestKNodes
  45. (std::vector<std::shared_ptr<DHTNode> >(&nodes[0], &nodes[DHTBucket::K]));
  46. std::string msgbody = msg.getBencodedMessage();
  47. Dict dict;
  48. dict.put("t", transactionID);
  49. dict.put("v", "A200");
  50. dict.put("y", "r");
  51. auto rDict = Dict::g();
  52. rDict->put("id", String::g(localNode->getID(), DHT_ID_LENGTH));
  53. rDict->put("nodes", compactNodeInfo);
  54. dict.put("r", std::move(rDict));
  55. CPPUNIT_ASSERT_EQUAL(bencode2::encode(&dict), msgbody);
  56. }
  57. void DHTFindNodeReplyMessageTest::testGetBencodedMessage6()
  58. {
  59. std::shared_ptr<DHTNode> localNode(new DHTNode());
  60. std::shared_ptr<DHTNode> remoteNode(new DHTNode());
  61. unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
  62. util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
  63. std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
  64. DHTFindNodeReplyMessage msg(AF_INET6, localNode, remoteNode, transactionID);
  65. msg.setVersion("A200");
  66. std::string compactNodeInfo;
  67. std::shared_ptr<DHTNode> nodes[8];
  68. for(size_t i = 0; i < DHTBucket::K; ++i) {
  69. nodes[i].reset(new DHTNode());
  70. nodes[i]->setIPAddress("2001::000"+util::uitos(i+1));
  71. nodes[i]->setPort(6881+i);
  72. unsigned char buf[COMPACT_LEN_IPV6];
  73. CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV6, bittorrent::packcompact
  74. (buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
  75. compactNodeInfo +=
  76. std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
  77. std::string(&buf[0], &buf[COMPACT_LEN_IPV6]);
  78. }
  79. msg.setClosestKNodes
  80. (std::vector<std::shared_ptr<DHTNode> >(&nodes[0], &nodes[DHTBucket::K]));
  81. std::string msgbody = msg.getBencodedMessage();
  82. Dict dict;
  83. dict.put("t", transactionID);
  84. dict.put("v", "A200");
  85. dict.put("y", "r");
  86. auto rDict = Dict::g();
  87. rDict->put("id", String::g(localNode->getID(), DHT_ID_LENGTH));
  88. rDict->put("nodes6", compactNodeInfo);
  89. dict.put("r", std::move(rDict));
  90. CPPUNIT_ASSERT_EQUAL(bencode2::encode(&dict), msgbody);
  91. }
  92. } // namespace aria2