forked from facebookarchive/RakNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoWayAuthenticationTest.cpp
More file actions
191 lines (173 loc) · 6.09 KB
/
Copy pathTwoWayAuthenticationTest.cpp
File metadata and controls
191 lines (173 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright (c) 2014, Oculus VR, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include "GetTime.h"
#include "Rand.h"
#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"
#include "TwoWayAuthentication.h"
#include "RakSleep.h"
static const int NUM_PEERS=2;
RakNet::RakPeerInterface *rakPeer[NUM_PEERS];
RakNet::TwoWayAuthentication *twoWayAuthenticationPlugin[NUM_PEERS];
int main(void)
{
int i;
for (i=0; i < NUM_PEERS; i++)
rakPeer[i]=RakNet::RakPeerInterface::GetInstance();
printf("This project tests and demonstrates the two way authentication plugin.\n");
printf("Difficulty: Beginner\n\n");
int peerIndex;
// Initialize the message handlers
for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
{
twoWayAuthenticationPlugin[peerIndex]=RakNet::OP_NEW<RakNet::TwoWayAuthentication>(_FILE_AND_LINE_);
rakPeer[peerIndex]->AttachPlugin(twoWayAuthenticationPlugin[peerIndex]);
rakPeer[peerIndex]->SetMaximumIncomingConnections(NUM_PEERS);
}
// Initialize the peers
for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
{
RakNet::SocketDescriptor socketDescriptor(60000+peerIndex,0);
rakPeer[peerIndex]->Startup(NUM_PEERS, &socketDescriptor, 1);
}
// Connect each peer to the prior peer
for (peerIndex=1; peerIndex < NUM_PEERS; peerIndex++)
{
rakPeer[peerIndex]->Connect("127.0.0.1", 60000+peerIndex-1, 0, 0);
}
RakSleep(100);
printf("Peers initialized and connected.\n");
twoWayAuthenticationPlugin[0]->AddPassword("PWD0", "Password0");
twoWayAuthenticationPlugin[0]->AddPassword("PWD1", "Password1");
twoWayAuthenticationPlugin[1]->AddPassword("PWD0", "Password0");
bool b = twoWayAuthenticationPlugin[0]->Challenge("PWD0", rakPeer[0]->GetGUIDFromIndex(0));
RakAssert(b);
printf("Stage 0, instance 0 challenges instance 1 (should pass)\n");
int stage=0;
int stage4FailureCount=0;
int passCount=0;
bool quit=false;
while (!quit)
{
RakNet::Packet *packet;
for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
{
packet=rakPeer[peerIndex]->Receive();
if (packet)
{
switch (packet->data[0])
{
case ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_SUCCESS:
case ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_SUCCESS:
{
RakNet::BitStream bs(packet->data, packet->length, false);
bs.IgnoreBytes(sizeof(RakNet::MessageID));
RakNet::RakString password;
bs.Read(password);
if (packet->data[0]==ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_SUCCESS)
printf("ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_SUCCESS with %s from %s\n", password.C_String(), packet->systemAddress.ToString(true));
else
printf("ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_SUCCESS with %s from %s\n", password.C_String(), packet->systemAddress.ToString(true));
if (++passCount==2)
{
if (stage<=2)
{
if (stage==0)
{
printf("Stage 1, instance 1 challenges instance 0 (should pass)\n");
twoWayAuthenticationPlugin[1]->Challenge("PWD0", rakPeer[1]->GetGUIDFromIndex(0));
passCount=0;
// stage==1
}
else if (stage==1)
{
printf("Stage 2, instance 1 re-challenges instance 0 (should pass)\n");
twoWayAuthenticationPlugin[1]->Challenge("PWD0", rakPeer[1]->GetGUIDFromIndex(0));
passCount=0;
// stage==2
}
else
{
printf("Stage 3, instance 0 challenges with bad password (call should fail)\n");
if (twoWayAuthenticationPlugin[0]->Challenge("PWD3", rakPeer[0]->GetGUIDFromIndex(0))==false)
{
printf("Passed stage 3\n");
stage++;
printf("Stage 4, instance 0 challenges with unknown password (should fail twice)\n");
twoWayAuthenticationPlugin[0]->Challenge("PWD1", rakPeer[0]->GetGUIDFromIndex(0));
}
else
{
printf("Failed stage 3, Challenge() did not return false\n");
}
}
stage++;
}
}
}
break;
case ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_FAILURE:
{
printf("ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_FAILED from %s\n", packet->systemAddress.ToString(true));
if (stage!=4)
{
printf("Failed stage %i\n", stage);
}
else
{
if (++stage4FailureCount==2)
quit=true;
}
}
break;
case ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT:
{
RakNet::BitStream bs(packet->data, packet->length, false);
bs.IgnoreBytes(sizeof(RakNet::MessageID));
RakNet::RakString password;
bs.Read(password);
printf("ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT with %s from %s\n", password.C_String(), packet->systemAddress.ToString(true));
printf("Failed stage %i\n", stage);
}
break;
case ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_FAILURE:
{
RakNet::BitStream bs(packet->data, packet->length, false);
bs.IgnoreBytes(sizeof(RakNet::MessageID));
RakNet::RakString password;
bs.Read(password);
printf("ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_FAILED with %s from %s\n", password.C_String(), packet->systemAddress.ToString(true));
if (stage!=4)
{
printf("Failed stage %i\n", stage);
}
else
{
if (++stage4FailureCount==2)
quit=true;
}
}
break;
}
rakPeer[peerIndex]->DeallocatePacket(packet);
}
}
RakSleep(30);
}
for (i=0; i < NUM_PEERS; i++)
RakNet::RakPeerInterface::DestroyInstance(rakPeer[i]);
for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
RakNet::OP_DELETE(twoWayAuthenticationPlugin[peerIndex], _FILE_AND_LINE_);
return 1;
}