2
0

SftpFileReaderTest_LastChunkBeforeEofIsPartial.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Moq;
  6. using Renci.SshNet.Common;
  7. using Renci.SshNet.Sftp;
  8. using BufferedRead = Renci.SshNet.Sftp.SftpFileReader.BufferedRead;
  9. namespace Renci.SshNet.Tests.Classes.Sftp
  10. {
  11. [TestClass]
  12. public class SftpFileReaderTest_LastChunkBeforeEofIsPartial : SftpFileReaderTestBase
  13. {
  14. private const int ChunkLength = 32 * 1024;
  15. private MockSequence _seq;
  16. private byte[] _handle;
  17. private int _fileSize;
  18. private WaitHandle[] _waitHandleArray;
  19. private int _operationTimeout;
  20. private SftpCloseAsyncResult _closeAsyncResult;
  21. private byte[] _chunk1;
  22. private byte[] _chunk2;
  23. private byte[] _chunk3;
  24. private SftpFileReader _reader;
  25. private byte[] _actualChunk1;
  26. private byte[] _actualChunk2;
  27. private byte[] _actualChunk3;
  28. protected override void SetupData()
  29. {
  30. var random = new Random();
  31. _handle = CreateByteArray(random, 5);
  32. _chunk1 = CreateByteArray(random, ChunkLength);
  33. _chunk2 = CreateByteArray(random, ChunkLength);
  34. _chunk3 = new byte[0];
  35. _fileSize = _chunk1.Length + _chunk2.Length;
  36. _waitHandleArray = new WaitHandle[2];
  37. _operationTimeout = random.Next(10000, 20000);
  38. _closeAsyncResult = new SftpCloseAsyncResult(null, null);
  39. }
  40. protected override void SetupMocks()
  41. {
  42. _seq = new MockSequence();
  43. SftpSessionMock.InSequence(_seq)
  44. .Setup(p => p.CreateWaitHandleArray(It.IsNotNull<WaitHandle>(), It.IsNotNull<WaitHandle>()))
  45. .Returns<WaitHandle, WaitHandle>((disposingWaitHandle, semaphoreAvailableWaitHandle) =>
  46. {
  47. _waitHandleArray[0] = disposingWaitHandle;
  48. _waitHandleArray[1] = semaphoreAvailableWaitHandle;
  49. return _waitHandleArray;
  50. });
  51. SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout);
  52. SftpSessionMock.InSequence(_seq)
  53. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  54. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  55. SftpSessionMock.InSequence(_seq)
  56. .Setup(p => p.BeginRead(_handle, 0, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  57. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  58. {
  59. var asyncResult = new SftpReadAsyncResult(callback, state);
  60. asyncResult.SetAsCompleted(_chunk1, false);
  61. })
  62. .Returns((SftpReadAsyncResult)null);
  63. SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout);
  64. SftpSessionMock.InSequence(_seq)
  65. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  66. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  67. SftpSessionMock.InSequence(_seq)
  68. .Setup(p => p.BeginRead(_handle, ChunkLength, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  69. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  70. {
  71. var asyncResult = new SftpReadAsyncResult(callback, state);
  72. asyncResult.SetAsCompleted(_chunk2, false);
  73. })
  74. .Returns((SftpReadAsyncResult)null);
  75. SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout);
  76. SftpSessionMock.InSequence(_seq)
  77. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  78. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  79. SftpSessionMock.InSequence(_seq)
  80. .Setup(p => p.BeginRead(_handle, 2 * ChunkLength, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  81. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  82. {
  83. var asyncResult = new SftpReadAsyncResult(callback, state);
  84. asyncResult.SetAsCompleted(_chunk3, false);
  85. })
  86. .Returns((SftpReadAsyncResult)null);
  87. }
  88. protected override void Arrange()
  89. {
  90. base.Arrange();
  91. _reader = new SftpFileReader(_handle, SftpSessionMock.Object, ChunkLength, 15, _fileSize);
  92. }
  93. protected override void Act()
  94. {
  95. _actualChunk1 = _reader.Read();
  96. _actualChunk2 = _reader.Read();
  97. _actualChunk3 = _reader.Read();
  98. }
  99. [TestMethod]
  100. public void FirstReadShouldReturnChunk1()
  101. {
  102. Assert.IsNotNull(_actualChunk1);
  103. Assert.AreSame(_chunk1, _actualChunk1);
  104. }
  105. [TestMethod]
  106. public void SecondReadShouldReturnChunk2()
  107. {
  108. Assert.IsNotNull(_actualChunk2);
  109. Assert.AreSame(_chunk2, _actualChunk2);
  110. }
  111. [TestMethod]
  112. public void ThirdReadShouldReturnChunk3()
  113. {
  114. Assert.IsNotNull(_actualChunk3);
  115. Assert.AreSame(_chunk3, _actualChunk3);
  116. }
  117. [TestMethod]
  118. public void ReadAfterEndOfFileShouldThrowSshException()
  119. {
  120. try
  121. {
  122. _reader.Read();
  123. Assert.Fail();
  124. }
  125. catch (SshException ex)
  126. {
  127. Assert.IsNull(ex.InnerException);
  128. Assert.AreEqual("Attempting to read beyond the end of the file.", ex.Message);
  129. }
  130. }
  131. [TestMethod]
  132. public void DisposeShouldCloseHandleAndCompleteImmediately()
  133. {
  134. SftpSessionMock.InSequence(_seq).Setup(p => p.IsOpen).Returns(true);
  135. SftpSessionMock.InSequence(_seq).Setup(p => p.BeginClose(_handle, null, null)).Returns(_closeAsyncResult);
  136. SftpSessionMock.InSequence(_seq).Setup(p => p.EndClose(_closeAsyncResult));
  137. var stopwatch = Stopwatch.StartNew();
  138. _reader.Dispose();
  139. stopwatch.Stop();
  140. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 200, "Dispose took too long to complete: " + stopwatch.ElapsedMilliseconds);
  141. SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
  142. SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Once);
  143. SftpSessionMock.Verify(p => p.EndClose(_closeAsyncResult), Times.Once);
  144. }
  145. }
  146. }