2
0

ForwardedPortDynamicTest_Started_SocketVersionNotSupported.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using Microsoft.Extensions.Logging.Abstractions;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using Moq;
  9. using Renci.SshNet.Channels;
  10. using Renci.SshNet.Common;
  11. using Renci.SshNet.Tests.Common;
  12. namespace Renci.SshNet.Tests.Classes
  13. {
  14. [TestClass]
  15. public class ForwardedPortDynamicTest_Started_SocketVersionNotSupported
  16. {
  17. private Mock<ISession> _sessionMock;
  18. private Mock<IChannelDirectTcpip> _channelMock;
  19. private Mock<IConnectionInfo> _connectionInfoMock;
  20. private ForwardedPortDynamic _forwardedPort;
  21. private Socket _client;
  22. private IList<EventArgs> _closingRegister;
  23. private IList<ExceptionEventArgs> _exceptionRegister;
  24. private ManualResetEvent _exceptionFired;
  25. private TimeSpan _connectionTimeout;
  26. [TestInitialize]
  27. public void Initialize()
  28. {
  29. Arrange();
  30. Act();
  31. }
  32. [TestCleanup]
  33. public void Cleanup()
  34. {
  35. if (_forwardedPort != null && _forwardedPort.IsStarted)
  36. {
  37. _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  38. _connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(5));
  39. _forwardedPort.Stop();
  40. }
  41. if (_client != null)
  42. {
  43. _client.Close();
  44. _client = null;
  45. }
  46. if (_exceptionFired != null)
  47. {
  48. _exceptionFired.Dispose();
  49. _exceptionFired = null;
  50. }
  51. }
  52. private void SetupData()
  53. {
  54. _closingRegister = new List<EventArgs>();
  55. _exceptionRegister = new List<ExceptionEventArgs>();
  56. _exceptionFired = new ManualResetEvent(false);
  57. _connectionTimeout = TimeSpan.FromSeconds(5);
  58. }
  59. private void CreateMocks()
  60. {
  61. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  62. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  63. _channelMock = new Mock<IChannelDirectTcpip>(MockBehavior.Strict);
  64. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  65. }
  66. private void SetupMocks()
  67. {
  68. var seq = new MockSequence();
  69. _sessionMock.InSequence(seq).Setup(p => p.IsConnected).Returns(true);
  70. _sessionMock.InSequence(seq).Setup(p => p.CreateChannelDirectTcpip()).Returns(_channelMock.Object);
  71. _sessionMock.InSequence(seq).Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  72. _connectionInfoMock.InSequence(seq).Setup(p => p.Timeout).Returns(_connectionTimeout);
  73. _channelMock.InSequence(seq).Setup(p => p.Dispose());
  74. }
  75. private void Arrange()
  76. {
  77. SetupData();
  78. CreateMocks();
  79. SetupMocks();
  80. _forwardedPort = new ForwardedPortDynamic(8122);
  81. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  82. _forwardedPort.Exception += (sender, args) =>
  83. {
  84. _exceptionRegister.Add(args);
  85. _exceptionFired.Set();
  86. };
  87. _forwardedPort.Session = _sessionMock.Object;
  88. _forwardedPort.Start();
  89. var endPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  90. _client = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  91. _client.Connect(endPoint);
  92. }
  93. private void Act()
  94. {
  95. var buffer = new byte[] { 0x07 };
  96. _client.Send(buffer, 0, buffer.Length, SocketFlags.None);
  97. // wait for Exception event to be fired as a way to ensure that SOCKS
  98. // request has been handled completely
  99. _exceptionFired.WaitOne(TimeSpan.FromMilliseconds(200));
  100. }
  101. [TestMethod]
  102. public void SocketShouldBeConnected()
  103. {
  104. Assert.IsTrue(_client.Connected);
  105. }
  106. [TestMethod]
  107. public void ForwardedPortShouldShutdownSendOnSocket()
  108. {
  109. var buffer = new byte[1];
  110. var bytesReceived = _client.Receive(buffer, 0, buffer.Length, SocketFlags.None);
  111. Assert.AreEqual(0, bytesReceived);
  112. }
  113. [TestMethod]
  114. public void ClosingShouldNotHaveFired()
  115. {
  116. Assert.AreEqual(0, _closingRegister.Count);
  117. }
  118. [TestMethod]
  119. public void ExceptionShouldHaveFiredOnce()
  120. {
  121. Assert.AreEqual(1, _exceptionRegister.Count, _exceptionRegister.AsString());
  122. var exception = _exceptionRegister[0].Exception;
  123. Assert.IsNotNull(exception);
  124. var notSupportedException = exception as NotSupportedException;
  125. Assert.IsNotNull(notSupportedException, exception.ToString());
  126. Assert.AreEqual("SOCKS version 7 is not supported.", notSupportedException.Message);
  127. }
  128. [TestMethod]
  129. public void CreateChannelDirectTcpipOnSessionShouldBeInvokedOnce()
  130. {
  131. _sessionMock.Verify(p => p.CreateChannelDirectTcpip(), Times.Once);
  132. }
  133. [TestMethod]
  134. public void DisposeOnChannelShouldBeInvokedOnce()
  135. {
  136. _channelMock.Verify(p => p.Dispose(), Times.Once);
  137. }
  138. }
  139. }