ClientAuthenticationTest_Success_SingleList_SameAllowedAuthenticationAfterPartialSuccess.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.Collections.Generic;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Moq;
  4. namespace Renci.SshNet.Tests.Classes
  5. {
  6. /// <summary>
  7. /// * ConnectionInfo provides the following authentication methods (in order):
  8. /// o keyboard-interactive
  9. /// o password
  10. /// o publickey
  11. /// * Partial success limit is 2
  12. /// * Scenario:
  13. /// none
  14. /// (1=FAIL)
  15. /// |
  16. /// password
  17. /// (2=PARTIAL)
  18. /// |
  19. /// password
  20. /// (3=SUCCESS)
  21. /// </summary>
  22. [TestClass]
  23. public class ClientAuthenticationTest_Success_SingleList_SameAllowedAuthenticationAfterPartialSuccess : ClientAuthenticationTestBase
  24. {
  25. private int _partialSuccessLimit;
  26. private ClientAuthentication _clientAuthentication;
  27. protected override void SetupData()
  28. {
  29. _partialSuccessLimit = 2;
  30. }
  31. protected override void SetupMocks()
  32. {
  33. var seq = new MockSequence();
  34. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  35. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  36. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  37. ConnectionInfoMock.InSequence(seq).Setup(p => p.CreateNoneAuthenticationMethod())
  38. .Returns(NoneAuthenticationMethodMock.Object);
  39. /* 1 */
  40. NoneAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object))
  41. .Returns(AuthenticationResult.Failure);
  42. ConnectionInfoMock.InSequence(seq)
  43. .Setup(p => p.AuthenticationMethods)
  44. .Returns(new List<IAuthenticationMethod>
  45. {
  46. PublicKeyAuthenticationMethodMock.Object,
  47. PasswordAuthenticationMethodMock.Object
  48. });
  49. NoneAuthenticationMethodMock.InSequence(seq)
  50. .Setup(p => p.AllowedAuthentications)
  51. .Returns(new[] {"password"});
  52. /* Enumerate supported authentication methods */
  53. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  54. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  55. /* 2 */
  56. PasswordAuthenticationMethodMock.InSequence(seq)
  57. .Setup(p => p.Authenticate(SessionMock.Object))
  58. .Returns(AuthenticationResult.PartialSuccess);
  59. PasswordAuthenticationMethodMock.InSequence(seq)
  60. .Setup(p => p.AllowedAuthentications)
  61. .Returns(new[] {"password"});
  62. /* Enumerate supported authentication methods */
  63. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  64. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  65. /* 3 */
  66. PasswordAuthenticationMethodMock.InSequence(seq)
  67. .Setup(p => p.Authenticate(SessionMock.Object))
  68. .Returns(AuthenticationResult.Success);
  69. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  70. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  71. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  72. }
  73. protected override void Arrange()
  74. {
  75. base.Arrange();
  76. _clientAuthentication = new ClientAuthentication(_partialSuccessLimit);
  77. }
  78. protected override void Act()
  79. {
  80. _clientAuthentication.Authenticate(ConnectionInfoMock.Object, SessionMock.Object);
  81. }
  82. }
  83. }