2
0

ClientAuthenticationTest_Success_MultiList_PartialSuccessLimitReachedFollowedByFailureInSameBranch.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /// <summary>
  8. /// * ConnectionInfo provides the following authentication methods (in order):
  9. /// o keyboard-interactive
  10. /// o password
  11. /// o publickey
  12. /// * Partial success limit is 2
  13. /// * Scenario:
  14. /// none
  15. /// (1=FAIL)
  16. /// |
  17. /// password
  18. /// (2=PARTIAL)
  19. /// |
  20. /// +------------------------------+
  21. /// | |
  22. /// password publickey
  23. /// (4=PARTIAL) (3=FAILURE)
  24. /// |
  25. /// keyboard-interactive
  26. /// (5=FAILURE)
  27. /// </summary>
  28. [TestClass]
  29. public class ClientAuthenticationTest_Success_MultiList_PartialSuccessLimitReachedFollowedByFailureInSameBranch : ClientAuthenticationTestBase
  30. {
  31. private int _partialSuccessLimit;
  32. private ClientAuthentication _clientAuthentication;
  33. private SshAuthenticationException _actualException;
  34. protected override void SetupData()
  35. {
  36. _partialSuccessLimit = 2;
  37. }
  38. protected override void SetupMocks()
  39. {
  40. var seq = new MockSequence();
  41. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  42. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  43. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  44. ConnectionInfoMock.InSequence(seq).Setup(p => p.CreateNoneAuthenticationMethod())
  45. .Returns(NoneAuthenticationMethodMock.Object);
  46. /* 1 */
  47. NoneAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object))
  48. .Returns(AuthenticationResult.Failure);
  49. ConnectionInfoMock.InSequence(seq)
  50. .Setup(p => p.AuthenticationMethods)
  51. .Returns(new List<IAuthenticationMethod>
  52. {
  53. KeyboardInteractiveAuthenticationMethodMock.Object,
  54. PasswordAuthenticationMethodMock.Object,
  55. PublicKeyAuthenticationMethodMock.Object
  56. });
  57. NoneAuthenticationMethodMock.InSequence(seq)
  58. .Setup(p => p.AllowedAuthentications)
  59. .Returns(new[] { "password" });
  60. /* Enumerate supported authentication methods */
  61. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  62. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  63. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  64. /* 2 */
  65. PasswordAuthenticationMethodMock.InSequence(seq)
  66. .Setup(p => p.Authenticate(SessionMock.Object))
  67. .Returns(AuthenticationResult.PartialSuccess);
  68. PasswordAuthenticationMethodMock.InSequence(seq)
  69. .Setup(p => p.AllowedAuthentications)
  70. .Returns(new[] { "password", "publickey" });
  71. /* Enumerate supported authentication methods */
  72. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  73. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  74. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  75. /* 3 */
  76. PublicKeyAuthenticationMethodMock.InSequence(seq)
  77. .Setup(p => p.Authenticate(SessionMock.Object))
  78. .Returns(AuthenticationResult.Failure);
  79. PublicKeyAuthenticationMethodMock.InSequence(seq)
  80. .Setup(p => p.Name)
  81. .Returns("publickey-failure");
  82. /* 4 */
  83. PasswordAuthenticationMethodMock.InSequence(seq)
  84. .Setup(p => p.Authenticate(SessionMock.Object))
  85. .Returns(AuthenticationResult.PartialSuccess);
  86. PasswordAuthenticationMethodMock.InSequence(seq)
  87. .Setup(p => p.AllowedAuthentications)
  88. .Returns(new[] { "keyboard-interactive" });
  89. /* Enumerate supported authentication methods */
  90. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  91. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  92. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  93. /* 5 */
  94. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq)
  95. .Setup(p => p.Authenticate(SessionMock.Object))
  96. .Returns(AuthenticationResult.Failure);
  97. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive-failure");
  98. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  99. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  100. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  101. }
  102. protected override void Arrange()
  103. {
  104. base.Arrange();
  105. _clientAuthentication = new ClientAuthentication(_partialSuccessLimit);
  106. }
  107. protected override void Act()
  108. {
  109. try
  110. {
  111. _clientAuthentication.Authenticate(ConnectionInfoMock.Object, SessionMock.Object);
  112. Assert.Fail();
  113. }
  114. catch (SshAuthenticationException ex)
  115. {
  116. _actualException = ex;
  117. }
  118. }
  119. [TestMethod]
  120. public void AuthenticateOnKeyboardInteractiveAuthenticationMethodShouldHaveBeenInvokedOnce()
  121. {
  122. KeyboardInteractiveAuthenticationMethodMock.Verify(p => p.Authenticate(SessionMock.Object), Times.Once);
  123. }
  124. [TestMethod]
  125. public void AuthenticateShouldThrowSshAuthenticationException()
  126. {
  127. Assert.IsNotNull(_actualException);
  128. Assert.IsNull(_actualException.InnerException);
  129. Assert.AreEqual("Permission denied (keyboard-interactive-failure).", _actualException.Message);
  130. }
  131. }
  132. }