diff --git a/.editorconfig b/.editorconfig index 8bbdc9ca2..788ade7af 100644 --- a/.editorconfig +++ b/.editorconfig @@ -719,7 +719,7 @@ dotnet_code_quality.CA1828.api_surface = all dotnet_diagnostic.CA1852.severity = none # CA1848: don't enforce LoggerMessage pattern -dotnet_diagnostic.CA1848.severity = suggestion +dotnet_diagnostic.CA1848.severity = silent # CA1859: Change return type for improved performance # diff --git a/src/Renci.SshNet/Common/Extensions.cs b/src/Renci.SshNet/Common/Extensions.cs index 14b80ac74..c334d19a1 100644 --- a/src/Renci.SshNet/Common/Extensions.cs +++ b/src/Renci.SshNet/Common/Extensions.cs @@ -1,12 +1,10 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Globalization; using System.Net; using System.Net.Sockets; using System.Numerics; using System.Runtime.CompilerServices; -using System.Text; using System.Threading; using Renci.SshNet.Abstractions; @@ -153,22 +151,6 @@ public static void SetIgnoringObjectDisposed(this EventWaitHandle waitHandle) } } - /// - /// Prints out the specified bytes. - /// - /// The bytes. - internal static void DebugPrint(this IEnumerable bytes) - { - var sb = new StringBuilder(); - - foreach (var b in bytes) - { - _ = sb.AppendFormat(CultureInfo.CurrentCulture, "0x{0:x2}, ", b); - } - - Debug.WriteLine(sb.ToString()); - } - internal static void ValidatePort(this uint value, [CallerArgumentExpression(nameof(value))] string argument = null) { if (value > IPEndPoint.MaxPort) diff --git a/src/Renci.SshNet/ISubsystemSession.cs b/src/Renci.SshNet/ISubsystemSession.cs index c00b5ac7b..f15de0c4f 100644 --- a/src/Renci.SshNet/ISubsystemSession.cs +++ b/src/Renci.SshNet/ISubsystemSession.cs @@ -84,57 +84,11 @@ internal interface ISubsystemSession : IDisposable /// A representing the wait. Task WaitOnHandleAsync(TaskCompletionSource tcs, int millisecondsTimeout, CancellationToken cancellationToken); - /// - /// Blocks the current thread until the specified gets signaled, using a - /// 32-bit signed integer to specify the time interval in milliseconds. - /// - /// The handle to wait for. - /// To number of milliseconds to wait for to get signaled, or -1 to wait indefinitely. - /// - /// if received a signal within the specified timeout; - /// otherwise, . - /// - /// The connection was closed by the server. - /// The channel was closed. - /// - /// The blocking wait is also interrupted when either the established channel is closed, the current - /// session is disconnected or an unexpected occurred while processing a channel - /// or session event. - /// - bool WaitOne(WaitHandle waitHandle, int millisecondsTimeout); - - /// - /// Blocks the current thread until the specified gets signaled, using a - /// 32-bit signed integer to specify the time interval in milliseconds. - /// - /// The first handle to wait for. - /// The second handle to wait for. - /// To number of milliseconds to wait for a to get signaled, or -1 to wait indefinitely. - /// - /// 0 if received a signal within the specified timeout and 1 - /// if received a signal within the specified timeout, or - /// if no object satisfied the wait. - /// - /// The connection was closed by the server. - /// The channel was closed. - /// - /// - /// The blocking wait is also interrupted when either the established channel is closed, the current - /// session is disconnected or an unexpected occurred while processing a channel - /// or session event. - /// - /// - /// When both and are signaled during the call, - /// then 0 is returned. - /// - /// - int WaitAny(WaitHandle waitHandleA, WaitHandle waitHandleB, int millisecondsTimeout); - /// /// Waits for any of the elements in the specified array to receive a signal, using a 32-bit signed /// integer to specify the time interval. /// - /// A array - constructed using - containing the objects to wait for. + /// A array - constructed using - containing the objects to wait for. /// To number of milliseconds to wait for a to get signaled, or -1 to wait indefinitely. /// /// The array index of the first non-system object that satisfied the wait. @@ -147,16 +101,6 @@ internal interface ISubsystemSession : IDisposable /// int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout); - /// - /// Creates a array that is composed of system objects and the specified - /// elements. - /// - /// A array containing the objects to wait for. - /// - /// A array that is composed of system objects and the specified elements. - /// - WaitHandle[] CreateWaitHandleArray(params WaitHandle[] waitHandles); - /// /// Creates a array that is composed of system objects and the specified /// elements. diff --git a/src/Renci.SshNet/Sftp/ISftpSession.cs b/src/Renci.SshNet/Sftp/ISftpSession.cs index 07cf1b8b9..b9baf43a5 100644 --- a/src/Renci.SshNet/Sftp/ISftpSession.cs +++ b/src/Renci.SshNet/Sftp/ISftpSession.cs @@ -40,7 +40,7 @@ internal interface ISftpSession : ISubsystemSession /// The new working directory. /// The token to monitor for cancellation requests. /// A that tracks the asynchronous change working directory request. - Task ChangeDirectoryAsync(string path, CancellationToken cancellationToken = default); + Task ChangeDirectoryAsync(string path, CancellationToken cancellationToken); /// /// Resolves a given path into an absolute path on the server. @@ -168,7 +168,7 @@ internal interface ISftpSession : ISubsystemSession /// The path. /// The to observe. /// A that represents the asynchronous SSH_FXP_MKDIR operation. - Task RequestMkDirAsync(string path, CancellationToken cancellationToken = default); + Task RequestMkDirAsync(string path, CancellationToken cancellationToken); /// /// Performs a SSH_FXP_OPEN request. @@ -389,7 +389,7 @@ internal interface ISftpSession : ISubsystemSession /// /// A task that represents the asynchronous SSH_FXP_RMDIR request. /// - Task RequestRmDirAsync(string path, CancellationToken cancellationToken = default); + Task RequestRmDirAsync(string path, CancellationToken cancellationToken); /// /// Performs SSH_FXP_SETSTAT request. diff --git a/src/Renci.SshNet/Sftp/SftpFileStream.cs b/src/Renci.SshNet/Sftp/SftpFileStream.cs index 0b86595e2..c5e486a9a 100644 --- a/src/Renci.SshNet/Sftp/SftpFileStream.cs +++ b/src/Renci.SshNet/Sftp/SftpFileStream.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; @@ -191,7 +192,7 @@ public TimeSpan Timeout } } - private SftpFileStream(ISftpSession session, string path, FileAccess access, int bufferSize, byte[] handle, long position) + private SftpFileStream(ISftpSession session, string path, FileAccess access, int readBufferSize, int writeBufferSize, byte[] handle, long position) { Timeout = TimeSpan.FromSeconds(30); Name = path; @@ -202,25 +203,24 @@ private SftpFileStream(ISftpSession session, string path, FileAccess access, int _canWrite = (access & FileAccess.Write) == FileAccess.Write; _handle = handle; + _readBufferSize = readBufferSize; + _writeBufferSize = writeBufferSize; + _position = position; + } - /* - * Instead of using the specified buffer size as is, we use it to calculate a buffer size - * that ensures we always receive or send the max. number of bytes in a single SSH_FXP_READ - * or SSH_FXP_WRITE message. - */ - - _readBufferSize = (int)session.CalculateOptimalReadLength((uint)bufferSize); - _writeBufferSize = (int)session.CalculateOptimalWriteLength((uint)bufferSize, _handle); + internal static SftpFileStream Open(ISftpSession session, string path, FileMode mode, FileAccess access, int bufferSize) + { + return Open(session, path, mode, access, bufferSize, isAsync: false, CancellationToken.None).GetAwaiter().GetResult(); + } - _position = position; + internal static Task OpenAsync(ISftpSession session, string path, FileMode mode, FileAccess access, int bufferSize, CancellationToken cancellationToken) + { + return Open(session, path, mode, access, bufferSize, isAsync: true, cancellationToken); } - internal SftpFileStream(ISftpSession session, string path, FileMode mode, FileAccess access, int bufferSize) + private static async Task Open(ISftpSession session, string path, FileMode mode, FileAccess access, int bufferSize, bool isAsync, CancellationToken cancellationToken) { - if (session is null) - { - throw new SshConnectionException("Client not connected."); - } + Debug.Assert(isAsync || cancellationToken == default); ThrowHelper.ThrowIfNull(path); @@ -229,14 +229,10 @@ internal SftpFileStream(ISftpSession session, string path, FileMode mode, FileAc throw new ArgumentOutOfRangeException(nameof(bufferSize), "Cannot be less than or equal to zero."); } - Timeout = TimeSpan.FromSeconds(30); - Name = path; - - // Initialize the object state. - _session = session; - _canRead = (access & FileAccess.Read) == FileAccess.Read; - _canSeek = true; - _canWrite = (access & FileAccess.Write) == FileAccess.Write; + if (session is null) + { + throw new SshConnectionException("Client not connected."); + } var flags = Flags.None; @@ -284,16 +280,7 @@ internal SftpFileStream(ISftpSession session, string path, FileMode mode, FileAc flags |= Flags.Append | Flags.CreateNewOrOpen; break; case FileMode.Create: - _handle = _session.RequestOpen(path, flags | Flags.Truncate, nullOnError: true); - if (_handle is null) - { - flags |= Flags.CreateNew; - } - else - { - flags |= Flags.Truncate; - } - + flags |= Flags.CreateNewOrOpen | Flags.Truncate; break; case FileMode.CreateNew: flags |= Flags.CreateNew; @@ -310,7 +297,16 @@ internal SftpFileStream(ISftpSession session, string path, FileMode mode, FileAc throw new ArgumentOutOfRangeException(nameof(mode)); } - _handle ??= _session.RequestOpen(path, flags); + byte[] handle; + + if (isAsync) + { + handle = await session.RequestOpenAsync(path, flags, cancellationToken).ConfigureAwait(false); + } + else + { + handle = session.RequestOpen(path, flags); + } /* * Instead of using the specified buffer size as is, we use it to calculate a buffer size @@ -318,119 +314,27 @@ internal SftpFileStream(ISftpSession session, string path, FileMode mode, FileAc * or SSH_FXP_WRITE message. */ - _readBufferSize = (int)session.CalculateOptimalReadLength((uint)bufferSize); - _writeBufferSize = (int)session.CalculateOptimalWriteLength((uint)bufferSize, _handle); + var readBufferSize = (int)session.CalculateOptimalReadLength((uint)bufferSize); + var writeBufferSize = (int)session.CalculateOptimalWriteLength((uint)bufferSize, handle); + long position = 0; if (mode == FileMode.Append) { - var attributes = _session.RequestFStat(_handle, nullOnError: false); - _position = attributes.Size; - } - } - - internal static async Task OpenAsync(ISftpSession session, string path, FileMode mode, FileAccess access, int bufferSize, CancellationToken cancellationToken) - { - if (session is null) - { - throw new SshConnectionException("Client not connected."); - } - - ThrowHelper.ThrowIfNull(path); - - if (bufferSize <= 0) - { - throw new ArgumentOutOfRangeException(nameof(bufferSize), "Cannot be less than or equal to zero."); - } - - var flags = Flags.None; - - switch (access) - { - case FileAccess.Read: - flags |= Flags.Read; - break; - case FileAccess.Write: - flags |= Flags.Write; - break; - case FileAccess.ReadWrite: - flags |= Flags.Read; - flags |= Flags.Write; - break; - default: - throw new ArgumentOutOfRangeException(nameof(access)); - } - - if ((access & FileAccess.Read) == FileAccess.Read && mode == FileMode.Append) - { - throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, - "{0} mode can be requested only when combined with write-only access.", - mode.ToString("G")), - nameof(mode)); - } + SftpFileAttributes attributes; - if ((access & FileAccess.Write) != FileAccess.Write) - { - if (mode is FileMode.Create or FileMode.CreateNew or FileMode.Truncate or FileMode.Append) + if (isAsync) { - throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, - "Combining {0}: {1} with {2}: {3} is invalid.", - nameof(FileMode), - mode, - nameof(FileAccess), - access), - nameof(mode)); + attributes = await session.RequestFStatAsync(handle, cancellationToken).ConfigureAwait(false); } - } - - switch (mode) - { - case FileMode.Append: - flags |= Flags.Append | Flags.CreateNewOrOpen; - break; - case FileMode.Create: - flags |= Flags.CreateNewOrOpen | Flags.Truncate; - break; - case FileMode.CreateNew: - flags |= Flags.CreateNew; - break; - case FileMode.Open: - break; - case FileMode.OpenOrCreate: - flags |= Flags.CreateNewOrOpen; - break; - case FileMode.Truncate: - flags |= Flags.Truncate; - break; - default: - throw new ArgumentOutOfRangeException(nameof(mode)); - } - - var handle = await session.RequestOpenAsync(path, flags, cancellationToken).ConfigureAwait(false); - - long position = 0; - if (mode == FileMode.Append) - { - try + else { - var attributes = await session.RequestFStatAsync(handle, cancellationToken).ConfigureAwait(false); - position = attributes.Size; + attributes = session.RequestFStat(handle, nullOnError: false); } - catch - { - try - { - await session.RequestCloseAsync(handle, cancellationToken).ConfigureAwait(false); - } - catch - { - // The original exception is presumably more informative, so we just ignore this one. - } - throw; - } + position = attributes.Size; } - return new SftpFileStream(session, path, access, bufferSize, handle, position); + return new SftpFileStream(session, path, access, readBufferSize, writeBufferSize, handle, position); } /// diff --git a/src/Renci.SshNet/Sftp/SftpSession.cs b/src/Renci.SshNet/Sftp/SftpSession.cs index b63ed01dd..1de63eaf2 100644 --- a/src/Renci.SshNet/Sftp/SftpSession.cs +++ b/src/Renci.SshNet/Sftp/SftpSession.cs @@ -90,7 +90,7 @@ public void ChangeDirectory(string path) /// The new working directory. /// The token to monitor for cancellation requests. /// A that tracks the asynchronous change working directory request. - public async Task ChangeDirectoryAsync(string path, CancellationToken cancellationToken = default) + public async Task ChangeDirectoryAsync(string path, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); @@ -1469,7 +1469,7 @@ public void RequestMkDir(string path) /// The path. /// The to observe. /// A that represents the asynchronous SSH_FXP_MKDIR operation. - public Task RequestMkDirAsync(string path, CancellationToken cancellationToken = default) + public Task RequestMkDirAsync(string path, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { @@ -1529,7 +1529,7 @@ public void RequestRmDir(string path) } /// - public Task RequestRmDirAsync(string path, CancellationToken cancellationToken = default) + public Task RequestRmDirAsync(string path, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { diff --git a/src/Renci.SshNet/SftpClient.cs b/src/Renci.SshNet/SftpClient.cs index f220bae24..a5e4fee9b 100644 --- a/src/Renci.SshNet/SftpClient.cs +++ b/src/Renci.SshNet/SftpClient.cs @@ -1423,7 +1423,7 @@ public StreamWriter AppendText(string path, Encoding encoding) CheckDisposed(); ThrowHelper.ThrowIfNull(encoding); - return new StreamWriter(new SftpFileStream(_sftpSession, path, FileMode.Append, FileAccess.Write, (int)_bufferSize), encoding); + return new StreamWriter(Open(path, FileMode.Append, FileAccess.Write), encoding); } /// @@ -1442,9 +1442,7 @@ public StreamWriter AppendText(string path, Encoding encoding) /// public SftpFileStream Create(string path) { - CheckDisposed(); - - return new SftpFileStream(_sftpSession, path, FileMode.Create, FileAccess.ReadWrite, (int)_bufferSize); + return Create(path, (int)_bufferSize); } /// @@ -1466,7 +1464,7 @@ public SftpFileStream Create(string path, int bufferSize) { CheckDisposed(); - return new SftpFileStream(_sftpSession, path, FileMode.Create, FileAccess.ReadWrite, bufferSize); + return SftpFileStream.Open(_sftpSession, path, FileMode.Create, FileAccess.ReadWrite, bufferSize); } /// @@ -1642,7 +1640,7 @@ public SftpFileStream Open(string path, FileMode mode, FileAccess access) { CheckDisposed(); - return new SftpFileStream(_sftpSession, path, mode, access, (int)_bufferSize); + return SftpFileStream.Open(_sftpSession, path, mode, access, (int)_bufferSize); } /// @@ -1662,14 +1660,6 @@ public SftpFileStream Open(string path, FileMode mode, FileAccess access) public Task OpenAsync(string path, FileMode mode, FileAccess access, CancellationToken cancellationToken) { CheckDisposed(); - ThrowHelper.ThrowIfNull(path); - - if (_sftpSession is null) - { - throw new SshConnectionException("Client not connected."); - } - - cancellationToken.ThrowIfCancellationRequested(); return SftpFileStream.OpenAsync(_sftpSession, path, mode, access, (int)_bufferSize, cancellationToken); } @@ -1719,9 +1709,7 @@ public StreamReader OpenText(string path) /// public SftpFileStream OpenWrite(string path) { - CheckDisposed(); - - return new SftpFileStream(_sftpSession, path, FileMode.OpenOrCreate, FileAccess.Write, (int)_bufferSize); + return Open(path, FileMode.OpenOrCreate, FileAccess.Write); } /// diff --git a/src/Renci.SshNet/SubsystemSession.cs b/src/Renci.SshNet/SubsystemSession.cs index c83cd6e02..a632f41e8 100644 --- a/src/Renci.SshNet/SubsystemSession.cs +++ b/src/Renci.SshNet/SubsystemSession.cs @@ -18,7 +18,7 @@ internal abstract class SubsystemSession : ISubsystemSession { /// /// Holds the number of system wait handles that are returned as the leading entries in the array returned - /// in . + /// in . /// private const int SystemWaitHandleCount = 3; @@ -335,124 +335,7 @@ public void Dispose() } } - /// - /// Blocks the current thread until the specified gets signaled, using a - /// 32-bit signed integer to specify the time interval in milliseconds. - /// - /// The handle to wait for. - /// To number of milliseconds to wait for to get signaled, or -1 to wait indefinitely. - /// - /// if received a signal within the specified timeout; - /// otherwise, . - /// - /// The connection was closed by the server. - /// The channel was closed. - /// - /// The blocking wait is also interrupted when either the established channel is closed, the current - /// session is disconnected or an unexpected occurred while processing a channel - /// or session event. - /// - public bool WaitOne(WaitHandle waitHandle, int millisecondsTimeout) - { - var waitHandles = new[] - { - _errorOccurredWaitHandle, - _sessionDisconnectedWaitHandle, - _channelClosedWaitHandle, - waitHandle - }; - - var result = WaitHandle.WaitAny(waitHandles, millisecondsTimeout); - switch (result) - { - case 0: - ExceptionDispatchInfo.Capture(_exception).Throw(); - return false; // unreached - case 1: - throw new SshException("Connection was closed by the server."); - case 2: - throw new SshException("Channel was closed."); - case 3: - return true; - case WaitHandle.WaitTimeout: - return false; - default: - throw new NotImplementedException(string.Format(CultureInfo.InvariantCulture, "WaitAny return value '{0}' is not implemented.", result)); - } - } - - /// - /// Blocks the current thread until the specified gets signaled, using a - /// 32-bit signed integer to specify the time interval in milliseconds. - /// - /// The first handle to wait for. - /// The second handle to wait for. - /// To number of milliseconds to wait for a to get signaled, or -1 to wait indefinitely. - /// - /// 0 if received a signal within the specified timeout, and 1 - /// if received a signal within the specified timeout. - /// - /// The connection was closed by the server. - /// The channel was closed. - /// The handle did not get signaled within the specified timeout. - /// - /// - /// The blocking wait is also interrupted when either the established channel is closed, the current - /// session is disconnected or an unexpected occurred while processing a channel - /// or session event. - /// - /// - /// When both and are signaled during the call, - /// then 0 is returned. - /// - /// - public int WaitAny(WaitHandle waitHandleA, WaitHandle waitHandleB, int millisecondsTimeout) - { - var waitHandles = new[] - { - _errorOccurredWaitHandle, - _sessionDisconnectedWaitHandle, - _channelClosedWaitHandle, - waitHandleA, - waitHandleB - }; - - var result = WaitHandle.WaitAny(waitHandles, millisecondsTimeout); - switch (result) - { - case 0: - ExceptionDispatchInfo.Capture(_exception).Throw(); - return -1; // unreached - case 1: - throw new SshException("Connection was closed by the server."); - case 2: - throw new SshException("Channel was closed."); - case 3: - return 0; - case 4: - return 1; - case WaitHandle.WaitTimeout: - throw new SshOperationTimeoutException("Operation has timed out."); - default: - throw new NotImplementedException(string.Format(CultureInfo.InvariantCulture, "WaitAny return value '{0}' is not implemented.", result)); - } - } - - /// - /// Waits for any of the elements in the specified array to receive a signal, using a 32-bit signed - /// integer to specify the time interval. - /// - /// A array - constructed using - containing the objects to wait for. - /// To number of milliseconds to wait for a to get signaled, or -1 to wait indefinitely. - /// - /// The array index of the first non-system object that satisfied the wait. - /// - /// The connection was closed by the server. - /// The channel was closed. - /// No object satisfied the wait and a time interval equivalent to has passed. - /// - /// For the return value, the index of the first non-system object is considered to be zero. - /// + /// public int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout) { var result = WaitHandle.WaitAny(waitHandles, millisecondsTimeout); @@ -472,15 +355,7 @@ public int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout) } } - /// - /// Creates a array that is composed of system objects and the specified - /// elements. - /// - /// The first to wait for. - /// The second to wait for. - /// - /// A array that is composed of system objects and the specified elements. - /// + /// public WaitHandle[] CreateWaitHandleArray(WaitHandle waitHandle1, WaitHandle waitHandle2) { return new WaitHandle[] @@ -493,29 +368,6 @@ public WaitHandle[] CreateWaitHandleArray(WaitHandle waitHandle1, WaitHandle wai }; } - /// - /// Creates a array that is composed of system objects and the specified - /// elements. - /// - /// A array containing the objects to wait for. - /// - /// A array that is composed of system objects and the specified elements. - /// - public WaitHandle[] CreateWaitHandleArray(params WaitHandle[] waitHandles) - { - var array = new WaitHandle[waitHandles.Length + SystemWaitHandleCount]; - array[0] = _errorOccurredWaitHandle; - array[1] = _sessionDisconnectedWaitHandle; - array[2] = _channelClosedWaitHandle; - - for (var i = 0; i < waitHandles.Length; i++) - { - array[i + SystemWaitHandleCount] = waitHandles[i]; - } - - return array; - } - private void Session_Disconnected(object sender, EventArgs e) { _ = _sessionDisconnectedWaitHandle?.Set(); diff --git a/test/Renci.SshNet.IntegrationTests/SftpTests.cs b/test/Renci.SshNet.IntegrationTests/SftpTests.cs index 64ca1521b..603c726a5 100644 --- a/test/Renci.SshNet.IntegrationTests/SftpTests.cs +++ b/test/Renci.SshNet.IntegrationTests/SftpTests.cs @@ -49,7 +49,8 @@ public void Sftp_UploadFile_FileStream(int size) using (var memoryStream = new MemoryStream(size)) { - client.DownloadFile(remoteFile, memoryStream); + DownloadFileRandomMethod(client, remoteFile, memoryStream); + memoryStream.Position = 0; Assert.AreEqual(CreateFileHash(file), CreateHash(memoryStream)); } @@ -164,7 +165,7 @@ public void Sftp_BeginUploadFile() // check uploaded file using (var memoryStream = new MemoryStream()) { - client.DownloadFile(remoteFile, memoryStream); + DownloadFileRandomMethod(client, remoteFile, memoryStream); memoryStream.Position = 0; var remoteContent = Encoding.ASCII.GetString(memoryStream.ToArray()); Assert.AreEqual(content, remoteContent); @@ -300,18 +301,12 @@ public void Sftp_Create_FileDoesNotExist() { using (var fs = client.Create(remoteFile)) { - byte[] buffer = new byte[Math.Min(client.BufferSize, imageStream.Length)]; - int bytesRead; - - while ((bytesRead = imageStream.Read(buffer, offset: 0, buffer.Length)) > 0) - { - fs.Write(buffer, offset: 0, bytesRead); - } + imageStream.CopyTo(fs); } using (var memoryStream = new MemoryStream()) { - client.DownloadFile(remoteFile, memoryStream); + DownloadFileRandomMethod(client, remoteFile, memoryStream); memoryStream.Position = 0; imageStream.Position = 0; @@ -3192,7 +3187,7 @@ public void Sftp_BeginUploadFile_InputAndPath_FileDoesNotExist() using (var downloadMemoryStream = new MemoryStream()) { - client.DownloadFile(remoteFile, downloadMemoryStream); + DownloadFileRandomMethod(client, remoteFile, downloadMemoryStream); downloadMemoryStream.Position = 0; @@ -3242,7 +3237,7 @@ public void Sftp_BeginUploadFile_InputAndPath_ExistingFile() using (var downloadMemoryStream = new MemoryStream()) { - client.DownloadFile(remoteFile, downloadMemoryStream); + DownloadFileRandomMethod(client, remoteFile, downloadMemoryStream); downloadMemoryStream.Position = 0; @@ -3328,7 +3323,7 @@ public void Sftp_BeginUploadFile_InputAndPathAndCanOverride_CanOverrideIsFalse_F using (var downloadMemoryStream = new MemoryStream()) { - client.DownloadFile(remoteFile, downloadMemoryStream); + DownloadFileRandomMethod(client, remoteFile, downloadMemoryStream); downloadMemoryStream.Position = 0; @@ -3463,7 +3458,7 @@ public void Sftp_BeginUploadFile_InputAndPathAndCanOverride_CanOverrideIsTrue_Fi using (var downloadMemoryStream = new MemoryStream()) { - client.DownloadFile(remoteFile, downloadMemoryStream); + DownloadFileRandomMethod(client, remoteFile, downloadMemoryStream); downloadMemoryStream.Position = 0; @@ -3515,7 +3510,7 @@ public void Sftp_BeginUploadFile_InputAndPathAndCanOverride_CanOverrideIsTrue_Ex using (var downloadMemoryStream = new MemoryStream()) { - client.DownloadFile(remoteFile, downloadMemoryStream); + DownloadFileRandomMethod(client, remoteFile, downloadMemoryStream); downloadMemoryStream.Position = 0; @@ -3556,20 +3551,25 @@ public void Sftp_UploadAndDownloadBigFile() { var memoryStream = CreateMemoryStream(size); memoryStream.Position = 0; + var expectedHash = CreateHash(memoryStream); + memoryStream.Position = 0; client.UploadFile(memoryStream, remoteFile); + memoryStream.SetLength(0); + var stopwatch = new Stopwatch(); stopwatch.Start(); - // check uploaded file - memoryStream = new MemoryStream(); client.DownloadFile(remoteFile, memoryStream); - Assert.AreEqual(size, memoryStream.Length); - stopwatch.Stop(); + Assert.AreEqual(size, memoryStream.Length); + memoryStream.Position = 0; + var actualHash = CreateHash(memoryStream); + Assert.AreEqual(expectedHash, actualHash); + Console.WriteLine(@"Elapsed: {0} ms", stopwatch.ElapsedMilliseconds); Console.WriteLine(@"Transfer speed: {0:N2} KB/s", CalculateTransferSpeed(memoryStream.Length, stopwatch.ElapsedMilliseconds)); @@ -4140,46 +4140,6 @@ public async Task Sftp_ChangeDirectory_DirectoryExistsAsync() } } - [TestMethod] - public void Sftp_DownloadFile_MemoryStream() - { - const int fileSize = 500 * 1024; - - using (var client = new SftpClient(_connectionInfoFactory.Create())) - { - client.Connect(); - - var remoteFile = GenerateUniqueRemoteFileName(); - - SftpCreateRemoteFile(client, remoteFile, fileSize); - - try - { - using (var memoryStream = new MemoryStream()) - { - var stopwatch = new Stopwatch(); - stopwatch.Start(); - - client.DownloadFile(remoteFile, memoryStream); - stopwatch.Stop(); - - var transferSpeed = CalculateTransferSpeed(memoryStream.Length, stopwatch.ElapsedMilliseconds); - Console.WriteLine(@"Elapsed: {0} ms", stopwatch.ElapsedMilliseconds); - Console.WriteLine(@"Transfer speed: {0:N2} KB/s", transferSpeed); - - Assert.AreEqual(fileSize, memoryStream.Length); - } - } - finally - { - if (client.Exists(remoteFile)) - { - client.DeleteFile(remoteFile); - } - } - } - } - [TestMethod] public void Sftp_SubsystemExecution_Failed() { @@ -4229,6 +4189,75 @@ public void Sftp_SubsystemExecution_Failed() [TestMethod] public void Sftp_SftpFileStream_ReadAndWrite() + { + TestReadAndWrite( + (s, buffer, offset, count) => s.Read(buffer, offset, count), + (s, buffer, offset, count) => s.Write(buffer, offset, count)); + } + + [TestMethod] + public void Sftp_SftpFileStream_ReadAndWriteByte() + { + TestReadAndWrite( + read: (s, buffer, offset, count) => + { + int bytesRead = 0; + int b; + + while (bytesRead < count && (b = s.ReadByte()) != -1) + { + buffer[offset + bytesRead] = (byte)b; + bytesRead++; + } + + return bytesRead; + }, + write: (s, buffer, offset, count) => + { + for (int i = 0; i < count; i++) + { + s.WriteByte(buffer[offset + i]); + } + }); + } + + [TestMethod] + public void Sftp_SftpFileStream_ReadAndWriteAsync() + { + TestReadAndWrite( + (s, buffer, offset, count) => s.ReadAsync(buffer, offset, count).GetAwaiter().GetResult(), + (s, buffer, offset, count) => s.WriteAsync(buffer, offset, count).GetAwaiter().GetResult()); + } + + [TestMethod] + public void Sftp_SftpFileStream_ReadAndWriteBeginEnd() + { + TestReadAndWrite( + (s, buffer, offset, count) => s.EndRead(s.BeginRead(buffer, offset, count, null, null)), + (s, buffer, offset, count) => s.EndWrite(s.BeginWrite(buffer, offset, count, null, null))); + } + +#if NET + [TestMethod] + public void Sftp_SftpFileStream_ReadAndWriteSpan() + { + TestReadAndWrite( + (s, buffer, offset, count) => s.Read(buffer.AsSpan(offset, count)), + (s, buffer, offset, count) => s.Write(buffer.AsSpan(offset, count))); + } + + [TestMethod] + public void Sftp_SftpFileStream_ReadAndWriteAsyncMemory() + { + TestReadAndWrite( + (s, buffer, offset, count) => s.ReadAsync(buffer.AsMemory(offset, count)).AsTask().GetAwaiter().GetResult(), + (s, buffer, offset, count) => s.WriteAsync(buffer.AsMemory(offset, count)).AsTask().GetAwaiter().GetResult()); + } +#endif + + private void TestReadAndWrite( + Func read, + Action write) { using (var client = new SftpClient(_connectionInfoFactory.Create())) { @@ -4245,60 +4274,69 @@ public void Sftp_SftpFileStream_ReadAndWrite() { using (var s = client.Open(remoteFile, FileMode.CreateNew, FileAccess.Write)) { - s.Write(new byte[] { 5, 4, 3, 2, 1 }, 1, 3); + Assert.IsFalse(s.CanRead); + Assert.IsTrue(s.CanWrite); + + write(s, new byte[] { 5, 4, 3, 2, 1 }, 1, 3); } // switch from read to write mode using (var s = client.Open(remoteFile, FileMode.Open, FileAccess.ReadWrite)) { - Assert.AreEqual(4, s.ReadByte()); - Assert.AreEqual(3, s.ReadByte()); + Assert.IsTrue(s.CanRead); + Assert.IsTrue(s.CanWrite); + + Assert.AreEqual(4, ReadByte(s)); + Assert.AreEqual(3, ReadByte(s)); Assert.AreEqual(2, s.Position); - s.WriteByte(7); - s.Write(new byte[] { 8, 9, 10, 11, 12 }, 1, 3); + WriteByte(s, 7); + write(s, new byte[] { 8, 9, 10, 11, 12 }, 1, 3); Assert.AreEqual(6, s.Position); } using (var s = client.Open(remoteFile, FileMode.Open, FileAccess.Read)) { + Assert.IsTrue(s.CanRead); + Assert.IsFalse(s.CanWrite); + Assert.AreEqual(6, s.Length); var buffer = new byte[s.Length]; - Assert.AreEqual(6, s.Read(buffer, offset: 0, buffer.Length)); + Assert.AreEqual(6, read(s, buffer, 0, buffer.Length)); CollectionAssert.AreEqual(new byte[] { 4, 3, 7, 9, 10, 11 }, buffer); // Ensure we've reached end of the stream - Assert.AreEqual(-1, s.ReadByte()); + Assert.AreEqual(-1, ReadByte(s)); } // switch from read to write mode, and back to read mode and finally // append a byte using (var s = client.Open(remoteFile, FileMode.Open, FileAccess.ReadWrite)) { - Assert.AreEqual(4, s.ReadByte()); - Assert.AreEqual(3, s.ReadByte()); - Assert.AreEqual(7, s.ReadByte()); + Assert.AreEqual(4, ReadByte(s)); + Assert.AreEqual(3, ReadByte(s)); + Assert.AreEqual(7, ReadByte(s)); - s.Write(new byte[] { 0, 1, 6, 4 }, 1, 2); + write(s, new byte[] { 0, 1, 6, 4 }, 1, 2); - Assert.AreEqual(11, s.ReadByte()); + Assert.AreEqual(11, ReadByte(s)); // Ensure we've reached end of the stream - Assert.AreEqual(-1, s.ReadByte()); + Assert.AreEqual(-1, ReadByte(s)); - s.WriteByte(12); + WriteByte(s, 12); } // switch from write to read mode, and back to write mode using (var s = client.Open(remoteFile, FileMode.Open, FileAccess.ReadWrite)) { - s.WriteByte(5); - Assert.AreEqual(3, s.ReadByte()); - s.WriteByte(13); + WriteByte(s, 5); + Assert.AreEqual(3, ReadByte(s)); + WriteByte(s, 13); Assert.AreEqual(3, s.Position); } @@ -4308,12 +4346,12 @@ public void Sftp_SftpFileStream_ReadAndWrite() Assert.AreEqual(7, s.Length); var buffer = new byte[s.Length]; - Assert.AreEqual(7, s.Read(buffer, offset: 0, buffer.Length)); + Assert.AreEqual(7, read(s, buffer, 0, buffer.Length)); CollectionAssert.AreEqual(new byte[] { 5, 3, 13, 1, 6, 11, 12 }, buffer); // Ensure we've reached end of the stream - Assert.AreEqual(-1, s.ReadByte()); + Assert.AreEqual(-1, ReadByte(s)); } } finally @@ -4324,6 +4362,18 @@ public void Sftp_SftpFileStream_ReadAndWrite() } } } + + int ReadByte(SftpFileStream s) + { + var buffer = new byte[1]; + var bytesRead = read(s, buffer, 0, 1); + return bytesRead == 0 ? -1 : buffer[0]; + } + + void WriteByte(SftpFileStream s, byte b) + { + write(s, [b], 0, 1); + } } [TestMethod] @@ -4502,6 +4552,7 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile_SeekOriginBegin() Assert.AreEqual(3, newPosition); Assert.AreEqual(3, fs.Position); + Assert.AreEqual(1, fs.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -4529,6 +4580,7 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile_SeekOriginBegin() Assert.AreEqual(700, newPosition); Assert.AreEqual(700, fs.Position); + Assert.AreEqual(1, fs.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -4563,6 +4615,8 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile_SeekOriginBegin() Assert.AreEqual(seekOffset, fs.Position); fs.Write(writeBuffer, offset: 0, writeBuffer.Length); + + Assert.AreEqual(seekOffset + writeBuffer.Length, fs.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -4605,6 +4659,8 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile_SeekOriginBegin() Assert.AreEqual(seekOffset, fs.Position); fs.Write(writeBuffer, offset: 0, writeBuffer.Length); + + Assert.AreEqual(seekOffset + writeBuffer.Length, fs.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -4742,6 +4798,7 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile_SeekOriginEnd() Assert.AreEqual(4, newPosition); Assert.AreEqual(4, fs.Position); + Assert.AreEqual(1, fs.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -4769,6 +4826,7 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile_SeekOriginEnd() Assert.AreEqual(701, newPosition); Assert.AreEqual(701, fs.Position); + Assert.AreEqual(1, fs.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -4803,6 +4861,8 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile_SeekOriginEnd() Assert.AreEqual(4, fs.Position); fs.Write(writeBuffer, offset: 0, writeBuffer.Length); + + Assert.AreEqual(1 + seekOffset + writeBuffer.Length, fs.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -5455,7 +5515,7 @@ public void Sftp_SftpFileStream_SetLength_FileDoesNotExist() using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); downloaded.Position = 0; Assert.AreEqual(CreateHash(new byte[size]), CreateHash(downloaded)); } @@ -5495,6 +5555,9 @@ public void Sftp_Open_Append_Write_ExistingFile() using (var s = client.Open(remoteFile, FileMode.Append, FileAccess.Write)) { + Assert.IsFalse(s.CanRead); + Assert.IsTrue(s.CanWrite); + var buffer = new byte[] { 0x05, 0x0f, 0x0d, 0x0a, 0x04 }; s.Write(buffer, offset: 0, buffer.Length); input.Write(buffer, offset: 0, buffer.Length); @@ -5502,7 +5565,7 @@ public void Sftp_Open_Append_Write_ExistingFile() using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); input.Position = 0; downloaded.Position = 0; @@ -5562,7 +5625,7 @@ public void Sftp_Open_Append_Write_FileDoesNotExist() using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); downloaded.Position = 0; Assert.AreEqual(CreateHash(content), CreateHash(downloaded)); } @@ -5579,8 +5642,6 @@ public void Sftp_Open_Append_Write_FileDoesNotExist() } } - - [TestMethod] public void Sftp_Open_PathAndMode_ModeIsCreate_FileDoesNotExist() { @@ -5619,12 +5680,15 @@ public void Sftp_Open_PathAndMode_ModeIsCreate_FileDoesNotExist() using (var s = client.Open(remoteFile, FileMode.Create)) { + Assert.IsTrue(s.CanRead); + Assert.IsTrue(s.CanWrite); + s.Write(content, offset: 0, content.Length); } using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); downloaded.Position = 0; Assert.AreEqual(CreateHash(content), CreateHash(downloaded)); } @@ -5730,7 +5794,7 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsReadWrite_FileDo using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); downloaded.Position = 0; Assert.AreEqual(CreateHash(content), CreateHash(downloaded)); } @@ -5823,7 +5887,7 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsWrite_ExistingFi using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); downloaded.Position = 0; Assert.AreEqual(CreateHash(newContent), CreateHash(downloaded)); @@ -5882,7 +5946,7 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsWrite_FileDoesNo using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); downloaded.Position = 0; Assert.AreEqual(CreateHash(content), CreateHash(downloaded)); } @@ -5940,7 +6004,7 @@ public void Sftp_Open_CreateNew_Write_ExistingFile() // Verify that the file was not modified using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); input.Position = 0; downloaded.Position = 0; @@ -6000,7 +6064,7 @@ public void Sftp_Open_CreateNew_Write_FileDoesNotExist() using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); downloaded.Position = 0; Assert.AreEqual(CreateHash(content), CreateHash(downloaded)); } @@ -6048,7 +6112,7 @@ public void Sftp_Open_Open_Write_ExistingFile() using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); downloaded.Position = 0; Assert.AreEqual(CreateHash(expectedContent), CreateHash(downloaded)); @@ -6138,7 +6202,7 @@ public void Sftp_Open_OpenOrCreate_Write_ExistingFile() using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); downloaded.Position = 0; Assert.AreEqual(CreateHash(expectedContent), CreateHash(downloaded)); @@ -6197,7 +6261,7 @@ public void Sftp_Open_OpenOrCreate_Write_FileDoesNotExist() using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); downloaded.Position = 0; Assert.AreEqual(CreateHash(content), CreateHash(downloaded)); } @@ -6214,12 +6278,6 @@ public void Sftp_Open_OpenOrCreate_Write_FileDoesNotExist() } } - - - - - - [TestMethod] public void Sftp_Open_Truncate_Write_ExistingFile() { @@ -6255,7 +6313,7 @@ public void Sftp_Open_Truncate_Write_ExistingFile() using (var downloaded = new MemoryStream()) { - client.DownloadFile(remoteFile, downloaded); + DownloadFileRandomMethod(client, remoteFile, downloaded); input.Position = 0; downloaded.Position = 0; @@ -6311,49 +6369,6 @@ public void Sftp_Open_Truncate_Write_FileDoesNotExist() } } - [TestMethod] - public void Sftp_OpenRead() - { - const int fileSize = 5 * 1024 * 1024; - - using (var client = new SftpClient(_connectionInfoFactory.Create())) - { - client.Connect(); - - var remoteFile = GenerateUniqueRemoteFileName(); - - SftpCreateRemoteFile(client, remoteFile, fileSize); - - try - { - using (var s = client.OpenRead(remoteFile)) - { - var buffer = new byte[s.Length]; - - var stopwatch = new Stopwatch(); - stopwatch.Start(); - - var bytesRead = s.Read(buffer, offset: 0, buffer.Length); - - stopwatch.Stop(); - - var transferSpeed = CalculateTransferSpeed(bytesRead, stopwatch.ElapsedMilliseconds); - Console.WriteLine(@"Elapsed: {0} ms", stopwatch.ElapsedMilliseconds); - Console.WriteLine(@"Transfer speed: {0:N2} KB/s", transferSpeed); - - Assert.AreEqual(fileSize, bytesRead); - } - } - finally - { - if (client.Exists(remoteFile)) - { - client.DeleteFile(remoteFile); - } - } - } - } - [TestMethod] public void Sftp_SetLastAccessTime() { @@ -6468,17 +6483,63 @@ private static IEnumerable GetSftpUploadFileFileStreamData() private static Encoding GetRandomEncoding() { - var random = new Random().Next(1, 3); + var random = new Random().Next(1, 4); switch (random) { case 1: return Encoding.Unicode; case 2: return Encoding.UTF8; - case 3: + default: + Debug.Assert(random == 3); return Encoding.UTF32; + } + } + + private void DownloadFileRandomMethod(SftpClient client, string path, Stream output) + { + Console.Write($"Downloading '{path}'"); + + var random = new Random().Next(1, 6); + switch (random) + { + case 1: + Console.WriteLine($" with {nameof(SftpClient.DownloadFile)}"); + + client.DownloadFile(path, output); + + break; + case 2: + Console.WriteLine($" with {nameof(SftpClient.DownloadFileAsync)}"); + + client.DownloadFileAsync(path, output).GetAwaiter().GetResult(); + + break; + case 3: + Console.WriteLine($" with {nameof(SftpClient.BeginDownloadFile)}"); + + client.EndDownloadFile(client.BeginDownloadFile(path, output)); + + break; + case 4: + Console.WriteLine($" with {nameof(SftpFileStream.CopyTo)}"); + + using (var fs = client.OpenRead(path)) + { + fs.CopyTo(output); + } + + break; default: - throw new NotImplementedException(); + Debug.Assert(random == 5); + Console.WriteLine($" with {nameof(SftpFileStream.CopyToAsync)}"); + + using (var fs = client.OpenAsync(path, FileMode.Open, FileAccess.Read, CancellationToken.None).GetAwaiter().GetResult()) + { + fs.CopyToAsync(output).GetAwaiter().GetResult(); + } + + break; } } diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest.cs new file mode 100644 index 000000000..6ecbbc1d9 --- /dev/null +++ b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest.cs @@ -0,0 +1,298 @@ +using System; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +using Renci.SshNet.Common; +using Renci.SshNet.Sftp; +using Renci.SshNet.Sftp.Responses; + +namespace Renci.SshNet.Tests.Classes.Sftp +{ + [TestClass] + public class SftpFileStreamTest + { + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task BadFileMode_ThrowsArgumentOutOfRangeException(bool isAsync) + { + ArgumentOutOfRangeException ex; + + if (isAsync) + { + ex = await Assert.ThrowsAsync(() => + SftpFileStream.OpenAsync(new Mock().Object, "file.txt", mode: 0, FileAccess.Read, bufferSize: 1024, CancellationToken.None)); + } + else + { + ex = Assert.Throws(() => + SftpFileStream.Open(new Mock().Object, "file.txt", mode: 0, FileAccess.Read, bufferSize: 1024)); + } + + Assert.AreEqual("mode", ex.ParamName); + } + + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task BadFileAccess_ThrowsArgumentOutOfRangeException(bool isAsync) + { + ArgumentOutOfRangeException ex; + + if (isAsync) + { + ex = await Assert.ThrowsAsync(() => + SftpFileStream.OpenAsync(new Mock().Object, "file.txt", FileMode.Open, access: 0, bufferSize: 1024, CancellationToken.None)); + } + else + { + ex = Assert.Throws(() => + SftpFileStream.Open(new Mock().Object, "file.txt", FileMode.Open, access: 0, bufferSize: 1024)); + } + + Assert.AreEqual("access", ex.ParamName); + } + + [TestMethod] + [DataRow(FileMode.Append, FileAccess.Read, false)] + [DataRow(FileMode.Append, FileAccess.Read, true)] + [DataRow(FileMode.Append, FileAccess.ReadWrite, false)] + [DataRow(FileMode.Append, FileAccess.ReadWrite, true)] + [DataRow(FileMode.Create, FileAccess.Read, false)] + [DataRow(FileMode.Create, FileAccess.Read, true)] + [DataRow(FileMode.CreateNew, FileAccess.Read, false)] + [DataRow(FileMode.CreateNew, FileAccess.Read, true)] + [DataRow(FileMode.Truncate, FileAccess.Read, false)] + [DataRow(FileMode.Truncate, FileAccess.Read, true)] + public async Task InvalidModeAccessCombination_ThrowsArgumentException(FileMode mode, FileAccess access, bool isAsync) + { + ArgumentException ex; + + if (isAsync) + { + ex = await Assert.ThrowsAsync(() => + SftpFileStream.OpenAsync(new Mock().Object, "file.txt", mode, access, bufferSize: 1024, CancellationToken.None)); + } + else + { + ex = Assert.Throws(() => + SftpFileStream.Open(new Mock().Object, "file.txt", mode, access, bufferSize: 1024)); + } + + Assert.AreEqual("mode", ex.ParamName); + } + + [TestMethod] + public void ReadWithWriteAccess_ThrowsNotSupportedException() + { + var sessionMock = new Mock(); + + sessionMock.Setup(s => s.IsOpen).Returns(true); + + SetupRemoteSize(sessionMock, 128); + + var s = SftpFileStream.Open(sessionMock.Object, "file.txt", FileMode.Create, FileAccess.Write, bufferSize: 1024); + + Assert.IsFalse(s.CanRead); + + Assert.Throws(() => _ = s.Read(new byte[4], 0, 4)); + Assert.Throws(() => _ = s.ReadByte()); + Assert.Throws(() => _ = s.ReadAsync(new byte[4], 0, 4).GetAwaiter().GetResult()); + Assert.Throws(() => _ = s.EndRead(s.BeginRead(new byte[4], 0, 4, null, null))); +#if NET + Assert.Throws(() => _ = s.Read(new byte[4])); + Assert.Throws(() => _ = s.ReadAsync(new byte[4]).AsTask().GetAwaiter().GetResult()); +#endif + Assert.Throws(() => s.CopyTo(Stream.Null)); + Assert.Throws(() => s.CopyToAsync(Stream.Null).GetAwaiter().GetResult()); + } + + [TestMethod] + public void WriteWithReadAccess_ThrowsNotSupportedException() + { + var sessionMock = new Mock(); + + sessionMock.Setup(s => s.IsOpen).Returns(true); + + var s = SftpFileStream.Open(sessionMock.Object, "file.txt", FileMode.Open, FileAccess.Read, bufferSize: 1024); + + Assert.IsFalse(s.CanWrite); + + Assert.Throws(() => s.Write(new byte[4], 0, 4)); + Assert.Throws(() => s.WriteByte(0xf)); + Assert.Throws(() => s.WriteAsync(new byte[4], 0, 4).GetAwaiter().GetResult()); + Assert.Throws(() => s.EndWrite(s.BeginWrite(new byte[4], 0, 4, null, null))); +#if NET + Assert.Throws(() => s.Write(new byte[4])); + Assert.Throws(() => s.WriteAsync(new byte[4]).AsTask().GetAwaiter().GetResult()); +#endif + Assert.Throws(() => s.SetLength(1024)); + } + + [Ignore("TODO Currently throws EndOfStreamException in all cases.")] + [TestMethod] + [DataRow(-1, SeekOrigin.Begin)] + [DataRow(-1, SeekOrigin.Current)] + [DataRow(-1000, SeekOrigin.End)] + public void SeekBeforeBeginning_ThrowsIOException(long offset, SeekOrigin origin) + { + var sessionMock = new Mock(); + + sessionMock.Setup(s => s.IsOpen).Returns(true); + + SetupRemoteSize(sessionMock, 128); + + var s = SftpFileStream.Open(sessionMock.Object, "file.txt", FileMode.Open, FileAccess.Read, bufferSize: 1024); + + Assert.Throws(() => s.Seek(offset, origin)); + } + + private static void SetupRemoteSize(Mock sessionMock, long size) + { + sessionMock.Setup(s => s.RequestFStat(It.IsAny(), It.IsAny())).Returns(new SftpFileAttributes( + default, default, size: size, default, default, default, default + )); + } + + // Operations which should cause writes to be flushed because they depend on + // the remote file being up to date. + // Most of these are already implicitly covered by integration tests and may + // not be so valuable here. + [TestMethod] + public void Flush_SendsBufferedWrites() + { + TestSendsBufferedWrites(s => s.Flush()); + } + + [TestMethod] + public void Read_SendsBufferedWrites() + { + TestSendsBufferedWrites(s => _ = s.Read(new byte[16], 0, 16)); + } + + [TestMethod] + public void Seek_SendsBufferedWrites() + { + TestSendsBufferedWrites(s => _ = s.Seek(-1, SeekOrigin.Current)); + } + + [TestMethod] + public void SetPosition_SendsBufferedWrites() + { + TestSendsBufferedWrites(s => s.Position++); + } + + [TestMethod] + public void SetLength_SendsBufferedWrites() + { + TestSendsBufferedWrites(s => s.SetLength(256)); + } + + [TestMethod] + public void GetLength_SendsBufferedWrites() + { + TestSendsBufferedWrites(s => _ = s.Length); + } + + [TestMethod] + public void Dispose_SendsBufferedWrites() + { + TestSendsBufferedWrites(s => s.Dispose()); + } + + private void TestSendsBufferedWrites(Action flushAction) + { + var sessionMock = new Mock(); + + sessionMock.Setup(s => s.CalculateOptimalWriteLength(It.IsAny(), It.IsAny())).Returns((x, _) => x); + sessionMock.Setup(s => s.IsOpen).Returns(true); + SetupRemoteSize(sessionMock, 0); + + var s = SftpFileStream.Open(sessionMock.Object, "file.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, bufferSize: 1024); + + // Buffer some data + byte[] newData = "Some new bytes"u8.ToArray(); + s.Write(newData, 0, newData.Length); + + byte[] newData2 = "Some more bytes"u8.ToArray(); + s.Write(newData2, 0, newData2.Length); + + // The written data does not exceed bufferSize so we do not expect + // it to have been sent. + sessionMock.Verify(s => s.RequestWrite( + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny>()), + Times.Never); + + // Whatever is called here should trigger the bytes to be sent + flushAction(s); + + VerifyRequestWrite(sessionMock, newData.Concat(newData2), serverOffset: 0); + } + + [TestMethod] + public void Dispose() + { + var sessionMock = new Mock(); + + sessionMock.Setup(s => s.IsOpen).Returns(true); + + var s = SftpFileStream.Open(sessionMock.Object, "file.txt", FileMode.Create, FileAccess.ReadWrite, bufferSize: 1024); + + Assert.IsTrue(s.CanRead); + Assert.IsTrue(s.CanSeek); + Assert.IsTrue(s.CanWrite); + + s.Dispose(); + sessionMock.Verify(p => p.RequestClose(It.IsAny()), Times.Once); + + Assert.IsFalse(s.CanRead); + Assert.IsFalse(s.CanSeek); + Assert.IsFalse(s.CanWrite); + + Assert.Throws(() => s.Read(new byte[16], 0, 16)); + Assert.Throws(() => s.ReadByte()); + Assert.Throws(() => s.Write(new byte[16], 0, 16)); + Assert.Throws(() => s.WriteByte(0xf)); + Assert.Throws(() => s.CopyTo(Stream.Null)); + Assert.Throws(s.Flush); + Assert.Throws(() => s.Seek(0, SeekOrigin.Begin)); + Assert.Throws(() => s.SetLength(128)); + Assert.Throws(() => _ = s.Length); + + // Test no-op second dispose + s.Dispose(); + sessionMock.Verify(p => p.RequestClose(It.IsAny()), Times.Once); + } + + private static void VerifyRequestWrite(Mock sessionMock, ReadOnlyMemory newData, int serverOffset) + { + sessionMock.Verify(s => s.RequestWrite( + /* handle: */ It.IsAny(), + /* serverOffset: */ (ulong)serverOffset, + /* data: */ It.Is(x => IndexOf(x, newData) >= 0), + /* offset: */ It.IsAny(), + /* length: */ newData.Length, + /* wait: */ It.IsAny(), + /* writeCompleted: */ It.IsAny>()), + Times.Once); + } + + private static int IndexOf(byte[] searchSpace, ReadOnlyMemory searchValue) + { + // Needed in a (non-local) function because expression lambdas can't contain spans + return searchSpace.AsSpan().IndexOf(searchValue.Span); + } + } +} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTestBase.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTestBase.cs deleted file mode 100644 index 7fe0b3428..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTestBase.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - public abstract class SftpFileStreamTestBase - { - internal Mock SftpSessionMock; - protected MockSequence MockSequence; - - protected virtual void Arrange() - { - SetupData(); - CreateMocks(); - SetupMocks(); - } - - protected virtual void SetupData() - { - MockSequence = new MockSequence(); - } - - protected abstract void SetupMocks(); - - private void CreateMocks() - { - SftpSessionMock = new Mock(MockBehavior.Strict); - } - - [TestInitialize] - public void SetUp() - { - Arrange(); - Act(); - } - - protected abstract void Act(); - - protected byte[] GenerateRandom(int length) - { - return GenerateRandom(length, new Random()); - } - - protected byte[] GenerateRandom(int length, Random random) - { - var buffer = new byte[length]; - random.NextBytes(buffer); - return buffer; - } - - protected byte[] GenerateRandom(uint length) - { - return GenerateRandom(length, new Random()); - } - - protected byte[] GenerateRandom(uint length, Random random) - { - var buffer = new byte[length]; - random.NextBytes(buffer); - return buffer; - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessRead.cs deleted file mode 100644 index 3209d5ee4..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessRead.cs +++ /dev/null @@ -1,76 +0,0 @@ -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_CanRead_Closed_FileAccessRead : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private bool _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(3, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - _target.Close(); - } - - protected override void Act() - { - _actual = _target.CanRead; - } - - [TestMethod] - public void CanReadShouldReturnFalse() - { - Assert.IsFalse(_actual); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessReadWrite.cs deleted file mode 100644 index 335233d3b..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessReadWrite.cs +++ /dev/null @@ -1,76 +0,0 @@ -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_CanRead_Closed_FileAccessReadWrite : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private bool _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(3, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.ReadWrite, - (int)_bufferSize); - _target.Close(); - } - - protected override void Act() - { - _actual = _target.CanRead; - } - - [TestMethod] - public void CanReadShouldReturnFalse() - { - Assert.IsFalse(_actual); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessWrite.cs deleted file mode 100644 index 4a84eaf84..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessWrite.cs +++ /dev/null @@ -1,76 +0,0 @@ -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_CanRead_Closed_FileAccessWrite : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private bool _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(3, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Write, - (int)_bufferSize); - _target.Close(); - } - - protected override void Act() - { - _actual = _target.CanRead; - } - - [TestMethod] - public void CanReadShouldReturnFalse() - { - Assert.IsFalse(_actual); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessRead.cs deleted file mode 100644 index 2be256a3a..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessRead.cs +++ /dev/null @@ -1,76 +0,0 @@ -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_CanRead_Disposed_FileAccessRead : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private bool _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(1, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - _target.Dispose(); - } - - protected override void Act() - { - _actual = _target.CanRead; - } - - [TestMethod] - public void CanReadShouldReturnFalse() - { - Assert.IsFalse(_actual); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessReadWrite.cs deleted file mode 100644 index aa618feb4..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessReadWrite.cs +++ /dev/null @@ -1,76 +0,0 @@ -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_CanRead_Disposed_FileAccessReadWrite : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private bool _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(1, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.ReadWrite, - (int)_bufferSize); - _target.Dispose(); - } - - protected override void Act() - { - _actual = _target.CanRead; - } - - [TestMethod] - public void CanReadShouldReturnFalse() - { - Assert.IsFalse(_actual); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessWrite.cs deleted file mode 100644 index 506c42141..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessWrite.cs +++ /dev/null @@ -1,76 +0,0 @@ -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_CanRead_Disposed_FileAccessWrite : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private bool _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(1, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Write, - (int)_bufferSize); - _target.Dispose(); - } - - protected override void Act() - { - _actual = _target.CanRead; - } - - [TestMethod] - public void CanReadShouldReturnFalse() - { - Assert.IsFalse(_actual); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessRead.cs deleted file mode 100644 index 519e7a529..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessRead.cs +++ /dev/null @@ -1,76 +0,0 @@ -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_CanWrite_Closed_FileAccessRead : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private bool _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(1, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - _target.Close(); - } - - protected override void Act() - { - _actual = _target.CanWrite; - } - - [TestMethod] - public void CanWriteShouldReturnFalse() - { - Assert.IsFalse(_actual); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessReadWrite.cs deleted file mode 100644 index 730f95429..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessReadWrite.cs +++ /dev/null @@ -1,76 +0,0 @@ -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_CanWrite_Closed_FileAccessReadWrite : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private bool _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(1, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.ReadWrite, - (int)_bufferSize); - _target.Close(); - } - - protected override void Act() - { - _actual = _target.CanWrite; - } - - [TestMethod] - public void CanWriteShouldReturnFalse() - { - Assert.IsFalse(_actual); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessWrite.cs deleted file mode 100644 index a187b0d07..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessWrite.cs +++ /dev/null @@ -1,76 +0,0 @@ -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_CanWrite_Closed_FileAccessWrite : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private bool _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(1, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Write, - (int)_bufferSize); - _target.Close(); - } - - protected override void Act() - { - _actual = _target.CanWrite; - } - - [TestMethod] - public void CanWriteShouldReturnFalse() - { - Assert.IsFalse(_actual); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessRead.cs deleted file mode 100644 index 669a20a5b..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessRead.cs +++ /dev/null @@ -1,76 +0,0 @@ -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_CanWrite_Disposed_FileAccessRead : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private bool _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(1, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - _target.Dispose(); - } - - protected override void Act() - { - _actual = _target.CanWrite; - } - - [TestMethod] - public void CanWriteShouldReturnFalse() - { - Assert.IsFalse(_actual); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessReadWrite.cs deleted file mode 100644 index 0c3ceb803..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessReadWrite.cs +++ /dev/null @@ -1,76 +0,0 @@ -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_CanWrite_Disposed_FileAccessReadWrite : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private bool _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(1, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.ReadWrite, - (int)_bufferSize); - _target.Dispose(); - } - - protected override void Act() - { - _actual = _target.CanWrite; - } - - [TestMethod] - public void CanWriteShouldReturnFalse() - { - Assert.IsFalse(_actual); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessWrite.cs deleted file mode 100644 index b1bc4e482..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessWrite.cs +++ /dev/null @@ -1,76 +0,0 @@ -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_CanWrite_Disposed_FileAccessWrite : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private bool _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(1, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write, false)) - .Returns(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Write, - (int)_bufferSize); - _target.Dispose(); - } - - protected override void Act() - { - _actual = _target.CanWrite; - } - - [TestMethod] - public void CanWriteShouldReturnFalse() - { - Assert.IsFalse(_actual); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_Closed.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_Closed.cs deleted file mode 100644 index b52537fb8..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_Closed.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System; -using System.Globalization; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Close_Closed - { - private Mock _sftpSessionMock; - private string _path; - private SftpFileStream _sftpFileStream; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - - [TestInitialize] - public void Setup() - { - Arrange(); - Act(); - } - - protected void Arrange() - { - var random = new Random(); - _path = random.Next().ToString(CultureInfo.InvariantCulture); - _handle = new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) }; - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(0, 1000); - _writeBufferSize = (uint)random.Next(0, 1000); - - _sftpSessionMock = new Mock(MockBehavior.Strict); - - var sequence = new MockSequence(); - - _ = _sftpSessionMock.InSequence(sequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - _ = _sftpSessionMock.InSequence(sequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _ = _sftpSessionMock.InSequence(sequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _ = _sftpSessionMock.InSequence(sequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = _sftpSessionMock.InSequence(sequence) - .Setup(p => p.RequestClose(_handle)); - - _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Open, FileAccess.Read, (int)_bufferSize); - _sftpFileStream.Close(); - } - - protected void Act() - { - _sftpFileStream.Close(); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldBeInvokedOnce() - { - _sftpSessionMock.Verify(p => p.IsOpen, Times.Once); - } - - [TestMethod] - public void RequestCloseOnSftpSessionShouldBeInvokedOnce() - { - _sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_Disposed.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_Disposed.cs deleted file mode 100644 index 7625e284c..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_Disposed.cs +++ /dev/null @@ -1,81 +0,0 @@ -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_Close_Disposed : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(2, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - _target.Dispose(); - } - - protected override void Act() - { - _target.Close(); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.IsOpen, Times.Once); - } - - [TestMethod] - public void RequestCloseOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_SessionNotOpen.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_SessionNotOpen.cs deleted file mode 100644 index 48fc75472..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_SessionNotOpen.cs +++ /dev/null @@ -1,78 +0,0 @@ -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_Close_SessionNotOpen : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(2, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(false); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - } - - protected override void Act() - { - _target.Close(); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.IsOpen, Times.Once); - } - - [TestMethod] - public void RequestCloseOnSftpSessionShouldNeverBeInvoked() - { - SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Never); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_SessionOpen.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_SessionOpen.cs deleted file mode 100644 index 911f1d111..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_SessionOpen.cs +++ /dev/null @@ -1,80 +0,0 @@ -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_Close_SessionOpen : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(2, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - } - - protected override void Act() - { - _target.Close(); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.IsOpen, Times.Once); - } - - [TestMethod] - public void RequestCloseOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileAccessInvalid.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileAccessInvalid.cs deleted file mode 100644 index bc1c9e737..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileAccessInvalid.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileAccessInvalid : SftpFileStreamTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentOutOfRangeException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Open; - _fileAccess = 0; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override void Act() - { - try - { - new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - Assert.Fail(); - } - catch (ArgumentOutOfRangeException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - Assert.AreEqual("access", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessRead.cs deleted file mode 100644 index cac46a882..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessRead.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeAppend_FileAccessRead : SftpFileStreamTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Append; - _fileAccess = FileAccess.Read; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override void Act() - { - try - { - new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - Assert.Fail(); - } - catch (ArgumentException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - ArgumentExceptionAssert.MessageEquals(string.Format("{0} mode can be requested only when combined with write-only access.", _fileMode), _actualException); - Assert.AreEqual("mode", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessReadWrite.cs deleted file mode 100644 index de5e72e4d..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessReadWrite.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeAppend_FileAccessReadWrite : SftpFileStreamTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Append; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override void Act() - { - try - { - new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - Assert.Fail(); - } - catch (ArgumentException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - ArgumentExceptionAssert.MessageEquals(string.Format("{0} mode can be requested only when combined with write-only access.", _fileMode), _actualException); - Assert.AreEqual("mode", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessWrite.cs deleted file mode 100644 index 63ee0a1ab..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessWrite.cs +++ /dev/null @@ -1,185 +0,0 @@ -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); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessRead.cs deleted file mode 100644 index 5fd18e511..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessRead.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessRead : SftpFileStreamTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.CreateNew; - _fileAccess = FileAccess.Read; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override void Act() - { - try - { - _ = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - Assert.Fail(); - } - catch (ArgumentException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - ArgumentExceptionAssert.MessageEquals(string.Format("Combining {0}: {1} with {2}: {3} is invalid.", nameof(FileMode), _fileMode, nameof(FileAccess), _fileAccess), _actualException); - Assert.AreEqual("mode", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessReadWrite.cs deleted file mode 100644 index 93721ba50..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessReadWrite.cs +++ /dev/null @@ -1,167 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessReadWrite : 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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.CreateNew; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.CreateNew, 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); - } - - protected override void Act() - { - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - _fileMode, - _fileAccess, - _bufferSize); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_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 PositionShouldReturnZero() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(data); - - var actual = _target.Read(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void ReadByteShouldStartReadingAtBeginningOfFile() - { - var data = GenerateRandom(5, _random); - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(data); - - var actual = _target.ReadByte(); - - Assert.AreEqual(data[0], actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, 0UL, 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, 0UL, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.CreateNew, false), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessWrite.cs deleted file mode 100644 index cb1edf111..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessWrite.cs +++ /dev/null @@ -1,158 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeCreateNew_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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.CreateNew; - _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); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNew, 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); - } - - 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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, 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 WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWrite(_handle, 0UL, 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, 0UL, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNew, false), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessRead.cs deleted file mode 100644 index fdc0e9a2c..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessRead.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeCreate_FileAccessRead : SftpFileStreamTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Create; - _fileAccess = FileAccess.Read; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override void Act() - { - try - { - _ = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - Assert.Fail(); - } - catch (ArgumentException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - ArgumentExceptionAssert.MessageEquals(string.Format("Combining {0}: {1} with {2}: {3} is invalid.", nameof(FileMode), _fileMode, nameof(FileAccess), _fileAccess), _actualException); - Assert.AreEqual("mode", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessReadWrite_FileDoesNotExist.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessReadWrite_FileDoesNotExist.cs deleted file mode 100644 index c226dfd9e..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessReadWrite_FileDoesNotExist.cs +++ /dev/null @@ -1,170 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeCreate_FileAccessReadWrite_FileDoesNotExist : 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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Create; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, true)) - .Returns((byte[])null); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.CreateNew, 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); - } - - protected override void Act() - { - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - _fileMode, - _fileAccess, - _bufferSize); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_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 PositionShouldReturnZero() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(data); - - var actual = _target.Read(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void ReadByteShouldStartReadingAtBeginningOfFile() - { - var data = GenerateRandom(5, _random); - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(data); - - var actual = _target.ReadByte(); - - Assert.AreEqual(data[0], actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, 0UL, 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, 0UL, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, true), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessReadWrite_FileExists.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessReadWrite_FileExists.cs deleted file mode 100644 index 4c39283e3..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessReadWrite_FileExists.cs +++ /dev/null @@ -1,155 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeCreate_FileAccessReadWrite_FileExists : 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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Create; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, true)) - .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); - } - - protected override void Act() - { - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - _fileMode, - _fileAccess, - _bufferSize); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)).Returns(data); - - var actual = _target.Read(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void ReadByteShouldStartReadingAtBeginningOfFile() - { - var data = GenerateRandom(5, _random); - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(data); - - var actual = _target.ReadByte(); - - Assert.AreEqual(data[0], actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWrite(_handle, 0UL, 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, 0UL, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, true), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileDoesNotExist.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileDoesNotExist.cs deleted file mode 100644 index 4976dd062..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileDoesNotExist.cs +++ /dev/null @@ -1,162 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileDoesNotExist : 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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Create; - _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); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true)) - .Returns((byte[])null); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNew, 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); - } - - 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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, 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 WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWrite(_handle, 0UL, 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, 0UL, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnceWithTruncateAndOnceWithCreateNew() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true), Times.Once); - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNew, false), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileExists.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileExists.cs deleted file mode 100644 index 2c704f46d..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileExists.cs +++ /dev/null @@ -1,158 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileExists : 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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Create; - _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); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true)) - .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); - } - - 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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, 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 WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWrite(_handle, 0UL, 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, 0UL, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeInvalid.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeInvalid.cs deleted file mode 100644 index c2606ff0f..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeInvalid.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeInvalid : SftpFileStreamTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentOutOfRangeException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = 0; - _fileAccess = FileAccess.Read; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override void Act() - { - try - { - new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - Assert.Fail(); - } - catch (ArgumentOutOfRangeException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - Assert.AreEqual("mode", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessRead.cs deleted file mode 100644 index b845c6b47..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessRead.cs +++ /dev/null @@ -1,178 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessRead : 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; - - 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); - } - - 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); - } - - protected override void Act() - { - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - _fileMode, - _fileAccess, - _bufferSize); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_target.CanRead); - } - - [TestMethod] - public void CanSeekShouldReturnTrue() - { - Assert.IsTrue(_target.CanSeek); - } - - [TestMethod] - public void CanWriteShouldReturnFalse() - { - Assert.IsFalse(_target.CanWrite); - } - - [TestMethod] - public void CanTimeoutShouldReturnTrue() - { - Assert.IsTrue(_target.CanTimeout); - } - - [TestMethod] - public void PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)).Returns(data); - - var actual = _target.Read(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void ReadByteShouldStartReadingAtBeginningOfFile() - { - var data = GenerateRandom(5, _random); - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(data); - - var actual = _target.ReadByte(); - - Assert.AreEqual(data[0], actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void WriteShouldThrowNotSupportedException() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - try - { - _target.Write(buffer, 0, buffer.Length); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - Assert.AreEqual("Write not supported.", ex.Message); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void WriteByteShouldThrowNotSupportedException() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - try - { - _target.WriteByte(0x0a); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - Assert.AreEqual("Write not supported.", ex.Message); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Read | Flags.CreateNewOrOpen, false), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessReadWrite.cs deleted file mode 100644 index d99593d52..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessReadWrite.cs +++ /dev/null @@ -1,155 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessReadWrite : 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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.OpenOrCreate; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | 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); - } - - protected override void Act() - { - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - _fileMode, - _fileAccess, - _bufferSize); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)).Returns(data); - - var actual = _target.Read(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void ReadByteShouldStartReadingAtBeginningOfFile() - { - var data = GenerateRandom(5, _random); - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(data); - - var actual = _target.ReadByte(); - - Assert.AreEqual(data[0], actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWrite(_handle, 0UL, 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, 0UL, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.CreateNewOrOpen, false), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessWrite.cs deleted file mode 100644 index 08ae92469..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessWrite.cs +++ /dev/null @@ -1,158 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeOpenOrCreate_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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.OpenOrCreate; - _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); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | 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); - } - - 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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, 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 WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWrite(_handle, 0UL, 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, 0UL, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNewOrOpen, false), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessRead.cs deleted file mode 100644 index f80dcc740..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessRead.cs +++ /dev/null @@ -1,178 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeOpen_FileAccessRead : 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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Open; - _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); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, 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); - } - - protected override void Act() - { - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - _fileMode, - _fileAccess, - _bufferSize); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_target.CanRead); - } - - [TestMethod] - public void CanSeekShouldReturnTrue() - { - Assert.IsTrue(_target.CanSeek); - } - - [TestMethod] - public void CanWriteShouldReturnFalse() - { - Assert.IsFalse(_target.CanWrite); - } - - [TestMethod] - public void CanTimeoutShouldReturnTrue() - { - Assert.IsTrue(_target.CanTimeout); - } - - [TestMethod] - public void PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)).Returns(data); - - var actual = _target.Read(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void ReadByteShouldStartReadingAtBeginningOfFile() - { - var data = GenerateRandom(5, _random); - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(data); - - var actual = _target.ReadByte(); - - Assert.AreEqual(data[0], actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void WriteShouldThrowNotSupportedException() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - try - { - _target.Write(buffer, 0, buffer.Length); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - Assert.AreEqual("Write not supported.", ex.Message); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void WriteByteShouldThrowNotSupportedException() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - try - { - _target.WriteByte(0x0a); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - Assert.AreEqual("Write not supported.", ex.Message); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Read, false), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessReadWrite.cs deleted file mode 100644 index f56d64521..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessReadWrite.cs +++ /dev/null @@ -1,155 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeOpen_FileAccessReadWrite : 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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Open; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, 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); - } - - protected override void Act() - { - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - _fileMode, - _fileAccess, - _bufferSize); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)).Returns(data); - - var actual = _target.Read(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void ReadByteShouldStartReadingAtBeginningOfFile() - { - var data = GenerateRandom(5, _random); - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(data); - - var actual = _target.ReadByte(); - - Assert.AreEqual(data[0], actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWrite(_handle, 0UL, 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, 0UL, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessWrite.cs deleted file mode 100644 index 41b80a7a9..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessWrite.cs +++ /dev/null @@ -1,158 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeOpen_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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Open; - _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); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write, 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); - } - - 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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, 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 WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWrite(_handle, 0UL, 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, 0UL, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write, false), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessRead.cs deleted file mode 100644 index dadd6f610..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessRead.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessRead : SftpFileStreamTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Truncate; - _fileAccess = FileAccess.Read; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override void Act() - { - try - { - _ = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - Assert.Fail(); - } - catch (ArgumentException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - ArgumentExceptionAssert.MessageEquals(string.Format("Combining {0}: {1} with {2}: {3} is invalid.", nameof(FileMode), _fileMode, nameof(FileAccess), _fileAccess), _actualException); - Assert.AreEqual("mode", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessReadWrite.cs deleted file mode 100644 index 4a7445b9a..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessReadWrite.cs +++ /dev/null @@ -1,167 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessReadWrite : 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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Truncate; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, 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); - } - - protected override void Act() - { - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - _fileMode, - _fileAccess, - _bufferSize); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_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 PositionShouldReturnZero() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(data); - - var actual = _target.Read(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void ReadByteShouldStartReadingAtBeginningOfFile() - { - var data = GenerateRandom(5, _random); - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(data); - - var actual = _target.ReadByte(); - - Assert.AreEqual(data[0], actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once); - } - - [TestMethod] - public void WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, 0UL, 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, 0UL, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, false), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessWrite.cs deleted file mode 100644 index a099f7dc5..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessWrite.cs +++ /dev/null @@ -1,158 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Ctor_FileModeTruncate_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; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Truncate; - _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); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, 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); - } - - 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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, 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 WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWrite(_handle, 0UL, 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, 0UL, buffer, 0, buffer.Length, It.IsNotNull(), null), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, false), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_Closed.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_Closed.cs deleted file mode 100644 index 4abdfbe75..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_Closed.cs +++ /dev/null @@ -1,81 +0,0 @@ -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_Dispose_Closed : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(random.Next(1, 5), random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - _target.Close(); - } - - protected override void Act() - { - _target.Dispose(); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.IsOpen, Times.Once); - } - - [TestMethod] - public void RequestCloseOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_Disposed.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_Disposed.cs deleted file mode 100644 index d4aef3322..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_Disposed.cs +++ /dev/null @@ -1,78 +0,0 @@ -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_Dispose_Disposed : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(1, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNewOrOpen, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.OpenOrCreate, - FileAccess.Write, - (int)_bufferSize); - _target.Dispose(); - } - - protected override void Act() - { - _target.Dispose(); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.IsOpen, Times.Once); - } - - [TestMethod] - public void RequestCloseOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_SessionNotOpen.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_SessionNotOpen.cs deleted file mode 100644 index d1e967f23..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_SessionNotOpen.cs +++ /dev/null @@ -1,80 +0,0 @@ -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_Dispose_SessionNotOpen : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(2, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNew, false)) - .Returns(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(false); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.CreateNew, - FileAccess.Write, - (int)_bufferSize); - } - - protected override void Act() - { - _target.Dispose(); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.IsOpen, Times.Once); - } - - [TestMethod] - public void RequestCloseOnSftpSessionShouldNeverBeInvoked() - { - SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Never); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_SessionOpen.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_SessionOpen.cs deleted file mode 100644 index 2d4ca4762..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_SessionOpen.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System; -using System.Globalization; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Dispose_SessionOpen : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(CultureInfo.InvariantCulture); - _handle = GenerateRandom(2, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, false)) - .Returns(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen).Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Truncate, - FileAccess.Write, - (int)_bufferSize); - } - - protected override void Act() - { - _target.Dispose(); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.IsOpen, Times.Once); - } - - [TestMethod] - public void RequestCloseOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Finalize_SessionOpen.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Finalize_SessionOpen.cs deleted file mode 100644 index c0d3be899..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Finalize_SessionOpen.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System; -using System.Globalization; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Finalize_SessionOpen : SftpFileStreamTestBase - { - private WeakReference _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(CultureInfo.InvariantCulture); - _handle = GenerateRandom(7, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.CreateNewOrOpen, false)) - .Returns(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = CreateWeakSftpFileStream(); - } - - protected override void Act() - { - GC.Collect(); - GC.WaitForPendingFinalizers(); - } - - [TestMethod] - public void SftpFileStreamShouldHaveBeenFinalized() - { - Assert.IsFalse(_target.TryGetTarget(out _)); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldNeverBeInvoked() - { - SftpSessionMock.Verify(p => p.IsOpen, Times.Never); - } - - [TestMethod] - public void RequestCloseOnSftpSessionShouldNeverBeInvoked() - { - SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Never); - } - - private WeakReference CreateWeakSftpFileStream() - { - var sftpFileStream = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.OpenOrCreate, - FileAccess.ReadWrite, - (int)_bufferSize); - return new WeakReference(sftpFileStream); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_ReadMode_DataInBuffer_NotReadFromBuffer.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_ReadMode_DataInBuffer_NotReadFromBuffer.cs deleted file mode 100644 index c6ce946ca..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_ReadMode_DataInBuffer_NotReadFromBuffer.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Flush_ReadMode_DataInBuffer_NotReadFromBuffer : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private byte[] _readBytes; - private byte[] _serverBytes; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = 100; - _writeBufferSize = 500; - _readBytes = new byte[random.Next(1, (int)_readBufferSize - 10)]; - _serverBytes = GenerateRandom(_readBytes.Length + 5); // store 5 bytes in read buffer - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(_serverBytes); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - _ = _target.Read(_readBytes, 0, _readBytes.Length); - } - - protected override void Act() - { - _target.Flush(); - } - - [TestMethod] - public void PositionShouldReturnSameValueAsBeforeFlush() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - Assert.AreEqual(_readBytes.Length, _target.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - } - - [TestMethod] - public void ReadShouldReadFromServer() - { - var serverBytes2 = GenerateRandom(5); - var readBytes2 = new byte[5]; - var expectedReadBytes = new ArrayBuilder().Add(new byte[2]) - .Add(serverBytes2.Take(0, 3)) - .Build(); - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, (ulong)_readBytes.Length, _readBufferSize)) - .Returns(serverBytes2); - - var bytesRead = _target.Read(readBytes2, 2, 3); - - Assert.AreEqual(3, bytesRead); - CollectionAssert.AreEqual(expectedReadBytes, readBytes2); - - SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)_readBytes.Length, _readBufferSize), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_ReadMode_DataInBuffer_ReadFromBuffer.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_ReadMode_DataInBuffer_ReadFromBuffer.cs deleted file mode 100644 index 6f2d12dca..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_ReadMode_DataInBuffer_ReadFromBuffer.cs +++ /dev/null @@ -1,123 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Flush_ReadMode_DataInBuffer_ReadFromBuffer : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private byte[] _readBytes1; - private byte[] _readBytes2; - private byte[] _serverBytes; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = 100; - _writeBufferSize = 500; - _readBytes1 = new byte[random.Next(1, (int)_readBufferSize - 10)]; - _readBytes2 = new byte[random.Next(1, 3)]; - _serverBytes = GenerateRandom(_readBytes1.Length + 10); // store 5 bytes in read buffer - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(_serverBytes); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - _ = _target.Read(_readBytes1, 0, _readBytes1.Length); - _ = _target.Read(_readBytes2, 0, _readBytes2.Length); - } - - protected override void Act() - { - _target.Flush(); - } - - [TestMethod] - public void PositionShouldReturnSameValueAsBeforeFlush() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - Assert.AreEqual(_readBytes1.Length + _readBytes2.Length, _target.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); - } - - [TestMethod] - public void ReadShouldReadFromServer() - { - var serverBytes3 = GenerateRandom(5); - var readBytes3 = new byte[3]; - var expectedReadBytes = new ArrayBuilder().Add(new byte[1]) - .Add(serverBytes3.Take(0, 2)) - .Build(); - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, (ulong)(_readBytes1.Length + _readBytes2.Length), _readBufferSize)) - .Returns(serverBytes3); - - var bytesRead = _target.Read(readBytes3, 1, 2); - - Assert.AreEqual(2, bytesRead); - CollectionAssert.AreEqual(expectedReadBytes, readBytes3); - - SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)(_readBytes1.Length + _readBytes2.Length), _readBufferSize), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_ReadMode_NoDataInBuffer.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_ReadMode_NoDataInBuffer.cs deleted file mode 100644 index 8463a3133..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_ReadMode_NoDataInBuffer.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Flush_ReadMode_NoDataInBuffer : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private byte[] _readBytes; - private byte[] _serverBytes; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = 100; - _writeBufferSize = 500; - _readBytes = new byte[random.Next(1, (int)_readBufferSize)]; - _serverBytes = GenerateRandom(_readBytes.Length); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(_serverBytes); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - _ = _target.Read(_readBytes, 0, _readBytes.Length); - } - - protected override void Act() - { - _target.Flush(); - } - - [TestMethod] - public void PositionShouldReturnSameValueAsBeforeFlush() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - Assert.AreEqual(_readBytes.Length, _target.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - } - - [TestMethod] - public void ReadShouldReadFromServer() - { - var serverBytes2 = GenerateRandom(5); - var readBytes2 = new byte[5]; - var expectedReadBytes = new ArrayBuilder().Add(new byte[2]) - .Add(serverBytes2.Take(0, 3)) - .Build(); - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, (ulong)_readBytes.Length, _readBufferSize)) - .Returns(serverBytes2); - - var bytesRead = _target.Read(readBytes2, 2, 3); - - Assert.AreEqual(3, bytesRead); - CollectionAssert.AreEqual(expectedReadBytes, readBytes2); - - SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)_readBytes.Length, _readBufferSize), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_SessionNotOpen.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_SessionNotOpen.cs deleted file mode 100644 index a8de06427..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_SessionNotOpen.cs +++ /dev/null @@ -1,84 +0,0 @@ -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_Flush_SessionNotOpen : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private ObjectDisposedException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = 20; - _writeBufferSize = 500; - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(false); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - } - - protected override void Act() - { - try - { - _target.Flush(); - Assert.Fail(); - } - catch (ObjectDisposedException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void FlushShouldHaveThrownObjectDisposedException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - Assert.AreEqual(string.Format("Cannot access a closed SFTP session.{0}Object name: '{1}'.", Environment.NewLine, _actualException.ObjectName), _actualException.Message); - Assert.AreEqual(typeof(SftpFileStream).FullName, _actualException.ObjectName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_WriteMode_DataInBuffer.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_WriteMode_DataInBuffer.cs deleted file mode 100644 index e2c381cd1..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_WriteMode_DataInBuffer.cs +++ /dev/null @@ -1,152 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Sftp.Responses; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Flush_WriteMode_DataInBuffer : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private byte[] _writeBytes1; - private byte[] _writeBytes2; - private byte[] _writeBytes3; - private byte[] _flushedBytes; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = 100; - _writeBufferSize = 500; - _writeBytes1 = GenerateRandom(_writeBufferSize); - _writeBytes2 = GenerateRandom(2); - _writeBytes3 = GenerateRandom(3); - _flushedBytes = null; - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false)) - .Returns(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, 0UL, It.IsAny(), 0, _writeBytes1.Length, It.IsAny(), null)) - .Callback>((handle, serverOffset, data, offset, length, wait, writeCompleted) => - { - _ = wait.Set(); - }); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, (ulong)_writeBytes1.Length, It.IsAny(), 0, _writeBytes2.Length + _writeBytes3.Length, It.IsAny(), null)) - .Callback>((handle, serverOffset, data, offset, length, wait, writeCompleted) => - { - _flushedBytes = data.Take(offset, length); - _ = wait.Set(); - }); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.ReadWrite, - (int)_bufferSize); - _target.Write(_writeBytes1, 0, _writeBytes1.Length); - _target.Write(_writeBytes2, 0, _writeBytes2.Length); - _target.Write(_writeBytes3, 0, _writeBytes3.Length); - } - - protected override void Act() - { - _target.Flush(); - } - - [TestMethod] - public void PositionShouldReturnSameValueAsBeforeFlush() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - Assert.AreEqual(_writeBytes1.Length + _writeBytes2.Length + _writeBytes3.Length, _target.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(5)); - } - - [TestMethod] - public void BufferedBytesShouldHaveBeenWrittenToTheServer() - { - var expected = new ArrayBuilder().Add(_writeBytes2) - .Add(_writeBytes3) - .Build(); - - Assert.IsNotNull(_flushedBytes); - CollectionAssert.AreEqual(expected, _flushedBytes); - } - - [TestMethod] - public void ReadShouldReadFromServer() - { - var serverBytes = GenerateRandom(5); - var readBytes = new byte[5]; - var expectedReadBytes = new ArrayBuilder().Add(new byte[2]) - .Add(serverBytes.Take(0, 3)) - .Build(); - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, (ulong)(_writeBytes1.Length + _writeBytes2.Length + _writeBytes3.Length), _readBufferSize)) - .Returns(serverBytes); - - var bytesRead = _target.Read(readBytes, 2, 3); - - Assert.AreEqual(3, bytesRead); - CollectionAssert.AreEqual(expectedReadBytes, readBytes); - - SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)(_writeBytes1.Length + _writeBytes2.Length + _writeBytes3.Length), _readBufferSize), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(5)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_WriteMode_NoDataInBuffer.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_WriteMode_NoDataInBuffer.cs deleted file mode 100644 index 27ed3abca..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Flush_WriteMode_NoDataInBuffer.cs +++ /dev/null @@ -1,120 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Sftp.Responses; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Flush_WriteMode_NoDataInBuffer : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private byte[] _writeBytes; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = 100; - _writeBufferSize = 500; - _writeBytes = GenerateRandom(_writeBufferSize); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, 0UL, It.IsAny(), 0, _writeBytes.Length, It.IsAny(), null)) - .Callback>((handle, serverOffset, data, offset, length, wait, writeCompleted) - => - { - wait.Set(); - }); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.ReadWrite, - (int)_bufferSize); - _target.Write(_writeBytes, 0, _writeBytes.Length); - } - - protected override void Act() - { - _target.Flush(); - } - - [TestMethod] - public void PositionShouldReturnSameValueAsBeforeFlush() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - Assert.AreEqual(_writeBytes.Length, _target.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - } - - [TestMethod] - public void ReadShouldReadFromServer() - { - var serverBytes = GenerateRandom(5); - var readBytes = new byte[5]; - var expectedReadBytes = new ArrayBuilder().Add(new byte[2]) - .Add(serverBytes.Take(0, 3)) - .Build(); - - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, (ulong)_writeBytes.Length, _readBufferSize)) - .Returns(serverBytes); - - var bytesRead = _target.Read(readBytes, 2, 3); - - Assert.AreEqual(3, bytesRead); - CollectionAssert.AreEqual(expectedReadBytes, readBytes); - - SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)_writeBytes.Length, _readBufferSize), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileAccessInvalid.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileAccessInvalid.cs deleted file mode 100644 index 3a1ea7cb5..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileAccessInvalid.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileAccessInvalid : SftpFileStreamAsyncTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentOutOfRangeException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Open; - _fileAccess = 0; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override async Task ActAsync() - { - try - { - await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, default); - Assert.Fail(); - } - catch (ArgumentOutOfRangeException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - Assert.AreEqual("access", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessRead.cs deleted file mode 100644 index c922ab67a..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessRead.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessRead : SftpFileStreamAsyncTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Append; - _fileAccess = FileAccess.Read; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override async Task ActAsync() - { - try - { - await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, default); - Assert.Fail(); - } - catch (ArgumentException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - ArgumentExceptionAssert.MessageEquals(string.Format("{0} mode can be requested only when combined with write-only access.", _fileMode), _actualException); - Assert.AreEqual("mode", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessReadWrite.cs deleted file mode 100644 index 5360ec63f..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessReadWrite.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessReadWrite : SftpFileStreamAsyncTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Append; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override async Task ActAsync() - { - try - { - await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, default); - Assert.Fail(); - } - catch (ArgumentException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - ArgumentExceptionAssert.MessageEquals(string.Format("{0} mode can be requested only when combined with write-only access.", _fileMode), _actualException); - Assert.AreEqual("mode", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessWrite.cs deleted file mode 100644 index 3e86b59fc..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessWrite.cs +++ /dev/null @@ -1,164 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -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_OpenAsync_FileModeAppend_FileAccessWrite : SftpFileStreamAsyncTestBase - { - 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; - private CancellationToken _cancellationToken; - - 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(); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Write | Flags.Append | Flags.CreateNewOrOpen, _cancellationToken)) - .ReturnsAsync(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestFStatAsync(_handle, _cancellationToken)) - .ReturnsAsync(_fileAttributes); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - - [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 async Task ReadShouldThrowNotSupportedException() - { - var buffer = new byte[_readBufferSize]; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - try - { - _ = await _target.ReadAsync(buffer, 0, buffer.Length, _cancellationToken); - Assert.Fail(); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtEndOfFile() - { - var buffer = new byte[_writeBufferSize]; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWriteAsync(_handle, (ulong)_fileAttributes.Size, buffer, 0, buffer.Length, _cancellationToken)) - .Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length, _cancellationToken); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, (ulong)_fileAttributes.Size, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Write | Flags.Append | Flags.CreateNewOrOpen, default), Times.Once); - } - - [TestMethod] - public void RequestFStatOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestFStatAsync(_handle, default), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreateNew_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreateNew_FileAccessRead.cs deleted file mode 100644 index 900f1b15d..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreateNew_FileAccessRead.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeCreateNew_FileAccessRead : SftpFileStreamAsyncTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.CreateNew; - _fileAccess = FileAccess.Read; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override async Task ActAsync() - { - try - { - _ = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, default); - Assert.Fail(); - } - catch (ArgumentException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - ArgumentExceptionAssert.MessageEquals(string.Format("Combining {0}: {1} with {2}: {3} is invalid.", nameof(FileMode), _fileMode, nameof(FileAccess), _fileAccess), _actualException); - Assert.AreEqual("mode", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreateNew_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreateNew_FileAccessReadWrite.cs deleted file mode 100644 index 30d00439c..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreateNew_FileAccessReadWrite.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeCreateNew_FileAccessReadWrite : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.CreateNew; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Read | Flags.Write | Flags.CreateNew, _cancellationToken)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken)).ReturnsAsync(data); - - var actual = await _target.ReadAsync(buffer, 1, data.Length, _cancellationToken); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken), Times.Once); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken)).Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length, _cancellationToken); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Read | Flags.Write | Flags.CreateNew, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreateNew_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreateNew_FileAccessWrite.cs deleted file mode 100644 index 82eaf0455..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreateNew_FileAccessWrite.cs +++ /dev/null @@ -1,145 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeCreateNew_FileAccessWrite : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.CreateNew; - _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); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Write | Flags.CreateNew, _cancellationToken)) - .ReturnsAsync(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [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 PositionShouldReturnZero() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldThrowNotSupportedException() - { - var buffer = new byte[_readBufferSize]; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - try - { - _ = await _target.ReadAsync(buffer, 0, buffer.Length); - Assert.Fail(); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken)) - .Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Write | Flags.CreateNew, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessRead.cs deleted file mode 100644 index 6924da972..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessRead.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessRead : SftpFileStreamAsyncTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Create; - _fileAccess = FileAccess.Read; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override async Task ActAsync() - { - try - { - _ = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, default); - Assert.Fail(); - } - catch (ArgumentException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - ArgumentExceptionAssert.MessageEquals(string.Format("Combining {0}: {1} with {2}: {3} is invalid.", nameof(FileMode), _fileMode, nameof(FileAccess), _fileAccess), _actualException); - Assert.AreEqual("mode", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessReadWrite_FileDoesNotExist.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessReadWrite_FileDoesNotExist.cs deleted file mode 100644 index 0fd6b8d69..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessReadWrite_FileDoesNotExist.cs +++ /dev/null @@ -1,147 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessReadWrite_FileDoesNotExist : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Create; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Read | Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate, _cancellationToken)) - .ReturnsAsync(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_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 PositionShouldReturnZero() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken)) - .ReturnsAsync(data); - - var actual = await _target.ReadAsync(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken), Times.Once); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken)) - .Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Read | Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessReadWrite_FileExists.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessReadWrite_FileExists.cs deleted file mode 100644 index 3a773ac63..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessReadWrite_FileExists.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessReadWrite_FileExists : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Create; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Read | Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate, _cancellationToken)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken)).ReturnsAsync(data); - - var actual = await _target.ReadAsync(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken), Times.Once); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken)).Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Read | Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessWrite_FileDoesNotExist.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessWrite_FileDoesNotExist.cs deleted file mode 100644 index e141d615e..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessWrite_FileDoesNotExist.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessWrite_FileDoesNotExist : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Create; - _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); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate, _cancellationToken)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldThrowNotSupportedException() - { - var buffer = new byte[_readBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - try - { - _ = await _target.ReadAsync(buffer, 0, buffer.Length); - Assert.Fail(); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken)).Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnceWithTruncateAndOnceWithCreateNew() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessWrite_FileExists.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessWrite_FileExists.cs deleted file mode 100644 index 605aad7a5..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessWrite_FileExists.cs +++ /dev/null @@ -1,145 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessWrite_FileExists : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Create; - _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); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate, _cancellationToken)) - .ReturnsAsync(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [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 PositionShouldReturnZero() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldThrowNotSupportedException() - { - var buffer = new byte[_readBufferSize]; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - try - { - _ = await _target.ReadAsync(buffer, 0, buffer.Length); - Assert.Fail(); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken)) - .Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeInvalid.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeInvalid.cs deleted file mode 100644 index bdafe81ce..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeInvalid.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeInvalid : SftpFileStreamAsyncTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentOutOfRangeException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = 0; - _fileAccess = FileAccess.Read; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override async Task ActAsync() - { - try - { - await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, default); - Assert.Fail(); - } - catch (ArgumentOutOfRangeException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - Assert.AreEqual("mode", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpenOrCreate_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpenOrCreate_FileAccessRead.cs deleted file mode 100644 index a015281e6..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpenOrCreate_FileAccessRead.cs +++ /dev/null @@ -1,142 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeOpenOrCreate_FileAccessRead : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - 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); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Read | Flags.CreateNewOrOpen, _cancellationToken)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_target.CanRead); - } - - [TestMethod] - public void CanSeekShouldReturnTrue() - { - Assert.IsTrue(_target.CanSeek); - } - - [TestMethod] - public void CanWriteShouldReturnFalse() - { - Assert.IsFalse(_target.CanWrite); - } - - [TestMethod] - public void CanTimeoutShouldReturnTrue() - { - Assert.IsTrue(_target.CanTimeout); - } - - [TestMethod] - public void PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken)).ReturnsAsync(data); - - var actual = await _target.ReadAsync(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken), Times.Once); - } - - [TestMethod] - public async Task WriteShouldThrowNotSupportedException() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - try - { - await _target.WriteAsync(buffer, 0, buffer.Length); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Read | Flags.CreateNewOrOpen, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpenOrCreate_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpenOrCreate_FileAccessReadWrite.cs deleted file mode 100644 index 486b16ee9..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpenOrCreate_FileAccessReadWrite.cs +++ /dev/null @@ -1,147 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeOpenOrCreate_FileAccessReadWrite : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.OpenOrCreate; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Read | Flags.Write | Flags.CreateNewOrOpen, _cancellationToken)) - .ReturnsAsync(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_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 PositionShouldReturnZero() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken)) - .ReturnsAsync(data); - - var actual = await _target.ReadAsync(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken), Times.Once); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken)) - .Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Read | Flags.Write | Flags.CreateNewOrOpen, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpenOrCreate_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpenOrCreate_FileAccessWrite.cs deleted file mode 100644 index 86135132a..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpenOrCreate_FileAccessWrite.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeOpenOrCreate_FileAccessWrite : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.OpenOrCreate; - _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); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Write | Flags.CreateNewOrOpen, _cancellationToken)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldThrowNotSupportedException() - { - var buffer = new byte[_readBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - try - { - _ = await _target.ReadAsync(buffer, 0, buffer.Length); - Assert.Fail(); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken)).Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Write | Flags.CreateNewOrOpen, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessRead.cs deleted file mode 100644 index b45a78df2..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessRead.cs +++ /dev/null @@ -1,143 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessRead : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Open; - _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); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Read, _cancellationToken)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_target.CanRead); - } - - [TestMethod] - public void CanSeekShouldReturnTrue() - { - Assert.IsTrue(_target.CanSeek); - } - - [TestMethod] - public void CanWriteShouldReturnFalse() - { - Assert.IsFalse(_target.CanWrite); - } - - [TestMethod] - public void CanTimeoutShouldReturnTrue() - { - Assert.IsTrue(_target.CanTimeout); - } - - [TestMethod] - public void PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken)).ReturnsAsync(data); - - var actual = await _target.ReadAsync(buffer, 1, data.Length, _cancellationToken); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken), Times.Once); - } - - [TestMethod] - public async Task WriteShouldThrowNotSupportedException() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - try - { - await _target.WriteAsync(buffer, 0, buffer.Length, _cancellationToken); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - Assert.AreEqual("Write not supported.", ex.Message); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Read, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessReadWrite.cs deleted file mode 100644 index 331b0e61d..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessReadWrite.cs +++ /dev/null @@ -1,147 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessReadWrite : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Open; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Read | Flags.Write, _cancellationToken)) - .ReturnsAsync(_handle); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_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 PositionShouldReturnZero() - { - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken)) - .ReturnsAsync(data); - - var actual = await _target.ReadAsync(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken), Times.Once); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - _ = SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken)) - .Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Read | Flags.Write, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessWrite.cs deleted file mode 100644 index ed2d72e4d..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessWrite.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessWrite : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Open; - _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); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Write, _cancellationToken)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldThrowNotSupportedException() - { - var buffer = new byte[_readBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - try - { - _ = await _target.ReadAsync(buffer, 0, buffer.Length); - Assert.Fail(); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken)).Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Write, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeTruncate_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeTruncate_FileAccessRead.cs deleted file mode 100644 index 276455b7a..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeTruncate_FileAccessRead.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeTruncate_FileAccessRead : SftpFileStreamAsyncTestBase - { - private Random _random; - private string _path; - private FileMode _fileMode; - private FileAccess _fileAccess; - private int _bufferSize; - private ArgumentException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Truncate; - _fileAccess = FileAccess.Read; - _bufferSize = _random.Next(5, 1000); - } - - protected override void SetupMocks() - { - } - - protected override async Task ActAsync() - { - try - { - _ = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, default); - Assert.Fail(); - } - catch (ArgumentException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void CtorShouldHaveThrownArgumentException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - ArgumentExceptionAssert.MessageEquals(string.Format("Combining {0}: {1} with {2}: {3} is invalid.", nameof(FileMode), _fileMode, nameof(FileAccess), _fileAccess), _actualException); - Assert.AreEqual("mode", _actualException.ParamName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeTruncate_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeTruncate_FileAccessReadWrite.cs deleted file mode 100644 index f5e78c0c4..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeTruncate_FileAccessReadWrite.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeTruncate_FileAccessReadWrite : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Truncate; - _fileAccess = FileAccess.ReadWrite; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Read | Flags.Write | Flags.Truncate, _cancellationToken)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [TestMethod] - public void CanReadShouldReturnTrue() - { - Assert.IsTrue(_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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldStartReadingAtBeginningOfFile() - { - var buffer = new byte[8]; - var data = new byte[] { 5, 4, 3, 2, 1 }; - var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 }; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken)).ReturnsAsync(data); - - var actual = await _target.ReadAsync(buffer, 1, data.Length); - - Assert.AreEqual(data.Length, actual); - Assert.IsTrue(buffer.IsEqualTo(expected)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, _cancellationToken), Times.Once); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken)).Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Read | Flags.Write | Flags.Truncate, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeTruncate_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeTruncate_FileAccessWrite.cs deleted file mode 100644 index 75c9ea1ac..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileModeTruncate_FileAccessWrite.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_OpenAsync_FileModeTruncate_FileAccessWrite : SftpFileStreamAsyncTestBase - { - 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 CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(); - _fileMode = FileMode.Truncate; - _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); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Write | Flags.Truncate, _cancellationToken)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - } - - protected override async Task ActAsync() - { - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken); - } - - [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 PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var actual = _target.Position; - - Assert.AreEqual(0L, actual); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task ReadShouldThrowNotSupportedException() - { - var buffer = new byte[_readBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - try - { - _ = await _target.ReadAsync(buffer, 0, buffer.Length); - Assert.Fail(); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - } - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - } - - [TestMethod] - public async Task WriteShouldStartWritingAtBeginningOfFile() - { - var buffer = new byte[_writeBufferSize]; - - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken)).Returns(Task.CompletedTask); - - await _target.WriteAsync(buffer, 0, buffer.Length); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1)); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once); - } - - [TestMethod] - public void RequestOpenOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Write | Flags.Truncate, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadAsync_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndEqualToBufferSize.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadAsync_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndEqualToBufferSize.cs deleted file mode 100644 index dbc891cbf..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadAsync_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndEqualToBufferSize.cs +++ /dev/null @@ -1,160 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_ReadAsync_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndEqualToBufferSize : SftpFileStreamAsyncTestBase - { - private string _path; - private SftpFileStream _target; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private int _actual; - private byte[] _buffer; - private byte[] _serverData1; - private byte[] _serverData2; - private int _serverData1Length; - private int _serverData2Length; - private int _numberOfBytesToRead; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = 20; - _writeBufferSize = 500; - - _numberOfBytesToRead = (int)_readBufferSize + 5; // greather than read buffer size - _buffer = new byte[_numberOfBytesToRead]; - _serverData1Length = (int)_readBufferSize; // equal to read buffer size - _serverData1 = GenerateRandom(_serverData1Length, random); - _serverData2Length = (int)_readBufferSize; // equal to read buffer size - _serverData2 = GenerateRandom(_serverData2Length, random); - - Assert.IsTrue(_serverData1Length < _numberOfBytesToRead && _serverData1Length == _readBufferSize); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Read, default)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, default)) - .ReturnsAsync(_serverData1); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestReadAsync(_handle, (ulong)_serverData1.Length, _readBufferSize, default)) - .ReturnsAsync(_serverData2); - } - - [TestCleanup] - public void TearDown() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override async Task ArrangeAsync() - { - await base.ArrangeAsync(); - - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize, - default); - } - - protected override async Task ActAsync() - { - _actual = await _target.ReadAsync(_buffer, 0, _numberOfBytesToRead, default); - } - - [TestMethod] - public void ReadShouldHaveReturnedTheNumberOfBytesRequested() - { - Assert.AreEqual(_numberOfBytesToRead, _actual); - } - - [TestMethod] - public void ReadShouldHaveWrittenBytesToTheCallerSuppliedBuffer() - { - Assert.IsTrue(_serverData1.IsEqualTo(_buffer.Take(_serverData1Length))); - - var bytesWrittenFromSecondRead = _numberOfBytesToRead - _serverData1Length; - Assert.IsTrue(_serverData2.Take(bytesWrittenFromSecondRead).IsEqualTo(_buffer.Take(_serverData1Length, bytesWrittenFromSecondRead))); - } - - [TestMethod] - public void PositionShouldReturnNumberOfBytesWrittenToCallerProvidedBuffer() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(_actual, _target.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public async Task ReadShouldReturnAllRemaningBytesFromReadBufferWhenCountIsEqualToNumberOfRemainingBytes() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var numberOfBytesRemainingInReadBuffer = _serverData1Length + _serverData2Length - _numberOfBytesToRead; - - _buffer = new byte[numberOfBytesRemainingInReadBuffer]; - - var actual = await _target.ReadAsync(_buffer, 0, _buffer.Length); - - Assert.AreEqual(_buffer.Length, actual); - Assert.IsTrue(_serverData2.Take(_numberOfBytesToRead - _serverData1Length, _buffer.Length).IsEqualTo(_buffer)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public async Task ReadShouldReturnAllRemaningBytesFromReadBufferAndReadAgainWhenCountIsGreaterThanNumberOfRemainingBytesAndNewReadReturnsZeroBytes() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestReadAsync(_handle, (ulong)(_serverData1Length + _serverData2Length), _readBufferSize, default)).ReturnsAsync(Array.Empty()); - - var numberOfBytesRemainingInReadBuffer = _serverData1Length + _serverData2Length - _numberOfBytesToRead; - - _buffer = new byte[numberOfBytesRemainingInReadBuffer + 1]; - - var actual = await _target.ReadAsync(_buffer, 0, _buffer.Length); - - Assert.AreEqual(numberOfBytesRemainingInReadBuffer, actual); - Assert.IsTrue(_serverData2.Take(_numberOfBytesToRead - _serverData1Length, numberOfBytesRemainingInReadBuffer).IsEqualTo(_buffer.Take(numberOfBytesRemainingInReadBuffer))); - Assert.AreEqual(0, _buffer[numberOfBytesRemainingInReadBuffer]); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - SftpSessionMock.Verify(p => p.RequestReadAsync(_handle, (ulong)(_serverData1Length + _serverData2Length), _readBufferSize, default)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadAsync_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndLessThanBufferSize.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadAsync_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndLessThanBufferSize.cs deleted file mode 100644 index fa497d143..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadAsync_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndLessThanBufferSize.cs +++ /dev/null @@ -1,153 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_ReadAsync_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndLessThanBufferSize : SftpFileStreamAsyncTestBase - { - private string _path; - private SftpFileStream _target; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private int _actual; - private byte[] _buffer; - private byte[] _serverData; - private int _serverDataLength; - private int _numberOfBytesToRead; - private byte[] _originalBuffer; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = 20; - _writeBufferSize = 500; - - _numberOfBytesToRead = (int)_readBufferSize + 2; // greater than read buffer size - _originalBuffer = GenerateRandom(_numberOfBytesToRead, random); - _buffer = _originalBuffer.Copy(); - - _serverDataLength = (int)_readBufferSize - 1; // less than read buffer size - _serverData = GenerateRandom(_serverDataLength, random); - - Assert.IsTrue(_serverDataLength < _numberOfBytesToRead && _serverDataLength < _readBufferSize); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Read, default)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, default)) - .ReturnsAsync(_serverData); - } - - [TestCleanup] - public void TearDown() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override async Task ArrangeAsync() - { - await base.ArrangeAsync(); - - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize, - default); - } - - protected override async Task ActAsync() - { - _actual = await _target.ReadAsync(_buffer, 0, _numberOfBytesToRead, default); - } - - [TestMethod] - public void ReadShouldHaveReturnedTheNumberOfBytesReturnedByTheReadFromTheServer() - { - Assert.AreEqual(_serverDataLength, _actual); - } - - [TestMethod] - public void ReadShouldHaveWrittenBytesToTheCallerSuppliedBufferAndRemainingBytesShouldRemainUntouched() - { - Assert.IsTrue(_serverData.IsEqualTo(_buffer.Take(_serverDataLength))); - Assert.IsTrue(_originalBuffer.Take(_serverDataLength, _originalBuffer.Length - _serverDataLength).IsEqualTo(_buffer.Take(_serverDataLength, _buffer.Length - _serverDataLength))); - } - - [TestMethod] - public void PositionShouldReturnNumberOfBytesWrittenToCallerProvidedBuffer() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(_actual, _target.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public async Task SubsequentReadShouldReadAgainFromCurrentPositionFromServerAndReturnZeroWhenServerReturnsZeroBytes() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestReadAsync(_handle, (ulong)_actual, _readBufferSize, default)) - .ReturnsAsync(Array.Empty()); - - var buffer = _originalBuffer.Copy(); - var actual = await _target.ReadAsync(buffer, 0, buffer.Length); - - Assert.AreEqual(0, actual); - Assert.IsTrue(_originalBuffer.IsEqualTo(buffer)); - - SftpSessionMock.Verify(p => p.RequestReadAsync(_handle, (ulong)_actual, _readBufferSize, default), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public async Task SubsequentReadShouldReadAgainFromCurrentPositionFromServerAndNotUpdatePositionWhenServerReturnsZeroBytes() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestReadAsync(_handle, (ulong)_actual, _readBufferSize, default)) - .ReturnsAsync(Array.Empty()); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - _ = await _target.ReadAsync(new byte[10], 0, 10); - - Assert.AreEqual(_actual, _target.Position); - - SftpSessionMock.Verify(p => p.RequestReadAsync(_handle, (ulong)_actual, _readBufferSize, default), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadAsync_ReadMode_NoDataInReaderBufferAndReadMoreBytesFromServerThanCount.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadAsync_ReadMode_NoDataInReaderBufferAndReadMoreBytesFromServerThanCount.cs deleted file mode 100644 index 183f66e33..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadAsync_ReadMode_NoDataInReaderBufferAndReadMoreBytesFromServerThanCount.cs +++ /dev/null @@ -1,144 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_ReadAsync_ReadMode_NoDataInReaderBufferAndReadMoreBytesFromServerThanCount : SftpFileStreamAsyncTestBase - { - private string _path; - private SftpFileStream _target; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private int _actual; - private byte[] _buffer; - private byte[] _serverData; - private int _numberOfBytesToWriteToReadBuffer; - private int _numberOfBytesToRead; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = 20; - _writeBufferSize = 500; - - _numberOfBytesToRead = 20; - _buffer = new byte[_numberOfBytesToRead]; - _numberOfBytesToWriteToReadBuffer = 10; // should be less than _readBufferSize - _serverData = GenerateRandom(_numberOfBytesToRead + _numberOfBytesToWriteToReadBuffer, random); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Read, default)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestReadAsync(_handle, 0UL, _readBufferSize, default)) - .ReturnsAsync(_serverData); - } - - [TestCleanup] - public void TearDown() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override async Task ArrangeAsync() - { - await base.ArrangeAsync(); - - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize, - default); - } - - protected override async Task ActAsync() - { - _actual = await _target.ReadAsync(_buffer, 0, _numberOfBytesToRead, default); - } - - [TestMethod] - public void ReadShouldHaveReturnedTheNumberOfBytesWrittenToCallerSuppliedBuffer() - { - Assert.AreEqual(_numberOfBytesToRead, _actual); - } - - [TestMethod] - public void ReadShouldHaveWrittenBytesToTheCallerSuppliedBuffer() - { - Assert.IsTrue(_serverData.Take(_actual).IsEqualTo(_buffer)); - } - - [TestMethod] - public void PositionShouldReturnNumberOfBytesWrittenToCallerProvidedBuffer() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(_actual, _target.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public async Task SubsequentReadShouldReturnAllRemaningBytesFromReadBufferWhenCountIsEqualToNumberOfRemainingBytes() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var buffer = new byte[_numberOfBytesToWriteToReadBuffer]; - - var actual = await _target.ReadAsync(buffer, 0, _numberOfBytesToWriteToReadBuffer, default); - - Assert.AreEqual(_numberOfBytesToWriteToReadBuffer, actual); - Assert.IsTrue(_serverData.Take(_numberOfBytesToRead, _numberOfBytesToWriteToReadBuffer).IsEqualTo(buffer)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public async Task SubsequentReadShouldReturnAllRemaningBytesFromReadBufferAndReadAgainWhenCountIsGreaterThanNumberOfRemainingBytesAndNewReadReturnsZeroBytes() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestReadAsync(_handle, (ulong)(_serverData.Length), _readBufferSize, default)).ReturnsAsync(Array.Empty()); - - var buffer = new byte[_numberOfBytesToWriteToReadBuffer + 1]; - - var actual = await _target.ReadAsync(buffer, 0, buffer.Length); - - Assert.AreEqual(_numberOfBytesToWriteToReadBuffer, actual); - Assert.IsTrue(_serverData.Take(_numberOfBytesToRead, _numberOfBytesToWriteToReadBuffer).IsEqualTo(buffer.Take(_numberOfBytesToWriteToReadBuffer))); - Assert.AreEqual(0, buffer[_numberOfBytesToWriteToReadBuffer]); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - SftpSessionMock.Verify(p => p.RequestReadAsync(_handle, (ulong)(_serverData.Length), _readBufferSize, default)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_Eof.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_Eof.cs deleted file mode 100644 index 50d56a5a9..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_Eof.cs +++ /dev/null @@ -1,88 +0,0 @@ -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_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_Eof : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private int _actual; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(1, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(new byte[0]); - } - - [TestCleanup] - public void TearDown() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - } - - protected override void Act() - { - _actual = _target.ReadByte(); - } - - [TestMethod] - public void ReadByteShouldReturnMinusOne() - { - Assert.AreEqual(-1, _actual); - } - - [TestMethod] - public void PositionShouldReturnZero() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(0L, _target.Position); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_LessDataThanReadBufferSizeAvailable.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_LessDataThanReadBufferSizeAvailable.cs deleted file mode 100644 index d48cac258..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_LessDataThanReadBufferSizeAvailable.cs +++ /dev/null @@ -1,93 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - /// - /// Test for issue #173. - /// - [TestClass] - public class SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_LessDataThanReadBufferSizeAvailable : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private int _actual; - private byte[] _data; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(5, 1000); - _readBufferSize = (uint)random.Next(10, 100); - _writeBufferSize = (uint)random.Next(10, 100); - _data = GenerateRandom((int)_readBufferSize - 2, random); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(_data); - } - - [TestCleanup] - public void TearDown() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - } - - protected override void Act() - { - _actual = _target.ReadByte(); - } - - [TestMethod] - public void ReadByteShouldReturnFirstByteThatWasReadFromServer() - { - Assert.AreEqual(_data[0], _actual); - } - - [TestMethod] - public void PositionShouldReturnOne() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(1L, _target.Position); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndEqualToBufferSize.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndEqualToBufferSize.cs deleted file mode 100644 index 8ae2138a3..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndEqualToBufferSize.cs +++ /dev/null @@ -1,158 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndEqualToBufferSize : SftpFileStreamTestBase - { - private string _path; - private SftpFileStream _target; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private int _actual; - private byte[] _buffer; - private byte[] _serverData1; - private byte[] _serverData2; - private int _serverData1Length; - private int _serverData2Length; - private int _numberOfBytesToRead; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = 20; - _writeBufferSize = 500; - - _numberOfBytesToRead = (int)_readBufferSize + 5; // greather than read buffer size - _buffer = new byte[_numberOfBytesToRead]; - _serverData1Length = (int)_readBufferSize; // equal to read buffer size - _serverData1 = GenerateRandom(_serverData1Length, random); - _serverData2Length = (int)_readBufferSize; // equal to read buffer size - _serverData2 = GenerateRandom(_serverData2Length, random); - - Assert.IsTrue(_serverData1Length < _numberOfBytesToRead && _serverData1Length == _readBufferSize); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(_serverData1); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, (ulong)_serverData1.Length, _readBufferSize)) - .Returns(_serverData2); - } - - [TestCleanup] - public void TearDown() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - } - - protected override void Act() - { - _actual = _target.Read(_buffer, 0, _numberOfBytesToRead); - } - - [TestMethod] - public void ReadShouldHaveReturnedTheNumberOfBytesRequested() - { - Assert.AreEqual(_numberOfBytesToRead, _actual); - } - - [TestMethod] - public void ReadShouldHaveWrittenBytesToTheCallerSuppliedBuffer() - { - Assert.IsTrue(_serverData1.IsEqualTo(_buffer.Take(_serverData1Length))); - - var bytesWrittenFromSecondRead = _numberOfBytesToRead - _serverData1Length; - Assert.IsTrue(_serverData2.Take(bytesWrittenFromSecondRead).IsEqualTo(_buffer.Take(_serverData1Length, bytesWrittenFromSecondRead))); - } - - [TestMethod] - public void PositionShouldReturnNumberOfBytesWrittenToCallerProvidedBuffer() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(_actual, _target.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void ReadShouldReturnAllRemaningBytesFromReadBufferWhenCountIsEqualToNumberOfRemainingBytes() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var numberOfBytesRemainingInReadBuffer = _serverData1Length + _serverData2Length - _numberOfBytesToRead; - - _buffer = new byte[numberOfBytesRemainingInReadBuffer]; - - var actual = _target.Read(_buffer, 0, _buffer.Length); - - Assert.AreEqual(_buffer.Length, actual); - Assert.IsTrue(_serverData2.Take(_numberOfBytesToRead - _serverData1Length, _buffer.Length).IsEqualTo(_buffer)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void ReadShouldReturnAllRemaningBytesFromReadBufferAndReadAgainWhenCountIsGreaterThanNumberOfRemainingBytesAndNewReadReturnsZeroBytes() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, (ulong)(_serverData1Length + _serverData2Length), _readBufferSize)).Returns(Array.Empty()); - - var numberOfBytesRemainingInReadBuffer = _serverData1Length + _serverData2Length - _numberOfBytesToRead; - - _buffer = new byte[numberOfBytesRemainingInReadBuffer + 1]; - - var actual = _target.Read(_buffer, 0, _buffer.Length); - - Assert.AreEqual(numberOfBytesRemainingInReadBuffer, actual); - Assert.IsTrue(_serverData2.Take(_numberOfBytesToRead - _serverData1Length, numberOfBytesRemainingInReadBuffer).IsEqualTo(_buffer.Take(numberOfBytesRemainingInReadBuffer))); - Assert.AreEqual(0, _buffer[numberOfBytesRemainingInReadBuffer]); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)(_serverData1Length + _serverData2Length), _readBufferSize)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndLessThanBufferSize.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndLessThanBufferSize.cs deleted file mode 100644 index 95008b9b8..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndLessThanBufferSize.cs +++ /dev/null @@ -1,151 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCountAndLessThanBufferSize : SftpFileStreamTestBase - { - private string _path; - private SftpFileStream _target; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private int _actual; - private byte[] _buffer; - private byte[] _serverData; - private int _serverDataLength; - private int _numberOfBytesToRead; - private byte[] _originalBuffer; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = 20; - _writeBufferSize = 500; - - _numberOfBytesToRead = (int)_readBufferSize + 2; // greater than read buffer size - _originalBuffer = GenerateRandom(_numberOfBytesToRead, random); - _buffer = _originalBuffer.Copy(); - - _serverDataLength = (int)_readBufferSize - 1; // less than read buffer size - _serverData = GenerateRandom(_serverDataLength, random); - - Assert.IsTrue(_serverDataLength < _numberOfBytesToRead && _serverDataLength < _readBufferSize); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(_serverData); - } - - [TestCleanup] - public void TearDown() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - } - - protected override void Act() - { - _actual = _target.Read(_buffer, 0, _numberOfBytesToRead); - } - - [TestMethod] - public void ReadShouldHaveReturnedTheNumberOfBytesReturnedByTheReadFromTheServer() - { - Assert.AreEqual(_serverDataLength, _actual); - } - - [TestMethod] - public void ReadShouldHaveWrittenBytesToTheCallerSuppliedBufferAndRemainingBytesShouldRemainUntouched() - { - Assert.IsTrue(_serverData.IsEqualTo(_buffer.Take(_serverDataLength))); - Assert.IsTrue(_originalBuffer.Take(_serverDataLength, _originalBuffer.Length - _serverDataLength).IsEqualTo(_buffer.Take(_serverDataLength, _buffer.Length - _serverDataLength))); - } - - [TestMethod] - public void PositionShouldReturnNumberOfBytesWrittenToCallerProvidedBuffer() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(_actual, _target.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void SubsequentReadShouldReadAgainFromCurrentPositionFromServerAndReturnZeroWhenServerReturnsZeroBytes() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, (ulong)_actual, _readBufferSize)) - .Returns(Array.Empty()); - - var buffer = _originalBuffer.Copy(); - var actual = _target.Read(buffer, 0, buffer.Length); - - Assert.AreEqual(0, actual); - Assert.IsTrue(_originalBuffer.IsEqualTo(buffer)); - - SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)_actual, _readBufferSize), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void SubsequentReadShouldReadAgainFromCurrentPositionFromServerAndNotUpdatePositionWhenServerReturnsZeroBytes() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, (ulong)_actual, _readBufferSize)) - .Returns(Array.Empty()); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - _ = _target.Read(new byte[10], 0, 10); - - Assert.AreEqual(_actual, _target.Position); - - SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)_actual, _readBufferSize), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadMoreBytesFromServerThanCount.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadMoreBytesFromServerThanCount.cs deleted file mode 100644 index 258975ada..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadMoreBytesFromServerThanCount.cs +++ /dev/null @@ -1,142 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadMoreBytesFromServerThanCount : SftpFileStreamTestBase - { - private string _path; - private SftpFileStream _target; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private int _actual; - private byte[] _buffer; - private byte[] _serverData; - private int _numberOfBytesToWriteToReadBuffer; - private int _numberOfBytesToRead; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = 20; - _writeBufferSize = 500; - - _numberOfBytesToRead = 20; - _buffer = new byte[_numberOfBytesToRead]; - _numberOfBytesToWriteToReadBuffer = 10; // should be less than _readBufferSize - _serverData = GenerateRandom(_numberOfBytesToRead + _numberOfBytesToWriteToReadBuffer, random); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(_serverData); - } - - [TestCleanup] - public void TearDown() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - } - - protected override void Act() - { - _actual = _target.Read(_buffer, 0, _numberOfBytesToRead); - } - - [TestMethod] - public void ReadShouldHaveReturnedTheNumberOfBytesWrittenToCallerSuppliedBuffer() - { - Assert.AreEqual(_numberOfBytesToRead, _actual); - } - - [TestMethod] - public void ReadShouldHaveWrittenBytesToTheCallerSuppliedBuffer() - { - Assert.IsTrue(_serverData.Take(_actual).IsEqualTo(_buffer)); - } - - [TestMethod] - public void PositionShouldReturnNumberOfBytesWrittenToCallerProvidedBuffer() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(_actual, _target.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void SubsequentReadShouldReturnAllRemaningBytesFromReadBufferWhenCountIsEqualToNumberOfRemainingBytes() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - var buffer = new byte[_numberOfBytesToWriteToReadBuffer]; - - var actual = _target.Read(buffer, 0, _numberOfBytesToWriteToReadBuffer); - - Assert.AreEqual(_numberOfBytesToWriteToReadBuffer, actual); - Assert.IsTrue(_serverData.Take(_numberOfBytesToRead, _numberOfBytesToWriteToReadBuffer).IsEqualTo(buffer)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void SubsequentReadShouldReturnAllRemaningBytesFromReadBufferAndReadAgainWhenCountIsGreaterThanNumberOfRemainingBytesAndNewReadReturnsZeroBytes() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, (ulong)(_serverData.Length), _readBufferSize)).Returns(Array.Empty()); - - var buffer = new byte[_numberOfBytesToWriteToReadBuffer + 1]; - - var actual = _target.Read(buffer, 0, buffer.Length); - - Assert.AreEqual(_numberOfBytesToWriteToReadBuffer, actual); - Assert.IsTrue(_serverData.Take(_numberOfBytesToRead, _numberOfBytesToWriteToReadBuffer).IsEqualTo(buffer.Take(_numberOfBytesToWriteToReadBuffer))); - Assert.AreEqual(0, buffer[_numberOfBytesToWriteToReadBuffer]); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)(_serverData.Length), _readBufferSize)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginBeginAndOffsetNegative.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginBeginAndOffsetNegative.cs deleted file mode 100644 index 1a94e7174..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginBeginAndOffsetNegative.cs +++ /dev/null @@ -1,100 +0,0 @@ -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_OriginBeginAndOffsetNegative : 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 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); - } - - 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); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - } - - protected override void Act() - { - try - { - _target.Seek(_offset, SeekOrigin.Begin); - Assert.Fail(); - } - 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)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginBeginAndOffsetPositive.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginBeginAndOffsetPositive.cs deleted file mode 100644 index 5be4026fb..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginBeginAndOffsetPositive.cs +++ /dev/null @@ -1,90 +0,0 @@ -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_OriginBeginAndOffsetPositive : 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 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); - } - - 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); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - } - - protected override void Act() - { - _actual = _target.Seek(_offset, SeekOrigin.Begin); - } - - [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)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginBeginAndOffsetZero.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginBeginAndOffsetZero.cs deleted file mode 100644 index 3370a44de..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginBeginAndOffsetZero.cs +++ /dev/null @@ -1,88 +0,0 @@ -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_OriginBeginAndOffsetZero : 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 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); - } - - 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); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - } - - protected override void Act() - { - _actual = _target.Seek(0L, SeekOrigin.Begin); - } - - [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)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetNegative.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetNegative.cs deleted file mode 100644 index 0c6bdfd35..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetNegative.cs +++ /dev/null @@ -1,106 +0,0 @@ -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 int _length; - 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.Write; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _length = _random.Next(5, 10000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - _offset = _random.Next(-_length, -1); - _attributes = SftpFileAttributes.Empty; - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.RequestOpen(_path, Flags.Write | Flags.CreateNewOrOpen, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.RequestFStat(_handle, false)) - .Returns(_attributes); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.RequestFSetStat(_handle, _attributes)); - SftpSessionMock.InSequence(MockSequence).Setup(session => session.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.RequestFStat(_handle, false)) - .Returns(_attributes); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - _target.SetLength(_length); - } - - protected override void Act() - { - _actual = _target.Seek(_offset, SeekOrigin.End); - } - - [TestMethod] - public void SeekShouldHaveReturnedOffset() - { - Assert.AreEqual(_attributes.Size + _offset, _actual); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldHaveBeenInvokedTwice() - { - SftpSessionMock.Verify(session => session.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void PositionShouldReturnOffset() - { - SftpSessionMock.InSequence(MockSequence).Setup(session => session.IsOpen).Returns(true); - - Assert.AreEqual(_attributes.Size + _offset, _target.Position); - - SftpSessionMock.Verify(session => session.IsOpen, Times.Exactly(3)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetPositive.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetPositive.cs deleted file mode 100644 index c7e2cf46f..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetPositive.cs +++ /dev/null @@ -1,106 +0,0 @@ -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 int _length; - 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.Write; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _length = _random.Next(5, 10000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - _offset = _random.Next(1, int.MaxValue); - _attributes = SftpFileAttributes.Empty; - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.RequestOpen(_path, Flags.Write | Flags.CreateNewOrOpen, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.RequestFStat(_handle, false)) - .Returns(_attributes); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.RequestFSetStat(_handle, _attributes)); - SftpSessionMock.InSequence(MockSequence).Setup(session => session.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.RequestFStat(_handle, false)) - .Returns(_attributes); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - _target.SetLength(_length); - } - - protected override void Act() - { - _actual = _target.Seek(_offset, SeekOrigin.End); - } - - [TestMethod] - public void SeekShouldHaveReturnedOffset() - { - Assert.AreEqual(_attributes.Size + _offset, _actual); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldHaveBeenInvokedTwice() - { - SftpSessionMock.Verify(session => session.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void PositionShouldReturnOffset() - { - SftpSessionMock.InSequence(MockSequence).Setup(session => session.IsOpen).Returns(true); - - Assert.AreEqual(_attributes.Size + _offset, _target.Position); - - SftpSessionMock.Verify(session => session.IsOpen, Times.Exactly(3)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetZero.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetZero.cs deleted file mode 100644 index c171bb525..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginEndAndOffsetZero.cs +++ /dev/null @@ -1,106 +0,0 @@ -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 int _length; - 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.Write; - _bufferSize = _random.Next(5, 1000); - _readBufferSize = (uint)_random.Next(5, 1000); - _writeBufferSize = (uint)_random.Next(5, 1000); - _length = _random.Next(5, 10000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - _offset = 0; - _attributes = SftpFileAttributes.Empty; - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.RequestOpen(_path, Flags.Write | Flags.CreateNewOrOpen, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.CalculateOptimalReadLength((uint)_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.CalculateOptimalWriteLength((uint)_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.RequestFStat(_handle, false)) - .Returns(_attributes); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.RequestFSetStat(_handle, _attributes)); - SftpSessionMock.InSequence(MockSequence).Setup(session => session.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(session => session.RequestFStat(_handle, false)) - .Returns(_attributes); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - _target.SetLength(_length); - } - - protected override void Act() - { - _actual = _target.Seek(_offset, SeekOrigin.End); - } - - [TestMethod] - public void SeekShouldHaveReturnedSize() - { - Assert.AreEqual(_attributes.Size, _actual); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldHaveBeenInvokedTwice() - { - SftpSessionMock.Verify(session => session.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void PositionShouldReturnSize() - { - SftpSessionMock.InSequence(MockSequence).Setup(session => session.IsOpen).Returns(true); - - Assert.AreEqual(_attributes.Size, _target.Position); - - SftpSessionMock.Verify(session => session.IsOpen, Times.Exactly(3)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtMiddleOfStream_OriginBeginAndOffsetZero_NoBuffering.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtMiddleOfStream_OriginBeginAndOffsetZero_NoBuffering.cs deleted file mode 100644 index 1cf004215..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtMiddleOfStream_OriginBeginAndOffsetZero_NoBuffering.cs +++ /dev/null @@ -1,125 +0,0 @@ -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_PositionedAtMiddleOfStream_OriginBeginAndOffsetZero_NoBuffering : 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 long _actual; - private byte[] _buffer; - private byte[] _serverData; - - 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 = 20; - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - _buffer = new byte[_readBufferSize]; - _serverData = GenerateRandom(_buffer.Length, _random); - } - - 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.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(_serverData); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - int readBytesCount = _target.Read(_buffer, 0, _buffer.Length); - Assert.AreEqual(_buffer.Length, readBytesCount); - } - - protected override void Act() - { - _actual = _target.Seek(0L, SeekOrigin.Begin); - } - - [TestMethod] - public void SeekShouldHaveReturnedZero() - { - Assert.AreEqual(0L, _actual); - } - - [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(3)); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldHaveBeenInvokedTwice() - { - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void ReadShouldReturnReadBytesFromServer() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(new byte[] { 0x05, 0x04 }); - - var buffer = new byte[1]; - - var bytesRead = _target.Read(buffer, 0, buffer.Length); - - Assert.AreEqual(buffer.Length, bytesRead); - Assert.AreEqual(0x05, buffer[0]); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Exactly(2)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtMiddleOfStream_OriginBeginAndOffsetZero_ReadBuffer.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtMiddleOfStream_OriginBeginAndOffsetZero_ReadBuffer.cs deleted file mode 100644 index 8a3b282b4..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Seek_PositionedAtMiddleOfStream_OriginBeginAndOffsetZero_ReadBuffer.cs +++ /dev/null @@ -1,143 +0,0 @@ -using System; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Seek_PositionedAtMiddleOfStream_OriginBeginAndOffsetZero_ReadBuffer : 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 long _actual; - private byte[] _buffer; - private byte[] _serverData1; - private byte[] _serverData2; - - 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 = 20; - _writeBufferSize = (uint)_random.Next(5, 1000); - _handle = GenerateRandom(_random.Next(1, 10), _random); - _buffer = new byte[2]; // should be less than size of read buffer - _serverData1 = GenerateRandom((int)_readBufferSize, _random); - _serverData2 = GenerateRandom((int)_readBufferSize, _random); - } - - 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.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(_serverData1); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize); - int readBytesCount = _target.Read(_buffer, 0, _buffer.Length); - Assert.AreEqual(_buffer.Length, readBytesCount); - } - - protected override void Act() - { - _actual = _target.Seek(0L, SeekOrigin.Begin); - } - - [TestMethod] - public void SeekShouldHaveReturnedZero() - { - Assert.AreEqual(0L, _actual); - } - - [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(3)); - } - - [TestMethod] - public void IsOpenOnSftpSessionShouldHaveBeenInvokedTwice() - { - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void ReadBytesThatWereNotBufferedBeforeSeekShouldReadBytesFromServer() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(_serverData2); - - var bytesRead = _target.Read(_buffer, 0, _buffer.Length); - - Assert.AreEqual(_buffer.Length, bytesRead); - Assert.IsTrue(_serverData2.Take(_buffer.Length).IsEqualTo(_buffer)); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Exactly(2)); - } - - [TestMethod] - public void ReadBytesThatWereBufferedBeforeSeekShouldReadBytesFromServer() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) - .Returns(_serverData2); - - var buffer = new byte[_buffer.Length + 1]; // we read one byte that was previously buffered - var bytesRead = _target.Read(buffer, 0, buffer.Length); - - Assert.AreEqual(buffer.Length, bytesRead); - Assert.IsTrue(_serverData2.Take(buffer.Length).IsEqualTo(buffer)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_Closed.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_Closed.cs deleted file mode 100644 index e87586788..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_Closed.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using System.Globalization; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_SetLength_Closed - { - private Mock _sftpSessionMock; - private string _path; - private SftpFileStream _sftpFileStream; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private ObjectDisposedException _actualException; - - [TestInitialize] - public void Setup() - { - Arrange(); - Act(); - } - - protected void Arrange() - { - var random = new Random(); - _path = random.Next().ToString(CultureInfo.InvariantCulture); - _handle = new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) }; - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(0, 1000); - _writeBufferSize = (uint)random.Next(0, 1000); - - _sftpSessionMock = new Mock(MockBehavior.Strict); - - var sequence = new MockSequence(); - _sftpSessionMock.InSequence(sequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true)) - .Returns(_handle); - _sftpSessionMock.InSequence(sequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _sftpSessionMock.InSequence(sequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _sftpSessionMock.InSequence(sequence) - .Setup(p => p.IsOpen) - .Returns(true); - _sftpSessionMock.InSequence(sequence) - .Setup(p => p.RequestClose(_handle)); - - _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize); - _sftpFileStream.Close(); - } - - protected void Act() - { - try - { - _sftpFileStream.SetLength(5); - Assert.Fail(); - } - catch (ObjectDisposedException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void SetLengthShouldHaveThrownObjectDisposedException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - Assert.AreEqual( - string.Format( - "Cannot access a disposed object.{0}Object name: '{1}'.", - Environment.NewLine, - _actualException.ObjectName), - _actualException.Message); - Assert.AreEqual(typeof(SftpFileStream).FullName, _actualException.ObjectName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition.cs deleted file mode 100644 index b5c6a2690..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition.cs +++ /dev/null @@ -1,209 +0,0 @@ -using System; -using System.Globalization; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Sftp.Responses; -using Renci.SshNet.Tests.Common; - - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - /// - /// - In read mode - /// - Bytes read from (read) buffer - /// - New length greater than client position and greater than server position - /// - [TestClass] - public class SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition : SftpFileStreamTestBase - { - private string _path; - private SftpFileStream _sftpFileStream; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private MockSequence _sequence; - private long _length; - - private SftpFileAttributes _fileAttributes; - private SftpFileAttributes _originalFileAttributes; - private SftpFileAttributes _newFileAttributes; - private byte[] _readBytes1; - private byte[] _readBytes2; - private byte[] _actualReadBytes; - - protected override void SetupData() - { - var random = new Random(); - - _path = random.Next().ToString(CultureInfo.InvariantCulture); - _handle = GenerateRandom(random.Next(2, 6), random); - _bufferSize = (uint)random.Next(1, 1000); - _readBytes1 = new byte[5]; - _readBytes2 = new byte[random.Next(1, 3)]; - _actualReadBytes = GenerateRandom(_readBytes1.Length + _readBytes2.Length + 2, random); // server returns more bytes than the caller requested - _readBufferSize = (uint)random.Next(_actualReadBytes.Length, _actualReadBytes.Length * 2); - _writeBufferSize = (uint)random.Next(100, 1000); - _length = _readBytes1.Length + _readBytes2.Length + 5; - - _fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC") - .WithExtension("V", "VValue") - .WithGroupId(random.Next()) - .WithLastAccessTime(DateTime.UtcNow.AddSeconds(random.Next())) - .WithLastWriteTime(DateTime.UtcNow.AddSeconds(random.Next())) - .WithPermissions((uint)random.Next()) - .WithSize(_length + 100) - .WithUserId(random.Next()) - .Build(); - _originalFileAttributes = _fileAttributes.Clone(); - _newFileAttributes = null; - } - - protected override void SetupMocks() - { - _sequence = new MockSequence(); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false)) - .Returns(_handle); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestRead(_handle, 0, _readBufferSize)) - .Returns(_actualReadBytes); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestFStat(_handle, false)) - .Returns(_fileAttributes); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestFSetStat(_handle, _fileAttributes)) - .Callback((bytes, attributes) => _newFileAttributes = attributes.Clone()); - } - - protected override void Arrange() - { - base.Arrange(); - - _sftpFileStream = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.ReadWrite, - (int)_bufferSize); - int readBytesCount1 = _sftpFileStream.Read(_readBytes1, 0, _readBytes1.Length); - Assert.AreEqual(_readBytes1.Length, readBytesCount1); - int readBytesCount2 = _sftpFileStream.Read(_readBytes2, 0, _readBytes2.Length); // this will return bytes from the buffer - Assert.AreEqual(_readBytes2.Length, readBytesCount2); - } - - protected override void Act() - { - _sftpFileStream.SetLength(_length); - } - - [TestMethod] - public void PositionShouldReturnSamePositionAsBeforeSetLength() - { - SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(_readBytes1.Length + _readBytes2.Length, _sftpFileStream.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); - } - - [TestMethod] - public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once); - } - - [TestMethod] - public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat() - { - DictionaryAssert.AreEqual(_originalFileAttributes.Extensions, _newFileAttributes.Extensions); - Assert.AreEqual(_originalFileAttributes.GroupId, _newFileAttributes.GroupId); - Assert.AreEqual(_originalFileAttributes.LastAccessTime, _newFileAttributes.LastAccessTime); - Assert.AreEqual(_originalFileAttributes.LastWriteTime, _newFileAttributes.LastWriteTime); - Assert.AreEqual(_originalFileAttributes.Permissions, _newFileAttributes.Permissions); - Assert.AreEqual(_originalFileAttributes.UserId, _newFileAttributes.UserId); - - Assert.AreEqual(_length, _newFileAttributes.Size); - } - - [TestMethod] - public void ReadShouldReadStartFromSamePositionAsBeforeSetLength() - { - SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestRead(_handle, - (uint)(_readBytes1.Length + _readBytes2.Length), - _readBufferSize)) - .Returns(new byte[] { 0x0f }); - - var byteRead = _sftpFileStream.ReadByte(); - - Assert.AreEqual(0x0f, byteRead); - - SftpSessionMock.Verify(p => p.RequestRead(_handle, - (uint)(_readBytes1.Length + _readBytes2.Length), - _readBufferSize), - Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); - } - - [TestMethod] - public void WriteShouldStartFromSamePositionAsBeforeSetLength() - { - var bytesToWrite = GenerateRandom(_writeBufferSize); - byte[] bytesWritten = null; - - SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestWrite(_handle, - (uint)(_readBytes1.Length + _readBytes2.Length), - It.IsAny(), - 0, - bytesToWrite.Length, - It.IsAny(), - null)) - .Callback>( - (handle, serverOffset, data, offset, length, wait, writeCompleted) => - { - bytesWritten = data.Take(offset, length); - wait.Set(); - }); - - _sftpFileStream.Write(bytesToWrite, 0, bytesToWrite.Length); - - Assert.IsNotNull(bytesWritten); - CollectionAssert.AreEqual(bytesToWrite, bytesWritten); - - SftpSessionMock.Verify(p => p.RequestWrite(_handle, - (uint)(_readBytes1.Length + _readBytes2.Length), - It.IsAny(), - 0, - bytesToWrite.Length, - It.IsAny(), - null), - Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthLessThanPosition.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthLessThanPosition.cs deleted file mode 100644 index 25d4bf854..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthLessThanPosition.cs +++ /dev/null @@ -1,178 +0,0 @@ -using System; -using System.Globalization; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Sftp.Responses; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - /// - /// - In read mode - /// - Bytes in (read) buffer, but not read from - /// - New length less than client position and less than server position - /// - [TestClass] - public class SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthLessThanPosition : SftpFileStreamTestBase - { - private string _path; - private SftpFileStream _sftpFileStream; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private MockSequence _sequence; - private long _length; - - private SftpFileAttributes _fileAttributes; - private SftpFileAttributes _originalFileAttributes; - private SftpFileAttributes _newFileAttributes; - private byte[] _readBytes; - private byte[] _actualReadBytes; - - protected override void SetupData() - { - var random = new Random(); - - _path = random.Next().ToString(CultureInfo.InvariantCulture); - _handle = GenerateRandom(random.Next(2, 6), random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(100, 1000); - _readBytes = new byte[5]; - _actualReadBytes = GenerateRandom(_readBytes.Length + 2, random); // add 2 bytes in read buffer - _length = _readBytes.Length - 2; - - _fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC") - .WithExtension("V", "VValue") - .WithGroupId(random.Next()) - .WithLastAccessTime(DateTime.UtcNow.AddSeconds(random.Next())) - .WithLastWriteTime(DateTime.UtcNow.AddSeconds(random.Next())) - .WithPermissions((uint)random.Next()) - .WithSize(_length + 100) - .WithUserId(random.Next()) - .Build(); - _originalFileAttributes = _fileAttributes.Clone(); - _newFileAttributes = null; - } - - protected override void SetupMocks() - { - _sequence = new MockSequence(); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false)) - .Returns(_handle); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestRead(_handle, 0, _readBufferSize)) - .Returns(_actualReadBytes); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestFStat(_handle, false)) - .Returns(_fileAttributes); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestFSetStat(_handle, _fileAttributes)) - .Callback((bytes, attributes) => _newFileAttributes = attributes.Clone()); - } - - protected override void Arrange() - { - base.Arrange(); - - _sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize); - int readBytesCount = _sftpFileStream.Read(_readBytes, 0, _readBytes.Length); - Assert.AreEqual(_readBytes.Length, readBytesCount); - } - - protected override void Act() - { - _sftpFileStream.SetLength(_length); - } - - [TestMethod] - public void PositionShouldReturnLengthOfStream() - { - SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(_length, _sftpFileStream.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - } - - [TestMethod] - public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once); - } - - [TestMethod] - public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat() - { - DictionaryAssert.AreEqual(_originalFileAttributes.Extensions, _newFileAttributes.Extensions); - Assert.AreEqual(_originalFileAttributes.GroupId, _newFileAttributes.GroupId); - Assert.AreEqual(_originalFileAttributes.LastAccessTime, _newFileAttributes.LastAccessTime); - Assert.AreEqual(_originalFileAttributes.LastWriteTime, _newFileAttributes.LastWriteTime); - Assert.AreEqual(_originalFileAttributes.Permissions, _newFileAttributes.Permissions); - Assert.AreEqual(_originalFileAttributes.UserId, _newFileAttributes.UserId); - - Assert.AreEqual(_length, _newFileAttributes.Size); - } - - [TestMethod] - public void ReadShouldStartFromEndOfStream() - { - SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestRead(_handle, (uint)_length, _readBufferSize)) - .Returns(Array.Empty()); - - var byteRead = _sftpFileStream.ReadByte(); - - Assert.AreEqual(-1, byteRead); - - SftpSessionMock.Verify(p => p.RequestRead(_handle, (uint)_length, _readBufferSize), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - } - - [TestMethod] - public void WriteShouldStartFromEndOfStream() - { - var bytesToWrite = GenerateRandom(_writeBufferSize); - byte[] bytesWritten = null; - - SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestWrite(_handle, (uint)_length, It.IsAny(), 0, bytesToWrite.Length, It.IsAny(), null)) - .Callback>((handle, serverOffset, data, offset, length, wait, writeCompleted) => - { - bytesWritten = data.Take(offset, length); - wait.Set(); - }); - - _sftpFileStream.Write(bytesToWrite, 0, bytesToWrite.Length); - - Assert.IsNotNull(bytesWritten); - CollectionAssert.AreEqual(bytesToWrite, bytesWritten); - - SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint)_length, It.IsAny(), 0, bytesToWrite.Length, It.IsAny(), null), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInWriteBuffer_NewLengthGreatherThanPosition.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInWriteBuffer_NewLengthGreatherThanPosition.cs deleted file mode 100644 index 90a89dde0..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInWriteBuffer_NewLengthGreatherThanPosition.cs +++ /dev/null @@ -1,201 +0,0 @@ -using System; -using System.Globalization; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Sftp.Responses; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - /// - /// - In write mode - /// - Bytes in (write) buffer - /// - New length greater than client position and greater than server position - /// - [TestClass] - public class SftpFileStreamTest_SetLength_DataInWriteBuffer_NewLengthGreaterThanPosition : SftpFileStreamTestBase - { - private string _path; - private SftpFileStream _sftpFileStream; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private MockSequence _sequence; - private long _length; - - private SftpFileAttributes _fileAttributes; - private SftpFileAttributes _originalFileAttributes; - private SftpFileAttributes _newFileAttributes; - private byte[] _readBytes; - private byte[] _actualReadBytes; - private byte[] _writeBytes; - private byte[] _actualWrittenBytes; - - protected override void SetupData() - { - var random = new Random(); - - _path = random.Next().ToString(CultureInfo.InvariantCulture); - _handle = GenerateRandom(random.Next(2, 6), random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(100, 1000); - _readBytes = new byte[5]; - _actualReadBytes = GenerateRandom(_readBytes.Length, random); - _writeBytes = new byte[] { 0x01, 0x02, 0x03, 0x04 }; - _length = _readBytes.Length + _writeBytes.Length + 2; - _actualWrittenBytes = null; - - _fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC") - .WithExtension("V", "VValue") - .WithGroupId(random.Next()) - .WithLastAccessTime(DateTime.UtcNow.AddSeconds(random.Next())) - .WithLastWriteTime(DateTime.UtcNow.AddSeconds(random.Next())) - .WithPermissions((uint)random.Next()) - .WithSize(_length + 100) - .WithUserId(random.Next()) - .Build(); - _originalFileAttributes = _fileAttributes.Clone(); - _newFileAttributes = null; - } - - protected override void SetupMocks() - { - _sequence = new MockSequence(); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false)) - .Returns(_handle); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestRead(_handle, 0, _readBufferSize)) - .Returns(_actualReadBytes); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestWrite(_handle, (uint)_readBytes.Length, It.IsAny(), 0, _writeBytes.Length, It.IsAny(), null)) - .Callback>((handle, serverOffset, data, offset, length, wait, writeCompleted) - => - { - _actualWrittenBytes = data.Take(0, _writeBytes.Length); - wait.Set(); - }); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestFStat(_handle, false)) - .Returns(_fileAttributes); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestFSetStat(_handle, _fileAttributes)) - .Callback((bytes, attributes) => _newFileAttributes = attributes.Clone()); - } - - protected override void Arrange() - { - base.Arrange(); - - _sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize); - int readBytesCount = _sftpFileStream.Read(_readBytes, 0, _readBytes.Length); - Assert.AreEqual(_readBytes.Length, readBytesCount); - _sftpFileStream.Write(new byte[] { 0x01, 0x02, 0x03, 0x04 }, 0, 4); - } - - protected override void Act() - { - _sftpFileStream.SetLength(_length); - } - - [TestMethod] - public void PositionShouldReturnSamePositionAsBeforeSetLength() - { - SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(_readBytes.Length + _writeBytes.Length, _sftpFileStream.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); - } - - [TestMethod] - public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once); - } - - [TestMethod] - public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat() - { - DictionaryAssert.AreEqual(_originalFileAttributes.Extensions, _newFileAttributes.Extensions); - Assert.AreEqual(_originalFileAttributes.GroupId, _newFileAttributes.GroupId); - Assert.AreEqual(_originalFileAttributes.LastAccessTime, _newFileAttributes.LastAccessTime); - Assert.AreEqual(_originalFileAttributes.LastWriteTime, _newFileAttributes.LastWriteTime); - Assert.AreEqual(_originalFileAttributes.Permissions, _newFileAttributes.Permissions); - Assert.AreEqual(_originalFileAttributes.UserId, _newFileAttributes.UserId); - - Assert.AreEqual(_length, _newFileAttributes.Size); - } - - [TestMethod] - public void WrittenBytesShouldByFlushedToServer() - { - Assert.IsNotNull(_actualWrittenBytes); - CollectionAssert.AreEqual(_writeBytes, _actualWrittenBytes); - } - - [TestMethod] - public void ReadShouldReadStartFromSamePositionAsBeforeSetLength() - { - SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestRead(_handle, (uint)(_readBytes.Length + _writeBytes.Length), _readBufferSize)) - .Returns(new byte[] { 0x0f }); - - var byteRead = _sftpFileStream.ReadByte(); - - Assert.AreEqual(0x0f, byteRead); - - SftpSessionMock.Verify(p => p.RequestRead(_handle, (uint)(_readBytes.Length + _writeBytes.Length), _readBufferSize), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); - } - - [TestMethod] - public void WriteShouldStartFromSamePositionAsBeforeSetLength() - { - var bytesToWrite = GenerateRandom(_writeBufferSize); - byte[] bytesWritten = null; - - SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestWrite(_handle, (uint)(_readBytes.Length + _writeBytes.Length), It.IsAny(), 0, bytesToWrite.Length, It.IsAny(), null)) - .Callback>((handle, serverOffset, data, offset, length, wait, writeCompleted) => - { - bytesWritten = data.Take(offset, length); - wait.Set(); - }); - - _sftpFileStream.Write(bytesToWrite, 0, bytesToWrite.Length); - - Assert.IsNotNull(bytesWritten); - CollectionAssert.AreEqual(bytesToWrite, bytesWritten); - - SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint)(_readBytes.Length + _writeBytes.Length), It.IsAny(), 0, bytesToWrite.Length, It.IsAny(), null), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInWriteBuffer_NewLengthLessThanPosition.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInWriteBuffer_NewLengthLessThanPosition.cs deleted file mode 100644 index 719b727ef..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInWriteBuffer_NewLengthLessThanPosition.cs +++ /dev/null @@ -1,201 +0,0 @@ -using System; -using System.Globalization; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Sftp.Responses; -using Renci.SshNet.Tests.Common; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - /// - /// - In write mode - /// - Bytes in (write) buffer - /// - New length less than client position and less than server position - /// - [TestClass] - public class SftpFileStreamTest_SetLength_DataInWriteBuffer_NewLengthLessThanPosition : SftpFileStreamTestBase - { - private string _path; - private SftpFileStream _sftpFileStream; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private MockSequence _sequence; - private long _length; - - private SftpFileAttributes _fileAttributes; - private SftpFileAttributes _originalFileAttributes; - private SftpFileAttributes _newFileAttributes; - private byte[] _readBytes; - private byte[] _actualReadBytes; - private byte[] _writeBytes; - private byte[] _actualWrittenBytes; - - protected override void SetupData() - { - var random = new Random(); - - _path = random.Next().ToString(CultureInfo.InvariantCulture); - _handle = GenerateRandom(random.Next(2, 6), random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(100, 1000); - _readBytes = new byte[5]; - _actualReadBytes = GenerateRandom(_readBytes.Length, random); - _writeBytes = new byte[] { 0x01, 0x02, 0x03, 0x04 }; - _length = _readBytes.Length - 2; - _actualWrittenBytes = null; - - _fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC") - .WithExtension("V", "VValue") - .WithGroupId(random.Next()) - .WithLastAccessTime(DateTime.UtcNow.AddSeconds(random.Next())) - .WithLastWriteTime(DateTime.UtcNow.AddSeconds(random.Next())) - .WithPermissions((uint)random.Next()) - .WithSize(_length + 100) - .WithUserId(random.Next()) - .Build(); - _originalFileAttributes = _fileAttributes.Clone(); - _newFileAttributes = null; - } - - protected override void SetupMocks() - { - _sequence = new MockSequence(); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false)) - .Returns(_handle); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestRead(_handle, 0, _readBufferSize)) - .Returns(_actualReadBytes); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestWrite(_handle, (uint)_readBytes.Length, It.IsAny(), 0, _writeBytes.Length, It.IsAny(), null)) - .Callback>((handle, serverOffset, data, offset, length, wait, writeCompleted) - => - { - _actualWrittenBytes = data.Take(offset, length); - wait.Set(); - }); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestFStat(_handle, false)) - .Returns(_fileAttributes); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestFSetStat(_handle, _fileAttributes)) - .Callback((bytes, attributes) => _newFileAttributes = attributes.Clone()); - } - - protected override void Arrange() - { - base.Arrange(); - - _sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize); - int readBytesCount = _sftpFileStream.Read(_readBytes, 0, _readBytes.Length); - Assert.AreEqual(_readBytes.Length, readBytesCount); - _sftpFileStream.Write(new byte[] { 0x01, 0x02, 0x03, 0x04 }, 0, 4); - } - - protected override void Act() - { - _sftpFileStream.SetLength(_length); - } - - [TestMethod] - public void PositionShouldReturnLengthOfStream() - { - SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(_length, _sftpFileStream.Position); - - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); - } - - [TestMethod] - public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce() - { - SftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once); - } - - [TestMethod] - public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat() - { - DictionaryAssert.AreEqual(_originalFileAttributes.Extensions, _newFileAttributes.Extensions); - Assert.AreEqual(_originalFileAttributes.GroupId, _newFileAttributes.GroupId); - Assert.AreEqual(_originalFileAttributes.LastAccessTime, _newFileAttributes.LastAccessTime); - Assert.AreEqual(_originalFileAttributes.LastWriteTime, _newFileAttributes.LastWriteTime); - Assert.AreEqual(_originalFileAttributes.Permissions, _newFileAttributes.Permissions); - Assert.AreEqual(_originalFileAttributes.UserId, _newFileAttributes.UserId); - - Assert.AreEqual(_length, _newFileAttributes.Size); - } - - [TestMethod] - public void WrittenBytesShouldByFlushedToServer() - { - Assert.IsNotNull(_actualWrittenBytes); - CollectionAssert.AreEqual(_writeBytes, _actualWrittenBytes); - } - - [TestMethod] - public void ReadShouldStartFromEndOfStream() - { - SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestRead(_handle, (uint)_length, _readBufferSize)) - .Returns(Array.Empty()); - - var byteRead = _sftpFileStream.ReadByte(); - - Assert.AreEqual(-1, byteRead); - - SftpSessionMock.Verify(p => p.RequestRead(_handle, (uint)_length, _readBufferSize), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); - } - - [TestMethod] - public void WriteShouldStartFromEndOfStream() - { - var bytesToWrite = GenerateRandom(_writeBufferSize); - byte[] bytesWritten = null; - - SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestWrite(_handle, (uint)_length, It.IsAny(), 0, bytesToWrite.Length, It.IsAny(), null)) - .Callback>((handle, serverOffset, data, offset, length, wait, writeCompleted) => - { - bytesWritten = data.Take(offset, length); - wait.Set(); - }); - - _sftpFileStream.Write(bytesToWrite, 0, bytesToWrite.Length); - - Assert.IsNotNull(bytesWritten); - CollectionAssert.AreEqual(bytesToWrite, bytesWritten); - - SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint)_length, It.IsAny(), 0, bytesToWrite.Length, It.IsAny(), null), Times.Once); - SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_Disposed.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_Disposed.cs deleted file mode 100644 index 8b588b1e5..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_Disposed.cs +++ /dev/null @@ -1,89 +0,0 @@ -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_SetLength_Disposed - { - private Mock _sftpSessionMock; - private string _path; - private SftpFileStream _sftpFileStream; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private ObjectDisposedException _actualException; - - [TestInitialize] - public void Setup() - { - Arrange(); - Act(); - } - - protected void Arrange() - { - var random = new Random(); - _path = random.Next().ToString(); - _handle = new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) }; - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - - _sftpSessionMock = new Mock(MockBehavior.Strict); - - var sequence = new MockSequence(); - _sftpSessionMock.InSequence(sequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true)) - .Returns(_handle); - _sftpSessionMock.InSequence(sequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _sftpSessionMock.InSequence(sequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _sftpSessionMock.InSequence(sequence) - .Setup(p => p.IsOpen) - .Returns(true); - _sftpSessionMock.InSequence(sequence) - .Setup(p => p.RequestClose(_handle)); - - _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize); - _sftpFileStream.Dispose(); - } - - protected void Act() - { - try - { - _sftpFileStream.SetLength(5); - Assert.Fail(); - } - catch (ObjectDisposedException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void SetLengthShouldHaveThrownObjectDisposedException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - Assert.AreEqual( - string.Format( - "Cannot access a disposed object.{0}Object name: '{1}'.", - Environment.NewLine, - _actualException.ObjectName), - _actualException.Message); - Assert.AreEqual(typeof(SftpFileStream).FullName, _actualException.ObjectName); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionNotOpen.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionNotOpen.cs deleted file mode 100644 index 626509ea5..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionNotOpen.cs +++ /dev/null @@ -1,90 +0,0 @@ -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_SetLength_SessionNotOpen : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private long _length; - private ObjectDisposedException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(4, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - _length = 5555; - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(false); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - } - - protected override void Act() - { - try - { - _target.SetLength(_length); - Assert.Fail(); - } - catch (ObjectDisposedException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void SetLengthShouldHaveThrownObjectDisposedException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - Assert.AreEqual( - string.Format( - "Cannot access a closed SFTP session.{0}Object name: '{1}'.", - Environment.NewLine, - _actualException.ObjectName), - _actualException.Message); - Assert.AreEqual(typeof(SftpFileStream).FullName, _actualException.ObjectName); - } - - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionOpen_FileAccessRead.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionOpen_FileAccessRead.cs deleted file mode 100644 index 993c15b6b..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionOpen_FileAccessRead.cs +++ /dev/null @@ -1,83 +0,0 @@ -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_SetLength_SessionOpen_FileAccessRead : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private long _length; - private NotSupportedException _actualException; - - protected override void SetupData() - { - base.SetupData(); - - var random = new Random(); - _path = random.Next().ToString(); - _handle = GenerateRandom(5, random); - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - _length = 6666; - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Read, false)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Open, - FileAccess.Read, - (int)_bufferSize); - } - - protected override void Act() - { - try - { - _target.SetLength(_length); - Assert.Fail(); - } - catch (NotSupportedException ex) - { - _actualException = ex; - } - } - - [TestMethod] - public void SetLengthShouldHaveThrownNotSupportedException() - { - Assert.IsNotNull(_actualException); - Assert.IsNull(_actualException.InnerException); - Assert.AreEqual("Write not supported.", _actualException.Message); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionOpen_FileAccessReadWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionOpen_FileAccessReadWrite.cs deleted file mode 100644 index 0e65bf453..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionOpen_FileAccessReadWrite.cs +++ /dev/null @@ -1,127 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_SetLength_SessionOpen_FileAccessReadWrite - { - private Mock _sftpSessionMock; - private string _path; - private SftpFileStream _sftpFileStream; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private MockSequence _sequence; - private long _length; - private long _lengthPassedToRequestFSetStat; - - private DateTime _fileAttributesLastAccessTime; - private DateTime _fileAttributesLastWriteTime; - private long _fileAttributesSize; - private int _fileAttributesUserId; - private int _fileAttributesGroupId; - private uint _fileAttributesPermissions; - private IDictionary _fileAttributesExtensions; - private SftpFileAttributes _fileAttributes; - - [TestInitialize] - public void Setup() - { - Arrange(); - Act(); - } - - protected void Arrange() - { - var random = new Random(); - _path = random.Next().ToString(CultureInfo.InvariantCulture); - _handle = new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) }; - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - _length = 7777; - - _fileAttributesLastAccessTime = DateTime.UtcNow.AddSeconds(random.Next()); - _fileAttributesLastWriteTime = DateTime.UtcNow.AddSeconds(random.Next()); - _fileAttributesSize = random.Next(); - _fileAttributesUserId = random.Next(); - _fileAttributesGroupId = random.Next(); - _fileAttributesPermissions = (uint)random.Next(); - _fileAttributesExtensions = new Dictionary(); - _fileAttributes = new SftpFileAttributes(_fileAttributesLastAccessTime, - _fileAttributesLastWriteTime, - _fileAttributesSize, - _fileAttributesUserId, - _fileAttributesGroupId, - _fileAttributesPermissions, - _fileAttributesExtensions); - - _sftpSessionMock = new Mock(MockBehavior.Strict); - - _sequence = new MockSequence(); - _sftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, true)) - .Returns(_handle); - _sftpSessionMock.InSequence(_sequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _sftpSessionMock.InSequence(_sequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _sftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - _sftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestFStat(_handle, false)) - .Returns(_fileAttributes); - _sftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestFSetStat(_handle, _fileAttributes)) - .Callback((bytes, attributes) => _lengthPassedToRequestFSetStat = attributes.Size); - - _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.ReadWrite, (int)_bufferSize); - } - - protected void Act() - { - _sftpFileStream.SetLength(_length); - } - - [TestMethod] - public void PositionShouldReturnOriginalPosition() - { - _sftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(0, _sftpFileStream.Position); - - _sftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce() - { - _sftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once); - } - - [TestMethod] - public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat() - { - Assert.AreEqual(_length, _lengthPassedToRequestFSetStat); - } - - [TestMethod] - public void SizeOfSftpFileAttributesShouldBeEqualToNewLength() - { - Assert.AreEqual(_length, _fileAttributes.Size); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionOpen_FileAccessWrite.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionOpen_FileAccessWrite.cs deleted file mode 100644 index ec896d273..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionOpen_FileAccessWrite.cs +++ /dev/null @@ -1,127 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_SetLength_SessionOpen_FileAccessWrite - { - private Mock _sftpSessionMock; - private string _path; - private SftpFileStream _sftpFileStream; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private MockSequence _sequence; - private long _length; - private long _lengthPassedToRequestFSetStat; - - private DateTime _fileAttributesLastAccessTime; - private DateTime _fileAttributesLastWriteTime; - private long _fileAttributesSize; - private int _fileAttributesUserId; - private int _fileAttributesGroupId; - private uint _fileAttributesPermissions; - private IDictionary _fileAttributesExtensions; - private SftpFileAttributes _fileAttributes; - - [TestInitialize] - public void Setup() - { - Arrange(); - Act(); - } - - protected void Arrange() - { - var random = new Random(); - _path = random.Next().ToString(CultureInfo.InvariantCulture); - _handle = new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) }; - _bufferSize = (uint)random.Next(1, 1000); - _readBufferSize = (uint)random.Next(1, 1000); - _writeBufferSize = (uint)random.Next(1, 1000); - _length = 8888; - - _fileAttributesLastAccessTime = DateTime.UtcNow.AddSeconds(random.Next()); - _fileAttributesLastWriteTime = DateTime.UtcNow.AddSeconds(random.Next()); - _fileAttributesSize = random.Next(); - _fileAttributesUserId = random.Next(); - _fileAttributesGroupId = random.Next(); - _fileAttributesPermissions = (uint)random.Next(); - _fileAttributesExtensions = new Dictionary(); - _fileAttributes = new SftpFileAttributes(_fileAttributesLastAccessTime, - _fileAttributesLastWriteTime, - _fileAttributesSize, - _fileAttributesUserId, - _fileAttributesGroupId, - _fileAttributesPermissions, - _fileAttributesExtensions); - - _sftpSessionMock = new Mock(MockBehavior.Strict); - - _sequence = new MockSequence(); - _sftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true)) - .Returns(_handle); - _sftpSessionMock.InSequence(_sequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - _sftpSessionMock.InSequence(_sequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - _sftpSessionMock.InSequence(_sequence) - .Setup(p => p.IsOpen) - .Returns(true); - _sftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestFStat(_handle, false)) - .Returns(_fileAttributes); - _sftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestFSetStat(_handle, _fileAttributes)) - .Callback((bytes, attributes) => _lengthPassedToRequestFSetStat = attributes.Size); - - _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize); - } - - protected void Act() - { - _sftpFileStream.SetLength(_length); - } - - [TestMethod] - public void PositionShouldReturnOriginalPosition() - { - _sftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(0, _sftpFileStream.Position); - - _sftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); - } - - [TestMethod] - public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce() - { - _sftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once); - } - - [TestMethod] - public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat() - { - Assert.AreEqual(_length, _lengthPassedToRequestFSetStat); - } - - [TestMethod] - public void SizeOfSftpFileAttributesShouldBeEqualToNewLength() - { - Assert.AreEqual(_length, _fileAttributes.Size); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_WriteAsync_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_WriteAsync_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize.cs deleted file mode 100644 index 8da36ddc2..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_WriteAsync_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize.cs +++ /dev/null @@ -1,143 +0,0 @@ -using System; -using System.Globalization; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_WriteAsync_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize : SftpFileStreamAsyncTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private byte[] _data; - private int _count; - private int _offset; - private Random _random; - private uint _expectedWrittenByteCount; - private int _expectedBufferedByteCount; - private byte[] _expectedBufferedBytes; - private CancellationToken _cancellationToken; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(CultureInfo.InvariantCulture); - _handle = GenerateRandom(5, _random); - _bufferSize = (uint)_random.Next(1, 1000); - _readBufferSize = (uint)_random.Next(0, 1000); - _writeBufferSize = (uint)_random.Next(500, 1000); - _data = new byte[(_writeBufferSize * 2) + 15]; - _random.NextBytes(_data); - _offset = _random.Next(1, 5); - // to get multiple SSH_FXP_WRITE messages (and verify the offset is updated correctly), we make sure - // the number of bytes to write is at least two times the write buffer size; we write a few extra bytes to - // ensure the buffer is not empty after the writes so we can verify whether Length, Dispose and Flush - // flush the buffer - _count = ((int)_writeBufferSize * 2) + _random.Next(1, 5); - - _expectedWrittenByteCount = (2 * _writeBufferSize); - _expectedBufferedByteCount = (int)(_count - _expectedWrittenByteCount); - _expectedBufferedBytes = _data.Take(_offset + (int)_expectedWrittenByteCount, _expectedBufferedByteCount); - _cancellationToken = new CancellationToken(); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpenAsync(_path, Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate, _cancellationToken)) - .ReturnsAsync(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWriteAsync(_handle, 0, _data, _offset, (int)_writeBufferSize, _cancellationToken)) - .Returns(Task.CompletedTask); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWriteAsync(_handle, _writeBufferSize, _data, _offset + (int)_writeBufferSize, (int)_writeBufferSize, _cancellationToken)) - .Returns(Task.CompletedTask); - } - - [TestCleanup] - public void TearDown() - { - if (SftpSessionMock != null) - { - // allow Dispose to complete successfully - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWriteAsync(_handle, _expectedWrittenByteCount, It.IsAny(), 0, _expectedBufferedByteCount, _cancellationToken)) - .Returns(Task.CompletedTask); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - } - - protected override async Task ArrangeAsync() - { - await base.ArrangeAsync(); - - _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize, _cancellationToken); - } - - protected override Task ActAsync() - { - return _target.WriteAsync(_data, _offset, _count); - } - - [TestMethod] - public void RequestWriteOnSftpSessionShouldBeInvokedTwice() - { - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0, _data, _offset, (int)_writeBufferSize, _cancellationToken), Times.Once); - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, _writeBufferSize, _data, _offset + (int)_writeBufferSize, (int)_writeBufferSize, _cancellationToken), Times.Once); - } - - [TestMethod] - public void PositionShouldBeNumberOfBytesWrittenToFileAndNUmberOfBytesInBuffer() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(_count, _target.Position); - } - - [TestMethod] - public async Task FlushShouldFlushBuffer() - { - byte[] actualFlushedData = null; - - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWriteAsync(_handle, _expectedWrittenByteCount, It.IsAny(), 0, _expectedBufferedByteCount, _cancellationToken)) - .Callback((handle, serverFileOffset, data, offset, length, ct) => actualFlushedData = data.Take(offset, length)) - .Returns(Task.CompletedTask); - - await _target.FlushAsync(); - - Assert.IsTrue(actualFlushedData.IsEqualTo(_expectedBufferedBytes)); - - SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, _expectedWrittenByteCount, It.IsAny(), 0, _expectedBufferedByteCount, _cancellationToken), Times.Once); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Write_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize.cs b/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Write_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize.cs deleted file mode 100644 index 0bb710193..000000000 --- a/test/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Write_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize.cs +++ /dev/null @@ -1,223 +0,0 @@ -using System; -using System.Globalization; -using System.IO; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Sftp.Responses; - -namespace Renci.SshNet.Tests.Classes.Sftp -{ - [TestClass] - public class SftpFileStreamTest_Write_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize : SftpFileStreamTestBase - { - private SftpFileStream _target; - private string _path; - private byte[] _handle; - private uint _bufferSize; - private uint _readBufferSize; - private uint _writeBufferSize; - private byte[] _data; - private int _count; - private int _offset; - private Random _random; - private uint _expectedWrittenByteCount; - private int _expectedBufferedByteCount; - private byte[] _expectedBufferedBytes; - - protected override void SetupData() - { - base.SetupData(); - - _random = new Random(); - _path = _random.Next().ToString(CultureInfo.InvariantCulture); - _handle = GenerateRandom(5, _random); - _bufferSize = (uint)_random.Next(1, 1000); - _readBufferSize = (uint)_random.Next(0, 1000); - _writeBufferSize = (uint)_random.Next(500, 1000); - _data = new byte[(_writeBufferSize * 2) + 15]; - _random.NextBytes(_data); - _offset = _random.Next(1, 5); - // to get multiple SSH_FXP_WRITE messages (and verify the offset is updated correctly), we make sure - // the number of bytes to write is at least two times the write buffer size; we write a few extra bytes to - // ensure the buffer is not empty after the writes so we can verify whether Length, Dispose and Flush - // flush the buffer - _count = ((int)_writeBufferSize * 2) + _random.Next(1, 5); - - _expectedWrittenByteCount = (2 * _writeBufferSize); - _expectedBufferedByteCount = (int)(_count - _expectedWrittenByteCount); - _expectedBufferedBytes = _data.Take(_offset + (int)_expectedWrittenByteCount, _expectedBufferedByteCount); - } - - protected override void SetupMocks() - { - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true)) - .Returns(_handle); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) - .Returns(_readBufferSize); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) - .Returns(_writeBufferSize); - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, 0, _data, _offset, (int)_writeBufferSize, It.IsAny(), null)); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, _writeBufferSize, _data, _offset + (int)_writeBufferSize, (int)_writeBufferSize, It.IsAny(), null)); - } - - [TestCleanup] - public void TearDown() - { - if (SftpSessionMock != null) - { - // allow Dispose to complete successfully - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny(), 0, _expectedBufferedByteCount, It.IsAny(), null)); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - } - } - - protected override void Arrange() - { - base.Arrange(); - - _target = new SftpFileStream(SftpSessionMock.Object, - _path, - FileMode.Create, - FileAccess.Write, - (int)_bufferSize); - } - - protected override void Act() - { - _target.Write(_data, _offset, _count); - } - - [TestMethod] - public void RequestWriteOnSftpSessionShouldBeInvokedTwice() - { - SftpSessionMock.Verify(p => p.RequestWrite(_handle, 0, _data, _offset, (int)_writeBufferSize, It.IsAny(), null), Times.Once); - SftpSessionMock.Verify(p => p.RequestWrite(_handle, _writeBufferSize, _data, _offset + (int)_writeBufferSize, (int)_writeBufferSize, It.IsAny(), null), Times.Once); - } - - [TestMethod] - public void PositionShouldBeNumberOfBytesWrittenToFileAndNUmberOfBytesInBuffer() - { - SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); - - Assert.AreEqual(_count, _target.Position); - } - - [TestMethod] - public void LengthShouldFlushBufferAndReturnSizeOfFile() - { - var lengthFileAttributes = new SftpFileAttributes(DateTime.UtcNow, - DateTime.UtcNow, - 123, - 456, - 789, - 7, - null); - byte[] actualFlushedData = null; - - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny(), 0, _expectedBufferedByteCount, It.IsAny(), null)) - .Callback>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length)); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestFStat(_handle, true)) - .Returns(lengthFileAttributes); - - Assert.AreEqual(lengthFileAttributes.Size, _target.Length); - Assert.IsTrue(actualFlushedData.IsEqualTo(_expectedBufferedBytes)); - - SftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny(), 0, _expectedBufferedByteCount, It.IsAny(), null), Times.Once); - } - - [TestMethod] - public void LengthShouldThrowIOExceptionIfRequestFStatReturnsNull() - { - const SftpFileAttributes lengthFileAttributes = null; - byte[] actualFlushedData = null; - - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny(), 0, _expectedBufferedByteCount, It.IsAny(), null)) - .Callback>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length)); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestFStat(_handle, true)) - .Returns(lengthFileAttributes); - - try - { - var length = _target.Length; - Assert.Fail("Length should have failed, but returned: " + length + "."); - } - catch (IOException ex) - { - Assert.IsNull(ex.InnerException); - Assert.AreEqual("Seek operation failed.", ex.Message); - } - - Assert.IsTrue(actualFlushedData.IsEqualTo(_expectedBufferedBytes)); - - SftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny(), 0, _expectedBufferedByteCount, It.IsAny(), null), Times.Once); - } - - [TestMethod] - public void DisposeShouldFlushBufferAndCloseRequest() - { - byte[] actualFlushedData = null; - - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny(), 0, _expectedBufferedByteCount, It.IsAny(), null)) - .Callback>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length)); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestClose(_handle)); - - _target.Dispose(); - - Assert.IsTrue(actualFlushedData.IsEqualTo(_expectedBufferedBytes)); - - SftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny(), 0, _expectedBufferedByteCount, It.IsAny(), null), Times.Once); - SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once); - } - - [TestMethod] - public void FlushShouldFlushBuffer() - { - byte[] actualFlushedData = null; - - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.IsOpen) - .Returns(true); - SftpSessionMock.InSequence(MockSequence) - .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny(), 0, _expectedBufferedByteCount, It.IsAny(), null)) - .Callback>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length)); - - _target.Flush(); - - Assert.IsTrue(actualFlushedData.IsEqualTo(_expectedBufferedBytes)); - - SftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny(), 0, _expectedBufferedByteCount, It.IsAny(), null), Times.Once); - } - } -}