2
0

SftpSessionTest_DataReceived_SingleSftpMessageInSshDataMessage.cs 7.9 KB

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