BtHandshakeMessageTest.cc 3.9 KB

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