SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSizeAndTerminalModes_Connected.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 ShellStream _expected;
  22. private ShellStream _actual;
  23. protected override void SetupData()
  24. {
  25. var random = new Random();
  26. _connectionInfo = new ConnectionInfo("host", "user", new NoneAuthenticationMethod("userauth"));
  27. _terminalName = random.Next().ToString();
  28. _widthColumns = (uint) random.Next();
  29. _heightRows = (uint) random.Next();
  30. _widthPixels = (uint) random.Next();
  31. _heightPixels = (uint) random.Next();
  32. _terminalModes = new Dictionary<TerminalModes, uint>();
  33. _bufferSize = random.Next(100, 1000);
  34. _expected = CreateShellStream();
  35. }
  36. protected override void SetupMocks()
  37. {
  38. var sequence = new MockSequence();
  39. ServiceFactoryMock.InSequence(sequence)
  40. .Setup(p => p.CreateSocketFactory())
  41. .Returns(SocketFactoryMock.Object);
  42. ServiceFactoryMock.InSequence(sequence)
  43. .Setup(p => p.CreateSession(_connectionInfo, SocketFactoryMock.Object))
  44. .Returns(SessionMock.Object);
  45. SessionMock.InSequence(sequence)
  46. .Setup(p => p.Connect());
  47. ServiceFactoryMock.InSequence(sequence)
  48. .Setup(p => p.CreateShellStream(SessionMock.Object,
  49. _terminalName,
  50. _widthColumns,
  51. _heightRows,
  52. _widthPixels,
  53. _heightPixels,
  54. _terminalModes,
  55. _bufferSize))
  56. .Returns(_expected);
  57. }
  58. protected override void Arrange()
  59. {
  60. base.Arrange();
  61. _sshClient = new SshClient(_connectionInfo, false, ServiceFactoryMock.Object);
  62. _sshClient.Connect();
  63. }
  64. protected override void Act()
  65. {
  66. _actual = _sshClient.CreateShellStream(_terminalName,
  67. _widthColumns,
  68. _heightRows,
  69. _widthPixels,
  70. _heightPixels,
  71. _bufferSize,
  72. _terminalModes);
  73. }
  74. [TestMethod]
  75. public void CreateShellStreamOnServiceFactoryShouldBeInvokedOnce()
  76. {
  77. ServiceFactoryMock.Verify(p => p.CreateShellStream(SessionMock.Object,
  78. _terminalName,
  79. _widthColumns,
  80. _heightRows,
  81. _widthPixels,
  82. _heightPixels,
  83. _terminalModes,
  84. _bufferSize),
  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. _terminalModes))
  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. _terminalModes,
  118. 1);
  119. }
  120. }
  121. }