1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
11 /**@name test_net_lowlevel.cpp - The test file for net_lowlevel.cpp. */
12 //
13 // (c) Copyright 2013 by Joris Dauphin
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29
30 #include <UnitTest++.h>
31
32 #include "stratagus.h"
33 #include "netconnect.h"
34
35
FillCustomValue(CNetworkHost * obj)36 void FillCustomValue(CNetworkHost *obj)
37 {
38 obj->Host = 0x12345678;
39 obj->Port = 0x9ABC;
40 for (int i = 0; i != sizeof(obj->PlyName); ++i) {
41 obj->PlyName[i] = i + 1;
42 }
43 obj->PlyNr = 0xDEF0;
44 }
45
FillCustomValue(CServerSetup * obj)46 void FillCustomValue(CServerSetup *obj)
47 {
48 obj->ResourcesOption = 42;
49 obj->UnitsOption = 44;
50 obj->FogOfWar = 46;
51 obj->RevealMap = 48;
52 obj->TilesetSelection = 50;
53 obj->GameTypeOption = 52;
54 obj->Difficulty = 54;
55 obj->MapRichness = 56;
56 obj->Opponents = 58;
57 for (int i = 0; i != PlayerMax; ++i) {
58 obj->CompOpt[i] = i + 1;
59 }
60 for (int i = 0; i != PlayerMax; ++i) {
61 obj->Ready[i] = i + 11;
62 }
63 for (int i = 0; i != PlayerMax; ++i) {
64 obj->Race[i] = i + 21;
65 }
66 }
67
FillCustomValue(CInitMessage_Header * obj)68 void FillCustomValue(CInitMessage_Header *obj)
69 {
70 *obj = CInitMessage_Header(0x12, 0x34);
71 }
72
FillCustomValue(CInitMessage_Hello * obj)73 void FillCustomValue(CInitMessage_Hello *obj)
74 {
75 for (int i = 0; i != sizeof(obj->PlyName); ++i) {
76 obj->PlyName[i] = i + 1;
77 }
78 obj->Stratagus = 0x12345678;
79 obj->Version = 0x90ABCDEF;
80 }
81
FillCustomValue(CInitMessage_Config * obj)82 void FillCustomValue(CInitMessage_Config *obj)
83 {
84 obj->hostsCount = PlayerMax;
85 obj->clientIndex = 3;
86 for (int i = 0; i != PlayerMax; ++i) {
87 FillCustomValue(&obj->hosts[i]);
88 }
89 }
90
FillCustomValue(CInitMessage_EngineMismatch * obj)91 void FillCustomValue(CInitMessage_EngineMismatch *obj)
92 {
93 obj->Stratagus = 0x01020304;
94 }
95
FillCustomValue(CInitMessage_ProtocolMismatch * obj)96 void FillCustomValue(CInitMessage_ProtocolMismatch *obj)
97 {
98 obj->Version = 0x01020304;
99 }
100
FillCustomValue(CInitMessage_Welcome * obj)101 void FillCustomValue(CInitMessage_Welcome *obj)
102 {
103 obj->Lag = 0x01020304;
104 obj->gameCyclesPerUpdate = 0x05060708;
105 for (int i = 0; i != PlayerMax; ++i) {
106 FillCustomValue(&obj->hosts[i]);
107 }
108 }
109
FillCustomValue(CInitMessage_Map * obj)110 void FillCustomValue(CInitMessage_Map *obj)
111 {
112 obj->MapUID = 0x01234567;
113 for (int i = 0; i != sizeof(obj->MapPath); ++i) {
114 obj->MapPath[i] = 1 + i;
115 }
116 }
117
FillCustomValue(CInitMessage_State * obj)118 void FillCustomValue(CInitMessage_State *obj)
119 {
120 FillCustomValue(&obj->State);
121 }
122
FillCustomValue(CInitMessage_Resync * obj)123 void FillCustomValue(CInitMessage_Resync *obj)
124 {
125 for (int i = 0; i != PlayerMax; ++i) {
126 FillCustomValue(&obj->hosts[i]);
127 }
128 }
129
130 template <typename T>
CheckSerialization()131 bool CheckSerialization()
132 {
133 T obj1;
134
135 FillCustomValue(&obj1);
136
137 unsigned char *buffer = new unsigned char [obj1.Size()];
138 unsigned char *end = buffer + obj1.Serialize(buffer);
139 bool res = size_t(end - buffer) == obj1.Size();
140 T obj2;
141 obj2.Deserialize(buffer);
142 delete [] buffer;
143 res &= memcmp(&obj1, &obj2, sizeof(T)) == 0; // may fail with padding
144 return res;
145 }
146
147 template <typename T>
CheckSerialization_return()148 bool CheckSerialization_return()
149 {
150 T obj1;
151
152 memset(&obj1, 0, sizeof(T));
153 FillCustomValue(&obj1);
154 const unsigned char *buffer = obj1.Serialize();
155
156 T obj2;
157 memset(&obj2, 0, sizeof(T));
158 obj2.Deserialize(buffer);
159 bool res = memcmp(&obj1, &obj2, sizeof(T)) == 0; // may fail with padding
160 delete [] buffer;
161 return res;
162 }
163
164
TEST(CNetworkHost)165 TEST(CNetworkHost)
166 {
167 CHECK(CheckSerialization<CNetworkHost>());
168 }
169
TEST(CServerSetup)170 TEST(CServerSetup)
171 {
172 CHECK(CheckSerialization<CServerSetup>());
173 }
174
TEST(CInitMessage_Header)175 TEST(CInitMessage_Header)
176 {
177 CHECK(CheckSerialization<CInitMessage_Header>());
178 }
179
TEST(CInitMessage_Hello)180 TEST(CInitMessage_Hello)
181 {
182 CHECK(CheckSerialization_return<CInitMessage_Hello>());
183 }
184
TEST(CInitMessage_Config)185 TEST(CInitMessage_Config)
186 {
187 CHECK(CheckSerialization_return<CInitMessage_Config>());
188 }
189
TEST(CInitMessage_EngineMismatch)190 TEST(CInitMessage_EngineMismatch)
191 {
192 CHECK(CheckSerialization_return<CInitMessage_EngineMismatch>());
193 }
194
TEST(CInitMessage_ProtocolMismatch)195 TEST(CInitMessage_ProtocolMismatch)
196 {
197 CHECK(CheckSerialization_return<CInitMessage_ProtocolMismatch>());
198 }
199
TEST(CInitMessage_Welcome)200 TEST(CInitMessage_Welcome)
201 {
202 CHECK(CheckSerialization_return<CInitMessage_Welcome>());
203 }
204
TEST(CInitMessage_Map)205 TEST(CInitMessage_Map)
206 {
207 CHECK(CheckSerialization_return<CInitMessage_Map>());
208 }
209
TEST(CInitMessage_State)210 TEST(CInitMessage_State)
211 {
212 CHECK(CheckSerialization_return<CInitMessage_State>());
213 }
214
TEST(CInitMessage_Resync)215 TEST(CInitMessage_Resync)
216 {
217 CHECK(CheckSerialization_return<CInitMessage_Resync>());
218 }
219