-
-
Notifications
You must be signed in to change notification settings - Fork 969
Fix Seek Operations in SftpFileStream #910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bdcf48b
Update .gitignore
lemonyte a6b5e6e
Fix offset operations in SftpFileStream.Seek
lemonyte 161c87e
Fix seek exception message and add default case for invalid seek origin
lemonyte 731571c
Revert .gitignore changes
lemonyte fcf3c56
Use named params when throwing ArgumentException
lemonyte 0cf229a
Add tests for seeking from end of file
lemonyte 8d7e004
Fix seek tests
lemonyte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
.../Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetNegative.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| using System; | ||
| using System.IO; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Moq; | ||
| using Renci.SshNet.Sftp; | ||
|
|
||
| namespace Renci.SshNet.Tests.Classes.Sftp | ||
| { | ||
| [TestClass] | ||
| public class SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetNegative : SftpFileStreamTestBase | ||
| { | ||
| private Random _random; | ||
| private string _path; | ||
| private FileMode _fileMode; | ||
| private FileAccess _fileAccess; | ||
| private int _bufferSize; | ||
| private uint _readBufferSize; | ||
| private uint _writeBufferSize; | ||
| private byte[] _handle; | ||
| private SftpFileStream _target; | ||
| private int _offset; | ||
| private SftpFileAttributes _attributes; | ||
| private EndOfStreamException _actualException; | ||
|
|
||
| protected override void SetupData() | ||
| { | ||
| base.SetupData(); | ||
|
|
||
| _random = new Random(); | ||
| _path = _random.Next().ToString(); | ||
| _fileMode = FileMode.OpenOrCreate; | ||
| _fileAccess = FileAccess.Read; | ||
| _bufferSize = _random.Next(5, 1000); | ||
| _readBufferSize = (uint)_random.Next(5, 1000); | ||
| _writeBufferSize = (uint)_random.Next(5, 1000); | ||
| _handle = GenerateRandom(_random.Next(1, 10), _random); | ||
| _offset = _random.Next(int.MinValue, -1); | ||
| _attributes = SftpFileAttributes.Empty; | ||
| } | ||
|
|
||
| protected override void SetupMocks() | ||
| { | ||
| SftpSessionMock.InSequence(MockSequence) | ||
| .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.CreateNewOrOpen, false)) | ||
| .Returns(_handle); | ||
| SftpSessionMock.InSequence(MockSequence) | ||
| .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) | ||
| .Returns(_readBufferSize); | ||
| SftpSessionMock.InSequence(MockSequence) | ||
| .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) | ||
| .Returns(_writeBufferSize); | ||
| SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); | ||
| SftpSessionMock.InSequence(MockSequence) | ||
| .Setup(p => p.RequestFStat(_handle, false)) | ||
| .Returns(_attributes); | ||
| } | ||
|
|
||
| protected override void Arrange() | ||
| { | ||
| base.Arrange(); | ||
|
|
||
| _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); | ||
| } | ||
|
|
||
| protected override void Act() | ||
| { | ||
| try | ||
| { | ||
| _target.Seek(_offset, SeekOrigin.End); | ||
| Assert.Fail(); | ||
lemonyte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| catch (EndOfStreamException ex) | ||
| { | ||
| _actualException = ex; | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void SeekShouldHaveThrownEndOfStreamException() | ||
| { | ||
| Assert.IsNotNull(_actualException); | ||
| Assert.IsNull(_actualException.InnerException); | ||
| Assert.AreEqual("Attempted to read past the end of the stream.", _actualException.Message); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IsOpenOnSftpSessionShouldHaveBeenInvokedOnce() | ||
| { | ||
| SftpSessionMock.Verify(p => p.IsOpen, Times.Once); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void PositionShouldReturnZero() | ||
| { | ||
| SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); | ||
|
|
||
| Assert.AreEqual(0L, _target.Position); | ||
|
|
||
| SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); | ||
| } | ||
| } | ||
| } | ||
92 changes: 92 additions & 0 deletions
92
.../Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetPositive.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| using System; | ||
| using System.IO; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Moq; | ||
| using Renci.SshNet.Sftp; | ||
|
|
||
| namespace Renci.SshNet.Tests.Classes.Sftp | ||
| { | ||
| [TestClass] | ||
| public class SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetPositive : SftpFileStreamTestBase | ||
| { | ||
| private Random _random; | ||
| private string _path; | ||
| private FileMode _fileMode; | ||
| private FileAccess _fileAccess; | ||
| private int _bufferSize; | ||
| private uint _readBufferSize; | ||
| private uint _writeBufferSize; | ||
| private byte[] _handle; | ||
| private SftpFileStream _target; | ||
| private int _offset; | ||
| private SftpFileAttributes _attributes; | ||
| private long _actual; | ||
|
|
||
| protected override void SetupData() | ||
| { | ||
| base.SetupData(); | ||
|
|
||
| _random = new Random(); | ||
| _path = _random.Next().ToString(); | ||
| _fileMode = FileMode.OpenOrCreate; | ||
| _fileAccess = FileAccess.Read; | ||
| _bufferSize = _random.Next(5, 1000); | ||
| _readBufferSize = (uint)_random.Next(5, 1000); | ||
| _writeBufferSize = (uint)_random.Next(5, 1000); | ||
| _handle = GenerateRandom(_random.Next(1, 10), _random); | ||
| _offset = _random.Next(1, int.MaxValue); | ||
| _attributes = SftpFileAttributes.Empty; | ||
| } | ||
|
|
||
| protected override void SetupMocks() | ||
| { | ||
| SftpSessionMock.InSequence(MockSequence) | ||
| .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.CreateNewOrOpen, false)) | ||
| .Returns(_handle); | ||
| SftpSessionMock.InSequence(MockSequence) | ||
| .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) | ||
| .Returns(_readBufferSize); | ||
| SftpSessionMock.InSequence(MockSequence) | ||
| .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) | ||
| .Returns(_writeBufferSize); | ||
| SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); | ||
| SftpSessionMock.InSequence(MockSequence) | ||
| .Setup(p => p.RequestFStat(_handle, false)) | ||
| .Returns(_attributes); | ||
| } | ||
|
|
||
| protected override void Arrange() | ||
| { | ||
| base.Arrange(); | ||
|
|
||
| _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); | ||
| } | ||
|
|
||
| protected override void Act() | ||
| { | ||
| _actual = _target.Seek(_offset, SeekOrigin.End); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void SeekShouldHaveReturnedOffset() | ||
| { | ||
| Assert.AreEqual(_offset, _actual); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IsOpenOnSftpSessionShouldHaveBeenInvokedOnce() | ||
| { | ||
| SftpSessionMock.Verify(p => p.IsOpen, Times.Once); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void PositionShouldReturnOffset() | ||
| { | ||
| SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); | ||
|
|
||
| Assert.AreEqual(_offset, _target.Position); | ||
|
|
||
| SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); | ||
| } | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
...sses/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetZero.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| using System; | ||
| using System.IO; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Moq; | ||
| using Renci.SshNet.Sftp; | ||
|
|
||
| namespace Renci.SshNet.Tests.Classes.Sftp | ||
| { | ||
| [TestClass] | ||
| public class SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetZero : SftpFileStreamTestBase | ||
| { | ||
| private Random _random; | ||
| private string _path; | ||
| private FileMode _fileMode; | ||
| private FileAccess _fileAccess; | ||
| private int _bufferSize; | ||
| private uint _readBufferSize; | ||
| private uint _writeBufferSize; | ||
| private byte[] _handle; | ||
| private SftpFileStream _target; | ||
| private SftpFileAttributes _attributes; | ||
| private long _actual; | ||
|
|
||
| protected override void SetupData() | ||
| { | ||
| base.SetupData(); | ||
|
|
||
| _random = new Random(); | ||
| _path = _random.Next().ToString(); | ||
| _fileMode = FileMode.OpenOrCreate; | ||
| _fileAccess = FileAccess.Read; | ||
| _bufferSize = _random.Next(5, 1000); | ||
| _readBufferSize = (uint)_random.Next(5, 1000); | ||
| _writeBufferSize = (uint)_random.Next(5, 1000); | ||
| _handle = GenerateRandom(_random.Next(1, 10), _random); | ||
| _attributes = SftpFileAttributes.Empty; | ||
| } | ||
|
|
||
| protected override void SetupMocks() | ||
| { | ||
| SftpSessionMock.InSequence(MockSequence) | ||
| .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.CreateNewOrOpen, false)) | ||
| .Returns(_handle); | ||
| SftpSessionMock.InSequence(MockSequence) | ||
| .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) | ||
| .Returns(_readBufferSize); | ||
| SftpSessionMock.InSequence(MockSequence) | ||
| .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) | ||
| .Returns(_writeBufferSize); | ||
| SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); | ||
| SftpSessionMock.InSequence(MockSequence) | ||
| .Setup(p => p.RequestFStat(_handle, false)) | ||
| .Returns(_attributes); | ||
| } | ||
|
|
||
| protected override void Arrange() | ||
| { | ||
| base.Arrange(); | ||
|
|
||
| _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); | ||
| } | ||
|
|
||
| protected override void Act() | ||
| { | ||
| _actual = _target.Seek(0L, SeekOrigin.End); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void SeekShouldHaveReturnedZero() | ||
| { | ||
| Assert.AreEqual(0L, _actual); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IsOpenOnSftpSessionShouldHaveBeenInvokedOnce() | ||
| { | ||
| SftpSessionMock.Verify(p => p.IsOpen, Times.Once); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void PositionShouldReturnZero() | ||
| { | ||
| SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); | ||
|
|
||
| Assert.AreEqual(0L, _target.Position); | ||
|
|
||
| SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.