using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using Microsoft.VisualStudio.TestTools.UnitTesting; using Renci.SshNet.Common; namespace Renci.SshNet.Tests.Classes { [TestClass] public class ForwardedPortDynamicTest_Start_SessionNull { private ForwardedPortDynamic _forwardedPort; private IList _closingRegister; private IList _exceptionRegister; private InvalidOperationException _actualException; private IPEndPoint _endpoint; [TestInitialize] public void Setup() { Arrange(); Act(); } [TestCleanup] public void Cleanup() { if (_forwardedPort != null) { _forwardedPort.Dispose(); _forwardedPort = null; } } protected void Arrange() { _closingRegister = new List(); _exceptionRegister = new List(); _endpoint = new IPEndPoint(IPAddress.Loopback, 8122); _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port); _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args); _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args); } protected void Act() { try { _forwardedPort.Start(); Assert.Fail(); } catch (InvalidOperationException ex) { _actualException = ex; } } [TestMethod] public void StartShouldThrowInvalidOperationException() { Assert.IsNotNull(_actualException); Assert.AreEqual("Forwarded port is not added to a client.", _actualException.Message); } [TestMethod] public void IsStartedShouldReturnFalse() { Assert.IsFalse(_forwardedPort.IsStarted); } [TestMethod] public void ForwardedPortShouldRejectNewConnections() { using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { try { client.Connect(_endpoint); } catch (SocketException ex) { Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode); } } } [TestMethod] public void ClosingShouldNotHaveFired() { Assert.AreEqual(0, _closingRegister.Count); } [TestMethod] public void ExceptionShouldNotHaveFired() { Assert.AreEqual(0, _exceptionRegister.Count); } } }