ServiceFactoryTest_CreateShellStream_SendPseudoTerminalRequestThrowsException.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using Microsoft.Extensions.Logging.Abstractions;
  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_SendPseudoTerminalRequestThrowsException
  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 SshException _sendPseudoTerminalRequestException;
  25. private SshException _actualException;
  26. private void SetupData()
  27. {
  28. _terminalName = "test";
  29. _columns = 80;
  30. _rows = 20;
  31. _width = 300;
  32. _height = 100;
  33. _terminalModeValues = new Dictionary<TerminalModes, uint>();
  34. _bufferSize = 512;
  35. _sendPseudoTerminalRequestException = new SshException();
  36. _actualException = null;
  37. }
  38. private void CreateMocks()
  39. {
  40. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  41. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  42. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  43. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  44. }
  45. private void SetupMocks()
  46. {
  47. var sequence = new MockSequence();
  48. _sessionMock.InSequence(sequence)
  49. .Setup(p => p.ConnectionInfo)
  50. .Returns(_connectionInfoMock.Object);
  51. _connectionInfoMock.InSequence(sequence)
  52. .Setup(p => p.Encoding)
  53. .Returns(new UTF8Encoding());
  54. _sessionMock.InSequence(sequence)
  55. .Setup(p => p.CreateChannelSession())
  56. .Returns(_channelSessionMock.Object);
  57. _channelSessionMock.InSequence(sequence)
  58. .Setup(p => p.Open());
  59. _channelSessionMock.InSequence(sequence)
  60. .Setup(p => p.SendPseudoTerminalRequest(_terminalName, _columns, _rows, _width, _height, _terminalModeValues))
  61. .Throws(_sendPseudoTerminalRequestException);
  62. _channelSessionMock.InSequence(sequence)
  63. .Setup(p => p.Dispose());
  64. }
  65. private void Arrange()
  66. {
  67. SetupData();
  68. CreateMocks();
  69. SetupMocks();
  70. _serviceFactory = new ServiceFactory();
  71. }
  72. [TestInitialize]
  73. public void Initialize()
  74. {
  75. Arrange();
  76. Act();
  77. }
  78. private void Act()
  79. {
  80. try
  81. {
  82. _serviceFactory.CreateShellStream(_sessionMock.Object,
  83. _terminalName,
  84. _columns,
  85. _rows,
  86. _width,
  87. _height,
  88. _terminalModeValues,
  89. _bufferSize);
  90. Assert.Fail();
  91. }
  92. catch (SshException ex)
  93. {
  94. _actualException = ex;
  95. }
  96. }
  97. [TestMethod]
  98. public void CreateShellStreamShouldRethrowExceptionThrownBySendPseudoTerminalRequestOnChannelSession()
  99. {
  100. Assert.IsNotNull(_actualException);
  101. Assert.AreSame(_sendPseudoTerminalRequestException, _actualException);
  102. }
  103. [TestMethod]
  104. public void DisposeOnChannelSessionShouldHaveBeenInvokedOnce()
  105. {
  106. _channelSessionMock.Verify(p => p.Dispose(), Times.Once);
  107. }
  108. }
  109. }