SubsystemSession_Disconnect_Disposed.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_Disconnect_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. [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 = TimeSpan.FromSeconds(30);
  33. _encoding = Encoding.UTF8;
  34. _disconnectedRegister = new List<EventArgs>();
  35. _errorOccurredRegister = new List<ExceptionEventArgs>();
  36. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  37. _channelMock = new Mock<IChannelSession>(MockBehavior.Strict);
  38. var sequence = new MockSequence();
  39. _sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelMock.Object);
  40. _channelMock.InSequence(sequence).Setup(p => p.Open());
  41. _channelMock.InSequence(sequence).Setup(p => p.SendSubsystemRequest(_subsystemName)).Returns(true);
  42. _channelMock.InSequence(sequence).Setup(p => p.Close());
  43. _channelMock.InSequence(sequence).Setup(p => p.Dispose());
  44. _subsystemSession = new SubsystemSessionStub(
  45. _sessionMock.Object,
  46. _subsystemName,
  47. _operationTimeout,
  48. _encoding);
  49. _subsystemSession.Disconnected += (sender, args) => _disconnectedRegister.Add(args);
  50. _subsystemSession.ErrorOccurred += (sender, args) => _errorOccurredRegister.Add(args);
  51. _subsystemSession.Connect();
  52. _subsystemSession.Dispose();
  53. }
  54. protected void Act()
  55. {
  56. _subsystemSession.Disconnect();
  57. }
  58. [TestMethod]
  59. public void DisconnectHasNeverFired()
  60. {
  61. Assert.AreEqual(0, _disconnectedRegister.Count);
  62. }
  63. [TestMethod]
  64. public void ErrorOccurredHasNeverFired()
  65. {
  66. Assert.AreEqual(0, _errorOccurredRegister.Count);
  67. }
  68. [TestMethod]
  69. public void IsOpenShouldReturnFalse()
  70. {
  71. Assert.IsFalse(_subsystemSession.IsOpen);
  72. }
  73. [TestMethod]
  74. public void CloseOnChannelShouldBeInvokedOnce()
  75. {
  76. _channelMock.Verify(p => p.Close(), Times.Once);
  77. }
  78. [TestMethod]
  79. public void DisposeOnChannelShouldBeInvokedOnce()
  80. {
  81. _channelMock.Verify(p => p.Dispose(), Times.Once);
  82. }
  83. }
  84. }