SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthLessThanPosition.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. using Renci.SshNet.Tests.Common;
  8. using System.Threading;
  9. using Renci.SshNet.Sftp.Responses;
  10. using Renci.SshNet.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. _sftpFileStream.Read(_readBytes, 0, _readBytes.Length);
  90. }
  91. protected override void Act()
  92. {
  93. _sftpFileStream.SetLength(_length);
  94. }
  95. [TestMethod]
  96. public void PositionShouldReturnLengthOfStream()
  97. {
  98. SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  99. Assert.AreEqual(_length, _sftpFileStream.Position);
  100. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
  101. }
  102. [TestMethod]
  103. public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce()
  104. {
  105. SftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once);
  106. }
  107. [TestMethod]
  108. public void SizeOfSftpFileAttributesShouldBeModifiedToNewLengthBeforePassedToRequestFSetStat()
  109. {
  110. DictionaryAssert.AreEqual(_originalFileAttributes.Extensions, _newFileAttributes.Extensions);
  111. Assert.AreEqual(_originalFileAttributes.GroupId, _newFileAttributes.GroupId);
  112. Assert.AreEqual(_originalFileAttributes.LastAccessTime, _newFileAttributes.LastAccessTime);
  113. Assert.AreEqual(_originalFileAttributes.LastWriteTime, _newFileAttributes.LastWriteTime);
  114. Assert.AreEqual(_originalFileAttributes.Permissions, _newFileAttributes.Permissions);
  115. Assert.AreEqual(_originalFileAttributes.UserId, _newFileAttributes.UserId);
  116. Assert.AreEqual(_length, _newFileAttributes.Size);
  117. }
  118. [TestMethod]
  119. public void ReadShouldStartFromEndOfStream()
  120. {
  121. SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  122. SftpSessionMock.InSequence(_sequence)
  123. .Setup(p => p.RequestRead(_handle, (uint) _length, _readBufferSize))
  124. .Returns(Array<byte>.Empty);
  125. var byteRead = _sftpFileStream.ReadByte();
  126. Assert.AreEqual(-1, byteRead);
  127. SftpSessionMock.Verify(p => p.RequestRead(_handle, (uint) _length, _readBufferSize), Times.Once);
  128. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
  129. }
  130. [TestMethod]
  131. public void WriteShouldStartFromEndOfStream()
  132. {
  133. var bytesToWrite = GenerateRandom(_writeBufferSize);
  134. byte[] bytesWritten = null;
  135. SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
  136. SftpSessionMock.InSequence(_sequence)
  137. .Setup(p => p.RequestWrite(_handle, (uint) _length, It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null))
  138. .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverOffset, data, offset, length, wait, writeCompleted) =>
  139. {
  140. bytesWritten = data.Take(offset, length);
  141. wait.Set();
  142. });
  143. _sftpFileStream.Write(bytesToWrite, 0, bytesToWrite.Length);
  144. Assert.IsNotNull(bytesWritten);
  145. CollectionAssert.AreEqual(bytesToWrite, bytesWritten);
  146. SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint)_length, It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null), Times.Once);
  147. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
  148. }
  149. }
  150. }