SftpFileStreamTest_Close_Closed.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Moq;
  6. using Renci.SshNet.Sftp;
  7. namespace Renci.SshNet.Tests.Classes.Sftp
  8. {
  9. [TestClass]
  10. public class SftpFileStreamTest_Close_Closed
  11. {
  12. private Mock<ISftpSession> _sftpSessionMock;
  13. private string _path;
  14. private SftpFileStream _sftpFileStream;
  15. private byte[] _handle;
  16. private SftpFileAttributes _fileAttributes;
  17. private uint _bufferSize;
  18. private uint _readBufferSize;
  19. private uint _writeBufferSize;
  20. [TestInitialize]
  21. public void Setup()
  22. {
  23. Arrange();
  24. Act();
  25. }
  26. protected void Arrange()
  27. {
  28. var random = new Random();
  29. _path = random.Next().ToString(CultureInfo.InvariantCulture);
  30. _handle = new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) };
  31. _fileAttributes = SftpFileAttributes.Empty;
  32. _bufferSize = (uint)random.Next(1, 1000);
  33. _readBufferSize = (uint)random.Next(0, 1000);
  34. _writeBufferSize = (uint)random.Next(0, 1000);
  35. _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
  36. var sequence = new MockSequence();
  37. _sftpSessionMock.InSequence(sequence)
  38. .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Truncate, true))
  39. .Returns(_handle);
  40. _sftpSessionMock.InSequence(sequence).Setup(p => p.RequestFStat(_handle)).Returns(_fileAttributes);
  41. _sftpSessionMock.InSequence(sequence)
  42. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  43. .Returns(_readBufferSize);
  44. _sftpSessionMock.InSequence(sequence)
  45. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  46. .Returns(_writeBufferSize);
  47. _sftpSessionMock.InSequence(sequence)
  48. .Setup(p => p.IsOpen)
  49. .Returns(true);
  50. _sftpSessionMock.InSequence(sequence)
  51. .Setup(p => p.RequestClose(_handle));
  52. _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Read, (int)_bufferSize);
  53. _sftpFileStream.Close();
  54. }
  55. protected void Act()
  56. {
  57. _sftpFileStream.Close();
  58. }
  59. [TestMethod]
  60. public void IsOpenOnSftpSessionShouldBeInvokedOnce()
  61. {
  62. _sftpSessionMock.Verify(p => p.IsOpen, Times.Once);
  63. }
  64. [TestMethod]
  65. public void RequestCloseOnSftpSessionShouldBeInvokedOnce()
  66. {
  67. _sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
  68. }
  69. }
  70. }