ForwardedPortDynamicTest_Start_SessionNull.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 Renci.SshNet.Common;
  7. namespace Renci.SshNet.Tests.Classes
  8. {
  9. [TestClass]
  10. public class ForwardedPortDynamicTest_Start_SessionNull
  11. {
  12. private ForwardedPortDynamic _forwardedPort;
  13. private IList<EventArgs> _closingRegister;
  14. private IList<ExceptionEventArgs> _exceptionRegister;
  15. private InvalidOperationException _actualException;
  16. private IPEndPoint _endpoint;
  17. [TestInitialize]
  18. public void Setup()
  19. {
  20. Arrange();
  21. Act();
  22. }
  23. [TestCleanup]
  24. public void Cleanup()
  25. {
  26. if (_forwardedPort != null)
  27. {
  28. _forwardedPort.Dispose();
  29. _forwardedPort = null;
  30. }
  31. }
  32. protected void Arrange()
  33. {
  34. _closingRegister = new List<EventArgs>();
  35. _exceptionRegister = new List<ExceptionEventArgs>();
  36. _endpoint = new IPEndPoint(IPAddress.Loopback, 8122);
  37. _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port);
  38. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  39. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  40. }
  41. protected void Act()
  42. {
  43. try
  44. {
  45. _forwardedPort.Start();
  46. Assert.Fail();
  47. }
  48. catch (InvalidOperationException ex)
  49. {
  50. _actualException = ex;
  51. }
  52. }
  53. [TestMethod]
  54. public void StartShouldThrowInvalidOperationException()
  55. {
  56. Assert.IsNotNull(_actualException);
  57. Assert.AreEqual("Forwarded port is not added to a client.", _actualException.Message);
  58. }
  59. [TestMethod]
  60. public void IsStartedShouldReturnFalse()
  61. {
  62. Assert.IsFalse(_forwardedPort.IsStarted);
  63. }
  64. [TestMethod]
  65. public void ForwardedPortShouldRejectNewConnections()
  66. {
  67. using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  68. {
  69. try
  70. {
  71. client.Connect(_endpoint);
  72. }
  73. catch (SocketException ex)
  74. {
  75. Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
  76. }
  77. }
  78. }
  79. [TestMethod]
  80. public void ClosingShouldNotHaveFired()
  81. {
  82. Assert.AreEqual(0, _closingRegister.Count);
  83. }
  84. [TestMethod]
  85. public void ExceptionShouldNotHaveFired()
  86. {
  87. Assert.AreEqual(0, _exceptionRegister.Count);
  88. }
  89. }
  90. }