SftpSessionTest_Connected_RequestStatVfs.cs 7.8 KB

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