SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition.cs 9.7 KB

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