SftpSessionTest_Connected_RequestRead.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. namespace Renci.SshNet.Tests.Classes.Sftp
  13. {
  14. [TestClass]
  15. public class SftpSessionTest_Connected_RequestRead
  16. {
  17. #region SftpSession.Connect()
  18. private Mock<ISession> _sessionMock;
  19. private Mock<IChannelSession> _channelSessionMock;
  20. private ISftpResponseFactory _sftpResponseFactory;
  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. #endregion SftpSession.Connect()
  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 byte[] _actual;
  37. [TestInitialize]
  38. public void Setup()
  39. {
  40. Arrange();
  41. Act();
  42. }
  43. private void SetupData()
  44. {
  45. var random = new Random();
  46. #region SftpSession.Connect()
  47. _operationTimeout = random.Next(100, 500);
  48. _protocolVersion = (uint)random.Next(0, 3);
  49. _encoding = Encoding.UTF8;
  50. _sftpResponseFactory = new SftpResponseFactory();
  51. _sftpInitRequestBytes = new SftpInitRequestBuilder().WithVersion(SftpSession.MaximumSupportedVersion)
  52. .Build()
  53. .GetBytes();
  54. _sftpVersionResponse = new SftpVersionResponseBuilder().WithVersion(_protocolVersion)
  55. .Build();
  56. _sftpRealPathRequestBytes = new SftpRealPathRequestBuilder().WithProtocolVersion(_protocolVersion)
  57. .WithRequestId(1)
  58. .WithPath(".")
  59. .WithEncoding(_encoding)
  60. .Build()
  61. .GetBytes();
  62. _sftpNameResponse = new SftpNameResponseBuilder().WithProtocolVersion(_protocolVersion)
  63. .WithResponseId(1)
  64. .WithEncoding(_encoding)
  65. .WithFile("XYZ", SftpFileAttributes.Empty)
  66. .Build();
  67. #endregion SftpSession.Connect()
  68. _handle = CryptoAbstraction.GenerateRandom(random.Next(1, 10));
  69. _offset = (uint)random.Next(1, 5);
  70. _length = (uint)random.Next(30, 50);
  71. _data = CryptoAbstraction.GenerateRandom((int)_length);
  72. _sftpReadRequestBytes = new SftpReadRequestBuilder().WithProtocolVersion(_protocolVersion)
  73. .WithRequestId(2)
  74. .WithHandle(_handle)
  75. .WithOffset(_offset)
  76. .WithLength(_length)
  77. .Build()
  78. .GetBytes();
  79. _sftpDataResponseBytes = new SftpDataResponseBuilder().WithProtocolVersion(_protocolVersion)
  80. .WithResponseId(2)
  81. .WithData(_data)
  82. .Build()
  83. .GetBytes();
  84. }
  85. private void CreateMocks()
  86. {
  87. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  88. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  89. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  90. }
  91. private void SetupMocks()
  92. {
  93. var sequence = new MockSequence();
  94. #region SftpSession.Connect()
  95. _sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  96. _channelSessionMock.InSequence(sequence).Setup(p => p.Open());
  97. _channelSessionMock.InSequence(sequence).Setup(p => p.SendSubsystemRequest("sftp")).Returns(true);
  98. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  99. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpInitRequestBytes))
  100. .Callback(() =>
  101. {
  102. _channelSessionMock.Raise(c => c.DataReceived += null,
  103. new ChannelDataEventArgs(0, _sftpVersionResponse.GetBytes()));
  104. });
  105. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  106. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpRealPathRequestBytes))
  107. .Callback(() =>
  108. {
  109. _channelSessionMock.Raise(c => c.DataReceived += null,
  110. new ChannelDataEventArgs(0, _sftpNameResponse.GetBytes()));
  111. });
  112. #endregion SftpSession.Connect()
  113. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  114. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpReadRequestBytes))
  115. .Callback(() =>
  116. {
  117. _channelSessionMock.Raise(
  118. c => c.DataReceived += null,
  119. new ChannelDataEventArgs(0, _sftpDataResponseBytes.Take(0, 20)));
  120. _channelSessionMock.Raise(
  121. c => c.DataReceived += null,
  122. new ChannelDataEventArgs(0, _sftpDataResponseBytes.Take(20, _sftpDataResponseBytes.Length - 20)));
  123. }
  124. );
  125. }
  126. private void Arrange()
  127. {
  128. SetupData();
  129. CreateMocks();
  130. SetupMocks();
  131. _sftpSession = new SftpSession(_sessionMock.Object, _operationTimeout, _encoding, _sftpResponseFactory);
  132. _sftpSession.Connect();
  133. }
  134. protected void Act()
  135. {
  136. _actual = _sftpSession.RequestRead(_handle, _offset, _length);
  137. }
  138. [TestMethod]
  139. public void ReturnedValueShouldBeDataOfSftpDataResponse()
  140. {
  141. Assert.IsNotNull(_actual);
  142. Assert.IsTrue(_data.SequenceEqual(_actual));
  143. }
  144. }
  145. }