ForwardedPortDynamicTest_Stop_PortStarted_ChannelNotBound.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. using Microsoft.Extensions.Logging.Abstractions;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. using Moq;
  10. using Renci.SshNet.Channels;
  11. using Renci.SshNet.Common;
  12. using Renci.SshNet.Tests.Common;
  13. namespace Renci.SshNet.Tests.Classes
  14. {
  15. [TestClass]
  16. public class ForwardedPortDynamicTest_Stop_PortStarted_ChannelNotBound
  17. {
  18. private Mock<ISession> _sessionMock;
  19. private Mock<IConnectionInfo> _connectionInfoMock;
  20. private Mock<IChannelDirectTcpip> _channelMock;
  21. private ForwardedPortDynamic _forwardedPort;
  22. private IList<EventArgs> _closingRegister;
  23. private IList<ExceptionEventArgs> _exceptionRegister;
  24. private IPEndPoint _endpoint;
  25. private Socket _client;
  26. [TestInitialize]
  27. public void Setup()
  28. {
  29. Arrange();
  30. Act();
  31. }
  32. [TestCleanup]
  33. public void Cleanup()
  34. {
  35. if (_client != null)
  36. {
  37. _client.Dispose();
  38. _client = null;
  39. }
  40. if (_forwardedPort != null)
  41. {
  42. _forwardedPort.Dispose();
  43. _forwardedPort = null;
  44. }
  45. }
  46. protected void Arrange()
  47. {
  48. _closingRegister = new List<EventArgs>();
  49. _exceptionRegister = new List<ExceptionEventArgs>();
  50. _endpoint = new IPEndPoint(IPAddress.Loopback, 8122);
  51. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  52. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  53. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  54. _channelMock = new Mock<IChannelDirectTcpip>(MockBehavior.Strict);
  55. _ = _connectionInfoMock.Setup(p => p.Timeout)
  56. .Returns(TimeSpan.FromSeconds(15));
  57. _ = _sessionMock.Setup(p => p.IsConnected)
  58. .Returns(true);
  59. _ = _sessionMock.Setup(p => p.ConnectionInfo)
  60. .Returns(_connectionInfoMock.Object);
  61. _ = _sessionMock.Setup(p => p.CreateChannelDirectTcpip())
  62. .Returns(_channelMock.Object);
  63. _ = _channelMock.Setup(p => p.Dispose());
  64. _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port);
  65. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  66. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  67. _forwardedPort.Session = _sessionMock.Object;
  68. _forwardedPort.Start();
  69. _client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
  70. {
  71. ReceiveTimeout = 500,
  72. SendTimeout = 500,
  73. SendBufferSize = 0
  74. };
  75. _client.Connect(_endpoint);
  76. // allow for client socket to establish connection
  77. Thread.Sleep(50);
  78. }
  79. protected void Act()
  80. {
  81. _forwardedPort.Stop();
  82. }
  83. [TestMethod]
  84. public void IsStartedShouldReturnFalse()
  85. {
  86. Assert.IsFalse(_forwardedPort.IsStarted);
  87. }
  88. [TestMethod]
  89. public void ForwardedPortShouldRefuseNewConnections()
  90. {
  91. using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  92. {
  93. try
  94. {
  95. client.Connect(_endpoint);
  96. Assert.Fail();
  97. }
  98. catch (SocketException ex)
  99. {
  100. Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
  101. }
  102. }
  103. }
  104. // TODO We should investigate why this method doesn't work on Linux
  105. [TestMethodForPlatform(nameof(OSPlatform.Windows))]
  106. public void ExistingConnectionShouldBeClosed()
  107. {
  108. try
  109. {
  110. _ = _client.Send(new byte[] { 0x0a }, 0, 1, SocketFlags.None);
  111. Assert.Fail();
  112. }
  113. catch (SocketException ex)
  114. {
  115. Assert.AreEqual(SocketError.ConnectionReset, ex.SocketErrorCode);
  116. }
  117. }
  118. [TestMethod]
  119. public void ClosingShouldHaveFiredOnce()
  120. {
  121. Assert.AreEqual(1, _closingRegister.Count);
  122. }
  123. [TestMethod]
  124. public void ExceptionShouldNotHaveFired()
  125. {
  126. Assert.AreEqual(0, _exceptionRegister.Count);
  127. }
  128. [TestMethod]
  129. public void DisposeOnChannelShouldBeInvokedOnce()
  130. {
  131. _channelMock.Verify(p => p.Dispose(), Times.Once);
  132. }
  133. }
  134. }