ForwardedPortDynamicTest_Start_SessionNotConnected.cs 3.4 KB

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