1 #include "PacketTest.hpp"
3 #include "model/CompositeModel.hpp"
4 #include "world/Entity.hpp"
8 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::PacketTest);
15 void PacketTest::setUp() {
16 udp_pack.data = new Uint8[sizeof(Packet)];
17 udp_pack.maxlen = sizeof(Packet);
20 void PacketTest::tearDown() {
21 delete[] udp_pack.data;
26 static constexpr uint32_t TEST_TAG = 0xFB1AB1AF;
30 void PacketTest::testControl() {
31 Packet::TControl ctrl;
34 CPPUNIT_ASSERT_MESSAGE(
35 "TControl should ack the packet in the ack field",
38 CPPUNIT_ASSERT_MESSAGE(
39 "TControl should ack the packet in the future",
42 CPPUNIT_ASSERT_MESSAGE(
43 "TControl should not ack a packet in the distant past",
46 CPPUNIT_ASSERT_MESSAGE(
47 "TControl should not ack the previous packet if the bitfield is 0",
50 CPPUNIT_ASSERT_EQUAL_MESSAGE(
51 "TControl's acks should begin at the packet in the ack field",
52 uint16_t(10), ctrl.AckBegin()
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()
59 CPPUNIT_ASSERT_MESSAGE(
60 "TControl should ack the previous packet if the bitfield is 1",
64 CPPUNIT_ASSERT_MESSAGE(
65 "TControl should not ack the previous packet if the bitfield is 2",
68 CPPUNIT_ASSERT_MESSAGE(
69 "TControl should ack the packet before the previous one if the bitfield is 2",
74 void PacketTest::testPing() {
75 auto pack = Packet::Make<Packet::Ping>(udp_pack);
76 AssertPacket("Ping", 0, 0, pack);
79 void PacketTest::testLogin() {
80 auto pack = Packet::Make<Packet::Login>(udp_pack);
81 AssertPacket("Login", 1, 0, 32, pack);
83 string write_name = "test";
85 pack.WritePlayerName(write_name);
86 pack.ReadPlayerName(read_name);
87 CPPUNIT_ASSERT_EQUAL_MESSAGE(
88 "player name not correctly transported in Login packet",
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
101 void PacketTest::testJoin() {
102 auto pack = Packet::Make<Packet::Join>(udp_pack);
103 AssertPacket("Join", 2, 68, 100, pack);
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);
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
121 pack.ReadPlayerState(read_state);
123 "player entity state not correctly transported in Join packet",
124 write_entity.GetState(), read_state
127 string write_name = "test";
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
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
145 void PacketTest::testPart() {
146 auto pack = Packet::Make<Packet::Part>(udp_pack);
147 AssertPacket("Part", 3, 0, pack);
150 void PacketTest::testPlayerUpdate() {
151 auto pack = Packet::Make<Packet::PlayerUpdate>(udp_pack);
152 AssertPacket("PlayerUpdate", 4, 64, pack);
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);
164 pack.ReadPlayerState(read_state);
166 "player entity state not correctly transported in PlayerUpdate packet",
167 write_entity.GetState(), read_state
171 void PacketTest::testSpawnEntity() {
172 auto pack = Packet::Make<Packet::SpawnEntity>(udp_pack);
173 AssertPacket("SpawnEntity", 5, 100, 132, pack);
176 write_entity.ID(534574);
177 CompositeModel model;
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);
191 uint32_t skeleton_id;
193 pack.ReadEntityID(entity_id);
194 pack.ReadSkeletonID(skeleton_id);
195 pack.ReadEntity(read_entity);
197 CPPUNIT_ASSERT_EQUAL_MESSAGE(
198 "entity ID not correctly transported in SpawnEntity packet",
199 write_entity.ID(), entity_id
201 CPPUNIT_ASSERT_EQUAL_MESSAGE(
202 "skeleton ID not correctly transported in SpawnEntity packet",
203 write_entity.GetModel().GetModel().ID(), skeleton_id
206 "entity state not correctly transported in PlayerUpdate packet",
207 write_entity.GetState(), read_entity.GetState()
210 "entity bounds not correctly transported in PlayerUpdate packet",
211 write_entity.Bounds(), read_entity.Bounds()
213 CPPUNIT_ASSERT_MESSAGE(
214 "entity flags not correctly transported in SpawnEntity packet",
215 read_entity.WorldCollidable()
217 CPPUNIT_ASSERT_EQUAL_MESSAGE(
218 "entity name not correctly transported in SpawnEntity packet",
219 write_entity.Name(), read_entity.Name()
223 void PacketTest::testDespawnEntity() {
224 auto pack = Packet::Make<Packet::DespawnEntity>(udp_pack);
225 AssertPacket("DespawnEntity", 6, 4, pack);
227 uint32_t write_id = 5437;
229 pack.WriteEntityID(write_id);
230 pack.ReadEntityID(read_id);
232 CPPUNIT_ASSERT_EQUAL_MESSAGE(
233 "entity ID not correctly transported in DespawnEntity packet",
238 void PacketTest::testEntityUpdate() {
239 auto pack = Packet::Make<Packet::EntityUpdate>(udp_pack);
240 AssertPacket("EntityUpdate", 7, 4, 452, pack);
242 pack.length = Packet::EntityUpdate::GetSize(3);
243 CPPUNIT_ASSERT_EQUAL_MESSAGE(
244 "length not correctly set in DespawnEntity packet",
245 size_t(4 + 3 * 68), pack.length
248 uint32_t write_count = 3;
250 pack.WriteEntityCount(write_count);
251 pack.ReadEntityCount(read_count);
252 CPPUNIT_ASSERT_EQUAL_MESSAGE(
253 "entity count not correctly transported in EntityUpdate packet",
254 write_count, read_count
258 write_entity.ID(8567234);
259 write_entity.GetState().chunk_pos = { 7, 2, -3 };
260 write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
261 write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
262 write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
263 write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
264 pack.WriteEntity(write_entity, 1);
265 pack.WriteEntity(write_entity, 0);
266 pack.WriteEntity(write_entity, 2);
269 EntityState read_state;
270 pack.ReadEntityID(read_id, 1);
271 pack.ReadEntityState(read_state, 1);
272 CPPUNIT_ASSERT_EQUAL_MESSAGE(
273 "entity ID not correctly transported in EntityUpdate packet",
274 write_entity.ID(), read_id
277 "entity state not correctly transported in EntityUpdate packet",
278 write_entity.GetState(), read_state
283 void PacketTest::AssertPacket(
285 uint8_t expected_type,
286 size_t expected_length,
287 const Packet::Payload &actual
289 CPPUNIT_ASSERT_EQUAL_MESSAGE(
290 name + " packet not correctly tagged",
291 TEST_TAG, actual.GetHeader().tag
293 CPPUNIT_ASSERT_EQUAL_MESSAGE(
294 "wrong type code for " + name + " packet",
295 int(expected_type), int(actual.GetHeader().type)
297 CPPUNIT_ASSERT_EQUAL_MESSAGE(
298 "bad payload length for " + name + " packet",
299 expected_length, actual.length
303 void PacketTest::AssertPacket(
305 uint8_t expected_type,
308 const Packet::Payload &actual
310 CPPUNIT_ASSERT_EQUAL_MESSAGE(
311 name + " packet not correctly tagged",
312 TEST_TAG, actual.GetHeader().tag
314 CPPUNIT_ASSERT_EQUAL_MESSAGE(
315 "wrong type code for " + name + " packet",
316 expected_type, actual.GetHeader().type
318 CPPUNIT_ASSERT_MESSAGE(
319 "bad payload length for " + name + " packet",
320 actual.length >= min_length && actual.length <= max_length
324 void PacketTest::AssertEqual(
325 const string &message,
326 const EntityState &expected,
327 const EntityState &actual
330 message + ": bad chunk position",
331 expected.chunk_pos, actual.chunk_pos
334 message + ": bad block position",
335 expected.block_pos, actual.block_pos
338 message + ": bad velocity",
339 expected.velocity, actual.velocity
342 message + ": bad orientation",
343 expected.orient, actual.orient
346 message + ": bad angular velocity",
347 expected.ang_vel, actual.ang_vel
351 void PacketTest::AssertEqual(
352 const string &message,
353 const AABB &expected,
357 message + ": bad lower bound",
358 expected.min, actual.min
361 message + ": bad upper bound",
362 expected.max, actual.max
366 void PacketTest::AssertEqual(
367 const string &message,
368 const glm::ivec3 &expected,
369 const glm::ivec3 &actual
371 CPPUNIT_ASSERT_EQUAL_MESSAGE(
372 message + " (X component)",
375 CPPUNIT_ASSERT_EQUAL_MESSAGE(
376 message + " (Y component)",
379 CPPUNIT_ASSERT_EQUAL_MESSAGE(
380 message + " (Z component)",
385 void PacketTest::AssertEqual(
386 const string &message,
387 const glm::vec3 &expected,
388 const glm::vec3 &actual
390 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
391 message + " (X component)",
392 expected.x, actual.x, numeric_limits<float>::epsilon()
394 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
395 message + " (Y component)",
396 expected.y, actual.y, numeric_limits<float>::epsilon()
398 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
399 message + " (Z component)",
400 expected.z, actual.z, numeric_limits<float>::epsilon()
404 void PacketTest::AssertEqual(
405 const string &message,
406 const glm::quat &expected,
407 const glm::quat &actual
409 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
410 message + " (W component)",
411 expected.w, actual.w, numeric_limits<float>::epsilon()
413 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
414 message + " (X component)",
415 expected.x, actual.x, numeric_limits<float>::epsilon()
417 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
418 message + " (Y component)",
419 expected.y, actual.y, numeric_limits<float>::epsilon()
421 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
422 message + " (Z component)",
423 expected.z, actual.z, numeric_limits<float>::epsilon()