Skip to content

Commit ad56cc9

Browse files
committed
Take into account the offset in SftpFileStream.Write(byte[] buffer, int offset, int count) when not writing to the buffer. Fixes issue #70.
1 parent fb9fe50 commit ad56cc9

File tree

8 files changed

+271
-40
lines changed

8 files changed

+271
-40
lines changed

src/Renci.SshNet.Tests/Classes/Sftp/Requests/SftpWriteRequestTest.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public class SftpWriteRequestTest
1515
private uint _protocolVersion;
1616
private uint _requestId;
1717
private byte[] _handle;
18-
private ulong _offset;
18+
private ulong _serverFileOffset;
1919
private byte[] _data;
20+
private int _offset;
2021
private int _length;
2122

2223
[TestInitialize]
@@ -28,22 +29,25 @@ public void Init()
2829
_requestId = (uint)random.Next(0, int.MaxValue);
2930
_handle = new byte[random.Next(1, 10)];
3031
random.NextBytes(_handle);
31-
_offset = (ulong) random.Next(0, int.MaxValue);
32-
_data = new byte[random.Next(5, 10)];
32+
_serverFileOffset = (ulong) random.Next(0, int.MaxValue);
33+
_data = new byte[random.Next(10, 15)];
3334
random.NextBytes(_data);
34-
_length = random.Next(1, 4);
35+
_offset = random.Next(0, 4);
36+
_length = random.Next(5, 10);
3537
}
3638

3739
[TestMethod]
3840
public void Constructor()
3941
{
40-
var request = new SftpWriteRequest(_protocolVersion, _requestId, _handle, _offset, _data, _length, null);
42+
var request = new SftpWriteRequest(_protocolVersion, _requestId, _handle, _serverFileOffset, _data, _offset, _length, null);
4143

4244
Assert.AreSame(_data, request.Data);
4345
Assert.AreSame(_handle, request.Handle);
4446
Assert.AreEqual(_length, request.Length);
47+
Assert.AreEqual(_offset, request.Offset);
4548
Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
4649
Assert.AreEqual(_requestId, request.RequestId);
50+
Assert.AreEqual(_serverFileOffset, request.ServerFileOffset);
4751
Assert.AreEqual(SftpMessageTypes.Write, request.SftpMessageType);
4852
}
4953

@@ -58,8 +62,9 @@ public void Complete_SftpStatusResponse()
5862
_protocolVersion,
5963
_requestId,
6064
_handle,
61-
_offset,
65+
_serverFileOffset,
6266
_data,
67+
_offset,
6368
_length,
6469
statusAction);
6570

@@ -72,7 +77,7 @@ public void Complete_SftpStatusResponse()
7277
[TestMethod]
7378
public void GetBytes()
7479
{
75-
var request = new SftpWriteRequest(_protocolVersion, _requestId, _handle, _offset, _data, _length, null);
80+
var request = new SftpWriteRequest(_protocolVersion, _requestId, _handle, _serverFileOffset, _data, _offset, _length, null);
7681

7782
var bytes = request.GetBytes();
7883

@@ -82,7 +87,7 @@ public void GetBytes()
8287
expectedBytesLength += 4; // RequestId
8388
expectedBytesLength += 4; // Handle length
8489
expectedBytesLength += _handle.Length; // Handle
85-
expectedBytesLength += 8; // Offset
90+
expectedBytesLength += 8; // ServerFileOffset
8691
expectedBytesLength += 4; // Data length
8792
expectedBytesLength += _length; // Data
8893

@@ -99,12 +104,12 @@ public void GetBytes()
99104
sshDataStream.Read(actualHandle, 0, actualHandle.Length);
100105
Assert.IsTrue(_handle.SequenceEqual(actualHandle));
101106

102-
Assert.AreEqual(_offset, sshDataStream.ReadUInt64());
107+
Assert.AreEqual(_serverFileOffset, sshDataStream.ReadUInt64());
103108

104109
Assert.AreEqual((uint) _length, sshDataStream.ReadUInt32());
105110
var actualData = new byte[_length];
106111
sshDataStream.Read(actualData, 0, actualData.Length);
107-
Assert.IsTrue(_data.Take(_length).SequenceEqual(actualData));
112+
Assert.IsTrue(_data.Take(_offset, _length).SequenceEqual(actualData));
108113

109114
Assert.IsTrue(sshDataStream.IsEndOfData);
110115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using System;
2+
using System.Globalization;
3+
using System.IO;
4+
using System.Threading;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using Moq;
7+
using Renci.SshNet.Sftp;
8+
using Renci.SshNet.Sftp.Responses;
9+
10+
namespace Renci.SshNet.Tests.Classes.Sftp
11+
{
12+
[TestClass]
13+
public class SftpFileStreamTest_Write_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize
14+
{
15+
private Mock<ISftpSession> _sftpSessionMock;
16+
private string _path;
17+
private SftpFileStream _sftpFileStream;
18+
private byte[] _handle;
19+
private SftpFileAttributes _fileAttributes;
20+
private uint _bufferSize;
21+
private uint _readBufferSize;
22+
private uint _writeBufferSize;
23+
private byte[] _data;
24+
private int _count;
25+
private int _offset;
26+
private MockSequence _sequence;
27+
private Random _random;
28+
private uint _expectedWrittenByteCount;
29+
private int _expectedBufferedByteCount;
30+
private byte[] _expectedBufferedBytes;
31+
32+
[TestInitialize]
33+
public void Setup()
34+
{
35+
Arrange();
36+
Act();
37+
}
38+
39+
[TestCleanup]
40+
public void TearDown()
41+
{
42+
if (_sftpSessionMock != null)
43+
{
44+
// allow Dispose to complete successfully
45+
_sftpSessionMock.InSequence(_sequence)
46+
.Setup(p => p.IsOpen)
47+
.Returns(true);
48+
_sftpSessionMock.InSequence(_sequence)
49+
.Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null));
50+
_sftpSessionMock.InSequence(_sequence)
51+
.Setup(p => p.RequestClose(_handle));
52+
}
53+
}
54+
55+
protected void Arrange()
56+
{
57+
_random = new Random();
58+
_path = _random.Next().ToString(CultureInfo.InvariantCulture);
59+
_handle = new[] {(byte) _random.Next(byte.MinValue, byte.MaxValue)};
60+
_fileAttributes = SftpFileAttributes.Empty;
61+
_bufferSize = (uint) _random.Next(1, 1000);
62+
_readBufferSize = (uint) _random.Next(0, 1000);
63+
_writeBufferSize = (uint) _random.Next(500, 1000);
64+
_data = new byte[(_writeBufferSize * 2) + 15];
65+
_random.NextBytes(_data);
66+
_offset = _random.Next(1, 5);
67+
// to get multiple SSH_FXP_WRITE messages (and verify the offset is updated correctly), we make sure
68+
// the number of bytes to write is at least two times the write buffer size; we write a few extra bytes to
69+
// ensure the buffer is not empty after the writes so we can verify whether Length, Dispose and Flush
70+
// flush the buffer
71+
_count = ((int) _writeBufferSize*2) + _random.Next(1, 5);
72+
73+
_expectedWrittenByteCount = (2 * _writeBufferSize);
74+
_expectedBufferedByteCount = (int)(_count - _expectedWrittenByteCount);
75+
_expectedBufferedBytes = _data.Take(_offset + (int)_expectedWrittenByteCount, _expectedBufferedByteCount);
76+
77+
_sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
78+
79+
_sequence = new MockSequence();
80+
_sftpSessionMock.InSequence(_sequence)
81+
.Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true))
82+
.Returns(_handle);
83+
_sftpSessionMock.InSequence(_sequence).Setup(p => p.RequestFStat(_handle)).Returns(_fileAttributes);
84+
_sftpSessionMock.InSequence(_sequence)
85+
.Setup(p => p.CalculateOptimalReadLength(_bufferSize))
86+
.Returns(_readBufferSize);
87+
_sftpSessionMock.InSequence(_sequence)
88+
.Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
89+
.Returns(_writeBufferSize);
90+
_sftpSessionMock.InSequence(_sequence)
91+
.Setup(p => p.IsOpen)
92+
.Returns(true);
93+
_sftpSessionMock.InSequence(_sequence)
94+
.Setup(p => p.RequestWrite(_handle, 0, _data, _offset, (int) _writeBufferSize, It.IsAny<AutoResetEvent>(), null));
95+
_sftpSessionMock.InSequence(_sequence)
96+
.Setup(p => p.RequestWrite(_handle, _writeBufferSize, _data, _offset + (int) _writeBufferSize, (int)_writeBufferSize, It.IsAny<AutoResetEvent>(), null));
97+
98+
_sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int) _bufferSize);
99+
}
100+
101+
protected void Act()
102+
{
103+
_sftpFileStream.Write(_data, _offset, _count);
104+
}
105+
106+
[TestMethod]
107+
public void RequestWriteOnSftpSessionShouldBeInvokedTwice()
108+
{
109+
_sftpSessionMock.Verify(p => p.RequestWrite(_handle, 0, _data, _offset, (int)_writeBufferSize, It.IsAny<AutoResetEvent>(), null), Times.Once);
110+
_sftpSessionMock.Verify(p => p.RequestWrite(_handle, _writeBufferSize, _data, _offset + (int)_writeBufferSize, (int)_writeBufferSize, It.IsAny<AutoResetEvent>(), null), Times.Once);
111+
}
112+
113+
[TestMethod]
114+
public void PositionShouldBeNumberOfBytesWrittenToFileAndNUmberOfBytesInBuffer()
115+
{
116+
_sftpSessionMock.InSequence(_sequence)
117+
.Setup(p => p.IsOpen)
118+
.Returns(true);
119+
120+
Assert.AreEqual(_count, _sftpFileStream.Position);
121+
}
122+
123+
[TestMethod]
124+
public void LengthShouldFlushBufferAndReturnSizeOfFile()
125+
{
126+
var lengthFileAttributes = new SftpFileAttributes(DateTime.Now, DateTime.Now, _random.Next(), _random.Next(),
127+
_random.Next(), (uint) _random.Next(0, int.MaxValue), null);
128+
byte[] actualFlushedData = null;
129+
130+
_sftpSessionMock.InSequence(_sequence)
131+
.Setup(p => p.IsOpen)
132+
.Returns(true);
133+
_sftpSessionMock.InSequence(_sequence)
134+
.Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null))
135+
.Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length));
136+
_sftpSessionMock.InSequence(_sequence)
137+
.Setup(p => p.RequestFStat(_handle))
138+
.Returns(lengthFileAttributes);
139+
140+
Assert.AreEqual(lengthFileAttributes.Size, _sftpFileStream.Length);
141+
Assert.IsTrue(actualFlushedData.IsEqualTo(_expectedBufferedBytes));
142+
143+
_sftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null), Times.Once);
144+
}
145+
146+
[TestMethod]
147+
public void DisposeShouldFlushBufferAndCloseRequest()
148+
{
149+
byte[] actualFlushedData = null;
150+
151+
_sftpSessionMock.InSequence(_sequence)
152+
.Setup(p => p.IsOpen)
153+
.Returns(true);
154+
_sftpSessionMock.InSequence(_sequence)
155+
.Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null))
156+
.Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length));
157+
_sftpSessionMock.InSequence(_sequence)
158+
.Setup(p => p.RequestClose(_handle));
159+
160+
_sftpFileStream.Dispose();
161+
162+
Assert.IsTrue(actualFlushedData.IsEqualTo(_expectedBufferedBytes));
163+
164+
_sftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null), Times.Once);
165+
_sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
166+
}
167+
168+
[TestMethod]
169+
public void FlushShouldFlushBuffer()
170+
{
171+
byte[] actualFlushedData = null;
172+
173+
_sftpSessionMock.InSequence(_sequence)
174+
.Setup(p => p.IsOpen)
175+
.Returns(true);
176+
_sftpSessionMock.InSequence(_sequence)
177+
.Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null))
178+
.Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length));
179+
180+
_sftpFileStream.Flush();
181+
182+
Assert.IsTrue(actualFlushedData.IsEqualTo(_expectedBufferedBytes));
183+
184+
_sftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null), Times.Once);
185+
}
186+
}
187+
}

src/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@
406406
<Compile Include="Classes\Sftp\SftpFileStreamTest_SetLength_SessionOpen_FIleAccessRead.cs" />
407407
<Compile Include="Classes\Sftp\SftpFileStreamTest_SetLength_SessionOpen_FIleAccessReadWrite.cs" />
408408
<Compile Include="Classes\Sftp\SftpFileStreamTest_SetLength_SessionOpen_FIleAccessWrite.cs" />
409+
<Compile Include="Classes\Sftp\SftpFileStreamTest_Write_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize.cs" />
409410
<Compile Include="Classes\Sftp\SftpFileTest.cs" />
410411
<Compile Include="Classes\Sftp\SftpSessionTest_Connected_RequestRead.cs" />
411412
<Compile Include="Classes\Sftp\SftpSessionTest_Connected_RequestStatVfs.cs" />

src/Renci.SshNet/Sftp/ISftpSession.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,16 @@ internal interface ISftpSession : ISubsystemSession
154154
/// Performs SSH_FXP_WRITE request.
155155
/// </summary>
156156
/// <param name="handle">The handle.</param>
157-
/// <param name="offset">The offset.</param>
158-
/// <param name="data">The data to send.</param>
159-
/// <param name="length">The number of bytes of <paramref name="data"/> to send.</param>
157+
/// <param name="serverOffset">The the zero-based offset (in bytes) relative to the beginning of the file that the write must start at.</param>
158+
/// <param name="data">The buffer holding the data to write.</param>
159+
/// <param name="offset">the zero-based offset in <paramref name="data" /> at which to begin taking bytes to write.</param>
160+
/// <param name="length">The length (in bytes) of the data to write.</param>
160161
/// <param name="wait">The wait event handle if needed.</param>
161162
/// <param name="writeCompleted">The callback to invoke when the write has completed.</param>
162163
void RequestWrite(byte[] handle,
163-
ulong offset,
164+
ulong serverOffset,
164165
byte[] data,
166+
int offset,
165167
int length,
166168
AutoResetEvent wait,
167169
Action<SftpStatusResponse> writeCompleted = null);

src/Renci.SshNet/Sftp/Requests/SftpWriteRequest.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,39 @@ public override SftpMessageTypes SftpMessageType
1212

1313
public byte[] Handle { get; private set; }
1414

15-
public ulong Offset { get; private set; }
15+
/// <summary>
16+
/// Gets the zero-based offset (in bytes) relative to the beginning of the file that the write
17+
/// must start at.
18+
/// </summary>
19+
/// <value>
20+
/// The zero-based offset (in bytes) relative to the beginning of the file that the write must
21+
/// start at.
22+
/// </value>
23+
public ulong ServerFileOffset { get; private set; }
1624

25+
/// <summary>
26+
/// Gets the buffer holding the data to write.
27+
/// </summary>
28+
/// <value>
29+
/// The buffer holding the data to write.
30+
/// </value>
1731
public byte[] Data { get; private set; }
1832

33+
/// <summary>
34+
/// Gets the zero-based offset in <see cref="Data" /> at which to begin taking bytes to
35+
/// write.
36+
/// </summary>
37+
/// <value>
38+
/// The zero-based offset in <see cref="Data" /> at which to begin taking bytes to write.
39+
/// </value>
40+
public int Offset { get; private set; }
41+
42+
/// <summary>
43+
/// Gets the length (in bytes) of the data to write.
44+
/// </summary>
45+
/// <value>
46+
/// The length (in bytes) of the data to write.
47+
/// </value>
1948
public int Length { get; private set; }
2049

2150
protected override int BufferCapacity
@@ -25,7 +54,7 @@ protected override int BufferCapacity
2554
var capacity = base.BufferCapacity;
2655
capacity += 4; // Handle length
2756
capacity += Handle.Length; // Handle
28-
capacity += 8; // Offset length
57+
capacity += 8; // ServerFileOffset length
2958
capacity += 4; // Data length
3059
capacity += Length; // Data
3160
return capacity;
@@ -35,33 +64,36 @@ protected override int BufferCapacity
3564
public SftpWriteRequest(uint protocolVersion,
3665
uint requestId,
3766
byte[] handle,
38-
ulong offset,
67+
ulong serverFileOffset,
3968
byte[] data,
69+
int offset,
4070
int length,
4171
Action<SftpStatusResponse> statusAction)
4272
: base(protocolVersion, requestId, statusAction)
4373
{
4474
Handle = handle;
45-
Offset = offset;
75+
ServerFileOffset = serverFileOffset;
4676
Data = data;
77+
Offset = offset;
4778
Length = length;
4879
}
4980

5081
protected override void LoadData()
5182
{
5283
base.LoadData();
5384
Handle = ReadBinary();
54-
Offset = ReadUInt64();
85+
ServerFileOffset = ReadUInt64();
5586
Data = ReadBinary();
87+
Offset = 0;
5688
Length = Data.Length;
5789
}
5890

5991
protected override void SaveData()
6092
{
6193
base.SaveData();
6294
WriteBinaryString(Handle);
63-
Write(Offset);
64-
WriteBinary(Data, 0, Length);
95+
Write(ServerFileOffset);
96+
WriteBinary(Data, Offset, Length);
6597
}
6698
}
6799
}

0 commit comments

Comments
 (0)