]> git.localhorst.tv Git - blank.git/blob - tst/net/PacketTest.cpp
some unit tests for Packet et al
[blank.git] / tst / net / PacketTest.cpp
1 #include "PacketTest.hpp"
2
3 #include "net/Packet.hpp"
4 #include "world/Entity.hpp"
5 #include "world/EntityState.hpp"
6
7 #include <string>
8
9 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::PacketTest);
10
11 using namespace std;
12
13 namespace blank {
14 namespace test {
15
16 void PacketTest::setUp() {
17         udp_pack.data = new Uint8[sizeof(Packet)];
18         udp_pack.maxlen = sizeof(Packet);
19 }
20
21 void PacketTest::tearDown() {
22         delete[] udp_pack.data;
23 }
24
25 namespace {
26
27 static constexpr uint32_t TEST_TAG = 0xFB1AB1AF;
28
29 }
30
31 void PacketTest::testControl() {
32         Packet::TControl ctrl;
33         ctrl.ack = 10;
34
35         CPPUNIT_ASSERT_MESSAGE(
36                 "TControl should ack the packet in the ack field",
37                 ctrl.Acks(10)
38         );
39         CPPUNIT_ASSERT_MESSAGE(
40                 "TControl should ack the packet in the future",
41                 !ctrl.Acks(11)
42         );
43         CPPUNIT_ASSERT_MESSAGE(
44                 "TControl should not ack a packet in the distant past",
45                 !ctrl.Acks(-30)
46         );
47         CPPUNIT_ASSERT_MESSAGE(
48                 "TControl should not ack the previous packet if the bitfield is 0",
49                 !ctrl.Acks(9)
50         );
51         CPPUNIT_ASSERT_EQUAL_MESSAGE(
52                 "TControl's acks should begin at the packet in the ack field",
53                 uint16_t(10), ctrl.AckBegin()
54         );
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()
58         );
59         ctrl.hist = 1;
60         CPPUNIT_ASSERT_MESSAGE(
61                 "TControl should ack the previous packet if the bitfield is 1",
62                 ctrl.Acks(9)
63         );
64         ctrl.hist = 2;
65         CPPUNIT_ASSERT_MESSAGE(
66                 "TControl should not ack the previous packet if the bitfield is 2",
67                 !ctrl.Acks(9)
68         );
69         CPPUNIT_ASSERT_MESSAGE(
70                 "TControl should ack the packet before the previous one if the bitfield is 2",
71                 ctrl.Acks(8)
72         );
73 }
74
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
80         );
81         CPPUNIT_ASSERT_EQUAL_MESSAGE(
82                 "wrong type code for Ping packet",
83                 uint8_t(0), pack.GetHeader().type
84         );
85         CPPUNIT_ASSERT_EQUAL_MESSAGE(
86                 "bad payload length for Ping packet",
87                 size_t(0), pack.length
88         );
89 }
90
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
96         );
97         CPPUNIT_ASSERT_EQUAL_MESSAGE(
98                 "wrong type code for Login packet",
99                 uint8_t(1), pack.GetHeader().type
100         );
101         CPPUNIT_ASSERT_MESSAGE(
102                 "bad payload length for Login packet",
103                 pack.length <= 32
104         );
105
106         string write_name = "test";
107         string read_name;
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
113         );
114
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
121         );
122 }
123
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
129         );
130         CPPUNIT_ASSERT_EQUAL_MESSAGE(
131                 "wrong type code for Join packet",
132                 uint8_t(2), pack.GetHeader().type
133         );
134         CPPUNIT_ASSERT_MESSAGE(
135                 "bad payload length for Join packet",
136                 pack.length >= 68 && pack.length <= 100
137         );
138
139         Entity write_entity;
140         write_entity.ID(534574);
141         uint32_t read_id = 0;
142         pack.WritePlayer(write_entity);
143
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
148         );
149
150         string write_name = "test";
151         string read_name;
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
157         );
158
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
165         );
166 }
167
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
173         );
174         CPPUNIT_ASSERT_EQUAL_MESSAGE(
175                 "wrong type code for Part packet",
176                 uint8_t(3), pack.GetHeader().type
177         );
178         CPPUNIT_ASSERT_EQUAL_MESSAGE(
179                 "bad payload length for Part packet",
180                 size_t(0), pack.length
181         );
182 }
183
184 }
185 }