Skip to content

Commit 14b571f

Browse files
committed
Update tests now that we reject invalid combination of FileMode and FileAccess.
Remove duplicate tests.
1 parent ff78430 commit 14b571f

File tree

49 files changed

+1102
-1496
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1102
-1496
lines changed

src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessRead.cs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Globalization;
32
using System.IO;
43
using Microsoft.VisualStudio.TestTools.UnitTesting;
54
using Moq;
@@ -8,58 +7,61 @@
87
namespace Renci.SshNet.Tests.Classes.Sftp
98
{
109
[TestClass]
11-
public class SftpFileStreamTest_CanRead_Closed_FileAccessRead
10+
public class SftpFileStreamTest_CanRead_Closed_FileAccessRead : SftpFileStreamTestBase
1211
{
13-
private Mock<ISftpSession> _sftpSessionMock;
12+
private SftpFileStream _target;
1413
private string _path;
15-
private SftpFileStream _sftpFileStream;
1614
private byte[] _handle;
1715
private uint _bufferSize;
1816
private uint _readBufferSize;
1917
private uint _writeBufferSize;
2018
private bool _actual;
2119

22-
[TestInitialize]
23-
public void Setup()
20+
protected override void SetupData()
2421
{
25-
Arrange();
26-
Act();
27-
}
22+
base.SetupData();
2823

29-
protected void Arrange()
30-
{
3124
var random = new Random();
32-
_path = random.Next().ToString(CultureInfo.InvariantCulture);
33-
_handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
25+
_path = random.Next().ToString();
26+
_handle = GenerateRandom(3, random);
3427
_bufferSize = (uint) random.Next(0, 1000);
3528
_readBufferSize = (uint) random.Next(0, 1000);
3629
_writeBufferSize = (uint) random.Next(0, 1000);
30+
}
3731

38-
_sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
32+
protected override void SetupMocks()
33+
{
34+
SftpSessionMock.InSequence(MockSequence)
35+
.Setup(p => p.RequestOpen(_path, Flags.Read, false))
36+
.Returns(_handle);
37+
SftpSessionMock.InSequence(MockSequence)
38+
.Setup(p => p.CalculateOptimalReadLength(_bufferSize))
39+
.Returns(_readBufferSize);
40+
SftpSessionMock.InSequence(MockSequence)
41+
.Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
42+
.Returns(_writeBufferSize);
43+
SftpSessionMock.InSequence(MockSequence)
44+
.Setup(p => p.IsOpen)
45+
.Returns(true);
46+
SftpSessionMock.InSequence(MockSequence)
47+
.Setup(p => p.RequestClose(_handle));
48+
}
3949

40-
var sequence = new MockSequence();
41-
_sftpSessionMock.InSequence(sequence)
42-
.Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Truncate, true))
43-
.Returns(_handle);
44-
_sftpSessionMock.InSequence(sequence)
45-
.Setup(p => p.CalculateOptimalReadLength(_bufferSize))
46-
.Returns(_readBufferSize);
47-
_sftpSessionMock.InSequence(sequence)
48-
.Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
49-
.Returns(_writeBufferSize);
50-
_sftpSessionMock.InSequence(sequence)
51-
.Setup(p => p.IsOpen)
52-
.Returns(true);
53-
_sftpSessionMock.InSequence(sequence)
54-
.Setup(p => p.RequestClose(_handle));
50+
protected override void Arrange()
51+
{
52+
base.Arrange();
5553

56-
_sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int)_bufferSize);
57-
_sftpFileStream.Close();
54+
_target = new SftpFileStream(SftpSessionMock.Object,
55+
_path,
56+
FileMode.Open,
57+
FileAccess.Read,
58+
(int) _bufferSize);
59+
_target.Close();
5860
}
5961

60-
protected void Act()
62+
protected override void Act()
6163
{
62-
_actual = _sftpFileStream.CanRead;
64+
_actual = _target.CanRead;
6365
}
6466

6567
[TestMethod]

src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessReadWrite.cs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Globalization;
32
using System.IO;
43
using Microsoft.VisualStudio.TestTools.UnitTesting;
54
using Moq;
@@ -8,58 +7,61 @@
87
namespace Renci.SshNet.Tests.Classes.Sftp
98
{
109
[TestClass]
11-
public class SftpFileStreamTest_CanRead_Closed_FileAccessReadWrite
10+
public class SftpFileStreamTest_CanRead_Closed_FileAccessReadWrite : SftpFileStreamTestBase
1211
{
13-
private Mock<ISftpSession> _sftpSessionMock;
12+
private SftpFileStream _target;
1413
private string _path;
15-
private SftpFileStream _sftpFileStream;
1614
private byte[] _handle;
1715
private uint _bufferSize;
1816
private uint _readBufferSize;
1917
private uint _writeBufferSize;
2018
private bool _actual;
2119

22-
[TestInitialize]
23-
public void Setup()
20+
protected override void SetupData()
2421
{
25-
Arrange();
26-
Act();
27-
}
22+
base.SetupData();
2823

29-
protected void Arrange()
30-
{
3124
var random = new Random();
32-
_path = random.Next().ToString(CultureInfo.InvariantCulture);
33-
_handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
25+
_path = random.Next().ToString();
26+
_handle = GenerateRandom(3, random);
3427
_bufferSize = (uint) random.Next(0, 1000);
3528
_readBufferSize = (uint) random.Next(0, 1000);
3629
_writeBufferSize = (uint) random.Next(0, 1000);
30+
}
3731

38-
_sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
32+
protected override void SetupMocks()
33+
{
34+
SftpSessionMock.InSequence(MockSequence)
35+
.Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false))
36+
.Returns(_handle);
37+
SftpSessionMock.InSequence(MockSequence)
38+
.Setup(p => p.CalculateOptimalReadLength(_bufferSize))
39+
.Returns(_readBufferSize);
40+
SftpSessionMock.InSequence(MockSequence)
41+
.Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
42+
.Returns(_writeBufferSize);
43+
SftpSessionMock.InSequence(MockSequence)
44+
.Setup(p => p.IsOpen)
45+
.Returns(true);
46+
SftpSessionMock.InSequence(MockSequence)
47+
.Setup(p => p.RequestClose(_handle));
48+
}
3949

40-
var sequence = new MockSequence();
41-
_sftpSessionMock.InSequence(sequence)
42-
.Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, true))
43-
.Returns(_handle);
44-
_sftpSessionMock.InSequence(sequence)
45-
.Setup(p => p.CalculateOptimalReadLength(_bufferSize))
46-
.Returns(_readBufferSize);
47-
_sftpSessionMock.InSequence(sequence)
48-
.Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
49-
.Returns(_writeBufferSize);
50-
_sftpSessionMock.InSequence(sequence)
51-
.Setup(p => p.IsOpen)
52-
.Returns(true);
53-
_sftpSessionMock.InSequence(sequence)
54-
.Setup(p => p.RequestClose(_handle));
50+
protected override void Arrange()
51+
{
52+
base.Arrange();
5553

56-
_sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.ReadWrite, (int)_bufferSize);
57-
_sftpFileStream.Close();
54+
_target = new SftpFileStream(SftpSessionMock.Object,
55+
_path,
56+
FileMode.Open,
57+
FileAccess.ReadWrite,
58+
(int) _bufferSize);
59+
_target.Close();
5860
}
5961

60-
protected void Act()
62+
protected override void Act()
6163
{
62-
_actual = _sftpFileStream.CanRead;
64+
_actual = _target.CanRead;
6365
}
6466

6567
[TestMethod]

src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_CanRead_Closed_FileAccessWrite.cs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Globalization;
32
using System.IO;
43
using Microsoft.VisualStudio.TestTools.UnitTesting;
54
using Moq;
@@ -8,58 +7,61 @@
87
namespace Renci.SshNet.Tests.Classes.Sftp
98
{
109
[TestClass]
11-
public class SftpFileStreamTest_CanRead_Closed_FileAccessWrite
10+
public class SftpFileStreamTest_CanRead_Closed_FileAccessWrite : SftpFileStreamTestBase
1211
{
13-
private Mock<ISftpSession> _sftpSessionMock;
12+
private SftpFileStream _target;
1413
private string _path;
15-
private SftpFileStream _sftpFileStream;
1614
private byte[] _handle;
1715
private uint _bufferSize;
1816
private uint _readBufferSize;
1917
private uint _writeBufferSize;
2018
private bool _actual;
2119

22-
[TestInitialize]
23-
public void Setup()
20+
protected override void SetupData()
2421
{
25-
Arrange();
26-
Act();
27-
}
22+
base.SetupData();
2823

29-
protected void Arrange()
30-
{
3124
var random = new Random();
32-
_path = random.Next().ToString(CultureInfo.InvariantCulture);
33-
_handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
25+
_path = random.Next().ToString();
26+
_handle = GenerateRandom(3, random);
3427
_bufferSize = (uint) random.Next(0, 1000);
3528
_readBufferSize = (uint) random.Next(0, 1000);
3629
_writeBufferSize = (uint) random.Next(0, 1000);
30+
}
3731

38-
_sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
32+
protected override void SetupMocks()
33+
{
34+
SftpSessionMock.InSequence(MockSequence)
35+
.Setup(p => p.RequestOpen(_path, Flags.Write, false))
36+
.Returns(_handle);
37+
SftpSessionMock.InSequence(MockSequence)
38+
.Setup(p => p.CalculateOptimalReadLength(_bufferSize))
39+
.Returns(_readBufferSize);
40+
SftpSessionMock.InSequence(MockSequence)
41+
.Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
42+
.Returns(_writeBufferSize);
43+
SftpSessionMock.InSequence(MockSequence)
44+
.Setup(p => p.IsOpen)
45+
.Returns(true);
46+
SftpSessionMock.InSequence(MockSequence)
47+
.Setup(p => p.RequestClose(_handle));
48+
}
3949

40-
var sequence = new MockSequence();
41-
_sftpSessionMock.InSequence(sequence)
42-
.Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true))
43-
.Returns(_handle);
44-
_sftpSessionMock.InSequence(sequence)
45-
.Setup(p => p.CalculateOptimalReadLength(_bufferSize))
46-
.Returns(_readBufferSize);
47-
_sftpSessionMock.InSequence(sequence)
48-
.Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
49-
.Returns(_writeBufferSize);
50-
_sftpSessionMock.InSequence(sequence)
51-
.Setup(p => p.IsOpen)
52-
.Returns(true);
53-
_sftpSessionMock.InSequence(sequence)
54-
.Setup(p => p.RequestClose(_handle));
50+
protected override void Arrange()
51+
{
52+
base.Arrange();
5553

56-
_sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize);
57-
_sftpFileStream.Close();
54+
_target = new SftpFileStream(SftpSessionMock.Object,
55+
_path,
56+
FileMode.Open,
57+
FileAccess.Write,
58+
(int) _bufferSize);
59+
_target.Close();
5860
}
5961

60-
protected void Act()
62+
protected override void Act()
6163
{
62-
_actual = _sftpFileStream.CanRead;
64+
_actual = _target.CanRead;
6365
}
6466

6567
[TestMethod]

0 commit comments

Comments
 (0)