Skip to content

Commit

Permalink
TEST: Make testing port assignment dynamic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JoostJM committed Apr 28, 2022
1 parent 987ce52 commit e84850d
Show file tree
Hide file tree
Showing 36 changed files with 362 additions and 356 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ protected sealed override void Arrange()
SetupMocks();
}

protected ConnectionInfo CreateConnectionInfo(string hostName)
protected ConnectionInfo CreateConnectionInfo(string hostName, int port)
{
return new ConnectionInfo(hostName,
777,
port,
"user",
new KeyboardInteractiveAuthenticationMethod("user"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected override void SetupData()
{
base.SetupData();

_connectionInfo = CreateConnectionInfo(IPAddress.Loopback.ToString());
_connectionInfo = CreateConnectionInfo(IPAddress.Loopback.ToString(), 777);
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(5000);
_stopWatch = new Stopwatch();
_actualException = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ protected override void SetupData()

var random = new Random();

_connectionInfo = CreateConnectionInfo(IPAddress.Loopback.ToString());
_server = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, 0));
_server.Disconnected += (socket) => _disconnected = true;
_server.Connected += (socket) => socket.Send(new byte[1] { 0x44 });
_server.Start();

_connectionInfo = CreateConnectionInfo(IPAddress.Loopback.ToString(), ((IPEndPoint)_server.ListenerEndPoint).Port);
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(random.Next(50, 200));
_stopWatch = new Stopwatch();
_disconnected = false;

_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

_server = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.Port));
_server.Disconnected += (socket) => _disconnected = true;
_server.Connected += (socket) => socket.Send(new byte[1] { 0x44 });
_server.Start();
}

protected override void SetupMocks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected override void SetupData()
{
base.SetupData();

_connectionInfo = CreateConnectionInfo("invalid.");
_connectionInfo = CreateConnectionInfo("invalid.", 777);
_actualException = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override void SetupData()

var random = new Random();

_connectionInfo = CreateConnectionInfo(IPAddress.Loopback.ToString());
_connectionInfo = CreateConnectionInfo(IPAddress.Loopback.ToString(), 777);
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(random.Next(50, 200));
_stopWatch = new Stopwatch();
_actualException = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,31 @@ public class HttpConnectorTest_Connect_ProxyClosesConnectionBeforeStatusLineIsSe
protected override void SetupData()
{
base.SetupData();
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, 0));
_proxyServer.Disconnected += socket => _disconnected = true;
_proxyServer.BytesReceived += (bytesReceived, socket) =>
{
socket.Shutdown(SocketShutdown.Send);
};
_proxyServer.Start();

_connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
777,
"user",
ProxyTypes.Http,
IPAddress.Loopback.ToString(),
8122,
((IPEndPoint)_proxyServer.ListenerEndPoint).Port,
"proxyUser",
"proxyPwd",
new KeyboardInteractiveAuthenticationMethod("user"));


_proxyConnectionInfo = (ProxyConnectionInfo)_connectionInfo.ProxyConnection;
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(100);
_actualException = null;

_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_proxyConnector = ServiceFactory.CreateConnector(_proxyConnectionInfo, SocketFactoryMock.Object);

_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _proxyConnectionInfo.Port));
_proxyServer.Disconnected += socket => _disconnected = true;
_proxyServer.BytesReceived += (bytesReceived, socket) =>
{
socket.Shutdown(SocketShutdown.Send);
};
_proxyServer.Start();
}

protected override void SetupMocks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,30 @@ protected override void SetupData()
{
base.SetupData();

_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, 0));
_proxyServer.Disconnected += (socket) => _disconnected = true;
_proxyServer.Connected += socket =>
{
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
socket.Send(Encoding.ASCII.GetBytes("SSH.NET\r\n"));
socket.Send(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
socket.Send(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
socket.Send(Encoding.ASCII.GetBytes("SSH4EVER"));
socket.Shutdown(SocketShutdown.Send);
};
_proxyServer.BytesReceived += (bytesReceived, socket) =>
{
_bytesReceivedByProxy.AddRange(bytesReceived);
};
_proxyServer.Start();

_connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
777,
"user",
ProxyTypes.Http,
IPAddress.Loopback.ToString(),
8122,
((IPEndPoint)_proxyServer.ListenerEndPoint).Port,
"proxyUser",
string.Empty,
new KeyboardInteractiveAuthenticationMethod("user"));
Expand All @@ -48,24 +66,6 @@ protected override void SetupData()
_disconnected = false;
_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_proxyConnector = ServiceFactory.CreateConnector(_proxyConnectionInfo, SocketFactoryMock.Object);

_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _proxyConnectionInfo.Port));
_proxyServer.Disconnected += (socket) => _disconnected = true;
_proxyServer.Connected += socket =>
{
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
socket.Send(Encoding.ASCII.GetBytes("SSH.NET\r\n"));
socket.Send(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
socket.Send(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
socket.Send(Encoding.ASCII.GetBytes("SSH4EVER"));
socket.Shutdown(SocketShutdown.Send);
};
_proxyServer.BytesReceived += (bytesReceived, socket) =>
{
_bytesReceivedByProxy.AddRange(bytesReceived);
};
_proxyServer.Start();
}

protected override void SetupMocks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,30 @@ protected override void SetupData()
{
base.SetupData();

_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, 0));
_proxyServer.Disconnected += (socket) => _disconnected = true;
_proxyServer.Connected += socket =>
{
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
socket.Send(Encoding.ASCII.GetBytes("SSH.NET\r\n"));
socket.Send(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
socket.Send(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
socket.Send(Encoding.ASCII.GetBytes("SSH4EVER"));
socket.Shutdown(SocketShutdown.Send);
};
_proxyServer.BytesReceived += (bytesReceived, socket) =>
{
_bytesReceivedByProxy.AddRange(bytesReceived);
};
_proxyServer.Start();

_connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
777,
"user",
ProxyTypes.Http,
IPAddress.Loopback.ToString(),
8122,
((IPEndPoint)_proxyServer.ListenerEndPoint).Port,
"proxyUser",
null,
new KeyboardInteractiveAuthenticationMethod("user"));
Expand All @@ -48,24 +66,6 @@ protected override void SetupData()
_disconnected = false;
_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_proxyConnector = ServiceFactory.CreateConnector(_proxyConnectionInfo, SocketFactoryMock.Object);

_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _proxyConnectionInfo.Port));
_proxyServer.Disconnected += (socket) => _disconnected = true;
_proxyServer.Connected += socket =>
{
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
socket.Send(Encoding.ASCII.GetBytes("SSH.NET\r\n"));
socket.Send(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
socket.Send(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
socket.Send(Encoding.ASCII.GetBytes("\r\n"));
socket.Send(Encoding.ASCII.GetBytes("SSH4EVER"));
socket.Shutdown(SocketShutdown.Send);
};
_proxyServer.BytesReceived += (bytesReceived, socket) =>
{
_bytesReceivedByProxy.AddRange(bytesReceived);
};
_proxyServer.Start();
}

protected override void SetupMocks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,26 @@ protected override void SetupData()
{
base.SetupData();

_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, 0));
_proxyServer.Disconnected += socket => _disconnected = true;
_proxyServer.BytesReceived += (bytesReceived, socket) =>
{
if (_bytesReceivedByProxy.Count == 0)
{
socket.Send(Encoding.ASCII.GetBytes("Whatever\r\n"));
socket.Shutdown(SocketShutdown.Send);
}

_bytesReceivedByProxy.AddRange(bytesReceived);
};
_proxyServer.Start();

_connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
777,
"user",
ProxyTypes.Http,
IPAddress.Loopback.ToString(),
8122,
((IPEndPoint)_proxyServer.ListenerEndPoint).Port,
"proxyUser",
"proxyPwd",
new KeyboardInteractiveAuthenticationMethod("user"));
Expand All @@ -43,20 +57,6 @@ protected override void SetupData()

_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_proxyConnector = ServiceFactory.CreateConnector(_proxyConnectionInfo, SocketFactoryMock.Object);

_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _proxyConnectionInfo.Port));
_proxyServer.Disconnected += socket => _disconnected = true;
_proxyServer.BytesReceived += (bytesReceived, socket) =>
{
if (_bytesReceivedByProxy.Count == 0)
{
socket.Send(Encoding.ASCII.GetBytes("Whatever\r\n"));
socket.Shutdown(SocketShutdown.Send);
}

_bytesReceivedByProxy.AddRange(bytesReceived);
};
_proxyServer.Start();
}

protected override void SetupMocks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,7 @@ protected override void SetupData()
{
base.SetupData();

_connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
777,
"user",
ProxyTypes.Http,
IPAddress.Loopback.ToString(),
8122,
"proxyUser",
"proxyPwd",
new KeyboardInteractiveAuthenticationMethod("user"));
_proxyConnectionInfo = (ProxyConnectionInfo)_connectionInfo.ProxyConnection;
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(20);
_expectedHttpRequest = string.Format("CONNECT {0}:{1} HTTP/1.0{2}" +
"Proxy-Authorization: Basic cHJveHlVc2VyOnByb3h5UHdk{2}{2}",
_connectionInfo.Host,
_connectionInfo.Port.ToString(CultureInfo.InvariantCulture),
"\r\n");
_bytesReceivedByProxy = new List<byte>();
_disconnected = false;
_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_proxyConnector = ServiceFactory.CreateConnector(_proxyConnectionInfo, SocketFactoryMock.Object);

_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _proxyConnectionInfo.Port));
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, 0));
_proxyServer.Disconnected += (socket) => _disconnected = true;
_proxyServer.BytesReceived += (bytesReceived, socket) =>
{
Expand All @@ -70,6 +49,27 @@ protected override void SetupData()
}
};
_proxyServer.Start();

_connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
777,
"user",
ProxyTypes.Http,
IPAddress.Loopback.ToString(),
((IPEndPoint)_proxyServer.ListenerEndPoint).Port,
"proxyUser",
"proxyPwd",
new KeyboardInteractiveAuthenticationMethod("user"));
_proxyConnectionInfo = (ProxyConnectionInfo)_connectionInfo.ProxyConnection;
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(20);
_expectedHttpRequest = string.Format("CONNECT {0}:{1} HTTP/1.0{2}" +
"Proxy-Authorization: Basic cHJveHlVc2VyOnByb3h5UHdk{2}{2}",
_connectionInfo.Host,
_connectionInfo.Port.ToString(CultureInfo.InvariantCulture),
"\r\n");
_bytesReceivedByProxy = new List<byte>();
_disconnected = false;
_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_proxyConnector = ServiceFactory.CreateConnector(_proxyConnectionInfo, SocketFactoryMock.Object);
}

protected override void SetupMocks()
Expand Down
Loading

0 comments on commit e84850d

Please sign in to comment.