ForwardedPortDynamicTest_Started_SocketVersionNotSupported.cs 4.9 KB

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