ServiceFactoryTest_CreateShellStream_SendShellRequestThrowsException.cs 4.5 KB

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