BtHandshakeMessageTest.cc 4.0 KB

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