ForwardedPortLocalTest_Start_SessionNotConnected.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using Microsoft.Extensions.Logging.Abstractions;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Moq;
  8. using Renci.SshNet.Common;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. [TestClass]
  12. public class ForwardedPortLocalTest_Start_SessionNotConnected
  13. {
  14. private Mock<ISession> _sessionMock;
  15. private Mock<IConnectionInfo> _connectionInfoMock;
  16. private ForwardedPortLocal _forwardedPort;
  17. private IPEndPoint _localEndpoint;
  18. private IPEndPoint _remoteEndpoint;
  19. private IList<EventArgs> _closingRegister;
  20. private IList<ExceptionEventArgs> _exceptionRegister;
  21. private SshConnectionException _actualException;
  22. [TestInitialize]
  23. public void Setup()
  24. {
  25. Arrange();
  26. Act();
  27. }
  28. [TestCleanup]
  29. public void Cleanup()
  30. {
  31. if (_forwardedPort != null)
  32. {
  33. _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  34. _connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(1));
  35. _forwardedPort.Dispose();
  36. _forwardedPort = null;
  37. }
  38. }
  39. protected void Arrange()
  40. {
  41. var random = new Random();
  42. _closingRegister = new List<EventArgs>();
  43. _exceptionRegister = new List<ExceptionEventArgs>();
  44. _localEndpoint = new IPEndPoint(IPAddress.Loopback, 8122);
  45. _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"),
  46. random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
  47. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  48. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  49. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  50. _sessionMock.Setup(p => p.IsConnected).Returns(false);
  51. _forwardedPort = new ForwardedPortLocal(_localEndpoint.Address.ToString(), (uint)_localEndpoint.Port,
  52. _remoteEndpoint.Address.ToString(), (uint)_remoteEndpoint.Port);
  53. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  54. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  55. _forwardedPort.Session = _sessionMock.Object;
  56. }
  57. protected void Act()
  58. {
  59. try
  60. {
  61. _forwardedPort.Start();
  62. Assert.Fail();
  63. }
  64. catch (SshConnectionException ex)
  65. {
  66. _actualException = ex;
  67. }
  68. }
  69. [TestMethod]
  70. public void StartShouldThrowSshConnectionException()
  71. {
  72. Assert.IsNotNull(_actualException);
  73. Assert.AreEqual("Client not connected.", _actualException.Message);
  74. }
  75. [TestMethod]
  76. public void IsStartedShouldReturnFalse()
  77. {
  78. Assert.IsFalse(_forwardedPort.IsStarted);
  79. }
  80. [TestMethod]
  81. public void ForwardedPortShouldRejectNewConnections()
  82. {
  83. using (var client = new Socket(_localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  84. {
  85. try
  86. {
  87. client.Connect(_localEndpoint);
  88. }
  89. catch (SocketException ex)
  90. {
  91. Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
  92. }
  93. }
  94. }
  95. [TestMethod]
  96. public void ClosingShouldNotHaveFired()
  97. {
  98. Assert.AreEqual(0, _closingRegister.Count);
  99. }
  100. [TestMethod]
  101. public void ExceptionShouldNotHaveFired()
  102. {
  103. Assert.AreEqual(0, _exceptionRegister.Count);
  104. }
  105. }
  106. }