SftpSessionTest_Connected_RequestStatVfs.cs 7.6 KB

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