TwoWayAuthenticationTest.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <cstdio>
  11. #include <cstring>
  12. #include <stdlib.h>
  13. #include "GetTime.h"
  14. #include "Rand.h"
  15. #include "RakPeerInterface.h"
  16. #include "MessageIdentifiers.h"
  17. #include "BitStream.h"
  18. #include "TwoWayAuthentication.h"
  19. #include "RakSleep.h"
  20. static const int NUM_PEERS=2;
  21. RakNet::RakPeerInterface *rakPeer[NUM_PEERS];
  22. RakNet::TwoWayAuthentication *twoWayAuthenticationPlugin[NUM_PEERS];
  23. int main(void)
  24. {
  25. int i;
  26. for (i=0; i < NUM_PEERS; i++)
  27. rakPeer[i]=RakNet::RakPeerInterface::GetInstance();
  28. printf("This project tests and demonstrates the two way authentication plugin.\n");
  29. printf("Difficulty: Beginner\n\n");
  30. int peerIndex;
  31. // Initialize the message handlers
  32. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  33. {
  34. twoWayAuthenticationPlugin[peerIndex]=RakNet::OP_NEW<RakNet::TwoWayAuthentication>(_FILE_AND_LINE_);
  35. rakPeer[peerIndex]->AttachPlugin(twoWayAuthenticationPlugin[peerIndex]);
  36. rakPeer[peerIndex]->SetMaximumIncomingConnections(NUM_PEERS);
  37. }
  38. // Initialize the peers
  39. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  40. {
  41. RakNet::SocketDescriptor socketDescriptor(60000+peerIndex,0);
  42. rakPeer[peerIndex]->Startup(NUM_PEERS, &socketDescriptor, 1);
  43. }
  44. // Connect each peer to the prior peer
  45. for (peerIndex=1; peerIndex < NUM_PEERS; peerIndex++)
  46. {
  47. rakPeer[peerIndex]->Connect("127.0.0.1", 60000+peerIndex-1, 0, 0);
  48. }
  49. RakSleep(100);
  50. printf("Peers initialized and connected.\n");
  51. twoWayAuthenticationPlugin[0]->AddPassword("PWD0", "Password0");
  52. twoWayAuthenticationPlugin[0]->AddPassword("PWD1", "Password1");
  53. twoWayAuthenticationPlugin[1]->AddPassword("PWD0", "Password0");
  54. bool b = twoWayAuthenticationPlugin[0]->Challenge("PWD0", rakPeer[0]->GetGUIDFromIndex(0));
  55. RakAssert(b);
  56. printf("Stage 0, instance 0 challenges instance 1 (should pass)\n");
  57. int stage=0;
  58. int stage4FailureCount=0;
  59. int passCount=0;
  60. bool quit=false;
  61. while (!quit)
  62. {
  63. RakNet::Packet *packet;
  64. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  65. {
  66. packet=rakPeer[peerIndex]->Receive();
  67. if (packet)
  68. {
  69. switch (packet->data[0])
  70. {
  71. case ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_SUCCESS:
  72. case ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_SUCCESS:
  73. {
  74. RakNet::BitStream bs(packet->data, packet->length, false);
  75. bs.IgnoreBytes(sizeof(RakNet::MessageID));
  76. RakNet::RakString password;
  77. bs.Read(password);
  78. if (packet->data[0]==ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_SUCCESS)
  79. printf("ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_SUCCESS with %s from %s\n", password.C_String(), packet->systemAddress.ToString(true));
  80. else
  81. printf("ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_SUCCESS with %s from %s\n", password.C_String(), packet->systemAddress.ToString(true));
  82. if (++passCount==2)
  83. {
  84. if (stage<=2)
  85. {
  86. if (stage==0)
  87. {
  88. printf("Stage 1, instance 1 challenges instance 0 (should pass)\n");
  89. twoWayAuthenticationPlugin[1]->Challenge("PWD0", rakPeer[1]->GetGUIDFromIndex(0));
  90. passCount=0;
  91. // stage==1
  92. }
  93. else if (stage==1)
  94. {
  95. printf("Stage 2, instance 1 re-challenges instance 0 (should pass)\n");
  96. twoWayAuthenticationPlugin[1]->Challenge("PWD0", rakPeer[1]->GetGUIDFromIndex(0));
  97. passCount=0;
  98. // stage==2
  99. }
  100. else
  101. {
  102. printf("Stage 3, instance 0 challenges with bad password (call should fail)\n");
  103. if (twoWayAuthenticationPlugin[0]->Challenge("PWD3", rakPeer[0]->GetGUIDFromIndex(0))==false)
  104. {
  105. printf("Passed stage 3\n");
  106. stage++;
  107. printf("Stage 4, instance 0 challenges with unknown password (should fail twice)\n");
  108. twoWayAuthenticationPlugin[0]->Challenge("PWD1", rakPeer[0]->GetGUIDFromIndex(0));
  109. }
  110. else
  111. {
  112. printf("Failed stage 3, Challenge() did not return false\n");
  113. }
  114. }
  115. stage++;
  116. }
  117. }
  118. }
  119. break;
  120. case ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_FAILURE:
  121. {
  122. printf("ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_FAILED from %s\n", packet->systemAddress.ToString(true));
  123. if (stage!=4)
  124. {
  125. printf("Failed stage %i\n", stage);
  126. }
  127. else
  128. {
  129. if (++stage4FailureCount==2)
  130. quit=true;
  131. }
  132. }
  133. break;
  134. case ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT:
  135. {
  136. RakNet::BitStream bs(packet->data, packet->length, false);
  137. bs.IgnoreBytes(sizeof(RakNet::MessageID));
  138. RakNet::RakString password;
  139. bs.Read(password);
  140. printf("ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT with %s from %s\n", password.C_String(), packet->systemAddress.ToString(true));
  141. printf("Failed stage %i\n", stage);
  142. }
  143. break;
  144. case ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_FAILURE:
  145. {
  146. RakNet::BitStream bs(packet->data, packet->length, false);
  147. bs.IgnoreBytes(sizeof(RakNet::MessageID));
  148. RakNet::RakString password;
  149. bs.Read(password);
  150. printf("ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_FAILED with %s from %s\n", password.C_String(), packet->systemAddress.ToString(true));
  151. if (stage!=4)
  152. {
  153. printf("Failed stage %i\n", stage);
  154. }
  155. else
  156. {
  157. if (++stage4FailureCount==2)
  158. quit=true;
  159. }
  160. }
  161. break;
  162. }
  163. rakPeer[peerIndex]->DeallocatePacket(packet);
  164. }
  165. }
  166. RakSleep(30);
  167. }
  168. for (i=0; i < NUM_PEERS; i++)
  169. RakNet::RakPeerInterface::DestroyInstance(rakPeer[i]);
  170. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  171. RakNet::OP_DELETE(twoWayAuthenticationPlugin[peerIndex], _FILE_AND_LINE_);
  172. return 1;
  173. }
粤ICP备19079148号