SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginBeginAndOffsetNegative.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.IO;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Sftp;
  6. namespace Renci.SshNet.Tests.Classes.Sftp
  7. {
  8. [TestClass]
  9. public class SftpFileStreamTest_Seek_PositionedAtBeginningOfStream_OriginBeginAndOffsetNegative : SftpFileStreamTestBase
  10. {
  11. private Random _random;
  12. private string _path;
  13. private FileMode _fileMode;
  14. private FileAccess _fileAccess;
  15. private int _bufferSize;
  16. private uint _readBufferSize;
  17. private uint _writeBufferSize;
  18. private byte[] _handle;
  19. private SftpFileStream _target;
  20. private int _offset;
  21. private EndOfStreamException _actualException;
  22. protected override void SetupData()
  23. {
  24. base.SetupData();
  25. _random = new Random();
  26. _path = _random.Next().ToString();
  27. _fileMode = FileMode.OpenOrCreate;
  28. _fileAccess = FileAccess.Read;
  29. _bufferSize = _random.Next(5, 1000);
  30. _readBufferSize = (uint)_random.Next(5, 1000);
  31. _writeBufferSize = (uint)_random.Next(5, 1000);
  32. _handle = GenerateRandom(_random.Next(1, 10), _random);
  33. _offset = _random.Next(int.MinValue, -1);
  34. }
  35. protected override void SetupMocks()
  36. {
  37. SftpSessionMock.InSequence(MockSequence)
  38. .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.CreateNewOrOpen, false))
  39. .Returns(_handle);
  40. SftpSessionMock.InSequence(MockSequence)
  41. .Setup(p => p.CalculateOptimalReadLength((uint)_bufferSize))
  42. .Returns(_readBufferSize);
  43. SftpSessionMock.InSequence(MockSequence)
  44. .Setup(p => p.CalculateOptimalWriteLength((uint)_bufferSize, _handle))
  45. .Returns(_writeBufferSize);
  46. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  47. }
  48. protected override void Arrange()
  49. {
  50. base.Arrange();
  51. _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
  52. }
  53. protected override void Act()
  54. {
  55. try
  56. {
  57. _target.Seek(_offset, SeekOrigin.Begin);
  58. Assert.Fail();
  59. }
  60. catch (EndOfStreamException ex)
  61. {
  62. _actualException = ex;
  63. }
  64. }
  65. [TestMethod]
  66. public void SeekShouldHaveThrownEndOfStreamException()
  67. {
  68. Assert.IsNotNull(_actualException);
  69. Assert.IsNull(_actualException.InnerException);
  70. Assert.AreEqual("Attempted to read past the end of the stream.", _actualException.Message);
  71. }
  72. [TestMethod]
  73. public void IsOpenOnSftpSessionShouldHaveBeenInvokedOnce()
  74. {
  75. SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
  76. }
  77. [TestMethod]
  78. public void PositionShouldReturnZero()
  79. {
  80. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  81. Assert.AreEqual(0L, _target.Position);
  82. SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2));
  83. }
  84. }
  85. }