SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessRead.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.IO;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Sftp;
  7. namespace Renci.SshNet.Tests.Classes.Sftp
  8. {
  9. [TestClass]
  10. public class SftpFileStreamTest_Ctor_FileModeOpenOrCreate_FileAccessRead : 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.OpenOrCreate;
  27. _fileAccess = FileAccess.Read;
  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.Read | Flags.CreateNewOrOpen, 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 CanReadShouldReturnTrue()
  55. {
  56. Assert.IsTrue(_target.CanRead);
  57. }
  58. [TestMethod]
  59. public void CanSeekShouldReturnTrue()
  60. {
  61. Assert.IsTrue(_target.CanSeek);
  62. }
  63. [TestMethod]
  64. public void CanWriteShouldReturnFalse()
  65. {
  66. Assert.IsFalse(_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 ReadShouldStartReadingAtBeginningOfFile()
  83. {
  84. var buffer = new byte[8];
  85. var data = new byte[] { 5, 4, 3, 2, 1 };
  86. var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 };
  87. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  88. SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)).Returns(data);
  89. var actual = _target.Read(buffer, 1, data.Length);
  90. Assert.AreEqual(data.Length, actual);
  91. Assert.IsTrue(buffer.IsEqualTo(expected));
  92. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  93. SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once);
  94. }
  95. [TestMethod]
  96. public void ReadByteShouldStartReadingAtBeginningOfFile()
  97. {
  98. var data = GenerateRandom(5, _random);
  99. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  100. SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize))
  101. .Returns(data);
  102. var actual = _target.ReadByte();
  103. Assert.AreEqual(data[0], actual);
  104. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  105. SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once);
  106. }
  107. [TestMethod]
  108. public void WriteShouldThrowNotSupportedException()
  109. {
  110. var buffer = new byte[_writeBufferSize];
  111. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  112. try
  113. {
  114. _target.Write(buffer, 0, buffer.Length);
  115. }
  116. catch (NotSupportedException ex)
  117. {
  118. Assert.IsNull(ex.InnerException);
  119. Assert.AreEqual("Write not supported.", ex.Message);
  120. }
  121. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  122. }
  123. [TestMethod]
  124. public void WriteByteShouldThrowNotSupportedException()
  125. {
  126. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  127. try
  128. {
  129. _target.WriteByte(0x0a);
  130. }
  131. catch (NotSupportedException ex)
  132. {
  133. Assert.IsNull(ex.InnerException);
  134. Assert.AreEqual("Write not supported.", ex.Message);
  135. }
  136. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
  137. }
  138. [TestMethod]
  139. public void RequestOpenOnSftpSessionShouldBeInvokedOnce()
  140. {
  141. SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Read | Flags.CreateNewOrOpen, false), Times.Once);
  142. }
  143. }
  144. }