ForwardedPortDynamicTest_Dispose_PortStarted_ChannelNotBound.cs 4.6 KB

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