forked from citizenfx/fivem
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestComponents.cpp
More file actions
424 lines (376 loc) · 15 KB
/
TestComponents.cpp
File metadata and controls
424 lines (376 loc) · 15 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include <type_traits>
#include <StdInc.h>
#include <catch_amalgamated.hpp>
#include <ByteReader.h>
#include <ByteWriter.h>
#include <SerializableComponent.h>
#include <SerializableProperty.h>
#include <SerializableOptional.h>
#include <SerializableVector.h>
#include <SerializableStorageType.h>
#include "ByteCounter.h"
#include "MumbleMessage.h"
#include "StreamByteReader.h"
#include "TestUtils.h"
struct SomeComponent : net::SerializableComponent
{
enum class SomeEnum: uint8_t
{
First,
Second,
Third
};
net::SerializableProperty<bool> someBool;
net::SerializableProperty<uint32_t> someNumber;
net::SerializableOptional<net::SerializableProperty<uint32_t>> someOptionalNumber;
// string_view property can only be used when the buffer stays in memory while the struct is read
net::SerializableProperty<std::string_view, net::storage_type::BytesArray> someStringView;
net::SerializableProperty<std::string, net::storage_type::BytesArray> someString;
net::SerializableVector<net::SerializableProperty<uint32_t>, net::storage_type::BytesArray> someNumberVector;
net::SerializableProperty<std::vector<uint8_t>, net::storage_type::BytesArray> someBuffer;
net::SerializableProperty<SomeEnum> someEnum;
net::SerializableProperty<net::Span<uint16_t>, net::storage_type::ConstrainedSmallBytesArray<0, 3>> someSpan;
template <typename T>
bool Process(T& stream)
{
return ProcessPropertiesInOrder<T>(
stream,
someBool,
someNumber,
someOptionalNumber,
someStringView,
someString,
someNumberVector,
someBuffer,
someEnum,
someSpan
);
}
};
struct SectorComponent : net::SerializableComponent
{
net::SerializableProperty<int16_t, net::storage_type::Bits<10>> x;
net::SerializableProperty<int16_t, net::storage_type::Bits<10>> y;
net::SerializableProperty<int16_t, net::storage_type::Bits<6>> z;
SomeComponent someComponent;
net::SerializableOptional<SomeComponent> someOptionalNumber;
template <typename T>
bool Process(T& stream)
{
return ProcessPropertiesInOrder<T>(
stream,
x,
y,
z,
someComponent,
someOptionalNumber
);
}
};
TEST_CASE("Components test")
{
std::vector<uint16_t> spanBuffer;
spanBuffer.resize(3);
spanBuffer[0] = fx::TestUtils::u64Random(500);
spanBuffer[1] = fx::TestUtils::u64Random(500);
spanBuffer[2] = fx::TestUtils::u64Random(500);
std::string someString = fx::TestUtils::asciiRandom(1000);
SectorComponent sectorComponent;
sectorComponent.x = fx::TestUtils::u64Random(500);
sectorComponent.y = fx::TestUtils::u64Random(500);
sectorComponent.z = fx::TestUtils::u64Random(500);
sectorComponent.someComponent.someBool = fx::TestUtils::u64Random(1) == 1;
sectorComponent.someComponent.someNumber = fx::TestUtils::u64Random(500);
sectorComponent.someComponent.someOptionalNumber = fx::TestUtils::u64Random(500);
sectorComponent.someComponent.someStringView = std::string_view(someString);
sectorComponent.someComponent.someString = fx::TestUtils::asciiRandom(10);
sectorComponent.someComponent.someNumberVector.EmplaceBack(1);
sectorComponent.someComponent.someNumberVector.EmplaceBack(2);
sectorComponent.someComponent.someBuffer.GetValue().resize(3);
sectorComponent.someComponent.someBuffer.GetValue()[0] = 1;
sectorComponent.someComponent.someBuffer.GetValue()[1] = 2;
sectorComponent.someComponent.someBuffer.GetValue()[2] = 3;
sectorComponent.someComponent.someEnum = SomeComponent::SomeEnum::Second;
sectorComponent.someComponent.someSpan = {spanBuffer.data(), sectorComponent.someComponent.someBuffer.GetValue().size()};
uint8_t spanIterations = 0;
for (const uint16_t value : sectorComponent.someComponent.someSpan.GetValue())
{
REQUIRE(value == spanBuffer[spanIterations]);
spanIterations++;
}
REQUIRE(spanIterations == spanBuffer.size());
uint8_t buffer[4 * 1024];
net::ByteWriter writeStream(buffer, sizeof(buffer));
if (!sectorComponent.Process(writeStream))
{
REQUIRE(false);
}
SectorComponent readSectorComponent;
net::ByteReader readStream(buffer, sizeof(buffer));
if (!readSectorComponent.Process(readStream))
{
REQUIRE(false);
}
REQUIRE(sectorComponent.x == readSectorComponent.x);
REQUIRE(sectorComponent.y == readSectorComponent.y);
REQUIRE(sectorComponent.z == readSectorComponent.z);
REQUIRE(sectorComponent.someComponent.someBool == readSectorComponent.someComponent.someBool);
REQUIRE(sectorComponent.someComponent.someNumber == readSectorComponent.someComponent.someNumber);
REQUIRE(
sectorComponent.someComponent.someOptionalNumber == readSectorComponent.someComponent.
someOptionalNumber);
REQUIRE(sectorComponent.someComponent.someStringView == readSectorComponent.someComponent.someStringView);
REQUIRE(sectorComponent.someComponent.someString == readSectorComponent.someComponent.someString);
REQUIRE(sectorComponent.someComponent.someNumberVector == readSectorComponent.someComponent.someNumberVector);
REQUIRE(sectorComponent.someComponent.someBuffer == readSectorComponent.someComponent.someBuffer);
REQUIRE(sectorComponent.someComponent.someEnum == readSectorComponent.someComponent.someEnum);
REQUIRE(sectorComponent.someComponent.someSpan == readSectorComponent.someComponent.someSpan);
net::ByteMaxCounter byteCounter;
if (!sectorComponent.Process(byteCounter))
{
REQUIRE(false);
}
uint64_t size = byteCounter.GetCapacity();
REQUIRE(size == uint64_t(917549));
}
namespace SerializableComponentPropertyTest {
class Component: public net::SerializableComponent
{
public:
net::SerializableProperty<uint32_t> value {};
template <typename T>
bool Process(T& stream)
{
return ProcessPropertiesInOrder<T>(
stream,
value
);
}
};
class ComponentHoldingClass : public net::SerializableComponent
{
public:
net::SerializableProperty<Component> component {};
template <typename T>
bool Process(T& stream)
{
return ProcessPropertiesInOrder<T>(
stream,
component
);
}
};
}
TEST_CASE("Serializable component property")
{
uint32_t random = fx::TestUtils::u64Random(UINT32_MAX);
SerializableComponentPropertyTest::ComponentHoldingClass instance;
instance.component.GetValue().value.SetValue(random);
uint8_t buffer[4];
net::ByteWriter writer(buffer, sizeof(buffer));
REQUIRE(instance.Process(writer));
REQUIRE(*reinterpret_cast<uint32_t*>(buffer) == random);
SerializableComponentPropertyTest::ComponentHoldingClass instanceRead;
net::ByteReader reader(buffer, sizeof(buffer));
REQUIRE(instanceRead.Process(reader));
REQUIRE(instanceRead.component.GetValue().value.GetValue() == random);
}
namespace SerializableComponentUnlimitedSizeTest
{
class Component: public net::SerializableComponent
{
public:
net::SerializableProperty<uint8_t> value1 {};
net::SerializableProperty<net::Span<uint8_t>, net::storage_type::StreamTail> value2 {};
template <typename T>
bool Process(T& stream)
{
return ProcessPropertiesInOrder<T>(
stream,
value1,
value2
);
}
};
}
TEST_CASE("Serializable component size of unlimited size")
{
REQUIRE(net::SerializableComponent::GetMaxSize<SerializableComponentUnlimitedSizeTest::Component>() == UINT64_MAX);
}
namespace ByteCounterTest
{
class Component: public net::SerializableComponent
{
public:
net::SerializableProperty<net::Span<uint8_t>, net::storage_type::StreamTail> value1 {};
template <typename T>
bool Process(T& stream)
{
return ProcessPropertiesInOrder<T>(
stream,
value1
);
}
};
class ComponentConstrained: public net::SerializableComponent
{
public:
net::SerializableProperty<net::Span<uint8_t>, net::storage_type::ConstrainedStreamTail<128, 2048>> value1 {};
template <typename T>
bool Process(T& stream)
{
return ProcessPropertiesInOrder<T>(
stream,
value1
);
}
};
}
TEST_CASE("byte counter test")
{
REQUIRE(net::SerializableComponent::GetMaxSize<ByteCounterTest::Component>() == UINT64_MAX);
REQUIRE(net::SerializableComponent::GetMinSize<ByteCounterTest::Component>() == 0);
ByteCounterTest::Component component;
const size_t bufferSize = GENERATE(fx::TestUtils::u64Random(UINT16_MAX), 0);
std::vector<uint8_t> buffer(bufferSize);
fx::TestUtils::fillVectorU8Random(buffer);
component.value1.SetValue({buffer.data(), buffer.size()});
REQUIRE(net::SerializableComponent::GetSize<ByteCounterTest::Component>(component) == bufferSize);
REQUIRE(net::SerializableComponent::GetMaxSize<ByteCounterTest::ComponentConstrained>() == 2048);
REQUIRE(net::SerializableComponent::GetMinSize<ByteCounterTest::ComponentConstrained>() == 128);
}
TEST_CASE("read tcp stream")
{
GIVEN("three mumble message")
{
REQUIRE(net::SerializableComponent::GetMaxSize<net::packet::ClientMumbleMessage>() == 2 + 4 + 8192);
REQUIRE(net::SerializableComponent::GetMinSize<net::packet::ClientMumbleMessage>() == 2 + 4 + 1);
std::vector<uint8_t> receiveBuffer(net::SerializableComponent::GetMaxSize<net::packet::ClientMumbleMessage>() * 10);
std::vector<uint8_t> clientData1(GENERATE(8192, 1));
std::vector<uint8_t> clientData2(GENERATE(8192, 1));
std::vector<uint8_t> clientData3(GENERATE(8192, 01));
uint8_t clientType1 = fx::TestUtils::u64Random(UINT8_MAX);
uint8_t clientType2 = fx::TestUtils::u64Random(UINT8_MAX);
uint8_t clientType3 = fx::TestUtils::u64Random(UINT8_MAX);
net::ByteWriter writer(receiveBuffer.data(), receiveBuffer.size());
{
net::packet::ClientMumbleMessage clientMumbleMessage;
clientMumbleMessage.type.SetValue(clientType1);
clientMumbleMessage.data.SetValue({ clientData1.data(), clientData1.size() });
clientMumbleMessage.Process(writer);
}
{
net::packet::ClientMumbleMessage clientMumbleMessage;
clientMumbleMessage.type.SetValue(clientType2);
clientMumbleMessage.data.SetValue({ clientData2.data(), clientData2.size() });
clientMumbleMessage.Process(writer);
}
size_t offsetBefore = writer.GetOffset();
{
net::packet::ClientMumbleMessage clientMumbleMessage;
clientMumbleMessage.type.SetValue(clientType3);
clientMumbleMessage.data.SetValue({ clientData3.data(), clientData3.size() });
clientMumbleMessage.Process(writer);
}
size_t offsetAfter = writer.GetOffset();
writer.Seek(offsetBefore + 6);
THEN("read the received stream")
{
std::vector<uint8_t> remainingMessageBuffer(net::SerializableComponent::GetMaxSize<net::packet::ClientMumbleMessage>());
net::StreamByteReader streamByteReader(remainingMessageBuffer.data(), remainingMessageBuffer.size());
net::Span receiveBufferSpan{ receiveBuffer.data(), writer.GetOffset() };
std::vector<net::packet::ClientMumbleMessage> receivedMessages;
bool res = streamByteReader.Push<net::packet::ClientMumbleMessage>(receiveBufferSpan, [&](net::packet::ClientMumbleMessage& clientMumbleMessage)
{
receivedMessages.push_back(clientMumbleMessage);
});
REQUIRE(res == true);
// when the 3th data is empty the + 6 allows reading it fully
REQUIRE(receivedMessages.size() == 2);
REQUIRE(receivedMessages[0].type.GetValue() == clientType1);
REQUIRE(receivedMessages[1].type.GetValue() == clientType2);
REQUIRE(receivedMessages[0].data.GetValue() == net::Span<uint8_t>(clientData1.data(), clientData1.size()));
REQUIRE(receivedMessages[1].data.GetValue() == net::Span<uint8_t>(clientData2.data(), clientData2.size()));
receivedMessages.clear();
// remaining data,if data is not empty
REQUIRE(clientData3.empty() == false);
REQUIRE(offsetAfter - offsetBefore == clientData3.size() + 6);
REQUIRE(streamByteReader.GetRemainingDataSize() == 6);
receiveBufferSpan = { receiveBuffer.data() + writer.GetOffset(), offsetAfter - offsetBefore - 6 };
REQUIRE(receiveBufferSpan.size() == clientData3.size());
REQUIRE(*reinterpret_cast<const uint16_t*>(streamByteReader.GetData()) == net::ntohs(clientType3));
REQUIRE(*reinterpret_cast<const uint32_t*>(streamByteReader.GetData() + 2) == net::ntohl(clientData3.size()));
res = streamByteReader.Push<net::packet::ClientMumbleMessage>(receiveBufferSpan, [&](net::packet::ClientMumbleMessage& clientMumbleMessage)
{
receivedMessages.push_back(clientMumbleMessage);
});
REQUIRE(res == true);
REQUIRE(receivedMessages.size() == 1);
REQUIRE(receivedMessages[0].type.GetValue() == clientType3);
REQUIRE(receivedMessages[0].data.GetValue() == net::Span{clientData3.data(), clientData3.size()});
REQUIRE(streamByteReader.GetRemainingDataSize() == 0);
REQUIRE(receiveBufferSpan.size() == 0);
// ensure data is within the same buffer
REQUIRE(*(uint16_t*)(receivedMessages[0].data.GetValue().data() - 6) == net::htons(clientType3));
REQUIRE(*(uint32_t*)(receivedMessages[0].data.GetValue().data() - 4) == net::htonl(clientData3.size()));
}
}
GIVEN("too large message")
{
std::vector<uint8_t> remainingMessageBuffer(net::SerializableComponent::GetMaxSize<net::packet::ClientMumbleMessage>());
std::vector<uint8_t> stream(12000);
uint32_t size = net::htonl(8193);
memcpy(stream.data() + 2, &size, 4);
net::StreamByteReader streamByteReader(remainingMessageBuffer.data(), remainingMessageBuffer.size());
net::Span receiveBufferSpan{ stream.data(), stream.size() };
bool res = streamByteReader.Push<net::packet::ClientMumbleMessage>(receiveBufferSpan, [&](net::packet::ClientMumbleMessage& clientMumbleMessage)
{
});
REQUIRE(res == false);
}
GIVEN("large random stream")
{
uint32_t amount = 50000;
std::vector<uint8_t> receiveBuffer (amount * (2 + 4 + 8192));
// vector can not be resized, otherwise pointers inside the receivedMessages will be invalid
std::vector<net::packet::ClientMumbleMessage> receivedMessages;
net::ByteWriter writer {receiveBuffer.data(), receiveBuffer.size()};
for (uint16_t i = 0; i < amount; i++)
{
net::packet::ClientMumbleMessage& clientMumbleMessage = receivedMessages.emplace_back();
clientMumbleMessage.type.SetValue(fx::TestUtils::u64Random(UINT16_MAX - 1));
const uint32_t size = fx::TestUtils::u64Random(8191) + 1;
clientMumbleMessage.data.SetValue({ receiveBuffer.data() + 6 + writer.GetOffset(), size });
REQUIRE(clientMumbleMessage.Process(writer) == net::SerializableResult::Success);
}
std::vector<uint8_t> remainingMessageBuffer(net::SerializableComponent::GetMaxSize<net::packet::ClientMumbleMessage>());
net::StreamByteReader streamByteReader(remainingMessageBuffer.data(), remainingMessageBuffer.size());
size_t offset {0};
uint32_t counter {0};
bool res;
while (writer.GetOffset() > offset)
{
size_t readSize = fx::TestUtils::u64Random(2 + 4 + 8192 - 1) + 1;
if (offset + readSize > writer.GetOffset())
{
readSize = writer.GetOffset() - offset;
}
net::Span receiveBufferSpan{ receiveBuffer.data() + offset, readSize };
res = streamByteReader.Push<net::packet::ClientMumbleMessage>(receiveBufferSpan, [&](net::packet::ClientMumbleMessage& clientMumbleMessage)
{
if (counter > amount)
{
// prevent overflow in case it fails
REQUIRE(false);
}
REQUIRE(receivedMessages[counter].type.GetValue() == clientMumbleMessage.type.GetValue());
REQUIRE(receivedMessages[counter].data.GetValue().size() == clientMumbleMessage.data.GetValue().size());
REQUIRE(receivedMessages[counter].data.GetValue() == clientMumbleMessage.data.GetValue());
++counter;
});
REQUIRE(res == true);
offset += readSize;
}
REQUIRE(counter == amount);
}
}