SshCommandTest_Dispose.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Text;
  5. using Microsoft.Extensions.Logging.Abstractions;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Moq;
  8. using Renci.SshNet.Channels;
  9. using Renci.SshNet.Common;
  10. using Renci.SshNet.Tests.Common;
  11. namespace Renci.SshNet.Tests.Classes
  12. {
  13. [TestClass]
  14. public class SshCommandTest_Dispose : TestBase
  15. {
  16. private Mock<ISession> _sessionMock;
  17. private Mock<IChannelSession> _channelSessionMock;
  18. private string _commandText;
  19. private Encoding _encoding;
  20. private SshCommand _sshCommand;
  21. private Stream _outputStream;
  22. private Stream _extendedOutputStream;
  23. protected override void OnInit()
  24. {
  25. base.OnInit();
  26. Arrange();
  27. Act();
  28. }
  29. private void Arrange()
  30. {
  31. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  32. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  33. _commandText = new Random().Next().ToString(CultureInfo.InvariantCulture);
  34. _encoding = Encoding.UTF8;
  35. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  36. var seq = new MockSequence();
  37. _sessionMock.InSequence(seq).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  38. _channelSessionMock.InSequence(seq).Setup(p => p.Open());
  39. _channelSessionMock.InSequence(seq).Setup(p => p.SendExecRequest(_commandText)).Returns(true);
  40. _channelSessionMock.InSequence(seq).Setup(p => p.Dispose());
  41. _sshCommand = new SshCommand(_sessionMock.Object, _commandText, _encoding);
  42. _sshCommand.BeginExecute();
  43. _outputStream = _sshCommand.OutputStream;
  44. _extendedOutputStream = _sshCommand.ExtendedOutputStream;
  45. }
  46. private void Act()
  47. {
  48. _sshCommand.Dispose();
  49. }
  50. [TestMethod]
  51. public void ChannelSessionShouldBeDisposedOnce()
  52. {
  53. _channelSessionMock.Verify(p => p.Dispose(), Times.Once);
  54. }
  55. [TestMethod]
  56. public void OutputStreamShouldHaveBeenDisposed()
  57. {
  58. Assert.AreEqual(-1, _outputStream.ReadByte());
  59. }
  60. [TestMethod]
  61. public void ExtendedOutputStreamShouldHaveBeenDisposed()
  62. {
  63. Assert.AreEqual(-1, _extendedOutputStream.ReadByte());
  64. }
  65. [TestMethod]
  66. public void RaisingDisconnectedOnSessionShouldDoNothing()
  67. {
  68. _sessionMock.Raise(s => s.Disconnected += null, new EventArgs());
  69. }
  70. [TestMethod]
  71. public void RaisingErrorOccurredOnSessionShouldDoNothing()
  72. {
  73. _sessionMock.Raise(s => s.ErrorOccured += null, new ExceptionEventArgs(new Exception()));
  74. }
  75. [TestMethod]
  76. public void InvokingDisposeAgainShouldNotRaiseAnException()
  77. {
  78. _sshCommand.Dispose();
  79. }
  80. }
  81. }