ForwardedPortDynamicTest_Start_SessionNotConnected.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using Microsoft.Extensions.Logging.Abstractions;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Moq;
  8. using Renci.SshNet.Common;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. [TestClass]
  12. public class ForwardedPortDynamicTest_Start_SessionNotConnected
  13. {
  14. private Mock<ISession> _sessionMock;
  15. private Mock<IConnectionInfo> _connectionInfoMock;
  16. private ForwardedPortDynamic _forwardedPort;
  17. private IList<EventArgs> _closingRegister;
  18. private IList<ExceptionEventArgs> _exceptionRegister;
  19. private SshConnectionException _actualException;
  20. private IPEndPoint _endpoint;
  21. [TestInitialize]
  22. public void Setup()
  23. {
  24. Arrange();
  25. Act();
  26. }
  27. [TestCleanup]
  28. public void Cleanup()
  29. {
  30. if (_forwardedPort != null)
  31. {
  32. _connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(1));
  33. _forwardedPort.Dispose();
  34. _forwardedPort = null;
  35. }
  36. }
  37. protected void Arrange()
  38. {
  39. _closingRegister = new List<EventArgs>();
  40. _exceptionRegister = new List<ExceptionEventArgs>();
  41. _endpoint = new IPEndPoint(IPAddress.Loopback, 8122);
  42. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  43. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  44. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  45. _sessionMock.Setup(p => p.IsConnected).Returns(false);
  46. _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  47. _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port);
  48. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  49. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  50. _forwardedPort.Session = _sessionMock.Object;
  51. }
  52. protected void Act()
  53. {
  54. try
  55. {
  56. _forwardedPort.Start();
  57. Assert.Fail();
  58. }
  59. catch (SshConnectionException ex)
  60. {
  61. _actualException = ex;
  62. }
  63. }
  64. [TestMethod]
  65. public void StartShouldThrowSshConnectionException()
  66. {
  67. Assert.IsNotNull(_actualException);
  68. Assert.AreEqual("Client not connected.", _actualException.Message);
  69. }
  70. [TestMethod]
  71. public void IsStartedShouldReturnFalse()
  72. {
  73. Assert.IsFalse(_forwardedPort.IsStarted);
  74. }
  75. [TestMethod]
  76. public void ForwardedPortShouldRejectNewConnections()
  77. {
  78. using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  79. {
  80. try
  81. {
  82. client.Connect(_endpoint);
  83. }
  84. catch (SocketException ex)
  85. {
  86. Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
  87. }
  88. }
  89. }
  90. [TestMethod]
  91. public void ClosingShouldNotHaveFired()
  92. {
  93. Assert.AreEqual(0, _closingRegister.Count);
  94. }
  95. [TestMethod]
  96. public void ExceptionShouldNotHaveFired()
  97. {
  98. Assert.AreEqual(0, _exceptionRegister.Count);
  99. }
  100. }
  101. }