Skip to content

Commit 8f0da78

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 8f0da78

File tree

41 files changed

+388
-372
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+388
-372
lines changed

src/Renci.SshNet.Tests/Classes/Channels/ChannelDirectTcpipTest.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void SocketShouldBeClosedAndBindShouldEndWhenForwardedPortSignalsClosingE
6464
_sessionMock.Setup(p => p.WaitOnHandle(It.IsAny<EventWaitHandle>()))
6565
.Callback<WaitHandle>(p => p.WaitOne(Session.Infinite));
6666

67-
var localPortEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
67+
var localPortEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
6868
using (var localPortListener = new AsyncSocketListener(localPortEndPoint))
6969
{
7070
localPortListener.Start();
@@ -91,6 +91,7 @@ public void SocketShouldBeClosedAndBindShouldEndWhenForwardedPortSignalsClosingE
9191

9292
closeForwardedPortThread.Join();
9393
};
94+
localPortEndPoint.Port = ((IPEndPoint)localPortListener.ListenerEndPoint).Port;
9495

9596
var client = new Socket(localPortEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
9697
client.Connect(localPortEndPoint);
@@ -119,7 +120,7 @@ public void SocketShouldBeClosedAndBindShouldEndWhenOnErrorOccurredIsInvoked()
119120
_sessionMock.Setup(p => p.WaitOnHandle(It.IsAny<EventWaitHandle>()))
120121
.Callback<WaitHandle>(p => p.WaitOne(Session.Infinite));
121122

122-
var localPortEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
123+
var localPortEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
123124
using (var localPortListener = new AsyncSocketListener(localPortEndPoint))
124125
{
125126
localPortListener.Start();
@@ -147,6 +148,7 @@ public void SocketShouldBeClosedAndBindShouldEndWhenOnErrorOccurredIsInvoked()
147148

148149
signalSessionErrorOccurredThread.Join();
149150
};
151+
localPortEndPoint.Port = ((IPEndPoint)localPortListener.ListenerEndPoint).Port;
150152

151153
var client = new Socket(localPortEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
152154
client.Connect(localPortEndPoint);
@@ -211,7 +213,7 @@ public void SocketShouldBeClosedAndEofShouldBeSentToServerWhenClientShutsDownSoc
211213
Socket handler = null;
212214
ChannelDirectTcpip channel = null;
213215

214-
var localPortEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
216+
var localPortEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
215217
using (var localPortListener = new AsyncSocketListener(localPortEndPoint))
216218
{
217219
localPortListener.Start();
@@ -230,6 +232,7 @@ public void SocketShouldBeClosedAndEofShouldBeSentToServerWhenClientShutsDownSoc
230232

231233
channelBindFinishedWaitHandle.Set();
232234
};
235+
localPortEndPoint.Port = ((IPEndPoint)localPortListener.ListenerEndPoint).Port;
233236

234237
var client = new Socket(localPortEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
235238
client.Connect(localPortEndPoint);

src/Renci.SshNet.Tests/Classes/Channels/ChannelDirectTcpipTest_Dispose_SessionIsConnectedAndChannelIsOpen.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private void Arrange()
122122
})
123123
.Returns(WaitResult.Success);
124124

125-
var localEndpoint = new IPEndPoint(IPAddress.Loopback, 8122);
125+
var localEndpoint = new IPEndPoint(IPAddress.Loopback, 0);
126126
_listener = new AsyncSocketListener(localEndpoint);
127127
_listener.Connected += socket =>
128128
{
@@ -145,6 +145,7 @@ private void Arrange()
145145
}
146146
};
147147
_listener.Start();
148+
localEndpoint.Port = ((IPEndPoint)_listener.ListenerEndPoint).Port;
148149

149150
_client = new Socket(localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
150151
_client.Connect(localEndpoint);

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()

0 commit comments

Comments
 (0)