ClientAuthenticationTest_Success_MultiList_PartialSuccessLimitReachedFollowedBySuccessInSameBranch.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /// +------------------------------+
  20. /// | |
  21. /// password publickey
  22. /// (4=PARTIAL) (3=FAILURE)
  23. /// |
  24. /// keyboard-interactive
  25. /// (5=FAILURE)
  26. /// </summary>
  27. [TestClass]
  28. public class ClientAuthenticationTest_Success_MultiList_PartialSuccessLimitReachedFollowedBySuccessInSameBranch : ClientAuthenticationTestBase
  29. {
  30. private int _partialSuccessLimit;
  31. private ClientAuthentication _clientAuthentication;
  32. protected override void SetupData()
  33. {
  34. _partialSuccessLimit = 2;
  35. }
  36. protected override void SetupMocks()
  37. {
  38. var seq = new MockSequence();
  39. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  40. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  41. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  42. ConnectionInfoMock.InSequence(seq).Setup(p => p.CreateNoneAuthenticationMethod())
  43. .Returns(NoneAuthenticationMethodMock.Object);
  44. /* 1 */
  45. NoneAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object))
  46. .Returns(AuthenticationResult.Failure);
  47. ConnectionInfoMock.InSequence(seq)
  48. .Setup(p => p.AuthenticationMethods)
  49. .Returns(new List<IAuthenticationMethod>
  50. {
  51. KeyboardInteractiveAuthenticationMethodMock.Object,
  52. PasswordAuthenticationMethodMock.Object,
  53. PublicKeyAuthenticationMethodMock.Object
  54. });
  55. NoneAuthenticationMethodMock.InSequence(seq)
  56. .Setup(p => p.AllowedAuthentications)
  57. .Returns(new[] { "password" });
  58. /* Enumerate supported authentication methods */
  59. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  60. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  61. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  62. /* 2 */
  63. PasswordAuthenticationMethodMock.InSequence(seq)
  64. .Setup(p => p.Authenticate(SessionMock.Object))
  65. .Returns(AuthenticationResult.PartialSuccess);
  66. PasswordAuthenticationMethodMock.InSequence(seq)
  67. .Setup(p => p.AllowedAuthentications)
  68. .Returns(new[] { "password", "publickey" });
  69. /* Enumerate supported authentication methods */
  70. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  71. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  72. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  73. /* 3 */
  74. PublicKeyAuthenticationMethodMock.InSequence(seq)
  75. .Setup(p => p.Authenticate(SessionMock.Object))
  76. .Returns(AuthenticationResult.Failure);
  77. PublicKeyAuthenticationMethodMock.InSequence(seq)
  78. .Setup(p => p.Name)
  79. .Returns("publickey-failure");
  80. /* 4 */
  81. PasswordAuthenticationMethodMock.InSequence(seq)
  82. .Setup(p => p.Authenticate(SessionMock.Object))
  83. .Returns(AuthenticationResult.PartialSuccess);
  84. PasswordAuthenticationMethodMock.InSequence(seq)
  85. .Setup(p => p.AllowedAuthentications)
  86. .Returns(new[] { "keyboard-interactive" });
  87. /* Enumerate supported authentication methods */
  88. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  89. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  90. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  91. /* 5 */
  92. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq)
  93. .Setup(p => p.Authenticate(SessionMock.Object))
  94. .Returns(AuthenticationResult.Success);
  95. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  96. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  97. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  98. }
  99. protected override void Arrange()
  100. {
  101. base.Arrange();
  102. _clientAuthentication = new ClientAuthentication(_partialSuccessLimit);
  103. }
  104. protected override void Act()
  105. {
  106. _clientAuthentication.Authenticate(ConnectionInfoMock.Object, SessionMock.Object);
  107. }
  108. [TestMethod]
  109. public void AuthenticateOnKeyboardInteractiveAuthenticationMethodShouldHaveBeenInvokedOnce()
  110. {
  111. KeyboardInteractiveAuthenticationMethodMock.Verify(p => p.Authenticate(SessionMock.Object), Times.Once);
  112. }
  113. }
  114. }