ClientAuthenticationTest_Failure_SingleList_AuthenticationMethodFailed.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections.Generic;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Moq;
  4. using Renci.SshNet.Common;
  5. namespace Renci.SshNet.Tests.Classes
  6. {
  7. [TestClass]
  8. public class ClientAuthenticationTest_Failure_SingleList_AuthenticationMethodFailed : ClientAuthenticationTestBase
  9. {
  10. private int _partialSuccessLimit;
  11. private ClientAuthentication _clientAuthentication;
  12. private SshAuthenticationException _actualException;
  13. protected override void SetupData()
  14. {
  15. _partialSuccessLimit = 1;
  16. }
  17. protected override void SetupMocks()
  18. {
  19. var seq = new MockSequence();
  20. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  21. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  22. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  23. ConnectionInfoMock.InSequence(seq).Setup(p => p.CreateNoneAuthenticationMethod())
  24. .Returns(NoneAuthenticationMethodMock.Object);
  25. NoneAuthenticationMethodMock.InSequence(seq)
  26. .Setup(p => p.Authenticate(SessionMock.Object))
  27. .Returns(AuthenticationResult.Failure);
  28. ConnectionInfoMock.InSequence(seq)
  29. .Setup(p => p.AuthenticationMethods)
  30. .Returns(new List<IAuthenticationMethod>
  31. {
  32. PublicKeyAuthenticationMethodMock.Object,
  33. PasswordAuthenticationMethodMock.Object
  34. });
  35. NoneAuthenticationMethodMock.InSequence(seq)
  36. .Setup(p => p.AllowedAuthentications)
  37. .Returns(new[] { "password" });
  38. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  39. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  40. PasswordAuthenticationMethodMock.InSequence(seq)
  41. .Setup(p => p.Authenticate(SessionMock.Object))
  42. .Returns(AuthenticationResult.Failure);
  43. // obtain name for inclusion in SshAuthenticationException
  44. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  45. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  46. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  47. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  48. }
  49. protected override void Arrange()
  50. {
  51. base.Arrange();
  52. _clientAuthentication = new ClientAuthentication(_partialSuccessLimit);
  53. }
  54. protected override void Act()
  55. {
  56. try
  57. {
  58. _clientAuthentication.Authenticate(ConnectionInfoMock.Object, SessionMock.Object);
  59. Assert.Fail();
  60. }
  61. catch (SshAuthenticationException ex)
  62. {
  63. _actualException = ex;
  64. }
  65. }
  66. [TestMethod]
  67. public void AuthenticateShouldThrowSshAuthenticationException()
  68. {
  69. Assert.IsNotNull(_actualException);
  70. Assert.IsNull(_actualException.InnerException);
  71. Assert.AreEqual("Permission denied (password).", _actualException.Message);
  72. }
  73. }
  74. }