2
0

SftpFileStreamTest_SetLength_Closed.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_Closed
  11. {
  12. private Mock<ISftpSession> _sftpSessionMock;
  13. private string _path;
  14. private SftpFileStream _sftpFileStream;
  15. private byte[] _handle;
  16. private uint _bufferSize;
  17. private uint _readBufferSize;
  18. private uint _writeBufferSize;
  19. private ObjectDisposedException _actualException;
  20. [TestInitialize]
  21. public void Setup()
  22. {
  23. Arrange();
  24. Act();
  25. }
  26. protected void Arrange()
  27. {
  28. var random = new Random();
  29. _path = random.Next().ToString(CultureInfo.InvariantCulture);
  30. _handle = new[] { (byte)random.Next(byte.MinValue, byte.MaxValue) };
  31. _bufferSize = (uint)random.Next(1, 1000);
  32. _readBufferSize = (uint)random.Next(0, 1000);
  33. _writeBufferSize = (uint)random.Next(0, 1000);
  34. _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
  35. var sequence = new MockSequence();
  36. _sftpSessionMock.InSequence(sequence)
  37. .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true))
  38. .Returns(_handle);
  39. _sftpSessionMock.InSequence(sequence)
  40. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  41. .Returns(_readBufferSize);
  42. _sftpSessionMock.InSequence(sequence)
  43. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  44. .Returns(_writeBufferSize);
  45. _sftpSessionMock.InSequence(sequence)
  46. .Setup(p => p.IsOpen)
  47. .Returns(true);
  48. _sftpSessionMock.InSequence(sequence)
  49. .Setup(p => p.RequestClose(_handle));
  50. _sftpFileStream = new SftpFileStream(_sftpSessionMock.Object, _path, FileMode.Create, FileAccess.Write, (int)_bufferSize);
  51. _sftpFileStream.Close();
  52. }
  53. protected void Act()
  54. {
  55. try
  56. {
  57. _sftpFileStream.SetLength(5);
  58. Assert.Fail();
  59. }
  60. catch (ObjectDisposedException ex)
  61. {
  62. _actualException = ex;
  63. }
  64. }
  65. [TestMethod]
  66. public void SetLengthShouldHaveThrownObjectDisposedException()
  67. {
  68. Assert.IsNotNull(_actualException);
  69. Assert.IsNull(_actualException.InnerException);
  70. Assert.AreEqual(
  71. string.Format(
  72. "Cannot access a disposed object.{0}Object name: '{1}'.",
  73. Environment.NewLine,
  74. _actualException.ObjectName),
  75. _actualException.Message);
  76. Assert.AreEqual(typeof(SftpFileStream).FullName, _actualException.ObjectName);
  77. }
  78. }
  79. }