2
0

SftpFileStreamTest_Close_Closed.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 uint _bufferSize;
  17. private uint _readBufferSize;
  18. private uint _writeBufferSize;
  19. [TestInitialize]
  20. public void Setup()
  21. {
  22. Arrange();
  23. Act();
  24. }
  25. protected void Arrange()
  26. {
  27. var random = new Random();
  28. _path = random.Next().ToString(CultureInfo.InvariantCulture);
  29. _handle = new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) };
  30. _bufferSize = (uint)random.Next(1, 1000);
  31. _readBufferSize = (uint)random.Next(0, 1000);
  32. _writeBufferSize = (uint)random.Next(0, 1000);
  33. _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
  34. var sequence = new MockSequence();
  35. _ = _sftpSessionMock.InSequence(sequence)
  36. .Setup(p => p.RequestOpen(_path, Flags.Read, false))
  37. .Returns(_handle);
  38. _ = _sftpSessionMock.InSequence(sequence)
  39. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  40. .Returns(_readBufferSize);
  41. _ = _sftpSessionMock.InSequence(sequence)
  42. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  43. .Returns(_writeBufferSize);
  44. _ = _sftpSessionMock.InSequence(sequence)
  45. .Setup(p => p.IsOpen)
  46. .Returns(true);
  47. _ = _sftpSessionMock.InSequence(sequence)
  48. .Setup(p => p.RequestClose(_handle));
  49. _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Open, FileAccess.Read, (int)_bufferSize);
  50. _sftpFileStream.Close();
  51. }
  52. protected void Act()
  53. {
  54. _sftpFileStream.Close();
  55. }
  56. [TestMethod]
  57. public void IsOpenOnSftpSessionShouldBeInvokedOnce()
  58. {
  59. _sftpSessionMock.Verify(p => p.IsOpen, Times.Once);
  60. }
  61. [TestMethod]
  62. public void RequestCloseOnSftpSessionShouldBeInvokedOnce()
  63. {
  64. _sftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
  65. }
  66. }
  67. }