|
|
@@ -64,7 +64,14 @@ namespace Renci.SshNet.Tests.Classes
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- protected void Arrange()
|
|
|
+ private void CreateMocks()
|
|
|
+ {
|
|
|
+ _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
|
|
|
+ _sessionMock = new Mock<ISession>(MockBehavior.Strict);
|
|
|
+ _channelMock = new Mock<IChannelDirectTcpip>(MockBehavior.Strict);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void SetupData()
|
|
|
{
|
|
|
var random = new Random();
|
|
|
|
|
|
@@ -74,20 +81,30 @@ namespace Renci.SshNet.Tests.Classes
|
|
|
_remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"), random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
|
|
|
_bindSleepTime = TimeSpan.FromMilliseconds(random.Next(100, 500));
|
|
|
_userName = random.Next().ToString(CultureInfo.InvariantCulture);
|
|
|
- _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port);
|
|
|
+ _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint) _endpoint.Port);
|
|
|
_sessionException = new Exception();
|
|
|
_channelBindStarted = new ManualResetEvent(false);
|
|
|
_channelBindCompleted = new ManualResetEvent(false);
|
|
|
|
|
|
- _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
|
|
|
- _sessionMock = new Mock<ISession>(MockBehavior.Strict);
|
|
|
- _channelMock = new Mock<IChannelDirectTcpip>(MockBehavior.Strict);
|
|
|
+ _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
|
|
|
+ _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
|
|
|
+ _forwardedPort.Session = _sessionMock.Object;
|
|
|
+
|
|
|
+ _client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
|
|
|
+ {
|
|
|
+ ReceiveTimeout = 100,
|
|
|
+ SendTimeout = 100,
|
|
|
+ SendBufferSize = 0
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
+ private void SetupMocks()
|
|
|
+ {
|
|
|
_connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(15));
|
|
|
_sessionMock.Setup(p => p.IsConnected).Returns(true);
|
|
|
_sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
|
|
|
_sessionMock.Setup(p => p.CreateChannelDirectTcpip()).Returns(_channelMock.Object);
|
|
|
- _channelMock.Setup(p => p.Open(_remoteEndpoint.Address.ToString(), (uint) _remoteEndpoint.Port, _forwardedPort, It.IsAny<Socket>()));
|
|
|
+ _channelMock.Setup(p => p.Open(_remoteEndpoint.Address.ToString(), (uint)_remoteEndpoint.Port, _forwardedPort, It.IsAny<Socket>()));
|
|
|
_channelMock.Setup(p => p.IsOpen).Returns(true);
|
|
|
_channelMock.Setup(p => p.Bind()).Callback(() =>
|
|
|
{
|
|
|
@@ -97,19 +114,20 @@ namespace Renci.SshNet.Tests.Classes
|
|
|
});
|
|
|
_channelMock.Setup(p => p.Close());
|
|
|
_channelMock.Setup(p => p.Dispose());
|
|
|
+ }
|
|
|
|
|
|
- _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
|
|
|
- _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
|
|
|
- _forwardedPort.Session = _sessionMock.Object;
|
|
|
- _forwardedPort.Start();
|
|
|
+ protected void Arrange()
|
|
|
+ {
|
|
|
+ CreateMocks();
|
|
|
+ SetupData();
|
|
|
+ SetupMocks();
|
|
|
|
|
|
- _client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
|
|
|
- {
|
|
|
- ReceiveTimeout = 500,
|
|
|
- SendTimeout = 500,
|
|
|
- SendBufferSize = 0
|
|
|
- };
|
|
|
+ // start port
|
|
|
+ _forwardedPort.Start();
|
|
|
+ // connect to port
|
|
|
EstablishSocks4Connection(_client);
|
|
|
+ // wait until SOCKS client is bound to channel
|
|
|
+ Assert.IsTrue(_channelBindStarted.WaitOne(TimeSpan.FromMilliseconds(200)));
|
|
|
}
|
|
|
|
|
|
protected void Act()
|
|
|
@@ -149,9 +167,23 @@ namespace Renci.SshNet.Tests.Classes
|
|
|
[TestMethod]
|
|
|
public void BoundClientShouldNotBeClosed()
|
|
|
{
|
|
|
- // the forwarded port itself does not close the client connection when the channel is closed properly
|
|
|
+ // the forwarded port itself does not close the client connection; when the channel is closed properly
|
|
|
// it's the channel that will take care of closing the client connection
|
|
|
- _client.Send(new byte[] { 0x0a }, 0, 1, SocketFlags.None);
|
|
|
+ //
|
|
|
+ // we'll check if the client connection is still alive by attempting to receive, which should time out
|
|
|
+ // as the forwarded port (or its channel) are not sending anything
|
|
|
+
|
|
|
+ var buffer = new byte[1];
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ _client.Receive(buffer);
|
|
|
+ Assert.Fail();
|
|
|
+ }
|
|
|
+ catch (SocketException ex)
|
|
|
+ {
|
|
|
+ Assert.AreEqual(SocketError.TimedOut, ex.SocketErrorCode);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
[TestMethod]
|
|
|
@@ -219,9 +251,6 @@ namespace Renci.SshNet.Tests.Classes
|
|
|
var buffer = new byte[8];
|
|
|
var bytesRead = SocketAbstraction.Read(client, buffer, 0, buffer.Length, TimeSpan.FromMilliseconds(500));
|
|
|
Assert.AreEqual(buffer.Length, bytesRead);
|
|
|
-
|
|
|
- // wait until SOCKS client is bound to channel
|
|
|
- Assert.IsTrue(_channelBindStarted.WaitOne(TimeSpan.FromMilliseconds(200)));
|
|
|
}
|
|
|
}
|
|
|
}
|