summaryrefslogtreecommitdiff
path: root/test/BtHandshakeMessageTest.cc
blob: ae626e3a5982a6833e8aa4e608b49ac67e4badcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "BtHandshakeMessage.h"
#include "PeerMessageUtil.h"
#include "Util.h"
#include "BtConstants.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>

namespace aria2 {

class BtHandshakeMessageTest:public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(BtHandshakeMessageTest);
  CPPUNIT_TEST(testCreate);
  CPPUNIT_TEST(testGetMessage);
  CPPUNIT_TEST(testToString);
  CPPUNIT_TEST(testSetDHTEnabled);
  CPPUNIT_TEST_SUITE_END();
private:

public:
  void setUp() {
  }

  void testCreate();
  void testGetMessage();
  void testToString();
  void testSetDHTEnabled();

  static std::string BTPSTR;
};


std::string BtHandshakeMessageTest::BTPSTR = "BitTorrent protocol";

CPPUNIT_TEST_SUITE_REGISTRATION(BtHandshakeMessageTest);

void createHandshakeMessageData(unsigned char* msg) {
  msg[0] = 19;
  memcpy(&msg[1], BtHandshakeMessageTest::BTPSTR.c_str(),
	 BtHandshakeMessageTest::BTPSTR.size());
  unsigned char reserved[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x04 };
  memcpy(&msg[20], reserved, sizeof(reserved));
  unsigned char infoHash[] = { 0xff, 0xff, 0xff, 0xff, 0xff,
			       0xff, 0xff, 0xff, 0xff, 0xff,
			       0xff, 0xff, 0xff, 0xff, 0xff,
			       0xff, 0xff, 0xff, 0xff, 0xff };
  memcpy(&msg[28], infoHash, sizeof(infoHash));
  unsigned char peerId[] = { 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
			     0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
			     0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
			     0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };
  memcpy(&msg[48], peerId, sizeof(peerId));
}

void BtHandshakeMessageTest::testCreate() {
  unsigned char msg[68];
  createHandshakeMessageData(msg);
  SharedHandle<BtHandshakeMessage> message = BtHandshakeMessage::create(&msg[0], sizeof(msg));
  CPPUNIT_ASSERT_EQUAL((uint8_t)INT8_MAX, message->getId());
  CPPUNIT_ASSERT_EQUAL((uint8_t)19, message->getPstrlen());
  CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)BTPSTR.c_str(), BTPSTR.size()),
		       Util::toHex(message->getPstr(), BtHandshakeMessage::PSTR_LENGTH));
  CPPUNIT_ASSERT_EQUAL(std::string("0000000000100004"),
		       Util::toHex(message->getReserved(), BtHandshakeMessage::RESERVED_LENGTH));
  CPPUNIT_ASSERT_EQUAL(std::string("ffffffffffffffffffffffffffffffffffffffff"),
		       Util::toHex(message->getInfoHash(), INFO_HASH_LENGTH));
  CPPUNIT_ASSERT_EQUAL(std::string("f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0"),
		       Util::toHex(message->getPeerId(), PEER_ID_LENGTH));
}

void BtHandshakeMessageTest::testGetMessage() {
  unsigned char infoHash[] = { 0xff, 0xff, 0xff, 0xff, 0xff,
			       0xff, 0xff, 0xff, 0xff, 0xff,
			       0xff, 0xff, 0xff, 0xff, 0xff,
			       0xff, 0xff, 0xff, 0xff, 0xff };
  unsigned char peerId[] = { 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
			     0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
			     0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
			     0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };

  SharedHandle<BtHandshakeMessage> msg(new BtHandshakeMessage());
  msg->setInfoHash(infoHash);
  msg->setPeerId(peerId);

  unsigned char data[68];
  createHandshakeMessageData(data);
  CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)data, 68),
		       Util::toHex((const unsigned char*)msg->getMessage(), 68));
}

void BtHandshakeMessageTest::testToString() {
  unsigned char infoHash[] = { 0xff, 0xff, 0xff, 0xff, 0xff,
			       0xff, 0xff, 0xff, 0xff, 0xff,
			       0xff, 0xff, 0xff, 0xff, 0xff,
			       0xff, 0xff, 0xff, 0xff, 0xff };
  unsigned char peerId[] = { 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
			     0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
			     0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
			     0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };

  BtHandshakeMessage msg;
  msg.setInfoHash(infoHash);
  msg.setPeerId(peerId);

  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());
}

void BtHandshakeMessageTest::testSetDHTEnabled()
{
  BtHandshakeMessage msg;
  CPPUNIT_ASSERT(!msg.isDHTEnabled());
  msg.setDHTEnabled(false);
  CPPUNIT_ASSERT(!msg.isDHTEnabled());
  msg.setDHTEnabled(true);
  CPPUNIT_ASSERT(msg.isDHTEnabled());
}

} // namespace aria2