LocalIsConnectedTest.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "LocalIsConnectedTest.h"
  11. /*
  12. Description:
  13. Tests
  14. IsLocalIP
  15. SendLoopback
  16. GetConnectionState
  17. GetLocalIP
  18. GetInternalID
  19. Success conditions:
  20. All tests pass
  21. Failure conditions:
  22. Any test fails
  23. RakPeerInterface Functions used, tested indirectly by its use:
  24. Startup
  25. SetMaximumIncomingConnections
  26. Receive
  27. DeallocatePacket
  28. Send
  29. RakPeerInterface Functions Explicitly Tested:
  30. IsLocalIP
  31. SendLoopback
  32. GetConnectionState
  33. GetLocalIP
  34. GetInternalID
  35. */
  36. int LocalIsConnectedTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses)
  37. {
  38. RakPeerInterface *server,*client;
  39. destroyList.Clear(false,_FILE_AND_LINE_);
  40. server=RakPeerInterface::GetInstance();
  41. destroyList.Push(server,_FILE_AND_LINE_);
  42. client=RakPeerInterface::GetInstance();
  43. destroyList.Push(client,_FILE_AND_LINE_);
  44. client->Startup(1,&SocketDescriptor(),1);
  45. server->Startup(1,&SocketDescriptor(60000,0),1);
  46. server->SetMaximumIncomingConnections(1);
  47. SystemAddress serverAddress;
  48. serverAddress.SetBinaryAddress("127.0.0.1");
  49. serverAddress.port=60000;
  50. TimeMS entryTime=GetTimeMS();
  51. bool lastConnect=false;
  52. if (isVerbose)
  53. printf("Testing GetConnectionState\n");
  54. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000)
  55. {
  56. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  57. {
  58. lastConnect=client->Connect("127.0.0.1",serverAddress.port,0,0)==CONNECTION_ATTEMPT_STARTED;
  59. }
  60. RakSleep(100);
  61. }
  62. if (!lastConnect)//Use thise method to only check if the connect function fails, detecting connected client is done next
  63. {
  64. if (isVerbose)
  65. DebugTools::ShowError("Client could not connect after 5 seconds\n",!noPauses && isVerbose,__LINE__,__FILE__);
  66. return 1;
  67. }
  68. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true))
  69. {
  70. if (isVerbose)
  71. DebugTools::ShowError("IsConnected did not detect connected client",!noPauses && isVerbose,__LINE__,__FILE__);
  72. return 2;
  73. }
  74. client->CloseConnection (serverAddress,true,0,LOW_PRIORITY);
  75. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,false,false,true))
  76. {
  77. DebugTools::ShowError("IsConnected did not detect disconnecting client",!noPauses && isVerbose,__LINE__,__FILE__);
  78. return 3;
  79. }
  80. RakSleep(1000);
  81. client->Connect("127.0.0.1",serverAddress.port,0,0);
  82. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true))
  83. {
  84. DebugTools::ShowError("IsConnected did not detect connecting client",!noPauses && isVerbose,__LINE__,__FILE__);
  85. return 4;
  86. }
  87. entryTime=GetTimeMS();
  88. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000)
  89. {
  90. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  91. {
  92. client->Connect("127.0.0.1",serverAddress.port,0,0);
  93. }
  94. RakSleep(100);
  95. }
  96. if (!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true))
  97. {
  98. if (isVerbose)
  99. DebugTools::ShowError("Client could not connect after 5 seconds\n",!noPauses && isVerbose,__LINE__,__FILE__);
  100. return 1;
  101. }
  102. if (isVerbose)
  103. printf("Testing IsLocalIP\n");
  104. if (!client->IsLocalIP("127.0.0.1"))
  105. {
  106. if (isVerbose)
  107. DebugTools::ShowError("IsLocalIP failed test\n",!noPauses && isVerbose,__LINE__,__FILE__);
  108. return 5;
  109. }
  110. if (isVerbose)
  111. printf("Testing SendLoopback\n");
  112. char str[]="AAAAAAAAAA";
  113. str[0]=(char)(ID_USER_PACKET_ENUM+1);
  114. client->SendLoopback(str, (int) strlen(str)+1);
  115. client->SendLoopback(str, (int) strlen(str)+1);
  116. client->SendLoopback(str, (int) strlen(str)+1);
  117. client->SendLoopback(str, (int) strlen(str)+1);
  118. client->SendLoopback(str, (int) strlen(str)+1);
  119. client->SendLoopback(str, (int) strlen(str)+1);
  120. client->SendLoopback(str, (int) strlen(str)+1);
  121. bool recievedPacket=false;
  122. Packet *packet;
  123. TimeMS stopWaiting = GetTimeMS() + 1000;
  124. while (GetTimeMS()<stopWaiting)
  125. {
  126. for (packet=client->Receive(); packet; client->DeallocatePacket(packet), packet=client->Receive())
  127. {
  128. if (packet->data[0]==ID_USER_PACKET_ENUM+1)
  129. {
  130. recievedPacket=true;
  131. }
  132. }
  133. }
  134. if (!recievedPacket)
  135. {
  136. if (isVerbose)
  137. DebugTools::ShowError("SendLoopback failed test\n",!noPauses && isVerbose,__LINE__,__FILE__);
  138. return 6;
  139. }
  140. if (isVerbose)
  141. printf("Testing GetLocalIP\n");
  142. const char * localIp=client->GetLocalIP(0);
  143. if (!client->IsLocalIP(localIp))
  144. {
  145. if (isVerbose)
  146. DebugTools::ShowError("GetLocalIP failed test\n",!noPauses && isVerbose,__LINE__,__FILE__);
  147. return 7;
  148. }
  149. if (isVerbose)
  150. printf("Testing GetInternalID\n");
  151. SystemAddress localAddress=client->GetInternalID();
  152. char convertedIp[39];
  153. sprintf(convertedIp,"%d.%d.%d.%d", ((localAddress.binaryAddress >> (24 - 8 * 3)) & 0xFF),((localAddress.binaryAddress >> (24 - 16)) & 0xFF),((localAddress.binaryAddress >> (24 - 8 )) & 0xFF),((localAddress.binaryAddress >> (24)) & 0xFF));
  154. printf("GetInternalID returned %s\n",convertedIp);
  155. if (!client->IsLocalIP(convertedIp))
  156. {
  157. if (isVerbose)
  158. DebugTools::ShowError("GetInternalID failed test\n",!noPauses && isVerbose,__LINE__,__FILE__);
  159. return 8;
  160. }
  161. return 0;
  162. }
  163. RakString LocalIsConnectedTest::GetTestName()
  164. {
  165. return "LocalIsConnectedTest";
  166. }
  167. RakString LocalIsConnectedTest::ErrorCodeToString(int errorCode)
  168. {
  169. switch (errorCode)
  170. {
  171. case 0:
  172. return "No error";
  173. break;
  174. case 1:
  175. return "Client could not connect after 5 seconds";
  176. break;
  177. case 2:
  178. return "IsConnected did not detect connected client";
  179. break;
  180. case 3:
  181. return "IsConnected did not detect disconnecting client";
  182. break;
  183. case 4:
  184. return "IsConnected did not detect connecting client";
  185. break;
  186. case 5:
  187. return "IsLocalIP failed test";
  188. break;
  189. case 6:
  190. return "Sendloopback failed test";
  191. break;
  192. case 7:
  193. return "GetLocalIP failed test";
  194. break;
  195. case 8:
  196. return "GetInternalID failed test";
  197. break;
  198. default:
  199. return "Undefined Error";
  200. }
  201. }
  202. LocalIsConnectedTest::LocalIsConnectedTest(void)
  203. {
  204. }
  205. LocalIsConnectedTest::~LocalIsConnectedTest(void)
  206. {
  207. }
  208. void LocalIsConnectedTest::DestroyPeers()
  209. {
  210. int theSize=destroyList.Size();
  211. for (int i=0; i < theSize; i++)
  212. RakPeerInterface::DestroyInstance(destroyList[i]);
  213. }
粤ICP备19079148号