Skip to content

Add unit tests for task-based asynchronous API #906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7d5ccb8
POC unit tests
IgorMilavec Dec 15, 2021
2a47c9e
Modify AppVeyor test script to only run net472 and net5.0
IgorMilavec Dec 16, 2021
ba7ecbb
Fix ScpClientTest_*.DisposeOnPipeStreamShouldBeInvokedOnce in modern …
IgorMilavec Dec 16, 2021
81b3a16
Fix runtime and culture dependant tests. Should we test this at all?
IgorMilavec Dec 16, 2021
be97986
Make ForwardedPortDynamic exception visible
IgorMilavec Dec 16, 2021
f91c5f1
Fix PipeStream_Close_BlockingWrite synchronization
IgorMilavec Dec 16, 2021
8c66267
Removed CountdownEventTest for FEATURE_THREAD_COUNTDOWNEVENT
IgorMilavec Dec 16, 2021
5304296
Remove Thread.Abort()
IgorMilavec Dec 16, 2021
a7a9474
Fix another runtime dependant string
IgorMilavec Dec 16, 2021
de9faec
Raise AppVeyor number of ports to avoid dynamic port exhaustion
IgorMilavec Dec 17, 2021
5bc16c8
Fix previous "speeedup" of ChannelForwardedTcpipTest_Dispose_SessionI…
IgorMilavec Dec 17, 2021
36b751b
Speedup tests
IgorMilavec Dec 17, 2021
362910f
Ignore SocketError.TryAgain when expecting SocketError.HostNotFound
IgorMilavec Dec 17, 2021
c1a657a
Better ObjectDisposedHandling in AsyncSocketListener
IgorMilavec Dec 17, 2021
bae6c54
Handle SocketError.ConnectionAborted in ForwardedPortDynamic
IgorMilavec Dec 17, 2021
48e1af3
Rename test to match implementation
IgorMilavec Dec 17, 2021
f0a8f5f
Fix BaseClientTest_Connected_KeepAliveInterval_NotNegativeOne timing
IgorMilavec Dec 17, 2021
382649b
Set C# 7.3 in Tests.csproj to limit intellisense's suggestions under …
IgorMilavec Dec 17, 2021
07c1535
Add SftpClientTest.*Async
IgorMilavec Dec 17, 2021
0030b35
Add SftpFileStreamTest_OpenAsync_*
IgorMilavec Dec 19, 2021
2fbdb46
Add SftpFileStreamTest_WriteAsync_*
IgorMilavec Dec 22, 2021
3547504
Add SftpFileStreamTest_ReadAsync_*
IgorMilavec Dec 22, 2021
3506707
Remove unrelated changes
IgorMilavec Feb 12, 2022
9f45e7f
Align AppVeyor script with Test project target frameworks
IgorMilavec Feb 12, 2022
a53f35b
Revert unrelated changes
IgorMilavec Feb 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ build:

test_script:
- cmd: >-
vstest.console /logger:Appveyor src\Renci.SshNet.Tests\bin\Debug\net40\Renci.SshNet.Tests.dll /TestCaseFilter:"TestCategory!=integration&TestCategory!=LongRunning"
vstest.console /logger:Appveyor src\Renci.SshNet.Tests\bin\Debug\net35\Renci.SshNet.Tests.dll /TestCaseFilter:"TestCategory!=integration&TestCategory!=LongRunning"

vstest.console /logger:Appveyor src\Renci.SshNet.Tests\bin\Debug\net35\Renci.SshNet.Tests.dll /TestCaseFilter:"TestCategory!=integration&TestCategory!=LongRunning"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd first like to discuss dropping .NET 3.5 support.
Perhaps we'll have a final release supporting the legacy frameworks.
As I said, let's discuss this first.

vstest.console /logger:Appveyor src\Renci.SshNet.Tests\bin\Debug\net472\Renci.SshNet.Tests.dll /TestCaseFilter:"TestCategory!=integration&TestCategory!=LongRunning"
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void IsConnectedOnSessionShouldBeInvokedOnce()
}

[TestMethod]
public void SendMessageOnSessionShouldBeInvokedThreeTimes()
public void SendMessageOnSessionShouldBeInvokedOneTime()
{
// allow keep-alive to be sent once
Thread.Sleep(100);
Expand Down
4 changes: 4 additions & 0 deletions src/Renci.SshNet.Tests/Classes/Common/PacketDumpTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public void Create_ByteArrayAndIndentLevel_IndentLevelLessThanZero()
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
#if NETFRAMEWORK
Assert.AreEqual(string.Format("Cannot be less than zero.{0}Parameter name: {1}", Environment.NewLine, ex.ParamName), ex.Message);
#else
Assert.AreEqual(string.Format("Cannot be less than zero. (Parameter '{1}')", Environment.NewLine, ex.ParamName), ex.Message);
#endif
Assert.AreEqual("indentLevel", ex.ParamName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ protected override void SetupMocks()
.Setup(p => p.SendExecRequest(string.Format("scp -prf {0}", _transformedPath)))
.Returns(false);
_channelSessionMock.InSequence(sequence).Setup(p => p.Dispose());
#if NET35
_pipeStreamMock.As<IDisposable>().InSequence(sequence).Setup(p => p.Dispose());

// On .NET Core, Dispose() in turn invokes Close() and since we're not mocking
// an interface, we need to expect this call as well
_pipeStreamMock.Setup(p => p.Close());
#else
_pipeStreamMock.InSequence(sequence).Setup(p => p.Close());
#endif
}

protected override void Arrange()
Expand Down Expand Up @@ -106,7 +106,11 @@ public void DisposeOnChannelShouldBeInvokedOnce()
[TestMethod]
public void DisposeOnPipeStreamShouldBeInvokedOnce()
{
#if NET35
_pipeStreamMock.As<IDisposable>().Verify(p => p.Dispose(), Times.Once);
#else
_pipeStreamMock.Verify(p => p.Close(), Times.Once);
#endif
Comment on lines +109 to +113
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IgorMilavec No need to move these changes (here and elsewhere) out of this PR.

}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ protected override void SetupMocks()
_channelSessionMock.InSequence(sequence)
.Setup(p => p.SendExecRequest(string.Format("scp -pf {0}", _transformedPath))).Returns(false);
_channelSessionMock.InSequence(sequence).Setup(p => p.Dispose());
#if NET35
_pipeStreamMock.As<IDisposable>().InSequence(sequence).Setup(p => p.Dispose());

// On .NET Core, Dispose() in turn invokes Close() and since we're not mocking
// an interface, we need to expect this call as well
_pipeStreamMock.Setup(p => p.Close());
#else
_pipeStreamMock.InSequence(sequence).Setup(p => p.Close());
#endif
}

protected override void Arrange()
Expand Down Expand Up @@ -105,7 +105,11 @@ public void DisposeOnChannelShouldBeInvokedOnce()
[TestMethod]
public void DisposeOnPipeStreamShouldBeInvokedOnce()
{
#if NET35
_pipeStreamMock.As<IDisposable>().Verify(p => p.Dispose(), Times.Once);
#else
_pipeStreamMock.Verify(p => p.Close(), Times.Once);
#endif
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ protected override void SetupMocks()
.Setup(p => p.SendExecRequest(string.Format("scp -f {0}", _transformedPath)))
.Returns(false);
_channelSessionMock.InSequence(sequence).Setup(p => p.Dispose());
#if NET35
_pipeStreamMock.As<IDisposable>().InSequence(sequence).Setup(p => p.Dispose());

// On .NET Core, Dispose() in turn invokes Close() and since we're not mocking
// an interface, we need to expect this call as well
_pipeStreamMock.Setup(p => p.Close());
#else
_pipeStreamMock.InSequence(sequence).Setup(p => p.Close());
#endif
}

protected override void Arrange()
Expand Down Expand Up @@ -116,7 +116,11 @@ public void DisposeOnChannelShouldBeInvokedOnce()
[TestMethod]
public void DisposeOnPipeStreamShouldBeInvokedOnce()
{
#if NET35
_pipeStreamMock.As<IDisposable>().Verify(p => p.Dispose(), Times.Once);
#else
_pipeStreamMock.Verify(p => p.Close(), Times.Once);
#endif
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ protected override void SetupMocks()
.Setup(p => p.SendExecRequest(string.Format("scp -r -p -d -t {0}", _transformedPath)))
.Returns(false);
_channelSessionMock.InSequence(sequence).Setup(p => p.Dispose());
#if NET35
_pipeStreamMock.As<IDisposable>().InSequence(sequence).Setup(p => p.Dispose());

// On .NET Core, Dispose() in turn invokes Close() and since we're not mocking
// an interface, we need to expect this call as well
_pipeStreamMock.Setup(p => p.Close());
#else
_pipeStreamMock.InSequence(sequence).Setup(p => p.Close());
#endif
}

protected override void Arrange()
Expand Down Expand Up @@ -105,7 +105,11 @@ public void DisposeOnChannelShouldBeInvokedOnce()
[TestMethod]
public void DisposeOnPipeStreamShouldBeInvokedOnce()
{
#if NET35
_pipeStreamMock.As<IDisposable>().Verify(p => p.Dispose(), Times.Once);
#else
_pipeStreamMock.Verify(p => p.Close(), Times.Once);
#endif
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ protected override void SetupMocks()
.Setup(p => p.SendExecRequest(string.Format("scp -t -d {0}", _transformedPath)))
.Returns(false);
_channelSessionMock.InSequence(sequence).Setup(p => p.Dispose());
#if NET35
_pipeStreamMock.As<IDisposable>().InSequence(sequence).Setup(p => p.Dispose());

// On .NET Core, Dispose() in turn invokes Close() and since we're not mocking
// an interface, we need to expect this call as well
_pipeStreamMock.Setup(p => p.Close());
}
#else
_pipeStreamMock.InSequence(sequence).Setup(p => p.Close());
#endif
}

protected override void Arrange()
{
Expand Down Expand Up @@ -122,7 +122,11 @@ public void DisposeOnChannelShouldBeInvokedOnce()
[TestMethod]
public void DisposeOnPipeStreamShouldBeInvokedOnce()
{
#if NET35
_pipeStreamMock.As<IDisposable>().Verify(p => p.Dispose(), Times.Once);
#else
_pipeStreamMock.Verify(p => p.Close(), Times.Once);
#endif
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ protected override void SetupMocks()
p => p.SendData(It.Is<byte[]>(b => b.SequenceEqual(new byte[] {0}))));
_pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(0);
_channelSessionMock.InSequence(sequence).Setup(p => p.Dispose());
#if NET35
_pipeStreamMock.As<IDisposable>().InSequence(sequence).Setup(p => p.Dispose());

// On .NET Core, Dispose() in turn invokes Close() and since we're not mocking
// an interface, we need to expect this call as well
_pipeStreamMock.Setup(p => p.Close());
#else
_pipeStreamMock.InSequence(sequence).Setup(p => p.Close());
#endif
}

protected override void Arrange()
Expand Down Expand Up @@ -134,7 +134,11 @@ public void DisposeOnChannelShouldBeInvokedOnce()
[TestMethod]
public void DisposeOnPipeStreamShouldBeInvokedOnce()
{
#if NET35
_pipeStreamMock.As<IDisposable>().Verify(p => p.Dispose(), Times.Once);
#else
_pipeStreamMock.Verify(p => p.Close(), Times.Once);
#endif
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ protected override void SetupMocks()
.Setup(p => p.SendExecRequest(string.Format("scp -t -d {0}", _transformedPath)))
.Returns(false);
_channelSessionMock.InSequence(sequence).Setup(p => p.Dispose());
#if NET35
_pipeStreamMock.As<IDisposable>().InSequence(sequence).Setup(p => p.Dispose());

// On .NET Core, Dispose() in turn invokes Close() and since we're not mocking
// an interface, we need to expect this call as well
_pipeStreamMock.Setup(p => p.Close());
#else
_pipeStreamMock.InSequence(sequence).Setup(p => p.Close());
#endif
}

protected override void Arrange()
Expand Down Expand Up @@ -119,7 +119,11 @@ public void DisposeOnChannelShouldBeInvokedOnce()
[TestMethod]
public void DisposeOnPipeStreamShouldBeInvokedOnce()
{
#if NET35
_pipeStreamMock.As<IDisposable>().Verify(p => p.Dispose(), Times.Once);
#else
_pipeStreamMock.Verify(p => p.Close(), Times.Once);
#endif
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#if FEATURE_TAP
using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Sftp;

namespace Renci.SshNet.Tests.Classes.Sftp
{
public abstract class SftpFileStreamAsyncTestBase
{
internal Mock<ISftpSession> SftpSessionMock;
protected MockSequence MockSequence;

protected virtual Task ArrangeAsync()
{
SetupData();
CreateMocks();
SetupMocks();
return Task.CompletedTask;
}

protected virtual void SetupData()
{
MockSequence = new MockSequence();
}

protected abstract void SetupMocks();

private void CreateMocks()
{
SftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
}

[TestInitialize]
public async Task SetUpAsync()
{
await ArrangeAsync();
await ActAsync();
}

protected abstract Task ActAsync();

protected byte[] GenerateRandom(int length)
{
return GenerateRandom(length, new Random());
}

protected byte[] GenerateRandom(int length, Random random)
{
var buffer = new byte[length];
random.NextBytes(buffer);
return buffer;
}

protected byte[] GenerateRandom(uint length)
{
return GenerateRandom(length, new Random());
}

protected byte[] GenerateRandom(uint length, Random random)
{
var buffer = new byte[length];
random.NextBytes(buffer);
return buffer;
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#if FEATURE_TAP
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Sftp;

namespace Renci.SshNet.Tests.Classes.Sftp
{
[TestClass]
public class SftpFileStreamTest_OpenAsync_FileAccessInvalid : SftpFileStreamAsyncTestBase
{
private Random _random;
private string _path;
private FileMode _fileMode;
private FileAccess _fileAccess;
private int _bufferSize;
private ArgumentOutOfRangeException _actualException;

protected override void SetupData()
{
base.SetupData();

_random = new Random();
_path = _random.Next().ToString();
_fileMode = FileMode.Open;
_fileAccess = 0;
_bufferSize = _random.Next(5, 1000);
}

protected override void SetupMocks()
{
}

protected override async Task ActAsync()
{
try
{
await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, default);
Assert.Fail();
}
catch (ArgumentOutOfRangeException ex)
{
_actualException = ex;
}
}

[TestMethod]
public void CtorShouldHaveThrownArgumentException()
{
Assert.IsNotNull(_actualException);
Assert.IsNull(_actualException.InnerException);
Assert.AreEqual("access", _actualException.ParamName);
}
}
}
#endif
Loading