瀏覽代碼

Harden tests.

drieseng 9 年之前
父節點
當前提交
8749441ec8

+ 18 - 4
src/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_Dispose_PortStarted_ChannelBound.cs

@@ -90,8 +90,8 @@ namespace Renci.SshNet.Tests.Classes
 
             _client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
             {
-                ReceiveTimeout = 500,
-                SendTimeout = 500,
+                ReceiveTimeout = 100,
+                SendTimeout = 100,
                 SendBufferSize = 0
             };
         }
@@ -162,9 +162,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]

+ 50 - 21
src/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_SessionErrorOccurred_ChannelBound.cs

@@ -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)));
         }
     }
 }

+ 33 - 19
src/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_Stop_PortStarted_ChannelBound.cs

@@ -63,7 +63,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();
 
@@ -76,17 +83,26 @@ namespace Renci.SshNet.Tests.Classes
             _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 = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port);
+            _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
+            _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
+            _forwardedPort.Session = _sessionMock.Object;
 
-            _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint) _endpoint.Port);
+            _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(() =>
                 {
@@ -96,19 +112,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()
@@ -201,9 +218,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)));
         }
     }
 }

+ 48 - 18
src/Renci.SshNet.Tests/Classes/ForwardedPortLocalTest_Stop_PortStarted_ChannelBound.cs

@@ -58,22 +58,40 @@ 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();
+
             _closingRegister = new List<EventArgs>();
             _exceptionRegister = new List<ExceptionEventArgs>();
             _localEndpoint = new IPEndPoint(IPAddress.Loopback, 8122);
             _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"), random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
             _bindSleepTime = TimeSpan.FromMilliseconds(random.Next(100, 500));
-            _forwardedPort = new ForwardedPortLocal(_localEndpoint.Address.ToString(), (uint)_localEndpoint.Port, _remoteEndpoint.Address.ToString(), (uint)_remoteEndpoint.Port);
             _channelBound = 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 = new ForwardedPortLocal(_localEndpoint.Address.ToString(), (uint) _localEndpoint.Port, _remoteEndpoint.Address.ToString(), (uint) _remoteEndpoint.Port);
+            _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
+            _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
+            _forwardedPort.Session = _sessionMock.Object;
+
+            _client = new Socket(_localEndpoint.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);
@@ -87,20 +105,18 @@ 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(_localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
-                {
-                    ReceiveTimeout = 500,
-                    SendTimeout = 500,
-                    SendBufferSize = 0
-                };
+            // start port
+            _forwardedPort.Start();
+            // connect to port
             _client.Connect(_localEndpoint);
-
             // wait for SOCKS client to bind to channel
             Assert.IsTrue(_channelBound.WaitOne(TimeSpan.FromMilliseconds(200)));
         }
@@ -142,9 +158,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]