|
| 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