2
0

SftpSessionTest_Connected_RequestRead.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  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_Connected_RequestRead
  15. {
  16. #region SftpSession.Connect()
  17. private Mock<ISession> _sessionMock;
  18. private Mock<IChannelSession> _channelSessionMock;
  19. private ISftpResponseFactory _sftpResponseFactory;
  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. #endregion SftpSession.Connect()
  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. [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. _sftpResponseFactory = new SftpResponseFactory();
  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("XYZ", SftpFileAttributes.Empty)
  65. .Build();
  66. #endregion SftpSession.Connect()
  67. _handle = CryptoAbstraction.GenerateRandom(random.Next(1, 10));
  68. _offset = (uint)random.Next(1, 5);
  69. _length = (uint)random.Next(30, 50);
  70. _data = CryptoAbstraction.GenerateRandom((int)_length);
  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. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  88. }
  89. private void SetupMocks()
  90. {
  91. var sequence = new MockSequence();
  92. #region SftpSession.Connect()
  93. _sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  94. _channelSessionMock.InSequence(sequence).Setup(p => p.Open());
  95. _channelSessionMock.InSequence(sequence).Setup(p => p.SendSubsystemRequest("sftp")).Returns(true);
  96. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  97. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpInitRequestBytes))
  98. .Callback(() =>
  99. {
  100. _channelSessionMock.Raise(c => c.DataReceived += null,
  101. new ChannelDataEventArgs(0, _sftpVersionResponse.GetBytes()));
  102. });
  103. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  104. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpRealPathRequestBytes))
  105. .Callback(() =>
  106. {
  107. _channelSessionMock.Raise(c => c.DataReceived += null,
  108. new ChannelDataEventArgs(0, _sftpNameResponse.GetBytes()));
  109. });
  110. #endregion SftpSession.Connect()
  111. _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
  112. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpReadRequestBytes))
  113. .Callback(() =>
  114. {
  115. _channelSessionMock.Raise(
  116. c => c.DataReceived += null,
  117. new ChannelDataEventArgs(0, _sftpDataResponseBytes.Take(0, 20)));
  118. _channelSessionMock.Raise(
  119. c => c.DataReceived += null,
  120. new ChannelDataEventArgs(0, _sftpDataResponseBytes.Take(20, _sftpDataResponseBytes.Length - 20)));
  121. }
  122. );
  123. }
  124. private void Arrange()
  125. {
  126. SetupData();
  127. CreateMocks();
  128. SetupMocks();
  129. _sftpSession = new SftpSession(_sessionMock.Object, _operationTimeout, _encoding, _sftpResponseFactory);
  130. _sftpSession.Connect();
  131. }
  132. protected void Act()
  133. {
  134. _actual = _sftpSession.RequestRead(_handle, _offset, _length);
  135. }
  136. [TestMethod]
  137. public void ReturnedValueShouldBeDataOfSftpDataResponse()
  138. {
  139. Assert.IsNotNull(_actual);
  140. Assert.IsTrue(_data.SequenceEqual(_actual));
  141. }
  142. }
  143. }