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",
239 void PacketTest::AssertPacket(
241 uint8_t expected_type,
242 size_t expected_length,
243 const Packet::Payload &actual
245 CPPUNIT_ASSERT_EQUAL_MESSAGE(
246 name + " packet not correctly tagged",
247 TEST_TAG, actual.GetHeader().tag
249 CPPUNIT_ASSERT_EQUAL_MESSAGE(
250 "wrong type code for " + name + " packet",
251 int(expected_type), int(actual.GetHeader().type)
253 CPPUNIT_ASSERT_EQUAL_MESSAGE(
254 "bad payload length for " + name + " packet",
255 expected_length, actual.length
259 void PacketTest::AssertPacket(
261 uint8_t expected_type,
264 const Packet::Payload &actual
266 CPPUNIT_ASSERT_EQUAL_MESSAGE(
267 name + " packet not correctly tagged",
268 TEST_TAG, actual.GetHeader().tag
270 CPPUNIT_ASSERT_EQUAL_MESSAGE(
271 "wrong type code for " + name + " packet",
272 expected_type, actual.GetHeader().type
274 CPPUNIT_ASSERT_MESSAGE(
275 "bad payload length for " + name + " packet",
276 actual.length >= min_length && actual.length <= max_length
280 void PacketTest::AssertEqual(
281 const string &message,
282 const EntityState &expected,
283 const EntityState &actual
286 message + ": bad chunk position",
287 expected.chunk_pos, actual.chunk_pos
290 message + ": bad block position",
291 expected.block_pos, actual.block_pos
294 message + ": bad velocity",
295 expected.velocity, actual.velocity
298 message + ": bad orientation",
299 expected.orient, actual.orient
302 message + ": bad angular velocity",
303 expected.ang_vel, actual.ang_vel
307 void PacketTest::AssertEqual(
308 const string &message,
309 const AABB &expected,
313 message + ": bad lower bound",
314 expected.min, actual.min
317 message + ": bad upper bound",
318 expected.max, actual.max
322 void PacketTest::AssertEqual(
323 const string &message,
324 const glm::ivec3 &expected,
325 const glm::ivec3 &actual
327 CPPUNIT_ASSERT_EQUAL_MESSAGE(
328 message + " (X component)",
331 CPPUNIT_ASSERT_EQUAL_MESSAGE(
332 message + " (Y component)",
335 CPPUNIT_ASSERT_EQUAL_MESSAGE(
336 message + " (Z component)",
341 void PacketTest::AssertEqual(
342 const string &message,
343 const glm::vec3 &expected,
344 const glm::vec3 &actual
346 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
347 message + " (X component)",
348 expected.x, actual.x, numeric_limits<float>::epsilon()
350 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
351 message + " (Y component)",
352 expected.y, actual.y, numeric_limits<float>::epsilon()
354 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
355 message + " (Z component)",
356 expected.z, actual.z, numeric_limits<float>::epsilon()
360 void PacketTest::AssertEqual(
361 const string &message,
362 const glm::quat &expected,
363 const glm::quat &actual
365 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
366 message + " (W component)",
367 expected.w, actual.w, numeric_limits<float>::epsilon()
369 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
370 message + " (X component)",
371 expected.x, actual.x, numeric_limits<float>::epsilon()
373 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
374 message + " (Y component)",
375 expected.y, actual.y, numeric_limits<float>::epsilon()
377 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
378 message + " (Z component)",
379 expected.z, actual.z, numeric_limits<float>::epsilon()