ClientAuthenticationTest_Success_SingleList_SameAllowedAuthenticationAfterPartialSuccess_PartialSuccessLimitReached.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 publickey
  10. /// o password
  11. /// * Partial success limit is 3
  12. /// * Scenario:
  13. /// none
  14. /// (1=FAIL)
  15. /// |
  16. /// password
  17. /// (2=PARTIAL)
  18. /// |
  19. /// password
  20. /// (3=PARTIAL)
  21. /// |
  22. /// password
  23. /// (4=PARTIAL)
  24. /// |
  25. /// password
  26. /// (5=SKIP)
  27. /// </summary>
  28. [TestClass]
  29. public class ClientAuthenticationTest_Success_SingleList_SameAllowedAuthenticationAfterPartialSuccess_PartialSuccessLimitReached : ClientAuthenticationTestBase
  30. {
  31. private int _partialSuccessLimit;
  32. private ClientAuthentication _clientAuthentication;
  33. private SshAuthenticationException _actualException;
  34. protected override void SetupData()
  35. {
  36. _partialSuccessLimit = 3;
  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. PublicKeyAuthenticationMethodMock.Object,
  54. PasswordAuthenticationMethodMock.Object
  55. });
  56. NoneAuthenticationMethodMock.InSequence(seq)
  57. .Setup(p => p.AllowedAuthentications)
  58. .Returns(new[] { "password" });
  59. /* Enumerate supported authentication methods */
  60. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  61. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  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"});
  69. /* Enumerate supported authentication methods */
  70. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  71. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  72. /* 3 */
  73. PasswordAuthenticationMethodMock.InSequence(seq)
  74. .Setup(p => p.Authenticate(SessionMock.Object))
  75. .Returns(AuthenticationResult.PartialSuccess);
  76. PasswordAuthenticationMethodMock.InSequence(seq)
  77. .Setup(p => p.AllowedAuthentications)
  78. .Returns(new[] { "password" });
  79. /* Enumerate supported authentication methods */
  80. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  81. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  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[] { "password" });
  89. /* Enumerate supported authentication methods */
  90. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  91. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  92. /* 5: Record partial success limit reached exception, and skip password authentication method */
  93. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("x_password_x");
  94. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  95. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  96. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  97. }
  98. protected override void Arrange()
  99. {
  100. base.Arrange();
  101. _clientAuthentication = new ClientAuthentication(_partialSuccessLimit);
  102. }
  103. protected override void Act()
  104. {
  105. try
  106. {
  107. _clientAuthentication.Authenticate(ConnectionInfoMock.Object, SessionMock.Object);
  108. Assert.Fail();
  109. }
  110. catch (SshAuthenticationException ex)
  111. {
  112. _actualException = ex;
  113. }
  114. }
  115. [TestMethod]
  116. public void AuthenticateShouldThrowSshAuthenticationException()
  117. {
  118. Assert.IsNotNull(_actualException);
  119. Assert.IsNull(_actualException.InnerException);
  120. Assert.AreEqual("Reached authentication attempt limit for method (x_password_x).",_actualException.Message);
  121. }
  122. }
  123. }