ServiceFactoryTest_CreateSftpFileReader_EndLStatThrowsSshException.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using Microsoft.Extensions.Logging.Abstractions;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Abstractions;
  6. using Renci.SshNet.Common;
  7. using Renci.SshNet.Sftp;
  8. namespace Renci.SshNet.Tests.Classes
  9. {
  10. [TestClass]
  11. public class ServiceFactoryTest_CreateSftpFileReader_EndLStatThrowsSshException
  12. {
  13. private ServiceFactory _serviceFactory;
  14. private Mock<ISftpSession> _sftpSessionMock;
  15. private Mock<ISftpFileReader> _sftpFileReaderMock;
  16. private uint _bufferSize;
  17. private string _fileName;
  18. private SftpOpenAsyncResult _openAsyncResult;
  19. private byte[] _handle;
  20. private SFtpStatAsyncResult _statAsyncResult;
  21. private uint _chunkSize;
  22. private ISftpFileReader _actual;
  23. private void SetupData()
  24. {
  25. var random = new Random();
  26. _bufferSize = (uint)random.Next(1, int.MaxValue);
  27. _openAsyncResult = new SftpOpenAsyncResult(null, null);
  28. _handle = CryptoAbstraction.GenerateRandom(random.Next(1, 10));
  29. _statAsyncResult = new SFtpStatAsyncResult(null, null);
  30. _fileName = random.Next().ToString();
  31. _chunkSize = (uint)random.Next(1, int.MaxValue);
  32. }
  33. private void CreateMocks()
  34. {
  35. _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
  36. _sftpSessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  37. _sftpFileReaderMock = new Mock<ISftpFileReader>(MockBehavior.Strict);
  38. }
  39. private void SetupMocks()
  40. {
  41. var seq = new MockSequence();
  42. _sftpSessionMock.InSequence(seq)
  43. .Setup(p => p.BeginOpen(_fileName, Flags.Read, null, null))
  44. .Returns(_openAsyncResult);
  45. _sftpSessionMock.InSequence(seq)
  46. .Setup(p => p.EndOpen(_openAsyncResult))
  47. .Returns(_handle);
  48. _sftpSessionMock.InSequence(seq)
  49. .Setup(p => p.BeginLStat(_fileName, null, null))
  50. .Returns(_statAsyncResult);
  51. _sftpSessionMock.InSequence(seq)
  52. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  53. .Returns(_chunkSize);
  54. _sftpSessionMock.InSequence(seq)
  55. .Setup(p => p.EndLStat(_statAsyncResult))
  56. .Throws(new SshException());
  57. _sftpSessionMock.InSequence(seq)
  58. .Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 10, null))
  59. .Returns(_sftpFileReaderMock.Object);
  60. }
  61. private void Arrange()
  62. {
  63. SetupData();
  64. CreateMocks();
  65. SetupMocks();
  66. _serviceFactory = new ServiceFactory();
  67. }
  68. [TestInitialize]
  69. public void Initialize()
  70. {
  71. Arrange();
  72. Act();
  73. }
  74. private void Act()
  75. {
  76. _actual = _serviceFactory.CreateSftpFileReader(_fileName, _sftpSessionMock.Object, _bufferSize);
  77. }
  78. [TestMethod]
  79. public void CreateSftpFileReaderShouldReturnCreatedInstance()
  80. {
  81. Assert.IsNotNull(_actual);
  82. Assert.AreSame(_sftpFileReaderMock.Object, _actual);
  83. }
  84. }
  85. }