ClientAuthenticationTest_Success_MultiList_PartialSuccessLimitReachedFollowedBySuccessInAlternateBranch.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 password
  9. /// o publickey
  10. /// o keyboard-interactive
  11. /// * Partial success limit is 2
  12. ///
  13. /// none
  14. /// (1=FAIL)
  15. /// |
  16. /// +-------------------+
  17. /// | |
  18. /// publickey keyboard-interactive
  19. /// (2=PS) ^ (6=PS)
  20. /// | | |
  21. /// password | +-----------+
  22. /// (3=PS) | | |
  23. /// | | password publickey
  24. /// password | (7=SKIP) (8=SUCCESS)
  25. /// (4=PS) |
  26. /// | |
  27. /// password |
  28. /// (5=SKIP) |
  29. /// +------------+
  30. /// </summary>
  31. [TestClass]
  32. public class ClientAuthenticationTest_Success_MultiList_PartialSuccessLimitReachedFollowedBySuccessInAlternateBranch : ClientAuthenticationTestBase
  33. {
  34. private int _partialSuccessLimit;
  35. private ClientAuthentication _clientAuthentication;
  36. protected override void SetupData()
  37. {
  38. _partialSuccessLimit = 2;
  39. }
  40. protected override void SetupMocks()
  41. {
  42. var seq = new MockSequence();
  43. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  44. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  45. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  46. ConnectionInfoMock.InSequence(seq).Setup(p => p.CreateNoneAuthenticationMethod())
  47. .Returns(NoneAuthenticationMethodMock.Object);
  48. /* 1 */
  49. NoneAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object))
  50. .Returns(AuthenticationResult.Failure);
  51. ConnectionInfoMock.InSequence(seq)
  52. .Setup(p => p.AuthenticationMethods)
  53. .Returns(new List<IAuthenticationMethod>
  54. {
  55. PasswordAuthenticationMethodMock.Object,
  56. PublicKeyAuthenticationMethodMock.Object,
  57. KeyboardInteractiveAuthenticationMethodMock.Object,
  58. });
  59. NoneAuthenticationMethodMock.InSequence(seq)
  60. .Setup(p => p.AllowedAuthentications)
  61. .Returns(new[] {"publickey", "keyboard-interactive"});
  62. /* Enumerate supported authentication methods */
  63. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  64. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  65. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  66. /* 2 */
  67. PublicKeyAuthenticationMethodMock.InSequence(seq)
  68. .Setup(p => p.Authenticate(SessionMock.Object))
  69. .Returns(AuthenticationResult.PartialSuccess);
  70. PublicKeyAuthenticationMethodMock.InSequence(seq)
  71. .Setup(p => p.AllowedAuthentications)
  72. .Returns(new[] {"password"});
  73. /* Enumerate supported authentication methods */
  74. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  75. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  76. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  77. /* 3 */
  78. PasswordAuthenticationMethodMock.InSequence(seq)
  79. .Setup(p => p.Authenticate(SessionMock.Object))
  80. .Returns(AuthenticationResult.PartialSuccess);
  81. PasswordAuthenticationMethodMock.InSequence(seq)
  82. .Setup(p => p.AllowedAuthentications)
  83. .Returns(new[] {"password"});
  84. /* Enumerate supported authentication methods */
  85. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  86. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  87. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  88. /* 4 */
  89. PasswordAuthenticationMethodMock.InSequence(seq)
  90. .Setup(p => p.Authenticate(SessionMock.Object))
  91. .Returns(AuthenticationResult.PartialSuccess);
  92. PasswordAuthenticationMethodMock.InSequence(seq)
  93. .Setup(p => p.AllowedAuthentications)
  94. .Returns(new[] {"password"});
  95. /* Enumerate supported authentication methods */
  96. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  97. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  98. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  99. /* 5: Record partial success limit reached exception, and skip password authentication method */
  100. PasswordAuthenticationMethodMock.InSequence(seq)
  101. .Setup(p => p.Name)
  102. .Returns("password-partial1");
  103. /* 6 */
  104. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq)
  105. .Setup(p => p.Authenticate(SessionMock.Object))
  106. .Returns(AuthenticationResult.PartialSuccess);
  107. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq)
  108. .Setup(p => p.AllowedAuthentications)
  109. .Returns(new[] {"password", "publickey"});
  110. /* Enumerate supported authentication methods */
  111. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  112. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  113. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  114. /* 7: Record partial success limit reached exception, and skip password authentication method */
  115. PasswordAuthenticationMethodMock.InSequence(seq)
  116. .Setup(p => p.Name)
  117. .Returns("password-partial2");
  118. /* 8 */
  119. PublicKeyAuthenticationMethodMock.InSequence(seq)
  120. .Setup(p => p.Authenticate(SessionMock.Object))
  121. .Returns(AuthenticationResult.Success);
  122. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  123. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  124. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  125. }
  126. protected override void Arrange()
  127. {
  128. base.Arrange();
  129. _clientAuthentication = new ClientAuthentication(_partialSuccessLimit);
  130. }
  131. protected override void Act()
  132. {
  133. _clientAuthentication.Authenticate(ConnectionInfoMock.Object, SessionMock.Object);
  134. }
  135. [TestMethod]
  136. public void AuthenticateOnKeyboardInteractiveAuthenticationMethodShouldHaveBeenInvokedOnce()
  137. {
  138. KeyboardInteractiveAuthenticationMethodMock.Verify(p => p.Authenticate(SessionMock.Object), Times.Once);
  139. }
  140. [TestMethod]
  141. public void AuthenticateOnPublicKeyAuthenticationMethodShouldHaveBeenInvokedTwice()
  142. {
  143. PublicKeyAuthenticationMethodMock.Verify(p => p.Authenticate(SessionMock.Object), Times.Exactly(2));
  144. }
  145. }
  146. }