]> git.localhorst.tv Git - blank.git/blob - tst/net/PacketTest.cpp
more unit tests for Packet
[blank.git] / tst / net / PacketTest.cpp
1 #include "PacketTest.hpp"
2
3 #include "model/CompositeModel.hpp"
4 #include "world/Entity.hpp"
5
6 #include <limits>
7
8 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::PacketTest);
9
10 using namespace std;
11
12 namespace blank {
13 namespace test {
14
15 void PacketTest::setUp() {
16         udp_pack.data = new Uint8[sizeof(Packet)];
17         udp_pack.maxlen = sizeof(Packet);
18 }
19
20 void PacketTest::tearDown() {
21         delete[] udp_pack.data;
22 }
23
24 namespace {
25
26 static constexpr uint32_t TEST_TAG = 0xFB1AB1AF;
27
28 }
29
30 void PacketTest::testControl() {
31         Packet::TControl ctrl;
32         ctrl.ack = 10;
33
34         CPPUNIT_ASSERT_MESSAGE(
35                 "TControl should ack the packet in the ack field",
36                 ctrl.Acks(10)
37         );
38         CPPUNIT_ASSERT_MESSAGE(
39                 "TControl should ack the packet in the future",
40                 !ctrl.Acks(11)
41         );
42         CPPUNIT_ASSERT_MESSAGE(
43                 "TControl should not ack a packet in the distant past",
44                 !ctrl.Acks(-30)
45         );
46         CPPUNIT_ASSERT_MESSAGE(
47                 "TControl should not ack the previous packet if the bitfield is 0",
48                 !ctrl.Acks(9)
49         );
50         CPPUNIT_ASSERT_EQUAL_MESSAGE(
51                 "TControl's acks should begin at the packet in the ack field",
52                 uint16_t(10), ctrl.AckBegin()
53         );
54         CPPUNIT_ASSERT_EQUAL_MESSAGE(
55                 "TControl's acks should end 33 packets before the one in the ack field",
56                 uint16_t(-23), ctrl.AckEnd()
57         );
58         ctrl.hist = 1;
59         CPPUNIT_ASSERT_MESSAGE(
60                 "TControl should ack the previous packet if the bitfield is 1",
61                 ctrl.Acks(9)
62         );
63         ctrl.hist = 2;
64         CPPUNIT_ASSERT_MESSAGE(
65                 "TControl should not ack the previous packet if the bitfield is 2",
66                 !ctrl.Acks(9)
67         );
68         CPPUNIT_ASSERT_MESSAGE(
69                 "TControl should ack the packet before the previous one if the bitfield is 2",
70                 ctrl.Acks(8)
71         );
72 }
73
74 void PacketTest::testPing() {
75         auto pack = Packet::Make<Packet::Ping>(udp_pack);
76         AssertPacket("Ping", 0, 0, pack);
77 }
78
79 void PacketTest::testLogin() {
80         auto pack = Packet::Make<Packet::Login>(udp_pack);
81         AssertPacket("Login", 1, 0, 32, pack);
82
83         string write_name = "test";
84         string read_name;
85         pack.WritePlayerName(write_name);
86         pack.ReadPlayerName(read_name);
87         CPPUNIT_ASSERT_EQUAL_MESSAGE(
88                 "player name not correctly transported in Login packet",
89                 write_name, read_name
90         );
91
92         write_name = "0123456789012345678901234567890123456789";
93         pack.WritePlayerName(write_name);
94         pack.ReadPlayerName(read_name);
95         CPPUNIT_ASSERT_EQUAL_MESSAGE(
96                 "player name not correctly truncated in Login packet",
97                 write_name.substr(0, 32), read_name
98         );
99 }
100
101 void PacketTest::testJoin() {
102         auto pack = Packet::Make<Packet::Join>(udp_pack);
103         AssertPacket("Join", 2, 68, 100, pack);
104
105         Entity write_entity;
106         write_entity.ID(534574);
107         write_entity.GetState().chunk_pos = { 7, 2, -3 };
108         write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
109         write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
110         write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
111         write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
112         uint32_t read_id = 0;
113         EntityState read_state;
114         pack.WritePlayer(write_entity);
115
116         pack.ReadPlayerID(read_id);
117         CPPUNIT_ASSERT_EQUAL_MESSAGE(
118                 "player entity ID not correctly transported in Join packet",
119                 write_entity.ID(), read_id
120         );
121         pack.ReadPlayerState(read_state);
122         AssertEqual(
123                 "player entity state not correctly transported in Join packet",
124                 write_entity.GetState(), read_state
125         );
126
127         string write_name = "test";
128         string read_name;
129         pack.WriteWorldName(write_name);
130         pack.ReadWorldName(read_name);
131         CPPUNIT_ASSERT_EQUAL_MESSAGE(
132                 "world name not correctly transported in Join packet",
133                 write_name, read_name
134         );
135
136         write_name = "0123456789012345678901234567890123456789";
137         pack.WriteWorldName(write_name);
138         pack.ReadWorldName(read_name);
139         CPPUNIT_ASSERT_EQUAL_MESSAGE(
140                 "world name not correctly truncated in Join packet",
141                 write_name.substr(0, 32), read_name
142         );
143 }
144
145 void PacketTest::testPart() {
146         auto pack = Packet::Make<Packet::Part>(udp_pack);
147         AssertPacket("Part", 3, 0, pack);
148 }
149
150 void PacketTest::testPlayerUpdate() {
151         auto pack = Packet::Make<Packet::PlayerUpdate>(udp_pack);
152         AssertPacket("PlayerUpdate", 4, 64, pack);
153
154         Entity write_entity;
155         write_entity.ID(534574);
156         write_entity.GetState().chunk_pos = { 7, 2, -3 };
157         write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
158         write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
159         write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
160         write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
161         EntityState read_state;
162         pack.WritePlayer(write_entity);
163
164         pack.ReadPlayerState(read_state);
165         AssertEqual(
166                 "player entity state not correctly transported in PlayerUpdate packet",
167                 write_entity.GetState(), read_state
168         );
169 }
170
171 void PacketTest::testSpawnEntity() {
172         auto pack = Packet::Make<Packet::SpawnEntity>(udp_pack);
173         AssertPacket("SpawnEntity", 5, 100, 132, pack);
174
175         Entity write_entity;
176         write_entity.ID(534574);
177         CompositeModel model;
178         model.ID(23);
179         model.Instantiate(write_entity.GetModel());
180         write_entity.GetState().chunk_pos = { 7, 2, -3 };
181         write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
182         write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
183         write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
184         write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
185         write_entity.Bounds({{ -1, -1, -1 }, { 1, 1, 1 }});
186         write_entity.WorldCollidable(true);
187         write_entity.Name("blah");
188         pack.WriteEntity(write_entity);
189
190         uint32_t entity_id;
191         uint32_t skeleton_id;
192         Entity read_entity;
193         pack.ReadEntityID(entity_id);
194         pack.ReadSkeletonID(skeleton_id);
195         pack.ReadEntity(read_entity);
196
197         CPPUNIT_ASSERT_EQUAL_MESSAGE(
198                 "entity ID not correctly transported in SpawnEntity packet",
199                 write_entity.ID(), entity_id
200         );
201         CPPUNIT_ASSERT_EQUAL_MESSAGE(
202                 "skeleton ID not correctly transported in SpawnEntity packet",
203                 write_entity.GetModel().GetModel().ID(), skeleton_id
204         );
205         AssertEqual(
206                 "entity state not correctly transported in PlayerUpdate packet",
207                 write_entity.GetState(), read_entity.GetState()
208         );
209         AssertEqual(
210                 "entity bounds not correctly transported in PlayerUpdate packet",
211                 write_entity.Bounds(), read_entity.Bounds()
212         );
213         CPPUNIT_ASSERT_MESSAGE(
214                 "entity flags not correctly transported in SpawnEntity packet",
215                 read_entity.WorldCollidable()
216         );
217         CPPUNIT_ASSERT_EQUAL_MESSAGE(
218                 "entity name not correctly transported in SpawnEntity packet",
219                 write_entity.Name(), read_entity.Name()
220         );
221 }
222
223 void PacketTest::testDespawnEntity() {
224         auto pack = Packet::Make<Packet::DespawnEntity>(udp_pack);
225         AssertPacket("DespawnEntity", 6, 4, pack);
226
227         uint32_t write_id = 5437;
228         uint32_t read_id;
229         pack.WriteEntityID(write_id);
230         pack.ReadEntityID(read_id);
231
232         CPPUNIT_ASSERT_EQUAL_MESSAGE(
233                 "entity ID not correctly transported in DespawnEntity packet",
234                 write_id, read_id
235         );
236 }
237
238
239 void PacketTest::AssertPacket(
240         const string &name,
241         uint8_t expected_type,
242         size_t expected_length,
243         const Packet::Payload &actual
244 ) {
245         CPPUNIT_ASSERT_EQUAL_MESSAGE(
246                 name + " packet not correctly tagged",
247                 TEST_TAG, actual.GetHeader().tag
248         );
249         CPPUNIT_ASSERT_EQUAL_MESSAGE(
250                 "wrong type code for " + name + " packet",
251                 int(expected_type), int(actual.GetHeader().type)
252         );
253         CPPUNIT_ASSERT_EQUAL_MESSAGE(
254                 "bad payload length for " + name + " packet",
255                 expected_length, actual.length
256         );
257 }
258
259 void PacketTest::AssertPacket(
260         const string &name,
261         uint8_t expected_type,
262         size_t min_length,
263         size_t max_length,
264         const Packet::Payload &actual
265 ) {
266         CPPUNIT_ASSERT_EQUAL_MESSAGE(
267                 name + " packet not correctly tagged",
268                 TEST_TAG, actual.GetHeader().tag
269         );
270         CPPUNIT_ASSERT_EQUAL_MESSAGE(
271                 "wrong type code for " + name + " packet",
272                 expected_type, actual.GetHeader().type
273         );
274         CPPUNIT_ASSERT_MESSAGE(
275                 "bad payload length for " + name + " packet",
276                 actual.length >= min_length && actual.length <= max_length
277         );
278 }
279
280 void PacketTest::AssertEqual(
281         const string &message,
282         const EntityState &expected,
283         const EntityState &actual
284 ) {
285         AssertEqual(
286                 message + ": bad chunk position",
287                 expected.chunk_pos, actual.chunk_pos
288         );
289         AssertEqual(
290                 message + ": bad block position",
291                 expected.block_pos, actual.block_pos
292         );
293         AssertEqual(
294                 message + ": bad velocity",
295                 expected.velocity, actual.velocity
296         );
297         AssertEqual(
298                 message + ": bad orientation",
299                 expected.orient, actual.orient
300         );
301         AssertEqual(
302                 message + ": bad angular velocity",
303                 expected.ang_vel, actual.ang_vel
304         );
305 }
306
307 void PacketTest::AssertEqual(
308         const string &message,
309         const AABB &expected,
310         const AABB &actual
311 ) {
312         AssertEqual(
313                 message + ": bad lower bound",
314                 expected.min, actual.min
315         );
316         AssertEqual(
317                 message + ": bad upper bound",
318                 expected.max, actual.max
319         );
320 }
321
322 void PacketTest::AssertEqual(
323         const string &message,
324         const glm::ivec3 &expected,
325         const glm::ivec3 &actual
326 ) {
327         CPPUNIT_ASSERT_EQUAL_MESSAGE(
328                 message + " (X component)",
329                 expected.x, actual.x
330         );
331         CPPUNIT_ASSERT_EQUAL_MESSAGE(
332                 message + " (Y component)",
333                 expected.y, actual.y
334         );
335         CPPUNIT_ASSERT_EQUAL_MESSAGE(
336                 message + " (Z component)",
337                 expected.z, actual.z
338         );
339 }
340
341 void PacketTest::AssertEqual(
342         const string &message,
343         const glm::vec3 &expected,
344         const glm::vec3 &actual
345 ) {
346         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
347                 message + " (X component)",
348                 expected.x, actual.x, numeric_limits<float>::epsilon()
349         );
350         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
351                 message + " (Y component)",
352                 expected.y, actual.y, numeric_limits<float>::epsilon()
353         );
354         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
355                 message + " (Z component)",
356                 expected.z, actual.z, numeric_limits<float>::epsilon()
357         );
358 }
359
360 void PacketTest::AssertEqual(
361         const string &message,
362         const glm::quat &expected,
363         const glm::quat &actual
364 ) {
365         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
366                 message + " (W component)",
367                 expected.w, actual.w, numeric_limits<float>::epsilon()
368         );
369         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
370                 message + " (X component)",
371                 expected.x, actual.x, numeric_limits<float>::epsilon()
372         );
373         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
374                 message + " (Y component)",
375                 expected.y, actual.y, numeric_limits<float>::epsilon()
376         );
377         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
378                 message + " (Z component)",
379                 expected.z, actual.z, numeric_limits<float>::epsilon()
380         );
381 }
382
383 }
384 }