SshCommandTest_Dispose.cs 3.4 KB

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