2
0

SftpFileStreamTest_SetLength_SessionNotOpen.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_SetLength_SessionNotOpen : SftpFileStreamTestBase
  10. {
  11. private SftpFileStream _target;
  12. private string _path;
  13. private byte[] _handle;
  14. private uint _bufferSize;
  15. private uint _readBufferSize;
  16. private uint _writeBufferSize;
  17. private long _length;
  18. private ObjectDisposedException _actualException;
  19. protected override void SetupData()
  20. {
  21. base.SetupData();
  22. var random = new Random();
  23. _path = random.Next().ToString();
  24. _handle = GenerateRandom(4, random);
  25. _bufferSize = (uint)random.Next(1, 1000);
  26. _readBufferSize = (uint)random.Next(1, 1000);
  27. _writeBufferSize = (uint)random.Next(1, 1000);
  28. _length = 5555;
  29. }
  30. protected override void SetupMocks()
  31. {
  32. SftpSessionMock.InSequence(MockSequence)
  33. .Setup(p => p.RequestOpen(_path, Flags.Read, false))
  34. .Returns(_handle);
  35. SftpSessionMock.InSequence(MockSequence)
  36. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  37. .Returns(_readBufferSize);
  38. SftpSessionMock.InSequence(MockSequence)
  39. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  40. .Returns(_writeBufferSize);
  41. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(false);
  42. }
  43. protected override void Arrange()
  44. {
  45. base.Arrange();
  46. _target = new SftpFileStream(SftpSessionMock.Object,
  47. _path,
  48. FileMode.Open,
  49. FileAccess.Read,
  50. (int)_bufferSize);
  51. }
  52. protected override void Act()
  53. {
  54. try
  55. {
  56. _target.SetLength(_length);
  57. Assert.Fail();
  58. }
  59. catch (ObjectDisposedException ex)
  60. {
  61. _actualException = ex;
  62. }
  63. }
  64. [TestMethod]
  65. public void SetLengthShouldHaveThrownObjectDisposedException()
  66. {
  67. Assert.IsNotNull(_actualException);
  68. Assert.IsNull(_actualException.InnerException);
  69. Assert.AreEqual(
  70. string.Format(
  71. "Cannot access a closed SFTP session.{0}Object name: '{1}'.",
  72. Environment.NewLine,
  73. _actualException.ObjectName),
  74. _actualException.Message);
  75. Assert.AreEqual(typeof(SftpFileStream).FullName, _actualException.ObjectName);
  76. }
  77. }
  78. }