SftpFileStreamTest_Flush_WriteMode_DataInBuffer.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Moq;
  6. using Renci.SshNet.Common;
  7. using Renci.SshNet.Sftp;
  8. using Renci.SshNet.Sftp.Responses;
  9. using Renci.SshNet.Tests.Common;
  10. namespace Renci.SshNet.Tests.Classes.Sftp
  11. {
  12. [TestClass]
  13. public class SftpFileStreamTest_Flush_WriteMode_DataInBuffer : SftpFileStreamTestBase
  14. {
  15. private SftpFileStream _target;
  16. private string _path;
  17. private byte[] _handle;
  18. private uint _bufferSize;
  19. private uint _readBufferSize;
  20. private uint _writeBufferSize;
  21. private byte[] _writeBytes1;
  22. private byte[] _writeBytes2;
  23. private byte[] _writeBytes3;
  24. private byte[] _flushedBytes;
  25. protected override void SetupData()
  26. {
  27. base.SetupData();
  28. var random = new Random();
  29. _path = random.Next().ToString();
  30. _handle = GenerateRandom(5, random);
  31. _bufferSize = (uint)random.Next(1, 1000);
  32. _readBufferSize = 100;
  33. _writeBufferSize = 500;
  34. _writeBytes1 = GenerateRandom(_writeBufferSize);
  35. _writeBytes2 = GenerateRandom(2);
  36. _writeBytes3 = GenerateRandom(3);
  37. _flushedBytes = null;
  38. }
  39. protected override void SetupMocks()
  40. {
  41. _ = SftpSessionMock.InSequence(MockSequence)
  42. .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, false))
  43. .Returns(_handle);
  44. _ = SftpSessionMock.InSequence(MockSequence)
  45. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  46. .Returns(_readBufferSize);
  47. _ = SftpSessionMock.InSequence(MockSequence)
  48. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  49. .Returns(_writeBufferSize);
  50. _ = SftpSessionMock.InSequence(MockSequence)
  51. .Setup(p => p.IsOpen)
  52. .Returns(true);
  53. _ = SftpSessionMock.InSequence(MockSequence)
  54. .Setup(p => p.RequestWrite(_handle, 0UL, It.IsAny<byte[]>(), 0, _writeBytes1.Length, It.IsAny<AutoResetEvent>(), null))
  55. .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverOffset, data, offset, length, wait, writeCompleted) =>
  56. {
  57. _ = wait.Set();
  58. });
  59. _ = SftpSessionMock.InSequence(MockSequence)
  60. .Setup(p => p.IsOpen)
  61. .Returns(true);
  62. _ = SftpSessionMock.InSequence(MockSequence)
  63. .Setup(p => p.IsOpen)
  64. .Returns(true);
  65. _ = SftpSessionMock.InSequence(MockSequence)
  66. .Setup(p => p.IsOpen)
  67. .Returns(true);
  68. _ = SftpSessionMock.InSequence(MockSequence)
  69. .Setup(p => p.RequestWrite(_handle, (ulong)_writeBytes1.Length, It.IsAny<byte[]>(), 0, _writeBytes2.Length + _writeBytes3.Length, It.IsAny<AutoResetEvent>(), null))
  70. .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverOffset, data, offset, length, wait, writeCompleted) =>
  71. {
  72. _flushedBytes = data.Take(offset, length);
  73. _ = wait.Set();
  74. });
  75. }
  76. protected override void Arrange()
  77. {
  78. base.Arrange();
  79. _target = new SftpFileStream(SftpSessionMock.Object,
  80. _path,
  81. FileMode.Open,
  82. FileAccess.ReadWrite,
  83. (int)_bufferSize);
  84. _target.Write(_writeBytes1, 0, _writeBytes1.Length);
  85. _target.Write(_writeBytes2, 0, _writeBytes2.Length);
  86. _target.Write(_writeBytes3, 0, _writeBytes3.Length);
  87. }
  88. protected override void Act()
  89. {
  90. _target.Flush();
  91. }
  92. [TestMethod]
  93. public void PositionShouldReturnSameValueAsBeforeFlush()
  94. {
  95. _ = SftpSessionMock.InSequence(MockSequence)
  96. .Setup(p => p.IsOpen)
  97. .Returns(true);
  98. Assert.AreEqual(_writeBytes1.Length + _writeBytes2.Length + _writeBytes3.Length, _target.Position);
  99. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(5));
  100. }
  101. [TestMethod]
  102. public void BufferedBytesShouldHaveBeenWrittenToTheServer()
  103. {
  104. var expected = new ArrayBuilder<byte>().Add(_writeBytes2)
  105. .Add(_writeBytes3)
  106. .Build();
  107. Assert.IsNotNull(_flushedBytes);
  108. CollectionAssert.AreEqual(expected, _flushedBytes);
  109. }
  110. [TestMethod]
  111. public void ReadShouldReadFromServer()
  112. {
  113. var serverBytes = GenerateRandom(5);
  114. var readBytes = new byte[5];
  115. var expectedReadBytes = new ArrayBuilder<byte>().Add(new byte[2])
  116. .Add(serverBytes.Take(0, 3))
  117. .Build();
  118. _ = SftpSessionMock.InSequence(MockSequence)
  119. .Setup(p => p.IsOpen)
  120. .Returns(true);
  121. _ = SftpSessionMock.InSequence(MockSequence)
  122. .Setup(p => p.RequestRead(_handle, (ulong)(_writeBytes1.Length + _writeBytes2.Length + _writeBytes3.Length), _readBufferSize))
  123. .Returns(serverBytes);
  124. var bytesRead = _target.Read(readBytes, 2, 3);
  125. Assert.AreEqual(3, bytesRead);
  126. CollectionAssert.AreEqual(expectedReadBytes, readBytes);
  127. SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)(_writeBytes1.Length + _writeBytes2.Length + _writeBytes3.Length), _readBufferSize), Times.Once);
  128. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(5));
  129. }
  130. }
  131. }