ClientAuthenticationTest_Failure_SingleList_AuthenticationMethodNotConfigured.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_AuthenticationMethodNotSupported : 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).Setup(p => p.AuthenticationMethods)
  29. .Returns(new List<IAuthenticationMethod>
  30. {
  31. PublicKeyAuthenticationMethodMock.Object
  32. });
  33. NoneAuthenticationMethodMock.InSequence(seq)
  34. .Setup(p => p.AllowedAuthentications)
  35. .Returns(new[] { "password" });
  36. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  37. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  38. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  39. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  40. }
  41. protected override void Arrange()
  42. {
  43. base.Arrange();
  44. _clientAuthentication = new ClientAuthentication(_partialSuccessLimit);
  45. }
  46. protected override void Act()
  47. {
  48. try
  49. {
  50. _clientAuthentication.Authenticate(ConnectionInfoMock.Object, SessionMock.Object);
  51. Assert.Fail();
  52. }
  53. catch (SshAuthenticationException ex)
  54. {
  55. _actualException = ex;
  56. }
  57. }
  58. [TestMethod]
  59. public void AuthenticateShouldThrowSshAuthenticationException()
  60. {
  61. Assert.IsNotNull(_actualException);
  62. Assert.IsNull(_actualException.InnerException);
  63. Assert.AreEqual("No suitable authentication method found to complete authentication (password).", _actualException.Message);
  64. }
  65. }
  66. }