SftpFileStreamTest_SetLength_Disposed.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_Disposed
  10. {
  11. private Mock<ISftpSession> _sftpSessionMock;
  12. private string _path;
  13. private SftpFileStream _sftpFileStream;
  14. private byte[] _handle;
  15. private uint _bufferSize;
  16. private uint _readBufferSize;
  17. private uint _writeBufferSize;
  18. private ObjectDisposedException _actualException;
  19. [TestInitialize]
  20. public void Setup()
  21. {
  22. Arrange();
  23. Act();
  24. }
  25. protected void Arrange()
  26. {
  27. var random = new Random();
  28. _path = random.Next().ToString();
  29. _handle = new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) };
  30. _bufferSize = (uint)random.Next(1, 1000);
  31. _readBufferSize = (uint)random.Next(1, 1000);
  32. _writeBufferSize = (uint)random.Next(1, 1000);
  33. _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
  34. var sequence = new MockSequence();
  35. _sftpSessionMock.InSequence(sequence)
  36. .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true))
  37. .Returns(_handle);
  38. _sftpSessionMock.InSequence(sequence)
  39. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  40. .Returns(_readBufferSize);
  41. _sftpSessionMock.InSequence(sequence)
  42. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  43. .Returns(_writeBufferSize);
  44. _sftpSessionMock.InSequence(sequence)
  45. .Setup(p => p.IsOpen)
  46. .Returns(true);
  47. _sftpSessionMock.InSequence(sequence)
  48. .Setup(p => p.RequestClose(_handle));
  49. _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize);
  50. _sftpFileStream.Dispose();
  51. }
  52. protected void Act()
  53. {
  54. try
  55. {
  56. _sftpFileStream.SetLength(5);
  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 disposed object.{0}Object name: '{1}'.",
  72. Environment.NewLine,
  73. _actualException.ObjectName),
  74. _actualException.Message);
  75. Assert.AreEqual(typeof(SftpFileStream).FullName, _actualException.ObjectName);
  76. }
  77. }
  78. }