SftpSessionTest_Connected_RequestStatVfs.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.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_Connected_RequestStatVfs
  14. {
  15. #region SftpSession.Connect()
  16. private Mock<ISession> _sessionMock;
  17. private Mock<IChannelSession> _channelSessionMock;
  18. private ISftpResponseFactory _sftpResponseFactory;
  19. private SftpSession _sftpSession;
  20. private int _operationTimeout;
  21. private Encoding _encoding;
  22. private uint _protocolVersion;
  23. private SftpVersionResponse _sftpVersionResponse;
  24. private SftpNameResponse _sftpNameResponse;
  25. private byte[] _sftpInitRequestBytes;
  26. private byte[] _sftpRealPathRequestBytes;
  27. #endregion SftpSession.Connect()
  28. private byte[] _sftpStatVfsRequestBytes;
  29. private StatVfsResponse _sftpStatVfsResponse;
  30. private ulong _bAvail;
  31. private string _path;
  32. private SftpFileSystemInformation _actual;
  33. [TestInitialize]
  34. public void Setup()
  35. {
  36. Arrange();
  37. Act();
  38. }
  39. private void SetupData()
  40. {
  41. var random = new Random();
  42. #region SftpSession.Connect()
  43. _operationTimeout = random.Next(100, 500);
  44. _encoding = Encoding.UTF8;
  45. _protocolVersion = 3;
  46. _sftpResponseFactory = new SftpResponseFactory();
  47. _sftpInitRequestBytes = new SftpInitRequestBuilder().WithVersion(SftpSession.MaximumSupportedVersion)
  48. .Build()
  49. .GetBytes();
  50. _sftpVersionResponse = new SftpVersionResponseBuilder().WithVersion(_protocolVersion)
  51. .WithExtension("statvfs@openssh.com", "")
  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(1U)
  61. .WithEncoding(_encoding)
  62. .WithFile("ABC", SftpFileAttributes.Empty)
  63. .Build();
  64. #endregion SftpSession.Connect()
  65. _path = random.Next().ToString();
  66. _bAvail = (ulong)random.Next(0, int.MaxValue);
  67. _sftpStatVfsRequestBytes = new SftpStatVfsRequestBuilder().WithProtocolVersion(_protocolVersion)
  68. .WithRequestId(2)
  69. .WithPath(_path)
  70. .WithEncoding(_encoding)
  71. .Build()
  72. .GetBytes();
  73. _sftpStatVfsResponse = new SftpStatVfsResponseBuilder().WithProtocolVersion(_protocolVersion)
  74. .WithResponseId(2U)
  75. .WithBAvail(_bAvail)
  76. .Build();
  77. }
  78. private void CreateMocks()
  79. {
  80. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  81. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  82. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  83. }
  84. private void SetupMocks()
  85. {
  86. var sequence = new MockSequence();
  87. #region SftpSession.Connect()
  88. _ = _sessionMock.InSequence(sequence)
  89. .Setup(p => p.CreateChannelSession())
  90. .Returns(_channelSessionMock.Object);
  91. _ = _channelSessionMock.InSequence(sequence)
  92. .Setup(p => p.Open());
  93. _ = _channelSessionMock.InSequence(sequence)
  94. .Setup(p => p.SendSubsystemRequest("sftp"))
  95. .Returns(true);
  96. _ = _channelSessionMock.InSequence(sequence)
  97. .Setup(p => p.IsOpen)
  98. .Returns(true);
  99. _ = _channelSessionMock.InSequence(sequence)
  100. .Setup(p => p.SendData(_sftpInitRequestBytes))
  101. .Callback(() =>
  102. {
  103. _channelSessionMock.Raise(c => c.DataReceived += null,
  104. new ChannelDataEventArgs(0, _sftpVersionResponse.GetBytes()));
  105. });
  106. _ = _channelSessionMock.InSequence(sequence)
  107. .Setup(p => p.IsOpen)
  108. .Returns(true);
  109. _ = _channelSessionMock.InSequence(sequence)
  110. .Setup(p => p.SendData(_sftpRealPathRequestBytes))
  111. .Callback(() =>
  112. {
  113. _channelSessionMock.Raise(c => c.DataReceived += null,
  114. new ChannelDataEventArgs(0, _sftpNameResponse.GetBytes()));
  115. });
  116. #endregion SftpSession.Connect()
  117. _ = _channelSessionMock.InSequence(sequence)
  118. .Setup(p => p.IsOpen)
  119. .Returns(true);
  120. _ = _channelSessionMock.InSequence(sequence)
  121. .Setup(p => p.SendData(_sftpStatVfsRequestBytes))
  122. .Callback(() =>
  123. {
  124. _channelSessionMock.Raise(c => c.DataReceived += null,
  125. new ChannelDataEventArgs(0, _sftpStatVfsResponse.GetBytes()));
  126. });
  127. }
  128. protected void Arrange()
  129. {
  130. SetupData();
  131. CreateMocks();
  132. SetupMocks();
  133. _sftpSession = new SftpSession(_sessionMock.Object, _operationTimeout, _encoding, _sftpResponseFactory);
  134. _sftpSession.Connect();
  135. }
  136. protected void Act()
  137. {
  138. _actual = _sftpSession.RequestStatVfs(_path);
  139. }
  140. [TestMethod]
  141. public void ReturnedValueShouldNotBeNull()
  142. {
  143. Assert.IsNotNull(_actual);
  144. }
  145. [TestMethod]
  146. public void AvailableBlocksInReturnedValueShouldMatchValueInSftpResponse()
  147. {
  148. Assert.AreEqual(_bAvail, _actual.AvailableBlocks);
  149. }
  150. }
  151. }