ClientAuthenticationTestBase.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using Microsoft.Extensions.Logging.Abstractions;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Moq;
  4. using Renci.SshNet.Tests.Common;
  5. namespace Renci.SshNet.Tests.Classes
  6. {
  7. [TestClass]
  8. public abstract class ClientAuthenticationTestBase : TestBase
  9. {
  10. internal Mock<IConnectionInfoInternal> ConnectionInfoMock { get; private set; }
  11. internal Mock<ISession> SessionMock { get; private set; }
  12. internal Mock<IAuthenticationMethod> NoneAuthenticationMethodMock { get; private set; }
  13. internal Mock<IAuthenticationMethod> PasswordAuthenticationMethodMock { get; private set; }
  14. internal Mock<IAuthenticationMethod> PublicKeyAuthenticationMethodMock { get; private set; }
  15. internal Mock<IAuthenticationMethod> KeyboardInteractiveAuthenticationMethodMock { get; private set; }
  16. protected abstract void SetupData();
  17. protected void CreateMocks()
  18. {
  19. ConnectionInfoMock = new Mock<IConnectionInfoInternal>(MockBehavior.Strict);
  20. SessionMock = new Mock<ISession>(MockBehavior.Strict);
  21. SessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  22. NoneAuthenticationMethodMock = new Mock<IAuthenticationMethod>(MockBehavior.Strict);
  23. PasswordAuthenticationMethodMock = new Mock<IAuthenticationMethod>(MockBehavior.Strict);
  24. PublicKeyAuthenticationMethodMock = new Mock<IAuthenticationMethod>(MockBehavior.Strict);
  25. KeyboardInteractiveAuthenticationMethodMock = new Mock<IAuthenticationMethod>(MockBehavior.Strict);
  26. }
  27. protected abstract void SetupMocks();
  28. protected virtual void Arrange()
  29. {
  30. SetupData();
  31. CreateMocks();
  32. SetupMocks();
  33. }
  34. protected abstract void Act();
  35. protected sealed override void OnInit()
  36. {
  37. base.OnInit();
  38. Arrange();
  39. Act();
  40. }
  41. [TestMethod]
  42. public void RegisterMessageWithUserAuthFailureShouldBeInvokedOnce()
  43. {
  44. SessionMock.Verify(p => p.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"), Times.Once);
  45. }
  46. [TestMethod]
  47. public void RegisterMessageWithUserAuthSuccessShouldBeInvokedOnce()
  48. {
  49. SessionMock.Verify(p => p.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"), Times.Once);
  50. }
  51. [TestMethod]
  52. public void RegisterMessageWithUserAuthBannerShouldBeInvokedOnce()
  53. {
  54. SessionMock.Verify(p => p.RegisterMessage("SSH_MSG_USERAUTH_BANNER"), Times.Once);
  55. }
  56. [TestMethod]
  57. public void UnRegisterMessageWithUserAuthFailureShouldBeInvokedOnce()
  58. {
  59. SessionMock.Verify(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"), Times.Once);
  60. }
  61. [TestMethod]
  62. public void UnRegisterMessageWithUserAuthSuccessShouldBeInvokedOnce()
  63. {
  64. SessionMock.Verify(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"), Times.Once);
  65. }
  66. [TestMethod]
  67. public void UnRegisterMessageWithUserAuthBannerShouldBeInvokedOnce()
  68. {
  69. SessionMock.Verify(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"), Times.Once);
  70. }
  71. }
  72. }