Files
ssh.net/test/Renci.SshNet.Tests/Classes/Sftp/SftpSessionTest_Connected_RequestStatVfs.cs
Rob Hague fa98d59384 Use the read buffer in UploadFile for the SFTP write packets (#1798)
* Use the read buffer in UploadFile for the SFTP write packets

In SftpClient.UploadFile, a buffer is allocated to read from the given stream, and for
each read, another array is allocated for the SFTP write packet (which consists of that
data prepended with headers). This change effectively leaves space at the start of the
buffer for the headers such that it can be used to assemble the packets without that
per-packet array allocation.

There are cleaner/more general ways to do this (e.g. for all packet types, leave space
for the SSH headers as well), but this gets the most impact for about as much effort as
I can be bothered with.

* Rent from pool
2026-06-26 18:20:53 +02:00

177 lines
7.9 KiB
C#

using System;
using System.Text;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Channels;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
using Renci.SshNet.Sftp.Responses;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes.Sftp
{
[TestClass]
public class SftpSessionTest_Connected_RequestStatVfs
{
#region SftpSession.Connect()
private Mock<ISession> _sessionMock;
private Mock<IChannelSession> _channelSessionMock;
private ISftpResponseFactory _sftpResponseFactory;
private SftpSession _sftpSession;
private int _operationTimeout;
private Encoding _encoding;
private uint _protocolVersion;
private SftpVersionResponse _sftpVersionResponse;
private SftpNameResponse _sftpNameResponse;
private byte[] _sftpInitRequestBytes;
private byte[] _sftpRealPathRequestBytes;
#endregion SftpSession.Connect()
private byte[] _sftpStatVfsRequestBytes;
private StatVfsResponse _sftpStatVfsResponse;
private ulong _bAvail;
private string _path;
private SftpFileSystemInformation _actual;
[TestInitialize]
public void Setup()
{
Arrange();
Act();
}
private void SetupData()
{
var random = new Random();
#region SftpSession.Connect()
_operationTimeout = random.Next(100, 500);
_encoding = Encoding.UTF8;
_protocolVersion = 3;
_sftpResponseFactory = new SftpResponseFactory();
_sftpInitRequestBytes = new SftpInitRequestBuilder().WithVersion(SftpSession.MaximumSupportedVersion)
.Build()
.GetBytes();
_sftpVersionResponse = new SftpVersionResponseBuilder().WithVersion(_protocolVersion)
.WithExtension("statvfs@openssh.com", "")
.Build();
_sftpRealPathRequestBytes = new SftpRealPathRequestBuilder().WithProtocolVersion(_protocolVersion)
.WithRequestId(1)
.WithPath(".")
.WithEncoding(_encoding)
.Build()
.GetBytes();
_sftpNameResponse = new SftpNameResponseBuilder().WithProtocolVersion(_protocolVersion)
.WithResponseId(1U)
.WithEncoding(_encoding)
.WithFile("ABC", SftpFileAttributesBuilder.Empty)
.Build();
#endregion SftpSession.Connect()
_path = random.Next().ToString();
_bAvail = (ulong)random.Next(0, int.MaxValue);
_sftpStatVfsRequestBytes = new SftpStatVfsRequestBuilder().WithProtocolVersion(_protocolVersion)
.WithRequestId(2)
.WithPath(_path)
.WithEncoding(_encoding)
.Build()
.GetBytes();
_sftpStatVfsResponse = new SftpStatVfsResponseBuilder().WithProtocolVersion(_protocolVersion)
.WithResponseId(2U)
.WithBAvail(_bAvail)
.Build();
}
private void CreateMocks()
{
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
_sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
_channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
}
private void SetupMocks()
{
var sequence = new MockSequence();
#region SftpSession.Connect()
_ = _sessionMock.InSequence(sequence)
.Setup(p => p.CreateChannelSession())
.Returns(_channelSessionMock.Object);
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.Open());
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.SendSubsystemRequest("sftp"))
.Returns(true);
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.IsOpen)
.Returns(true);
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.SendData(_sftpInitRequestBytes, 0, _sftpInitRequestBytes.Length))
.Callback(() =>
{
_channelSessionMock.Raise(c => c.DataReceived += null,
new ChannelDataEventArgs(0, _sftpVersionResponse.GetBytes()));
});
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.IsOpen)
.Returns(true);
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.SendData(_sftpRealPathRequestBytes, 0, _sftpRealPathRequestBytes.Length))
.Callback(() =>
{
_channelSessionMock.Raise(c => c.DataReceived += null,
new ChannelDataEventArgs(0, _sftpNameResponse.GetBytes()));
});
#endregion SftpSession.Connect()
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.IsOpen)
.Returns(true);
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.SendData(_sftpStatVfsRequestBytes, 0, _sftpStatVfsRequestBytes.Length))
.Callback(() =>
{
_channelSessionMock.Raise(c => c.DataReceived += null,
new ChannelDataEventArgs(0, _sftpStatVfsResponse.GetBytes()));
});
}
protected void Arrange()
{
SetupData();
CreateMocks();
SetupMocks();
_sftpSession = new SftpSession(_sessionMock.Object, _operationTimeout, _encoding, _sftpResponseFactory);
_sftpSession.Connect();
}
protected void Act()
{
_actual = _sftpSession.RequestStatVfs(_path);
}
[TestMethod]
public void ReturnedValueShouldNotBeNull()
{
Assert.IsNotNull(_actual);
}
[TestMethod]
public void AvailableBlocksInReturnedValueShouldMatchValueInSftpResponse()
{
Assert.AreEqual(_bAvail, _actual.AvailableBlocks);
}
}
}