2
0

SftpClientTest_Connect_SftpSessionConnectFailure.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Linq;
  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.Tests.Common;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. [TestClass]
  12. public class SftpClientTest_Connect_SftpSessionConnectFailure : SftpClientTestBase
  13. {
  14. private ConnectionInfo _connectionInfo;
  15. private ApplicationException _sftpSessionConnectionException;
  16. private SftpClient _sftpClient;
  17. private ApplicationException _actualException;
  18. protected override void SetupData()
  19. {
  20. _connectionInfo = new ConnectionInfo("host", "user", new NoneAuthenticationMethod("userauth"));
  21. _sftpSessionConnectionException = new ApplicationException();
  22. }
  23. protected override void SetupMocks()
  24. {
  25. var sequence = new MockSequence();
  26. _ = ServiceFactoryMock.InSequence(sequence)
  27. .Setup(p => p.CreateSocketFactory())
  28. .Returns(SocketFactoryMock.Object);
  29. _ = ServiceFactoryMock.InSequence(sequence)
  30. .Setup(p => p.CreateSession(_connectionInfo, SocketFactoryMock.Object))
  31. .Returns(SessionMock.Object);
  32. _ = SessionMock.InSequence(sequence)
  33. .Setup(p => p.Connect());
  34. _ = ServiceFactoryMock.InSequence(sequence)
  35. .Setup(p => p.CreateSftpResponseFactory())
  36. .Returns(SftpResponseFactoryMock.Object);
  37. _ = ServiceFactoryMock.InSequence(sequence)
  38. .Setup(p => p.CreateSftpSession(SessionMock.Object, -1, _connectionInfo.Encoding, SftpResponseFactoryMock.Object))
  39. .Returns(SftpSessionMock.Object);
  40. _ = SftpSessionMock.InSequence(sequence)
  41. .Setup(p => p.Connect())
  42. .Throws(_sftpSessionConnectionException);
  43. _ = SftpSessionMock.InSequence(sequence)
  44. .Setup(p => p.Dispose());
  45. _ = SessionMock.InSequence(sequence)
  46. .Setup(p => p.Dispose());
  47. }
  48. protected override void Arrange()
  49. {
  50. base.Arrange();
  51. _sftpClient = new SftpClient(_connectionInfo, false, ServiceFactoryMock.Object);
  52. }
  53. protected override void Act()
  54. {
  55. try
  56. {
  57. _sftpClient.Connect();
  58. Assert.Fail();
  59. }
  60. catch (ApplicationException ex)
  61. {
  62. _actualException = ex;
  63. }
  64. }
  65. [TestMethod]
  66. public void ConnectShouldHaveThrownApplicationException()
  67. {
  68. Assert.IsNotNull(_actualException);
  69. Assert.AreSame(_sftpSessionConnectionException, _actualException);
  70. }
  71. [TestMethod]
  72. public void SessionShouldBeNull()
  73. {
  74. Assert.IsNull(_sftpClient.Session);
  75. }
  76. [TestMethod]
  77. public void SftpSessionShouldBeNull()
  78. {
  79. Assert.IsNull(_sftpClient.SftpSession);
  80. }
  81. [TestMethod]
  82. public void ErrorOccurredOnSessionShouldNoLongerBeSignaledViaErrorOccurredOnSftpClient()
  83. {
  84. var errorOccurredSignalCount = 0;
  85. _sftpClient.ErrorOccurred += (sender, args) => Interlocked.Increment(ref errorOccurredSignalCount);
  86. SessionMock.Raise(p => p.ErrorOccured += null, new ExceptionEventArgs(new Exception()));
  87. Assert.AreEqual(0, errorOccurredSignalCount);
  88. }
  89. [TestMethod]
  90. public void HostKeyReceivedOnSessionShouldNoLongerBeSignaledViaHostKeyReceivedOnSftpClient()
  91. {
  92. var hostKeyReceivedSignalCount = 0;
  93. _sftpClient.HostKeyReceived += (sender, args) => Interlocked.Increment(ref hostKeyReceivedSignalCount);
  94. SessionMock.Raise(p => p.HostKeyReceived += null, new HostKeyEventArgs(GetKeyHostAlgorithm()));
  95. Assert.AreEqual(0, hostKeyReceivedSignalCount);
  96. }
  97. private static KeyHostAlgorithm GetKeyHostAlgorithm()
  98. {
  99. using (var s = TestBase.GetData("Key.RSA.txt"))
  100. {
  101. var privateKey = new PrivateKeyFile(s);
  102. return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();
  103. }
  104. }
  105. }
  106. }