SftpFileStreamTest_Ctor_FileModeCreate_FileAccessWrite_FileDoesNotExist.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_FileModeCreate_FileAccessWrite_FileDoesNotExist : 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.Create;
  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 | Flags.Truncate, true))
  37. .Returns((byte[])null);
  38. SftpSessionMock.InSequence(MockSequence)
  39. .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNew, false))
  40. .Returns(_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 void Act()
  49. {
  50. _target = new SftpFileStream(SftpSessionMock.Object,
  51. _path,
  52. _fileMode,
  53. _fileAccess,
  54. _bufferSize);
  55. }
  56. [TestMethod]
  57. public void CanReadShouldReturnFalse()
  58. {
  59. Assert.IsFalse(_target.CanRead);
  60. }
  61. [TestMethod]
  62. public void CanSeekShouldReturnTrue()
  63. {
  64. Assert.IsTrue(_target.CanSeek);
  65. }
  66. [TestMethod]
  67. public void CanWriteShouldReturnTrue()
  68. {
  69. Assert.IsTrue(_target.CanWrite);
  70. }
  71. [TestMethod]
  72. public void CanTimeoutShouldReturnTrue()
  73. {
  74. Assert.IsTrue(_target.CanTimeout);
  75. }
  76. [TestMethod]
  77. public void PositionShouldReturnZero()
  78. {
  79. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  80. var actual = _target.Position;
  81. Assert.AreEqual(0L, actual);
  82. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  83. }
  84. [TestMethod]
  85. public void ReadShouldThrowNotSupportedException()
  86. {
  87. var buffer = new byte[_readBufferSize];
  88. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  89. try
  90. {
  91. _ = _target.Read(buffer, 0, buffer.Length);
  92. Assert.Fail();
  93. }
  94. catch (NotSupportedException ex)
  95. {
  96. Assert.IsNull(ex.InnerException);
  97. Assert.AreEqual("Read not supported.", ex.Message);
  98. }
  99. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  100. }
  101. [TestMethod]
  102. public void ReadByteShouldThrowNotSupportedException()
  103. {
  104. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  105. try
  106. {
  107. _target.ReadByte();
  108. Assert.Fail();
  109. }
  110. catch (NotSupportedException ex)
  111. {
  112. Assert.IsNull(ex.InnerException);
  113. Assert.AreEqual("Read not supported.", ex.Message);
  114. }
  115. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  116. }
  117. [TestMethod]
  118. public void WriteShouldStartWritingAtBeginningOfFile()
  119. {
  120. var buffer = new byte[_writeBufferSize];
  121. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  122. SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestWrite(_handle, 0UL, buffer, 0, buffer.Length, It.IsNotNull<AutoResetEvent>(), null));
  123. _target.Write(buffer, 0, buffer.Length);
  124. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  125. SftpSessionMock.Verify(p => p.RequestWrite(_handle, 0UL, buffer, 0, buffer.Length, It.IsNotNull<AutoResetEvent>(), null), Times.Once);
  126. }
  127. [TestMethod]
  128. public void RequestOpenOnSftpSessionShouldBeInvokedOnceWithTruncateAndOnceWithCreateNew()
  129. {
  130. SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true), Times.Once);
  131. SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNew, false), Times.Once);
  132. }
  133. }
  134. }