Skip to content

Commit 9444f21

Browse files
committed
Added more tests for SftpFileStream.SetLength(long).
1 parent 7bc67f3 commit 9444f21

9 files changed

+828
-4
lines changed

src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTestBase.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,28 @@ public void SetUp()
3838

3939
protected abstract void Act();
4040

41+
protected byte[] GenerateRandom(int length)
42+
{
43+
return GenerateRandom(length, new Random());
44+
}
45+
4146
protected byte[] GenerateRandom(int length, Random random)
4247
{
4348
var buffer = new byte[length];
4449
random.NextBytes(buffer);
4550
return buffer;
4651
}
52+
53+
protected byte[] GenerateRandom(uint length)
54+
{
55+
return GenerateRandom(length, new Random());
56+
}
57+
58+
protected byte[] GenerateRandom(uint length, Random random)
59+
{
60+
var buffer = new byte[length];
61+
random.NextBytes(buffer);
62+
return buffer;
63+
}
4764
}
4865
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
using System;
2+
using System.Globalization;
3+
using System.IO;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Moq;
6+
using Renci.SshNet.Sftp;
7+
using Renci.SshNet.Tests.Common;
8+
using System.Threading;
9+
using Renci.SshNet.Sftp.Responses;
10+
11+
namespace Renci.SshNet.Tests.Classes.Sftp
12+
{
13+
/// <summary>
14+
/// - In read mode
15+
/// - Bytes in (read) buffer
16+
/// - New length greater than client position and greater than server position
17+
/// </summary>
18+
[TestClass]
19+
public class SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition : SftpFileStreamTestBase
20+
{
21+
private string _path;
22+
private SftpFileStream _sftpFileStream;
23+
private byte[] _handle;
24+
private uint _bufferSize;
25+
private uint _readBufferSize;
26+
private uint _writeBufferSize;
27+
private MockSequence _sequence;
28+
private long _length;
29+
30+
private SftpFileAttributes _fileAttributes;
31+
private SftpFileAttributes _originalFileAttributes;
32+
private SftpFileAttributes _newFileAttributes;
33+
private byte[] _readBytes;
34+
private byte[] _actualReadBytes;
35+
36+
protected override void SetupData()
37+
{
38+
var random = new Random();
39+
40+
_path = random.Next().ToString(CultureInfo.InvariantCulture);
41+
_handle = GenerateRandom(random.Next(2, 6), random);
42+
_bufferSize = (uint) random.Next(1, 1000);
43+
_readBufferSize = (uint) random.Next(1, 1000);
44+
_writeBufferSize = (uint) random.Next(100, 1000);
45+
_readBytes = new byte[5];
46+
_actualReadBytes = GenerateRandom(_readBytes.Length, random);
47+
_length = _readBytes.Length + 2;
48+
49+
_fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC")
50+
.WithExtension("V", "VValue")
51+
.WithGroupId(random.Next())
52+
.WithLastAccessTime(DateTime.Now.AddSeconds(random.Next()))
53+
.WithLastWriteTime(DateTime.Now.AddSeconds(random.Next()))
54+
.WithPermissions((uint)random.Next())
55+
.WithSize(_length + 100)
56+
.WithUserId(random.Next())
57+
.Build();
58+
_originalFileAttributes = _fileAttributes.Clone();
59+
_newFileAttributes = null;
60+
}
61+
62+
protected override void SetupMocks()
63+
{
64+
_sequence = new MockSequence();
65+
SftpSessionMock.InSequence(_sequence)
66+
.Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false))
67+
.Returns(_handle);
68+
SftpSessionMock.InSequence(_sequence)
69+
.Setup(p => p.CalculateOptimalReadLength(_bufferSize))
70+
.Returns(_readBufferSize);
71+
SftpSessionMock.InSequence(_sequence)
72+
.Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
73+
.Returns(_writeBufferSize);
74+
SftpSessionMock.InSequence(_sequence)
75+
.Setup(p => p.IsOpen)
76+
.Returns(true);
77+
SftpSessionMock.InSequence(_sequence)
78+
.Setup(p => p.RequestRead(_handle, 0, _readBufferSize))
79+
.Returns(_actualReadBytes);
80+
SftpSessionMock.InSequence(_sequence)
81+
.Setup(p => p.IsOpen)
82+
.Returns(true);
83+
SftpSessionMock.InSequence(_sequence)
84+
.Setup(p => p.RequestFStat(_handle, false))
85+
.Returns(_fileAttributes);
86+
SftpSessionMock.InSequence(_sequence)
87+
.Setup(p => p.RequestFSetStat(_handle, _fileAttributes))
88+
.Callback<byte[], SftpFileAttributes>((bytes, attributes) => _newFileAttributes = attributes.Clone());
89+
}
90+
91+
protected override void Arrange()
92+
{
93+
base.Arrange();
94+
95+
_sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize);
96+
_sftpFileStream.Read(_readBytes, 0, _readBytes.Length);
97+
}
98+
99+
protected override void Act()
100+
{
101+
_sftpFileStream.SetLength(_length);
102+
}
103+
104+
[TestMethod]
105+
public void PositionShouldReturnSamePositionAsBeforeSetLength()
106+
{
107+
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
108+
109+
Assert.AreEqual(_readBytes.Length, _sftpFileStream.Position);
110+
111+
SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
112+
}
113+
114+
[TestMethod]
115+
public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce()
116+
{
117+
SftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once);
118+
}
119+
120+
[TestMethod]
121+
public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat()
122+
{
123+
DictionaryAssert.AreEqual(_originalFileAttributes.Extensions, _newFileAttributes.Extensions);
124+
Assert.AreEqual(_originalFileAttributes.GroupId, _newFileAttributes.GroupId);
125+
Assert.AreEqual(_originalFileAttributes.LastAccessTime, _newFileAttributes.LastAccessTime);
126+
Assert.AreEqual(_originalFileAttributes.LastWriteTime, _newFileAttributes.LastWriteTime);
127+
Assert.AreEqual(_originalFileAttributes.Permissions, _newFileAttributes.Permissions);
128+
Assert.AreEqual(_originalFileAttributes.UserId, _newFileAttributes.UserId);
129+
130+
Assert.AreEqual(_length, _newFileAttributes.Size);
131+
}
132+
133+
[TestMethod]
134+
public void ReadShouldReadStartFromSamePositionAsBeforeSetLength()
135+
{
136+
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
137+
SftpSessionMock.InSequence(_sequence)
138+
.Setup(p => p.RequestRead(_handle, (uint) _readBytes.Length, _readBufferSize))
139+
.Returns(new byte[] { 0x0f });
140+
141+
var byteRead = _sftpFileStream.ReadByte();
142+
143+
Assert.AreEqual(0x0f, byteRead);
144+
145+
SftpSessionMock.Verify(p => p.RequestRead(_handle, (uint) _readBytes.Length, _readBufferSize), Times.Once);
146+
SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
147+
}
148+
149+
[TestMethod]
150+
public void WriteShouldStartFromSamePositionAsBeforeSetLength()
151+
{
152+
var bytesToWrite = GenerateRandom(5);
153+
154+
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
155+
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
156+
SftpSessionMock.InSequence(_sequence)
157+
.Setup(p => p.RequestWrite(_handle, (uint) _readBytes.Length, It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null))
158+
.Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverOffset, data, offset, length, wait, writeCompleted) =>
159+
{
160+
wait.Set();
161+
});
162+
163+
_sftpFileStream.Write(bytesToWrite, 0, bytesToWrite.Length);
164+
_sftpFileStream.Flush();
165+
166+
SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint) _readBytes.Length, It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null), Times.Once);
167+
SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
168+
}
169+
}
170+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
using System;
2+
using System.Globalization;
3+
using System.IO;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Moq;
6+
using Renci.SshNet.Sftp;
7+
using Renci.SshNet.Tests.Common;
8+
using System.Threading;
9+
using Renci.SshNet.Sftp.Responses;
10+
using Renci.SshNet.Common;
11+
12+
namespace Renci.SshNet.Tests.Classes.Sftp
13+
{
14+
/// <summary>
15+
/// - In read mode
16+
/// - Bytes in (read) buffer
17+
/// - New length less than client position and greater than server position
18+
/// </summary>
19+
[TestClass]
20+
public class SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthLessThanPosition : SftpFileStreamTestBase
21+
{
22+
private string _path;
23+
private SftpFileStream _sftpFileStream;
24+
private byte[] _handle;
25+
private uint _bufferSize;
26+
private uint _readBufferSize;
27+
private uint _writeBufferSize;
28+
private MockSequence _sequence;
29+
private long _length;
30+
31+
private SftpFileAttributes _fileAttributes;
32+
private SftpFileAttributes _originalFileAttributes;
33+
private SftpFileAttributes _newFileAttributes;
34+
private byte[] _readBytes;
35+
private byte[] _actualReadBytes;
36+
37+
protected override void SetupData()
38+
{
39+
var random = new Random();
40+
41+
_path = random.Next().ToString(CultureInfo.InvariantCulture);
42+
_handle = GenerateRandom(random.Next(2, 6), random);
43+
_bufferSize = (uint)random.Next(1, 1000);
44+
_readBufferSize = (uint)random.Next(1, 1000);
45+
_writeBufferSize = (uint)random.Next(100, 1000);
46+
_readBytes = new byte[5];
47+
_actualReadBytes = GenerateRandom(_readBytes.Length, random);
48+
_length = _readBytes.Length - 2;
49+
50+
_fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC")
51+
.WithExtension("V", "VValue")
52+
.WithGroupId(random.Next())
53+
.WithLastAccessTime(DateTime.Now.AddSeconds(random.Next()))
54+
.WithLastWriteTime(DateTime.Now.AddSeconds(random.Next()))
55+
.WithPermissions((uint)random.Next())
56+
.WithSize(_length + 100)
57+
.WithUserId(random.Next())
58+
.Build();
59+
_originalFileAttributes = _fileAttributes.Clone();
60+
_newFileAttributes = null;
61+
}
62+
63+
protected override void SetupMocks()
64+
{
65+
_sequence = new MockSequence();
66+
SftpSessionMock.InSequence(_sequence)
67+
.Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false))
68+
.Returns(_handle);
69+
SftpSessionMock.InSequence(_sequence)
70+
.Setup(p => p.CalculateOptimalReadLength(_bufferSize))
71+
.Returns(_readBufferSize);
72+
SftpSessionMock.InSequence(_sequence)
73+
.Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
74+
.Returns(_writeBufferSize);
75+
SftpSessionMock.InSequence(_sequence)
76+
.Setup(p => p.IsOpen)
77+
.Returns(true);
78+
SftpSessionMock.InSequence(_sequence)
79+
.Setup(p => p.RequestRead(_handle, 0, _readBufferSize))
80+
.Returns(_actualReadBytes);
81+
SftpSessionMock.InSequence(_sequence)
82+
.Setup(p => p.IsOpen)
83+
.Returns(true);
84+
SftpSessionMock.InSequence(_sequence)
85+
.Setup(p => p.RequestFStat(_handle, false))
86+
.Returns(_fileAttributes);
87+
SftpSessionMock.InSequence(_sequence)
88+
.Setup(p => p.RequestFSetStat(_handle, _fileAttributes))
89+
.Callback<byte[], SftpFileAttributes>((bytes, attributes) => _newFileAttributes = attributes.Clone());
90+
}
91+
92+
protected override void Arrange()
93+
{
94+
base.Arrange();
95+
96+
_sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize);
97+
_sftpFileStream.Read(_readBytes, 0, _readBytes.Length);
98+
}
99+
100+
protected override void Act()
101+
{
102+
_sftpFileStream.SetLength(_length);
103+
}
104+
105+
[TestMethod]
106+
public void PositionShouldReturnLengthOfStream()
107+
{
108+
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
109+
110+
Assert.AreEqual(_length, _sftpFileStream.Position);
111+
112+
SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
113+
}
114+
115+
[TestMethod]
116+
public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce()
117+
{
118+
SftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once);
119+
}
120+
121+
[TestMethod]
122+
public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat()
123+
{
124+
DictionaryAssert.AreEqual(_originalFileAttributes.Extensions, _newFileAttributes.Extensions);
125+
Assert.AreEqual(_originalFileAttributes.GroupId, _newFileAttributes.GroupId);
126+
Assert.AreEqual(_originalFileAttributes.LastAccessTime, _newFileAttributes.LastAccessTime);
127+
Assert.AreEqual(_originalFileAttributes.LastWriteTime, _newFileAttributes.LastWriteTime);
128+
Assert.AreEqual(_originalFileAttributes.Permissions, _newFileAttributes.Permissions);
129+
Assert.AreEqual(_originalFileAttributes.UserId, _newFileAttributes.UserId);
130+
131+
Assert.AreEqual(_length, _newFileAttributes.Size);
132+
}
133+
134+
[TestMethod]
135+
public void ReadShouldStartFromEndOfStream()
136+
{
137+
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
138+
SftpSessionMock.InSequence(_sequence)
139+
.Setup(p => p.RequestRead(_handle, (uint) _length, _readBufferSize))
140+
.Returns(Array<byte>.Empty);
141+
142+
var byteRead = _sftpFileStream.ReadByte();
143+
144+
Assert.AreEqual(-1, byteRead);
145+
146+
SftpSessionMock.Verify(p => p.RequestRead(_handle, (uint) _length, _readBufferSize), Times.Once);
147+
SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
148+
}
149+
150+
[TestMethod]
151+
public void WriteShouldStartFromEndOfStream()
152+
{
153+
var bytesToWrite = GenerateRandom(5);
154+
155+
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
156+
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
157+
SftpSessionMock.InSequence(_sequence)
158+
.Setup(p => p.RequestWrite(_handle, (uint) _length, It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null))
159+
.Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverOffset, data, offset, length, wait, writeCompleted) =>
160+
{
161+
wait.Set();
162+
});
163+
164+
_sftpFileStream.Write(bytesToWrite, 0, bytesToWrite.Length);
165+
_sftpFileStream.Flush();
166+
167+
SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint)_length, It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null), Times.Once);
168+
SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
169+
}
170+
}
171+
}

0 commit comments

Comments
 (0)