ForwardedPortLocalTest_Start_SessionNull.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 ForwardedPortLocalTest_Start_SessionNull
  13. {
  14. private Mock<ISession> _sessionMock;
  15. private Mock<IConnectionInfo> _connectionInfoMock;
  16. private ForwardedPortLocal _forwardedPort;
  17. private IPEndPoint _localEndpoint;
  18. private IPEndPoint _remoteEndpoint;
  19. private IList<EventArgs> _closingRegister;
  20. private IList<ExceptionEventArgs> _exceptionRegister;
  21. private InvalidOperationException _actualException;
  22. [TestInitialize]
  23. public void Setup()
  24. {
  25. Arrange();
  26. Act();
  27. }
  28. [TestCleanup]
  29. public void Cleanup()
  30. {
  31. if (_forwardedPort != null)
  32. {
  33. _connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(1));
  34. _forwardedPort.Dispose();
  35. _forwardedPort = null;
  36. }
  37. }
  38. protected void Arrange()
  39. {
  40. var random = new Random();
  41. _closingRegister = new List<EventArgs>();
  42. _exceptionRegister = new List<ExceptionEventArgs>();
  43. _localEndpoint = new IPEndPoint(IPAddress.Loopback, 8122);
  44. _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"),
  45. random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
  46. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  47. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  48. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  49. _sessionMock.Setup(p => p.IsConnected).Returns(false);
  50. _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  51. _forwardedPort = new ForwardedPortLocal(_localEndpoint.Address.ToString(), (uint)_localEndpoint.Port,
  52. _remoteEndpoint.Address.ToString(), (uint)_remoteEndpoint.Port);
  53. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  54. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  55. }
  56. protected void Act()
  57. {
  58. try
  59. {
  60. _forwardedPort.Start();
  61. Assert.Fail();
  62. }
  63. catch (InvalidOperationException ex)
  64. {
  65. _actualException = ex;
  66. }
  67. }
  68. [TestMethod]
  69. public void StartShouldThrowInvalidOperationException()
  70. {
  71. Assert.IsNotNull(_actualException);
  72. Assert.AreEqual("Forwarded port is not added to a client.", _actualException.Message);
  73. }
  74. [TestMethod]
  75. public void IsStartedShouldReturnFalse()
  76. {
  77. Assert.IsFalse(_forwardedPort.IsStarted);
  78. }
  79. [TestMethod]
  80. public void ForwardedPortShouldRejectNewConnections()
  81. {
  82. using (var client = new Socket(_localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  83. {
  84. try
  85. {
  86. client.Connect(_localEndpoint);
  87. }
  88. catch (SocketException ex)
  89. {
  90. Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
  91. }
  92. }
  93. }
  94. [TestMethod]
  95. public void ClosingShouldNotHaveFired()
  96. {
  97. Assert.AreEqual(0, _closingRegister.Count);
  98. }
  99. [TestMethod]
  100. public void ExceptionShouldNotHaveFired()
  101. {
  102. Assert.AreEqual(0, _exceptionRegister.Count);
  103. }
  104. }
  105. }