ClientAuthenticationTest_Failure_SingleList_AuthenticationMethodNotConfigured.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 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. });
  26. NoneAuthenticationMethodMock.InSequence(seq)
  27. .Setup(p => p.AllowedAuthentications)
  28. .Returns(new[] { "password" });
  29. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  30. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  31. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  32. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  33. }
  34. protected override void Act()
  35. {
  36. try
  37. {
  38. ClientAuthentication.Authenticate(ConnectionInfoMock.Object, SessionMock.Object);
  39. Assert.Fail();
  40. }
  41. catch (SshAuthenticationException ex)
  42. {
  43. _actualException = ex;
  44. }
  45. }
  46. [TestMethod]
  47. public void AuthenticateShouldThrowSshAuthenticationException()
  48. {
  49. Assert.IsNotNull(_actualException);
  50. Assert.IsNull(_actualException.InnerException);
  51. Assert.AreEqual("No suitable authentication method found to complete authentication (password).", _actualException.Message);
  52. }
  53. }
  54. }