2
0

ClientAuthenticationTestBase.cs 3.2 KB

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