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)
39 void PacketTest::testControl() {
40 Packet::TControl ctrl{ 0, 10, 0 };
42 CPPUNIT_ASSERT_MESSAGE(
43 "TControl should ack the packet in the ack field",
46 CPPUNIT_ASSERT_MESSAGE(
47 "TControl should ack the packet in the future",
50 CPPUNIT_ASSERT_MESSAGE(
51 "TControl should not ack a packet in the distant past",
54 CPPUNIT_ASSERT_MESSAGE(
55 "TControl should not ack the previous packet if the bitfield is 0",
58 CPPUNIT_ASSERT_EQUAL_MESSAGE(
59 "TControl's acks should begin at the packet in the ack field",
60 uint16_t(10), ctrl.AckBegin()
62 CPPUNIT_ASSERT_EQUAL_MESSAGE(
63 "TControl's acks should end 33 packets before the one in the ack field",
64 uint16_t(-23), ctrl.AckEnd()
67 CPPUNIT_ASSERT_MESSAGE(
68 "TControl should ack the previous packet if the bitfield is 1",
72 CPPUNIT_ASSERT_MESSAGE(
73 "TControl should not ack the previous packet if the bitfield is 2",
76 CPPUNIT_ASSERT_MESSAGE(
77 "TControl should ack the packet before the previous one if the bitfield is 2",
82 void PacketTest::testPing() {
83 auto pack = Packet::Make<Packet::Ping>(udp_pack);
84 AssertPacket("Ping", 0, 0, pack);
87 void PacketTest::testLogin() {
88 auto pack = Packet::Make<Packet::Login>(udp_pack);
89 AssertPacket("Login", 1, 0, 32, pack);
91 string write_name = "test";
93 pack.WritePlayerName(write_name);
94 pack.ReadPlayerName(read_name);
95 CPPUNIT_ASSERT_EQUAL_MESSAGE(
96 "player name not correctly transported in Login packet",
100 write_name = "0123456789012345678901234567890123456789";
101 pack.WritePlayerName(write_name);
102 pack.ReadPlayerName(read_name);
103 CPPUNIT_ASSERT_EQUAL_MESSAGE(
104 "player name not correctly truncated in Login packet",
105 write_name.substr(0, 32), read_name
109 void PacketTest::testJoin() {
110 auto pack = Packet::Make<Packet::Join>(udp_pack);
111 AssertPacket("Join", 2, 54, 86, pack);
114 write_entity.ID(534574);
115 write_entity.GetState().chunk_pos = { 7, 2, -3 };
116 write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
117 write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
118 write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
119 write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
120 uint32_t read_id = 0;
121 EntityState read_state;
122 pack.WritePlayer(write_entity);
124 pack.ReadPlayerID(read_id);
125 CPPUNIT_ASSERT_EQUAL_MESSAGE(
126 "player entity ID not correctly transported in Join packet",
127 write_entity.ID(), read_id
129 pack.ReadPlayerState(read_state);
131 "player entity state not correctly transported in Join packet",
132 write_entity.GetState(), read_state
135 string write_name = "test";
137 pack.WriteWorldName(write_name);
138 pack.ReadWorldName(read_name);
139 CPPUNIT_ASSERT_EQUAL_MESSAGE(
140 "world name not correctly transported in Join packet",
141 write_name, read_name
144 write_name = "0123456789012345678901234567890123456789";
145 pack.WriteWorldName(write_name);
146 pack.ReadWorldName(read_name);
147 CPPUNIT_ASSERT_EQUAL_MESSAGE(
148 "world name not correctly truncated in Join packet",
149 write_name.substr(0, 32), read_name
153 void PacketTest::testPart() {
154 auto pack = Packet::Make<Packet::Part>(udp_pack);
155 AssertPacket("Part", 3, 0, pack);
158 void PacketTest::testPlayerUpdate() {
159 auto pack = Packet::Make<Packet::PlayerUpdate>(udp_pack);
160 AssertPacket("PlayerUpdate", 4, 62, pack);
162 EntityState write_state;
163 write_state.chunk_pos = { 7, 2, -3 };
164 write_state.block_pos = { 1.5f, 0.9f, 12.0f };
165 write_state.velocity = { 0.025f, 0.001f, 0.0f };
166 write_state.orient = { 1.0f, 0.0f, 0.0f, 0.0f };
167 write_state.ang_vel = { 0.01f, 0.00302f, 0.0985f };
168 glm::vec3 write_movement(0.5f, -1.0f, 1.0f);
169 float write_pitch = 1.25f;
170 float write_yaw = -2.5f;
171 uint8_t write_actions = 0x05;
172 uint8_t write_slot = 3;
173 pack.WritePredictedState(write_state);
174 pack.WriteMovement(write_movement);
175 pack.WritePitch(write_pitch);
176 pack.WriteYaw(write_yaw);
177 pack.WriteActions(write_actions);
178 pack.WriteSlot(write_slot);
180 EntityState read_state;
181 glm::vec3 read_movement;
184 uint8_t read_actions;
186 pack.ReadPredictedState(read_state);
187 pack.ReadMovement(read_movement);
188 pack.ReadPitch(read_pitch);
189 pack.ReadYaw(read_yaw);
190 pack.ReadActions(read_actions);
191 pack.ReadSlot(read_slot);
193 "player predicted entity state not correctly transported in PlayerUpdate packet",
194 write_state, read_state
197 "player movement input not correctly transported in PlayerUpdate packet",
198 write_movement, read_movement, 0.0001f
200 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
201 "player pitch input not correctly transported in PlayerUpdate packet",
202 write_pitch, read_pitch, 0.0001f
204 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
205 "player yaw input not correctly transported in PlayerUpdate packet",
206 write_yaw, read_yaw, 0.0001f
208 CPPUNIT_ASSERT_EQUAL_MESSAGE(
209 "player actions not correctly transported in PlayerUpdate packet",
210 int(write_actions), int(read_actions)
212 CPPUNIT_ASSERT_EQUAL_MESSAGE(
213 "player inventory slot not correctly transported in PlayerUpdate packet",
214 int(write_slot), int(read_slot)
218 void PacketTest::testSpawnEntity() {
219 auto pack = Packet::Make<Packet::SpawnEntity>(udp_pack);
220 AssertPacket("SpawnEntity", 5, 87, 118, pack);
223 write_entity.ID(534574);
226 model.Instantiate(write_entity.GetModel());
227 write_entity.GetState().chunk_pos = { 7, 2, -3 };
228 write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
229 write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
230 write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
231 write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
232 write_entity.Bounds({{ -1, -1, -1 }, { 1, 1, 1 }});
233 write_entity.WorldCollidable(true);
234 write_entity.Name("blah");
235 pack.WriteEntity(write_entity);
240 pack.ReadEntityID(entity_id);
241 pack.ReadModelID(model_id);
242 pack.ReadEntity(read_entity);
244 CPPUNIT_ASSERT_EQUAL_MESSAGE(
245 "entity ID not correctly transported in SpawnEntity packet",
246 write_entity.ID(), entity_id
248 CPPUNIT_ASSERT_EQUAL_MESSAGE(
249 "model ID not correctly transported in SpawnEntity packet",
250 write_entity.GetModel().GetModel().ID(), model_id
253 "entity state not correctly transported in PlayerUpdate packet",
254 write_entity.GetState(), read_entity.GetState()
257 "entity bounds not correctly transported in PlayerUpdate packet",
258 write_entity.Bounds(), read_entity.Bounds()
260 CPPUNIT_ASSERT_MESSAGE(
261 "entity flags not correctly transported in SpawnEntity packet",
262 read_entity.WorldCollidable()
264 CPPUNIT_ASSERT_EQUAL_MESSAGE(
265 "entity name not correctly transported in SpawnEntity packet",
266 write_entity.Name(), read_entity.Name()
270 void PacketTest::testDespawnEntity() {
271 auto pack = Packet::Make<Packet::DespawnEntity>(udp_pack);
272 AssertPacket("DespawnEntity", 6, 4, pack);
274 uint32_t write_id = 5437;
276 pack.WriteEntityID(write_id);
277 pack.ReadEntityID(read_id);
279 CPPUNIT_ASSERT_EQUAL_MESSAGE(
280 "entity ID not correctly transported in DespawnEntity packet",
285 void PacketTest::testEntityUpdate() {
286 auto pack = Packet::Make<Packet::EntityUpdate>(udp_pack);
287 AssertPacket("EntityUpdate", 7, 16, 466, pack);
289 pack.length = Packet::EntityUpdate::GetSize(3);
290 CPPUNIT_ASSERT_EQUAL_MESSAGE(
291 "length not correctly set in EntityUpdate packet",
292 size_t(16 + 3 * 45), pack.length
295 uint32_t write_count = 3;
296 glm::ivec3 write_base(8, -15, 1);
297 pack.WriteEntityCount(write_count);
298 pack.WriteChunkBase(write_base);
301 glm::ivec3 read_base;
302 pack.ReadEntityCount(read_count);
303 pack.ReadChunkBase(read_base);
305 CPPUNIT_ASSERT_EQUAL_MESSAGE(
306 "entity count not correctly transported in EntityUpdate packet",
307 write_count, read_count
310 "chunk base not correctly transported in EntityUpdate packet",
311 write_base, read_base
315 write_entity.ID(8567234);
316 write_entity.GetState().chunk_pos = { 7, 2, -3 };
317 write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
318 write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
319 write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
320 write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
321 pack.WriteEntity(write_entity, write_base, 1);
322 pack.WriteEntity(write_entity, write_base, 0);
323 pack.WriteEntity(write_entity, write_base, 2);
326 EntityState read_state;
327 pack.ReadEntityID(read_id, 1);
328 pack.ReadEntityState(read_state, write_base, 1);
329 CPPUNIT_ASSERT_EQUAL_MESSAGE(
330 "entity ID not correctly transported in EntityUpdate packet",
331 write_entity.ID(), read_id
334 "entity state not correctly transported in EntityUpdate packet",
335 write_entity.GetState(), read_state
339 void PacketTest::testPlayerCorrection() {
340 auto pack = Packet::Make<Packet::PlayerCorrection>(udp_pack);
341 AssertPacket("PlayerCorrection", 8, 52, pack);
343 uint16_t write_seq = 50050;
345 pack.WritePacketSeq(write_seq);
346 pack.ReadPacketSeq(read_seq);
347 CPPUNIT_ASSERT_EQUAL_MESSAGE(
348 "packet sequence not correctly transported in PlayerCorrection packet",
353 write_entity.GetState().chunk_pos = { 7, 2, -3 };
354 write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
355 write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
356 write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
357 write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
358 pack.WritePlayer(write_entity);
360 EntityState read_state;
361 pack.ReadPlayerState(read_state);
363 "entity state not correctly transported in PlayerCorrection packet",
364 write_entity.GetState(), read_state
368 void PacketTest::testChunkBegin() {
369 auto pack = Packet::Make<Packet::ChunkBegin>(udp_pack);
370 AssertPacket("ChunkBegin", 9, 24, pack);
372 uint32_t write_id = 532;
373 uint32_t write_flags = 9864328;
374 glm::ivec3 write_pos = { -6, 15, 38 };
375 uint32_t write_size = 4097;
377 pack.WriteTransmissionId(write_id);
378 pack.WriteFlags(write_flags);
379 pack.WriteChunkCoords(write_pos);
380 pack.WriteDataSize(write_size);
387 pack.ReadTransmissionId(read_id);
388 pack.ReadFlags(read_flags);
389 pack.ReadChunkCoords(read_pos);
390 pack.ReadDataSize(read_size);
392 CPPUNIT_ASSERT_EQUAL_MESSAGE(
393 "transmission ID not correctly transported in ChunkBegin packet",
396 CPPUNIT_ASSERT_EQUAL_MESSAGE(
397 "flags not correctly transported in ChunkBegin packet",
398 write_flags, read_flags
401 "chunk coordinates not correctly transported in ChunkBegin packet",
404 CPPUNIT_ASSERT_EQUAL_MESSAGE(
405 "data size not correctly transported in ChunkBegin packet",
406 write_size, read_size
410 void PacketTest::testChunkData() {
411 auto pack = Packet::Make<Packet::ChunkData>(udp_pack);
412 AssertPacket("ChunkData", 10, 12, 484, pack);
414 constexpr size_t block_size = 97;
416 uint32_t write_id = 6743124;
417 uint32_t write_offset = 8583;
418 uint32_t write_size = block_size;
419 uint8_t write_data[block_size];
420 memset(write_data, 'X', block_size);
422 pack.WriteTransmissionId(write_id);
423 pack.WriteDataOffset(write_offset);
424 pack.WriteDataSize(write_size);
425 pack.WriteData(write_data, write_size);
428 uint32_t read_offset;
430 uint8_t read_data[block_size];
432 pack.ReadTransmissionId(read_id);
433 pack.ReadDataOffset(read_offset);
434 pack.ReadDataSize(read_size);
435 pack.ReadData(read_data, read_size);
437 CPPUNIT_ASSERT_EQUAL_MESSAGE(
438 "transmission ID not correctly transported in ChunkData packet",
441 CPPUNIT_ASSERT_EQUAL_MESSAGE(
442 "data offset not correctly transported in ChunkData packet",
443 write_offset, read_offset
445 CPPUNIT_ASSERT_EQUAL_MESSAGE(
446 "data size not correctly transported in ChunkData packet",
447 write_size, read_size
449 CPPUNIT_ASSERT_EQUAL_MESSAGE(
450 "raw data not correctly transported in ChunkData packet",
451 string(write_data, write_data + write_size), string(read_data, read_data + read_size)
455 void PacketTest::testBlockUpdate() {
456 auto pack = Packet::Make<Packet::BlockUpdate>(udp_pack);
457 AssertPacket("BlockUpdate", 11, 16, 484, pack);
459 pack.length = Packet::BlockUpdate::GetSize(3);
460 CPPUNIT_ASSERT_EQUAL_MESSAGE(
461 "length not correctly set in BlockUpdate packet",
462 size_t(16 + 3 * 6), pack.length
465 glm::ivec3 write_coords(432, -325, 99998);
466 uint32_t write_count = 3;
467 uint16_t write_index = 432;
468 Block write_block(324, Block::FACE_DOWN, Block::TURN_AROUND);
470 pack.WriteChunkCoords(write_coords);
471 pack.WriteBlockCount(write_count);
472 pack.WriteIndex(write_index, 1);
473 pack.WriteBlock(write_block, 1);
474 pack.WriteIndex(write_index, 0);
475 pack.WriteBlock(write_block, 0);
476 pack.WriteIndex(write_index, 2);
477 pack.WriteBlock(write_block, 2);
479 glm::ivec3 read_coords;
484 pack.ReadChunkCoords(read_coords);
485 pack.ReadBlockCount(read_count);
486 pack.ReadIndex(read_index, 1);
487 pack.ReadBlock(read_block, 1);
490 "chunk coordinates not correctly transported in BlockUpdate packet",
491 write_coords, read_coords
493 CPPUNIT_ASSERT_EQUAL_MESSAGE(
494 "block count not correctly transported in BlockUpdate packet",
495 write_count, read_count
497 CPPUNIT_ASSERT_EQUAL_MESSAGE(
498 "block index not correctly transported in BlockUpdate packet",
499 write_index, read_index
501 CPPUNIT_ASSERT_EQUAL_MESSAGE(
502 "block type not correctly transported in BlockUpdate packet",
503 write_block.type, read_block.type
505 CPPUNIT_ASSERT_EQUAL_MESSAGE(
506 "block face not correctly transported in BlockUpdate packet",
507 write_block.GetFace(), read_block.GetFace()
509 CPPUNIT_ASSERT_EQUAL_MESSAGE(
510 "block turn not correctly transported in BlockUpdate packet",
511 write_block.GetTurn(), read_block.GetTurn()
515 void PacketTest::testMessage() {
516 auto pack = Packet::Make<Packet::Message>(udp_pack);
517 AssertPacket("Message", 12, 6, 455, pack);
519 const uint8_t write_type = 1;
520 const uint32_t write_ref = 6433235;
521 const string write_msg("hello, world");
523 pack.length = Packet::Message::GetSize(write_msg);
524 CPPUNIT_ASSERT_EQUAL_MESSAGE(
525 "length not correctly set in BlockUpdate packet",
526 size_t(5 + write_msg.size() + 1), pack.length
529 pack.WriteType(write_type);
530 pack.WriteReferral(write_ref);
531 pack.WriteMessage(write_msg);
533 uint8_t read_type = 5;
534 uint32_t read_ref = 884373;
537 pack.ReadType(read_type);
538 pack.ReadReferral(read_ref);
539 pack.ReadMessage(read_msg);
541 CPPUNIT_ASSERT_EQUAL_MESSAGE(
542 "type not correctly transported in Message packet",
543 write_type, read_type
545 CPPUNIT_ASSERT_EQUAL_MESSAGE(
546 "referral not correctly transported in Message packet",
549 CPPUNIT_ASSERT_EQUAL_MESSAGE(
550 "message not correctly transported in Message packet",
556 void PacketTest::AssertPacket(
558 uint8_t expected_type,
559 size_t expected_length,
560 const Packet::Payload &actual
562 CPPUNIT_ASSERT_EQUAL_MESSAGE(
563 name + " packet not correctly tagged",
564 TEST_TAG, actual.GetHeader().tag
566 CPPUNIT_ASSERT_EQUAL_MESSAGE(
567 "wrong type code for " + name + " packet",
568 int(expected_type), int(actual.GetHeader().type)
570 CPPUNIT_ASSERT_EQUAL_MESSAGE(
571 "bad payload length for " + name + " packet",
572 expected_length, actual.length
576 void PacketTest::AssertPacket(
578 uint8_t expected_type,
581 const Packet::Payload &actual
583 CPPUNIT_ASSERT_EQUAL_MESSAGE(
584 name + " packet not correctly tagged",
585 TEST_TAG, actual.GetHeader().tag
587 CPPUNIT_ASSERT_EQUAL_MESSAGE(
588 "wrong type code for " + name + " packet",
589 expected_type, actual.GetHeader().type
591 CPPUNIT_ASSERT_MESSAGE(
592 "bad payload length for " + name + " packet",
593 actual.length >= min_length && actual.length <= max_length
597 void PacketTest::AssertEqual(
598 const string &message,
599 const EntityState &expected,
600 const EntityState &actual
603 message + ": bad chunk position",
604 expected.chunk_pos, actual.chunk_pos
607 message + ": bad block position",
608 expected.block_pos, actual.block_pos, 16.0f/65535.0f // that's about the max accuracy that packing's going to give us
611 message + ": bad velocity",
612 expected.velocity, actual.velocity
615 message + ": bad orientation",
616 expected.orient, actual.orient
619 message + ": bad angular velocity",
620 expected.ang_vel, actual.ang_vel
624 void PacketTest::AssertEqual(
625 const string &message,
626 const AABB &expected,
630 message + ": bad lower bound",
631 expected.min, actual.min
634 message + ": bad upper bound",
635 expected.max, actual.max
639 void PacketTest::AssertEqual(
640 const string &message,
641 const glm::ivec3 &expected,
642 const glm::ivec3 &actual
644 CPPUNIT_ASSERT_EQUAL_MESSAGE(
645 message + " (X component)",
648 CPPUNIT_ASSERT_EQUAL_MESSAGE(
649 message + " (Y component)",
652 CPPUNIT_ASSERT_EQUAL_MESSAGE(
653 message + " (Z component)",
658 void PacketTest::AssertEqual(
659 const string &message,
660 const glm::vec3 &expected,
661 const glm::vec3 &actual,
664 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
665 message + " (X component)",
666 expected.x, actual.x, epsilon
668 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
669 message + " (Y component)",
670 expected.y, actual.y, epsilon
672 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
673 message + " (Z component)",
674 expected.z, actual.z, epsilon
678 void PacketTest::AssertEqual(
679 const string &message,
680 const glm::quat &expected,
681 const glm::quat &actual
683 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
684 message + " (W component)",
685 expected.w, actual.w, numeric_limits<float>::epsilon()
687 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
688 message + " (X component)",
689 expected.x, actual.x, numeric_limits<float>::epsilon()
691 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
692 message + " (Y component)",
693 expected.y, actual.y, numeric_limits<float>::epsilon()
695 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
696 message + " (Z component)",
697 expected.z, actual.z, numeric_limits<float>::epsilon()