SubsystemSession_OnSessionErrorOccurred_Disposed.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_OnSessionErrorOccurred_Disposed
  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 _errorOccurredEventArgs;
  23. [TestInitialize]
  24. public void Setup()
  25. {
  26. Arrange();
  27. Act();
  28. }
  29. protected void Arrange()
  30. {
  31. var random = new Random();
  32. _subsystemName = random.Next().ToString(CultureInfo.InvariantCulture);
  33. _operationTimeout = TimeSpan.FromSeconds(30);
  34. _encoding = Encoding.UTF8;
  35. _disconnectedRegister = new List<EventArgs>();
  36. _errorOccurredRegister = new List<ExceptionEventArgs>();
  37. _errorOccurredEventArgs = new ExceptionEventArgs(new SystemException());
  38. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  39. _channelMock = new Mock<IChannelSession>(MockBehavior.Strict);
  40. var sequence = new MockSequence();
  41. _sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelMock.Object);
  42. _channelMock.InSequence(sequence).Setup(p => p.Open());
  43. _channelMock.InSequence(sequence).Setup(p => p.SendSubsystemRequest(_subsystemName)).Returns(true);
  44. _channelMock.InSequence(sequence).Setup(p => p.Close());
  45. _channelMock.InSequence(sequence).Setup(p => p.Dispose());
  46. _subsystemSession = new SubsystemSessionStub(
  47. _sessionMock.Object,
  48. _subsystemName,
  49. _operationTimeout,
  50. _encoding);
  51. _subsystemSession.Disconnected += (sender, args) => _disconnectedRegister.Add(args);
  52. _subsystemSession.ErrorOccurred += (sender, args) => _errorOccurredRegister.Add(args);
  53. _subsystemSession.Connect();
  54. _subsystemSession.Dispose();
  55. }
  56. protected void Act()
  57. {
  58. _sessionMock.Raise(s => s.ErrorOccured += null, _errorOccurredEventArgs);
  59. }
  60. [TestMethod]
  61. public void DisconnectHasNeverFired()
  62. {
  63. Assert.AreEqual(0, _disconnectedRegister.Count);
  64. }
  65. [TestMethod]
  66. public void ErrorOccurredHasNeverFired()
  67. {
  68. Assert.AreEqual(0, _errorOccurredRegister.Count);
  69. }
  70. [TestMethod]
  71. public void IsOpenShouldReturnFalse()
  72. {
  73. Assert.IsFalse(_subsystemSession.IsOpen);
  74. }
  75. }
  76. }