2
0

ConnectionInfoTest_Authenticate_Failure.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Microsoft.Extensions.Logging.Abstractions;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Moq;
  4. using Renci.SshNet.Common;
  5. using Renci.SshNet.Tests.Properties;
  6. namespace Renci.SshNet.Tests.Classes
  7. {
  8. [TestClass]
  9. public class ConnectionInfoTest_Authenticate_Failure
  10. {
  11. private Mock<IServiceFactory> _serviceFactoryMock;
  12. private Mock<IClientAuthentication> _clientAuthenticationMock;
  13. private Mock<ISession> _sessionMock;
  14. private ConnectionInfo _connectionInfo;
  15. private SshAuthenticationException _authenticationException;
  16. private SshAuthenticationException _actualException;
  17. [TestInitialize]
  18. public void Init()
  19. {
  20. Arrange();
  21. Act();
  22. }
  23. protected void Arrange()
  24. {
  25. _serviceFactoryMock = new Mock<IServiceFactory>(MockBehavior.Strict);
  26. _clientAuthenticationMock = new Mock<IClientAuthentication>(MockBehavior.Strict);
  27. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  28. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  29. _connectionInfo = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None,
  30. Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
  31. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  32. _authenticationException = new SshAuthenticationException();
  33. _serviceFactoryMock.Setup(p => p.CreateClientAuthentication()).Returns(_clientAuthenticationMock.Object);
  34. _clientAuthenticationMock.Setup(p => p.Authenticate(_connectionInfo, _sessionMock.Object))
  35. .Throws(_authenticationException);
  36. }
  37. protected void Act()
  38. {
  39. try
  40. {
  41. _connectionInfo.Authenticate(_sessionMock.Object, _serviceFactoryMock.Object);
  42. }
  43. catch (SshAuthenticationException ex)
  44. {
  45. _actualException = ex;
  46. }
  47. }
  48. [TestMethod]
  49. public void AuthenticateShouldHaveThrownSshAuthenticationException()
  50. {
  51. Assert.IsNotNull(_actualException);
  52. Assert.AreSame(_authenticationException, _actualException);
  53. }
  54. [TestMethod]
  55. public void IsAuthenticatedShouldReturnFalse()
  56. {
  57. Assert.IsFalse(_connectionInfo.IsAuthenticated);
  58. }
  59. [TestMethod]
  60. public void CreateClientAuthenticationOnServiceFactoryShouldBeInvokedOnce()
  61. {
  62. _serviceFactoryMock.Verify(p => p.CreateClientAuthentication(), Times.Once);
  63. }
  64. [TestMethod]
  65. public void AuthenticateOnClientAuthenticationShouldBeInvokedOnce()
  66. {
  67. _clientAuthenticationMock.Verify(p => p.Authenticate(_connectionInfo, _sessionMock.Object), Times.Once);
  68. }
  69. }
  70. }