SubsystemSession_OnChannelException_Connected.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  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_Connected
  13. {
  14. private Mock<ISession> _sessionMock;
  15. private Mock<IChannelSession> _channelMock;
  16. private string _subsystemName;
  17. private SubsystemSessionStub _subsystemSession;
  18. private TimeSpan _operationTimeout;
  19. private Encoding _encoding;
  20. private IList<EventArgs> _disconnectedRegister;
  21. private IList<ExceptionEventArgs> _errorOccurredRegister;
  22. private ExceptionEventArgs _channelExceptionEventArgs;
  23. private MockSequence _sequence;
  24. [TestInitialize]
  25. public void Setup()
  26. {
  27. Arrange();
  28. Act();
  29. }
  30. protected void Arrange()
  31. {
  32. var random = new Random();
  33. _subsystemName = random.Next().ToString(CultureInfo.InvariantCulture);
  34. _operationTimeout = TimeSpan.FromSeconds(30);
  35. _encoding = Encoding.UTF8;
  36. _disconnectedRegister = new List<EventArgs>();
  37. _errorOccurredRegister = new List<ExceptionEventArgs>();
  38. _channelExceptionEventArgs = new ExceptionEventArgs(new SystemException());
  39. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  40. _channelMock = new Mock<IChannelSession>(MockBehavior.Strict);
  41. _sequence = new MockSequence();
  42. _sessionMock.InSequence(_sequence).Setup(p => p.CreateChannelSession()).Returns(_channelMock.Object);
  43. _channelMock.InSequence(_sequence).Setup(p => p.Open());
  44. _channelMock.InSequence(_sequence).Setup(p => p.SendSubsystemRequest(_subsystemName)).Returns(true);
  45. _subsystemSession = new SubsystemSessionStub(
  46. _sessionMock.Object,
  47. _subsystemName,
  48. _operationTimeout,
  49. _encoding);
  50. _subsystemSession.Disconnected += (sender, args) => _disconnectedRegister.Add(args);
  51. _subsystemSession.ErrorOccurred += (sender, args) => _errorOccurredRegister.Add(args);
  52. _subsystemSession.Connect();
  53. }
  54. protected void Act()
  55. {
  56. _channelMock.Raise(s => s.Exception += null, _channelExceptionEventArgs);
  57. }
  58. [TestMethod]
  59. public void DisconnectHasNeverFired()
  60. {
  61. Assert.AreEqual(0, _disconnectedRegister.Count);
  62. }
  63. [TestMethod]
  64. public void ErrorOccurredHasFiredOnce()
  65. {
  66. Assert.AreEqual(1, _errorOccurredRegister.Count);
  67. Assert.AreSame(_channelExceptionEventArgs.Exception, _errorOccurredRegister[0].Exception);
  68. }
  69. [TestMethod]
  70. public void IsOpenShouldReturnTrueWhenChannelIsOpen()
  71. {
  72. _channelMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  73. Assert.IsTrue(_subsystemSession.IsOpen);
  74. _channelMock.Verify(p => p.IsOpen, Times.Exactly(1));
  75. }
  76. [TestMethod]
  77. public void IsOpenShouldReturnFalseWhenChannelIsNotOpen()
  78. {
  79. _channelMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(false);
  80. Assert.IsFalse(_subsystemSession.IsOpen);
  81. _channelMock.Verify(p => p.IsOpen, Times.Exactly(1));
  82. }
  83. [TestMethod]
  84. public void CloseOnChannelShouldNeverBeInvoked()
  85. {
  86. _channelMock.Verify(p => p.Close(), Times.Never);
  87. }
  88. [TestMethod]
  89. public void DisposeOnChannelShouldNeverBeInvoked()
  90. {
  91. _channelMock.Verify(p => p.Dispose(), Times.Never);
  92. }
  93. }
  94. }