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, 47, 78, pack);
114 write_entity.ID(534574);
115 EntityState write_state;
116 write_state.chunk_pos = { 7, 2, -3 };
117 write_state.block_pos = { 1.5f, 0.9f, 12.0f };
118 write_state.velocity = { 0.025f, 0.001f, 0.0f };
119 write_state.orient = { 1.0f, 0.0f, 0.0f, 0.0f };
120 write_state.pitch = 0.3f;
121 write_state.yaw = -2.3f;
122 write_entity.SetState(write_state);
123 uint32_t read_id = 0;
124 EntityState read_state;
125 pack.WritePlayer(write_entity);
127 pack.ReadPlayerID(read_id);
128 CPPUNIT_ASSERT_EQUAL_MESSAGE(
129 "player entity ID not correctly transported in Join packet",
130 write_entity.ID(), read_id
132 pack.ReadPlayerState(read_state);
134 "player entity state not correctly transported in Join packet",
135 write_entity.GetState(), read_state
138 string write_name = "test";
140 pack.WriteWorldName(write_name);
141 pack.ReadWorldName(read_name);
142 CPPUNIT_ASSERT_EQUAL_MESSAGE(
143 "world name not correctly transported in Join packet",
144 write_name, read_name
147 write_name = "0123456789012345678901234567890123456789";
148 pack.WriteWorldName(write_name);
149 pack.ReadWorldName(read_name);
150 CPPUNIT_ASSERT_EQUAL_MESSAGE(
151 "world name not correctly truncated in Join packet",
152 write_name.substr(0, 32), read_name
156 void PacketTest::testPart() {
157 auto pack = Packet::Make<Packet::Part>(udp_pack);
158 AssertPacket("Part", 3, 0, pack);
161 void PacketTest::testPlayerUpdate() {
162 auto pack = Packet::Make<Packet::PlayerUpdate>(udp_pack);
163 AssertPacket("PlayerUpdate", 4, 50, pack);
165 EntityState write_state;
166 write_state.chunk_pos = { 7, 2, -3 };
167 write_state.block_pos = { 1.5f, 0.9f, 12.0f };
168 write_state.velocity = { 0.025f, 0.001f, 0.0f };
169 write_state.orient = { 1.0f, 0.0f, 0.0f, 0.0f };
170 glm::vec3 write_movement(0.5f, -1.0f, 1.0f);
171 uint8_t write_actions = 0x05;
172 uint8_t write_slot = 3;
173 pack.WritePredictedState(write_state);
174 pack.WriteMovement(write_movement);
175 pack.WriteActions(write_actions);
176 pack.WriteSlot(write_slot);
178 EntityState read_state;
179 glm::vec3 read_movement;
180 uint8_t read_actions;
182 pack.ReadPredictedState(read_state);
183 pack.ReadMovement(read_movement);
184 pack.ReadActions(read_actions);
185 pack.ReadSlot(read_slot);
187 "player predicted entity state not correctly transported in PlayerUpdate packet",
188 write_state, read_state
191 "player movement input not correctly transported in PlayerUpdate packet",
192 write_movement, read_movement, 0.0001f
194 CPPUNIT_ASSERT_EQUAL_MESSAGE(
195 "player actions not correctly transported in PlayerUpdate packet",
196 int(write_actions), int(read_actions)
198 CPPUNIT_ASSERT_EQUAL_MESSAGE(
199 "player inventory slot not correctly transported in PlayerUpdate packet",
200 int(write_slot), int(read_slot)
204 void PacketTest::testSpawnEntity() {
205 auto pack = Packet::Make<Packet::SpawnEntity>(udp_pack);
206 AssertPacket("SpawnEntity", 5, 79, 110, pack);
209 write_entity.ID(534574);
213 model.Instantiate(write_entity.GetModel());
214 EntityState write_state;
215 write_state.chunk_pos = { 7, 2, -3 };
216 write_state.block_pos = { 1.5f, 0.9f, 12.0f };
217 write_state.velocity = { 0.025f, 0.001f, 0.0f };
218 write_state.orient = { 1.0f, 0.0f, 0.0f, 0.0f };
219 write_state.pitch = 0.3f;
220 write_state.yaw = -2.3f;
221 write_entity.SetState(write_state);
222 write_entity.Bounds({{ -1, -1, -1 }, { 1, 1, 1 }});
223 write_entity.WorldCollidable(true);
224 write_entity.Name("blah");
225 pack.WriteEntity(write_entity);
230 pack.ReadEntityID(entity_id);
231 pack.ReadModelID(model_id);
232 pack.ReadEntity(read_entity);
234 CPPUNIT_ASSERT_EQUAL_MESSAGE(
235 "entity ID not correctly transported in SpawnEntity packet",
236 write_entity.ID(), entity_id
238 CPPUNIT_ASSERT_EQUAL_MESSAGE(
239 "model ID not correctly transported in SpawnEntity packet",
240 write_entity.GetModel().GetModel().ID(), model_id
243 "entity state not correctly transported in PlayerUpdate packet",
244 write_entity.GetState(), read_entity.GetState()
247 "entity bounds not correctly transported in PlayerUpdate packet",
248 write_entity.Bounds(), read_entity.Bounds()
250 CPPUNIT_ASSERT_MESSAGE(
251 "entity flags not correctly transported in SpawnEntity packet",
252 read_entity.WorldCollidable()
254 CPPUNIT_ASSERT_EQUAL_MESSAGE(
255 "entity name not correctly transported in SpawnEntity packet",
256 write_entity.Name(), read_entity.Name()
260 void PacketTest::testDespawnEntity() {
261 auto pack = Packet::Make<Packet::DespawnEntity>(udp_pack);
262 AssertPacket("DespawnEntity", 6, 4, pack);
264 uint32_t write_id = 5437;
266 pack.WriteEntityID(write_id);
267 pack.ReadEntityID(read_id);
269 CPPUNIT_ASSERT_EQUAL_MESSAGE(
270 "entity ID not correctly transported in DespawnEntity packet",
275 void PacketTest::testEntityUpdate() {
276 auto pack = Packet::Make<Packet::EntityUpdate>(udp_pack);
277 AssertPacket("EntityUpdate", 7, 16, 460, pack);
279 pack.length = Packet::EntityUpdate::GetSize(3);
280 CPPUNIT_ASSERT_EQUAL_MESSAGE(
281 "length not correctly set in EntityUpdate packet",
282 size_t(16 + 3 * 37), pack.length
285 uint32_t write_count = 3;
286 glm::ivec3 write_base(8, -15, 1);
287 pack.WriteEntityCount(write_count);
288 pack.WriteChunkBase(write_base);
291 glm::ivec3 read_base;
292 pack.ReadEntityCount(read_count);
293 pack.ReadChunkBase(read_base);
295 CPPUNIT_ASSERT_EQUAL_MESSAGE(
296 "entity count not correctly transported in EntityUpdate packet",
297 write_count, read_count
300 "chunk base not correctly transported in EntityUpdate packet",
301 write_base, read_base
305 write_entity.ID(8567234);
306 EntityState write_state;
307 write_state.chunk_pos = { 7, 2, -3 };
308 write_state.block_pos = { 1.5f, 0.9f, 12.0f };
309 write_state.velocity = { 0.025f, 0.001f, 0.0f };
310 write_state.orient = { 1.0f, 0.0f, 0.0f, 0.0f };
311 write_state.pitch = 0.3f;
312 write_state.yaw = -2.3f;
313 write_entity.SetState(write_state);
314 pack.WriteEntity(write_entity, write_base, 1);
315 pack.WriteEntity(write_entity, write_base, 0);
316 pack.WriteEntity(write_entity, write_base, 2);
319 EntityState read_state;
320 pack.ReadEntityID(read_id, 1);
321 pack.ReadEntityState(read_state, write_base, 1);
322 CPPUNIT_ASSERT_EQUAL_MESSAGE(
323 "entity ID not correctly transported in EntityUpdate packet",
324 write_entity.ID(), read_id
327 "entity state not correctly transported in EntityUpdate packet",
328 write_entity.GetState(), read_state
332 void PacketTest::testPlayerCorrection() {
333 auto pack = Packet::Make<Packet::PlayerCorrection>(udp_pack);
334 AssertPacket("PlayerCorrection", 8, 44, pack);
336 uint16_t write_seq = 50050;
338 pack.WritePacketSeq(write_seq);
339 pack.ReadPacketSeq(read_seq);
340 CPPUNIT_ASSERT_EQUAL_MESSAGE(
341 "packet sequence not correctly transported in PlayerCorrection packet",
346 EntityState write_state;
347 write_state.chunk_pos = { 7, 2, -3 };
348 write_state.block_pos = { 1.5f, 0.9f, 12.0f };
349 write_state.velocity = { 0.025f, 0.001f, 0.0f };
350 write_state.orient = { 1.0f, 0.0f, 0.0f, 0.0f };
351 write_state.pitch = 0.3f;
352 write_state.yaw = -2.3f;
353 write_entity.SetState(write_state);
354 pack.WritePlayer(write_entity);
356 EntityState read_state;
357 pack.ReadPlayerState(read_state);
359 "entity state not correctly transported in PlayerCorrection packet",
360 write_entity.GetState(), read_state
364 void PacketTest::testChunkBegin() {
365 auto pack = Packet::Make<Packet::ChunkBegin>(udp_pack);
366 AssertPacket("ChunkBegin", 9, 24, pack);
368 uint32_t write_id = 532;
369 uint32_t write_flags = 9864328;
370 glm::ivec3 write_pos = { -6, 15, 38 };
371 uint32_t write_size = 4097;
373 pack.WriteTransmissionId(write_id);
374 pack.WriteFlags(write_flags);
375 pack.WriteChunkCoords(write_pos);
376 pack.WriteDataSize(write_size);
383 pack.ReadTransmissionId(read_id);
384 pack.ReadFlags(read_flags);
385 pack.ReadChunkCoords(read_pos);
386 pack.ReadDataSize(read_size);
388 CPPUNIT_ASSERT_EQUAL_MESSAGE(
389 "transmission ID not correctly transported in ChunkBegin packet",
392 CPPUNIT_ASSERT_EQUAL_MESSAGE(
393 "flags not correctly transported in ChunkBegin packet",
394 write_flags, read_flags
397 "chunk coordinates not correctly transported in ChunkBegin packet",
400 CPPUNIT_ASSERT_EQUAL_MESSAGE(
401 "data size not correctly transported in ChunkBegin packet",
402 write_size, read_size
406 void PacketTest::testChunkData() {
407 auto pack = Packet::Make<Packet::ChunkData>(udp_pack);
408 AssertPacket("ChunkData", 10, 12, 484, pack);
410 constexpr size_t block_size = 97;
412 uint32_t write_id = 6743124;
413 uint32_t write_offset = 8583;
414 uint32_t write_size = block_size;
415 uint8_t write_data[block_size];
416 memset(write_data, 'X', block_size);
418 pack.WriteTransmissionId(write_id);
419 pack.WriteDataOffset(write_offset);
420 pack.WriteDataSize(write_size);
421 pack.WriteData(write_data, write_size);
424 uint32_t read_offset;
426 uint8_t read_data[block_size];
428 pack.ReadTransmissionId(read_id);
429 pack.ReadDataOffset(read_offset);
430 pack.ReadDataSize(read_size);
431 pack.ReadData(read_data, read_size);
433 CPPUNIT_ASSERT_EQUAL_MESSAGE(
434 "transmission ID not correctly transported in ChunkData packet",
437 CPPUNIT_ASSERT_EQUAL_MESSAGE(
438 "data offset not correctly transported in ChunkData packet",
439 write_offset, read_offset
441 CPPUNIT_ASSERT_EQUAL_MESSAGE(
442 "data size not correctly transported in ChunkData packet",
443 write_size, read_size
445 CPPUNIT_ASSERT_EQUAL_MESSAGE(
446 "raw data not correctly transported in ChunkData packet",
447 string(write_data, write_data + write_size), string(read_data, read_data + read_size)
451 void PacketTest::testBlockUpdate() {
452 auto pack = Packet::Make<Packet::BlockUpdate>(udp_pack);
453 AssertPacket("BlockUpdate", 11, 16, 484, pack);
455 pack.length = Packet::BlockUpdate::GetSize(3);
456 CPPUNIT_ASSERT_EQUAL_MESSAGE(
457 "length not correctly set in BlockUpdate packet",
458 size_t(16 + 3 * 6), pack.length
461 glm::ivec3 write_coords(432, -325, 99998);
462 uint32_t write_count = 3;
463 uint16_t write_index = 432;
464 Block write_block(324, Block::FACE_DOWN, Block::TURN_AROUND);
466 pack.WriteChunkCoords(write_coords);
467 pack.WriteBlockCount(write_count);
468 pack.WriteIndex(write_index, 1);
469 pack.WriteBlock(write_block, 1);
470 pack.WriteIndex(write_index, 0);
471 pack.WriteBlock(write_block, 0);
472 pack.WriteIndex(write_index, 2);
473 pack.WriteBlock(write_block, 2);
475 glm::ivec3 read_coords;
480 pack.ReadChunkCoords(read_coords);
481 pack.ReadBlockCount(read_count);
482 pack.ReadIndex(read_index, 1);
483 pack.ReadBlock(read_block, 1);
486 "chunk coordinates not correctly transported in BlockUpdate packet",
487 write_coords, read_coords
489 CPPUNIT_ASSERT_EQUAL_MESSAGE(
490 "block count not correctly transported in BlockUpdate packet",
491 write_count, read_count
493 CPPUNIT_ASSERT_EQUAL_MESSAGE(
494 "block index not correctly transported in BlockUpdate packet",
495 write_index, read_index
497 CPPUNIT_ASSERT_EQUAL_MESSAGE(
498 "block type not correctly transported in BlockUpdate packet",
499 write_block.type, read_block.type
501 CPPUNIT_ASSERT_EQUAL_MESSAGE(
502 "block face not correctly transported in BlockUpdate packet",
503 write_block.GetFace(), read_block.GetFace()
505 CPPUNIT_ASSERT_EQUAL_MESSAGE(
506 "block turn not correctly transported in BlockUpdate packet",
507 write_block.GetTurn(), read_block.GetTurn()
511 void PacketTest::testMessage() {
512 auto pack = Packet::Make<Packet::Message>(udp_pack);
513 AssertPacket("Message", 12, 6, 455, pack);
515 const uint8_t write_type = 1;
516 const uint32_t write_ref = 6433235;
517 const string write_msg("hello, world");
519 pack.length = Packet::Message::GetSize(write_msg);
520 CPPUNIT_ASSERT_EQUAL_MESSAGE(
521 "length not correctly set in BlockUpdate packet",
522 size_t(5 + write_msg.size() + 1), pack.length
525 pack.WriteType(write_type);
526 pack.WriteReferral(write_ref);
527 pack.WriteMessage(write_msg);
529 uint8_t read_type = 5;
530 uint32_t read_ref = 884373;
533 pack.ReadType(read_type);
534 pack.ReadReferral(read_ref);
535 pack.ReadMessage(read_msg);
537 CPPUNIT_ASSERT_EQUAL_MESSAGE(
538 "type not correctly transported in Message packet",
539 write_type, read_type
541 CPPUNIT_ASSERT_EQUAL_MESSAGE(
542 "referral not correctly transported in Message packet",
545 CPPUNIT_ASSERT_EQUAL_MESSAGE(
546 "message not correctly transported in Message packet",
552 void PacketTest::AssertPacket(
554 uint8_t expected_type,
555 size_t expected_length,
556 const Packet::Payload &actual
558 CPPUNIT_ASSERT_EQUAL_MESSAGE(
559 name + " packet not correctly tagged",
560 TEST_TAG, actual.GetHeader().tag
562 CPPUNIT_ASSERT_EQUAL_MESSAGE(
563 "wrong type code for " + name + " packet",
564 int(expected_type), int(actual.GetHeader().type)
566 CPPUNIT_ASSERT_EQUAL_MESSAGE(
567 "bad payload length for " + name + " packet",
568 expected_length, actual.length
572 void PacketTest::AssertPacket(
574 uint8_t expected_type,
577 const Packet::Payload &actual
579 CPPUNIT_ASSERT_EQUAL_MESSAGE(
580 name + " packet not correctly tagged",
581 TEST_TAG, actual.GetHeader().tag
583 CPPUNIT_ASSERT_EQUAL_MESSAGE(
584 "wrong type code for " + name + " packet",
585 expected_type, actual.GetHeader().type
587 CPPUNIT_ASSERT_MESSAGE(
588 "bad payload length for " + name + " packet",
589 actual.length >= min_length && actual.length <= max_length
593 void PacketTest::AssertEqual(
594 const string &message,
595 const EntityState &expected,
596 const EntityState &actual
599 message + ": bad chunk position",
600 expected.chunk_pos, actual.chunk_pos
603 message + ": bad block position",
604 expected.block_pos, actual.block_pos, 16.0f/65535.0f // that's about the max accuracy that packing's going to give us
607 message + ": bad velocity",
608 expected.velocity, actual.velocity
611 message + ": bad orientation",
612 expected.orient, actual.orient
614 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
615 message + ": bad pitch",
616 expected.pitch, actual.pitch, PI/65534.0f
618 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
619 message + ": bad yaw",
620 expected.yaw, actual.yaw, PI/32767.0f
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()