2
0

ClientAuthenticationTest_Success_MultiList_DifferentAllowedAuthenticationsAfterPartialSuccess.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 1
  12. /// * Scenario:
  13. /// none
  14. /// (1=FAIL)
  15. /// |
  16. /// +------------------------------+
  17. /// | |
  18. /// publickey password
  19. /// (2=PARTIAL)
  20. /// *----------------------*
  21. /// | |
  22. /// keyboard-interactive publickey
  23. /// (3=SUCCESS)
  24. /// </summary>
  25. [TestClass]
  26. public class ClientAuthenticationTest_Success_MultiList_DifferentAllowedAuthenticationsAfterPartialSuccess : ClientAuthenticationTestBase
  27. {
  28. private int _partialSuccessLimit;
  29. private ClientAuthentication _clientAuthentication;
  30. protected override void SetupData()
  31. {
  32. _partialSuccessLimit = 1;
  33. }
  34. protected override void SetupMocks()
  35. {
  36. var seq = new MockSequence();
  37. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  38. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  39. SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  40. ConnectionInfoMock.InSequence(seq).Setup(p => p.CreateNoneAuthenticationMethod())
  41. .Returns(NoneAuthenticationMethodMock.Object);
  42. /* 1 */
  43. NoneAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object))
  44. .Returns(AuthenticationResult.Failure);
  45. ConnectionInfoMock.InSequence(seq).Setup(p => p.AuthenticationMethods)
  46. .Returns(new List<IAuthenticationMethod>
  47. {
  48. PasswordAuthenticationMethodMock.Object,
  49. PublicKeyAuthenticationMethodMock.Object,
  50. KeyboardInteractiveAuthenticationMethodMock.Object,
  51. });
  52. NoneAuthenticationMethodMock.InSequence(seq)
  53. .Setup(p => p.AllowedAuthentications)
  54. .Returns(new[] {"publickey", "password"});
  55. /* Enumerate supported authentication methods */
  56. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  57. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  58. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  59. /* 2 */
  60. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object))
  61. .Returns(AuthenticationResult.PartialSuccess);
  62. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.AllowedAuthentications)
  63. .Returns(new[] {"keyboard-interactive", "publickey"});
  64. /* Enumerate supported authentication methods */
  65. PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password");
  66. PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey");
  67. KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive");
  68. /* 3 */
  69. PublicKeyAuthenticationMethodMock.InSequence(seq)
  70. .Setup(p => p.Authenticate(SessionMock.Object))
  71. .Returns(AuthenticationResult.Success);
  72. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"));
  73. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"));
  74. SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"));
  75. }
  76. protected override void Arrange()
  77. {
  78. base.Arrange();
  79. _clientAuthentication = new ClientAuthentication(_partialSuccessLimit);
  80. }
  81. protected override void Act()
  82. {
  83. _clientAuthentication.Authenticate(ConnectionInfoMock.Object, SessionMock.Object);
  84. }
  85. }
  86. }