ForwardedPortDynamicTest_Dispose_PortStarted_ChannelNotBound.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_Dispose_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).Returns(TimeSpan.FromSeconds(15));
  56. _sessionMock.Setup(p => p.IsConnected).Returns(true);
  57. _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  58. _sessionMock.Setup(p => p.CreateChannelDirectTcpip()).Returns(_channelMock.Object);
  59. _channelMock.Setup(p => p.Dispose());
  60. _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port);
  61. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  62. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  63. _forwardedPort.Session = _sessionMock.Object;
  64. _forwardedPort.Start();
  65. _client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
  66. {
  67. ReceiveTimeout = 500,
  68. SendTimeout = 500,
  69. SendBufferSize = 0
  70. };
  71. _client.Connect(_endpoint);
  72. // allow for client socket to establish connection
  73. Thread.Sleep(50);
  74. }
  75. protected void Act()
  76. {
  77. _forwardedPort.Dispose();
  78. }
  79. [TestMethod]
  80. public void IsStartedShouldReturnFalse()
  81. {
  82. Assert.IsFalse(_forwardedPort.IsStarted);
  83. }
  84. [TestMethod]
  85. public void ForwardedPortShouldRefuseNewConnections()
  86. {
  87. using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  88. {
  89. try
  90. {
  91. client.Connect(_endpoint);
  92. Assert.Fail();
  93. }
  94. catch (SocketException ex)
  95. {
  96. Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
  97. }
  98. }
  99. }
  100. // TODO We should investigate why this method doesn't work on Linux
  101. [TestMethodForPlatform(nameof(OSPlatform.Windows))]
  102. public void ExistingConnectionShouldBeClosed()
  103. {
  104. try
  105. {
  106. _client.Send(new byte[] { 0x0a }, 0, 1, SocketFlags.None);
  107. Assert.Fail();
  108. }
  109. catch (SocketException ex)
  110. {
  111. Assert.AreEqual(SocketError.ConnectionReset, ex.SocketErrorCode);
  112. }
  113. }
  114. [TestMethod]
  115. public void ClosingShouldHaveFiredOnce()
  116. {
  117. Assert.AreEqual(1, _closingRegister.Count);
  118. }
  119. [TestMethod]
  120. public void ExceptionShouldNotHaveFired()
  121. {
  122. Assert.AreEqual(0, _exceptionRegister.Count);
  123. }
  124. [TestMethod]
  125. public void DisposeOnChannelShouldBeInvokedOnce()
  126. {
  127. _channelMock.Verify(p => p.Dispose(), Times.Once);
  128. }
  129. }
  130. }