ForwardedPortDynamicTest_Start_PortStarted.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.Channels;
  9. using Renci.SshNet.Common;
  10. namespace Renci.SshNet.Tests.Classes
  11. {
  12. [TestClass]
  13. public class ForwardedPortDynamicTest_Start_PortStarted
  14. {
  15. private Mock<ISession> _sessionMock;
  16. private Mock<IConnectionInfo> _connectionInfoMock;
  17. private Mock<IChannelDirectTcpip> _channelMock;
  18. private ForwardedPortDynamic _forwardedPort;
  19. private IList<EventArgs> _closingRegister;
  20. private IList<ExceptionEventArgs> _exceptionRegister;
  21. private IPEndPoint _endpoint;
  22. private InvalidOperationException _actualException;
  23. [TestInitialize]
  24. public void Setup()
  25. {
  26. Arrange();
  27. Act();
  28. }
  29. [TestCleanup]
  30. public void Cleanup()
  31. {
  32. if (_forwardedPort != null)
  33. {
  34. _forwardedPort.Dispose();
  35. _forwardedPort = null;
  36. }
  37. }
  38. protected void Arrange()
  39. {
  40. _closingRegister = new List<EventArgs>();
  41. _exceptionRegister = new List<ExceptionEventArgs>();
  42. _endpoint = new IPEndPoint(IPAddress.Loopback, 8122);
  43. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  44. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  45. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  46. _channelMock = new Mock<IChannelDirectTcpip>(MockBehavior.Strict);
  47. _connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(15));
  48. _sessionMock.Setup(p => p.IsConnected).Returns(true);
  49. _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  50. _sessionMock.Setup(p => p.CreateChannelDirectTcpip()).Returns(_channelMock.Object);
  51. _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port);
  52. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  53. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  54. _forwardedPort.Session = _sessionMock.Object;
  55. _forwardedPort.Start();
  56. }
  57. protected void Act()
  58. {
  59. try
  60. {
  61. _forwardedPort.Start();
  62. Assert.Fail();
  63. }
  64. catch (InvalidOperationException ex)
  65. {
  66. _actualException = ex;
  67. }
  68. }
  69. [TestMethod]
  70. public void StartShouldThrowInvalidOperationException()
  71. {
  72. Assert.IsNotNull(_actualException);
  73. Assert.AreEqual("Forwarded port is already started.", _actualException.Message);
  74. }
  75. [TestMethod]
  76. public void IsStartedShouldReturnTrue()
  77. {
  78. Assert.IsTrue(_forwardedPort.IsStarted);
  79. }
  80. [TestMethod]
  81. public void ForwardedPortShouldAcceptNewConnections()
  82. {
  83. using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  84. {
  85. client.Connect(_endpoint);
  86. }
  87. }
  88. [TestMethod]
  89. public void ClosingShouldNotHaveFired()
  90. {
  91. Assert.AreEqual(0, _closingRegister.Count);
  92. }
  93. [TestMethod]
  94. public void ExceptionShouldNotHaveFired()
  95. {
  96. Assert.AreEqual(0, _exceptionRegister.Count);
  97. }
  98. }
  99. }