SftpSessionTest_DataReceived_SingleSftpMessageInSshDataMessage.cs 7.7 KB

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