mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-10 09:15:47 +00:00
4e02502bdf
* Add .NET 10 target * fix IDE0031 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0031 * fix ca5399 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca5399 * fix ca1515 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1515 * fix ca2002 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2002 * fix ca1508 new false positives. https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1508 * fix ca2000 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2000 * fix ca2025 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2025 * fix ca1849 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1849 * fix Reverse() overloads because of https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/10.0/csharp-overload-resolution * supress CA2002 * Use extension members for ThrowHelpers * use extension members for CryptoAbstractions * use extension member for DateTime.UnixEpoch * use extension members for string.Join etc * use extension members for Convert.To/FromHexString * disable CA1508 * Update .NET 10 RC2 * Workaround Build Regression in .NET 10 RC2 https://github.com/dotnet/sdk/issues/51265 * suppress new warnings introduced by merge * Update to .NET 10 final release * Revert "Workaround Build Regression in .NET 10 RC2" This is fixed in the final release. This reverts commit5a59ac9aa8. * fix new warnings with MSTest 4 + .NET 10 * use same Randomizer instance * disable CA2000 * reduce CA1849 suppressions and disable duplicate S6966 * disable preview analyzers reverts6c3c06d95a
150 lines
5.7 KiB
C#
150 lines
5.7 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_WriteBufferEmptyAndWriteMoreBytesThanBufferSize
|
|
{
|
|
private Mock<ISession> _sessionMock;
|
|
private Mock<IConnectionInfo> _connectionInfoMock;
|
|
private Mock<IChannelSession> _channelSessionMock;
|
|
private MockSequence _mockSequence;
|
|
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 byte[] _expectedBytesSent1;
|
|
private byte[] _expectedBytesSent2;
|
|
|
|
[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);
|
|
|
|
_data = RandomNumberGenerator.GetBytes((_bufferSize * 2) + 10);
|
|
_offset = 0;
|
|
_count = _data.Length;
|
|
|
|
_expectedBytesSent1 = _data.Take(0, _bufferSize);
|
|
_expectedBytesSent2 = _data.Take(_bufferSize, _bufferSize);
|
|
}
|
|
|
|
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(_expectedBytesSent1, 0, _bufferSize));
|
|
_ = _channelSessionMock.InSequence(_mockSequence)
|
|
.Setup(p => p.SendData(_expectedBytesSent2, 0, _bufferSize));
|
|
}
|
|
|
|
private void Arrange()
|
|
{
|
|
SetupData();
|
|
CreateMocks();
|
|
SetupMocks();
|
|
|
|
_shellStream = new ShellStream(_sessionMock.Object,
|
|
_terminalName,
|
|
_widthColumns,
|
|
_heightRows,
|
|
_widthPixels,
|
|
_heightPixels,
|
|
_terminalModes,
|
|
_bufferSize);
|
|
}
|
|
|
|
private void Act()
|
|
{
|
|
_shellStream.Write(_data, _offset, _count);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void BufferShouldHaveBeenFlushedTwice()
|
|
{
|
|
_channelSessionMock.VerifyAll();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void FlushShouldSendRemainingBytesToServer()
|
|
{
|
|
var expectedBytesSent = _data.Take(_bufferSize * 2, _data.Length - (_bufferSize * 2));
|
|
|
|
_ = _channelSessionMock.InSequence(_mockSequence)
|
|
.Setup(p => p.SendData(
|
|
It.Is<byte[]>(data => data.Take(expectedBytesSent.Length).IsEqualTo(expectedBytesSent)),
|
|
0,
|
|
expectedBytesSent.Length));
|
|
|
|
_shellStream.Flush();
|
|
|
|
_channelSessionMock.VerifyAll();
|
|
}
|
|
}
|
|
}
|