2
0

SessionTest.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Net;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Connection;
  6. using Renci.SshNet.Tests.Common;
  7. namespace Renci.SshNet.Tests.Classes
  8. {
  9. /// <summary>
  10. /// Provides functionality to connect and interact with SSH server.
  11. /// </summary>
  12. [TestClass]
  13. public partial class SessionTest : TestBase
  14. {
  15. private Mock<IServiceFactory> _serviceFactoryMock;
  16. private Mock<ISocketFactory> _socketFactoryMock;
  17. protected override void OnInit()
  18. {
  19. base.OnInit();
  20. _serviceFactoryMock = new Mock<IServiceFactory>(MockBehavior.Strict);
  21. _socketFactoryMock = new Mock<ISocketFactory>(MockBehavior.Strict);
  22. }
  23. [TestMethod]
  24. public void ConstructorShouldThrowArgumentNullExceptionWhenConnectionInfoIsNull()
  25. {
  26. const ConnectionInfo connectionInfo = null;
  27. try
  28. {
  29. _ = new Session(connectionInfo, _serviceFactoryMock.Object, _socketFactoryMock.Object);
  30. Assert.Fail();
  31. }
  32. catch (ArgumentNullException ex)
  33. {
  34. Assert.IsNull(ex.InnerException);
  35. Assert.AreEqual("connectionInfo", ex.ParamName);
  36. }
  37. }
  38. [TestMethod]
  39. public void ConstructorShouldThrowArgumentNullExceptionWhenServiceFactoryIsNull()
  40. {
  41. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  42. var connectionInfo = CreateConnectionInfo(serverEndPoint, TimeSpan.FromSeconds(5));
  43. IServiceFactory serviceFactory = null;
  44. try
  45. {
  46. _ = new Session(connectionInfo, serviceFactory, _socketFactoryMock.Object);
  47. Assert.Fail();
  48. }
  49. catch (ArgumentNullException ex)
  50. {
  51. Assert.IsNull(ex.InnerException);
  52. Assert.AreEqual("serviceFactory", ex.ParamName);
  53. }
  54. }
  55. [TestMethod]
  56. public void ConstructorShouldThrowArgumentNullExceptionWhenSocketFactoryIsNull()
  57. {
  58. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  59. var connectionInfo = CreateConnectionInfo(serverEndPoint, TimeSpan.FromSeconds(5));
  60. const ISocketFactory socketFactory = null;
  61. try
  62. {
  63. _ = new Session(connectionInfo, _serviceFactoryMock.Object, socketFactory);
  64. Assert.Fail();
  65. }
  66. catch (ArgumentNullException ex)
  67. {
  68. Assert.IsNull(ex.InnerException);
  69. Assert.AreEqual("socketFactory", ex.ParamName);
  70. }
  71. }
  72. private static ConnectionInfo CreateConnectionInfo(IPEndPoint serverEndPoint, TimeSpan timeout)
  73. {
  74. return new ConnectionInfo(serverEndPoint.Address.ToString(), serverEndPoint.Port, "eric", new NoneAuthenticationMethod("eric"))
  75. {
  76. Timeout = timeout
  77. };
  78. }
  79. }
  80. }