forked from citizenfx/fivem
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestOOB.cpp
More file actions
304 lines (277 loc) · 10.6 KB
/
TestOOB.cpp
File metadata and controls
304 lines (277 loc) · 10.6 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <StdInc.h>
#include <catch_amalgamated.hpp>
#include <GameServer.h>
#include <decorators/WithOutOfBand.h>
#include <EventCore.h>
#include "TestUtils.h"
class TestServer : public fwRefCountable, public fx::ComponentHolderImpl<TestServer>
{
};
class TestGameServer : public fwRefCountable, public fx::ComponentHolderImpl<TestGameServer>
{
public:
struct InterceptCallbackData
{
net::PeerAddress address;
const uint8_t* data;
size_t len;
bool intercepted;
InterceptCallbackData(const net::PeerAddress& address, const uint8_t* data, size_t len, bool intercepted)
: address(address),
data(data),
len(len),
intercepted(intercepted)
{
}
};
std::optional<InterceptCallbackData> m_interceptCallbackLastCall{};
bool m_interceptNext{false};
std::function<bool(const uint8_t*, size_t, const net::PeerAddress&)> m_interceptor;
TestServer m_instance;
void AddRawInterceptor(const std::function<bool(const uint8_t*, size_t, const net::PeerAddress&)>& interceptor)
{
m_interceptor = interceptor;
}
void SendOutOfBand(const net::PeerAddress& to, const std::string_view& oob, bool prefix = true)
{
// test sending at a later point
}
TestServer* GetInstance()
{
return &m_instance;
}
// only here for testing, that function is not part of the actual game server implementation
bool TriggerRawInterceptor(const uint8_t* receivedData, size_t receivedDataLength,
const net::PeerAddress& receivedAddress)
{
return m_interceptor(receivedData, receivedDataLength, receivedAddress);
}
};
struct ProcessCallbackData
{
fwRefContainer<TestGameServer> server;
net::PeerAddress from;
std::string_view data;
ProcessCallbackData(const fwRefContainer<TestGameServer>& server, const net::PeerAddress& from,
const std::string_view& data)
: server(server),
from(from),
data(data)
{
}
};
namespace
{
TestGameServer* CreateTestGameServer()
{
TestGameServer* testGameServer = new TestGameServer();
testGameServer->GetInstance()->SetComponent(new fx::UdpInterceptor());
testGameServer->GetInstance()->GetComponent<fx::UdpInterceptor>()->OnIntercept.Connect(
[testGameServer](const net::PeerAddress& address, const uint8_t* data, size_t len, bool* intercepted)
{
*intercepted = testGameServer->m_interceptNext;
testGameServer->m_interceptCallbackLastCall.emplace(address, data, len, *intercepted);
// this return value is never used, only the intercepted value is used
return true;
});
return testGameServer;
}
TestGameServer* g_testGameServer = CreateTestGameServer();
}
class TestOutOfBand
{
public:
// the actual out of band handler has a instance created internally inside WithOutOfBandImpl
// so this one needs to be static
static inline std::optional<ProcessCallbackData> processCallbackLastCall{};
template<typename ServerImpl>
TestOutOfBand(const fwRefContainer<ServerImpl>& server)
{
}
TestOutOfBand()
{
}
template<typename ServerImpl>
void Process(const fwRefContainer<ServerImpl>& server, const net::PeerAddress& from,
const std::string_view& data)
{
processCallbackLastCall.emplace(server, from, data);
}
static constexpr const char* GetName()
{
return "test";
}
void RegisterOutOfBand()
{
fx::ServerDecorators::WithOutOfBandImpl<TestGameServer, TestOutOfBand>(g_testGameServer);
}
bool SendOutOfBand(const std::string& data, const net::PeerAddress& address)
{
g_testGameServer->m_interceptCallbackLastCall.reset();
processCallbackLastCall.reset();
const bool result = g_testGameServer->TriggerRawInterceptor(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), address);
g_testGameServer->m_interceptNext = false;
return result;
}
};
TEST_CASE("Out of band protocol test")
{
TestOutOfBand testOutOfBand;
GIVEN("A out of band handler")
{
testOutOfBand.RegisterOutOfBand();
WHEN("Sending a out of band message")
{
std::string randomData = fx::TestUtils::asciiRandom(fx::TestUtils::u64Random(100) + 1);
std::string testMsg = std::string("test") + "\n" + randomData;
auto oobMsg = "\xFF\xFF\xFF\xFF" + std::string(testMsg);
auto local = net::PeerAddress::FromString("127.0.0.1").get();
REQUIRE(testOutOfBand.SendOutOfBand(oobMsg, local) == true);
THEN("The intercept callback won't be triggered")
{
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.has_value() == false);
}
THEN("The process callback of the registered handler will be triggered")
{
REQUIRE(testOutOfBand.processCallbackLastCall.has_value() == true);
REQUIRE(testOutOfBand.processCallbackLastCall.value().server.GetRef() == g_testGameServer);
REQUIRE(testOutOfBand.processCallbackLastCall.value().data == randomData);
REQUIRE(testOutOfBand.processCallbackLastCall.value().from == local);
}
}
WHEN("Sending without key")
{
std::string randomData = fx::TestUtils::asciiRandom(fx::TestUtils::u64Random(100) + 1);
std::string testMsg = "\n" + randomData;
auto oobMsg = "\xFF\xFF\xFF\xFF" + std::string(testMsg);
auto local = net::PeerAddress::FromString("127.0.0.1").get();
REQUIRE(testOutOfBand.SendOutOfBand(oobMsg, local) == true);
THEN("The intercept callback won't be triggered, because the message header was -1")
{
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.has_value() == false);
}
THEN("The process callback won't be triggered")
{
REQUIRE(testOutOfBand.processCallbackLastCall.has_value() == false);
}
}
WHEN("Sending without data")
{
std::string testMsg = std::string("test") + "\n";
auto oobMsg = "\xFF\xFF\xFF\xFF" + std::string(testMsg);
auto local = net::PeerAddress::FromString("127.0.0.1").get();
REQUIRE(testOutOfBand.SendOutOfBand(oobMsg, local) == true);
THEN("The intercept callback won't be triggered, because the message header was -1")
{
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.has_value() == false);
}
THEN("The process callback will be triggered with empty data")
{
REQUIRE(testOutOfBand.processCallbackLastCall.has_value() == true);
REQUIRE(testOutOfBand.processCallbackLastCall.value().data == "");
REQUIRE(testOutOfBand.processCallbackLastCall.value().from == local);
}
}
WHEN("Sending without seperator")
{
std::string testMsg = "test";
auto oobMsg = "\xFF\xFF\xFF\xFF" + std::string(testMsg);
auto local = net::PeerAddress::FromString("127.0.0.1").get();
REQUIRE(testOutOfBand.SendOutOfBand(oobMsg, local) == true);
THEN("The intercept callback won't be triggered, because the message header was -1")
{
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.has_value() == false);
}
THEN("The process callback will be triggered without data")
{
REQUIRE(testOutOfBand.processCallbackLastCall.has_value() == true);
// last data was matching the randomKey before the fix
REQUIRE(testOutOfBand.processCallbackLastCall.value().data == "");
REQUIRE(testOutOfBand.processCallbackLastCall.value().from == local);
}
}
WHEN("Sending with one char data")
{
std::string data = fx::TestUtils::asciiRandom(1);
std::string testMsg = std::string("test") + "\n" + data;
auto oobMsg = "\xFF\xFF\xFF\xFF" + std::string(testMsg);
auto local = net::PeerAddress::FromString("127.0.0.1").get();
REQUIRE(testOutOfBand.SendOutOfBand(oobMsg, local) == true);
THEN("The intercept callback won't be triggered, because the message header was -1")
{
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.has_value() == false);
}
THEN("The process callback will be triggered and the data will match")
{
REQUIRE(testOutOfBand.processCallbackLastCall.has_value() == true);
REQUIRE(testOutOfBand.processCallbackLastCall.value().data == data);
REQUIRE(testOutOfBand.processCallbackLastCall.value().from == local);
}
}
WHEN("Sending without key and data")
{
std::string data = fx::TestUtils::asciiRandom(1);
auto oobMsg = "\xFF\xFF\xFF\xFF";
auto local = net::PeerAddress::FromString("127.0.0.1").get();
REQUIRE(testOutOfBand.SendOutOfBand(oobMsg, local) == true);
THEN("The intercept callback won't be triggered, because the message header was -1")
{
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.has_value() == false);
}
THEN("The process callback won't be called, because the key and data is empty")
{
// caused a cpp exception before checking len to be 0
REQUIRE(testOutOfBand.processCallbackLastCall.has_value() == false);
}
}
}
}
TEST_CASE("udp interceptor test")
{
TestOutOfBand testOutOfBand;
GIVEN("A out of band handler")
{
testOutOfBand.RegisterOutOfBand();
WHEN("Sending a message that does not contain the out of band prefix")
{
std::string randomData = fx::TestUtils::asciiRandom(fx::TestUtils::u64Random(100) + 1);
auto local = net::PeerAddress::FromString("127.0.0.1").get();
REQUIRE(testOutOfBand.SendOutOfBand(randomData, local) == false);
THEN("The intercept callback will be triggered")
{
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.has_value() == true);
REQUIRE(
memcmp(g_testGameServer->m_interceptCallbackLastCall.value().data, reinterpret_cast<uint8_t*>(randomData.data()),
randomData.size()) == 0);
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.value().len == randomData.size());
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.value().intercepted == false);
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.value().address == local);
}
THEN("The process callback won't be triggered, because there was no -1 header")
{
REQUIRE(testOutOfBand.processCallbackLastCall.has_value() == false);
}
}
WHEN("Marking the message as intercepted inside the udp interceptor")
{
std::string randomData = fx::TestUtils::asciiRandom(fx::TestUtils::u64Random(100) + 1);
auto local = net::PeerAddress::FromString("127.0.0.1").get();
g_testGameServer->m_interceptNext = true;
REQUIRE(testOutOfBand.SendOutOfBand(randomData, local) == true);
THEN("The intercept callback will be triggered")
{
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.has_value() == true);
REQUIRE(
memcmp(g_testGameServer->m_interceptCallbackLastCall.value().data, reinterpret_cast<uint8_t*>(randomData.data()),
randomData.size()) == 0);
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.value().len == randomData.size());
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.value().intercepted == true);
REQUIRE(g_testGameServer->m_interceptCallbackLastCall.value().address == local);
}
THEN("The process callback won't be triggered, because there was no -1 header")
{
REQUIRE(testOutOfBand.processCallbackLastCall.has_value() == false);
}
}
}
}