using System; using System.IO; using System.Threading; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using Renci.SshNet.Sftp; using Renci.SshNet.Tests.Common; namespace Renci.SshNet.Tests.Classes.Sftp { [TestClass] public class SftpFileStreamTest_Ctor_FileModeAppend_FileAccessWrite : 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 _fileAttributes; protected override void SetupData() { base.SetupData(); _random = new Random(); _path = _random.Next().ToString(); _fileMode = FileMode.Append; _fileAccess = FileAccess.Write; _bufferSize = _random.Next(5, 1000); _readBufferSize = (uint)_random.Next(5, 1000); _writeBufferSize = (uint)_random.Next(5, 1000); _handle = GenerateRandom(_random.Next(1, 10), _random); _fileAttributes = new SftpFileAttributesBuilder().WithLastAccessTime(DateTime.UtcNow.AddSeconds(_random.Next())) .WithLastWriteTime(DateTime.UtcNow.AddSeconds(_random.Next())) .WithSize(_random.Next()) .WithUserId(_random.Next()) .WithGroupId(_random.Next()) .WithPermissions((uint)_random.Next()) .Build(); } protected override void SetupMocks() { _ = SftpSessionMock.InSequence(MockSequence) .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Append | 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.RequestFStat(_handle, false)) .Returns(_fileAttributes); } protected override void Act() { _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); } [TestMethod] public void CanReadShouldReturnFalse() { Assert.IsFalse(_target.CanRead); } [TestMethod] public void CanSeekShouldReturnTrue() { Assert.IsTrue(_target.CanSeek); } [TestMethod] public void CanWriteShouldReturnTrue() { Assert.IsTrue(_target.CanWrite); } [TestMethod] public void CanTimeoutShouldReturnTrue() { Assert.IsTrue(_target.CanTimeout); } [TestMethod] public void PositionShouldReturnSizeOfFile() { _ = SftpSessionMock.InSequence(MockSequence) .Setup(p => p.IsOpen) .Returns(true); var actual = _target.Position; Assert.AreEqual(_fileAttributes.Size, actual); SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); } [TestMethod] public void ReadShouldThrowNotSupportedException() { var buffer = new byte[_readBufferSize]; _ = SftpSessionMock.InSequence(MockSequence) .Setup(p => p.IsOpen) .Returns(true); try { _ = _target.Read(buffer, 0, buffer.Length); Assert.Fail(); } catch (NotSupportedException ex) { Assert.IsNull(ex.InnerException); Assert.AreEqual("Read not supported.", ex.Message); } SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); } [TestMethod] public void ReadByteShouldThrowNotSupportedException() { _ = SftpSessionMock.InSequence(MockSequence) .Setup(p => p.IsOpen) .Returns(true); try { _ = _target.ReadByte(); Assert.Fail(); } catch (NotSupportedException ex) { Assert.IsNull(ex.InnerException); Assert.AreEqual("Read not supported.", ex.Message); } SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); } [TestMethod] public void WriteShouldStartWritingAtEndOfFile() { var buffer = new byte[_writeBufferSize]; _ = SftpSessionMock.InSequence(MockSequence) .Setup(p => p.IsOpen) .Returns(true); _ = SftpSessionMock.InSequence(MockSequence) .Setup(p => p.RequestWrite(_handle, (ulong)_fileAttributes.Size, buffer, 0, buffer.Length, It.IsNotNull(), null)); _target.Write(buffer, 0, buffer.Length); SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); SftpSessionMock.Verify(p => p.RequestWrite(_handle, (ulong)_fileAttributes.Size, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); } [TestMethod] public void RequestOpenOnSftpSessionShouldBeInvokedOnce() { SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.Append | Flags.CreateNewOrOpen, false), Times.Once); } [TestMethod] public void RequestFStatOnSftpSessionShouldBeInvokedOnce() { SftpSessionMock.Verify(p => p.RequestFStat(_handle, false), Times.Once); } } }