SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSize_Connected.cs 6.0 KB

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