SftpFileStreamTest_Ctor_FileModeOpen_FileAccessWrite.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. namespace Renci.SshNet.Tests.Classes.Sftp
  8. {
  9. [TestClass]
  10. public class SftpFileStreamTest_Ctor_FileModeOpen_FileAccessWrite : SftpFileStreamTestBase
  11. {
  12. private Random _random;
  13. private string _path;
  14. private FileMode _fileMode;
  15. private FileAccess _fileAccess;
  16. private int _bufferSize;
  17. private uint _readBufferSize;
  18. private uint _writeBufferSize;
  19. private byte[] _handle;
  20. private SftpFileStream _target;
  21. protected override void SetupData()
  22. {
  23. base.SetupData();
  24. _random = new Random();
  25. _path = _random.Next().ToString();
  26. _fileMode = FileMode.Open;
  27. _fileAccess = FileAccess.Write;
  28. _bufferSize = _random.Next(5, 1000);
  29. _readBufferSize = (uint)_random.Next(5, 1000);
  30. _writeBufferSize = (uint)_random.Next(5, 1000);
  31. _handle = GenerateRandom(_random.Next(1, 10), _random);
  32. }
  33. protected override void SetupMocks()
  34. {
  35. SftpSessionMock.InSequence(MockSequence)
  36. .Setup(p => p.RequestOpen(_path, Flags.Write, false))
  37. .Returns(_handle);
  38. SftpSessionMock.InSequence(MockSequence)
  39. .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize))
  40. .Returns(_readBufferSize);
  41. SftpSessionMock.InSequence(MockSequence)
  42. .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle))
  43. .Returns(_writeBufferSize);
  44. }
  45. protected override void Act()
  46. {
  47. _target = new SftpFileStream(SftpSessionMock.Object,
  48. _path,
  49. _fileMode,
  50. _fileAccess,
  51. _bufferSize);
  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 void ReadShouldThrowNotSupportedException()
  83. {
  84. var buffer = new byte[_readBufferSize];
  85. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  86. try
  87. {
  88. _ = _target.Read(buffer, 0, buffer.Length);
  89. Assert.Fail();
  90. }
  91. catch (NotSupportedException ex)
  92. {
  93. Assert.IsNull(ex.InnerException);
  94. Assert.AreEqual("Read not supported.", ex.Message);
  95. }
  96. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  97. }
  98. [TestMethod]
  99. public void ReadByteShouldThrowNotSupportedException()
  100. {
  101. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  102. try
  103. {
  104. _target.ReadByte();
  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 WriteShouldStartWritingAtBeginningOfFile()
  116. {
  117. var buffer = new byte[_writeBufferSize];
  118. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  119. SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWrite(_handle, 0UL, buffer, 0, buffer.Length, It.IsNotNull<AutoResetEvent>(), null));
  120. _target.Write(buffer, 0, buffer.Length);
  121. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  122. SftpSessionMock.Verify(p => p.RequestWrite(_handle, 0UL, buffer, 0, buffer.Length, It.IsNotNull<AutoResetEvent>(), null), Times.Once);
  123. }
  124. [TestMethod]
  125. public void RequestOpenOnSftpSessionShouldBeInvokedOnce()
  126. {
  127. SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write, false), Times.Once);
  128. }
  129. }
  130. }