mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-10 09:15:47 +00:00
933613e31c
* Update to MSTest 4 * fix TestMethodForPlatformAttribute replace Execute with ExecuteAsync and fix MSTEST0057 https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0057 (link currently dead) * fix compilation error * fix MSTEST0037 https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0037 * fix MSTEST0052 https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0052 * fix MSTEST0045 Fixing this properly would require the tests to respect testContext.CancellationToken. I'm not sure this is worth fixing or how to even do it for the sync methods. https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0045 * fix MSTEST0001 I assume that parallelization would break a lot of stuff. https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0001 * Workaround for new Sonar warnings because of MSTest4 * revert analyzer fixes in OrderedDictionaryTest * use custom sync console logger for MSTest to work around https://github.com/microsoft/testfx/issues/6457 * remove redundant args --------- Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com> Co-authored-by: Rob Hague <rob.hague00@gmail.com>
247 lines
7.5 KiB
C#
247 lines
7.5 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Net.Sockets;
|
|
using System.Threading;
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
using Renci.SshNet.Common;
|
|
using Renci.SshNet.Messages.Transport;
|
|
using Renci.SshNet.Tests.Common;
|
|
|
|
namespace Renci.SshNet.Tests.Classes
|
|
{
|
|
[TestClass]
|
|
public class SessionTest_Connected_ServerShutsDownSocket : SessionTest_ConnectedBase
|
|
{
|
|
protected override void Act()
|
|
{
|
|
ServerSocket.Shutdown(SocketShutdown.Send);
|
|
|
|
// give session some time to process socket shutdown
|
|
Thread.Sleep(200);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void IsConnectedShouldReturnFalse()
|
|
{
|
|
Assert.IsFalse(Session.IsConnected);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void DisconnectShouldFinishImmediately()
|
|
{
|
|
var stopwatch = new Stopwatch();
|
|
stopwatch.Start();
|
|
|
|
Session.Disconnect();
|
|
|
|
stopwatch.Stop();
|
|
Assert.IsLessThan(500, stopwatch.ElapsedMilliseconds);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void DisconnectedIsNeverRaised()
|
|
{
|
|
Assert.IsEmpty(DisconnectedRegister);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void DisconnectReceivedIsNeverRaised()
|
|
{
|
|
Assert.IsEmpty(DisconnectReceivedRegister);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ErrorOccurredIsRaisedOnce()
|
|
{
|
|
Assert.HasCount(1, ErrorOccurredRegister, ErrorOccurredRegister.AsString());
|
|
|
|
var errorOccurred = ErrorOccurredRegister[0];
|
|
Assert.IsNotNull(errorOccurred);
|
|
|
|
var exception = errorOccurred.Exception;
|
|
Assert.IsNotNull(exception);
|
|
Assert.AreEqual(typeof(SshConnectionException), exception.GetType());
|
|
|
|
var connectionException = (SshConnectionException)exception;
|
|
Assert.AreEqual(DisconnectReason.ConnectionLost, connectionException.DisconnectReason);
|
|
Assert.IsNull(connectionException.InnerException);
|
|
Assert.AreEqual("An established connection was aborted by the server.", connectionException.Message);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void DisposeShouldFinishImmediately()
|
|
{
|
|
var stopwatch = new Stopwatch();
|
|
stopwatch.Start();
|
|
|
|
Session.Dispose();
|
|
|
|
stopwatch.Stop();
|
|
Assert.IsLessThan(500, stopwatch.ElapsedMilliseconds);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ReceiveOnServerSocketShouldReturnZero()
|
|
{
|
|
var buffer = new byte[1];
|
|
|
|
var actual = ServerSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
|
|
|
|
Assert.AreEqual(0, actual);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void SendMessageShouldThrowSshConnectionException()
|
|
{
|
|
try
|
|
{
|
|
Session.SendMessage(new IgnoreMessage());
|
|
Assert.Fail();
|
|
}
|
|
catch (SshConnectionException ex)
|
|
{
|
|
Assert.IsNull(ex.InnerException);
|
|
Assert.AreEqual("Client not connected.", ex.Message);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ISession_MessageListenerCompletedShouldBeSignaled()
|
|
{
|
|
var session = (ISession)Session;
|
|
|
|
Assert.IsNotNull(session.MessageListenerCompleted);
|
|
Assert.IsTrue(session.MessageListenerCompleted.WaitOne());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ISession_SendMessageShouldThrowSshConnectionException()
|
|
{
|
|
var session = (ISession)Session;
|
|
|
|
try
|
|
{
|
|
session.SendMessage(new IgnoreMessage());
|
|
Assert.Fail();
|
|
}
|
|
catch (SshConnectionException ex)
|
|
{
|
|
Assert.IsNull(ex.InnerException);
|
|
Assert.AreEqual("Client not connected.", ex.Message);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ISession_TrySendMessageShouldReturnFalse()
|
|
{
|
|
var session = (ISession)Session;
|
|
|
|
var actual = session.TrySendMessage(new IgnoreMessage());
|
|
|
|
Assert.IsFalse(actual);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ISession_WaitOnHandle_WaitHandle_ShouldThrowSshConnectionExceptionDetailingAbortedConnection()
|
|
{
|
|
var session = (ISession)Session;
|
|
var waitHandle = new ManualResetEvent(false);
|
|
|
|
try
|
|
{
|
|
session.WaitOnHandle(waitHandle);
|
|
Assert.Fail();
|
|
}
|
|
catch (SshConnectionException ex)
|
|
{
|
|
Assert.IsNull(ex.InnerException);
|
|
Assert.AreEqual("An established connection was aborted by the server.", ex.Message);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ISession_WaitOnHandle_WaitHandleAndTimeout_ShouldThrowSshConnectionExceptionDetailingAbortedConnection()
|
|
{
|
|
var session = (ISession)Session;
|
|
var waitHandle = new ManualResetEvent(false);
|
|
|
|
try
|
|
{
|
|
session.WaitOnHandle(waitHandle, Timeout.InfiniteTimeSpan);
|
|
Assert.Fail();
|
|
}
|
|
catch (SshConnectionException ex)
|
|
{
|
|
Assert.IsNull(ex.InnerException);
|
|
Assert.AreEqual("An established connection was aborted by the server.", ex.Message);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ISession_TryWait_WaitHandleAndTimeout_ShouldReturnDisconnected()
|
|
{
|
|
var session = (ISession)Session;
|
|
var waitHandle = new ManualResetEvent(false);
|
|
|
|
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan);
|
|
|
|
Assert.AreEqual(WaitResult.Disconnected, result);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ISession_TryWait_WaitHandleAndTimeout_ShouldThrowArgumentNullExceptionWhenWaitHandleIsNull()
|
|
{
|
|
var session = (ISession)Session;
|
|
const WaitHandle waitHandle = null;
|
|
var timeout = TimeSpan.FromMinutes(5);
|
|
|
|
try
|
|
{
|
|
_ = session.TryWait(waitHandle, timeout);
|
|
Assert.Fail();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
Assert.IsNull(ex.InnerException);
|
|
Assert.AreEqual("waitHandle", ex.ParamName);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ISession_TryWait_WaitHandleAndTimeoutAndException_ShouldReturnDisconnected()
|
|
{
|
|
var session = (ISession)Session;
|
|
var waitHandle = new ManualResetEvent(false);
|
|
|
|
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan, out var exception);
|
|
|
|
Assert.AreEqual(WaitResult.Disconnected, result);
|
|
Assert.IsNull(exception);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ISession_TryWait_WaitHandleAndTimeoutAndException_ShouldThrowArgumentNullExceptionWhenWaitHandleIsNull()
|
|
{
|
|
var session = (ISession)Session;
|
|
const WaitHandle waitHandle = null;
|
|
var timeout = TimeSpan.FromMinutes(5);
|
|
Exception exception = null;
|
|
|
|
try
|
|
{
|
|
_ = session.TryWait(waitHandle, timeout, out exception);
|
|
Assert.Fail();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
Assert.IsNull(ex.InnerException);
|
|
Assert.AreEqual("waitHandle", ex.ParamName);
|
|
}
|
|
|
|
Assert.IsNull(exception);
|
|
}
|
|
}
|
|
}
|