ClientAuthenticationTest_Success_MultiList_SkipFailedAuthenticationMethod.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.Collections.Generic;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Moq;
  4. namespace Renci.SshNet.Tests.Classes
  5. {
  6. [TestClass]
  7. public class ClientAuthenticationTest_Success_MultiList_SkipFailedAuthenticationMethod : ClientAuthenticationTestBase
  8. {
  9. private int _partialSuccessLimit;
  10. private ClientAuthentication _clientAuthentication;
  11. protected override void SetupData()
  12. {
  13. _partialSuccessLimit = 1;
  14. }
  15. protected override void SetupMocks()
  16. {
  17. var seq = new MockSequence();
  18. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  19. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  20. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  21. ConnectionInfoMock.InSequence(seq).Setup(p => p.CreateNoneAuthenticationMethod())
  22. .Returns(NoneAuthenticationMethodMock.Object);
  23. NoneAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object))
  24. .Returns(AuthenticationResult.Failure);
  25. ConnectionInfoMock.InSequence(seq).Setup(p => p.AuthenticationMethods)
  26. .Returns(new List<IAuthenticationMethod>
  27. {
  28. PasswordAuthenticationMethodMock.Object,
  29. PublicKeyAuthenticationMethodMock.Object,
  30. });
  31. NoneAuthenticationMethodMock.InSequence(seq)
  32. .Setup(p => p.AllowedAuthentications)
  33. .Returns(new[] { "publickey", "password" });
  34. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  35. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  36. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object))
  37. .Returns(AuthenticationResult.Failure);
  38. // obtain name for inclusion in SshAuthenticationException
  39. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  40. PublicKeyAuthenticationMethodMock.InSequence(seq)
  41. .Setup(p => p.Authenticate(SessionMock.Object))
  42. .Returns(AuthenticationResult.Success);
  43. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  44. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  45. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  46. }
  47. protected override void Arrange()
  48. {
  49. base.Arrange();
  50. _clientAuthentication = new ClientAuthentication(_partialSuccessLimit);
  51. }
  52. protected override void Act()
  53. {
  54. _clientAuthentication.Authenticate(ConnectionInfoMock.Object, SessionMock.Object);
  55. }
  56. }
  57. }