2
0

ServiceFactoryTest_CreateSftpFileReader_EndLStatThrowsSshException.cs 3.3 KB

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