Skip to content

Commit e84850d

Browse files
committed
TEST: Make testing port assignment dynamic
When testing connectors and proxy connectors, initialize (proxy) servers first with port set to 0 (resulting in dynamic assignment of the port number). Then use the actual port number to initialize the connection info. This prevents failing test due to already occupied ports.
1 parent 987ce52 commit e84850d

File tree

36 files changed

+362
-356
lines changed

36 files changed

+362
-356
lines changed

src/Renci.SshNet.Tests/Classes/Connection/DirectConnectorTestBase.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ protected sealed override void Arrange()
3636
SetupMocks();
3737
}
3838

39-
protected ConnectionInfo CreateConnectionInfo(string hostName)
39+
protected ConnectionInfo CreateConnectionInfo(string hostName, int port)
4040
{
4141
return new ConnectionInfo(hostName,
42-
777,
42+
port,
4343
"user",
4444
new KeyboardInteractiveAuthenticationMethod("user"));
4545
}

src/Renci.SshNet.Tests/Classes/Connection/DirectConnectorTest_Connect_ConnectionRefusedByServer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected override void SetupData()
2020
{
2121
base.SetupData();
2222

23-
_connectionInfo = CreateConnectionInfo(IPAddress.Loopback.ToString());
23+
_connectionInfo = CreateConnectionInfo(IPAddress.Loopback.ToString(), 777);
2424
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(5000);
2525
_stopWatch = new Stopwatch();
2626
_actualException = null;

src/Renci.SshNet.Tests/Classes/Connection/DirectConnectorTest_Connect_ConnectionSucceeded.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ protected override void SetupData()
2626

2727
var random = new Random();
2828

29-
_connectionInfo = CreateConnectionInfo(IPAddress.Loopback.ToString());
29+
_server = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, 0));
30+
_server.Disconnected += (socket) => _disconnected = true;
31+
_server.Connected += (socket) => socket.Send(new byte[1] { 0x44 });
32+
_server.Start();
33+
34+
_connectionInfo = CreateConnectionInfo(IPAddress.Loopback.ToString(), ((IPEndPoint)_server.ListenerEndPoint).Port);
3035
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(random.Next(50, 200));
3136
_stopWatch = new Stopwatch();
3237
_disconnected = false;
3338

3439
_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
35-
36-
_server = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.Port));
37-
_server.Disconnected += (socket) => _disconnected = true;
38-
_server.Connected += (socket) => socket.Send(new byte[1] { 0x44 });
39-
_server.Start();
4040
}
4141

4242
protected override void SetupMocks()

src/Renci.SshNet.Tests/Classes/Connection/DirectConnectorTest_Connect_HostNameInvalid.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected override void SetupData()
1414
{
1515
base.SetupData();
1616

17-
_connectionInfo = CreateConnectionInfo("invalid.");
17+
_connectionInfo = CreateConnectionInfo("invalid.", 777);
1818
_actualException = null;
1919
}
2020

src/Renci.SshNet.Tests/Classes/Connection/DirectConnectorTest_Connect_TimeoutConnectingToServer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected override void SetupData()
2424

2525
var random = new Random();
2626

27-
_connectionInfo = CreateConnectionInfo(IPAddress.Loopback.ToString());
27+
_connectionInfo = CreateConnectionInfo(IPAddress.Loopback.ToString(), 777);
2828
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(random.Next(50, 200));
2929
_stopWatch = new Stopwatch();
3030
_actualException = null;

src/Renci.SshNet.Tests/Classes/Connection/HttpConnectorTest_Connect_ProxyClosesConnectionBeforeStatusLineIsSent.cs

+10-9
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,31 @@ public class HttpConnectorTest_Connect_ProxyClosesConnectionBeforeStatusLineIsSe
2323
protected override void SetupData()
2424
{
2525
base.SetupData();
26+
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, 0));
27+
_proxyServer.Disconnected += socket => _disconnected = true;
28+
_proxyServer.BytesReceived += (bytesReceived, socket) =>
29+
{
30+
socket.Shutdown(SocketShutdown.Send);
31+
};
32+
_proxyServer.Start();
2633

2734
_connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
2835
777,
2936
"user",
3037
ProxyTypes.Http,
3138
IPAddress.Loopback.ToString(),
32-
8122,
39+
((IPEndPoint)_proxyServer.ListenerEndPoint).Port,
3340
"proxyUser",
3441
"proxyPwd",
3542
new KeyboardInteractiveAuthenticationMethod("user"));
43+
44+
3645
_proxyConnectionInfo = (ProxyConnectionInfo)_connectionInfo.ProxyConnection;
3746
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(100);
3847
_actualException = null;
3948

4049
_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4150
_proxyConnector = ServiceFactory.CreateConnector(_proxyConnectionInfo, SocketFactoryMock.Object);
42-
43-
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _proxyConnectionInfo.Port));
44-
_proxyServer.Disconnected += socket => _disconnected = true;
45-
_proxyServer.BytesReceived += (bytesReceived, socket) =>
46-
{
47-
socket.Shutdown(SocketShutdown.Send);
48-
};
49-
_proxyServer.Start();
5051
}
5152

5253
protected override void SetupMocks()

src/Renci.SshNet.Tests/Classes/Connection/HttpConnectorTest_Connect_ProxyPasswordIsEmpty.cs

+19-19
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,30 @@ protected override void SetupData()
2828
{
2929
base.SetupData();
3030

31+
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, 0));
32+
_proxyServer.Disconnected += (socket) => _disconnected = true;
33+
_proxyServer.Connected += socket =>
34+
{
35+
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
36+
socket.Send(Encoding.ASCII.GetBytes("SSH.NET\r\n"));
37+
socket.Send(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
38+
socket.Send(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
39+
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
40+
socket.Send(Encoding.ASCII.GetBytes("SSH4EVER"));
41+
socket.Shutdown(SocketShutdown.Send);
42+
};
43+
_proxyServer.BytesReceived += (bytesReceived, socket) =>
44+
{
45+
_bytesReceivedByProxy.AddRange(bytesReceived);
46+
};
47+
_proxyServer.Start();
48+
3149
_connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
3250
777,
3351
"user",
3452
ProxyTypes.Http,
3553
IPAddress.Loopback.ToString(),
36-
8122,
54+
((IPEndPoint)_proxyServer.ListenerEndPoint).Port,
3755
"proxyUser",
3856
string.Empty,
3957
new KeyboardInteractiveAuthenticationMethod("user"));
@@ -48,24 +66,6 @@ protected override void SetupData()
4866
_disconnected = false;
4967
_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
5068
_proxyConnector = ServiceFactory.CreateConnector(_proxyConnectionInfo, SocketFactoryMock.Object);
51-
52-
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _proxyConnectionInfo.Port));
53-
_proxyServer.Disconnected += (socket) => _disconnected = true;
54-
_proxyServer.Connected += socket =>
55-
{
56-
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
57-
socket.Send(Encoding.ASCII.GetBytes("SSH.NET\r\n"));
58-
socket.Send(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
59-
socket.Send(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
60-
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
61-
socket.Send(Encoding.ASCII.GetBytes("SSH4EVER"));
62-
socket.Shutdown(SocketShutdown.Send);
63-
};
64-
_proxyServer.BytesReceived += (bytesReceived, socket) =>
65-
{
66-
_bytesReceivedByProxy.AddRange(bytesReceived);
67-
};
68-
_proxyServer.Start();
6969
}
7070

7171
protected override void SetupMocks()

src/Renci.SshNet.Tests/Classes/Connection/HttpConnectorTest_Connect_ProxyPasswordIsNull.cs

+19-19
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,30 @@ protected override void SetupData()
2828
{
2929
base.SetupData();
3030

31+
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, 0));
32+
_proxyServer.Disconnected += (socket) => _disconnected = true;
33+
_proxyServer.Connected += socket =>
34+
{
35+
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
36+
socket.Send(Encoding.ASCII.GetBytes("SSH.NET\r\n"));
37+
socket.Send(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
38+
socket.Send(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
39+
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
40+
socket.Send(Encoding.ASCII.GetBytes("SSH4EVER"));
41+
socket.Shutdown(SocketShutdown.Send);
42+
};
43+
_proxyServer.BytesReceived += (bytesReceived, socket) =>
44+
{
45+
_bytesReceivedByProxy.AddRange(bytesReceived);
46+
};
47+
_proxyServer.Start();
48+
3149
_connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
3250
777,
3351
"user",
3452
ProxyTypes.Http,
3553
IPAddress.Loopback.ToString(),
36-
8122,
54+
((IPEndPoint)_proxyServer.ListenerEndPoint).Port,
3755
"proxyUser",
3856
null,
3957
new KeyboardInteractiveAuthenticationMethod("user"));
@@ -48,24 +66,6 @@ protected override void SetupData()
4866
_disconnected = false;
4967
_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
5068
_proxyConnector = ServiceFactory.CreateConnector(_proxyConnectionInfo, SocketFactoryMock.Object);
51-
52-
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _proxyConnectionInfo.Port));
53-
_proxyServer.Disconnected += (socket) => _disconnected = true;
54-
_proxyServer.Connected += socket =>
55-
{
56-
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
57-
socket.Send(Encoding.ASCII.GetBytes("SSH.NET\r\n"));
58-
socket.Send(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
59-
socket.Send(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
60-
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
61-
socket.Send(Encoding.ASCII.GetBytes("SSH4EVER"));
62-
socket.Shutdown(SocketShutdown.Send);
63-
};
64-
_proxyServer.BytesReceived += (bytesReceived, socket) =>
65-
{
66-
_bytesReceivedByProxy.AddRange(bytesReceived);
67-
};
68-
_proxyServer.Start();
6969
}
7070

7171
protected override void SetupMocks()

src/Renci.SshNet.Tests/Classes/Connection/HttpConnectorTest_Connect_ProxyResponseDoesNotContainHttpStatusLine.cs

+15-15
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,26 @@ protected override void SetupData()
2727
{
2828
base.SetupData();
2929

30+
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, 0));
31+
_proxyServer.Disconnected += socket => _disconnected = true;
32+
_proxyServer.BytesReceived += (bytesReceived, socket) =>
33+
{
34+
if (_bytesReceivedByProxy.Count == 0)
35+
{
36+
socket.Send(Encoding.ASCII.GetBytes("Whatever\r\n"));
37+
socket.Shutdown(SocketShutdown.Send);
38+
}
39+
40+
_bytesReceivedByProxy.AddRange(bytesReceived);
41+
};
42+
_proxyServer.Start();
43+
3044
_connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
3145
777,
3246
"user",
3347
ProxyTypes.Http,
3448
IPAddress.Loopback.ToString(),
35-
8122,
49+
((IPEndPoint)_proxyServer.ListenerEndPoint).Port,
3650
"proxyUser",
3751
"proxyPwd",
3852
new KeyboardInteractiveAuthenticationMethod("user"));
@@ -43,20 +57,6 @@ protected override void SetupData()
4357

4458
_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4559
_proxyConnector = ServiceFactory.CreateConnector(_proxyConnectionInfo, SocketFactoryMock.Object);
46-
47-
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _proxyConnectionInfo.Port));
48-
_proxyServer.Disconnected += socket => _disconnected = true;
49-
_proxyServer.BytesReceived += (bytesReceived, socket) =>
50-
{
51-
if (_bytesReceivedByProxy.Count == 0)
52-
{
53-
socket.Send(Encoding.ASCII.GetBytes("Whatever\r\n"));
54-
socket.Shutdown(SocketShutdown.Send);
55-
}
56-
57-
_bytesReceivedByProxy.AddRange(bytesReceived);
58-
};
59-
_proxyServer.Start();
6060
}
6161

6262
protected override void SetupMocks()

src/Renci.SshNet.Tests/Classes/Connection/HttpConnectorTest_Connect_ProxyResponseStatusIs200_ExtraTextBeforeStatusLine.cs

+22-22
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,7 @@ protected override void SetupData()
2828
{
2929
base.SetupData();
3030

31-
_connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
32-
777,
33-
"user",
34-
ProxyTypes.Http,
35-
IPAddress.Loopback.ToString(),
36-
8122,
37-
"proxyUser",
38-
"proxyPwd",
39-
new KeyboardInteractiveAuthenticationMethod("user"));
40-
_proxyConnectionInfo = (ProxyConnectionInfo)_connectionInfo.ProxyConnection;
41-
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(20);
42-
_expectedHttpRequest = string.Format("CONNECT {0}:{1} HTTP/1.0{2}" +
43-
"Proxy-Authorization: Basic cHJveHlVc2VyOnByb3h5UHdk{2}{2}",
44-
_connectionInfo.Host,
45-
_connectionInfo.Port.ToString(CultureInfo.InvariantCulture),
46-
"\r\n");
47-
_bytesReceivedByProxy = new List<byte>();
48-
_disconnected = false;
49-
_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
50-
_proxyConnector = ServiceFactory.CreateConnector(_proxyConnectionInfo, SocketFactoryMock.Object);
51-
52-
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _proxyConnectionInfo.Port));
31+
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, 0));
5332
_proxyServer.Disconnected += (socket) => _disconnected = true;
5433
_proxyServer.BytesReceived += (bytesReceived, socket) =>
5534
{
@@ -70,6 +49,27 @@ protected override void SetupData()
7049
}
7150
};
7251
_proxyServer.Start();
52+
53+
_connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
54+
777,
55+
"user",
56+
ProxyTypes.Http,
57+
IPAddress.Loopback.ToString(),
58+
((IPEndPoint)_proxyServer.ListenerEndPoint).Port,
59+
"proxyUser",
60+
"proxyPwd",
61+
new KeyboardInteractiveAuthenticationMethod("user"));
62+
_proxyConnectionInfo = (ProxyConnectionInfo)_connectionInfo.ProxyConnection;
63+
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(20);
64+
_expectedHttpRequest = string.Format("CONNECT {0}:{1} HTTP/1.0{2}" +
65+
"Proxy-Authorization: Basic cHJveHlVc2VyOnByb3h5UHdk{2}{2}",
66+
_connectionInfo.Host,
67+
_connectionInfo.Port.ToString(CultureInfo.InvariantCulture),
68+
"\r\n");
69+
_bytesReceivedByProxy = new List<byte>();
70+
_disconnected = false;
71+
_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
72+
_proxyConnector = ServiceFactory.CreateConnector(_proxyConnectionInfo, SocketFactoryMock.Object);
7373
}
7474

7575
protected override void SetupMocks()

0 commit comments

Comments
 (0)