SftpFileStreamTest_SetLength_SessionOpen_FIleAccessWrite.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_SetLength_SessionOpen_FIleAccessWrite
  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. private MockSequence _sequence;
  21. private long _length;
  22. private long _lengthPassedToRequestFSetStat;
  23. [TestInitialize]
  24. public void Setup()
  25. {
  26. Arrange();
  27. Act();
  28. }
  29. protected void Arrange()
  30. {
  31. var random = new Random();
  32. _path = random.Next().ToString(CultureInfo.InvariantCulture);
  33. _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
  34. _fileAttributes = SftpFileAttributes.Empty;
  35. _bufferSize = (uint) random.Next(1, 1000);
  36. _readBufferSize = (uint) random.Next(0, 1000);
  37. _writeBufferSize = (uint) random.Next(0, 1000);
  38. _length = random.Next();
  39. _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
  40. _sequence = new MockSequence();
  41. _sftpSessionMock.InSequence(_sequence)
  42. .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true))
  43. .Returns(_handle);
  44. _sftpSessionMock.InSequence(_sequence).Setup(p => p.RequestFStat(_handle)).Returns(_fileAttributes);
  45. _sftpSessionMock.InSequence(_sequence)
  46. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  47. .Returns(_readBufferSize);
  48. _sftpSessionMock.InSequence(_sequence)
  49. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  50. .Returns(_writeBufferSize);
  51. _sftpSessionMock.InSequence(_sequence)
  52. .Setup(p => p.IsOpen)
  53. .Returns(true);
  54. _sftpSessionMock.InSequence(_sequence)
  55. .Setup(p => p.RequestFSetStat(_handle, _fileAttributes))
  56. .Callback<byte[], SftpFileAttributes>((bytes, attributes) => _lengthPassedToRequestFSetStat = attributes.Size);
  57. _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize);
  58. }
  59. protected void Act()
  60. {
  61. _sftpFileStream.SetLength(_length);
  62. }
  63. [TestMethod]
  64. public void PositionShouldReturnOriginalPosition()
  65. {
  66. _sftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  67. Assert.AreEqual(0, _sftpFileStream.Position);
  68. _sftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2));
  69. }
  70. [TestMethod]
  71. public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce()
  72. {
  73. _sftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once);
  74. }
  75. [TestMethod]
  76. public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat()
  77. {
  78. Assert.AreEqual(_length, _lengthPassedToRequestFSetStat);
  79. }
  80. [TestMethod]
  81. public void SizeOfSftpFileAttributesShouldBeEqualToNewLength()
  82. {
  83. Assert.AreEqual(_length, _fileAttributes.Size);
  84. }
  85. }
  86. }