SftpFileReaderTest_LastChunkBeforeEofIsComplete.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Moq;
  3. using Renci.SshNet.Common;
  4. using Renci.SshNet.Sftp;
  5. using System;
  6. using System.Diagnostics;
  7. using BufferedRead = Renci.SshNet.Sftp.SftpFileReader.BufferedRead;
  8. namespace Renci.SshNet.Tests.Classes.Sftp
  9. {
  10. [TestClass]
  11. public class SftpFileReaderTest_LastChunkBeforeEofIsComplete : SftpFileReaderTestBase
  12. {
  13. private const int ChunkLength = 32 * 1024;
  14. private MockSequence _seq;
  15. private byte[] _handle;
  16. private int _fileSize;
  17. private byte[] _chunk1;
  18. private byte[] _chunk2;
  19. private byte[] _chunk3;
  20. private SftpFileReader _reader;
  21. private byte[] _actualChunk1;
  22. private byte[] _actualChunk2;
  23. private byte[] _actualChunk3;
  24. protected override void SetupData()
  25. {
  26. var random = new Random();
  27. _handle = CreateByteArray(random, 5);
  28. _chunk1 = CreateByteArray(random, ChunkLength);
  29. // chunk is less than the requested length, but - together with chunk 1 - contains all data up to the EOF
  30. _chunk2 = CreateByteArray(random, ChunkLength - 10);
  31. _chunk3 = new byte[0];
  32. _fileSize = _chunk1.Length + _chunk2.Length;
  33. }
  34. protected override void SetupMocks()
  35. {
  36. _seq = new MockSequence();
  37. SftpSessionMock.InSequence(_seq)
  38. .Setup(p => p.BeginRead(_handle, 0, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  39. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  40. {
  41. var asyncResult = new SftpReadAsyncResult(callback, state);
  42. asyncResult.SetAsCompleted(_chunk1, false);
  43. })
  44. .Returns((SftpReadAsyncResult)null);
  45. SftpSessionMock.InSequence(_seq)
  46. .Setup(p => p.BeginRead(_handle, ChunkLength, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  47. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  48. {
  49. var asyncResult = new SftpReadAsyncResult(callback, state);
  50. asyncResult.SetAsCompleted(_chunk2, false);
  51. })
  52. .Returns((SftpReadAsyncResult)null);
  53. SftpSessionMock.InSequence(_seq)
  54. .Setup(p => p.BeginRead(_handle, 2 * ChunkLength, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  55. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  56. {
  57. var asyncResult = new SftpReadAsyncResult(callback, state);
  58. asyncResult.SetAsCompleted(_chunk3, false);
  59. })
  60. .Returns((SftpReadAsyncResult)null);
  61. }
  62. protected override void Arrange()
  63. {
  64. base.Arrange();
  65. _reader = new SftpFileReader(_handle, SftpSessionMock.Object, ChunkLength, 15, _fileSize);
  66. }
  67. protected override void Act()
  68. {
  69. _actualChunk1 = _reader.Read();
  70. _actualChunk2 = _reader.Read();
  71. _actualChunk3 = _reader.Read();
  72. }
  73. [TestMethod]
  74. public void FirstReadShouldReturnChunk1()
  75. {
  76. Assert.IsNotNull(_actualChunk1);
  77. Assert.AreSame(_chunk1, _actualChunk1);
  78. }
  79. [TestMethod]
  80. public void SecondReadShouldReturnChunk2()
  81. {
  82. Assert.IsNotNull(_actualChunk2);
  83. Assert.AreSame(_chunk2, _actualChunk2);
  84. }
  85. [TestMethod]
  86. public void ThirdReadShouldReturnChunk3()
  87. {
  88. Assert.IsNotNull(_actualChunk3);
  89. Assert.AreSame(_chunk3, _actualChunk3);
  90. }
  91. [TestMethod]
  92. public void ReadAfterEndOfFileShouldThrowSshException()
  93. {
  94. try
  95. {
  96. _reader.Read();
  97. Assert.Fail();
  98. }
  99. catch (SshException ex)
  100. {
  101. Assert.IsNull(ex.InnerException);
  102. Assert.AreEqual("Attempting to read beyond the end of the file.", ex.Message);
  103. }
  104. }
  105. [TestMethod]
  106. public void DisposeShouldCloseHandleAndCompleteImmediately()
  107. {
  108. SftpSessionMock.InSequence(_seq).Setup(p => p.RequestClose(_handle));
  109. var stopwatch = Stopwatch.StartNew();
  110. _reader.Dispose();
  111. stopwatch.Stop();
  112. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 200, "Dispose took too long to complete: " + stopwatch.ElapsedMilliseconds);
  113. SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
  114. }
  115. }
  116. }