Files
ssh.net/test/Renci.SshNet.Tests/Classes/SubsystemSession_OnChannelDataReceived_Connected.cs
mus65 933613e31c Update to MSTest 4 (#1721)
* 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>
2025-11-14 22:09:13 +01:00

117 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Channels;
using Renci.SshNet.Common;
namespace Renci.SshNet.Tests.Classes
{
[TestClass]
public class SubsystemSession_OnChannelDataReceived_Connected
{
private Mock<ISession> _sessionMock;
private Mock<IChannelSession> _channelMock;
private string _subsystemName;
private SubsystemSessionStub _subsystemSession;
private int _operationTimeout;
private IList<EventArgs> _disconnectedRegister;
private IList<ExceptionEventArgs> _errorOccurredRegister;
private ChannelDataEventArgs _channelDataEventArgs;
private MockSequence _sequence;
[TestInitialize]
public void Setup()
{
Arrange();
Act();
}
protected void Arrange()
{
var random = new Random();
_subsystemName = random.Next().ToString(CultureInfo.InvariantCulture);
_operationTimeout = 30000;
_disconnectedRegister = new List<EventArgs>();
_errorOccurredRegister = new List<ExceptionEventArgs>();
_channelDataEventArgs = new ChannelDataEventArgs(
(uint)random.Next(0, int.MaxValue),
new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) });
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
_sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
_channelMock = new Mock<IChannelSession>(MockBehavior.Strict);
_sequence = new MockSequence();
_sessionMock.InSequence(_sequence).Setup(p => p.CreateChannelSession()).Returns(_channelMock.Object);
_channelMock.InSequence(_sequence).Setup(p => p.Open());
_channelMock.InSequence(_sequence).Setup(p => p.SendSubsystemRequest(_subsystemName)).Returns(true);
_subsystemSession = new SubsystemSessionStub(
_sessionMock.Object,
_subsystemName,
_operationTimeout);
_subsystemSession.Disconnected += (sender, args) => _disconnectedRegister.Add(args);
_subsystemSession.ErrorOccurred += (sender, args) => _errorOccurredRegister.Add(args);
_subsystemSession.Connect();
}
protected void Act()
{
_channelMock.Raise(s => s.DataReceived += null, _channelDataEventArgs);
}
[TestMethod]
public void DisconnectHasNeverFired()
{
Assert.IsEmpty(_disconnectedRegister);
}
[TestMethod]
public void ErrorOccurredHasNeverFired()
{
Assert.IsEmpty(_errorOccurredRegister);
}
[TestMethod]
public void OnDataReceivedShouldBeInvokedOnce()
{
Assert.HasCount(1, _subsystemSession.OnDataReceivedInvocations);
var received = _subsystemSession.OnDataReceivedInvocations[0];
Assert.AreEqual(_channelDataEventArgs.Data, received.Data);
}
[TestMethod]
public void IsOpenShouldReturnTrueWhenChannelIsOpen()
{
_channelMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
Assert.IsTrue(_subsystemSession.IsOpen);
_channelMock.Verify(p => p.IsOpen, Times.Once);
}
[TestMethod]
public void IsOpenShouldReturnFalseWhenSessionIsNotConnected()
{
_channelMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(false);
Assert.IsFalse(_subsystemSession.IsOpen);
_channelMock.Verify(p => p.IsOpen, Times.Once);
}
[TestMethod]
public void DisposeOnChannelShouldNeverBeInvoked()
{
_channelMock.Verify(p => p.Dispose(), Times.Never);
}
}
}