ConnectionInfoTest_Authenticate_Failure.cs 2.8 KB

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