1 #include "PacketTest.hpp"
3 #include "model/Model.hpp"
4 #include "world/Entity.hpp"
6 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::PacketTest);
13 void PacketTest::setUp() {
14 udp_pack.data = new Uint8[sizeof(Packet)];
15 udp_pack.maxlen = sizeof(Packet);
18 void PacketTest::tearDown() {
19 delete[] udp_pack.data;
24 static constexpr uint32_t TEST_TAG = 0xFB1AB1AF;
28 void PacketTest::testSizes() {
29 CPPUNIT_ASSERT_EQUAL_MESSAGE(
30 "unexpected size of vec3",
31 size_t(12), sizeof(glm::vec3)
33 CPPUNIT_ASSERT_EQUAL_MESSAGE(
34 "unexpected size of vec3i",
35 size_t(12), sizeof(glm::ivec3)
37 CPPUNIT_ASSERT_EQUAL_MESSAGE(
38 "unexpected size of quat",
39 size_t(16), sizeof(glm::quat)
41 CPPUNIT_ASSERT_EQUAL_MESSAGE(
42 "unexpected size of entity state",
43 size_t(64), sizeof(EntityState)
47 void PacketTest::testControl() {
48 Packet::TControl ctrl{ 0, 10, 0 };
50 CPPUNIT_ASSERT_MESSAGE(
51 "TControl should ack the packet in the ack field",
54 CPPUNIT_ASSERT_MESSAGE(
55 "TControl should ack the packet in the future",
58 CPPUNIT_ASSERT_MESSAGE(
59 "TControl should not ack a packet in the distant past",
62 CPPUNIT_ASSERT_MESSAGE(
63 "TControl should not ack the previous packet if the bitfield is 0",
66 CPPUNIT_ASSERT_EQUAL_MESSAGE(
67 "TControl's acks should begin at the packet in the ack field",
68 uint16_t(10), ctrl.AckBegin()
70 CPPUNIT_ASSERT_EQUAL_MESSAGE(
71 "TControl's acks should end 33 packets before the one in the ack field",
72 uint16_t(-23), ctrl.AckEnd()
75 CPPUNIT_ASSERT_MESSAGE(
76 "TControl should ack the previous packet if the bitfield is 1",
80 CPPUNIT_ASSERT_MESSAGE(
81 "TControl should not ack the previous packet if the bitfield is 2",
84 CPPUNIT_ASSERT_MESSAGE(
85 "TControl should ack the packet before the previous one if the bitfield is 2",
90 void PacketTest::testPing() {
91 auto pack = Packet::Make<Packet::Ping>(udp_pack);
92 AssertPacket("Ping", 0, 0, pack);
95 void PacketTest::testLogin() {
96 auto pack = Packet::Make<Packet::Login>(udp_pack);
97 AssertPacket("Login", 1, 0, 32, pack);
99 string write_name = "test";
101 pack.WritePlayerName(write_name);
102 pack.ReadPlayerName(read_name);
103 CPPUNIT_ASSERT_EQUAL_MESSAGE(
104 "player name not correctly transported in Login packet",
105 write_name, read_name
108 write_name = "0123456789012345678901234567890123456789";
109 pack.WritePlayerName(write_name);
110 pack.ReadPlayerName(read_name);
111 CPPUNIT_ASSERT_EQUAL_MESSAGE(
112 "player name not correctly truncated in Login packet",
113 write_name.substr(0, 32), read_name
117 void PacketTest::testJoin() {
118 auto pack = Packet::Make<Packet::Join>(udp_pack);
119 AssertPacket("Join", 2, 68, 100, pack);
122 write_entity.ID(534574);
123 write_entity.GetState().chunk_pos = { 7, 2, -3 };
124 write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
125 write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
126 write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
127 write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
128 uint32_t read_id = 0;
129 EntityState read_state;
130 pack.WritePlayer(write_entity);
132 pack.ReadPlayerID(read_id);
133 CPPUNIT_ASSERT_EQUAL_MESSAGE(
134 "player entity ID not correctly transported in Join packet",
135 write_entity.ID(), read_id
137 pack.ReadPlayerState(read_state);
139 "player entity state not correctly transported in Join packet",
140 write_entity.GetState(), read_state
143 string write_name = "test";
145 pack.WriteWorldName(write_name);
146 pack.ReadWorldName(read_name);
147 CPPUNIT_ASSERT_EQUAL_MESSAGE(
148 "world name not correctly transported in Join packet",
149 write_name, read_name
152 write_name = "0123456789012345678901234567890123456789";
153 pack.WriteWorldName(write_name);
154 pack.ReadWorldName(read_name);
155 CPPUNIT_ASSERT_EQUAL_MESSAGE(
156 "world name not correctly truncated in Join packet",
157 write_name.substr(0, 32), read_name
161 void PacketTest::testPart() {
162 auto pack = Packet::Make<Packet::Part>(udp_pack);
163 AssertPacket("Part", 3, 0, pack);
166 void PacketTest::testPlayerUpdate() {
167 auto pack = Packet::Make<Packet::PlayerUpdate>(udp_pack);
168 AssertPacket("PlayerUpdate", 4, 76, pack);
170 EntityState write_state;
171 write_state.chunk_pos = { 7, 2, -3 };
172 write_state.block_pos = { 1.5f, 0.9f, 12.0f };
173 write_state.velocity = { 0.025f, 0.001f, 0.0f };
174 write_state.orient = { 1.0f, 0.0f, 0.0f, 0.0f };
175 write_state.ang_vel = { 0.01f, 0.00302f, 0.0985f };
176 glm::vec3 write_movement(0.5f, -1.0f, 1.0f);
177 float write_pitch = 1.25f;
178 float write_yaw = -2.5f;
179 uint8_t write_actions = 0x05;
180 uint8_t write_slot = 3;
181 pack.WritePredictedState(write_state);
182 pack.WriteMovement(write_movement);
183 pack.WritePitch(write_pitch);
184 pack.WriteYaw(write_yaw);
185 pack.WriteActions(write_actions);
186 pack.WriteSlot(write_slot);
188 EntityState read_state;
189 glm::vec3 read_movement;
192 uint8_t read_actions;
194 pack.ReadPredictedState(read_state);
195 pack.ReadMovement(read_movement);
196 pack.ReadPitch(read_pitch);
197 pack.ReadYaw(read_yaw);
198 pack.ReadActions(read_actions);
199 pack.ReadSlot(read_slot);
201 "player predicted entity state not correctly transported in PlayerUpdate packet",
202 write_state, read_state
205 "player movement input not correctly transported in PlayerUpdate packet",
206 write_movement, read_movement, 0.0001f
208 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
209 "player pitch input not correctly transported in PlayerUpdate packet",
210 write_pitch, read_pitch, 0.0001f
212 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
213 "player yaw input not correctly transported in PlayerUpdate packet",
214 write_yaw, read_yaw, 0.0001f
216 CPPUNIT_ASSERT_EQUAL_MESSAGE(
217 "player actions not correctly transported in PlayerUpdate packet",
218 int(write_actions), int(read_actions)
220 CPPUNIT_ASSERT_EQUAL_MESSAGE(
221 "player inventory slot not correctly transported in PlayerUpdate packet",
222 int(write_slot), int(read_slot)
226 void PacketTest::testSpawnEntity() {
227 auto pack = Packet::Make<Packet::SpawnEntity>(udp_pack);
228 AssertPacket("SpawnEntity", 5, 100, 132, pack);
231 write_entity.ID(534574);
234 model.Instantiate(write_entity.GetModel());
235 write_entity.GetState().chunk_pos = { 7, 2, -3 };
236 write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
237 write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
238 write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
239 write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
240 write_entity.Bounds({{ -1, -1, -1 }, { 1, 1, 1 }});
241 write_entity.WorldCollidable(true);
242 write_entity.Name("blah");
243 pack.WriteEntity(write_entity);
246 uint32_t skeleton_id;
248 pack.ReadEntityID(entity_id);
249 pack.ReadSkeletonID(skeleton_id);
250 pack.ReadEntity(read_entity);
252 CPPUNIT_ASSERT_EQUAL_MESSAGE(
253 "entity ID not correctly transported in SpawnEntity packet",
254 write_entity.ID(), entity_id
256 CPPUNIT_ASSERT_EQUAL_MESSAGE(
257 "skeleton ID not correctly transported in SpawnEntity packet",
258 write_entity.GetModel().GetModel().ID(), skeleton_id
261 "entity state not correctly transported in PlayerUpdate packet",
262 write_entity.GetState(), read_entity.GetState()
265 "entity bounds not correctly transported in PlayerUpdate packet",
266 write_entity.Bounds(), read_entity.Bounds()
268 CPPUNIT_ASSERT_MESSAGE(
269 "entity flags not correctly transported in SpawnEntity packet",
270 read_entity.WorldCollidable()
272 CPPUNIT_ASSERT_EQUAL_MESSAGE(
273 "entity name not correctly transported in SpawnEntity packet",
274 write_entity.Name(), read_entity.Name()
278 void PacketTest::testDespawnEntity() {
279 auto pack = Packet::Make<Packet::DespawnEntity>(udp_pack);
280 AssertPacket("DespawnEntity", 6, 4, pack);
282 uint32_t write_id = 5437;
284 pack.WriteEntityID(write_id);
285 pack.ReadEntityID(read_id);
287 CPPUNIT_ASSERT_EQUAL_MESSAGE(
288 "entity ID not correctly transported in DespawnEntity packet",
293 void PacketTest::testEntityUpdate() {
294 auto pack = Packet::Make<Packet::EntityUpdate>(udp_pack);
295 AssertPacket("EntityUpdate", 7, 4, 480, pack);
297 pack.length = Packet::EntityUpdate::GetSize(3);
298 CPPUNIT_ASSERT_EQUAL_MESSAGE(
299 "length not correctly set in EntityUpdate packet",
300 size_t(4 + 3 * 68), pack.length
303 uint32_t write_count = 3;
305 pack.WriteEntityCount(write_count);
306 pack.ReadEntityCount(read_count);
307 CPPUNIT_ASSERT_EQUAL_MESSAGE(
308 "entity count not correctly transported in EntityUpdate packet",
309 write_count, read_count
313 write_entity.ID(8567234);
314 write_entity.GetState().chunk_pos = { 7, 2, -3 };
315 write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
316 write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
317 write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
318 write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
319 pack.WriteEntity(write_entity, 1);
320 pack.WriteEntity(write_entity, 0);
321 pack.WriteEntity(write_entity, 2);
324 EntityState read_state;
325 pack.ReadEntityID(read_id, 1);
326 pack.ReadEntityState(read_state, 1);
327 CPPUNIT_ASSERT_EQUAL_MESSAGE(
328 "entity ID not correctly transported in EntityUpdate packet",
329 write_entity.ID(), read_id
332 "entity state not correctly transported in EntityUpdate packet",
333 write_entity.GetState(), read_state
337 void PacketTest::testPlayerCorrection() {
338 auto pack = Packet::Make<Packet::PlayerCorrection>(udp_pack);
339 AssertPacket("PlayerCorrection", 8, 66, pack);
341 uint16_t write_seq = 50050;
343 pack.WritePacketSeq(write_seq);
344 pack.ReadPacketSeq(read_seq);
345 CPPUNIT_ASSERT_EQUAL_MESSAGE(
346 "packet sequence not correctly transported in PlayerCorrection packet",
351 write_entity.GetState().chunk_pos = { 7, 2, -3 };
352 write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
353 write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
354 write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
355 write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
356 pack.WritePlayer(write_entity);
358 EntityState read_state;
359 pack.ReadPlayerState(read_state);
361 "entity state not correctly transported in PlayerCorrection packet",
362 write_entity.GetState(), read_state
366 void PacketTest::testChunkBegin() {
367 auto pack = Packet::Make<Packet::ChunkBegin>(udp_pack);
368 AssertPacket("ChunkBegin", 9, 24, pack);
370 uint32_t write_id = 532;
371 uint32_t write_flags = 9864328;
372 glm::ivec3 write_pos = { -6, 15, 38 };
373 uint32_t write_size = 4097;
375 pack.WriteTransmissionId(write_id);
376 pack.WriteFlags(write_flags);
377 pack.WriteChunkCoords(write_pos);
378 pack.WriteDataSize(write_size);
385 pack.ReadTransmissionId(read_id);
386 pack.ReadFlags(read_flags);
387 pack.ReadChunkCoords(read_pos);
388 pack.ReadDataSize(read_size);
390 CPPUNIT_ASSERT_EQUAL_MESSAGE(
391 "transmission ID not correctly transported in ChunkBegin packet",
394 CPPUNIT_ASSERT_EQUAL_MESSAGE(
395 "flags not correctly transported in ChunkBegin packet",
396 write_flags, read_flags
399 "chunk coordinates not correctly transported in ChunkBegin packet",
402 CPPUNIT_ASSERT_EQUAL_MESSAGE(
403 "data size not correctly transported in ChunkBegin packet",
404 write_size, read_size
408 void PacketTest::testChunkData() {
409 auto pack = Packet::Make<Packet::ChunkData>(udp_pack);
410 AssertPacket("ChunkData", 10, 12, 484, pack);
412 constexpr size_t block_size = 97;
414 uint32_t write_id = 6743124;
415 uint32_t write_offset = 8583;
416 uint32_t write_size = block_size;
417 uint8_t write_data[block_size];
418 memset(write_data, 'X', block_size);
420 pack.WriteTransmissionId(write_id);
421 pack.WriteDataOffset(write_offset);
422 pack.WriteDataSize(write_size);
423 pack.WriteData(write_data, write_size);
426 uint32_t read_offset;
428 uint8_t read_data[block_size];
430 pack.ReadTransmissionId(read_id);
431 pack.ReadDataOffset(read_offset);
432 pack.ReadDataSize(read_size);
433 pack.ReadData(read_data, read_size);
435 CPPUNIT_ASSERT_EQUAL_MESSAGE(
436 "transmission ID not correctly transported in ChunkData packet",
439 CPPUNIT_ASSERT_EQUAL_MESSAGE(
440 "data offset not correctly transported in ChunkData packet",
441 write_offset, read_offset
443 CPPUNIT_ASSERT_EQUAL_MESSAGE(
444 "data size not correctly transported in ChunkData packet",
445 write_size, read_size
447 CPPUNIT_ASSERT_EQUAL_MESSAGE(
448 "raw data not correctly transported in ChunkData packet",
449 string(write_data, write_data + write_size), string(read_data, read_data + read_size)
453 void PacketTest::testBlockUpdate() {
454 auto pack = Packet::Make<Packet::BlockUpdate>(udp_pack);
455 AssertPacket("BlockUpdate", 11, 16, 484, pack);
457 pack.length = Packet::BlockUpdate::GetSize(3);
458 CPPUNIT_ASSERT_EQUAL_MESSAGE(
459 "length not correctly set in BlockUpdate packet",
460 size_t(16 + 3 * 6), pack.length
463 glm::ivec3 write_coords(432, -325, 99998);
464 uint32_t write_count = 3;
465 uint16_t write_index = 432;
466 Block write_block(324, Block::FACE_DOWN, Block::TURN_AROUND);
468 pack.WriteChunkCoords(write_coords);
469 pack.WriteBlockCount(write_count);
470 pack.WriteIndex(write_index, 1);
471 pack.WriteBlock(write_block, 1);
472 pack.WriteIndex(write_index, 0);
473 pack.WriteBlock(write_block, 0);
474 pack.WriteIndex(write_index, 2);
475 pack.WriteBlock(write_block, 2);
477 glm::ivec3 read_coords;
482 pack.ReadChunkCoords(read_coords);
483 pack.ReadBlockCount(read_count);
484 pack.ReadIndex(read_index, 1);
485 pack.ReadBlock(read_block, 1);
488 "chunk coordinates not correctly transported in BlockUpdate packet",
489 write_coords, read_coords
491 CPPUNIT_ASSERT_EQUAL_MESSAGE(
492 "block count not correctly transported in BlockUpdate packet",
493 write_count, read_count
495 CPPUNIT_ASSERT_EQUAL_MESSAGE(
496 "block index not correctly transported in BlockUpdate packet",
497 write_index, read_index
499 CPPUNIT_ASSERT_EQUAL_MESSAGE(
500 "block type not correctly transported in BlockUpdate packet",
501 write_block.type, read_block.type
503 CPPUNIT_ASSERT_EQUAL_MESSAGE(
504 "block face not correctly transported in BlockUpdate packet",
505 write_block.GetFace(), read_block.GetFace()
507 CPPUNIT_ASSERT_EQUAL_MESSAGE(
508 "block turn not correctly transported in BlockUpdate packet",
509 write_block.GetTurn(), read_block.GetTurn()
513 void PacketTest::testMessage() {
514 auto pack = Packet::Make<Packet::Message>(udp_pack);
515 AssertPacket("Message", 12, 6, 455, pack);
517 const uint8_t write_type = 1;
518 const uint32_t write_ref = 6433235;
519 const string write_msg("hello, world");
521 pack.length = Packet::Message::GetSize(write_msg);
522 CPPUNIT_ASSERT_EQUAL_MESSAGE(
523 "length not correctly set in BlockUpdate packet",
524 size_t(5 + write_msg.size() + 1), pack.length
527 pack.WriteType(write_type);
528 pack.WriteReferral(write_ref);
529 pack.WriteMessage(write_msg);
531 uint8_t read_type = 5;
532 uint32_t read_ref = 884373;
535 pack.ReadType(read_type);
536 pack.ReadReferral(read_ref);
537 pack.ReadMessage(read_msg);
539 CPPUNIT_ASSERT_EQUAL_MESSAGE(
540 "type not correctly transported in Message packet",
541 write_type, read_type
543 CPPUNIT_ASSERT_EQUAL_MESSAGE(
544 "referral not correctly transported in Message packet",
547 CPPUNIT_ASSERT_EQUAL_MESSAGE(
548 "message not correctly transported in Message packet",
554 void PacketTest::AssertPacket(
556 uint8_t expected_type,
557 size_t expected_length,
558 const Packet::Payload &actual
560 CPPUNIT_ASSERT_EQUAL_MESSAGE(
561 name + " packet not correctly tagged",
562 TEST_TAG, actual.GetHeader().tag
564 CPPUNIT_ASSERT_EQUAL_MESSAGE(
565 "wrong type code for " + name + " packet",
566 int(expected_type), int(actual.GetHeader().type)
568 CPPUNIT_ASSERT_EQUAL_MESSAGE(
569 "bad payload length for " + name + " packet",
570 expected_length, actual.length
574 void PacketTest::AssertPacket(
576 uint8_t expected_type,
579 const Packet::Payload &actual
581 CPPUNIT_ASSERT_EQUAL_MESSAGE(
582 name + " packet not correctly tagged",
583 TEST_TAG, actual.GetHeader().tag
585 CPPUNIT_ASSERT_EQUAL_MESSAGE(
586 "wrong type code for " + name + " packet",
587 expected_type, actual.GetHeader().type
589 CPPUNIT_ASSERT_MESSAGE(
590 "bad payload length for " + name + " packet",
591 actual.length >= min_length && actual.length <= max_length
595 void PacketTest::AssertEqual(
596 const string &message,
597 const EntityState &expected,
598 const EntityState &actual
601 message + ": bad chunk position",
602 expected.chunk_pos, actual.chunk_pos
605 message + ": bad block position",
606 expected.block_pos, actual.block_pos
609 message + ": bad velocity",
610 expected.velocity, actual.velocity
613 message + ": bad orientation",
614 expected.orient, actual.orient
617 message + ": bad angular velocity",
618 expected.ang_vel, actual.ang_vel
622 void PacketTest::AssertEqual(
623 const string &message,
624 const AABB &expected,
628 message + ": bad lower bound",
629 expected.min, actual.min
632 message + ": bad upper bound",
633 expected.max, actual.max
637 void PacketTest::AssertEqual(
638 const string &message,
639 const glm::ivec3 &expected,
640 const glm::ivec3 &actual
642 CPPUNIT_ASSERT_EQUAL_MESSAGE(
643 message + " (X component)",
646 CPPUNIT_ASSERT_EQUAL_MESSAGE(
647 message + " (Y component)",
650 CPPUNIT_ASSERT_EQUAL_MESSAGE(
651 message + " (Z component)",
656 void PacketTest::AssertEqual(
657 const string &message,
658 const glm::vec3 &expected,
659 const glm::vec3 &actual,
662 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
663 message + " (X component)",
664 expected.x, actual.x, epsilon
666 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
667 message + " (Y component)",
668 expected.y, actual.y, epsilon
670 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
671 message + " (Z component)",
672 expected.z, actual.z, epsilon
676 void PacketTest::AssertEqual(
677 const string &message,
678 const glm::quat &expected,
679 const glm::quat &actual
681 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
682 message + " (W component)",
683 expected.w, actual.w, numeric_limits<float>::epsilon()
685 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
686 message + " (X component)",
687 expected.x, actual.x, numeric_limits<float>::epsilon()
689 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
690 message + " (Y component)",
691 expected.y, actual.y, numeric_limits<float>::epsilon()
693 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
694 message + " (Z component)",
695 expected.z, actual.z, numeric_limits<float>::epsilon()