Skip to content

Commit 660b075

Browse files
committed
Added more tests for issue #338.
1 parent 7932ba7 commit 660b075

File tree

2 files changed

+285
-0
lines changed

2 files changed

+285
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System;
2+
using System.Reflection;
3+
using System.Threading;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Moq;
6+
using Renci.SshNet.Common;
7+
using Renci.SshNet.NetConf;
8+
using Renci.SshNet.Security;
9+
10+
namespace Renci.SshNet.Tests.Classes
11+
{
12+
[TestClass]
13+
public class NetConfClientTest_Connect_NetConfSessionConnectFailure
14+
{
15+
private Mock<IServiceFactory> _serviceFactoryMock;
16+
private Mock<ISession> _sessionMock;
17+
private Mock<INetConfSession> _netConfSessionMock;
18+
private ConnectionInfo _connectionInfo;
19+
private ApplicationException _netConfSessionConnectionException;
20+
private NetConfClient _netConfClient;
21+
private ApplicationException _actualException;
22+
23+
[TestInitialize]
24+
public void Setup()
25+
{
26+
Arrange();
27+
Act();
28+
}
29+
30+
private void Arrange()
31+
{
32+
SetupData();
33+
CreateMocks();
34+
SetupMocks();
35+
36+
_netConfClient = new NetConfClient(_connectionInfo, false, _serviceFactoryMock.Object);
37+
}
38+
39+
private void SetupData()
40+
{
41+
_connectionInfo = new ConnectionInfo("host", "user", new NoneAuthenticationMethod("userauth"));
42+
_netConfSessionConnectionException = new ApplicationException();
43+
}
44+
45+
private void CreateMocks()
46+
{
47+
_serviceFactoryMock = new Mock<IServiceFactory>(MockBehavior.Strict);
48+
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
49+
_netConfSessionMock = new Mock<INetConfSession>(MockBehavior.Strict);
50+
}
51+
52+
private void SetupMocks()
53+
{
54+
var sequence = new MockSequence();
55+
56+
_serviceFactoryMock.InSequence(sequence)
57+
.Setup(p => p.CreateSession(_connectionInfo))
58+
.Returns(_sessionMock.Object);
59+
_sessionMock.InSequence(sequence)
60+
.Setup(p => p.Connect());
61+
_serviceFactoryMock.InSequence(sequence)
62+
.Setup(p => p.CreateNetConfSession(_sessionMock.Object, -1))
63+
.Returns(_netConfSessionMock.Object);
64+
_netConfSessionMock.InSequence(sequence)
65+
.Setup(p => p.Connect())
66+
.Throws(_netConfSessionConnectionException);
67+
_netConfSessionMock.InSequence(sequence)
68+
.Setup(p => p.Dispose());
69+
_sessionMock.InSequence(sequence)
70+
.Setup(p => p.Dispose());
71+
}
72+
73+
private void Act()
74+
{
75+
try
76+
{
77+
_netConfClient.Connect();
78+
Assert.Fail();
79+
}
80+
catch (ApplicationException ex)
81+
{
82+
_actualException = ex;
83+
}
84+
}
85+
86+
[TestMethod]
87+
public void ConnectShouldHaveThrownApplicationException()
88+
{
89+
Assert.IsNotNull(_actualException);
90+
Assert.AreSame(_netConfSessionConnectionException, _actualException);
91+
}
92+
93+
[TestMethod]
94+
public void SessionShouldBeNull()
95+
{
96+
Assert.IsNull(_netConfClient.Session);
97+
}
98+
99+
[TestMethod]
100+
public void NetConfSessionShouldBeNull()
101+
{
102+
Assert.IsNull(_netConfClient.NetConfSession);
103+
}
104+
105+
[TestMethod]
106+
public void ErrorOccuredOnSessionShouldNoLongerBeSignaledViaErrorOccurredOnNetConfClient()
107+
{
108+
var errorOccurredSignalCount = 0;
109+
110+
_netConfClient.ErrorOccurred += (sender, args) => Interlocked.Increment(ref errorOccurredSignalCount);
111+
112+
_sessionMock.Raise(p => p.ErrorOccured += null, new ExceptionEventArgs(new Exception()));
113+
114+
Assert.AreEqual(0, errorOccurredSignalCount);
115+
}
116+
117+
[TestMethod]
118+
public void HostKeyReceivedOnSessionShouldNoLongerBeSignaledViaHostKeyReceivedOnSftpClient()
119+
{
120+
var hostKeyReceivedSignalCount = 0;
121+
122+
_netConfClient.HostKeyReceived += (sender, args) => Interlocked.Increment(ref hostKeyReceivedSignalCount);
123+
124+
_sessionMock.Raise(p => p.HostKeyReceived += null, new HostKeyEventArgs(GetKeyHostAlgorithm()));
125+
126+
Assert.AreEqual(0, hostKeyReceivedSignalCount);
127+
}
128+
129+
private static KeyHostAlgorithm GetKeyHostAlgorithm()
130+
{
131+
var executingAssembly = Assembly.GetExecutingAssembly();
132+
133+
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
134+
{
135+
var privateKey = new PrivateKeyFile(s);
136+
return (KeyHostAlgorithm)privateKey.HostKey;
137+
}
138+
}
139+
}
140+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using System;
2+
using System.Reflection;
3+
using System.Threading;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Moq;
6+
using Renci.SshNet.Common;
7+
using Renci.SshNet.Security;
8+
using Renci.SshNet.Sftp;
9+
10+
namespace Renci.SshNet.Tests.Classes
11+
{
12+
[TestClass]
13+
public class SftpClientTest_Connect_SftpSessionConnectFailure
14+
{
15+
private Mock<IServiceFactory> _serviceFactoryMock;
16+
private Mock<ISession> _sessionMock;
17+
private Mock<ISftpResponseFactory> _sftpResponseFactoryMock;
18+
private Mock<ISftpSession> _sftpSessionMock;
19+
private ConnectionInfo _connectionInfo;
20+
private ApplicationException _sftpSessionConnectionException;
21+
private SftpClient _sftpClient;
22+
private ApplicationException _actualException;
23+
24+
[TestInitialize]
25+
public void Setup()
26+
{
27+
Arrange();
28+
Act();
29+
}
30+
31+
private void Arrange()
32+
{
33+
SetupData();
34+
CreateMocks();
35+
SetupMocks();
36+
37+
_sftpClient = new SftpClient(_connectionInfo, false, _serviceFactoryMock.Object);
38+
}
39+
40+
private void SetupData()
41+
{
42+
_connectionInfo = new ConnectionInfo("host", "user", new NoneAuthenticationMethod("userauth"));
43+
_sftpSessionConnectionException = new ApplicationException();
44+
}
45+
46+
private void CreateMocks()
47+
{
48+
_serviceFactoryMock = new Mock<IServiceFactory>(MockBehavior.Strict);
49+
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
50+
_sftpResponseFactoryMock = new Mock<ISftpResponseFactory>(MockBehavior.Strict);
51+
_sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
52+
}
53+
54+
private void SetupMocks()
55+
{
56+
var sequence = new MockSequence();
57+
58+
_serviceFactoryMock.InSequence(sequence)
59+
.Setup(p => p.CreateSession(_connectionInfo))
60+
.Returns(_sessionMock.Object);
61+
_sessionMock.InSequence(sequence)
62+
.Setup(p => p.Connect());
63+
_serviceFactoryMock.InSequence(sequence)
64+
.Setup(p => p.CreateSftpResponseFactory())
65+
.Returns(_sftpResponseFactoryMock.Object);
66+
_serviceFactoryMock.InSequence(sequence)
67+
.Setup(p => p.CreateSftpSession(_sessionMock.Object, -1, _connectionInfo.Encoding, _sftpResponseFactoryMock.Object))
68+
.Returns(_sftpSessionMock.Object);
69+
_sftpSessionMock.InSequence(sequence)
70+
.Setup(p => p.Connect())
71+
.Throws(_sftpSessionConnectionException);
72+
_sftpSessionMock.InSequence(sequence)
73+
.Setup(p => p.Dispose());
74+
_sessionMock.InSequence(sequence)
75+
.Setup(p => p.Dispose());
76+
}
77+
78+
private void Act()
79+
{
80+
try
81+
{
82+
_sftpClient.Connect();
83+
Assert.Fail();
84+
}
85+
catch (ApplicationException ex)
86+
{
87+
_actualException = ex;
88+
}
89+
}
90+
91+
[TestMethod]
92+
public void ConnectShouldHaveThrownApplicationException()
93+
{
94+
Assert.IsNotNull(_actualException);
95+
Assert.AreSame(_sftpSessionConnectionException, _actualException);
96+
}
97+
98+
[TestMethod]
99+
public void SessionShouldBeNull()
100+
{
101+
Assert.IsNull(_sftpClient.Session);
102+
}
103+
104+
[TestMethod]
105+
public void SftpSessionShouldBeNull()
106+
{
107+
Assert.IsNull(_sftpClient.SftpSession);
108+
}
109+
110+
[TestMethod]
111+
public void ErrorOccuredOnSessionShouldNoLongerBeSignaledViaErrorOccurredOnSftpClient()
112+
{
113+
var errorOccurredSignalCount = 0;
114+
115+
_sftpClient.ErrorOccurred += (sender, args) => Interlocked.Increment(ref errorOccurredSignalCount);
116+
117+
_sessionMock.Raise(p => p.ErrorOccured += null, new ExceptionEventArgs(new Exception()));
118+
119+
Assert.AreEqual(0, errorOccurredSignalCount);
120+
}
121+
122+
[TestMethod]
123+
public void HostKeyReceivedOnSessionShouldNoLongerBeSignaledViaHostKeyReceivedOnSftpClient()
124+
{
125+
var hostKeyReceivedSignalCount = 0;
126+
127+
_sftpClient.HostKeyReceived += (sender, args) => Interlocked.Increment(ref hostKeyReceivedSignalCount);
128+
129+
_sessionMock.Raise(p => p.HostKeyReceived += null, new HostKeyEventArgs(GetKeyHostAlgorithm()));
130+
131+
Assert.AreEqual(0, hostKeyReceivedSignalCount);
132+
}
133+
134+
private static KeyHostAlgorithm GetKeyHostAlgorithm()
135+
{
136+
var executingAssembly = Assembly.GetExecutingAssembly();
137+
138+
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
139+
{
140+
var privateKey = new PrivateKeyFile(s);
141+
return (KeyHostAlgorithm)privateKey.HostKey;
142+
}
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)