SftpFileStreamTest_Ctor_FileModeAppend_FileAccessWrite.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.Now.AddSeconds(_random.Next()))
  35. .WithLastWriteTime(DateTime.Now.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, 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).Setup(p => p.IsOpen).Returns(true);
  89. var actual = _target.Position;
  90. Assert.AreEqual(_fileAttributes.Size, actual);
  91. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  92. }
  93. [TestMethod]
  94. public void ReadShouldThrowNotSupportedException()
  95. {
  96. var buffer = new byte[_readBufferSize];
  97. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  98. try
  99. {
  100. _target.Read(buffer, 0, buffer.Length);
  101. Assert.Fail();
  102. }
  103. catch (NotSupportedException ex)
  104. {
  105. Assert.IsNull(ex.InnerException);
  106. Assert.AreEqual("Read not supported.", ex.Message);
  107. }
  108. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  109. }
  110. [TestMethod]
  111. public void ReadByteShouldThrowNotSupportedException()
  112. {
  113. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  114. try
  115. {
  116. _target.ReadByte();
  117. Assert.Fail();
  118. }
  119. catch (NotSupportedException ex)
  120. {
  121. Assert.IsNull(ex.InnerException);
  122. Assert.AreEqual("Read not supported.", ex.Message);
  123. }
  124. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  125. }
  126. [TestMethod]
  127. public void WriteShouldStartWritingAtEndOfFile()
  128. {
  129. var buffer = new byte[_writeBufferSize];
  130. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  131. SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWrite(_handle, (ulong) _fileAttributes.Size, buffer, 0, buffer.Length, It.IsNotNull<AutoResetEvent>(), null));
  132. _target.Write(buffer, 0, buffer.Length);
  133. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  134. SftpSessionMock.Verify(p => p.RequestWrite(_handle, (ulong)_fileAttributes.Size, buffer, 0, buffer.Length, It.IsNotNull<AutoResetEvent>(), null), Times.Once);
  135. }
  136. [TestMethod]
  137. public void RequestOpenOnSftpSessionShouldBeInvokedOnce()
  138. {
  139. SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.Append, false), Times.Once);
  140. }
  141. [TestMethod]
  142. public void RequestFStatOnSftpSessionShouldBeInvokedOnce()
  143. {
  144. SftpSessionMock.Verify(p => p.RequestFStat(_handle, false), Times.Once);
  145. }
  146. }
  147. }