ForwardedPortDynamicTest_Stop_PortDisposed.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_Stop_PortDisposed
  11. {
  12. private ForwardedPortDynamic _forwardedPort;
  13. private IList<EventArgs> _closingRegister;
  14. private IList<ExceptionEventArgs> _exceptionRegister;
  15. private ObjectDisposedException _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. _forwardedPort.Dispose();
  41. }
  42. protected void Act()
  43. {
  44. try
  45. {
  46. _forwardedPort.Stop();
  47. Assert.Fail();
  48. }
  49. catch (ObjectDisposedException ex)
  50. {
  51. _actualException = ex;
  52. }
  53. }
  54. [TestMethod]
  55. public void StopShouldThrowObjectDisposedException()
  56. {
  57. Assert.IsNotNull(_actualException);
  58. Assert.AreEqual(_forwardedPort.GetType().FullName, _actualException.ObjectName);
  59. }
  60. [TestMethod]
  61. public void IsStartedShouldReturnFalse()
  62. {
  63. Assert.IsFalse(_forwardedPort.IsStarted);
  64. }
  65. [TestMethod]
  66. public void ForwardedPortShouldRejectNewConnections()
  67. {
  68. using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  69. {
  70. try
  71. {
  72. client.Connect(_endpoint);
  73. }
  74. catch (SocketException ex)
  75. {
  76. Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
  77. }
  78. }
  79. }
  80. [TestMethod]
  81. public void ClosingShouldNotHaveFired()
  82. {
  83. Assert.AreEqual(0, _closingRegister.Count);
  84. }
  85. [TestMethod]
  86. public void ExceptionShouldNotHaveFired()
  87. {
  88. Assert.AreEqual(0, _exceptionRegister.Count);
  89. }
  90. }
  91. }