SftpFileStreamTest_SetLength_SessionOpen_FileAccessRead.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_SessionOpen_FileAccessRead : 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 NotSupportedException _actualException;
  19. protected override void SetupData()
  20. {
  21. base.SetupData();
  22. var random = new Random();
  23. _path = random.Next().ToString();
  24. _handle = GenerateRandom(5, random);
  25. _bufferSize = (uint)random.Next(1, 1000);
  26. _readBufferSize = (uint)random.Next(1, 1000);
  27. _writeBufferSize = (uint)random.Next(1, 1000);
  28. _length = 6666;
  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(true);
  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 (NotSupportedException ex)
  60. {
  61. _actualException = ex;
  62. }
  63. }
  64. [TestMethod]
  65. public void SetLengthShouldHaveThrownNotSupportedException()
  66. {
  67. Assert.IsNotNull(_actualException);
  68. Assert.IsNull(_actualException.InnerException);
  69. Assert.AreEqual("Write not supported.", _actualException.Message);
  70. }
  71. }
  72. }