SftpFileStreamTest_Ctor_FileModeAppend_FileAccessWrite.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Moq;
  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_Ctor_FileModeAppend_FileAccessWrite : SftpFileStreamTestBase
  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 SftpFileAttributes _fileAttributes;
  23. protected override void SetupData()
  24. {
  25. base.SetupData();
  26. _random = new Random();
  27. _path = _random.Next().ToString();
  28. _fileMode = FileMode.Append;
  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. _fileAttributes = new SftpFileAttributesBuilder().WithLastAccessTime(DateTime.UtcNow.AddSeconds(_random.Next()))
  35. .WithLastWriteTime(DateTime.UtcNow.AddSeconds(_random.Next()))
  36. .WithSize(_random.Next())
  37. .WithUserId(_random.Next())
  38. .WithGroupId(_random.Next())
  39. .WithPermissions((uint)_random.Next())
  40. .Build();
  41. }
  42. protected override void SetupMocks()
  43. {
  44. _ = SftpSessionMock.InSequence(MockSequence)
  45. .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Append | Flags.CreateNewOrOpen, false))
  46. .Returns(_handle);
  47. _ = SftpSessionMock.InSequence(MockSequence)
  48. .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize))
  49. .Returns(_readBufferSize);
  50. _ = SftpSessionMock.InSequence(MockSequence)
  51. .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle))
  52. .Returns(_writeBufferSize);
  53. _ = SftpSessionMock.InSequence(MockSequence)
  54. .Setup(p => p.RequestFStat(_handle, false))
  55. .Returns(_fileAttributes);
  56. }
  57. protected override void Act()
  58. {
  59. _target = new SftpFileStream(SftpSessionMock.Object,
  60. _path,
  61. _fileMode,
  62. _fileAccess,
  63. _bufferSize);
  64. }
  65. [TestMethod]
  66. public void CanReadShouldReturnFalse()
  67. {
  68. Assert.IsFalse(_target.CanRead);
  69. }
  70. [TestMethod]
  71. public void CanSeekShouldReturnTrue()
  72. {
  73. Assert.IsTrue(_target.CanSeek);
  74. }
  75. [TestMethod]
  76. public void CanWriteShouldReturnTrue()
  77. {
  78. Assert.IsTrue(_target.CanWrite);
  79. }
  80. [TestMethod]
  81. public void CanTimeoutShouldReturnTrue()
  82. {
  83. Assert.IsTrue(_target.CanTimeout);
  84. }
  85. [TestMethod]
  86. public void PositionShouldReturnSizeOfFile()
  87. {
  88. _ = SftpSessionMock.InSequence(MockSequence)
  89. .Setup(p => p.IsOpen)
  90. .Returns(true);
  91. var actual = _target.Position;
  92. Assert.AreEqual(_fileAttributes.Size, actual);
  93. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  94. }
  95. [TestMethod]
  96. public void ReadShouldThrowNotSupportedException()
  97. {
  98. var buffer = new byte[_readBufferSize];
  99. _ = SftpSessionMock.InSequence(MockSequence)
  100. .Setup(p => p.IsOpen)
  101. .Returns(true);
  102. try
  103. {
  104. _ = _target.Read(buffer, 0, buffer.Length);
  105. Assert.Fail();
  106. }
  107. catch (NotSupportedException ex)
  108. {
  109. Assert.IsNull(ex.InnerException);
  110. Assert.AreEqual("Read not supported.", ex.Message);
  111. }
  112. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  113. }
  114. [TestMethod]
  115. public void ReadByteShouldThrowNotSupportedException()
  116. {
  117. _ = SftpSessionMock.InSequence(MockSequence)
  118. .Setup(p => p.IsOpen)
  119. .Returns(true);
  120. try
  121. {
  122. _ = _target.ReadByte();
  123. Assert.Fail();
  124. }
  125. catch (NotSupportedException ex)
  126. {
  127. Assert.IsNull(ex.InnerException);
  128. Assert.AreEqual("Read not supported.", ex.Message);
  129. }
  130. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  131. }
  132. [TestMethod]
  133. public void WriteShouldStartWritingAtEndOfFile()
  134. {
  135. var buffer = new byte[_writeBufferSize];
  136. _ = SftpSessionMock.InSequence(MockSequence)
  137. .Setup(p => p.IsOpen)
  138. .Returns(true);
  139. _ = SftpSessionMock.InSequence(MockSequence)
  140. .Setup(p => p.RequestWrite(_handle, (ulong)_fileAttributes.Size, buffer, 0, buffer.Length, It.IsNotNull<AutoResetEvent>(), null));
  141. _target.Write(buffer, 0, buffer.Length);
  142. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  143. SftpSessionMock.Verify(p => p.RequestWrite(_handle, (ulong)_fileAttributes.Size, buffer, 0, buffer.Length, It.IsNotNull<AutoResetEvent>(), null), Times.Once);
  144. }
  145. [TestMethod]
  146. public void RequestOpenOnSftpSessionShouldBeInvokedOnce()
  147. {
  148. SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.Append | Flags.CreateNewOrOpen, false), Times.Once);
  149. }
  150. [TestMethod]
  151. public void RequestFStatOnSftpSessionShouldBeInvokedOnce()
  152. {
  153. SftpSessionMock.Verify(p => p.RequestFStat(_handle, false), Times.Once);
  154. }
  155. }
  156. }