2
0

ServiceFactoryTest_CreateShellStream_SendShellRequestThrowsException.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Channels;
  6. using Renci.SshNet.Common;
  7. namespace Renci.SshNet.Tests.Classes
  8. {
  9. [TestClass]
  10. public class ServiceFactoryTest_CreateShellStream_SendShellRequestThrowsException
  11. {
  12. private Mock<ISession> _sessionMock;
  13. private Mock<IConnectionInfo> _connectionInfoMock;
  14. private Mock<IChannelSession> _channelSessionMock;
  15. private ServiceFactory _serviceFactory;
  16. private string _terminalName;
  17. private uint _columns;
  18. private uint _rows;
  19. private uint _width;
  20. private uint _height;
  21. private IDictionary<TerminalModes, uint> _terminalModeValues;
  22. private int _bufferSize;
  23. private SshException _sendShellRequestException;
  24. private SshException _actualException;
  25. private void SetupData()
  26. {
  27. _terminalName = "test";
  28. _columns = 80;
  29. _rows = 20;
  30. _width = 300;
  31. _height = 100;
  32. _terminalModeValues = new Dictionary<TerminalModes, uint>();
  33. _bufferSize = 512;
  34. _sendShellRequestException = new SshException();
  35. _actualException = null;
  36. }
  37. private void CreateMocks()
  38. {
  39. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  40. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  41. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  42. }
  43. private void SetupMocks()
  44. {
  45. var sequence = new MockSequence();
  46. _sessionMock.InSequence(sequence)
  47. .Setup(p => p.ConnectionInfo)
  48. .Returns(_connectionInfoMock.Object);
  49. _connectionInfoMock.InSequence(sequence)
  50. .Setup(p => p.Encoding)
  51. .Returns(new UTF8Encoding());
  52. _sessionMock.InSequence(sequence)
  53. .Setup(p => p.CreateChannelSession())
  54. .Returns(_channelSessionMock.Object);
  55. _channelSessionMock.InSequence(sequence)
  56. .Setup(p => p.Open());
  57. _channelSessionMock.InSequence(sequence)
  58. .Setup(p => p.SendPseudoTerminalRequest(_terminalName, _columns, _rows, _width, _height, _terminalModeValues))
  59. .Returns(true);
  60. _channelSessionMock.InSequence(sequence)
  61. .Setup(p => p.SendShellRequest())
  62. .Throws(_sendShellRequestException);
  63. _channelSessionMock.InSequence(sequence)
  64. .Setup(p => p.Dispose());
  65. }
  66. private void Arrange()
  67. {
  68. SetupData();
  69. CreateMocks();
  70. SetupMocks();
  71. _serviceFactory = new ServiceFactory();
  72. }
  73. [TestInitialize]
  74. public void Initialize()
  75. {
  76. Arrange();
  77. Act();
  78. }
  79. private void Act()
  80. {
  81. try
  82. {
  83. _serviceFactory.CreateShellStream(_sessionMock.Object,
  84. _terminalName,
  85. _columns,
  86. _rows,
  87. _width,
  88. _height,
  89. _terminalModeValues,
  90. _bufferSize);
  91. Assert.Fail();
  92. }
  93. catch (SshException ex)
  94. {
  95. _actualException = ex;
  96. }
  97. }
  98. [TestMethod]
  99. public void CreateShellStreamShouldRethrowExceptionThrownBySendShellRequestOnChannelSession()
  100. {
  101. Assert.IsNotNull(_actualException);
  102. Assert.AreSame(_sendShellRequestException, _actualException);
  103. }
  104. [TestMethod]
  105. public void DisposeOnChannelSessionShouldHaveBeenInvokedOnce()
  106. {
  107. _channelSessionMock.Verify(p => p.Dispose(), Times.Once);
  108. }
  109. }
  110. }