ForwardedPortDynamicTest_Stop_PortStarted_ChannelNotBound.cs 4.7 KB

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