ForwardedPortLocalTest_Dispose_PortDisposed.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Moq;
  6. using Renci.SshNet.Common;
  7. namespace Renci.SshNet.Tests.Classes
  8. {
  9. [TestClass]
  10. public class ForwardedPortLocalTest_Dispose_PortDisposed
  11. {
  12. private Mock<ISession> _sessionMock;
  13. private Mock<IConnectionInfo> _connectionInfoMock;
  14. private ForwardedPortLocal _forwardedPort;
  15. private IList<EventArgs> _closingRegister;
  16. private IList<ExceptionEventArgs> _exceptionRegister;
  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. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  37. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  38. var sequence = new MockSequence();
  39. _sessionMock.InSequence(sequence).Setup(p => p.IsConnected).Returns(true);
  40. _sessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  41. _connectionInfoMock.InSequence(sequence).Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(30));
  42. _forwardedPort = new ForwardedPortLocal(IPAddress.Loopback.ToString(), "host", 22);
  43. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  44. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  45. _forwardedPort.Session = _sessionMock.Object;
  46. _forwardedPort.Start();
  47. _forwardedPort.Dispose();
  48. }
  49. protected void Act()
  50. {
  51. _forwardedPort.Dispose();
  52. }
  53. [TestMethod]
  54. public void ClosingShouldHaveFiredOnce()
  55. {
  56. Assert.AreEqual(1, _closingRegister.Count);
  57. }
  58. [TestMethod]
  59. public void ExceptionShouldNotHaveFired()
  60. {
  61. Assert.AreEqual(0, _exceptionRegister.Count);
  62. }
  63. [TestMethod]
  64. public void SessionShouldBeNull()
  65. {
  66. Assert.IsNull(_forwardedPort.Session);
  67. }
  68. }
  69. }