소스 검색

Update tests now that we reject invalid combination of FileMode and FileAccess.
Remove duplicate tests.

Gert Driesen 8 년 전
부모
커밋
14b571fdd4
49개의 변경된 파일1102개의 추가작업 그리고 1496개의 파일을 삭제
  1. 35 33
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessRead.cs
  2. 35 33
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessReadWrite.cs
  3. 35 33
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessWrite.cs
  4. 38 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessRead.cs
  5. 38 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessReadWrite.cs
  6. 38 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessWrite.cs
  7. 0 70
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_SessionOpen_FileAccessRead.cs
  8. 0 70
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_SessionOpen_FileAccessReadWrite.cs
  9. 0 70
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_SessionOpen_FileAccessWrite.cs
  10. 38 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessRead.cs
  11. 38 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessReadWrite.cs
  12. 38 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessWrite.cs
  13. 38 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessRead.cs
  14. 38 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessReadWrite.cs
  15. 35 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessWrite.cs
  16. 0 70
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_SessionOpen_FileAccessRead.cs
  17. 0 70
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_SessionOpen_FileAccessReadWrite.cs
  18. 0 70
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_SessionOpen_FileAccessWrite.cs
  19. 2 3
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_Closed.cs
  20. 37 38
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_Disposed.cs
  21. 37 35
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_SessionNotOpen.cs
  22. 36 33
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_SessionOpen.cs
  23. 9 5
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessReadWrite.cs
  24. 9 5
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessWrite.cs
  25. 9 5
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessReadWrite.cs
  26. 7 3
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessWrite.cs
  27. 11 7
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessReadWrite_FileDoesNotExist.cs
  28. 9 5
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessReadWrite_FileExists.cs
  29. 11 7
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileDoesNotExist.cs
  30. 7 3
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileExists.cs
  31. 7 3
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessRead.cs
  32. 7 3
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessReadWrite.cs
  33. 9 5
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessWrite.cs
  34. 9 5
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessRead.cs
  35. 7 3
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessReadWrite.cs
  36. 7 3
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessWrite.cs
  37. 7 3
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessReadWrite.cs
  38. 7 3
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessWrite.cs
  39. 35 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_Closed.cs
  40. 35 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_Disposed.cs
  41. 32 33
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_SessionNotOpen.cs
  42. 32 32
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_SessionOpen.cs
  43. 32 32
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Finalize_SessionOpen.cs
  44. 39 43
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_Eof.cs
  45. 41 48
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_LessDataThanReadBufferSizeAvailable.cs
  46. 30 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionNotOpen.cs
  47. 30 36
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionOpen_FIleAccessRead.cs
  48. 108 138
      src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Write_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize.cs
  49. 0 6
      src/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj

+ 35 - 33
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessRead.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,58 +7,61 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_CanRead_Closed_FileAccessRead
+    public class SftpFileStreamTest_CanRead_Closed_FileAccessRead : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private bool _actual;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
-            _path = random.Next().ToString(CultureInfo.InvariantCulture);
-            _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(3, random);
             _bufferSize = (uint) random.Next(0, 1000);
             _readBufferSize = (uint) random.Next(0, 1000);
             _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int)_bufferSize);
-            _sftpFileStream.Close();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Read,
+                                         (int) _bufferSize);
+            _target.Close();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.CanRead;
+            _actual = _target.CanRead;
         }
 
         [TestMethod]

+ 35 - 33
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessReadWrite.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,58 +7,61 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_CanRead_Closed_FileAccessReadWrite
+    public class SftpFileStreamTest_CanRead_Closed_FileAccessReadWrite : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private bool _actual;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
-            _path = random.Next().ToString(CultureInfo.InvariantCulture);
-            _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(3, random);
             _bufferSize = (uint) random.Next(0, 1000);
             _readBufferSize = (uint) random.Next(0, 1000);
             _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var 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.RequestClose(_handle));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.ReadWrite, (int)_bufferSize);
-            _sftpFileStream.Close();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.ReadWrite,
+                                         (int) _bufferSize);
+            _target.Close();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.CanRead;
+            _actual = _target.CanRead;
         }
 
         [TestMethod]

+ 35 - 33
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessWrite.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,58 +7,61 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_CanRead_Closed_FileAccessWrite
+    public class SftpFileStreamTest_CanRead_Closed_FileAccessWrite : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private bool _actual;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
-            _path = random.Next().ToString(CultureInfo.InvariantCulture);
-            _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(3, random);
             _bufferSize = (uint) random.Next(0, 1000);
             _readBufferSize = (uint) random.Next(0, 1000);
             _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize);
-            _sftpFileStream.Close();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Write,
+                                         (int) _bufferSize);
+            _target.Close();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.CanRead;
+            _actual = _target.CanRead;
         }
 
         [TestMethod]

+ 38 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessRead.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,58 +7,61 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_CanRead_Disposed_FileAccessRead
+    public class SftpFileStreamTest_CanRead_Disposed_FileAccessRead : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private bool _actual;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(1, random);
+            _bufferSize = (uint) random.Next(0, 1000);
+            _readBufferSize = (uint) random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int)_bufferSize);
-            _sftpFileStream.Dispose();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Read,
+                                         (int) _bufferSize);
+            _target.Dispose();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.CanRead;
+            _actual = _target.CanRead;
         }
 
         [TestMethod]

+ 38 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessReadWrite.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,58 +7,61 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_CanRead_Disposed_FileAccessReadWrite
+    public class SftpFileStreamTest_CanRead_Disposed_FileAccessReadWrite : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private bool _actual;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(1, random);
+            _bufferSize = (uint) random.Next(0, 1000);
+            _readBufferSize = (uint) random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var 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.RequestClose(_handle));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.ReadWrite, (int)_bufferSize);
-            _sftpFileStream.Dispose();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.ReadWrite,
+                                         (int) _bufferSize);
+            _target.Dispose();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.CanRead;
+            _actual = _target.CanRead;
         }
 
         [TestMethod]

+ 38 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Disposed_FileAccessWrite.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,58 +7,61 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_CanRead_Disposed_FileAccessWrite
+    public class SftpFileStreamTest_CanRead_Disposed_FileAccessWrite : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private bool _actual;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(1, random);
+            _bufferSize = (uint) random.Next(0, 1000);
+            _readBufferSize = (uint) random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize);
-            _sftpFileStream.Dispose();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Write,
+                                         (int) _bufferSize);
+            _target.Dispose();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.CanRead;
+            _actual = _target.CanRead;
         }
 
         [TestMethod]

+ 0 - 70
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_SessionOpen_FileAccessRead.cs

@@ -1,70 +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_CanRead_SessionOpen_FileAccessRead
-    {
-        private Mock<ISftpSession> _sftpSessionMock;
-        private string _path;
-        private SftpFileStream _sftpFileStream;
-        private byte[] _handle;
-        private uint _bufferSize;
-        private uint _readBufferSize;
-        private uint _writeBufferSize;
-        private bool _actual;
-
-        [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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
-
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
-
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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.Read, (int)_bufferSize);
-        }
-
-        protected void Act()
-        {
-            _actual = _sftpFileStream.CanRead;
-        }
-
-        [TestMethod]
-        public void CanReadShouldReturnTrue()
-        {
-            Assert.IsTrue(_actual);
-        }
-    }
-}

+ 0 - 70
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_SessionOpen_FileAccessReadWrite.cs

@@ -1,70 +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_CanRead_SessionOpen_FileAccessReadWrite
-    {
-        private Mock<ISftpSession> _sftpSessionMock;
-        private string _path;
-        private SftpFileStream _sftpFileStream;
-        private byte[] _handle;
-        private uint _bufferSize;
-        private uint _readBufferSize;
-        private uint _writeBufferSize;
-        private bool _actual;
-
-        [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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
-
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
-
-            var 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.RequestClose(_handle));
-
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.ReadWrite, (int)_bufferSize);
-        }
-
-        protected void Act()
-        {
-            _actual = _sftpFileStream.CanRead;
-        }
-
-        [TestMethod]
-        public void CanReadShouldReturnTrue()
-        {
-            Assert.IsTrue(_actual);
-        }
-    }
-}

+ 0 - 70
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_SessionOpen_FileAccessWrite.cs

@@ -1,70 +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_CanRead_SessionOpen_FileAccessWrite
-    {
-        private Mock<ISftpSession> _sftpSessionMock;
-        private string _path;
-        private SftpFileStream _sftpFileStream;
-        private byte[] _handle;
-        private uint _bufferSize;
-        private uint _readBufferSize;
-        private uint _writeBufferSize;
-        private bool _actual;
-
-        [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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
-
-            _sftpSessionMock = new Mock<ISftpSession>(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);
-        }
-
-        protected void Act()
-        {
-            _actual = _sftpFileStream.CanRead;
-        }
-
-        [TestMethod]
-        public void CanReadShouldReturnFalse()
-        {
-            Assert.IsFalse(_actual);
-        }
-    }
-}

+ 38 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessRead.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,58 +7,61 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_CanWrite_Closed_FileAccessRead
+    public class SftpFileStreamTest_CanWrite_Closed_FileAccessRead : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private bool _actual;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(1, random);
+            _bufferSize = (uint) random.Next(0, 1000);
+            _readBufferSize = (uint) random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int)_bufferSize);
-            _sftpFileStream.Close();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Read,
+                                         (int) _bufferSize);
+            _target.Close();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.CanWrite;
+            _actual = _target.CanWrite;
         }
 
         [TestMethod]

+ 38 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessReadWrite.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,58 +7,61 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_CanWrite_Closed_FileAccessReadWrite
+    public class SftpFileStreamTest_CanWrite_Closed_FileAccessReadWrite : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private bool _actual;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(1, random);
+            _bufferSize = (uint) random.Next(0, 1000);
+            _readBufferSize = (uint) random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var 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.RequestClose(_handle));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.ReadWrite, (int)_bufferSize);
-            _sftpFileStream.Close();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.ReadWrite,
+                                         (int) _bufferSize);
+            _target.Close();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.CanWrite;
+            _actual = _target.CanWrite;
         }
 
         [TestMethod]

+ 38 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Closed_FileAccessWrite.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,58 +7,61 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_CanWrite_Closed_FileAccessWrite
+    public class SftpFileStreamTest_CanWrite_Closed_FileAccessWrite : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private bool _actual;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(1, random);
+            _bufferSize = (uint) random.Next(0, 1000);
+            _readBufferSize = (uint) random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize);
-            _sftpFileStream.Close();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Write,
+                                         (int) _bufferSize);
+            _target.Close();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.CanWrite;
+            _actual = _target.CanWrite;
         }
 
         [TestMethod]

+ 38 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessRead.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,58 +7,61 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_CanWrite_Disposed_FileAccessRead
+    public class SftpFileStreamTest_CanWrite_Disposed_FileAccessRead : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private bool _actual;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(1, random);
+            _bufferSize = (uint) random.Next(0, 1000);
+            _readBufferSize = (uint) random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int)_bufferSize);
-            _sftpFileStream.Dispose();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Read,
+                                         (int) _bufferSize);
+            _target.Dispose();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.CanWrite;
+            _actual = _target.CanWrite;
         }
 
         [TestMethod]

+ 38 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessReadWrite.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,58 +7,61 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_CanWrite_Disposed_FileAccessReadWrite
+    public class SftpFileStreamTest_CanWrite_Disposed_FileAccessReadWrite : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private bool _actual;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(1, random);
+            _bufferSize = (uint) random.Next(0, 1000);
+            _readBufferSize = (uint) random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var 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.RequestClose(_handle));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.ReadWrite, (int)_bufferSize);
-            _sftpFileStream.Dispose();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.ReadWrite,
+                                         (int) _bufferSize);
+            _target.Dispose();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.CanWrite;
+            _actual = _target.CanWrite;
         }
 
         [TestMethod]

+ 35 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_Disposed_FileAccessWrite.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,58 +7,58 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_CanWrite_Disposed_FileAccessWrite
+    public class SftpFileStreamTest_CanWrite_Disposed_FileAccessWrite : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private bool _actual;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(1, random);
+            _bufferSize = (uint) random.Next(0, 1000);
+            _readBufferSize = (uint) random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize);
-            _sftpFileStream.Dispose();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Write,
+                                         (int) _bufferSize);
+            _target.Dispose();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.CanWrite;
+            _actual = _target.CanWrite;
         }
 
         [TestMethod]

+ 0 - 70
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_SessionOpen_FileAccessRead.cs

@@ -1,70 +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_CanWrite_SessionOpen_FileAccessRead
-    {
-        private Mock<ISftpSession> _sftpSessionMock;
-        private string _path;
-        private SftpFileStream _sftpFileStream;
-        private byte[] _handle;
-        private uint _bufferSize;
-        private uint _readBufferSize;
-        private uint _writeBufferSize;
-        private bool _actual;
-
-        [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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
-
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
-
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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.Read, (int)_bufferSize);
-        }
-
-        protected void Act()
-        {
-            _actual = _sftpFileStream.CanWrite;
-        }
-
-        [TestMethod]
-        public void CaWriteShouldReturnFalse()
-        {
-            Assert.IsFalse(_actual);
-        }
-    }
-}

+ 0 - 70
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_SessionOpen_FileAccessReadWrite.cs

@@ -1,70 +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_CanWrite_SessionOpen_FileAccessReadWrite
-    {
-        private Mock<ISftpSession> _sftpSessionMock;
-        private string _path;
-        private SftpFileStream _sftpFileStream;
-        private byte[] _handle;
-        private uint _bufferSize;
-        private uint _readBufferSize;
-        private uint _writeBufferSize;
-        private bool _actual;
-
-        [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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
-
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
-
-            var 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.RequestClose(_handle));
-
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.ReadWrite, (int)_bufferSize);
-        }
-
-        protected void Act()
-        {
-            _actual = _sftpFileStream.CanWrite;
-        }
-
-        [TestMethod]
-        public void CanWriteShouldReturnTrue()
-        {
-            Assert.IsTrue(_actual);
-        }
-    }
-}

+ 0 - 70
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanWrite_SessionOpen_FileAccessWrite.cs

@@ -1,70 +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_CanWrite_SessionOpen_FileAccessWrite
-    {
-        private Mock<ISftpSession> _sftpSessionMock;
-        private string _path;
-        private SftpFileStream _sftpFileStream;
-        private byte[] _handle;
-        private uint _bufferSize;
-        private uint _readBufferSize;
-        private uint _writeBufferSize;
-        private bool _actual;
-
-        [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(0, 1000);
-            _readBufferSize = (uint)random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
-
-            _sftpSessionMock = new Mock<ISftpSession>(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);
-        }
-
-        protected void Act()
-        {
-            _actual = _sftpFileStream.CanWrite;
-        }
-
-        [TestMethod]
-        public void CanWriteShouldReturnTrue()
-        {
-            Assert.IsTrue(_actual);
-        }
-    }
-}

+ 2 - 3
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_Closed.cs

@@ -14,7 +14,6 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         private string _path;
         private SftpFileStream _sftpFileStream;
         private byte[] _handle;
-        private SftpFileAttributes _fileAttributes;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
@@ -39,7 +38,7 @@ namespace Renci.SshNet.Tests.Classes.Sftp
 
             var sequence = new MockSequence();
             _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Truncate, true))
+                .Setup(p => p.RequestOpen(_path, Flags.Read, false))
                 .Returns(_handle);
             _sftpSessionMock.InSequence(sequence)
                 .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
@@ -53,7 +52,7 @@ namespace Renci.SshNet.Tests.Classes.Sftp
             _sftpSessionMock.InSequence(sequence)
                 .Setup(p => p.RequestClose(_handle));
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int)_bufferSize);
+            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Open, FileAccess.Read, (int)_bufferSize);
             _sftpFileStream.Close();
         }
 

+ 37 - 38
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_Disposed.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,69 +7,69 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_Close_Disposed
+    public class SftpFileStreamTest_Close_Disposed : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        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);
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(2, random);
+            _bufferSize = (uint) random.Next(1, 1000);
+            _readBufferSize = (uint) random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int)_bufferSize);
-            _sftpFileStream.Dispose();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Read,
+                                         (int) _bufferSize);
+            _target.Dispose();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _sftpFileStream.Close();
+            _target.Close();
         }
 
         [TestMethod]
         public void IsOpenOnSftpSessionShouldBeInvokedOnce()
         {
-            _sftpSessionMock.Verify(p => p.IsOpen, Times.Once);
+            SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
         }
 
         [TestMethod]
         public void RequestCloseOnSftpSessionShouldBeInvokedOnce()
         {
-            _sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
+            SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
         }
     }
 }

+ 37 - 35
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_SessionNotOpen.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,66 +7,69 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_Close_SessionNotOpen
+    public class SftpFileStreamTest_Close_SessionNotOpen : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        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);
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(2, random);
+            _bufferSize = (uint) random.Next(1, 1000);
+            _readBufferSize = (uint) random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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);
+        }
 
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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(false);
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int)_bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Read,
+                                         (int) _bufferSize);
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _sftpFileStream.Close();
+            _target.Close();
         }
 
         [TestMethod]
         public void IsOpenOnSftpSessionShouldBeInvokedOnce()
         {
-            _sftpSessionMock.Verify(p => p.IsOpen, Times.Once);
+            SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
         }
 
         [TestMethod]
         public void RequestCloseOnSftpSessionShouldNeverBeInvoked()
         {
-            _sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Never);
+            SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Never);
         }
     }
 }

+ 36 - 33
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Close_SessionOpen.cs

@@ -8,68 +8,71 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_Close_SessionOpen
+    public class SftpFileStreamTest_Close_SessionOpen : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
-            _path = random.Next().ToString(CultureInfo.InvariantCulture);
-            _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(2, random);
             _bufferSize = (uint) random.Next(1, 1000);
             _readBufferSize = (uint) random.Next(0, 1000);
             _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int) _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Read,
+                                         (int) _bufferSize);
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _sftpFileStream.Close();
+            _target.Close();
         }
 
         [TestMethod]
         public void IsOpenOnSftpSessionShouldBeInvokedOnce()
         {
-            _sftpSessionMock.Verify(p => p.IsOpen, Times.Once);
+            SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
         }
 
         [TestMethod]
         public void RequestCloseOnSftpSessionShouldBeInvokedOnce()
         {
-            _sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
+            SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
         }
     }
 }

+ 9 - 5
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessReadWrite.cs

@@ -46,8 +46,8 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Append, false))
-                            .Returns(_handle);
+                           .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Append, false))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
                            .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize))
                            .Returns(_readBufferSize);
@@ -55,13 +55,17 @@ namespace Renci.SshNet.Tests.Classes.Sftp
                            .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle))
                            .Returns(_writeBufferSize);
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestFStat(_handle, false))
-                            .Returns(_fileAttributes);
+                           .Setup(p => p.RequestFStat(_handle, false))
+                           .Returns(_fileAttributes);
         }
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 9 - 5
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeAppend_FileAccessWrite.cs

@@ -46,8 +46,8 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Append, false))
-                            .Returns(_handle);
+                           .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Append, false))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
                            .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
@@ -55,13 +55,17 @@ namespace Renci.SshNet.Tests.Classes.Sftp
                            .Setup(p => p.CalculateOptimalWriteLength((uint) _bufferSize, _handle))
                            .Returns(_writeBufferSize);
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestFStat(_handle, false))
-                            .Returns(_fileAttributes);
+                           .Setup(p => p.RequestFStat(_handle, false))
+                           .Returns(_fileAttributes);
         }
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 9 - 5
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessReadWrite.cs

@@ -37,19 +37,23 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.CreateNew, false))
-                            .Returns(_handle);
+                           .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.CreateNew, false))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
-                           .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize))
+                           .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
             SftpSessionMock.InSequence(MockSequence)
-                           .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle))
+                           .Setup(p => p.CalculateOptimalWriteLength((uint) _bufferSize, _handle))
                            .Returns(_writeBufferSize);
         }
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 7 - 3
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreateNew_FileAccessWrite.cs

@@ -37,8 +37,8 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNew, false))
-                            .Returns(_handle);
+                           .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNew, false))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
                            .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
@@ -49,7 +49,11 @@ namespace Renci.SshNet.Tests.Classes.Sftp
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 11 - 7
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessReadWrite_FileDoesNotExist.cs

@@ -37,22 +37,26 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, true))
-                            .Returns((byte[]) null);
+                           .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);
+                           .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.CreateNew, false))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
-                           .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize))
+                           .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
             SftpSessionMock.InSequence(MockSequence)
-                           .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle))
+                           .Setup(p => p.CalculateOptimalWriteLength((uint) _bufferSize, _handle))
                            .Returns(_writeBufferSize);
         }
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 9 - 5
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessReadWrite_FileExists.cs

@@ -37,19 +37,23 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, true))
-                            .Returns(_handle);
+                           .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, true))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
-                           .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize))
+                           .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
             SftpSessionMock.InSequence(MockSequence)
-                           .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle))
+                           .Setup(p => p.CalculateOptimalWriteLength((uint) _bufferSize, _handle))
                            .Returns(_writeBufferSize);
         }
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 11 - 7
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileDoesNotExist.cs

@@ -37,22 +37,26 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true))
-                            .Returns((byte[])null);
+                           .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);
+                           .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNew, false))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
-                           .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize))
+                           .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
             SftpSessionMock.InSequence(MockSequence)
-                           .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle))
+                           .Setup(p => p.CalculateOptimalWriteLength((uint) _bufferSize, _handle))
                            .Returns(_writeBufferSize);
         }
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 7 - 3
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileExists.cs

@@ -37,8 +37,8 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true))
-                            .Returns(_handle);
+                           .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
                            .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
@@ -49,7 +49,11 @@ namespace Renci.SshNet.Tests.Classes.Sftp
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 7 - 3
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessRead.cs

@@ -36,8 +36,8 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.CreateNewOrOpen, false))
-                            .Returns(_handle);
+                           .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.CreateNewOrOpen, false))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
                            .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
@@ -48,7 +48,11 @@ namespace Renci.SshNet.Tests.Classes.Sftp
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 7 - 3
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessReadWrite.cs

@@ -37,8 +37,8 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.CreateNewOrOpen, false))
-                            .Returns(_handle);
+                           .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);
@@ -49,7 +49,11 @@ namespace Renci.SshNet.Tests.Classes.Sftp
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 9 - 5
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessWrite.cs

@@ -37,19 +37,23 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNewOrOpen, false))
-                            .Returns(_handle);
+                           .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNewOrOpen, false))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
-                           .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize))
+                           .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
             SftpSessionMock.InSequence(MockSequence)
-                           .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle))
+                           .Setup(p => p.CalculateOptimalWriteLength((uint) _bufferSize, _handle))
                            .Returns(_writeBufferSize);
         }
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 9 - 5
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessRead.cs

@@ -36,19 +36,23 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Read, false))
-                            .Returns(_handle);
+                           .Setup(p => p.RequestOpen(_path, Flags.Read, false))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
-                           .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize))
+                           .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
             SftpSessionMock.InSequence(MockSequence)
-                           .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle))
+                           .Setup(p => p.CalculateOptimalWriteLength((uint) _bufferSize, _handle))
                            .Returns(_writeBufferSize);
         }
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 7 - 3
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessReadWrite.cs

@@ -37,8 +37,8 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false))
-                            .Returns(_handle);
+                           .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
                            .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
@@ -49,7 +49,11 @@ namespace Renci.SshNet.Tests.Classes.Sftp
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 7 - 3
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeOpen_FileAccessWrite.cs

@@ -37,8 +37,8 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Write, false))
-                            .Returns(_handle);
+                           .Setup(p => p.RequestOpen(_path, Flags.Write, false))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
                            .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
@@ -49,7 +49,11 @@ namespace Renci.SshNet.Tests.Classes.Sftp
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 7 - 3
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessReadWrite.cs

@@ -37,8 +37,8 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, false))
-                            .Returns(_handle);
+                           .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);
@@ -49,7 +49,11 @@ namespace Renci.SshNet.Tests.Classes.Sftp
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 7 - 3
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Ctor_FileModeTruncate_FileAccessWrite.cs

@@ -37,8 +37,8 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         protected override void SetupMocks()
         {
             SftpSessionMock.InSequence(MockSequence)
-                            .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, false))
-                            .Returns(_handle);
+                           .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, false))
+                           .Returns(_handle);
             SftpSessionMock.InSequence(MockSequence)
                            .Setup(p => p.CalculateOptimalReadLength((uint) _bufferSize))
                            .Returns(_readBufferSize);
@@ -49,7 +49,11 @@ namespace Renci.SshNet.Tests.Classes.Sftp
 
         protected override void Act()
         {
-            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         _fileMode,
+                                         _fileAccess,
+                                         _bufferSize);
         }
 
         [TestMethod]

+ 35 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_Closed.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,69 +7,69 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_Dispose_Closed
+    public class SftpFileStreamTest_Dispose_Closed : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
-            _path = random.Next().ToString(CultureInfo.InvariantCulture);
-            _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(random.Next(1, 5), random);
             _bufferSize = (uint) random.Next(1, 1000);
             _readBufferSize = (uint) random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int) _bufferSize);
-            _sftpFileStream.Close();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Read,
+                                         (int) _bufferSize);
+            _target.Close();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _sftpFileStream.Dispose();
+            _target.Dispose();
         }
 
         [TestMethod]
         public void IsOpenOnSftpSessionShouldBeInvokedOnce()
         {
-            _sftpSessionMock.Verify(p => p.IsOpen, Times.Once);
+            SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
         }
 
         [TestMethod]
         public void RequestCloseOnSftpSessionShouldBeInvokedOnce()
         {
-            _sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
+            SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
         }
     }
 }

+ 35 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_Disposed.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,69 +7,69 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_Dispose_Disposed
+    public class SftpFileStreamTest_Dispose_Disposed : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
-            _path = random.Next().ToString(CultureInfo.InvariantCulture);
-            _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(1, random);
             _bufferSize = (uint) random.Next(1, 1000);
             _readBufferSize = (uint) random.Next(0, 1000);
-            _writeBufferSize = (uint)random.Next(0, 1000);
+            _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int) _bufferSize);
-            _sftpFileStream.Dispose();
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.OpenOrCreate,
+                                         FileAccess.Write,
+                                         (int) _bufferSize);
+            _target.Dispose();
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _sftpFileStream.Dispose();
+            _target.Dispose();
         }
 
         [TestMethod]
         public void IsOpenOnSftpSessionShouldBeInvokedOnce()
         {
-            _sftpSessionMock.Verify(p => p.IsOpen, Times.Once);
+            SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
         }
 
         [TestMethod]
         public void RequestCloseOnSftpSessionShouldBeInvokedOnce()
         {
-            _sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
+            SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
         }
     }
 }

+ 32 - 33
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_SessionNotOpen.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,68 +7,68 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_Dispose_SessionNotOpen
+    public class SftpFileStreamTest_Dispose_SessionNotOpen : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
             _path = random.Next().ToString();
-            _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
+            _handle = GenerateRandom(2, random);
             _bufferSize = (uint) random.Next(1, 1000);
             _readBufferSize = (uint) random.Next(0, 1000);
             _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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(false);
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestClose(_handle));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int)_bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.CreateNew,
+                                         FileAccess.Write,
+                                         (int) _bufferSize);
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _sftpFileStream.Dispose();
+            _target.Dispose();
         }
 
         [TestMethod]
         public void IsOpenOnSftpSessionShouldBeInvokedOnce()
         {
-            _sftpSessionMock.Verify(p => p.IsOpen, Times.Once);
+            SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
         }
 
         [TestMethod]
         public void RequestCloseOnSftpSessionShouldNeverBeInvoked()
         {
-            _sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Never);
+            SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Never);
         }
     }
 }

+ 32 - 32
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Dispose_SessionOpen.cs

@@ -8,68 +8,68 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_Dispose_SessionOpen
+    public class SftpFileStreamTest_Dispose_SessionOpen : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
             _path = random.Next().ToString(CultureInfo.InvariantCulture);
-            _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
+            _handle = GenerateRandom(2, random);
             _bufferSize = (uint) random.Next(1, 1000);
             _readBufferSize = (uint) random.Next(0, 1000);
             _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int) _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Truncate,
+                                         FileAccess.Write,
+                                         (int) _bufferSize);
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _sftpFileStream.Dispose();
+            _target.Dispose();
         }
 
         [TestMethod]
         public void IsOpenOnSftpSessionShouldBeInvokedOnce()
         {
-            _sftpSessionMock.Verify(p => p.IsOpen, Times.Once);
+            SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
         }
 
         [TestMethod]
         public void RequestCloseOnSftpSessionShouldBeInvokedOnce()
         {
-            _sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
+            SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
         }
     }
 }

+ 32 - 32
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Finalize_SessionOpen.cs

@@ -8,55 +8,55 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_Finalize_SessionOpen
+    public class SftpFileStreamTest_Finalize_SessionOpen : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
             _path = random.Next().ToString(CultureInfo.InvariantCulture);
-            _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
+            _handle = GenerateRandom(7, random);
             _bufferSize = (uint) random.Next(1, 1000);
             _readBufferSize = (uint) random.Next(0, 1000);
             _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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));
+        }
 
-            var sequence = new MockSequence();
-            _sftpSessionMock.InSequence(sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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));
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int) _bufferSize);
-            _sftpFileStream = null;
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.OpenOrCreate,
+                                         FileAccess.ReadWrite,
+                                         (int) _bufferSize);
+            _target = null;
         }
 
-        protected void Act()
+        protected override void Act()
         {
             GC.Collect();
             GC.WaitForPendingFinalizers();
@@ -65,13 +65,13 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         [TestMethod]
         public void IsOpenOnSftpSessionShouldNeverBeInvoked()
         {
-            _sftpSessionMock.Verify(p => p.IsOpen, Times.Never);
+            SftpSessionMock.Verify(p => p.IsOpen, Times.Never);
         }
 
         [TestMethod]
         public void RequestCloseOnSftpSessionShouldNeverBeInvoked()
         {
-            _sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Never);
+            SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Never);
         }
     }
 }

+ 39 - 43
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_Eof.cs

@@ -7,67 +7,65 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_Eof
+    public class SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_Eof : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private int _actual;
-        private MockSequence _sequence;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
-
-        [TestCleanup]
-        public void TearDown()
-        {
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.RequestClose(_handle));
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
             _path = random.Next().ToString();
-            _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
+            _handle = GenerateRandom(1, random);
             _bufferSize = (uint) random.Next(1, 1000);
             _readBufferSize = (uint) random.Next(0, 1000);
             _writeBufferSize = (uint) random.Next(0, 1000);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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]);
+        }
 
-            _sequence = new MockSequence();
+        [TestCleanup]
+        public void TearDown()
+        {
+            SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestClose(_handle));
+        }
 
-            _sftpSessionMock.InSequence(_sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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.RequestRead(_handle, 0UL, _readBufferSize))
-                .Returns(new byte[0]);
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int)_bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Read,
+                                         (int)_bufferSize);
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.ReadByte();
+            _actual = _target.ReadByte();
         }
 
         [TestMethod]
@@ -79,11 +77,9 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         [TestMethod]
         public void PositionShouldReturnZero()
         {
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.IsOpen)
-                            .Returns(true);
+            SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
 
-            Assert.AreEqual(0L, _sftpFileStream.Position);
+            Assert.AreEqual(0L, _target.Position);
         }
     }
 }

+ 41 - 48
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_LessDataThanReadBufferSizeAvailable.cs

@@ -1,9 +1,7 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
-using Renci.SshNet.Abstractions;
 using Renci.SshNet.Sftp;
 
 namespace Renci.SshNet.Tests.Classes.Sftp
@@ -12,70 +10,67 @@ namespace Renci.SshNet.Tests.Classes.Sftp
     /// Test for issue #173.
     /// </summary>
     [TestClass]
-    public class SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_LessDataThanReadBufferSizeAvailable
+    public class SftpFileStreamTest_ReadByte_ReadMode_NoDataInWriteBufferAndNoDataInReadBuffer_LessDataThanReadBufferSizeAvailable : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
         private uint _writeBufferSize;
         private int _actual;
         private byte[] _data;
-        private MockSequence _sequence;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
-
-        [TestCleanup]
-        public void TearDown()
-        {
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.RequestClose(_handle));
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
-            _path = random.Next().ToString(CultureInfo.InvariantCulture);
-            _handle = new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) };
+            _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 = new byte[_readBufferSize - 2];
-            CryptoAbstraction.GenerateRandom(_data);
+            _data = GenerateRandom((int) _readBufferSize - 2, random);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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);
+        }
 
-            _sequence = new MockSequence();
+        [TestCleanup]
+        public void TearDown()
+        {
+            SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestClose(_handle));
+        }
 
-            _sftpSessionMock.InSequence(_sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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.RequestRead(_handle, 0UL, _readBufferSize))
-                .Returns(_data);
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int)_bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Read,
+                                         (int)_bufferSize);
         }
 
-        protected void Act()
+        protected override void Act()
         {
-            _actual = _sftpFileStream.ReadByte();
+            _actual = _target.ReadByte();
         }
 
         [TestMethod]
@@ -87,11 +82,9 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         [TestMethod]
         public void PositionShouldReturnOne()
         {
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.IsOpen)
-                            .Returns(true);
+            SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
 
-            Assert.AreEqual(1L, _sftpFileStream.Position);
+            Assert.AreEqual(1L, _target.Position);
         }
     }
 }

+ 30 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionNotOpen.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,65 +7,60 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_SetLength_SessionNotOpen
+    public class SftpFileStreamTest_SetLength_SessionNotOpen : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         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 ObjectDisposedException _actualException;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
-            _path = random.Next().ToString(CultureInfo.InvariantCulture);
-            _handle = new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) };
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(4, random);
             _bufferSize = (uint) random.Next(1, 1000);
             _readBufferSize = (uint) random.Next(0, 1000);
             _writeBufferSize = (uint) random.Next(0, 1000);
             _length = random.Next();
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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);
+        }
 
-            _sequence = new MockSequence();
-            _sftpSessionMock.InSequence(_sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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(false);
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(
-                _sftpSessionMock.Object,
-                _path,
-                FileMode.Create,
-                FileAccess.Read,
-                (int)_bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Read,
+                                         (int) _bufferSize);
         }
 
-        protected void Act()
+        protected override void Act()
         {
             try
             {
-                _sftpFileStream.SetLength(_length);
+                _target.SetLength(_length);
                 Assert.Fail();
             }
             catch (ObjectDisposedException ex)

+ 30 - 36
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_SessionOpen_FIleAccessRead.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Globalization;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -8,65 +7,60 @@ using Renci.SshNet.Sftp;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_SetLength_SessionOpen_FIleAccess
+    public class SftpFileStreamTest_SetLength_SessionOpen_FIleAccess : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         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 NotSupportedException _actualException;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        protected void Arrange()
-        {
             var random = new Random();
-            _path = random.Next().ToString(CultureInfo.InvariantCulture);
-            _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
+            _path = random.Next().ToString();
+            _handle = GenerateRandom(5, random);
             _bufferSize = (uint) random.Next(1, 1000);
             _readBufferSize = (uint) random.Next(0, 1000);
             _writeBufferSize = (uint) random.Next(0, 1000);
             _length = random.Next();
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
+        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);
+        }
 
-            _sequence = new MockSequence();
-            _sftpSessionMock.InSequence(_sequence)
-                .Setup(p => p.RequestOpen(_path, Flags.Read | 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);
+        protected override void Arrange()
+        {
+            base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(
-                _sftpSessionMock.Object,
-                _path,
-                FileMode.Create,
-                FileAccess.Read,
-                (int) _bufferSize);
+            _target = new SftpFileStream(SftpSessionMock.Object,
+                                         _path,
+                                         FileMode.Open,
+                                         FileAccess.Read,
+                                         (int) _bufferSize);
         }
 
-        protected void Act()
+        protected override void Act()
         {
             try
             {
-                _sftpFileStream.SetLength(_length);
+                _target.SetLength(_length);
                 Assert.Fail();
             }
             catch (NotSupportedException ex)

+ 108 - 138
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_Write_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize.cs

@@ -10,11 +10,10 @@ using Renci.SshNet.Sftp.Responses;
 namespace Renci.SshNet.Tests.Classes.Sftp
 {
     [TestClass]
-    public class SftpFileStreamTest_Write_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize
+    public class SftpFileStreamTest_Write_SessionOpen_CountGreatherThanTwoTimesTheWriteBufferSize : SftpFileStreamTestBase
     {
-        private Mock<ISftpSession> _sftpSessionMock;
+        private SftpFileStream _target;
         private string _path;
-        private SftpFileStream _sftpFileStream;
         private byte[] _handle;
         private uint _bufferSize;
         private uint _readBufferSize;
@@ -22,122 +21,126 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         private byte[] _data;
         private int _count;
         private int _offset;
-        private MockSequence _sequence;
         private Random _random;
         private uint _expectedWrittenByteCount;
         private int _expectedBufferedByteCount;
         private byte[] _expectedBufferedBytes;
 
-        [TestInitialize]
-        public void Setup()
+        protected override void SetupData()
         {
-            Arrange();
-            Act();
-        }
+            base.SetupData();
 
-        [TestCleanup]
-        public void TearDown()
-        {
-            if (_sftpSessionMock != null)
-            {
-                // allow Dispose to complete successfully
-                _sftpSessionMock.InSequence(_sequence)
-                    .Setup(p => p.IsOpen)
-                    .Returns(true);
-                _sftpSessionMock.InSequence(_sequence)
-                    .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null));
-                _sftpSessionMock.InSequence(_sequence)
-                    .Setup(p => p.RequestClose(_handle));
-            }
-        }
-
-        protected void Arrange()
-        {
             _random = new Random();
             _path = _random.Next().ToString(CultureInfo.InvariantCulture);
-            _handle = new[] {(byte) _random.Next(byte.MinValue, byte.MaxValue)};
-            _bufferSize = (uint) _random.Next(1, 1000);
+            _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];
+            _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);
+            _count = ((int) _writeBufferSize * 2) + _random.Next(1, 5);
 
             _expectedWrittenByteCount = (2 * _writeBufferSize);
             _expectedBufferedByteCount = (int)(_count - _expectedWrittenByteCount);
             _expectedBufferedBytes = _data.Take(_offset + (int)_expectedWrittenByteCount, _expectedBufferedByteCount);
+        }
 
-            _sftpSessionMock = new Mock<ISftpSession>(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.RequestWrite(_handle, 0, _data, _offset, (int) _writeBufferSize, It.IsAny<AutoResetEvent>(), null));
-            _sftpSessionMock.InSequence(_sequence)
-                .Setup(p => p.RequestWrite(_handle, _writeBufferSize, _data, _offset + (int) _writeBufferSize, (int)_writeBufferSize, It.IsAny<AutoResetEvent>(), null));
-
-            _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int) _bufferSize);
+        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<AutoResetEvent>(), null));
+            SftpSessionMock.InSequence(MockSequence)
+                           .Setup(p => p.RequestWrite(_handle, _writeBufferSize, _data, _offset + (int)_writeBufferSize, (int)_writeBufferSize, It.IsAny<AutoResetEvent>(), null));
         }
 
-        protected void Act()
+        [TestCleanup]
+        public void TearDown()
         {
-            _sftpFileStream.Write(_data, _offset, _count);
+            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<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), 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<AutoResetEvent>(), null), Times.Once);
-            _sftpSessionMock.Verify(p => p.RequestWrite(_handle, _writeBufferSize, _data, _offset + (int)_writeBufferSize, (int)_writeBufferSize, It.IsAny<AutoResetEvent>(), null), Times.Once);
+            SftpSessionMock.Verify(p => p.RequestWrite(_handle, 0, _data, _offset, (int)_writeBufferSize, It.IsAny<AutoResetEvent>(), null), Times.Once);
+            SftpSessionMock.Verify(p => p.RequestWrite(_handle, _writeBufferSize, _data, _offset + (int)_writeBufferSize, (int)_writeBufferSize, It.IsAny<AutoResetEvent>(), null), Times.Once);
         }
 
         [TestMethod]
         public void PositionShouldBeNumberOfBytesWrittenToFileAndNUmberOfBytesInBuffer()
         {
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.IsOpen)
-                            .Returns(true);
+            SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
 
-            Assert.AreEqual(_count, _sftpFileStream.Position);
+            Assert.AreEqual(_count, _target.Position);
         }
 
         [TestMethod]
         public void LengthShouldFlushBufferAndReturnSizeOfFile()
         {
-            var lengthFileAttributes = new SftpFileAttributes(DateTime.Now, DateTime.Now, _random.Next(), _random.Next(),
-                                                        _random.Next(), (uint) _random.Next(0, int.MaxValue), null);
+            var lengthFileAttributes = new SftpFileAttributes(DateTime.Now,
+                                                              DateTime.Now,
+                                                              _random.Next(),
+                                                              _random.Next(),
+                                                              _random.Next(),
+                                                              (uint) _random.Next(0, int.MaxValue),
+                                                              null);
             byte[] actualFlushedData = null;
 
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.IsOpen)
-                            .Returns(true);
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null))
-                            .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length));
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.RequestFStat(_handle, true))
-                            .Returns(lengthFileAttributes);
-
-            Assert.AreEqual(lengthFileAttributes.Size, _sftpFileStream.Length);
+            SftpSessionMock.InSequence(MockSequence)
+                           .Setup(p => p.IsOpen)
+                           .Returns(true);
+            SftpSessionMock.InSequence(MockSequence)
+                           .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null))
+                           .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((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<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null), Times.Once);
+            SftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null), Times.Once);
         }
 
         [TestMethod]
@@ -146,20 +149,20 @@ namespace Renci.SshNet.Tests.Classes.Sftp
             const SftpFileAttributes lengthFileAttributes = null;
             byte[] actualFlushedData = null;
 
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.IsOpen)
-                            .Returns(true);
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null))
-                            .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length));
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.RequestFStat(_handle, true))
-                            .Returns(lengthFileAttributes);
+            SftpSessionMock.InSequence(MockSequence)
+                           .Setup(p => p.IsOpen)
+                           .Returns(true);
+            SftpSessionMock.InSequence(MockSequence)
+                           .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null))
+                           .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((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 = _sftpFileStream.Length;
-                Assert.Fail();
+                var length = _target.Length;
+                Assert.Fail("Length should have failed, but returned: " + length + ".");
             }
             catch (IOException ex)
             {
@@ -169,62 +172,29 @@ namespace Renci.SshNet.Tests.Classes.Sftp
 
             Assert.IsTrue(actualFlushedData.IsEqualTo(_expectedBufferedBytes));
 
-            _sftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null), Times.Once);
+            SftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null), Times.Once);
         }
 
-        [TestMethod]
-        public void LengthShouldThrowIOExceptionIfSizeIsMinusOne()
-        {
-            var lengthFileAttributes = new SftpFileAttributes(DateTime.Now, DateTime.Now, -1, _random.Next(), _random.Next(), (uint)_random.Next(0, int.MaxValue), null);
-            byte[] actualFlushedData = null;
-
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.IsOpen)
-                            .Returns(true);
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null))
-                            .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length));
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.RequestFStat(_handle, true))
-                            .Returns(lengthFileAttributes);
-
-            try
-            {
-                var length = _sftpFileStream.Length;
-                Assert.Fail();
-            }
-            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<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null), Times.Once);
-        }
-
-
         [TestMethod]
         public void DisposeShouldFlushBufferAndCloseRequest()
         {
             byte[] actualFlushedData = null;
 
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.IsOpen)
-                            .Returns(true);
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null))
-                            .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length));
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.RequestClose(_handle));
+            SftpSessionMock.InSequence(MockSequence)
+                           .Setup(p => p.IsOpen)
+                           .Returns(true);
+            SftpSessionMock.InSequence(MockSequence)
+                           .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null))
+                           .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length));
+            SftpSessionMock.InSequence(MockSequence)
+                           .Setup(p => p.RequestClose(_handle));
 
-            _sftpFileStream.Dispose();
+            _target.Dispose();
 
             Assert.IsTrue(actualFlushedData.IsEqualTo(_expectedBufferedBytes));
 
-            _sftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null), Times.Once);
-            _sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
+            SftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null), Times.Once);
+            SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
         }
 
         [TestMethod]
@@ -232,18 +202,18 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         {
             byte[] actualFlushedData = null;
 
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.IsOpen)
-                            .Returns(true);
-            _sftpSessionMock.InSequence(_sequence)
-                            .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null))
-                            .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length));
+            SftpSessionMock.InSequence(MockSequence)
+                           .Setup(p => p.IsOpen)
+                           .Returns(true);
+            SftpSessionMock.InSequence(MockSequence)
+                           .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null))
+                           .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length));
 
-            _sftpFileStream.Flush();
+            _target.Flush();
 
             Assert.IsTrue(actualFlushedData.IsEqualTo(_expectedBufferedBytes));
 
-            _sftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null), Times.Once);
+            SftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny<byte[]>(), 0, _expectedBufferedByteCount, It.IsAny<AutoResetEvent>(), null), Times.Once);
         }
     }
 }

+ 0 - 6
src/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj

@@ -363,18 +363,12 @@
     <Compile Include="Classes\Sftp\SftpFileStreamTest_CanRead_Disposed_FileAccessRead.cs" />
     <Compile Include="Classes\Sftp\SftpFileStreamTest_CanRead_Disposed_FileAccessReadWrite.cs" />
     <Compile Include="Classes\Sftp\SftpFileStreamTest_CanRead_Disposed_FileAccessWrite.cs" />
-    <Compile Include="Classes\Sftp\SftpFileStreamTest_CanRead_SessionOpen_FileAccessRead.cs" />
-    <Compile Include="Classes\Sftp\SftpFileStreamTest_CanRead_SessionOpen_FileAccessReadWrite.cs" />
-    <Compile Include="Classes\Sftp\SftpFileStreamTest_CanRead_SessionOpen_FileAccessWrite.cs" />
     <Compile Include="Classes\Sftp\SftpFileStreamTest_CanWrite_Closed_FileAccessRead.cs" />
     <Compile Include="Classes\Sftp\SftpFileStreamTest_CanWrite_Closed_FileAccessReadWrite.cs" />
     <Compile Include="Classes\Sftp\SftpFileStreamTest_CanWrite_Closed_FileAccessWrite.cs" />
     <Compile Include="Classes\Sftp\SftpFileStreamTest_CanWrite_Disposed_FileAccessRead.cs" />
     <Compile Include="Classes\Sftp\SftpFileStreamTest_CanWrite_Disposed_FileAccessReadWrite.cs" />
     <Compile Include="Classes\Sftp\SftpFileStreamTest_CanWrite_Disposed_FileAccessWrite.cs" />
-    <Compile Include="Classes\Sftp\SftpFileStreamTest_CanWrite_SessionOpen_FileAccessRead.cs" />
-    <Compile Include="Classes\Sftp\SftpFileStreamTest_CanWrite_SessionOpen_FileAccessReadWrite.cs" />
-    <Compile Include="Classes\Sftp\SftpFileStreamTest_CanWrite_SessionOpen_FileAccessWrite.cs" />
     <Compile Include="Classes\Sftp\SftpFileStreamTest_Close_Closed.cs" />
     <Compile Include="Classes\Sftp\SftpFileStreamTest_Close_Disposed.cs" />
     <Compile Include="Classes\Sftp\SftpFileStreamTest_Close_SessionNotOpen.cs" />