SftpSessionTest_Connected_RequestRead.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using Microsoft.Extensions.Logging.Abstractions;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Moq;
  7. using Renci.SshNet.Abstractions;
  8. using Renci.SshNet.Channels;
  9. using Renci.SshNet.Common;
  10. using Renci.SshNet.Sftp;
  11. using Renci.SshNet.Sftp.Responses;
  12. using Renci.SshNet.Tests.Common;
  13. namespace Renci.SshNet.Tests.Classes.Sftp
  14. {
  15. [TestClass]
  16. public class SftpSessionTest_Connected_RequestRead
  17. {
  18. #region SftpSession.Connect()
  19. private Mock<ISession> _sessionMock;
  20. private Mock<IChannelSession> _channelSessionMock;
  21. private ISftpResponseFactory _sftpResponseFactory;
  22. private SftpSession _sftpSession;
  23. private int _operationTimeout;
  24. private Encoding _encoding;
  25. private uint _protocolVersion;
  26. private byte[] _sftpInitRequestBytes;
  27. private SftpVersionResponse _sftpVersionResponse;
  28. private byte[] _sftpRealPathRequestBytes;
  29. private SftpNameResponse _sftpNameResponse;
  30. #endregion SftpSession.Connect()
  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 byte[] _actual;
  38. [TestInitialize]
  39. public void Setup()
  40. {
  41. Arrange();
  42. Act();
  43. }
  44. private void SetupData()
  45. {
  46. var random = new Random();
  47. #region SftpSession.Connect()
  48. _operationTimeout = random.Next(100, 500);
  49. _protocolVersion = (uint)random.Next(0, 3);
  50. _encoding = Encoding.UTF8;
  51. _sftpResponseFactory = new SftpResponseFactory();
  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("XYZ", SftpFileAttributesBuilder.Empty)
  67. .Build();
  68. #endregion SftpSession.Connect()
  69. _handle = CryptoAbstraction.GenerateRandom(random.Next(1, 10));
  70. _offset = (uint)random.Next(1, 5);
  71. _length = (uint)random.Next(30, 50);
  72. _data = CryptoAbstraction.GenerateRandom((int)_length);
  73. _sftpReadRequestBytes = new SftpReadRequestBuilder().WithProtocolVersion(_protocolVersion)
  74. .WithRequestId(2)
  75. .WithHandle(_handle)
  76. .WithOffset(_offset)
  77. .WithLength(_length)
  78. .Build()
  79. .GetBytes();
  80. _sftpDataResponseBytes = new SftpDataResponseBuilder().WithProtocolVersion(_protocolVersion)
  81. .WithResponseId(2)
  82. .WithData(_data)
  83. .Build()
  84. .GetBytes();
  85. }
  86. private void CreateMocks()
  87. {
  88. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  89. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  90. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  91. }
  92. private void SetupMocks()
  93. {
  94. var sequence = new MockSequence();
  95. #region SftpSession.Connect()
  96. _sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  97. _channelSessionMock.InSequence(sequence).Setup(p => p.Open());
  98. _channelSessionMock.InSequence(sequence).Setup(p => p.SendSubsystemRequest("sftp")).Returns(true);
  99. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  100. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpInitRequestBytes))
  101. .Callback(() =>
  102. {
  103. _channelSessionMock.Raise(c => c.DataReceived += null,
  104. new ChannelDataEventArgs(0, _sftpVersionResponse.GetBytes()));
  105. });
  106. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  107. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpRealPathRequestBytes))
  108. .Callback(() =>
  109. {
  110. _channelSessionMock.Raise(c => c.DataReceived += null,
  111. new ChannelDataEventArgs(0, _sftpNameResponse.GetBytes()));
  112. });
  113. #endregion SftpSession.Connect()
  114. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  115. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpReadRequestBytes))
  116. .Callback(() =>
  117. {
  118. _channelSessionMock.Raise(
  119. c => c.DataReceived += null,
  120. new ChannelDataEventArgs(0, _sftpDataResponseBytes.Take(0, 20)));
  121. _channelSessionMock.Raise(
  122. c => c.DataReceived += null,
  123. new ChannelDataEventArgs(0, _sftpDataResponseBytes.Take(20, _sftpDataResponseBytes.Length - 20)));
  124. }
  125. );
  126. }
  127. private void Arrange()
  128. {
  129. SetupData();
  130. CreateMocks();
  131. SetupMocks();
  132. _sftpSession = new SftpSession(_sessionMock.Object, _operationTimeout, _encoding, _sftpResponseFactory);
  133. _sftpSession.Connect();
  134. }
  135. protected void Act()
  136. {
  137. _actual = _sftpSession.RequestRead(_handle, _offset, _length);
  138. }
  139. [TestMethod]
  140. public void ReturnedValueShouldBeDataOfSftpDataResponse()
  141. {
  142. Assert.IsNotNull(_actual);
  143. Assert.IsTrue(_data.SequenceEqual(_actual));
  144. }
  145. }
  146. }