SftpFileStreamTest_SetLength_DataInWriteBuffer_NewLengthLessThanPosition.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 write mode
  15. /// - Bytes in (write) buffer
  16. /// - New length less than client position and less than server position
  17. /// </summary>
  18. [TestClass]
  19. public class SftpFileStreamTest_SetLength_DataInWriteBuffer_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. private byte[] _writeBytes;
  35. private byte[] _actualWrittenBytes;
  36. protected override void SetupData()
  37. {
  38. var random = new Random();
  39. _path = random.Next().ToString(CultureInfo.InvariantCulture);
  40. _handle = GenerateRandom(random.Next(2, 6), random);
  41. _bufferSize = (uint)random.Next(1, 1000);
  42. _readBufferSize = (uint)random.Next(1, 1000);
  43. _writeBufferSize = (uint)random.Next(100, 1000);
  44. _readBytes = new byte[5];
  45. _actualReadBytes = GenerateRandom(_readBytes.Length, random);
  46. _writeBytes = new byte[] { 0x01, 0x02, 0x03, 0x04 };
  47. _length = _readBytes.Length - 2;
  48. _actualWrittenBytes = null;
  49. _fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC")
  50. .WithExtension("V", "VValue")
  51. .WithGroupId(random.Next())
  52. .WithLastAccessTime(DateTime.UtcNow.AddSeconds(random.Next()))
  53. .WithLastWriteTime(DateTime.UtcNow.AddSeconds(random.Next()))
  54. .WithPermissions((uint)random.Next())
  55. .WithSize(_length + 100)
  56. .WithUserId(random.Next())
  57. .Build();
  58. _originalFileAttributes = _fileAttributes.Clone();
  59. _newFileAttributes = null;
  60. }
  61. protected override void SetupMocks()
  62. {
  63. _sequence = new MockSequence();
  64. SftpSessionMock.InSequence(_sequence)
  65. .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false))
  66. .Returns(_handle);
  67. SftpSessionMock.InSequence(_sequence)
  68. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  69. .Returns(_readBufferSize);
  70. SftpSessionMock.InSequence(_sequence)
  71. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  72. .Returns(_writeBufferSize);
  73. SftpSessionMock.InSequence(_sequence)
  74. .Setup(p => p.IsOpen)
  75. .Returns(true);
  76. SftpSessionMock.InSequence(_sequence)
  77. .Setup(p => p.RequestRead(_handle, 0, _readBufferSize))
  78. .Returns(_actualReadBytes);
  79. SftpSessionMock.InSequence(_sequence)
  80. .Setup(p => p.IsOpen)
  81. .Returns(true);
  82. SftpSessionMock.InSequence(_sequence)
  83. .Setup(p => p.IsOpen)
  84. .Returns(true);
  85. SftpSessionMock.InSequence(_sequence)
  86. .Setup(p => p.RequestWrite(_handle, (uint)_readBytes.Length, It.IsAny<byte[]>(), 0, _writeBytes.Length, It.IsAny<AutoResetEvent>(), null))
  87. .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverOffset, data, offset, length, wait, writeCompleted)
  88. =>
  89. {
  90. _actualWrittenBytes = data.Take(offset, length);
  91. wait.Set();
  92. });
  93. SftpSessionMock.InSequence(_sequence)
  94. .Setup(p => p.RequestFStat(_handle, false))
  95. .Returns(_fileAttributes);
  96. SftpSessionMock.InSequence(_sequence)
  97. .Setup(p => p.RequestFSetStat(_handle, _fileAttributes))
  98. .Callback<byte[], SftpFileAttributes>((bytes, attributes) => _newFileAttributes = attributes.Clone());
  99. }
  100. protected override void Arrange()
  101. {
  102. base.Arrange();
  103. _sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize);
  104. int readBytesCount = _sftpFileStream.Read(_readBytes, 0, _readBytes.Length);
  105. Assert.AreEqual(_readBytes.Length, readBytesCount);
  106. _sftpFileStream.Write(new byte[] { 0x01, 0x02, 0x03, 0x04 }, 0, 4);
  107. }
  108. protected override void Act()
  109. {
  110. _sftpFileStream.SetLength(_length);
  111. }
  112. [TestMethod]
  113. public void PositionShouldReturnLengthOfStream()
  114. {
  115. SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  116. Assert.AreEqual(_length, _sftpFileStream.Position);
  117. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
  118. }
  119. [TestMethod]
  120. public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce()
  121. {
  122. SftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once);
  123. }
  124. [TestMethod]
  125. public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat()
  126. {
  127. DictionaryAssert.AreEqual(_originalFileAttributes.Extensions, _newFileAttributes.Extensions);
  128. Assert.AreEqual(_originalFileAttributes.GroupId, _newFileAttributes.GroupId);
  129. Assert.AreEqual(_originalFileAttributes.LastAccessTime, _newFileAttributes.LastAccessTime);
  130. Assert.AreEqual(_originalFileAttributes.LastWriteTime, _newFileAttributes.LastWriteTime);
  131. Assert.AreEqual(_originalFileAttributes.Permissions, _newFileAttributes.Permissions);
  132. Assert.AreEqual(_originalFileAttributes.UserId, _newFileAttributes.UserId);
  133. Assert.AreEqual(_length, _newFileAttributes.Size);
  134. }
  135. [TestMethod]
  136. public void WrittenBytesShouldByFlushedToServer()
  137. {
  138. Assert.IsNotNull(_actualWrittenBytes);
  139. CollectionAssert.AreEqual(_writeBytes, _actualWrittenBytes);
  140. }
  141. [TestMethod]
  142. public void ReadShouldStartFromEndOfStream()
  143. {
  144. SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  145. SftpSessionMock.InSequence(_sequence)
  146. .Setup(p => p.RequestRead(_handle, (uint)_length, _readBufferSize))
  147. .Returns(Array.Empty<byte>());
  148. var byteRead = _sftpFileStream.ReadByte();
  149. Assert.AreEqual(-1, byteRead);
  150. SftpSessionMock.Verify(p => p.RequestRead(_handle, (uint)_length, _readBufferSize), Times.Once);
  151. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
  152. }
  153. [TestMethod]
  154. public void WriteShouldStartFromEndOfStream()
  155. {
  156. var bytesToWrite = GenerateRandom(_writeBufferSize);
  157. byte[] bytesWritten = null;
  158. SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  159. SftpSessionMock.InSequence(_sequence)
  160. .Setup(p => p.RequestWrite(_handle, (uint)_length, It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null))
  161. .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverOffset, data, offset, length, wait, writeCompleted) =>
  162. {
  163. bytesWritten = data.Take(offset, length);
  164. wait.Set();
  165. });
  166. _sftpFileStream.Write(bytesToWrite, 0, bytesToWrite.Length);
  167. Assert.IsNotNull(bytesWritten);
  168. CollectionAssert.AreEqual(bytesToWrite, bytesWritten);
  169. SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint)_length, It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null), Times.Once);
  170. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
  171. }
  172. }
  173. }