SftpSessionTest_DataReceived_MultipleSftpMessagesSplitOverMultipleSshDataMessages.cs 11 KB

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