ForwardedPortDynamicTest_Stop_PortStarted_ChannelNotBound.cs 4.4 KB

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