|
|
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|
|
using System.Net;
|
|
|
using System.Net.Sockets;
|
|
|
using System.Text;
|
|
|
+using System.Threading;
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
using Moq;
|
|
|
using Renci.SshNet.Channels;
|
|
|
@@ -21,6 +22,8 @@ namespace Renci.SshNet.Tests.Classes
|
|
|
private IList<EventArgs> _closingRegister;
|
|
|
private IList<ExceptionEventArgs> _exceptionRegister;
|
|
|
private TimeSpan _connectionTimeout;
|
|
|
+ private ManualResetEvent _channelDisposed;
|
|
|
+ private IPEndPoint _forwardedPortEndPoint;
|
|
|
|
|
|
[TestInitialize]
|
|
|
public void Initialize()
|
|
|
@@ -38,6 +41,7 @@ namespace Renci.SshNet.Tests.Classes
|
|
|
_connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(5));
|
|
|
_forwardedPort.Stop();
|
|
|
}
|
|
|
+
|
|
|
if (_client != null)
|
|
|
{
|
|
|
if (_client.Connected)
|
|
|
@@ -47,6 +51,12 @@ namespace Renci.SshNet.Tests.Classes
|
|
|
_client = null;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ if (_channelDisposed != null)
|
|
|
+ {
|
|
|
+ _channelDisposed.Dispose();
|
|
|
+ _channelDisposed = null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private void SetupData()
|
|
|
@@ -54,6 +64,15 @@ namespace Renci.SshNet.Tests.Classes
|
|
|
_closingRegister = new List<EventArgs>();
|
|
|
_exceptionRegister = new List<ExceptionEventArgs>();
|
|
|
_connectionTimeout = TimeSpan.FromSeconds(5);
|
|
|
+ _channelDisposed = new ManualResetEvent(false);
|
|
|
+ _forwardedPortEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
|
|
|
+
|
|
|
+ _forwardedPort = new ForwardedPortDynamic((uint) _forwardedPortEndPoint.Port);
|
|
|
+ _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
|
|
|
+ _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
|
|
|
+ _forwardedPort.Session = _sessionMock.Object;
|
|
|
+
|
|
|
+ _client = new Socket(_forwardedPortEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
|
|
}
|
|
|
|
|
|
private void CreateMocks()
|
|
|
@@ -72,30 +91,26 @@ namespace Renci.SshNet.Tests.Classes
|
|
|
_sessionMock.InSequence(seq).Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
|
|
|
_connectionInfoMock.InSequence(seq).Setup(p => p.Timeout).Returns(_connectionTimeout);
|
|
|
_channelMock.InSequence(seq).Setup(p => p.Close());
|
|
|
- _channelMock.InSequence(seq).Setup(p => p.Dispose());
|
|
|
+ _channelMock.InSequence(seq).Setup(p => p.Dispose()).Callback(() => _channelDisposed.Set());
|
|
|
}
|
|
|
|
|
|
private void Arrange()
|
|
|
{
|
|
|
- SetupData();
|
|
|
CreateMocks();
|
|
|
+ SetupData();
|
|
|
SetupMocks();
|
|
|
|
|
|
- _forwardedPort = new ForwardedPortDynamic(8122);
|
|
|
- _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
|
|
|
- _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
|
|
|
- _forwardedPort.Session = _sessionMock.Object;
|
|
|
_forwardedPort.Start();
|
|
|
|
|
|
- var endPoint = new IPEndPoint(IPAddress.Loopback, 8122);
|
|
|
-
|
|
|
- _client = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
|
|
- _client.Connect(endPoint);
|
|
|
+ _client.Connect(_forwardedPortEndPoint);
|
|
|
}
|
|
|
|
|
|
private void Act()
|
|
|
{
|
|
|
_client.Shutdown(SocketShutdown.Send);
|
|
|
+
|
|
|
+ // wait for channel to be disposed
|
|
|
+ _channelDisposed.WaitOne(TimeSpan.FromMilliseconds(200));
|
|
|
}
|
|
|
|
|
|
[TestMethod]
|