2
0

ShellStreamTest_Write_WriteBufferNotEmptyAndWriteMoreBytesThanBufferCanContain.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.Extensions.Logging.Abstractions;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Moq;
  7. using Renci.SshNet.Abstractions;
  8. using Renci.SshNet.Channels;
  9. using Renci.SshNet.Common;
  10. namespace Renci.SshNet.Tests.Classes
  11. {
  12. [TestClass]
  13. public class ShellStreamTest_Write_WriteBufferNotEmptyAndWriteMoreBytesThanBufferCanContain
  14. {
  15. private Mock<ISession> _sessionMock;
  16. private Mock<IConnectionInfo> _connectionInfoMock;
  17. private Mock<IChannelSession> _channelSessionMock;
  18. private string _terminalName;
  19. private uint _widthColumns;
  20. private uint _heightRows;
  21. private uint _widthPixels;
  22. private uint _heightPixels;
  23. private Dictionary<TerminalModes, uint> _terminalModes;
  24. private ShellStream _shellStream;
  25. private int _bufferSize;
  26. private byte[] _data;
  27. private int _offset;
  28. private int _count;
  29. private MockSequence _mockSequence;
  30. private byte[] _bufferData;
  31. private byte[] _expectedBytesSent;
  32. [TestInitialize]
  33. public void Initialize()
  34. {
  35. Arrange();
  36. Act();
  37. }
  38. private void SetupData()
  39. {
  40. var random = new Random();
  41. _terminalName = random.Next().ToString();
  42. _widthColumns = (uint)random.Next();
  43. _heightRows = (uint)random.Next();
  44. _widthPixels = (uint)random.Next();
  45. _heightPixels = (uint)random.Next();
  46. _terminalModes = new Dictionary<TerminalModes, uint>();
  47. _bufferSize = random.Next(100, 1000);
  48. _bufferData = CryptoAbstraction.GenerateRandom(_bufferSize - 60);
  49. _data = CryptoAbstraction.GenerateRandom(_bufferSize - _bufferData.Length + random.Next(1, 10));
  50. _offset = 0;
  51. _count = _data.Length;
  52. _expectedBytesSent = [.. _bufferData, .. _data.Take(0, _bufferSize - _bufferData.Length)];
  53. }
  54. private void CreateMocks()
  55. {
  56. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  57. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  58. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  59. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  60. }
  61. private void SetupMocks()
  62. {
  63. _mockSequence = new MockSequence();
  64. _sessionMock.InSequence(_mockSequence)
  65. .Setup(p => p.ConnectionInfo)
  66. .Returns(_connectionInfoMock.Object);
  67. _connectionInfoMock.InSequence(_mockSequence)
  68. .Setup(p => p.Encoding)
  69. .Returns(new UTF8Encoding());
  70. _sessionMock.InSequence(_mockSequence)
  71. .Setup(p => p.CreateChannelSession())
  72. .Returns(_channelSessionMock.Object);
  73. _channelSessionMock.InSequence(_mockSequence)
  74. .Setup(p => p.Open());
  75. _channelSessionMock.InSequence(_mockSequence)
  76. .Setup(p => p.SendPseudoTerminalRequest(_terminalName,
  77. _widthColumns,
  78. _heightRows,
  79. _widthPixels,
  80. _heightPixels,
  81. _terminalModes))
  82. .Returns(true);
  83. _channelSessionMock.InSequence(_mockSequence)
  84. .Setup(p => p.SendShellRequest())
  85. .Returns(true);
  86. _channelSessionMock.InSequence(_mockSequence)
  87. .Setup(p => p.SendData(_expectedBytesSent, 0, _expectedBytesSent.Length));
  88. }
  89. private void Arrange()
  90. {
  91. SetupData();
  92. CreateMocks();
  93. SetupMocks();
  94. _shellStream = new ShellStream(_sessionMock.Object,
  95. _terminalName,
  96. _widthColumns,
  97. _heightRows,
  98. _widthPixels,
  99. _heightPixels,
  100. _terminalModes,
  101. _bufferSize);
  102. _shellStream.Write(_bufferData, 0, _bufferData.Length);
  103. }
  104. private void Act()
  105. {
  106. _shellStream.Write(_data, _offset, _count);
  107. }
  108. [TestMethod]
  109. public void BufferShouldBeSentToServer()
  110. {
  111. _channelSessionMock.VerifyAll();
  112. }
  113. [TestMethod]
  114. public void FlushShouldSendRemainingBytesInBufferToServer()
  115. {
  116. var expectedBytesSent = _data.Take(_bufferSize - _bufferData.Length, _data.Length + _bufferData.Length - _bufferSize);
  117. byte[] actualBytesSent = null;
  118. _channelSessionMock.InSequence(_mockSequence)
  119. .Setup(p => p.SendData(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
  120. .Callback<byte[], int, int>((data, offset, count) => actualBytesSent = data.Take(offset, count));
  121. _shellStream.Flush();
  122. Assert.IsNotNull(actualBytesSent);
  123. Assert.AreEqual(expectedBytesSent.Length, actualBytesSent.Length);
  124. Assert.IsTrue(expectedBytesSent.IsEqualTo(actualBytesSent));
  125. _channelSessionMock.VerifyAll();
  126. }
  127. }
  128. }