SshCommandTest_EndExecute.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Moq;
  6. using Renci.SshNet.Channels;
  7. using Renci.SshNet.Common;
  8. using Renci.SshNet.Tests.Common;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. [TestClass]
  12. public class SshCommand_EndExecute : TestBase
  13. {
  14. private Mock<ISession> _sessionMock;
  15. private Mock<IChannelSession> _channelSessionMock;
  16. private string _commandText;
  17. private Encoding _encoding;
  18. private SshCommand _sshCommand;
  19. protected override void OnInit()
  20. {
  21. base.OnInit();
  22. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  23. _commandText = new Random().Next().ToString(CultureInfo.InvariantCulture);
  24. _encoding = Encoding.UTF8;
  25. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  26. _sshCommand = new SshCommand(_sessionMock.Object, _commandText, _encoding);
  27. }
  28. [TestMethod]
  29. public void EndExecute_ChannelClosed_ShouldDisposeChannelSession()
  30. {
  31. var seq = new MockSequence();
  32. _sessionMock.InSequence(seq).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  33. _channelSessionMock.InSequence(seq).Setup(p => p.Open());
  34. _channelSessionMock.InSequence(seq).Setup(p => p.SendExecRequest(_commandText))
  35. .Returns(true)
  36. .Raises(c => c.Closed += null, new ChannelEventArgs(5));
  37. _channelSessionMock.InSequence(seq).Setup(p => p.Dispose());
  38. var asyncResult = _sshCommand.BeginExecute();
  39. _sshCommand.EndExecute(asyncResult);
  40. _channelSessionMock.Verify(p => p.Dispose(), Times.Once);
  41. }
  42. [TestMethod]
  43. public void EndExecute_ChannelOpen_ShouldSendEofAndCloseAndDisposeChannelSession()
  44. {
  45. var seq = new MockSequence();
  46. _sessionMock.InSequence(seq).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  47. _channelSessionMock.InSequence(seq).Setup(p => p.Open());
  48. _channelSessionMock.InSequence(seq).Setup(p => p.SendExecRequest(_commandText))
  49. .Returns(true)
  50. .Raises(c => c.Closed += null, new ChannelEventArgs(5));
  51. _channelSessionMock.InSequence(seq).Setup(p => p.Dispose());
  52. var asyncResult = _sshCommand.BeginExecute();
  53. _sshCommand.EndExecute(asyncResult);
  54. _channelSessionMock.Verify(p => p.Dispose(), Times.Once);
  55. }
  56. }
  57. }