ForwardedPortRemoteTest_Start_SessionNotConnected.cs 4.8 KB

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