mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-10 17:25:51 +00:00
41053cb80f
* Initial plan * Refactor collection assertions to use CollectionAssert.AreEqual Replace Assert.IsTrue(xxx.IsEqualTo(yyy)) and Assert.IsTrue(xxx.SequenceEqual(yyy)) with CollectionAssert.AreEqual(expected, actual) across 55 test files Co-authored-by: Rob-Hague <5132141+Rob-Hague@users.noreply.github.com> * Fix argument order in CollectionAssert.AreEqual and remove unnecessary using directives - Fixed argument order in KeyExchangeDhGroupExchangeReplyTest.cs (expected first, actual second) - Fixed argument order in KeyExchangeInitMessageTest.cs (expected first, actual second) - Removed unnecessary 'using System.Linq' directives from all affected test files Co-authored-by: Rob-Hague <5132141+Rob-Hague@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Rob-Hague <5132141+Rob-Hague@users.noreply.github.com> Co-authored-by: Rob Hague <rob.hague00@gmail.com>
150 lines
5.6 KiB
C#
150 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
using Moq;
|
|
|
|
using Renci.SshNet.Channels;
|
|
using Renci.SshNet.Common;
|
|
|
|
namespace Renci.SshNet.Tests.Classes
|
|
{
|
|
[TestClass]
|
|
public class ShellStreamTest_Write_WriteBufferNotEmptyAndWriteMoreBytesThanBufferCanContain
|
|
{
|
|
private Mock<ISession> _sessionMock;
|
|
private Mock<IConnectionInfo> _connectionInfoMock;
|
|
private Mock<IChannelSession> _channelSessionMock;
|
|
private string _terminalName;
|
|
private uint _widthColumns;
|
|
private uint _heightRows;
|
|
private uint _widthPixels;
|
|
private uint _heightPixels;
|
|
private Dictionary<TerminalModes, uint> _terminalModes;
|
|
private ShellStream _shellStream;
|
|
private int _bufferSize;
|
|
|
|
private byte[] _data;
|
|
private int _offset;
|
|
private int _count;
|
|
private MockSequence _mockSequence;
|
|
private byte[] _bufferData;
|
|
private byte[] _expectedBytesSent;
|
|
|
|
[TestInitialize]
|
|
public void Initialize()
|
|
{
|
|
Arrange();
|
|
Act();
|
|
}
|
|
|
|
private void SetupData()
|
|
{
|
|
var random = new Random();
|
|
|
|
_terminalName = random.Next().ToString();
|
|
_widthColumns = (uint)random.Next();
|
|
_heightRows = (uint)random.Next();
|
|
_widthPixels = (uint)random.Next();
|
|
_heightPixels = (uint)random.Next();
|
|
_terminalModes = new Dictionary<TerminalModes, uint>();
|
|
_bufferSize = random.Next(100, 1000);
|
|
|
|
_bufferData = RandomNumberGenerator.GetBytes(_bufferSize - 60);
|
|
_data = RandomNumberGenerator.GetBytes(_bufferSize - _bufferData.Length + random.Next(1, 10));
|
|
_offset = 0;
|
|
_count = _data.Length;
|
|
|
|
_expectedBytesSent = [.. _bufferData, .. _data.Take(0, _bufferSize - _bufferData.Length)];
|
|
}
|
|
|
|
private void CreateMocks()
|
|
{
|
|
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
|
|
_sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
|
|
_connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
|
|
_channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
|
|
}
|
|
|
|
private void SetupMocks()
|
|
{
|
|
_mockSequence = new MockSequence();
|
|
|
|
_sessionMock.InSequence(_mockSequence)
|
|
.Setup(p => p.ConnectionInfo)
|
|
.Returns(_connectionInfoMock.Object);
|
|
_connectionInfoMock.InSequence(_mockSequence)
|
|
.Setup(p => p.Encoding)
|
|
.Returns(new UTF8Encoding());
|
|
_sessionMock.InSequence(_mockSequence)
|
|
.Setup(p => p.CreateChannelSession())
|
|
.Returns(_channelSessionMock.Object);
|
|
_channelSessionMock.InSequence(_mockSequence)
|
|
.Setup(p => p.Open());
|
|
_channelSessionMock.InSequence(_mockSequence)
|
|
.Setup(p => p.SendPseudoTerminalRequest(_terminalName,
|
|
_widthColumns,
|
|
_heightRows,
|
|
_widthPixels,
|
|
_heightPixels,
|
|
_terminalModes))
|
|
.Returns(true);
|
|
_channelSessionMock.InSequence(_mockSequence)
|
|
.Setup(p => p.SendShellRequest())
|
|
.Returns(true);
|
|
_channelSessionMock.InSequence(_mockSequence)
|
|
.Setup(p => p.SendData(_expectedBytesSent, 0, _expectedBytesSent.Length));
|
|
}
|
|
|
|
private void Arrange()
|
|
{
|
|
SetupData();
|
|
CreateMocks();
|
|
SetupMocks();
|
|
|
|
_shellStream = new ShellStream(_sessionMock.Object,
|
|
_terminalName,
|
|
_widthColumns,
|
|
_heightRows,
|
|
_widthPixels,
|
|
_heightPixels,
|
|
_terminalModes,
|
|
_bufferSize);
|
|
|
|
_shellStream.Write(_bufferData, 0, _bufferData.Length);
|
|
}
|
|
|
|
private void Act()
|
|
{
|
|
_shellStream.Write(_data, _offset, _count);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void BufferShouldBeSentToServer()
|
|
{
|
|
_channelSessionMock.VerifyAll();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void FlushShouldSendRemainingBytesInBufferToServer()
|
|
{
|
|
var expectedBytesSent = _data.Take(_bufferSize - _bufferData.Length, _data.Length + _bufferData.Length - _bufferSize);
|
|
byte[] actualBytesSent = null;
|
|
|
|
_channelSessionMock.InSequence(_mockSequence)
|
|
.Setup(p => p.SendData(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
|
|
.Callback<byte[], int, int>((data, offset, count) => actualBytesSent = data.Take(offset, count));
|
|
|
|
_shellStream.Flush();
|
|
|
|
CollectionAssert.AreEqual(expectedBytesSent, actualBytesSent);
|
|
|
|
_channelSessionMock.VerifyAll();
|
|
}
|
|
}
|
|
}
|