2
0

SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSizeAndTerminalModes_Connected.cs 6.4 KB

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