BtHandshakeMessageTest.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }
  18. void testCreate();
  19. void testCreateMessage();
  20. void testToString();
  21. void testSetDHTEnabled();
  22. static std::string BTPSTR;
  23. };
  24. std::string BtHandshakeMessageTest::BTPSTR = "BitTorrent protocol";
  25. CPPUNIT_TEST_SUITE_REGISTRATION(BtHandshakeMessageTest);
  26. void createHandshakeMessageData(unsigned char* msg) {
  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,
  33. 0xff, 0xff, 0xff, 0xff, 0xff,
  34. 0xff, 0xff, 0xff, 0xff, 0xff,
  35. 0xff, 0xff, 0xff, 0xff, 0xff };
  36. memcpy(&msg[28], infoHash, sizeof(infoHash));
  37. unsigned char peerId[] = { 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  38. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  39. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  40. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };
  41. memcpy(&msg[48], peerId, sizeof(peerId));
  42. }
  43. void BtHandshakeMessageTest::testCreate() {
  44. unsigned char msg[68];
  45. createHandshakeMessageData(msg);
  46. SharedHandle<BtHandshakeMessage> message = 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(util::toHex((const unsigned char*)BTPSTR.c_str(), BTPSTR.size()),
  50. util::toHex(message->getPstr(), BtHandshakeMessage::PSTR_LENGTH));
  51. CPPUNIT_ASSERT_EQUAL(std::string("0000000000100004"),
  52. util::toHex(message->getReserved(), BtHandshakeMessage::RESERVED_LENGTH));
  53. CPPUNIT_ASSERT_EQUAL(std::string("ffffffffffffffffffffffffffffffffffffffff"),
  54. util::toHex(message->getInfoHash(), INFO_HASH_LENGTH));
  55. CPPUNIT_ASSERT_EQUAL(std::string("f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0"),
  56. util::toHex(message->getPeerId(), PEER_ID_LENGTH));
  57. }
  58. void BtHandshakeMessageTest::testCreateMessage() {
  59. unsigned char infoHash[] = { 0xff, 0xff, 0xff, 0xff, 0xff,
  60. 0xff, 0xff, 0xff, 0xff, 0xff,
  61. 0xff, 0xff, 0xff, 0xff, 0xff,
  62. 0xff, 0xff, 0xff, 0xff, 0xff };
  63. unsigned char peerId[] = { 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  64. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  65. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  66. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };
  67. SharedHandle<BtHandshakeMessage> msg(new BtHandshakeMessage());
  68. msg->setInfoHash(infoHash);
  69. msg->setPeerId(peerId);
  70. unsigned char data[68];
  71. createHandshakeMessageData(data);
  72. unsigned char* rawmsg = msg->createMessage();
  73. CPPUNIT_ASSERT_EQUAL(util::toHex((const unsigned char*)data, 68),
  74. util::toHex(rawmsg, 68));
  75. delete [] rawmsg;
  76. }
  77. void BtHandshakeMessageTest::testToString() {
  78. unsigned char infoHash[] = { 0xff, 0xff, 0xff, 0xff, 0xff,
  79. 0xff, 0xff, 0xff, 0xff, 0xff,
  80. 0xff, 0xff, 0xff, 0xff, 0xff,
  81. 0xff, 0xff, 0xff, 0xff, 0xff };
  82. unsigned char peerId[] = { 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  83. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  84. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  85. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };
  86. BtHandshakeMessage msg;
  87. msg.setInfoHash(infoHash);
  88. msg.setPeerId(peerId);
  89. 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());
  90. }
  91. void BtHandshakeMessageTest::testSetDHTEnabled()
  92. {
  93. BtHandshakeMessage msg;
  94. CPPUNIT_ASSERT(!msg.isDHTEnabled());
  95. msg.setDHTEnabled(false);
  96. CPPUNIT_ASSERT(!msg.isDHTEnabled());
  97. msg.setDHTEnabled(true);
  98. CPPUNIT_ASSERT(msg.isDHTEnabled());
  99. }
  100. } // namespace aria2