ForwardedPortLocalTest_Start_PortDisposed.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 ForwardedPortLocalTest_Start_PortDisposed
  10. {
  11. private ForwardedPortLocal _forwardedPort;
  12. private IPEndPoint _localEndpoint;
  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. _localEndpoint = new IPEndPoint(IPAddress.Loopback, 8122);
  38. _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"),
  39. random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
  40. _forwardedPort = new ForwardedPortLocal(_localEndpoint.Address.ToString(), (uint)_localEndpoint.Port,
  41. _remoteEndpoint.Address.ToString(), (uint)_remoteEndpoint.Port);
  42. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  43. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  44. _forwardedPort.Dispose();
  45. }
  46. protected void Act()
  47. {
  48. try
  49. {
  50. _forwardedPort.Start();
  51. Assert.Fail();
  52. }
  53. catch (ObjectDisposedException ex)
  54. {
  55. _actualException = ex;
  56. }
  57. }
  58. [TestMethod]
  59. public void StartShouldThrowObjectDisposedException()
  60. {
  61. Assert.IsNotNull(_actualException);
  62. Assert.AreEqual(_forwardedPort.GetType().FullName, _actualException.ObjectName);
  63. }
  64. [TestMethod]
  65. public void ClosingShouldNotHaveFired()
  66. {
  67. Assert.AreEqual(0, _closingRegister.Count);
  68. }
  69. [TestMethod]
  70. public void ExceptionShouldNotHaveFired()
  71. {
  72. Assert.AreEqual(0, _exceptionRegister.Count);
  73. }
  74. }
  75. }