1 #include "PacketTest.hpp"
3 #include "net/Packet.hpp"
4 #include "world/Entity.hpp"
5 #include "world/EntityState.hpp"
9 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::PacketTest);
16 void PacketTest::setUp() {
17 udp_pack.data = new Uint8[sizeof(Packet)];
18 udp_pack.maxlen = sizeof(Packet);
21 void PacketTest::tearDown() {
22 delete[] udp_pack.data;
27 static constexpr uint32_t TEST_TAG = 0xFB1AB1AF;
31 void PacketTest::testControl() {
32 Packet::TControl ctrl;
35 CPPUNIT_ASSERT_MESSAGE(
36 "TControl should ack the packet in the ack field",
39 CPPUNIT_ASSERT_MESSAGE(
40 "TControl should ack the packet in the future",
43 CPPUNIT_ASSERT_MESSAGE(
44 "TControl should not ack a packet in the distant past",
47 CPPUNIT_ASSERT_MESSAGE(
48 "TControl should not ack the previous packet if the bitfield is 0",
51 CPPUNIT_ASSERT_EQUAL_MESSAGE(
52 "TControl's acks should begin at the packet in the ack field",
53 uint16_t(10), ctrl.AckBegin()
55 CPPUNIT_ASSERT_EQUAL_MESSAGE(
56 "TControl's acks should end 33 packets before the one in the ack field",
57 uint16_t(-23), ctrl.AckEnd()
60 CPPUNIT_ASSERT_MESSAGE(
61 "TControl should ack the previous packet if the bitfield is 1",
65 CPPUNIT_ASSERT_MESSAGE(
66 "TControl should not ack the previous packet if the bitfield is 2",
69 CPPUNIT_ASSERT_MESSAGE(
70 "TControl should ack the packet before the previous one if the bitfield is 2",
75 void PacketTest::testPing() {
76 auto pack = Packet::Make<Packet::Ping>(udp_pack);
77 CPPUNIT_ASSERT_EQUAL_MESSAGE(
78 "Ping packet not correctly tagged",
79 TEST_TAG, pack.GetHeader().tag
81 CPPUNIT_ASSERT_EQUAL_MESSAGE(
82 "wrong type code for Ping packet",
83 uint8_t(0), pack.GetHeader().type
85 CPPUNIT_ASSERT_EQUAL_MESSAGE(
86 "bad payload length for Ping packet",
87 size_t(0), pack.length
91 void PacketTest::testLogin() {
92 auto pack = Packet::Make<Packet::Login>(udp_pack);
93 CPPUNIT_ASSERT_EQUAL_MESSAGE(
94 "Login packet not correctly tagged",
95 TEST_TAG, pack.GetHeader().tag
97 CPPUNIT_ASSERT_EQUAL_MESSAGE(
98 "wrong type code for Login packet",
99 uint8_t(1), pack.GetHeader().type
101 CPPUNIT_ASSERT_MESSAGE(
102 "bad payload length for Login packet",
106 string write_name = "test";
108 pack.WritePlayerName(write_name);
109 pack.ReadPlayerName(read_name);
110 CPPUNIT_ASSERT_EQUAL_MESSAGE(
111 "player name not correctly transported in Login packet",
112 write_name, read_name
115 write_name = "0123456789012345678901234567890123456789";
116 pack.WritePlayerName(write_name);
117 pack.ReadPlayerName(read_name);
118 CPPUNIT_ASSERT_EQUAL_MESSAGE(
119 "player name not correctly truncated in Login packet",
120 write_name.substr(0, 32), read_name
124 void PacketTest::testJoin() {
125 auto pack = Packet::Make<Packet::Join>(udp_pack);
126 CPPUNIT_ASSERT_EQUAL_MESSAGE(
127 "Join packet not correctly tagged",
128 TEST_TAG, pack.GetHeader().tag
130 CPPUNIT_ASSERT_EQUAL_MESSAGE(
131 "wrong type code for Join packet",
132 uint8_t(2), pack.GetHeader().type
134 CPPUNIT_ASSERT_MESSAGE(
135 "bad payload length for Join packet",
136 pack.length >= 68 && pack.length <= 100
140 write_entity.ID(534574);
141 uint32_t read_id = 0;
142 pack.WritePlayer(write_entity);
144 pack.ReadPlayerID(read_id);
145 CPPUNIT_ASSERT_EQUAL_MESSAGE(
146 "player entity ID not correctly transported in Join packet",
147 write_entity.ID(), read_id
150 string write_name = "test";
152 pack.WriteWorldName(write_name);
153 pack.ReadWorldName(read_name);
154 CPPUNIT_ASSERT_EQUAL_MESSAGE(
155 "world name not correctly transported in Join packet",
156 write_name, read_name
159 write_name = "0123456789012345678901234567890123456789";
160 pack.WriteWorldName(write_name);
161 pack.ReadWorldName(read_name);
162 CPPUNIT_ASSERT_EQUAL_MESSAGE(
163 "world name not correctly truncated in Join packet",
164 write_name.substr(0, 32), read_name
168 void PacketTest::testPart() {
169 auto pack = Packet::Make<Packet::Part>(udp_pack);
170 CPPUNIT_ASSERT_EQUAL_MESSAGE(
171 "Part packet not correctly tagged",
172 TEST_TAG, pack.GetHeader().tag
174 CPPUNIT_ASSERT_EQUAL_MESSAGE(
175 "wrong type code for Part packet",
176 uint8_t(3), pack.GetHeader().type
178 CPPUNIT_ASSERT_EQUAL_MESSAGE(
179 "bad payload length for Part packet",
180 size_t(0), pack.length