ServiceFactoryTest_CreateShellStream_ChannelOpenThrowsException.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_ChannelOpenThrowsException
  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 _channelOpenException;
  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. _channelOpenException = 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. .Throws(_channelOpenException);
  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. _expectSize);
  91. Assert.Fail();
  92. }
  93. catch (SshException ex)
  94. {
  95. _actualException = ex;
  96. }
  97. }
  98. [TestMethod]
  99. public void CreateShellStreamShouldRethrowExceptionThrownByOpenOnChannelSession()
  100. {
  101. Assert.IsNotNull(_actualException);
  102. Assert.AreSame(_channelOpenException, _actualException);
  103. }
  104. [TestMethod]
  105. public void DisposeOnChannelSessionShouldHaveBeenInvokedOnce()
  106. {
  107. _channelSessionMock.Verify(p => p.Dispose(), Times.Once);
  108. }
  109. }
  110. }