SftpFileStreamTest_Close_SessionNotOpen.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_Close_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. protected override void SetupData()
  18. {
  19. base.SetupData();
  20. var random = new Random();
  21. _path = random.Next().ToString();
  22. _handle = GenerateRandom(2, random);
  23. _bufferSize = (uint) random.Next(1, 1000);
  24. _readBufferSize = (uint) random.Next(1, 1000);
  25. _writeBufferSize = (uint) random.Next(1, 1000);
  26. }
  27. protected override void SetupMocks()
  28. {
  29. SftpSessionMock.InSequence(MockSequence)
  30. .Setup(p => p.RequestOpen(_path, Flags.Read, false))
  31. .Returns(_handle);
  32. SftpSessionMock.InSequence(MockSequence)
  33. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  34. .Returns(_readBufferSize);
  35. SftpSessionMock.InSequence(MockSequence)
  36. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  37. .Returns(_writeBufferSize);
  38. SftpSessionMock.InSequence(MockSequence)
  39. .Setup(p => p.IsOpen)
  40. .Returns(false);
  41. }
  42. protected override void Arrange()
  43. {
  44. base.Arrange();
  45. _target = new SftpFileStream(SftpSessionMock.Object,
  46. _path,
  47. FileMode.Open,
  48. FileAccess.Read,
  49. (int) _bufferSize);
  50. }
  51. protected override void Act()
  52. {
  53. _target.Close();
  54. }
  55. [TestMethod]
  56. public void IsOpenOnSftpSessionShouldBeInvokedOnce()
  57. {
  58. SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
  59. }
  60. [TestMethod]
  61. public void RequestCloseOnSftpSessionShouldNeverBeInvoked()
  62. {
  63. SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Never);
  64. }
  65. }
  66. }