ForwardedPortRemoteTest_Start_PortDisposed.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Renci.SshNet.Common;
  6. namespace Renci.SshNet.Tests.Classes
  7. {
  8. [TestClass]
  9. public class ForwardedPortRemoteTest_Start_PortDisposed
  10. {
  11. private ForwardedPortRemote _forwardedPort;
  12. private IPEndPoint _bindEndpoint;
  13. private IPEndPoint _remoteEndpoint;
  14. private IList<EventArgs> _closingRegister;
  15. private IList<ExceptionEventArgs> _exceptionRegister;
  16. private ObjectDisposedException _actualException;
  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. var random = new Random();
  35. _closingRegister = new List<EventArgs>();
  36. _exceptionRegister = new List<ExceptionEventArgs>();
  37. _bindEndpoint = new IPEndPoint(IPAddress.Any, random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
  38. _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"), random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
  39. _forwardedPort = new ForwardedPortRemote(_bindEndpoint.Address, (uint)_bindEndpoint.Port, _remoteEndpoint.Address, (uint)_remoteEndpoint.Port);
  40. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  41. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  42. _forwardedPort.Dispose();
  43. }
  44. protected void Act()
  45. {
  46. try
  47. {
  48. _forwardedPort.Start();
  49. Assert.Fail();
  50. }
  51. catch (ObjectDisposedException ex)
  52. {
  53. _actualException = ex;
  54. }
  55. }
  56. [TestMethod]
  57. public void StartShouldThrowObjectDisposedException()
  58. {
  59. Assert.IsNotNull(_actualException);
  60. Assert.AreEqual(_forwardedPort.GetType().FullName, _actualException.ObjectName);
  61. }
  62. [TestMethod]
  63. public void ClosingShouldNotHaveFired()
  64. {
  65. Assert.AreEqual(0, _closingRegister.Count);
  66. }
  67. [TestMethod]
  68. public void ExceptionShouldNotHaveFired()
  69. {
  70. Assert.AreEqual(0, _exceptionRegister.Count);
  71. }
  72. }
  73. }