2
0

SftpSessionTest_DataReceived_MultipleSftpMessagesInSingleSshDataMessage.cs 11 KB

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