ForwardedPortRemoteTest_Start_SessionNull.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_SessionNull
  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 InvalidOperationException _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, 1000));
  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. }
  43. protected void Act()
  44. {
  45. try
  46. {
  47. _forwardedPort.Start();
  48. Assert.Fail();
  49. }
  50. catch (InvalidOperationException ex)
  51. {
  52. _actualException = ex;
  53. }
  54. }
  55. [TestMethod]
  56. public void StartShouldThrowSshConnectionException()
  57. {
  58. Assert.IsNotNull(_actualException);
  59. Assert.AreEqual("Forwarded port is not added to a client.", _actualException.Message);
  60. }
  61. [TestMethod]
  62. public void IsStartedShouldReturnFalse()
  63. {
  64. Assert.IsFalse(_forwardedPort.IsStarted);
  65. }
  66. [TestMethod]
  67. public void ClosingShouldNotHaveFired()
  68. {
  69. Assert.AreEqual(0, _closingRegister.Count);
  70. }
  71. [TestMethod]
  72. public void ExceptionShouldNotHaveFired()
  73. {
  74. Assert.AreEqual(0, _exceptionRegister.Count);
  75. }
  76. }
  77. }