SftpFileStreamTest_Flush_WriteMode_NoDataInBuffer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_NoDataInBuffer : 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[] _writeBytes;
  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. _writeBytes = GenerateRandom(_writeBufferSize);
  32. }
  33. protected override void SetupMocks()
  34. {
  35. SftpSessionMock.InSequence(MockSequence)
  36. .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Write, 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.RequestWrite(_handle, 0UL, It.IsAny<byte[]>(), 0, _writeBytes.Length, It.IsAny<AutoResetEvent>(), null))
  49. .Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverOffset, data, offset, length, wait, writeCompleted)
  50. =>
  51. {
  52. wait.Set();
  53. });
  54. SftpSessionMock.InSequence(MockSequence)
  55. .Setup(p => p.IsOpen)
  56. .Returns(true);
  57. }
  58. protected override void Arrange()
  59. {
  60. base.Arrange();
  61. _target = new SftpFileStream(SftpSessionMock.Object,
  62. _path,
  63. FileMode.Open,
  64. FileAccess.ReadWrite,
  65. (int)_bufferSize);
  66. _target.Write(_writeBytes, 0, _writeBytes.Length);
  67. }
  68. protected override void Act()
  69. {
  70. _target.Flush();
  71. }
  72. [TestMethod]
  73. public void PositionShouldReturnSameValueAsBeforeFlush()
  74. {
  75. SftpSessionMock.InSequence(MockSequence)
  76. .Setup(p => p.IsOpen)
  77. .Returns(true);
  78. Assert.AreEqual(_writeBytes.Length, _target.Position);
  79. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
  80. }
  81. [TestMethod]
  82. public void ReadShouldReadFromServer()
  83. {
  84. var serverBytes = GenerateRandom(5);
  85. var readBytes = new byte[5];
  86. var expectedReadBytes = new ArrayBuilder<byte>().Add(new byte[2])
  87. .Add(serverBytes.Take(0, 3))
  88. .Build();
  89. SftpSessionMock.InSequence(MockSequence)
  90. .Setup(p => p.IsOpen)
  91. .Returns(true);
  92. SftpSessionMock.InSequence(MockSequence)
  93. .Setup(p => p.RequestRead(_handle, (ulong)_writeBytes.Length, _readBufferSize))
  94. .Returns(serverBytes);
  95. var bytesRead = _target.Read(readBytes, 2, 3);
  96. Assert.AreEqual(3, bytesRead);
  97. CollectionAssert.AreEqual(expectedReadBytes, readBytes);
  98. SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)_writeBytes.Length, _readBufferSize), Times.Once);
  99. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
  100. }
  101. }
  102. }