ForwardedPortLocalTest_Start_SessionNull.cs 3.7 KB

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