SftpFileStreamTest_Flush_SessionNotOpen.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_Flush_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 ObjectDisposedException _actualException;
  18. protected override void SetupData()
  19. {
  20. base.SetupData();
  21. var random = new Random();
  22. _path = random.Next().ToString();
  23. _handle = GenerateRandom(5, random);
  24. _bufferSize = (uint)random.Next(1, 1000);
  25. _readBufferSize = 20;
  26. _writeBufferSize = 500;
  27. }
  28. protected override void SetupMocks()
  29. {
  30. SftpSessionMock.InSequence(MockSequence)
  31. .Setup(p => p.RequestOpen(_path, Flags.Read, false))
  32. .Returns(_handle);
  33. SftpSessionMock.InSequence(MockSequence)
  34. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  35. .Returns(_readBufferSize);
  36. SftpSessionMock.InSequence(MockSequence)
  37. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  38. .Returns(_writeBufferSize);
  39. SftpSessionMock.InSequence(MockSequence)
  40. .Setup(p => p.IsOpen)
  41. .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.Flush();
  57. Assert.Fail();
  58. }
  59. catch (ObjectDisposedException ex)
  60. {
  61. _actualException = ex;
  62. }
  63. }
  64. [TestMethod]
  65. public void FlushShouldHaveThrownObjectDisposedException()
  66. {
  67. Assert.IsNotNull(_actualException);
  68. Assert.IsNull(_actualException.InnerException);
  69. Assert.AreEqual(string.Format("Cannot access a closed SFTP session.{0}Object name: '{1}'.", Environment.NewLine, _actualException.ObjectName), _actualException.Message);
  70. Assert.AreEqual(typeof(SftpFileStream).FullName, _actualException.ObjectName);
  71. }
  72. }
  73. }