BtHandshakeMessageTest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_SUITE_END();
  12. private:
  13. public:
  14. void setUp() {
  15. }
  16. void testCreate();
  17. void testGetMessage();
  18. void testToString();
  19. static string BTPSTR;
  20. };
  21. string BtHandshakeMessageTest::BTPSTR = "BitTorrent protocol";
  22. CPPUNIT_TEST_SUITE_REGISTRATION(BtHandshakeMessageTest);
  23. void createHandshakeMessageData(unsigned char* msg) {
  24. msg[0] = 19;
  25. memcpy(&msg[1], BtHandshakeMessageTest::BTPSTR.c_str(),
  26. BtHandshakeMessageTest::BTPSTR.size());
  27. unsigned char reserved[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04 };
  28. memcpy(&msg[20], reserved, sizeof(reserved));
  29. unsigned char infoHash[] = { 0xff, 0xff, 0xff, 0xff, 0xff,
  30. 0xff, 0xff, 0xff, 0xff, 0xff,
  31. 0xff, 0xff, 0xff, 0xff, 0xff,
  32. 0xff, 0xff, 0xff, 0xff, 0xff };
  33. memcpy(&msg[28], infoHash, sizeof(infoHash));
  34. unsigned char peerId[] = { 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  35. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  36. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  37. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };
  38. memcpy(&msg[48], peerId, sizeof(peerId));
  39. }
  40. void BtHandshakeMessageTest::testCreate() {
  41. unsigned char msg[68];
  42. createHandshakeMessageData(msg);
  43. BtHandshakeMessageHandle message = BtHandshakeMessage::create(&msg[0], sizeof(msg));
  44. CPPUNIT_ASSERT_EQUAL((uint8_t)UINT8_MAX, message->getId());
  45. CPPUNIT_ASSERT_EQUAL((uint8_t)19, message->getPstrlen());
  46. CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)BTPSTR.c_str(), BTPSTR.size()),
  47. Util::toHex(message->getPstr(), BtHandshakeMessage::PSTR_LENGTH));
  48. CPPUNIT_ASSERT_EQUAL(string("0000000000000004"),
  49. Util::toHex(message->getReserved(), BtHandshakeMessage::RESERVED_LENGTH));
  50. CPPUNIT_ASSERT_EQUAL(string("ffffffffffffffffffffffffffffffffffffffff"),
  51. Util::toHex(message->getInfoHash(), INFO_HASH_LENGTH));
  52. CPPUNIT_ASSERT_EQUAL(string("f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0"),
  53. Util::toHex(message->getPeerId(), PEER_ID_LENGTH));
  54. }
  55. void BtHandshakeMessageTest::testGetMessage() {
  56. unsigned char infoHash[] = { 0xff, 0xff, 0xff, 0xff, 0xff,
  57. 0xff, 0xff, 0xff, 0xff, 0xff,
  58. 0xff, 0xff, 0xff, 0xff, 0xff,
  59. 0xff, 0xff, 0xff, 0xff, 0xff };
  60. unsigned char peerId[] = { 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  61. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  62. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  63. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };
  64. BtHandshakeMessageHandle msg = new BtHandshakeMessage();
  65. msg->setInfoHash(infoHash);
  66. msg->setPeerId(peerId);
  67. unsigned char data[68];
  68. createHandshakeMessageData(data);
  69. CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)data, 68),
  70. Util::toHex((const unsigned char*)msg->getMessage(), 68));
  71. }
  72. void BtHandshakeMessageTest::testToString() {
  73. unsigned char infoHash[] = { 0xff, 0xff, 0xff, 0xff, 0xff,
  74. 0xff, 0xff, 0xff, 0xff, 0xff,
  75. 0xff, 0xff, 0xff, 0xff, 0xff,
  76. 0xff, 0xff, 0xff, 0xff, 0xff };
  77. unsigned char peerId[] = { 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  78. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  79. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  80. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };
  81. BtHandshakeMessage msg;
  82. msg.setInfoHash(infoHash);
  83. msg.setPeerId(peerId);
  84. 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=0000000000000004"), msg.toString());
  85. }