ClientAuthenticationTestBase.cs 3.2 KB

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