ForwardedPortRemoteTest_Start_SessionNull.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Moq;
  8. using Renci.SshNet.Channels;
  9. using Renci.SshNet.Common;
  10. using Renci.SshNet.Messages.Connection;
  11. namespace Renci.SshNet.Tests.Classes
  12. {
  13. [TestClass]
  14. public class ForwardedPortRemoteTest_Start_SessionNull
  15. {
  16. private ForwardedPortRemote _forwardedPort;
  17. private IPEndPoint _bindEndpoint;
  18. private IPEndPoint _remoteEndpoint;
  19. private IList<EventArgs> _closingRegister;
  20. private IList<ExceptionEventArgs> _exceptionRegister;
  21. private InvalidOperationException _actualException;
  22. [TestInitialize]
  23. public void Setup()
  24. {
  25. Arrange();
  26. Act();
  27. }
  28. [TestCleanup]
  29. public void Cleanup()
  30. {
  31. if (_forwardedPort != null)
  32. {
  33. _forwardedPort.Dispose();
  34. _forwardedPort = null;
  35. }
  36. }
  37. protected void Arrange()
  38. {
  39. var random = new Random();
  40. _closingRegister = new List<EventArgs>();
  41. _exceptionRegister = new List<ExceptionEventArgs>();
  42. _bindEndpoint = new IPEndPoint(IPAddress.Any, random.Next(IPEndPoint.MinPort, 1000));
  43. _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"), random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
  44. _forwardedPort = new ForwardedPortRemote(_bindEndpoint.Address, (uint)_bindEndpoint.Port, _remoteEndpoint.Address, (uint)_remoteEndpoint.Port);
  45. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  46. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  47. }
  48. protected void Act()
  49. {
  50. try
  51. {
  52. _forwardedPort.Start();
  53. Assert.Fail();
  54. }
  55. catch (InvalidOperationException ex)
  56. {
  57. _actualException = ex;
  58. }
  59. }
  60. [TestMethod]
  61. public void StartShouldThrowSshConnectionException()
  62. {
  63. Assert.IsNotNull(_actualException);
  64. Assert.AreEqual("Forwarded port is not added to a client.", _actualException.Message);
  65. }
  66. [TestMethod]
  67. public void IsStartedShouldReturnFalse()
  68. {
  69. Assert.IsFalse(_forwardedPort.IsStarted);
  70. }
  71. [TestMethod]
  72. public void ClosingShouldNotHaveFired()
  73. {
  74. Assert.AreEqual(0, _closingRegister.Count);
  75. }
  76. [TestMethod]
  77. public void ExceptionShouldNotHaveFired()
  78. {
  79. Assert.AreEqual(0, _exceptionRegister.Count);
  80. }
  81. }
  82. }