SftpFileStreamTest_CanWrite_Disposed_FileAccessWrite.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_CanWrite_Disposed_FileAccessWrite
  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 bool _actual;
  21. [TestInitialize]
  22. public void Setup()
  23. {
  24. Arrange();
  25. Act();
  26. }
  27. protected void Arrange()
  28. {
  29. var random = new Random();
  30. _path = random.Next().ToString(CultureInfo.InvariantCulture);
  31. _handle = new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) };
  32. _fileAttributes = SftpFileAttributes.Empty;
  33. _bufferSize = (uint)random.Next(0, 1000);
  34. _readBufferSize = (uint)random.Next(0, 1000);
  35. _writeBufferSize = (uint)random.Next(0, 1000);
  36. _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
  37. var sequence = new MockSequence();
  38. _sftpSessionMock.InSequence(sequence)
  39. .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true))
  40. .Returns(_handle);
  41. _sftpSessionMock.InSequence(sequence).Setup(p => p.RequestFStat(_handle, false)).Returns(_fileAttributes);
  42. _sftpSessionMock.InSequence(sequence)
  43. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  44. .Returns(_readBufferSize);
  45. _sftpSessionMock.InSequence(sequence)
  46. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  47. .Returns(_writeBufferSize);
  48. _sftpSessionMock.InSequence(sequence)
  49. .Setup(p => p.IsOpen)
  50. .Returns(true);
  51. _sftpSessionMock.InSequence(sequence)
  52. .Setup(p => p.RequestClose(_handle));
  53. _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize);
  54. _sftpFileStream.Dispose();
  55. }
  56. protected void Act()
  57. {
  58. _actual = _sftpFileStream.CanWrite;
  59. }
  60. [TestMethod]
  61. public void CanWriteShouldReturnFalse()
  62. {
  63. Assert.IsFalse(_actual);
  64. }
  65. }
  66. }