Skip to content
This repository was archived by the owner on Dec 23, 2022. It is now read-only.

Commit 191bc6f

Browse files
authored
Merge pull request #2 from TWaalen/fix-unit-tests
Fix ConnectionTests unit tests
2 parents d85b6ba + a2ee016 commit 191bc6f

File tree

4 files changed

+97
-51
lines changed

4 files changed

+97
-51
lines changed
Lines changed: 74 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Net;
1+
using System;
2+
using System.Net;
3+
using System.Threading;
24
using Xunit;
35

46
namespace RCONServerLib.Tests
@@ -11,17 +13,23 @@ public void TestAuthFail()
1113
var server = new RemoteConServer(IPAddress.Any, 27015);
1214
server.StartListening();
1315

14-
var client = new RemoteConClient();
15-
client.OnAuthResult += success =>
16+
bool authResult = false;
17+
using (var waitEvent = new AutoResetEvent(false))
1618
{
17-
Assert.False(success);
19+
var client = new RemoteConClient();
20+
client.OnAuthResult += success =>
21+
{
22+
authResult = success;
23+
24+
client.Disconnect();
25+
server.StopListening();
26+
};
1827

19-
client.Disconnect();
20-
server.StopListening();
21-
};
28+
client.Connect("127.0.0.1", 27015);
29+
client.Authenticate("unitfail");
30+
}
2231

23-
client.Connect("127.0.0.1", 27015);
24-
client.Authenticate("unitfail");
32+
Assert.False(authResult);
2533
}
2634

2735
[Fact]
@@ -30,17 +38,25 @@ public void TestAuthSuccess()
3038
var server = new RemoteConServer(IPAddress.Any, 27015);
3139
server.StartListening();
3240

33-
var client = new RemoteConClient();
34-
client.OnAuthResult += success =>
41+
bool authResult = false;
42+
using (var waitEvent = new AutoResetEvent(false))
3543
{
36-
Assert.True(success);
44+
var client = new RemoteConClient();
45+
client.OnAuthResult += success =>
46+
{
47+
authResult = success;
48+
49+
client.Disconnect();
50+
server.StopListening();
51+
waitEvent.Set();
52+
};
3753

38-
client.Disconnect();
39-
server.StopListening();
40-
};
54+
client.Connect("127.0.0.1", 27015);
55+
client.Authenticate("changeme");
56+
waitEvent.WaitOne();
57+
}
4158

42-
client.Connect("127.0.0.1", 27015);
43-
client.Authenticate("changeme");
59+
Assert.True(authResult);
4460
}
4561

4662
[Fact]
@@ -49,44 +65,59 @@ public void TestCommandFail()
4965
var server = new RemoteConServer(IPAddress.Any, 27015);
5066
server.StartListening();
5167

52-
var client = new RemoteConClient();
53-
client.OnAuthResult += success =>
68+
string commandResult = null;
69+
using (var waitEvent = new AutoResetEvent(false))
5470
{
55-
//Assert.True(success);
56-
client.SendCommand("testing", result =>
71+
var client = new RemoteConClient();
72+
client.OnAuthResult += success =>
5773
{
58-
Assert.Contains("invalid command", result);
59-
60-
client.Disconnect();
61-
server.StopListening();
62-
});
63-
};
64-
65-
client.Connect("127.0.0.1", 27015);
66-
client.Authenticate("changeme");
74+
client.SendCommand("testing", result =>
75+
{
76+
commandResult = result;
77+
78+
client.Disconnect();
79+
server.StopListening();
80+
waitEvent.Set();
81+
});
82+
};
83+
84+
client.Connect("127.0.0.1", 27015);
85+
client.Authenticate("changeme");
86+
waitEvent.WaitOne();
87+
}
88+
89+
Assert.Contains("Invalid command", commandResult);
6790
}
6891

6992
[Fact]
7093
public void TestCommandSuccess()
7194
{
7295
var server = new RemoteConServer(IPAddress.Any, 27015);
96+
server.CommandManager.Add("hello", "Replies with world", (command, args) => "world");
7397
server.StartListening();
7498

75-
var client = new RemoteConClient();
76-
client.OnAuthResult += success =>
99+
string commandResult = null;
100+
using (var waitEvent = new AutoResetEvent(false))
77101
{
78-
//Assert.True(success);
79-
client.SendCommand("hello", result =>
102+
var client = new RemoteConClient();
103+
client.OnAuthResult += success =>
80104
{
81-
Assert.Contains("world", result);
82-
83-
client.Disconnect();
84-
server.StopListening();
85-
});
86-
};
87-
88-
client.Connect("127.0.0.1", 27015);
89-
client.Authenticate("changeme");
105+
client.SendCommand("hello", result =>
106+
{
107+
commandResult = result;
108+
109+
client.Disconnect();
110+
server.StopListening();
111+
waitEvent.Set();
112+
});
113+
};
114+
115+
client.Connect("127.0.0.1", 27015);
116+
client.Authenticate("changeme");
117+
waitEvent.WaitOne();
118+
}
119+
120+
Assert.Contains("world", commandResult);
90121
}
91122
}
92123
}

RCONServerLib/RemoteConClient.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,22 @@ internal void ParsePacket(byte[] rawPacket)
267267
{
268268
// ExecCommand is AuthResponse too.
269269
if (packet.Type == RemoteConPacket.PacketType.ExecCommand)
270+
{
270271
if (packet.Id == -1)
271272
{
272273
Log("Authentication failed.");
273274
Authenticated = false;
274-
if (OnAuthResult != null)
275-
OnAuthResult(false);
276275
}
277276
else
278277
{
279278
Log("Authentication success.");
280279
Authenticated = true;
281-
if (OnAuthResult != null)
282-
OnAuthResult(false);
283280
}
284281

282+
if (OnAuthResult != null)
283+
OnAuthResult(Authenticated);
284+
}
285+
285286
return;
286287
}
287288

RCONServerLib/RemoteConServer.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,22 @@ public void StopListening()
151151
{
152152
if (!_listener.Server.IsBound)
153153
return;
154-
155-
_listener.Server.Shutdown(SocketShutdown.Both);
156-
_listener.Server.Close(0);
154+
155+
_listener.Stop();
157156
}
158157

159158
private void OnAccept(IAsyncResult result)
160159
{
161-
var tcpClient = _listener.EndAcceptTcpClient(result);
160+
TcpClient tcpClient;
161+
try
162+
{
163+
tcpClient = _listener.EndAcceptTcpClient(result);
164+
}
165+
catch (ObjectDisposedException)
166+
{
167+
LogDebug("Socket was closed");
168+
return;
169+
}
162170

163171
var ip = ((IPEndPoint) tcpClient.Client.RemoteEndPoint).Address;
164172

RCONServerLib/RemoteConTcpClient.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Net;
55
using System.Net.Sockets;
6+
using System.Threading;
67
using RCONServerLib.Utils;
78

89
namespace RCONServerLib
@@ -183,6 +184,11 @@ private void OnPacket(IAsyncResult result)
183184
_remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + " lost connection.");
184185
CloseConnection();
185186
}
187+
catch (ThreadAbortException)
188+
{
189+
_remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + " socket closed.");
190+
CloseConnection();
191+
}
186192
catch (RconServerException)
187193
{
188194
throw;

0 commit comments

Comments
 (0)