SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSize_Connected.cs 6.2 KB

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