BtHandshakeMessageTest.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "BtHandshakeMessage.h"
  2. #include <cstring>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "util.h"
  5. #include "BtConstants.h"
  6. namespace aria2 {
  7. class BtHandshakeMessageTest : public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(BtHandshakeMessageTest);
  9. CPPUNIT_TEST(testCreate);
  10. CPPUNIT_TEST(testCreateMessage);
  11. CPPUNIT_TEST(testToString);
  12. CPPUNIT_TEST(testSetDHTEnabled);
  13. CPPUNIT_TEST_SUITE_END();
  14. private:
  15. public:
  16. void setUp() {}
  17. void testCreate();
  18. void testCreateMessage();
  19. void testToString();
  20. void testSetDHTEnabled();
  21. static std::string BTPSTR;
  22. };
  23. std::string BtHandshakeMessageTest::BTPSTR = "BitTorrent protocol";
  24. CPPUNIT_TEST_SUITE_REGISTRATION(BtHandshakeMessageTest);
  25. void createHandshakeMessageData(unsigned char* msg)
  26. {
  27. msg[0] = 19;
  28. memcpy(&msg[1], BtHandshakeMessageTest::BTPSTR.c_str(),
  29. BtHandshakeMessageTest::BTPSTR.size());
  30. unsigned char reserved[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x04};
  31. memcpy(&msg[20], reserved, sizeof(reserved));
  32. unsigned char infoHash[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  33. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  34. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  35. memcpy(&msg[28], infoHash, sizeof(infoHash));
  36. unsigned char peerId[] = {0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  37. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  38. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0};
  39. memcpy(&msg[48], peerId, sizeof(peerId));
  40. }
  41. void BtHandshakeMessageTest::testCreate()
  42. {
  43. unsigned char msg[68];
  44. createHandshakeMessageData(msg);
  45. std::shared_ptr<BtHandshakeMessage> message =
  46. BtHandshakeMessage::create(&msg[0], sizeof(msg));
  47. CPPUNIT_ASSERT_EQUAL((uint8_t)INT8_MAX, message->getId());
  48. CPPUNIT_ASSERT_EQUAL((uint8_t)19, message->getPstrlen());
  49. CPPUNIT_ASSERT_EQUAL(
  50. util::toHex((const unsigned char*)BTPSTR.c_str(), BTPSTR.size()),
  51. util::toHex(message->getPstr(), BtHandshakeMessage::PSTR_LENGTH));
  52. CPPUNIT_ASSERT_EQUAL(
  53. std::string("0000000000100004"),
  54. util::toHex(message->getReserved(), BtHandshakeMessage::RESERVED_LENGTH));
  55. CPPUNIT_ASSERT_EQUAL(std::string("ffffffffffffffffffffffffffffffffffffffff"),
  56. util::toHex(message->getInfoHash(), INFO_HASH_LENGTH));
  57. CPPUNIT_ASSERT_EQUAL(std::string("f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0"),
  58. util::toHex(message->getPeerId(), PEER_ID_LENGTH));
  59. }
  60. void BtHandshakeMessageTest::testCreateMessage()
  61. {
  62. unsigned char infoHash[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  63. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  64. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  65. unsigned char peerId[] = {0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  66. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  67. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0};
  68. std::shared_ptr<BtHandshakeMessage> msg(new BtHandshakeMessage());
  69. msg->setInfoHash(infoHash);
  70. msg->setPeerId(peerId);
  71. unsigned char data[68];
  72. createHandshakeMessageData(data);
  73. unsigned char* rawmsg = msg->createMessage();
  74. CPPUNIT_ASSERT_EQUAL(util::toHex((const unsigned char*)data, 68),
  75. util::toHex(rawmsg, 68));
  76. delete[] rawmsg;
  77. }
  78. void BtHandshakeMessageTest::testToString()
  79. {
  80. unsigned char infoHash[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  81. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  82. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  83. unsigned char peerId[] = {0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  84. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  85. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0};
  86. BtHandshakeMessage msg;
  87. msg.setInfoHash(infoHash);
  88. msg.setPeerId(peerId);
  89. CPPUNIT_ASSERT_EQUAL(
  90. std::string("handshake "
  91. "peerId=%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%"
  92. "F0%F0%F0, reserved=0000000000100004"),
  93. msg.toString());
  94. }
  95. void BtHandshakeMessageTest::testSetDHTEnabled()
  96. {
  97. BtHandshakeMessage msg;
  98. CPPUNIT_ASSERT(!msg.isDHTEnabled());
  99. msg.setDHTEnabled(false);
  100. CPPUNIT_ASSERT(!msg.isDHTEnabled());
  101. msg.setDHTEnabled(true);
  102. CPPUNIT_ASSERT(msg.isDHTEnabled());
  103. }
  104. } // namespace aria2