|
| 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 | +} |
0 commit comments