SubsystemSession_OnChannelException_Disposed.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using Microsoft.Extensions.Logging.Abstractions;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Moq;
  7. using Renci.SshNet.Channels;
  8. using Renci.SshNet.Common;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. [TestClass]
  12. public class SubsystemSession_OnChannelException_Disposed
  13. {
  14. private Mock<ISession> _sessionMock;
  15. private Mock<IChannelSession> _channelMock;
  16. private string _subsystemName;
  17. private SubsystemSessionStub _subsystemSession;
  18. private int _operationTimeout;
  19. private IList<EventArgs> _disconnectedRegister;
  20. private IList<ExceptionEventArgs> _errorOccurredRegister;
  21. private ExceptionEventArgs _channelExceptionEventArgs;
  22. [TestInitialize]
  23. public void Setup()
  24. {
  25. Arrange();
  26. Act();
  27. }
  28. protected void Arrange()
  29. {
  30. var random = new Random();
  31. _subsystemName = random.Next().ToString(CultureInfo.InvariantCulture);
  32. _operationTimeout = 30000;
  33. _disconnectedRegister = new List<EventArgs>();
  34. _errorOccurredRegister = new List<ExceptionEventArgs>();
  35. _channelExceptionEventArgs = new ExceptionEventArgs(new SystemException());
  36. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  37. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  38. _channelMock = new Mock<IChannelSession>(MockBehavior.Strict);
  39. var sequence = new MockSequence();
  40. _ = _sessionMock.InSequence(sequence)
  41. .Setup(p => p.CreateChannelSession())
  42. .Returns(_channelMock.Object);
  43. _ = _channelMock.InSequence(sequence)
  44. .Setup(p => p.Open());
  45. _ = _channelMock.InSequence(sequence)
  46. .Setup(p => p.SendSubsystemRequest(_subsystemName))
  47. .Returns(true);
  48. _ = _channelMock.InSequence(sequence)
  49. .Setup(p => p.Dispose());
  50. _subsystemSession = new SubsystemSessionStub(
  51. _sessionMock.Object,
  52. _subsystemName,
  53. _operationTimeout);
  54. _subsystemSession.Disconnected += (sender, args) => _disconnectedRegister.Add(args);
  55. _subsystemSession.ErrorOccurred += (sender, args) => _errorOccurredRegister.Add(args);
  56. _subsystemSession.Connect();
  57. _subsystemSession.Dispose();
  58. _disconnectedRegister.Clear();
  59. _errorOccurredRegister.Clear();
  60. }
  61. protected void Act()
  62. {
  63. _channelMock.Raise(s => s.Exception += null, _channelExceptionEventArgs);
  64. }
  65. [TestMethod]
  66. public void DisconnectHasNeverFired()
  67. {
  68. Assert.AreEqual(0, _disconnectedRegister.Count);
  69. }
  70. [TestMethod]
  71. public void ErrorOccurredHasNeverFired()
  72. {
  73. Assert.AreEqual(0, _errorOccurredRegister.Count);
  74. }
  75. [TestMethod]
  76. public void IsOpenShouldReturnFalse()
  77. {
  78. Assert.IsFalse(_subsystemSession.IsOpen);
  79. }
  80. }
  81. }