2
0

SftpFileStreamTest_SetLength_SessionNotOpen.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_SessionNotOpen
  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 ObjectDisposedException _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(false);
  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 (ObjectDisposedException ex)
  69. {
  70. _actualException = ex;
  71. }
  72. }
  73. [TestMethod]
  74. public void SetLengthShouldHaveThrownObjectDisposedException()
  75. {
  76. Assert.IsNotNull(_actualException);
  77. Assert.IsNull(_actualException.InnerException);
  78. Assert.AreEqual(
  79. string.Format(
  80. "Cannot access a closed SFTP session.{0}Object name: '{1}'.",
  81. Environment.NewLine,
  82. _actualException.ObjectName),
  83. _actualException.Message);
  84. Assert.AreEqual(typeof(SftpFileStream).FullName, _actualException.ObjectName);
  85. }
  86. }
  87. }