SftpFileStreamTest_OpenAsync_FileModeOpen_FileAccessWrite.cs 4.5 KB

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