SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessWrite_FileExists.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. namespace Renci.SshNet.Tests.Classes.Sftp
  9. {
  10. [TestClass]
  11. public class SftpFileStreamTest_OpenAsync_FileModeCreate_FileAccessWrite_FileExists : SftpFileStreamAsyncTestBase
  12. {
  13. private Random _random;
  14. private string _path;
  15. private FileMode _fileMode;
  16. private FileAccess _fileAccess;
  17. private int _bufferSize;
  18. private uint _readBufferSize;
  19. private uint _writeBufferSize;
  20. private byte[] _handle;
  21. private SftpFileStream _target;
  22. private CancellationToken _cancellationToken;
  23. protected override void SetupData()
  24. {
  25. base.SetupData();
  26. _random = new Random();
  27. _path = _random.Next().ToString();
  28. _fileMode = FileMode.Create;
  29. _fileAccess = FileAccess.Write;
  30. _bufferSize = _random.Next(5, 1000);
  31. _readBufferSize = (uint)_random.Next(5, 1000);
  32. _writeBufferSize = (uint)_random.Next(5, 1000);
  33. _handle = GenerateRandom(_random.Next(1, 10), _random);
  34. _cancellationToken = new CancellationToken();
  35. }
  36. protected override void SetupMocks()
  37. {
  38. _ = SftpSessionMock.InSequence(MockSequence)
  39. .Setup(p => p.RequestOpenAsync(_path, Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate, _cancellationToken))
  40. .ReturnsAsync(_handle);
  41. _ = SftpSessionMock.InSequence(MockSequence)
  42. .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize))
  43. .Returns(_readBufferSize);
  44. _ = SftpSessionMock.InSequence(MockSequence)
  45. .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle))
  46. .Returns(_writeBufferSize);
  47. }
  48. protected override async Task ActAsync()
  49. {
  50. _target = await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, _cancellationToken);
  51. }
  52. [TestMethod]
  53. public void CanReadShouldReturnFalse()
  54. {
  55. Assert.IsFalse(_target.CanRead);
  56. }
  57. [TestMethod]
  58. public void CanSeekShouldReturnTrue()
  59. {
  60. Assert.IsTrue(_target.CanSeek);
  61. }
  62. [TestMethod]
  63. public void CanWriteShouldReturnTrue()
  64. {
  65. Assert.IsTrue(_target.CanWrite);
  66. }
  67. [TestMethod]
  68. public void CanTimeoutShouldReturnTrue()
  69. {
  70. Assert.IsTrue(_target.CanTimeout);
  71. }
  72. [TestMethod]
  73. public void PositionShouldReturnZero()
  74. {
  75. _ = SftpSessionMock.InSequence(MockSequence)
  76. .Setup(p => p.IsOpen)
  77. .Returns(true);
  78. var actual = _target.Position;
  79. Assert.AreEqual(0L, actual);
  80. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  81. }
  82. [TestMethod]
  83. public async Task ReadShouldThrowNotSupportedException()
  84. {
  85. var buffer = new byte[_readBufferSize];
  86. _ = SftpSessionMock.InSequence(MockSequence)
  87. .Setup(p => p.IsOpen)
  88. .Returns(true);
  89. try
  90. {
  91. _ = await _target.ReadAsync(buffer, 0, buffer.Length);
  92. Assert.Fail();
  93. }
  94. catch (NotSupportedException ex)
  95. {
  96. Assert.IsNull(ex.InnerException);
  97. }
  98. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  99. }
  100. [TestMethod]
  101. public async Task WriteShouldStartWritingAtBeginningOfFile()
  102. {
  103. var buffer = new byte[_writeBufferSize];
  104. _ = SftpSessionMock.InSequence(MockSequence)
  105. .Setup(p => p.IsOpen)
  106. .Returns(true);
  107. _ = SftpSessionMock.InSequence(MockSequence)
  108. .Setup(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken))
  109. .Returns(Task.CompletedTask);
  110. await _target.WriteAsync(buffer, 0, buffer.Length);
  111. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  112. SftpSessionMock.Verify(p => p.RequestWriteAsync(_handle, 0UL, buffer, 0, buffer.Length, _cancellationToken), Times.Once);
  113. }
  114. [TestMethod]
  115. public void RequestOpenOnSftpSessionShouldBeInvokedOnce()
  116. {
  117. SftpSessionMock.Verify(p => p.RequestOpenAsync(_path, Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate, _cancellationToken), Times.Once);
  118. }
  119. }
  120. }