| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 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_SendData_NeverConnected
- {
- 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 InvalidOperationException _actualException;
- private byte[] _data;
- 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>();
- _data = 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.IsConnected).Returns(true);
- _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);
- }
- protected void Act()
- {
- try
- {
- _subsystemSession.SendData(_data);
- }
- catch (InvalidOperationException ex)
- {
- _actualException = ex;
- }
- }
- [TestMethod]
- public void SendDataShouldHaveThrownInvalidOperationException()
- {
- Assert.IsNotNull(_actualException);
- Assert.IsNull(_actualException.InnerException);
- Assert.AreEqual("The session is not open.", _actualException.Message);
- }
- [TestMethod]
- public void DisconnectHasNeverFired()
- {
- Assert.AreEqual(0, _disconnectedRegister.Count);
- }
- [TestMethod]
- public void ErrorOccurredHasNeverFired()
- {
- Assert.AreEqual(0, _errorOccurredRegister.Count);
- }
- [TestMethod]
- public void SendDataOnChannelShouldNeverBeInvoked()
- {
- _channelMock.Verify(p => p.SendData(_data), Times.Never);
- }
- [TestMethod]
- public void IsOpenShouldReturnFalse()
- {
- Assert.IsFalse(_subsystemSession.IsOpen);
- }
- }
- }
|