OfflineMessagesConvertTest.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "OfflineMessagesConvertTest.h"
  11. /*
  12. Description:
  13. Description: Tests / Demonstrates sending messages to systems you are not connected to.
  14. Success conditions:
  15. Proper offline response.
  16. Proper offline ping response.
  17. Failure conditions:
  18. Any success conditions failed
  19. RakPeerInterface Functions used, tested indirectly by its use:
  20. GetGuidFromSystemAddress
  21. Startup
  22. SetMaximumIncomingConnections
  23. Receive
  24. DeallocatePacket
  25. RakPeerInterface Functions Explicitly Tested:
  26. SetOfflinePingResponse
  27. GetOfflinePingResponse
  28. AdvertiseSystem
  29. Ping
  30. */
  31. int OfflineMessagesConvertTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses)
  32. {
  33. bool recievedProperOfflineData=false;
  34. bool recievedProperPingData=false;
  35. int nextTest;
  36. destroyList.Clear(false,_FILE_AND_LINE_);
  37. RakPeerInterface *peer1=RakPeerInterface::GetInstance();
  38. destroyList.Push( peer1,_FILE_AND_LINE_);
  39. RakPeerInterface *peer2=RakPeerInterface::GetInstance();
  40. destroyList.Push(peer2,_FILE_AND_LINE_);
  41. bool sentPacket=false;
  42. nextTest=0;
  43. peer1->SetMaximumIncomingConnections(1);
  44. SocketDescriptor socketDescriptor(60001, 0);
  45. peer1->Startup(1, &socketDescriptor, 1);
  46. socketDescriptor.port=60002;
  47. peer2->Startup(1, &socketDescriptor, 1);
  48. char * pingResponseData=0;
  49. unsigned int responseLen=0;
  50. peer1->SetOfflinePingResponse("Offline Ping Data", (int)strlen("Offline Ping Data")+1);
  51. peer1->GetOfflinePingResponse(&pingResponseData,&responseLen);
  52. if(strcmp(pingResponseData,"Offline Ping Data")!=0)
  53. {
  54. if (isVerbose)
  55. DebugTools::ShowError("GetOfflinePingResponse failed.\n",!noPauses && isVerbose,__LINE__,__FILE__);
  56. return 5;
  57. }
  58. if (isVerbose)
  59. printf("Peer 1 guid = %s\n", peer1->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS).ToString());
  60. if (isVerbose)
  61. printf("Peer 2 guid = %s\n", peer2->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS).ToString());
  62. if (isVerbose)
  63. printf("Systems started. Waiting for advertise system packet\n");
  64. // Wait for connection to complete
  65. RakSleep(300);
  66. if (isVerbose)
  67. printf("Sending advertise system from %s\n", peer1->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS).ToString());
  68. peer1->AdvertiseSystem("127.0.0.1", 60002,"hello world", (int)strlen("hello world")+1);
  69. TimeMS entryTime=GetTimeMS();//Loop entry time
  70. while (nextTest!=2&&GetTimeMS()-entryTime<10000)// run for 10 seconds
  71. {
  72. peer1->DeallocatePacket(peer1->Receive());
  73. Packet *packet = peer2->Receive();
  74. if (packet)
  75. {
  76. if (packet->data[0]==ID_ADVERTISE_SYSTEM)
  77. {
  78. if (packet->length>1)
  79. {
  80. if (isVerbose)
  81. printf("Got Advertise system with data: %s\n", packet->data+1);
  82. if(strcmp((const char*)(packet->data+1),"hello world")==0)
  83. {
  84. recievedProperOfflineData=true;
  85. }
  86. else
  87. {
  88. if (isVerbose)
  89. DebugTools::ShowError("Got Advertise system with unexpected data\n",!noPauses && isVerbose,__LINE__,__FILE__);
  90. return 1;
  91. }
  92. }
  93. else
  94. {
  95. if (isVerbose)
  96. DebugTools::ShowError("Got Advertise system with unexpected data\n",!noPauses && isVerbose,__LINE__,__FILE__);
  97. return 1;
  98. }
  99. if (isVerbose)
  100. printf("Was sent from GUID %s\n", packet->guid.ToString());
  101. if (isVerbose)
  102. printf("Sending ping from %s\n", peer2->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS).ToString());
  103. peer2->Ping("127.0.0.1", 60001, false);
  104. nextTest++;
  105. }
  106. else if (packet->data[0]==ID_UNCONNECTED_PONG)
  107. {
  108. // Peer or client. Response from a ping for an unconnected system.
  109. TimeMS packetTime, dataLength;
  110. TimeMS curTime = GetTimeMS();
  111. memcpy( ( char* ) & packetTime, packet->data + sizeof( unsigned char ), sizeof( TimeMS ) );
  112. dataLength = packet->length - sizeof( unsigned char ) - sizeof( TimeMS );
  113. if (peer2->IsLocalIP(packet->systemAddress.ToString(false)))
  114. {
  115. if (isVerbose)
  116. printf("ID_UNCONNECTED_PONG from our own");
  117. }
  118. else
  119. {
  120. if (isVerbose)
  121. printf( "ID_UNCONNECTED_PONG from");
  122. }
  123. if (isVerbose)
  124. {
  125. printf(" %s on %p.\nPing is %i\nData is %i bytes long.\n", packet->systemAddress.ToString(), peer2, curTime-packetTime, dataLength );
  126. printf("Was sent from GUID %s\n", packet->guid.ToString());
  127. }
  128. const char * recString=(const char *)(packet->data + sizeof( unsigned char ) + sizeof( TimeMS ));
  129. if ( dataLength > 0 )
  130. {
  131. printf( "Data is %s\n",recString );
  132. if (strcmp(recString, "Offline Ping Data")!=0)
  133. {
  134. if (isVerbose)
  135. DebugTools::ShowError("Received wrong offline ping response\n",!noPauses && isVerbose,__LINE__,__FILE__);
  136. return 2;
  137. }
  138. recievedProperPingData=true;
  139. }
  140. nextTest++;
  141. // ProcessUnhandledPacket(packet, ID_UNCONNECTED_PONG,interfaceType);
  142. }
  143. peer2->DeallocatePacket(packet);
  144. }
  145. RakSleep(30);
  146. }
  147. if (!recievedProperOfflineData)
  148. {
  149. if (isVerbose)
  150. DebugTools::ShowError("Never got proper offline data\n",!noPauses && isVerbose,__LINE__,__FILE__);
  151. return 3;
  152. }
  153. if (!recievedProperPingData)
  154. {
  155. if (isVerbose)
  156. DebugTools::ShowError("Never got proper ping data\n",!noPauses && isVerbose,__LINE__,__FILE__);
  157. return 4;
  158. }
  159. return 0;
  160. }
  161. RakString OfflineMessagesConvertTest::GetTestName()
  162. {
  163. return "OfflineMessagesConvertTest";
  164. }
  165. RakString OfflineMessagesConvertTest::ErrorCodeToString(int errorCode)
  166. {
  167. switch (errorCode)
  168. {
  169. case 0:
  170. return "No error";
  171. break;
  172. case 1:
  173. return "Unexpected advertise data";
  174. break;
  175. case 2:
  176. return "Wrong ping response";
  177. break;
  178. case 3:
  179. return "Never got proper offline data";
  180. break;
  181. case 4:
  182. return "Never got proper ping data";
  183. break;
  184. case 5:
  185. return "GetOfflinePingResponse failed.";
  186. break;
  187. default:
  188. return "Undefined Error";
  189. }
  190. }
  191. OfflineMessagesConvertTest::OfflineMessagesConvertTest(void)
  192. {
  193. }
  194. OfflineMessagesConvertTest::~OfflineMessagesConvertTest(void)
  195. {
  196. }
  197. void OfflineMessagesConvertTest::DestroyPeers()
  198. {
  199. int theSize=destroyList.Size();
  200. for (int i=0; i < theSize; i++)
  201. RakPeerInterface::DestroyInstance(destroyList[i]);
  202. }
粤ICP备19079148号