SftpFileStreamTest_Flush_ReadMode_NoDataInBuffer.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_NoDataInBuffer : 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[] _readBytes;
  20. private byte[] _serverBytes;
  21. protected override void SetupData()
  22. {
  23. base.SetupData();
  24. var random = new Random();
  25. _path = random.Next().ToString();
  26. _handle = GenerateRandom(5, random);
  27. _bufferSize = (uint)random.Next(1, 1000);
  28. _readBufferSize = 100;
  29. _writeBufferSize = 500;
  30. _readBytes = new byte[random.Next(1, (int)_readBufferSize)];
  31. _serverBytes = GenerateRandom(_readBytes.Length);
  32. }
  33. protected override void SetupMocks()
  34. {
  35. _ = SftpSessionMock.InSequence(MockSequence)
  36. .Setup(p => p.RequestOpen(_path, Flags.Read, false))
  37. .Returns(_handle);
  38. _ = SftpSessionMock.InSequence(MockSequence)
  39. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  40. .Returns(_readBufferSize);
  41. _ = SftpSessionMock.InSequence(MockSequence)
  42. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  43. .Returns(_writeBufferSize);
  44. _ = SftpSessionMock.InSequence(MockSequence)
  45. .Setup(p => p.IsOpen)
  46. .Returns(true);
  47. _ = SftpSessionMock.InSequence(MockSequence)
  48. .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize))
  49. .Returns(_serverBytes);
  50. _ = SftpSessionMock.InSequence(MockSequence)
  51. .Setup(p => p.IsOpen)
  52. .Returns(true);
  53. }
  54. protected override void Arrange()
  55. {
  56. base.Arrange();
  57. _target = new SftpFileStream(SftpSessionMock.Object,
  58. _path,
  59. FileMode.Open,
  60. FileAccess.Read,
  61. (int)_bufferSize);
  62. _ = _target.Read(_readBytes, 0, _readBytes.Length);
  63. }
  64. protected override void Act()
  65. {
  66. _target.Flush();
  67. }
  68. [TestMethod]
  69. public void PositionShouldReturnSameValueAsBeforeFlush()
  70. {
  71. _ = SftpSessionMock.InSequence(MockSequence)
  72. .Setup(p => p.IsOpen)
  73. .Returns(true);
  74. Assert.AreEqual(_readBytes.Length, _target.Position);
  75. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
  76. }
  77. [TestMethod]
  78. public void ReadShouldReadFromServer()
  79. {
  80. var serverBytes2 = GenerateRandom(5);
  81. var readBytes2 = new byte[5];
  82. var expectedReadBytes = new ArrayBuilder<byte>().Add(new byte[2])
  83. .Add(serverBytes2.Take(0, 3))
  84. .Build();
  85. _ = SftpSessionMock.InSequence(MockSequence)
  86. .Setup(p => p.IsOpen)
  87. .Returns(true);
  88. _ = SftpSessionMock.InSequence(MockSequence)
  89. .Setup(p => p.RequestRead(_handle, (ulong)_readBytes.Length, _readBufferSize))
  90. .Returns(serverBytes2);
  91. var bytesRead = _target.Read(readBytes2, 2, 3);
  92. Assert.AreEqual(3, bytesRead);
  93. CollectionAssert.AreEqual(expectedReadBytes, readBytes2);
  94. SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)_readBytes.Length, _readBufferSize), Times.Once);
  95. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
  96. }
  97. }
  98. }