SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessWrite.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Moq;
  7. using Renci.SshNet.Sftp;
  8. using Renci.SshNet.Tests.Common;
  9. namespace Renci.SshNet.Tests.Classes.Sftp
  10. {
  11. [TestClass]
  12. public class SftpFileStreamTest_OpenAsync_FileModeAppend_FileAccessWrite : SftpFileStreamAsyncTestBase
  13. {
  14. private Random _random;
  15. private string _path;
  16. private FileMode _fileMode;
  17. private FileAccess _fileAccess;
  18. private int _bufferSize;
  19. private uint _readBufferSize;
  20. private uint _writeBufferSize;
  21. private byte[] _handle;
  22. private SftpFileStream _target;
  23. private SftpFileAttributes _fileAttributes;
  24. private CancellationToken _cancellationToken;
  25. protected override void SetupData()
  26. {
  27. base.SetupData();
  28. _random = new Random();
  29. _path = _random.Next().ToString();
  30. _fileMode = FileMode.Append;
  31. _fileAccess = FileAccess.Write;
  32. _bufferSize = _random.Next(5, 1000);
  33. _readBufferSize = (uint)_random.Next(5, 1000);
  34. _writeBufferSize = (uint)_random.Next(5, 1000);
  35. _handle = GenerateRandom(_random.Next(1, 10), _random);
  36. _fileAttributes = new SftpFileAttributesBuilder().WithLastAccessTime(DateTime.UtcNow.AddSeconds(_random.Next()))
  37. .WithLastWriteTime(DateTime.UtcNow.AddSeconds(_random.Next()))
  38. .WithSize(_random.Next())
  39. .WithUserId(_random.Next())
  40. .WithGroupId(_random.Next())
  41. .WithPermissions((uint)_random.Next())
  42. .Build();
  43. _cancellationToken = new CancellationToken();
  44. }
  45. protected override void SetupMocks()
  46. {
  47. _ = SftpSessionMock.InSequence(MockSequence)
  48. .Setup(p => p.RequestOpenAsync(_path, Flags.Write | Flags.Append | Flags.CreateNewOrOpen, _cancellationToken))
  49. .ReturnsAsync(_handle);
  50. _ = SftpSessionMock.InSequence(MockSequence)
  51. .Setup(p => p.RequestFStatAsync(_handle, _cancellationToken))
  52. .ReturnsAsync(_fileAttributes);
  53. _ = SftpSessionMock.InSequence(MockSequence)
  54. .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize))
  55. .Returns(_readBufferSize);
  56. _ = SftpSessionMock.InSequence(MockSequence)
  57. .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle))
  58. .Returns(_writeBufferSize);
  59. }
  60. protected override async Task ActAsync()
  61. {
  62. _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken);
  63. }
  64. [TestMethod]
  65. public void CanReadShouldReturnFalse()
  66. {
  67. Assert.IsFalse(_target.CanRead);
  68. }
  69. [TestMethod]
  70. public void CanSeekShouldReturnTrue()
  71. {
  72. Assert.IsTrue(_target.CanSeek);
  73. }
  74. [TestMethod]
  75. public void CanWriteShouldReturnTrue()
  76. {
  77. Assert.IsTrue(_target.CanWrite);
  78. }
  79. [TestMethod]
  80. public void CanTimeoutShouldReturnTrue()
  81. {
  82. Assert.IsTrue(_target.CanTimeout);
  83. }
  84. [TestMethod]
  85. public void PositionShouldReturnSizeOfFile()
  86. {
  87. _ = SftpSessionMock.InSequence(MockSequence)
  88. .Setup(p => p.IsOpen)
  89. .Returns(true);
  90. var actual = _target.Position;
  91. Assert.AreEqual(_fileAttributes.Size, actual);
  92. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  93. }
  94. [TestMethod]
  95. public async Task ReadShouldThrowNotSupportedException()
  96. {
  97. var buffer = new byte[_readBufferSize];
  98. _ = SftpSessionMock.InSequence(MockSequence)
  99. .Setup(p => p.IsOpen)
  100. .Returns(true);
  101. try
  102. {
  103. _ = await _target.ReadAsync(buffer, 0, buffer.Length, _cancellationToken);
  104. Assert.Fail();
  105. }
  106. catch (NotSupportedException ex)
  107. {
  108. Assert.IsNull(ex.InnerException);
  109. }
  110. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  111. }
  112. [TestMethod]
  113. public async Task WriteShouldStartWritingAtEndOfFile()
  114. {
  115. var buffer = new byte[_writeBufferSize];
  116. _ = SftpSessionMock.InSequence(MockSequence)
  117. .Setup(p => p.IsOpen)
  118. .Returns(true);
  119. _ = SftpSessionMock.InSequence(MockSequence)
  120. .Setup(p => p.RequestWriteAsync(_handle, (ulong)_fileAttributes.Size, buffer, 0, buffer.Length, _cancellationToken))
  121. .Returns(Task.CompletedTask);
  122. await _target.WriteAsync(buffer, 0, buffer.Length, _cancellationToken);
  123. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  124. SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, (ulong)_fileAttributes.Size, buffer, 0, buffer.Length, _cancellationToken), Times.Once);
  125. }
  126. [TestMethod]
  127. public void RequestOpenOnSftpSessionShouldBeInvokedOnce()
  128. {
  129. SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Write | Flags.Append | Flags.CreateNewOrOpen, default), Times.Once);
  130. }
  131. [TestMethod]
  132. public void RequestFStatOnSftpSessionShouldBeInvokedOnce()
  133. {
  134. SftpSessionMock.Verify(p => p.RequestFStatAsync(_handle, default), Times.Once);
  135. }
  136. }
  137. }