SftpSessionTest_DataReceived_MultipleSftpMessagesInSingleSshDataMessage.cs 10 KB

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