ForwardedPortDynamicTest_Stop_PortStarted_ChannelNotBound.cs 4.8 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.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.Close());
  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.Stop();
  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. [TestMethod]
  99. public void ExistingConnectionShouldBeClosed()
  100. {
  101. try
  102. {
  103. _client.Send(new byte[] { 0x0a }, 0, 1, SocketFlags.None);
  104. Assert.Fail();
  105. }
  106. catch (SocketException ex)
  107. {
  108. Assert.AreEqual(SocketError.ConnectionReset, ex.SocketErrorCode);
  109. }
  110. }
  111. [TestMethod]
  112. public void ClosingShouldHaveFiredOnce()
  113. {
  114. Assert.AreEqual(1, _closingRegister.Count);
  115. }
  116. [TestMethod]
  117. public void ExceptionShouldNotHaveFired()
  118. {
  119. Assert.AreEqual(0, _exceptionRegister.Count);
  120. }
  121. [TestMethod]
  122. public void CloseOnChannelShouldBeInvokedOnce()
  123. {
  124. _channelMock.Verify(p => p.Close(), Times.Once);
  125. }
  126. [TestMethod]
  127. public void DisposeOnChannelShouldBeInvokedOnce()
  128. {
  129. _channelMock.Verify(p => p.Dispose(), Times.Once);
  130. }
  131. }
  132. }