SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSizeAndTerminalModes_Connected.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Extensions.Logging.Abstractions;
  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 SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSizeAndTerminalModes_Connected : BaseClientTestBase
  12. {
  13. private SshClient _sshClient;
  14. private ConnectionInfo _connectionInfo;
  15. private string _terminalName;
  16. private uint _widthColumns;
  17. private uint _heightRows;
  18. private uint _widthPixels;
  19. private uint _heightPixels;
  20. private Dictionary<TerminalModes, uint> _terminalModes;
  21. private int _bufferSize;
  22. private ShellStream _expected;
  23. private ShellStream _actual;
  24. protected override void SetupData()
  25. {
  26. var random = new Random();
  27. _connectionInfo = new ConnectionInfo("host", "user", new NoneAuthenticationMethod("userauth"));
  28. _terminalName = random.Next().ToString();
  29. _widthColumns = (uint)random.Next();
  30. _heightRows = (uint)random.Next();
  31. _widthPixels = (uint)random.Next();
  32. _heightPixels = (uint)random.Next();
  33. _terminalModes = new Dictionary<TerminalModes, uint>();
  34. _bufferSize = random.Next(100, 1000);
  35. _expected = CreateShellStream();
  36. }
  37. protected override void SetupMocks()
  38. {
  39. var sequence = new MockSequence();
  40. ServiceFactoryMock.InSequence(sequence)
  41. .Setup(p => p.CreateSocketFactory())
  42. .Returns(SocketFactoryMock.Object);
  43. ServiceFactoryMock.InSequence(sequence)
  44. .Setup(p => p.CreateSession(_connectionInfo, SocketFactoryMock.Object))
  45. .Returns(SessionMock.Object);
  46. SessionMock.InSequence(sequence)
  47. .Setup(p => p.Connect());
  48. ServiceFactoryMock.InSequence(sequence)
  49. .Setup(p => p.CreateShellStream(SessionMock.Object,
  50. _terminalName,
  51. _widthColumns,
  52. _heightRows,
  53. _widthPixels,
  54. _heightPixels,
  55. _terminalModes,
  56. _bufferSize))
  57. .Returns(_expected);
  58. }
  59. protected override void Arrange()
  60. {
  61. base.Arrange();
  62. _sshClient = new SshClient(_connectionInfo, false, ServiceFactoryMock.Object);
  63. _sshClient.Connect();
  64. }
  65. protected override void Act()
  66. {
  67. _actual = _sshClient.CreateShellStream(_terminalName,
  68. _widthColumns,
  69. _heightRows,
  70. _widthPixels,
  71. _heightPixels,
  72. _bufferSize,
  73. _terminalModes);
  74. }
  75. [TestMethod]
  76. public void CreateShellStreamOnServiceFactoryShouldBeInvokedOnce()
  77. {
  78. ServiceFactoryMock.Verify(p => p.CreateShellStream(SessionMock.Object,
  79. _terminalName,
  80. _widthColumns,
  81. _heightRows,
  82. _widthPixels,
  83. _heightPixels,
  84. _terminalModes,
  85. _bufferSize),
  86. Times.Once);
  87. }
  88. [TestMethod]
  89. public void CreateShellStreamShouldReturnValueReturnedByCreateShellStreamOnServiceFactory()
  90. {
  91. Assert.IsNotNull(_actual);
  92. Assert.AreSame(_expected, _actual);
  93. }
  94. private ShellStream CreateShellStream()
  95. {
  96. var sessionMock = new Mock<ISession>(MockBehavior.Loose);
  97. sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  98. var channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  99. sessionMock.Setup(p => p.ConnectionInfo)
  100. .Returns(new ConnectionInfo("A", "B", new PasswordAuthenticationMethod("A", "B")));
  101. sessionMock.Setup(p => p.CreateChannelSession())
  102. .Returns(channelSessionMock.Object);
  103. channelSessionMock.Setup(p => p.Open());
  104. channelSessionMock.Setup(p => p.SendPseudoTerminalRequest(_terminalName,
  105. _widthColumns,
  106. _heightRows,
  107. _widthPixels,
  108. _heightPixels,
  109. _terminalModes))
  110. .Returns(true);
  111. channelSessionMock.Setup(p => p.SendShellRequest())
  112. .Returns(true);
  113. return new ShellStream(sessionMock.Object,
  114. _terminalName,
  115. _widthColumns,
  116. _heightRows,
  117. _widthPixels,
  118. _heightPixels,
  119. _terminalModes,
  120. 1);
  121. }
  122. }
  123. }