forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DHTGetPeersReplyMessageTest.cc
160 lines (135 loc) · 5.51 KB
/
DHTGetPeersReplyMessageTest.cc
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "DHTGetPeersReplyMessage.h"
#include <cppunit/extensions/HelperMacros.h>
#include "DHTNode.h"
#include "Exception.h"
#include "util.h"
#include "DHTBucket.h"
#include "bittorrent_helper.h"
#include "Peer.h"
#include "bencode2.h"
namespace aria2 {
class DHTGetPeersReplyMessageTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DHTGetPeersReplyMessageTest);
CPPUNIT_TEST(testGetBencodedMessage);
CPPUNIT_TEST(testGetBencodedMessage6);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
void testGetBencodedMessage();
void testGetBencodedMessage6();
};
CPPUNIT_TEST_SUITE_REGISTRATION(DHTGetPeersReplyMessageTest);
void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
{
std::shared_ptr<DHTNode> localNode(new DHTNode());
std::shared_ptr<DHTNode> remoteNode(new DHTNode());
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
std::string token = "token";
DHTGetPeersReplyMessage msg(AF_INET, localNode, remoteNode, token,
transactionID);
msg.setVersion("A200");
Dict dict;
dict.put("t", transactionID);
dict.put("v", "A200");
dict.put("y", "r");
auto rDict = Dict::g();
rDict->put("id", String::g(localNode->getID(), DHT_ID_LENGTH));
rDict->put("token", token);
{
std::string compactNodeInfo;
std::shared_ptr<DHTNode> nodes[8];
for (size_t i = 0; i < DHTBucket::K; ++i) {
nodes[i].reset(new DHTNode());
nodes[i]->setIPAddress("192.168.0." + util::uitos(i + 1));
nodes[i]->setPort(6881 + i);
unsigned char buf[COMPACT_LEN_IPV6];
CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV4, bittorrent::packcompact(
buf, nodes[i]->getIPAddress(),
nodes[i]->getPort()));
compactNodeInfo += std::string(&nodes[i]->getID()[0],
&nodes[i]->getID()[DHT_ID_LENGTH]) +
std::string(&buf[0], &buf[COMPACT_LEN_IPV4]);
}
msg.setClosestKNodes(
std::vector<std::shared_ptr<DHTNode>>(&nodes[0], &nodes[DHTBucket::K]));
rDict->put("nodes", compactNodeInfo);
std::vector<std::shared_ptr<Peer>> peers;
auto valuesList = List::g();
for (size_t i = 0; i < 4; ++i) {
auto peer =
std::make_shared<Peer>("192.168.0." + util::uitos(i + 1), 6881 + i);
unsigned char buffer[COMPACT_LEN_IPV6];
CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV4,
bittorrent::packcompact(buffer, peer->getIPAddress(),
peer->getPort()));
valuesList->append(String::g(buffer, COMPACT_LEN_IPV4));
peers.push_back(peer);
}
msg.setValues(peers);
rDict->put("values", std::move(valuesList));
dict.put("r", std::move(rDict));
std::string msgbody = msg.getBencodedMessage();
CPPUNIT_ASSERT_EQUAL(util::percentEncode(bencode2::encode(&dict)),
util::percentEncode(msgbody));
}
}
void DHTGetPeersReplyMessageTest::testGetBencodedMessage6()
{
std::shared_ptr<DHTNode> localNode(new DHTNode());
std::shared_ptr<DHTNode> remoteNode(new DHTNode());
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
std::string token = "token";
DHTGetPeersReplyMessage msg(AF_INET6, localNode, remoteNode, token,
transactionID);
msg.setVersion("A200");
Dict dict;
dict.put("t", transactionID);
dict.put("v", "A200");
dict.put("y", "r");
auto rDict = Dict::g();
rDict->put("id", String::g(localNode->getID(), DHT_ID_LENGTH));
rDict->put("token", token);
{
std::string compactNodeInfo;
std::shared_ptr<DHTNode> nodes[8];
for (size_t i = 0; i < DHTBucket::K; ++i) {
nodes[i].reset(new DHTNode());
nodes[i]->setIPAddress("2001::000" + util::uitos(i + 1));
nodes[i]->setPort(6881 + i);
unsigned char buf[COMPACT_LEN_IPV6];
CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV6, bittorrent::packcompact(
buf, nodes[i]->getIPAddress(),
nodes[i]->getPort()));
compactNodeInfo += std::string(&nodes[i]->getID()[0],
&nodes[i]->getID()[DHT_ID_LENGTH]) +
std::string(&buf[0], &buf[COMPACT_LEN_IPV6]);
}
msg.setClosestKNodes(
std::vector<std::shared_ptr<DHTNode>>(&nodes[0], &nodes[DHTBucket::K]));
rDict->put("nodes6", compactNodeInfo);
std::vector<std::shared_ptr<Peer>> peers;
auto valuesList = List::g();
for (size_t i = 0; i < 4; ++i) {
auto peer =
std::make_shared<Peer>("2001::100" + util::uitos(i + 1), 6881 + i);
unsigned char buffer[COMPACT_LEN_IPV6];
CPPUNIT_ASSERT_EQUAL(COMPACT_LEN_IPV6,
bittorrent::packcompact(buffer, peer->getIPAddress(),
peer->getPort()));
valuesList->append(String::g(buffer, COMPACT_LEN_IPV6));
peers.push_back(peer);
}
msg.setValues(peers);
rDict->put("values", std::move(valuesList));
dict.put("r", std::move(rDict));
std::string msgbody = msg.getBencodedMessage();
CPPUNIT_ASSERT_EQUAL(util::percentEncode(bencode2::encode(&dict)),
util::percentEncode(msgbody));
}
}
} // namespace aria2