1 #include "PacketAndLowLevelTestsTest.h"
2 
3 #include <stdlib.h>
4 
5 /*
6 Description:
7 Tests out the sunctions:
8 virtual int RakPeerInterface::GetSplitMessageProgressInterval  	(  	void   	  	 )   	 const "
9 virtual void RakPeerInterface::PushBackPacket  	(  	Packet *   	 packet, 		bool  	pushAtHead	  	) 			"
10 virtual bool RakPeerInterface::SendList  	(  	char **   	 data, 		const int *  	lengths, 		const int  	numParameters, 		PacketPriority  	priority, 		PacketReliability  	reliability, 		char  	orderingChannel, 		SystemAddress  	systemAddress, 		bool  	broadcast	  	) 			"
11 virtual void RakPeerInterface::SetSplitMessageProgressInterval 	( 	int  	interval 	 )
12 virtual void RakPeerInterface::SetUnreliableTimeout  	(  	RakNetTime   	 timeoutMS  	 )
13 virtual Packet* RakPeerInterface::AllocatePacket  	(  	unsigned   	 dataSize  	 )
14 AttachPlugin (PluginInterface2 *plugin)=0
15 DetachPlugin (PluginInterface2 *plugin)=0
16 
17 Success conditions:
18 
19 Failure conditions:
20 
21 RakPeerInterface Functions used, tested indirectly by its use,list may not be complete:
22 Startup
23 SetMaximumIncomingConnections
24 Receive
25 DeallocatePacket
26 Send
27 IsConnected
28 RakPeerInterface Functions Explicitly Tested:
29 GetSplitMessageProgressInterval
30 PushBackPacket
31 SendList
32 SetSplitMessageProgressInterval
33 AllocatePacket
34 GetMTUSize
35 AttachPlugin
36 DetachPlugin
37 
38 */
RunTest(DataStructures::List<RakNet::RakString> params,bool isVerbose,bool noPauses)39 int PacketAndLowLevelTestsTest::RunTest(DataStructures::List<RakNet::RakString> params,bool isVerbose,bool noPauses)
40 {
41 	RakPeerInterface *server,*client;
42 	destroyList.Clear(false,__FILE__,__LINE__);
43 
44 	TestHelpers::StandardClientPrep(client,destroyList);
45 	TestHelpers::StandardServerPrep(server,destroyList);
46 	printf("Connecting to server\n");
47 	if (!TestHelpers::WaitAndConnectTwoPeersLocally(client,server,5000))
48 	{
49 
50 		if (isVerbose)
51 			DebugTools::ShowError(errorList[1-1],!noPauses && isVerbose,__LINE__,__FILE__);
52 
53 		return 1;
54 	}
55 
56 	printf("Testing SendList\n");
57 
58 	char* dataList2[5];
59 	char **dataList=(char **)dataList2;
60 	int lengths[5];
61 	char curString1[]="AAAA";
62 	char curString2[]="ABBB";
63 	char curString3[]="ACCC";
64 	char curString4[]="ADDD";
65 	char curString5[]="AEEE";
66 
67 	dataList[0]=curString1;
68 	dataList[1]=curString2;
69 	dataList[2]=curString3;
70 	dataList[3]=curString4;
71 	dataList[4]=curString5;
72 
73 	for (int i=0;i<5;i++)
74 	{
75 		dataList[i][0]=ID_USER_PACKET_ENUM+1+i;
76 		lengths[i]=5;
77 	}
78 
79 	client->SendList((const char**)dataList,lengths,5,HIGH_PRIORITY,RELIABLE_ORDERED,0, UNASSIGNED_SYSTEM_ADDRESS, true);
80 
81 	Packet* packet;
82 	if (!(packet=CommonFunctions::WaitAndReturnMessageWithID(server,ID_USER_PACKET_ENUM+1,1000)))
83 	{
84 
85 		if (isVerbose)
86 			DebugTools::ShowError(errorList[9-1],!noPauses && isVerbose,__LINE__,__FILE__);
87 
88 		return 9;
89 	}
90 
91 	if (packet->length!=25)
92 	{
93 
94 		if (isVerbose)
95 			DebugTools::ShowError(errorList[13],!noPauses && isVerbose,__LINE__,__FILE__);
96 
97 		return 14;
98 
99 	}
100 
101 	server->DeallocatePacket(packet);
102 
103 	PluginInterface2* myPlug=new PacketChangerPlugin();
104 
105 	printf("Test attach detach of plugins\n");
106 	client->AttachPlugin(myPlug);
107 	TestHelpers::BroadCastTestPacket(client);
108 	if (TestHelpers::WaitForTestPacket(server,2000))
109 	{
110 
111 		if (isVerbose)
112 			DebugTools::ShowError(errorList[2-1],!noPauses && isVerbose,__LINE__,__FILE__);
113 
114 		return 2;
115 	}
116 
117 	client->DetachPlugin(myPlug);
118 	TestHelpers::BroadCastTestPacket(client);
119 	if (!TestHelpers::WaitForTestPacket(server,2000))
120 	{
121 
122 		if (isVerbose)
123 			DebugTools::ShowError(errorList[3-1],!noPauses && isVerbose,__LINE__,__FILE__);
124 
125 		return 3;
126 	}
127 
128 	printf("Test AllocatePacket\n");
129 	Packet * hugePacket,*hugePacket2;
130 	const int dataSize=3000000;//around 30 meg didn't want to calculate the exact
131 	hugePacket=client->AllocatePacket(dataSize);
132 	hugePacket2=client->AllocatePacket(dataSize);
133 
134 	/*//Couldn't find a good cross platform way for allocated memory so skipped this check
135 	if (somemalloccheck<3000000)
136 	{}
137 	*/
138 
139 	printf("Assuming 3000000 allocation for splitpacket, testing setsplitpacket\n");
140 
141 	hugePacket->data[0]=ID_USER_PACKET_ENUM+1;
142 	hugePacket2->data[0]=ID_USER_PACKET_ENUM+1;
143 
144 	server->SetSplitMessageProgressInterval(1);
145 
146 	if (server->GetSplitMessageProgressInterval()!=1)
147 	{
148 
149 		if (isVerbose)
150 			DebugTools::ShowError(errorList[4-1],!noPauses && isVerbose,__LINE__,__FILE__);
151 
152 		return 4;
153 
154 	}
155 
156 	if (!client->Send((const char *)hugePacket->data,dataSize,HIGH_PRIORITY,RELIABLE_ORDERED,0, UNASSIGNED_SYSTEM_ADDRESS, true))
157 	{
158 
159 		if (isVerbose)
160 			DebugTools::ShowError(errorList[5-1],!noPauses && isVerbose,__LINE__,__FILE__);
161 
162 		return 5;
163 	}
164 
165 	if (!CommonFunctions::WaitForMessageWithID(server,ID_DOWNLOAD_PROGRESS,2000))
166 	{
167 
168 		if (isVerbose)
169 			DebugTools::ShowError(errorList[6-1],!noPauses && isVerbose,__LINE__,__FILE__);
170 
171 		return 6;
172 	}
173 
174 	while(CommonFunctions::WaitForMessageWithID(server,ID_DOWNLOAD_PROGRESS,500))//Clear out the rest before next test
175 	{
176 	}
177 
178 	printf("Making sure still connected, if not connect\n");
179 	if (!TestHelpers::WaitAndConnectTwoPeersLocally(client,server,5000))//Make sure connected before test
180 	{
181 
182 		if (isVerbose)
183 			DebugTools::ShowError(errorList[11-1],!noPauses && isVerbose,__LINE__,__FILE__);
184 
185 		return 11;
186 	}
187 
188 	printf("Making sure standard send/recieve still functioning\n");
189 	TestHelpers::BroadCastTestPacket(client);
190 	if (!TestHelpers::WaitForTestPacket(server,5000))
191 	{
192 
193 		if (isVerbose)
194 			DebugTools::ShowError(errorList[12],!noPauses && isVerbose,__LINE__,__FILE__);
195 
196 		return 13;
197 	}
198 
199 	printf("Testing PushBackPacket\n");
200 
201 	server->PushBackPacket(hugePacket,false);
202 
203 	if (!TestHelpers::WaitForTestPacket(server,2000))
204 	{
205 
206 		if (isVerbose)
207 			DebugTools::ShowError(errorList[7-1],!noPauses && isVerbose,__LINE__,__FILE__);
208 
209 		return 7;
210 	}
211 
212 	printf("Making sure still connected, if not connect\n");
213 	if (!TestHelpers::WaitAndConnectTwoPeersLocally(client,server,5000))//Make sure connected before test
214 	{
215 
216 		if (isVerbose)
217 			DebugTools::ShowError(errorList[11-1],!noPauses && isVerbose,__LINE__,__FILE__);
218 
219 		return 11;
220 	}
221 
222 	printf("Making sure standard send/recieve still functioning\n");
223 	TestHelpers::BroadCastTestPacket(client);
224 	if (!TestHelpers::WaitForTestPacket(server,2000))
225 	{
226 
227 		if (isVerbose)
228 			DebugTools::ShowError(errorList[12-1],!noPauses && isVerbose,__LINE__,__FILE__);
229 
230 		return 12;
231 	}
232 
233 	printf("PushBackPacket head true test\n");
234 	server->PushBackPacket(hugePacket2,true);
235 
236 	if (!TestHelpers::WaitForTestPacket(server,2000))
237 	{
238 
239 		if (isVerbose)
240 			DebugTools::ShowError(errorList[10-1],!noPauses && isVerbose,__LINE__,__FILE__);
241 
242 		return 10;
243 	}
244 
245 	printf("Making sure still connected, if not connect\n");
246 	if (!TestHelpers::WaitAndConnectTwoPeersLocally(client,server,5000))//Make sure connected before test
247 	{
248 
249 		if (isVerbose)
250 			DebugTools::ShowError(errorList[11-1],!noPauses && isVerbose,__LINE__,__FILE__);
251 
252 		return 11;
253 	}
254 
255 	printf("Run recieve test\n");
256 	TestHelpers::BroadCastTestPacket(client);
257 	if (!TestHelpers::WaitForTestPacket(server,2000))
258 	{
259 
260 		if (isVerbose)
261 			DebugTools::ShowError(errorList[12-1],!noPauses && isVerbose,__LINE__,__FILE__);
262 
263 		return 12;
264 	}
265 
266 	return 0;
267 
268 }
269 
FloodWithHighPriority(RakPeerInterface * client)270 void PacketAndLowLevelTestsTest::FloodWithHighPriority(RakPeerInterface* client)
271 {
272 
273 	for (int i=0;i<60000;i++)
274 	{
275 		TestHelpers::BroadCastTestPacket(client,UNRELIABLE,HIGH_PRIORITY,ID_USER_PACKET_ENUM+2);
276 	}
277 
278 }
279 
GetTestName()280 RakNet::RakString PacketAndLowLevelTestsTest::GetTestName()
281 {
282 
283 	return "PacketAndLowLevelTestsTest";
284 
285 }
286 
ErrorCodeToString(int errorCode)287 RakNet::RakString PacketAndLowLevelTestsTest::ErrorCodeToString(int errorCode)
288 {
289 
290 	if (errorCode>0&&(unsigned int)errorCode<=errorList.Size())
291 	{
292 		return errorList[errorCode-1];
293 	}
294 	else
295 	{
296 		return "Undefined Error";
297 	}
298 
299 }
300 
DestroyPeers()301 void PacketAndLowLevelTestsTest::DestroyPeers()
302 {
303 
304 	int theSize=destroyList.Size();
305 
306 	for (int i=0; i < theSize; i++)
307 		RakNetworkFactory::DestroyRakPeerInterface(destroyList[i]);
308 
309 }
310 
PacketAndLowLevelTestsTest(void)311 PacketAndLowLevelTestsTest::PacketAndLowLevelTestsTest(void)
312 {
313 
314 	errorList.Push("Client failed to connect to server",__FILE__,__LINE__);
315 	errorList.Push("Attached plugin failed to modify packet",__FILE__,__LINE__);
316 	errorList.Push("Plugin is still modifying packets after detach",__FILE__,__LINE__);
317 	errorList.Push("GetSplitMessageProgressInterval returned wrong value",__FILE__,__LINE__);
318 	errorList.Push("Send to server failed",__FILE__,__LINE__);
319 	errorList.Push("Large packet did not split or did not properly get ID_DOWNLOAD_PROGRESS after SetSplitMessageProgressInterval is set to 1 millisecond",__FILE__,__LINE__);
320 	errorList.Push("Did not recieve and put on packet made with AllocatePacket and put on recieve stack with PushBackPacket",__FILE__,__LINE__);
321 	errorList.Push("Client failed to connect to server",__FILE__,__LINE__);
322 	errorList.Push("Did not recieve all packets from SendList",__FILE__,__LINE__);
323 	errorList.Push("Did not recieve and put on packet made with AllocatePacket and put on recieve stack with PushBackPacket",__FILE__,__LINE__);
324 	errorList.Push("Client failed to connect to server",__FILE__,__LINE__);
325 	errorList.Push("PushBackPacket messed up future communication",__FILE__,__LINE__);
326 	errorList.Push("Send/Recieve failed",__FILE__,__LINE__);
327 	errorList.Push("Recieved size incorrect",__FILE__,__LINE__);
328 
329 }
330 
~PacketAndLowLevelTestsTest(void)331 PacketAndLowLevelTestsTest::~PacketAndLowLevelTestsTest(void)
332 {
333 }