ServiceFactoryTest_CreateShellStream_ChannelOpenThrowsException.cs 3.8 KB

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