SftpSessionTest_DataReceived_SingleSftpMessageInSshDataMessage.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_SingleSftpMessageInSshDataMessage
  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[] _sftpReadRequestBytes;
  30. private byte[] _sftpDataResponseBytes;
  31. private byte[] _handle;
  32. private uint _offset;
  33. private uint _length;
  34. private byte[] _data;
  35. private byte[] _actual;
  36. #endregion SftpSession.Connect()
  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. _sftpInitRequestBytes = new SftpInitRequestBuilder().WithVersion(SftpSession.MaximumSupportedVersion)
  51. .Build()
  52. .GetBytes();
  53. _sftpVersionResponse = new SftpVersionResponseBuilder().WithVersion(_protocolVersion)
  54. .Build();
  55. _sftpRealPathRequestBytes = new SftpRealPathRequestBuilder().WithProtocolVersion(_protocolVersion)
  56. .WithRequestId(1)
  57. .WithPath(".")
  58. .WithEncoding(_encoding)
  59. .Build()
  60. .GetBytes();
  61. _sftpNameResponse = new SftpNameResponseBuilder().WithProtocolVersion(_protocolVersion)
  62. .WithResponseId(1)
  63. .WithEncoding(_encoding)
  64. .WithFile("/ABC", SftpFileAttributesBuilder.Empty)
  65. .Build();
  66. #endregion SftpSession.Connect()
  67. _handle = CryptoAbstraction.GenerateRandom(4);
  68. _offset = (uint)random.Next(1, 5);
  69. _length = (uint)random.Next(30, 50);
  70. _data = CryptoAbstraction.GenerateRandom(200);
  71. _sftpReadRequestBytes = new SftpReadRequestBuilder().WithProtocolVersion(_protocolVersion)
  72. .WithRequestId(2)
  73. .WithHandle(_handle)
  74. .WithOffset(_offset)
  75. .WithLength(_length)
  76. .Build()
  77. .GetBytes();
  78. _sftpDataResponseBytes = new SftpDataResponseBuilder().WithProtocolVersion(_protocolVersion)
  79. .WithResponseId(2)
  80. .WithData(_data)
  81. .Build()
  82. .GetBytes();
  83. }
  84. private void CreateMocks()
  85. {
  86. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  87. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  88. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  89. _sftpResponseFactoryMock = new Mock<ISftpResponseFactory>(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. _sftpResponseFactoryMock.InSequence(sequence)
  106. .Setup(p => p.Create(0U, (byte)SftpMessageTypes.Version, _encoding))
  107. .Returns(_sftpVersionResponse);
  108. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  109. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpRealPathRequestBytes))
  110. .Callback(() =>
  111. {
  112. _channelSessionMock.Raise(c => c.DataReceived += null,
  113. new ChannelDataEventArgs(0, _sftpNameResponse.GetBytes()));
  114. });
  115. _sftpResponseFactoryMock.InSequence(sequence)
  116. .Setup(p => p.Create(_protocolVersion, (byte)SftpMessageTypes.Name, _encoding))
  117. .Returns(_sftpNameResponse);
  118. #endregion SftpSession.Connect()
  119. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  120. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpReadRequestBytes))
  121. .Callback(() =>
  122. {
  123. _channelSessionMock.Raise(c => c.DataReceived += null,
  124. new ChannelDataEventArgs(0, _sftpDataResponseBytes));
  125. });
  126. _sftpResponseFactoryMock.InSequence(sequence)
  127. .Setup(p => p.Create(_protocolVersion, (byte)SftpMessageTypes.Data, _encoding))
  128. .Returns(new SftpDataResponse(_protocolVersion));
  129. }
  130. protected void Arrange()
  131. {
  132. SetupData();
  133. CreateMocks();
  134. SetupMocks();
  135. _sftpSession = new SftpSession(_sessionMock.Object, _operationTimeout, _encoding, _sftpResponseFactoryMock.Object);
  136. _sftpSession.Connect();
  137. }
  138. protected void Act()
  139. {
  140. _actual = _sftpSession.RequestRead(_handle, _offset, _length);
  141. }
  142. [TestMethod]
  143. public void ReturnedValueShouldBeDataOfSftpDataResponse()
  144. {
  145. Assert.IsTrue(_data.IsEqualTo(_actual));
  146. }
  147. }
  148. }