ForwardedPortDynamicTest_Started_SocketVersionNotSupported.cs 5.3 KB

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