SftpFileStreamTest_Dispose_Closed.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.IO;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Sftp;
  6. namespace Renci.SshNet.Tests.Classes.Sftp
  7. {
  8. [TestClass]
  9. public class SftpFileStreamTest_Dispose_Closed : SftpFileStreamTestBase
  10. {
  11. private SftpFileStream _target;
  12. private string _path;
  13. private byte[] _handle;
  14. private uint _bufferSize;
  15. private uint _readBufferSize;
  16. private uint _writeBufferSize;
  17. protected override void SetupData()
  18. {
  19. base.SetupData();
  20. var random = new Random();
  21. _path = random.Next().ToString();
  22. _handle = GenerateRandom(random.Next(1, 5), random);
  23. _bufferSize = (uint) random.Next(1, 1000);
  24. _readBufferSize = (uint) random.Next(1, 1000);
  25. _writeBufferSize = (uint) random.Next(1, 1000);
  26. }
  27. protected override void SetupMocks()
  28. {
  29. _ = SftpSessionMock.InSequence(MockSequence)
  30. .Setup(p => p.RequestOpen(_path, Flags.Read, false))
  31. .Returns(_handle);
  32. _ = SftpSessionMock.InSequence(MockSequence)
  33. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  34. .Returns(_readBufferSize);
  35. _ = SftpSessionMock.InSequence(MockSequence)
  36. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  37. .Returns(_writeBufferSize);
  38. _ = SftpSessionMock.InSequence(MockSequence)
  39. .Setup(p => p.IsOpen)
  40. .Returns(true);
  41. _ = SftpSessionMock.InSequence(MockSequence)
  42. .Setup(p => p.RequestClose(_handle));
  43. }
  44. protected override void Arrange()
  45. {
  46. base.Arrange();
  47. _target = new SftpFileStream(SftpSessionMock.Object,
  48. _path,
  49. FileMode.Open,
  50. FileAccess.Read,
  51. (int) _bufferSize);
  52. _target.Close();
  53. }
  54. protected override void Act()
  55. {
  56. _target.Dispose();
  57. }
  58. [TestMethod]
  59. public void IsOpenOnSftpSessionShouldBeInvokedOnce()
  60. {
  61. SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
  62. }
  63. [TestMethod]
  64. public void RequestCloseOnSftpSessionShouldBeInvokedOnce()
  65. {
  66. SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
  67. }
  68. }
  69. }