SftpFileStreamTest_Flush_ReadMode_DataInBuffer_ReadFromBuffer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.IO;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Sftp;
  7. using Renci.SshNet.Tests.Common;
  8. namespace Renci.SshNet.Tests.Classes.Sftp
  9. {
  10. [TestClass]
  11. public class SftpFileStreamTest_Flush_ReadMode_DataInBuffer_ReadFromBuffer : SftpFileStreamTestBase
  12. {
  13. private SftpFileStream _target;
  14. private string _path;
  15. private byte[] _handle;
  16. private uint _bufferSize;
  17. private uint _readBufferSize;
  18. private uint _writeBufferSize;
  19. private byte[] _readBytes1;
  20. private byte[] _readBytes2;
  21. private byte[] _serverBytes;
  22. protected override void SetupData()
  23. {
  24. base.SetupData();
  25. var random = new Random();
  26. _path = random.Next().ToString();
  27. _handle = GenerateRandom(5, random);
  28. _bufferSize = (uint)random.Next(1, 1000);
  29. _readBufferSize = 100;
  30. _writeBufferSize = 500;
  31. _readBytes1 = new byte[random.Next(1, (int)_readBufferSize - 10)];
  32. _readBytes2 = new byte[random.Next(1, 3)];
  33. _serverBytes = GenerateRandom(_readBytes1.Length + 10); // store 5 bytes in read buffer
  34. }
  35. protected override void SetupMocks()
  36. {
  37. _ = SftpSessionMock.InSequence(MockSequence)
  38. .Setup(p => p.RequestOpen(_path, Flags.Read, false))
  39. .Returns(_handle);
  40. _ = SftpSessionMock.InSequence(MockSequence)
  41. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  42. .Returns(_readBufferSize);
  43. _ = SftpSessionMock.InSequence(MockSequence)
  44. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  45. .Returns(_writeBufferSize);
  46. _ = SftpSessionMock.InSequence(MockSequence)
  47. .Setup(p => p.IsOpen)
  48. .Returns(true);
  49. _ = SftpSessionMock.InSequence(MockSequence)
  50. .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize))
  51. .Returns(_serverBytes);
  52. _ = SftpSessionMock.InSequence(MockSequence)
  53. .Setup(p => p.IsOpen)
  54. .Returns(true);
  55. _ = SftpSessionMock.InSequence(MockSequence)
  56. .Setup(p => p.IsOpen)
  57. .Returns(true);
  58. }
  59. protected override void Arrange()
  60. {
  61. base.Arrange();
  62. _target = new SftpFileStream(SftpSessionMock.Object,
  63. _path,
  64. FileMode.Open,
  65. FileAccess.Read,
  66. (int)_bufferSize);
  67. _ = _target.Read(_readBytes1, 0, _readBytes1.Length);
  68. _ = _target.Read(_readBytes2, 0, _readBytes2.Length);
  69. }
  70. protected override void Act()
  71. {
  72. _target.Flush();
  73. }
  74. [TestMethod]
  75. public void PositionShouldReturnSameValueAsBeforeFlush()
  76. {
  77. _ = SftpSessionMock.InSequence(MockSequence)
  78. .Setup(p => p.IsOpen)
  79. .Returns(true);
  80. Assert.AreEqual(_readBytes1.Length + _readBytes2.Length, _target.Position);
  81. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
  82. }
  83. [TestMethod]
  84. public void ReadShouldReadFromServer()
  85. {
  86. var serverBytes3 = GenerateRandom(5);
  87. var readBytes3 = new byte[3];
  88. var expectedReadBytes = new ArrayBuilder<byte>().Add(new byte[1])
  89. .Add(serverBytes3.Take(0, 2))
  90. .Build();
  91. _ = SftpSessionMock.InSequence(MockSequence)
  92. .Setup(p => p.IsOpen)
  93. .Returns(true);
  94. _ = SftpSessionMock.InSequence(MockSequence)
  95. .Setup(p => p.RequestRead(_handle, (ulong)(_readBytes1.Length + _readBytes2.Length), _readBufferSize))
  96. .Returns(serverBytes3);
  97. var bytesRead = _target.Read(readBytes3, 1, 2);
  98. Assert.AreEqual(2, bytesRead);
  99. CollectionAssert.AreEqual(expectedReadBytes, readBytes3);
  100. SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)(_readBytes1.Length + _readBytes2.Length), _readBufferSize), Times.Once);
  101. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
  102. }
  103. }
  104. }