ServiceFactoryTest_CreateShellStream_Success.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_Success
  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 ShellStream _shellStream;
  24. private void SetupData()
  25. {
  26. _terminalName = "test";
  27. _columns = 80;
  28. _rows = 20;
  29. _width = 300;
  30. _height = 100;
  31. _terminalModeValues = new Dictionary<TerminalModes, uint>();
  32. _bufferSize = 512;
  33. }
  34. private void CreateMocks()
  35. {
  36. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  37. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  38. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  39. }
  40. private void SetupMocks()
  41. {
  42. var sequence = new MockSequence();
  43. _sessionMock.InSequence(sequence)
  44. .Setup(p => p.ConnectionInfo)
  45. .Returns(_connectionInfoMock.Object);
  46. _connectionInfoMock.InSequence(sequence)
  47. .Setup(p => p.Encoding)
  48. .Returns(new UTF8Encoding());
  49. _sessionMock.InSequence(sequence)
  50. .Setup(p => p.CreateChannelSession())
  51. .Returns(_channelSessionMock.Object);
  52. _channelSessionMock.InSequence(sequence)
  53. .Setup(p => p.Open());
  54. _channelSessionMock.InSequence(sequence)
  55. .Setup(p => p.SendPseudoTerminalRequest(_terminalName,
  56. _columns,
  57. _rows,
  58. _width,
  59. _height,
  60. _terminalModeValues))
  61. .Returns(true);
  62. _channelSessionMock.InSequence(sequence)
  63. .Setup(p => p.SendShellRequest())
  64. .Returns(true);
  65. }
  66. private void Arrange()
  67. {
  68. SetupData();
  69. CreateMocks();
  70. SetupMocks();
  71. _serviceFactory = new ServiceFactory();
  72. }
  73. [TestInitialize]
  74. public void Initialize()
  75. {
  76. Arrange();
  77. Act();
  78. }
  79. private void Act()
  80. {
  81. _shellStream = _serviceFactory.CreateShellStream(_sessionMock.Object,
  82. _terminalName,
  83. _columns,
  84. _rows,
  85. _width,
  86. _height,
  87. _terminalModeValues,
  88. _bufferSize);
  89. }
  90. [TestMethod]
  91. public void CreateShellStreamShouldNotReturnNull()
  92. {
  93. Assert.IsNotNull(_shellStream);
  94. }
  95. [TestMethod]
  96. public void SendPseudoTerminalRequestShouldHaveBeenInvokedOnce()
  97. {
  98. _channelSessionMock.Verify(p => p.SendPseudoTerminalRequest(_terminalName,
  99. _columns,
  100. _rows,
  101. _width,
  102. _height,
  103. _terminalModeValues),
  104. Times.Once);
  105. }
  106. [TestMethod]
  107. public void SendShellRequestShouldHaveBeenInvokedOnce()
  108. {
  109. _channelSessionMock.Verify(p => p.SendShellRequest(), Times.Once);
  110. }
  111. }
  112. }