SftpSessionTest_DataReceived_MultipleSftpMessagesInSingleSshDataMessage.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Text;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Microsoft.Extensions.Logging.Abstractions;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Moq;
  8. using Renci.SshNet.Abstractions;
  9. using Renci.SshNet.Channels;
  10. using Renci.SshNet.Common;
  11. using Renci.SshNet.Sftp;
  12. using Renci.SshNet.Sftp.Responses;
  13. using Renci.SshNet.Tests.Common;
  14. namespace Renci.SshNet.Tests.Classes.Sftp
  15. {
  16. [TestClass]
  17. public class SftpSessionTest_DataReceived_MultipleSftpMessagesInSingleSshDataMessage
  18. {
  19. #region SftpSession.Connect()
  20. private Mock<ISession> _sessionMock;
  21. private Mock<IChannelSession> _channelSessionMock;
  22. private Mock<ISftpResponseFactory> _sftpResponseFactoryMock;
  23. private SftpSession _sftpSession;
  24. private int _operationTimeout;
  25. private Encoding _encoding;
  26. private uint _protocolVersion;
  27. private byte[] _sftpInitRequestBytes;
  28. private SftpVersionResponse _sftpVersionResponse;
  29. private byte[] _sftpRealPathRequestBytes;
  30. private SftpNameResponse _sftpNameResponse;
  31. private byte[] _sftpOpenRequestBytes;
  32. private byte[] _sftpHandleResponseBytes;
  33. private byte[] _sftpReadRequestBytes;
  34. private byte[] _sftpDataResponseBytes;
  35. private byte[] _handle;
  36. private uint _offset;
  37. private uint _length;
  38. private byte[] _data;
  39. private string _path;
  40. private byte[] _actualHandle;
  41. private byte[] _actualData;
  42. #endregion SftpSession.Connect()
  43. [TestInitialize]
  44. public void Setup()
  45. {
  46. Arrange();
  47. Act();
  48. }
  49. private void SetupData()
  50. {
  51. var random = new Random();
  52. #region SftpSession.Connect()
  53. _operationTimeout = random.Next(100, 500);
  54. _protocolVersion = (uint)random.Next(0, 3);
  55. _encoding = Encoding.UTF8;
  56. _sftpInitRequestBytes = new SftpInitRequestBuilder().WithVersion(SftpSession.MaximumSupportedVersion)
  57. .Build()
  58. .GetBytes();
  59. _sftpVersionResponse = new SftpVersionResponseBuilder().WithVersion(_protocolVersion)
  60. .Build();
  61. _sftpRealPathRequestBytes = new SftpRealPathRequestBuilder().WithProtocolVersion(_protocolVersion)
  62. .WithRequestId(1)
  63. .WithPath(".")
  64. .WithEncoding(_encoding)
  65. .Build()
  66. .GetBytes();
  67. _sftpNameResponse = new SftpNameResponseBuilder().WithProtocolVersion(_protocolVersion)
  68. .WithResponseId(1)
  69. .WithEncoding(_encoding)
  70. .WithFile("/ABC", SftpFileAttributesBuilder.Empty)
  71. .Build();
  72. #endregion SftpSession.Connect()
  73. _path = random.Next().ToString();
  74. _handle = CryptoAbstraction.GenerateRandom(4);
  75. _offset = (uint)random.Next(1, 5);
  76. _length = (uint)random.Next(30, 50);
  77. _data = CryptoAbstraction.GenerateRandom(200);
  78. _sftpOpenRequestBytes = new SftpOpenRequestBuilder().WithProtocolVersion(_protocolVersion)
  79. .WithRequestId(2)
  80. .WithFileName(_path)
  81. .WithFlags(Flags.Read)
  82. .WithEncoding(_encoding)
  83. .Build()
  84. .GetBytes();
  85. _sftpHandleResponseBytes = new SftpHandleResponseBuilder().WithProtocolVersion(_protocolVersion)
  86. .WithResponseId(2)
  87. .WithHandle(_handle)
  88. .Build()
  89. .GetBytes();
  90. _sftpReadRequestBytes = new SftpReadRequestBuilder().WithProtocolVersion(_protocolVersion)
  91. .WithRequestId(3)
  92. .WithHandle(_handle)
  93. .WithOffset(_offset)
  94. .WithLength(_length)
  95. .Build()
  96. .GetBytes();
  97. _sftpDataResponseBytes = new SftpDataResponseBuilder().WithProtocolVersion(_protocolVersion)
  98. .WithResponseId(3)
  99. .WithData(_data)
  100. .Build()
  101. .GetBytes();
  102. }
  103. private void CreateMocks()
  104. {
  105. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  106. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  107. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  108. _sftpResponseFactoryMock = new Mock<ISftpResponseFactory>(MockBehavior.Strict);
  109. }
  110. private void SetupMocks()
  111. {
  112. var sequence = new MockSequence();
  113. #region SftpSession.Connect()
  114. _sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  115. _channelSessionMock.InSequence(sequence).Setup(p => p.Open());
  116. _channelSessionMock.InSequence(sequence).Setup(p => p.SendSubsystemRequest("sftp")).Returns(true);
  117. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  118. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpInitRequestBytes))
  119. .Callback(() =>
  120. {
  121. _channelSessionMock.Raise(c => c.DataReceived += null,
  122. new ChannelDataEventArgs(0, _sftpVersionResponse.GetBytes()));
  123. });
  124. _sftpResponseFactoryMock.InSequence(sequence)
  125. .Setup(p => p.Create(0U, (byte)SftpMessageTypes.Version, _encoding))
  126. .Returns(_sftpVersionResponse);
  127. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  128. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpRealPathRequestBytes))
  129. .Callback(() =>
  130. {
  131. _channelSessionMock.Raise(c => c.DataReceived += null,
  132. new ChannelDataEventArgs(0, _sftpNameResponse.GetBytes()));
  133. });
  134. _sftpResponseFactoryMock.InSequence(sequence)
  135. .Setup(p => p.Create(_protocolVersion, (byte)SftpMessageTypes.Name, _encoding))
  136. .Returns(_sftpNameResponse);
  137. #endregion SftpSession.Connect()
  138. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  139. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpOpenRequestBytes));
  140. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  141. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpReadRequestBytes)).Callback(() =>
  142. {
  143. var sshMessagePayload = new byte[_sftpHandleResponseBytes.Length + _sftpDataResponseBytes.Length];
  144. Buffer.BlockCopy(_sftpHandleResponseBytes, 0, sshMessagePayload, 0, _sftpHandleResponseBytes.Length);
  145. Buffer.BlockCopy(_sftpDataResponseBytes, 0, sshMessagePayload, _sftpHandleResponseBytes.Length, _sftpDataResponseBytes.Length);
  146. _channelSessionMock.Raise(c => c.DataReceived += null,
  147. new ChannelDataEventArgs(0, sshMessagePayload));
  148. });
  149. _sftpResponseFactoryMock.InSequence(sequence)
  150. .Setup(p => p.Create(_protocolVersion, (byte)SftpMessageTypes.Handle, _encoding))
  151. .Returns(new SftpHandleResponse(_protocolVersion));
  152. _sftpResponseFactoryMock.InSequence(sequence)
  153. .Setup(p => p.Create(_protocolVersion, (byte)SftpMessageTypes.Data, _encoding))
  154. .Returns(new SftpDataResponse(_protocolVersion));
  155. }
  156. protected void Arrange()
  157. {
  158. SetupData();
  159. CreateMocks();
  160. SetupMocks();
  161. _sftpSession = new SftpSession(_sessionMock.Object, _operationTimeout, _encoding, _sftpResponseFactoryMock.Object);
  162. _sftpSession.Connect();
  163. }
  164. protected void Act()
  165. {
  166. Task<byte[]> openTask = _sftpSession.RequestOpenAsync(_path, Flags.Read, CancellationToken.None);
  167. Task<byte[]> readTask = _sftpSession.RequestReadAsync(_handle, _offset, _length, CancellationToken.None);
  168. Task.WaitAll(openTask, readTask);
  169. _actualHandle = openTask.Result;
  170. _actualData = readTask.Result;
  171. }
  172. [TestMethod]
  173. public void ReturnedValueShouldBeDataOfSftpDataResponse()
  174. {
  175. Assert.IsTrue(_data.IsEqualTo(_actualData));
  176. }
  177. [TestMethod]
  178. public void ReturnedHandleShouldBeHandleOfSftpHandleResponse()
  179. {
  180. Assert.IsTrue(_handle.IsEqualTo(_actualHandle));
  181. }
  182. }
  183. }