2
0

ServiceFactoryTest_CreateShellStream_SendPseudoTerminalRequestThrowsException.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_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 int _expectSize;
  25. private SshException _sendPseudoTerminalRequestException;
  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. _sendPseudoTerminalRequestException = 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. .Throws(_sendPseudoTerminalRequestException);
  64. _channelSessionMock.InSequence(sequence)
  65. .Setup(p => p.Dispose());
  66. }
  67. private void Arrange()
  68. {
  69. SetupData();
  70. CreateMocks();
  71. SetupMocks();
  72. _serviceFactory = new ServiceFactory();
  73. }
  74. [TestInitialize]
  75. public void Initialize()
  76. {
  77. Arrange();
  78. Act();
  79. }
  80. private void Act()
  81. {
  82. try
  83. {
  84. _serviceFactory.CreateShellStream(_sessionMock.Object,
  85. _terminalName,
  86. _columns,
  87. _rows,
  88. _width,
  89. _height,
  90. _terminalModeValues,
  91. _bufferSize,
  92. _expectSize);
  93. Assert.Fail();
  94. }
  95. catch (SshException ex)
  96. {
  97. _actualException = ex;
  98. }
  99. }
  100. [TestMethod]
  101. public void CreateShellStreamShouldRethrowExceptionThrownBySendPseudoTerminalRequestOnChannelSession()
  102. {
  103. Assert.IsNotNull(_actualException);
  104. Assert.AreSame(_sendPseudoTerminalRequestException, _actualException);
  105. }
  106. [TestMethod]
  107. public void DisposeOnChannelSessionShouldHaveBeenInvokedOnce()
  108. {
  109. _channelSessionMock.Verify(p => p.Dispose(), Times.Once);
  110. }
  111. }
  112. }