SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 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. int readBytesCount1 = _sftpFileStream.Read(_readBytes1, 0, _readBytes1.Length);
  99. Assert.AreEqual(_readBytes1.Length, readBytesCount1);
  100. int readBytesCount2 = _sftpFileStream.Read(_readBytes2, 0, _readBytes2.Length); // this will return bytes from the buffer
  101. Assert.AreEqual(_readBytes2.Length, readBytesCount2);
  102. }
  103. protected override void Act()
  104. {
  105. _sftpFileStream.SetLength(_length);
  106. }
  107. [TestMethod]
  108. public void PositionShouldReturnSamePositionAsBeforeSetLength()
  109. {
  110. SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  111. Assert.AreEqual(_readBytes1.Length + _readBytes2.Length, _sftpFileStream.Position);
  112. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
  113. }
  114. [TestMethod]
  115. public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce()
  116. {
  117. SftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once);
  118. }
  119. [TestMethod]
  120. public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat()
  121. {
  122. DictionaryAssert.AreEqual(_originalFileAttributes.Extensions, _newFileAttributes.Extensions);
  123. Assert.AreEqual(_originalFileAttributes.GroupId, _newFileAttributes.GroupId);
  124. Assert.AreEqual(_originalFileAttributes.LastAccessTime, _newFileAttributes.LastAccessTime);
  125. Assert.AreEqual(_originalFileAttributes.LastWriteTime, _newFileAttributes.LastWriteTime);
  126. Assert.AreEqual(_originalFileAttributes.Permissions, _newFileAttributes.Permissions);
  127. Assert.AreEqual(_originalFileAttributes.UserId, _newFileAttributes.UserId);
  128. Assert.AreEqual(_length, _newFileAttributes.Size);
  129. }
  130. [TestMethod]
  131. public void ReadShouldReadStartFromSamePositionAsBeforeSetLength()
  132. {
  133. SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  134. SftpSessionMock.InSequence(_sequence)
  135. .Setup(p => p.RequestRead(_handle,
  136. (uint)(_readBytes1.Length + _readBytes2.Length),
  137. _readBufferSize))
  138. .Returns(new byte[] { 0x0f });
  139. var byteRead = _sftpFileStream.ReadByte();
  140. Assert.AreEqual(0x0f, byteRead);
  141. SftpSessionMock.Verify(p => p.RequestRead(_handle,
  142. (uint)(_readBytes1.Length + _readBytes2.Length),
  143. _readBufferSize),
  144. Times.Once);
  145. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
  146. }
  147. [TestMethod]
  148. public void WriteShouldStartFromSamePositionAsBeforeSetLength()
  149. {
  150. var bytesToWrite = GenerateRandom(_writeBufferSize);
  151. byte[] bytesWritten = null;
  152. SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  153. SftpSessionMock.InSequence(_sequence)
  154. .Setup(p => p.RequestWrite(_handle,
  155. (uint)(_readBytes1.Length + _readBytes2.Length),
  156. It.IsAny<byte[]>(),
  157. 0,
  158. bytesToWrite.Length,
  159. It.IsAny<AutoResetEvent>(),
  160. null))
  161. .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>(
  162. (handle, serverOffset, data, offset, length, wait, writeCompleted) =>
  163. {
  164. bytesWritten = data.Take(offset, length);
  165. wait.Set();
  166. });
  167. _sftpFileStream.Write(bytesToWrite, 0, bytesToWrite.Length);
  168. Assert.IsNotNull(bytesWritten);
  169. CollectionAssert.AreEqual(bytesToWrite, bytesWritten);
  170. SftpSessionMock.Verify(p => p.RequestWrite(_handle,
  171. (uint)(_readBytes1.Length + _readBytes2.Length),
  172. It.IsAny<byte[]>(),
  173. 0,
  174. bytesToWrite.Length,
  175. It.IsAny<AutoResetEvent>(),
  176. null),
  177. Times.Once);
  178. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
  179. }
  180. }
  181. }