2
0

ServiceFactoryTest_CreateShellStream_SendShellRequestThrowsException.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 SshException _sendShellRequestException;
  25. private SshException _actualException;
  26. private void SetupData()
  27. {
  28. var random = new Random();
  29. _terminalName = random.Next().ToString();
  30. _columns = (uint) random.Next();
  31. _rows = (uint) random.Next();
  32. _width = (uint) random.Next();
  33. _height = (uint) random.Next();
  34. _terminalModeValues = new Dictionary<TerminalModes, uint>();
  35. _bufferSize = random.Next();
  36. _sendShellRequestException = new SshException();
  37. _actualException = null;
  38. }
  39. private void CreateMocks()
  40. {
  41. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  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. .Returns(true);
  62. _channelSessionMock.InSequence(sequence)
  63. .Setup(p => p.SendShellRequest())
  64. .Throws(_sendShellRequestException);
  65. _channelSessionMock.InSequence(sequence)
  66. .Setup(p => p.Dispose());
  67. }
  68. private void Arrange()
  69. {
  70. SetupData();
  71. CreateMocks();
  72. SetupMocks();
  73. _serviceFactory = new ServiceFactory();
  74. }
  75. [TestInitialize]
  76. public void Initialize()
  77. {
  78. Arrange();
  79. Act();
  80. }
  81. private void Act()
  82. {
  83. try
  84. {
  85. _serviceFactory.CreateShellStream(_sessionMock.Object,
  86. _terminalName,
  87. _columns,
  88. _rows,
  89. _width,
  90. _height,
  91. _terminalModeValues,
  92. _bufferSize);
  93. Assert.Fail();
  94. }
  95. catch (SshException ex)
  96. {
  97. _actualException = ex;
  98. }
  99. }
  100. [TestMethod]
  101. public void CreateShellStreamShouldRethrowExceptionThrownBySendShellRequestOnChannelSession()
  102. {
  103. Assert.IsNotNull(_actualException);
  104. Assert.AreSame(_sendShellRequestException, _actualException);
  105. }
  106. [TestMethod]
  107. public void DisposeOnChannelSessionShouldHaveBeenInvokedOnce()
  108. {
  109. _channelSessionMock.Verify(p => p.Dispose(), Times.Once);
  110. }
  111. }
  112. }