ClientAuthenticationTest_Failure_SingleList_AuthenticationMethodFailed.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 SshAuthenticationException _actualException;
  11. protected override void SetupMocks()
  12. {
  13. var seq = new MockSequence();
  14. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  15. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  16. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  17. ConnectionInfoMock.InSequence(seq).Setup(p => p.CreateNoneAuthenticationMethod())
  18. .Returns(NoneAuthenticationMethodMock.Object);
  19. NoneAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object))
  20. .Returns(AuthenticationResult.Failure);
  21. ConnectionInfoMock.InSequence(seq).Setup(p => p.AuthenticationMethods)
  22. .Returns(new List<IAuthenticationMethod>
  23. {
  24. PublicKeyAuthenticationMethodMock.Object,
  25. PasswordAuthenticationMethodMock.Object
  26. });
  27. NoneAuthenticationMethodMock.InSequence(seq)
  28. .Setup(p => p.AllowedAuthentications)
  29. .Returns(new[] { "password" });
  30. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  31. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  32. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object))
  33. .Returns(AuthenticationResult.Failure);
  34. // obtain name for inclusion in SshAuthenticationException
  35. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  36. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  37. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  38. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  39. }
  40. protected override void Act()
  41. {
  42. try
  43. {
  44. ClientAuthentication.Authenticate(ConnectionInfoMock.Object, SessionMock.Object);
  45. Assert.Fail();
  46. }
  47. catch (SshAuthenticationException ex)
  48. {
  49. _actualException = ex;
  50. }
  51. }
  52. [TestMethod]
  53. public void AuthenticateShouldThrowSshAuthenticationException()
  54. {
  55. Assert.IsNotNull(_actualException);
  56. Assert.IsNull(_actualException.InnerException);
  57. Assert.AreEqual("Permission denied (password).", _actualException.Message);
  58. }
  59. }
  60. }