SftpFileStreamTest_SetLength_SessionOpen_FIleAccessRead.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  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_SetLength_SessionOpen_FIleAccess
  11. {
  12. private Mock<ISftpSession> _sftpSessionMock;
  13. private string _path;
  14. private SftpFileStream _sftpFileStream;
  15. private byte[] _handle;
  16. private SftpFileAttributes _fileAttributes;
  17. private uint _bufferSize;
  18. private uint _readBufferSize;
  19. private uint _writeBufferSize;
  20. private MockSequence _sequence;
  21. private long _length;
  22. private NotSupportedException _actualException;
  23. [TestInitialize]
  24. public void Setup()
  25. {
  26. Arrange();
  27. Act();
  28. }
  29. protected void Arrange()
  30. {
  31. var random = new Random();
  32. _path = random.Next().ToString(CultureInfo.InvariantCulture);
  33. _handle = new[] {(byte) random.Next(byte.MinValue, byte.MaxValue)};
  34. _fileAttributes = SftpFileAttributes.Empty;
  35. _bufferSize = (uint) random.Next(1, 1000);
  36. _readBufferSize = (uint) random.Next(0, 1000);
  37. _writeBufferSize = (uint) random.Next(0, 1000);
  38. _length = random.Next();
  39. _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
  40. _sequence = new MockSequence();
  41. _sftpSessionMock.InSequence(_sequence)
  42. .Setup(p => p.RequestOpen(_path, Flags.Read | Flags.Truncate, true))
  43. .Returns(_handle);
  44. _sftpSessionMock.InSequence(_sequence).Setup(p => p.RequestFStat(_handle)).Returns(_fileAttributes);
  45. _sftpSessionMock.InSequence(_sequence)
  46. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  47. .Returns(_readBufferSize);
  48. _sftpSessionMock.InSequence(_sequence)
  49. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  50. .Returns(_writeBufferSize);
  51. _sftpSessionMock.InSequence(_sequence)
  52. .Setup(p => p.IsOpen)
  53. .Returns(true);
  54. _sftpFileStream = new SftpFileStream(
  55. _sftpSessionMock.Object,
  56. _path,
  57. FileMode.Create,
  58. FileAccess.Read,
  59. (int) _bufferSize);
  60. }
  61. protected void Act()
  62. {
  63. try
  64. {
  65. _sftpFileStream.SetLength(_length);
  66. Assert.Fail();
  67. }
  68. catch (NotSupportedException ex)
  69. {
  70. _actualException = ex;
  71. }
  72. }
  73. [TestMethod]
  74. public void SetLengthShouldHaveThrownNotSupportedException()
  75. {
  76. Assert.IsNotNull(_actualException);
  77. Assert.IsNull(_actualException.InnerException);
  78. Assert.AreEqual("Write not supported.", _actualException.Message);
  79. }
  80. }
  81. }