forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When the request to open a subsystem fails, close the channel session…
… and throw a SshException. Fixes sshnet#308.
- Loading branch information
Showing
4 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/Renci.SshNet.Tests/Classes/SubsystemSession_Connect_SendSubsystemRequestFails.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using Renci.SshNet.Channels; | ||
using Renci.SshNet.Common; | ||
|
||
namespace Renci.SshNet.Tests.Classes | ||
{ | ||
[TestClass] | ||
public class SubsystemSession_Connect_SendSubsystemRequestFails | ||
{ | ||
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 SshException _actualException; | ||
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>(); | ||
|
||
_sessionMock = new Mock<ISession>(MockBehavior.Strict); | ||
_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(false); | ||
_channelMock.InSequence(_sequence).Setup(p => p.Dispose()); | ||
|
||
_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.Connect(); | ||
Assert.Fail(); | ||
} | ||
catch (SshException ex) | ||
{ | ||
_actualException = ex; | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void ConnectShouldHaveThrownSshException() | ||
{ | ||
Assert.IsNotNull(_actualException); | ||
Assert.IsNull(_actualException.InnerException); | ||
Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Subsystem '{0}' could not be executed.", _subsystemName), _actualException.Message); | ||
} | ||
|
||
[TestMethod] | ||
public void ChannelShouldBeNull() | ||
{ | ||
Assert.IsNull(_subsystemSession.Channel); | ||
} | ||
|
||
[TestMethod] | ||
public void DisconnectHasNeverFired() | ||
{ | ||
Assert.AreEqual(0, _disconnectedRegister.Count); | ||
} | ||
|
||
[TestMethod] | ||
public void ErrorOccurredHasNeverFired() | ||
{ | ||
Assert.AreEqual(0, _errorOccurredRegister.Count); | ||
} | ||
|
||
[TestMethod] | ||
public void IsOpenShouldReturnFalse() | ||
{ | ||
Assert.IsFalse(_subsystemSession.IsOpen); | ||
} | ||
|
||
[TestMethod] | ||
public void DisposeOnChannelShouldBeInvokedOnce() | ||
{ | ||
_channelMock.Verify(p => p.Dispose(), Times.Once); | ||
} | ||
|
||
[TestMethod] | ||
public void ErrorOccuredOnSessionShouldNoLongerBeSignaledViaErrorOccurredOnSubsystemSession() | ||
{ | ||
_sessionMock.Raise(p => p.ErrorOccured += null, new ExceptionEventArgs(new Exception())); | ||
|
||
Assert.AreEqual(0, _errorOccurredRegister.Count); | ||
} | ||
|
||
[TestMethod] | ||
public void DisconnectedOnSessionShouldNoLongerBeSignaledViaDisconnectedOnSubsystemSession() | ||
{ | ||
_sessionMock.Raise(p => p.Disconnected += null, new EventArgs()); | ||
|
||
Assert.AreEqual(0, _disconnectedRegister.Count); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters