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