ForwardedPortRemoteTest_Start_SessionNotConnected.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Net;
  5. using Microsoft.Extensions.Logging.Abstractions;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Moq;
  8. using Renci.SshNet.Channels;
  9. using Renci.SshNet.Common;
  10. using Renci.SshNet.Messages.Connection;
  11. namespace Renci.SshNet.Tests.Classes
  12. {
  13. [TestClass]
  14. public class ForwardedPortRemoteTest_Start_SessionNotConnected
  15. {
  16. private Mock<ISession> _sessionMock;
  17. private Mock<IConnectionInfo> _connectionInfoMock;
  18. private ForwardedPortRemote _forwardedPort;
  19. private IPEndPoint _bindEndpoint;
  20. private IPEndPoint _remoteEndpoint;
  21. private IList<EventArgs> _closingRegister;
  22. private IList<ExceptionEventArgs> _exceptionRegister;
  23. private SshConnectionException _actualException;
  24. [TestInitialize]
  25. public void Setup()
  26. {
  27. Arrange();
  28. Act();
  29. }
  30. [TestCleanup]
  31. public void Cleanup()
  32. {
  33. if (_forwardedPort != null)
  34. {
  35. _ = _sessionMock.Setup(p => p.ConnectionInfo)
  36. .Returns(_connectionInfoMock.Object);
  37. _ = _connectionInfoMock.Setup(p => p.Timeout)
  38. .Returns(TimeSpan.FromSeconds(1));
  39. _forwardedPort.Dispose();
  40. _forwardedPort = null;
  41. }
  42. }
  43. protected void Arrange()
  44. {
  45. var random = new Random();
  46. _closingRegister = new List<EventArgs>();
  47. _exceptionRegister = new List<ExceptionEventArgs>();
  48. _bindEndpoint = new IPEndPoint(IPAddress.Any, random.Next(IPEndPoint.MinPort, 1000));
  49. _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"), random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
  50. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  51. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  52. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  53. _ = _sessionMock.Setup(p => p.IsConnected)
  54. .Returns(false);
  55. _forwardedPort = new ForwardedPortRemote(_bindEndpoint.Address, (uint)_bindEndpoint.Port, _remoteEndpoint.Address, (uint)_remoteEndpoint.Port);
  56. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  57. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  58. _forwardedPort.Session = _sessionMock.Object;
  59. }
  60. protected void Act()
  61. {
  62. try
  63. {
  64. _forwardedPort.Start();
  65. Assert.Fail();
  66. }
  67. catch (SshConnectionException ex)
  68. {
  69. _actualException = ex;
  70. }
  71. }
  72. [TestMethod]
  73. public void StartShouldThrowSshConnectionException()
  74. {
  75. Assert.IsNotNull(_actualException);
  76. Assert.AreEqual("Client not connected.", _actualException.Message);
  77. }
  78. [TestMethod]
  79. public void IsStartedShouldReturnFalse()
  80. {
  81. Assert.IsFalse(_forwardedPort.IsStarted);
  82. }
  83. [TestMethod]
  84. public void ForwardedPortShouldIgnoreReceivedSignalForNewConnection()
  85. {
  86. var channelNumber = (uint)new Random().Next(1001, int.MaxValue);
  87. var initialWindowSize = (uint)new Random().Next(0, int.MaxValue);
  88. var maximumPacketSize = (uint)new Random().Next(0, int.MaxValue);
  89. var originatorAddress = new Random().Next().ToString(CultureInfo.InvariantCulture);
  90. var originatorPort = (uint)new Random().Next(0, int.MaxValue);
  91. var channelMock = new Mock<IChannelForwardedTcpip>(MockBehavior.Strict);
  92. _ = _sessionMock.Setup(p => p.CreateChannelForwardedTcpip(channelNumber, initialWindowSize, maximumPacketSize)).Returns(channelMock.Object);
  93. _sessionMock.Raise(p => p.ChannelOpenReceived += null,
  94. new MessageEventArgs<ChannelOpenMessage>(new ChannelOpenMessage(channelNumber, initialWindowSize,
  95. maximumPacketSize,
  96. new ForwardedTcpipChannelInfo(_forwardedPort.BoundHost, _forwardedPort.BoundPort, originatorAddress,
  97. originatorPort))));
  98. _sessionMock.Verify(p => p.CreateChannelForwardedTcpip(channelNumber, initialWindowSize, maximumPacketSize), Times.Never);
  99. _sessionMock.Verify(p => p.SendMessage(new ChannelOpenFailureMessage(channelNumber, string.Empty, ChannelOpenFailureMessage.AdministrativelyProhibited)), Times.Never);
  100. }
  101. [TestMethod]
  102. public void ClosingShouldNotHaveFired()
  103. {
  104. Assert.AreEqual(0, _closingRegister.Count);
  105. }
  106. [TestMethod]
  107. public void ExceptionShouldNotHaveFired()
  108. {
  109. Assert.AreEqual(0, _exceptionRegister.Count);
  110. }
  111. }
  112. }