Files
ssh.net/test/Renci.SshNet.Tests/Classes/BaseClientTest_ConnectAsync_Timeout.cs
dependabot[bot] 03ae6bfa0f Bump dependencies (#1682)
* Bump the dependencies group with 4 updates

Bumps BouncyCastle.Cryptography from 2.6.1 to 2.6.2
Bumps Meziantou.Analyzer from 2.0.205 to 2.0.210
Bumps MSTest from 3.9.3 to 3.10.0
Bumps SonarAnalyzer.CSharp from 10.13.0.120203 to 10.15.0.120848

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2025-08-02 17:57:09 +02:00

76 lines
2.5 KiB
C#

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
#if !NET
using Renci.SshNet.Abstractions;
#endif
using Renci.SshNet.Common;
using Renci.SshNet.Connection;
namespace Renci.SshNet.Tests.Classes
{
[TestClass]
public class BaseClientTest_ConnectAsync_Timeout
{
private BaseClient _client;
[TestInitialize]
public void Init()
{
var sessionMock = new Mock<ISession>();
sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
var serviceFactoryMock = new Mock<IServiceFactory>();
var socketFactoryMock = new Mock<ISocketFactory>();
sessionMock.Setup(p => p.ConnectAsync(It.IsAny<CancellationToken>()))
.Returns<CancellationToken>(c => Task.Delay(Timeout.Infinite, c));
serviceFactoryMock.Setup(p => p.CreateSocketFactory())
.Returns(socketFactoryMock.Object);
var connectionInfo = new ConnectionInfo("host", "user", new PasswordAuthenticationMethod("user", "pwd"))
{
Timeout = TimeSpan.FromSeconds(1)
};
serviceFactoryMock.Setup(p => p.CreateSession(connectionInfo, socketFactoryMock.Object))
.Returns(sessionMock.Object);
_client = new MyClient(connectionInfo, false, serviceFactoryMock.Object);
}
[TestMethod]
public async Task ConnectAsyncWithTimeoutThrowsSshTimeoutException()
{
await Assert.ThrowsExactlyAsync<SshOperationTimeoutException>(() => _client.ConnectAsync(CancellationToken.None));
}
[TestMethod]
public async Task ConnectAsyncWithCancelledTokenThrowsOperationCancelledException()
{
using var cancellationTokenSource = new CancellationTokenSource();
await cancellationTokenSource.CancelAsync();
await Assert.ThrowsExactlyAsync<OperationCanceledException>(() => _client.ConnectAsync(cancellationTokenSource.Token));
}
[TestCleanup]
public void Cleanup()
{
_client?.Dispose();
}
private class MyClient : BaseClient
{
public MyClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory) : base(connectionInfo, ownsConnectionInfo, serviceFactory)
{
}
}
}
}