SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthLessThanPosition.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Threading;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Moq;
  7. using Renci.SshNet.Common;
  8. using Renci.SshNet.Sftp;
  9. using Renci.SshNet.Sftp.Responses;
  10. using Renci.SshNet.Tests.Common;
  11. namespace Renci.SshNet.Tests.Classes.Sftp
  12. {
  13. /// <summary>
  14. /// - In read mode
  15. /// - Bytes in (read) buffer, but not read from
  16. /// - New length less than client position and less than server position
  17. /// </summary>
  18. [TestClass]
  19. public class SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthLessThanPosition : SftpFileStreamTestBase
  20. {
  21. private string _path;
  22. private SftpFileStream _sftpFileStream;
  23. private byte[] _handle;
  24. private uint _bufferSize;
  25. private uint _readBufferSize;
  26. private uint _writeBufferSize;
  27. private MockSequence _sequence;
  28. private long _length;
  29. private SftpFileAttributes _fileAttributes;
  30. private SftpFileAttributes _originalFileAttributes;
  31. private SftpFileAttributes _newFileAttributes;
  32. private byte[] _readBytes;
  33. private byte[] _actualReadBytes;
  34. protected override void SetupData()
  35. {
  36. var random = new Random();
  37. _path = random.Next().ToString(CultureInfo.InvariantCulture);
  38. _handle = GenerateRandom(random.Next(2, 6), random);
  39. _bufferSize = (uint)random.Next(1, 1000);
  40. _readBufferSize = (uint)random.Next(1, 1000);
  41. _writeBufferSize = (uint)random.Next(100, 1000);
  42. _readBytes = new byte[5];
  43. _actualReadBytes = GenerateRandom(_readBytes.Length + 2, random); // add 2 bytes in read buffer
  44. _length = _readBytes.Length - 2;
  45. _fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC")
  46. .WithExtension("V", "VValue")
  47. .WithGroupId(random.Next())
  48. .WithLastAccessTime(DateTime.UtcNow.AddSeconds(random.Next()))
  49. .WithLastWriteTime(DateTime.UtcNow.AddSeconds(random.Next()))
  50. .WithPermissions((uint)random.Next())
  51. .WithSize(_length + 100)
  52. .WithUserId(random.Next())
  53. .Build();
  54. _originalFileAttributes = _fileAttributes.Clone();
  55. _newFileAttributes = null;
  56. }
  57. protected override void SetupMocks()
  58. {
  59. _sequence = new MockSequence();
  60. SftpSessionMock.InSequence(_sequence)
  61. .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false))
  62. .Returns(_handle);
  63. SftpSessionMock.InSequence(_sequence)
  64. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  65. .Returns(_readBufferSize);
  66. SftpSessionMock.InSequence(_sequence)
  67. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  68. .Returns(_writeBufferSize);
  69. SftpSessionMock.InSequence(_sequence)
  70. .Setup(p => p.IsOpen)
  71. .Returns(true);
  72. SftpSessionMock.InSequence(_sequence)
  73. .Setup(p => p.RequestRead(_handle, 0, _readBufferSize))
  74. .Returns(_actualReadBytes);
  75. SftpSessionMock.InSequence(_sequence)
  76. .Setup(p => p.IsOpen)
  77. .Returns(true);
  78. SftpSessionMock.InSequence(_sequence)
  79. .Setup(p => p.RequestFStat(_handle, false))
  80. .Returns(_fileAttributes);
  81. SftpSessionMock.InSequence(_sequence)
  82. .Setup(p => p.RequestFSetStat(_handle, _fileAttributes))
  83. .Callback<byte[], SftpFileAttributes>((bytes, attributes) => _newFileAttributes = attributes.Clone());
  84. }
  85. protected override void Arrange()
  86. {
  87. base.Arrange();
  88. _sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize);
  89. int readBytesCount = _sftpFileStream.Read(_readBytes, 0, _readBytes.Length);
  90. Assert.AreEqual(_readBytes.Length, readBytesCount);
  91. }
  92. protected override void Act()
  93. {
  94. _sftpFileStream.SetLength(_length);
  95. }
  96. [TestMethod]
  97. public void PositionShouldReturnLengthOfStream()
  98. {
  99. SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  100. Assert.AreEqual(_length, _sftpFileStream.Position);
  101. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
  102. }
  103. [TestMethod]
  104. public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce()
  105. {
  106. SftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once);
  107. }
  108. [TestMethod]
  109. public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat()
  110. {
  111. DictionaryAssert.AreEqual(_originalFileAttributes.Extensions, _newFileAttributes.Extensions);
  112. Assert.AreEqual(_originalFileAttributes.GroupId, _newFileAttributes.GroupId);
  113. Assert.AreEqual(_originalFileAttributes.LastAccessTime, _newFileAttributes.LastAccessTime);
  114. Assert.AreEqual(_originalFileAttributes.LastWriteTime, _newFileAttributes.LastWriteTime);
  115. Assert.AreEqual(_originalFileAttributes.Permissions, _newFileAttributes.Permissions);
  116. Assert.AreEqual(_originalFileAttributes.UserId, _newFileAttributes.UserId);
  117. Assert.AreEqual(_length, _newFileAttributes.Size);
  118. }
  119. [TestMethod]
  120. public void ReadShouldStartFromEndOfStream()
  121. {
  122. SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  123. SftpSessionMock.InSequence(_sequence)
  124. .Setup(p => p.RequestRead(_handle, (uint)_length, _readBufferSize))
  125. .Returns(Array.Empty<byte>());
  126. var byteRead = _sftpFileStream.ReadByte();
  127. Assert.AreEqual(-1, byteRead);
  128. SftpSessionMock.Verify(p => p.RequestRead(_handle, (uint)_length, _readBufferSize), Times.Once);
  129. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
  130. }
  131. [TestMethod]
  132. public void WriteShouldStartFromEndOfStream()
  133. {
  134. var bytesToWrite = GenerateRandom(_writeBufferSize);
  135. byte[] bytesWritten = null;
  136. SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  137. SftpSessionMock.InSequence(_sequence)
  138. .Setup(p => p.RequestWrite(_handle, (uint)_length, It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null))
  139. .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverOffset, data, offset, length, wait, writeCompleted) =>
  140. {
  141. bytesWritten = data.Take(offset, length);
  142. wait.Set();
  143. });
  144. _sftpFileStream.Write(bytesToWrite, 0, bytesToWrite.Length);
  145. Assert.IsNotNull(bytesWritten);
  146. CollectionAssert.AreEqual(bytesToWrite, bytesWritten);
  147. SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint)_length, It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null), Times.Once);
  148. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
  149. }
  150. }
  151. }