SftpClientTest.UploadFile.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Logging.Abstractions;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. using Moq;
  10. using Renci.SshNet.Channels;
  11. using Renci.SshNet.Common;
  12. using Renci.SshNet.Connection;
  13. using Renci.SshNet.Messages;
  14. using Renci.SshNet.Messages.Authentication;
  15. using Renci.SshNet.Messages.Connection;
  16. using Renci.SshNet.Sftp;
  17. using Renci.SshNet.Sftp.Responses;
  18. namespace Renci.SshNet.Tests.Classes
  19. {
  20. public partial class SftpClientTest
  21. {
  22. [TestMethod]
  23. public void UploadFile_ObservesErrorResponses()
  24. {
  25. // A regression test for UploadFile hanging instead of observing
  26. // error responses from the server.
  27. // https://github.com/sshnet/SSH.NET/issues/957
  28. var serviceFactoryMock = new Mock<IServiceFactory>();
  29. var connInfo = new PasswordConnectionInfo("host", "user", "pwd");
  30. var session = new MySession(connInfo);
  31. var concreteServiceFactory = new ServiceFactory();
  32. serviceFactoryMock
  33. .Setup(p => p.CreateSession(It.IsAny<ConnectionInfo>(), It.IsAny<ISocketFactory>()))
  34. .Returns(session);
  35. serviceFactoryMock
  36. .Setup(p => p.CreateSftpResponseFactory())
  37. .Returns(concreteServiceFactory.CreateSftpResponseFactory);
  38. serviceFactoryMock
  39. .Setup(p => p.CreateSftpSession(session, It.IsAny<int>(), It.IsAny<Encoding>(), It.IsAny<ISftpResponseFactory>()))
  40. .Returns(concreteServiceFactory.CreateSftpSession);
  41. using var client = new SftpClient(connInfo, false, serviceFactoryMock.Object);
  42. client.Connect();
  43. Assert.Throws<SftpPermissionDeniedException>(() => client.UploadFile(
  44. new OneByteStream(new MemoryStream("Hello World"u8.ToArray())),
  45. "path.txt"));
  46. }
  47. #pragma warning disable IDE0022 // Use block body for method
  48. #pragma warning disable IDE0025 // Use block body for property
  49. #pragma warning disable IDE0027 // Use block body for accessor
  50. #pragma warning disable CS0067 // event is unused
  51. private class MySession(ConnectionInfo connectionInfo) : ISession
  52. {
  53. public IConnectionInfo ConnectionInfo => connectionInfo;
  54. public event EventHandler<MessageEventArgs<ChannelCloseMessage>> ChannelCloseReceived;
  55. public event EventHandler<MessageEventArgs<ChannelDataMessage>> ChannelDataReceived;
  56. public event EventHandler<MessageEventArgs<ChannelEofMessage>> ChannelEofReceived;
  57. public event EventHandler<MessageEventArgs<ChannelExtendedDataMessage>> ChannelExtendedDataReceived;
  58. public event EventHandler<MessageEventArgs<ChannelFailureMessage>> ChannelFailureReceived;
  59. public event EventHandler<MessageEventArgs<ChannelOpenConfirmationMessage>> ChannelOpenConfirmationReceived;
  60. public event EventHandler<MessageEventArgs<ChannelOpenFailureMessage>> ChannelOpenFailureReceived;
  61. public event EventHandler<MessageEventArgs<ChannelOpenMessage>> ChannelOpenReceived;
  62. public event EventHandler<MessageEventArgs<ChannelRequestMessage>> ChannelRequestReceived;
  63. public event EventHandler<MessageEventArgs<ChannelSuccessMessage>> ChannelSuccessReceived;
  64. public event EventHandler<MessageEventArgs<ChannelWindowAdjustMessage>> ChannelWindowAdjustReceived;
  65. public event EventHandler<EventArgs> Disconnected;
  66. public event EventHandler<ExceptionEventArgs> ErrorOccured;
  67. public event EventHandler<SshIdentificationEventArgs> ServerIdentificationReceived;
  68. public event EventHandler<HostKeyEventArgs> HostKeyReceived;
  69. public event EventHandler<MessageEventArgs<RequestSuccessMessage>> RequestSuccessReceived;
  70. public event EventHandler<MessageEventArgs<RequestFailureMessage>> RequestFailureReceived;
  71. public event EventHandler<MessageEventArgs<BannerMessage>> UserAuthenticationBannerReceived;
  72. private uint _numRequests;
  73. private int _numWriteRequests;
  74. public void SendMessage(Message message)
  75. {
  76. // Initialisation sequence for SFTP session
  77. if (message is ChannelOpenMessage)
  78. {
  79. ChannelOpenConfirmationReceived?.Invoke(
  80. this,
  81. new MessageEventArgs<ChannelOpenConfirmationMessage>(
  82. new ChannelOpenConfirmationMessage(0, int.MaxValue, int.MaxValue, 0)));
  83. }
  84. else if (message is ChannelRequestMessage)
  85. {
  86. ChannelSuccessReceived?.Invoke(
  87. this,
  88. new MessageEventArgs<ChannelSuccessMessage>(new ChannelSuccessMessage(0)));
  89. }
  90. else if (message is ChannelDataMessage dataMsg)
  91. {
  92. if (dataMsg.Data[sizeof(uint)] == (byte)SftpMessageTypes.Init)
  93. {
  94. ChannelDataReceived?.Invoke(
  95. this,
  96. new MessageEventArgs<ChannelDataMessage>(
  97. new ChannelDataMessage(0, new SftpVersionResponse() { Version = 3 }.GetBytes())));
  98. }
  99. else if (dataMsg.Data[sizeof(uint)] == (byte)SftpMessageTypes.RealPath)
  100. {
  101. ChannelDataReceived?.Invoke(
  102. this,
  103. new MessageEventArgs<ChannelDataMessage>(
  104. new ChannelDataMessage(0,
  105. new SftpNameResponse(3, Encoding.UTF8)
  106. {
  107. ResponseId = ++_numRequests,
  108. Files = [new("thepath", new SftpFileAttributes(default, default, default, default, default, default, default))]
  109. }.GetBytes())));
  110. }
  111. else if (dataMsg.Data[sizeof(uint)] == (byte)SftpMessageTypes.Open)
  112. {
  113. ChannelDataReceived?.Invoke(
  114. this,
  115. new MessageEventArgs<ChannelDataMessage>(
  116. new ChannelDataMessage(0,
  117. new SftpHandleResponse(3)
  118. {
  119. ResponseId = ++_numRequests,
  120. Handle = "file"u8.ToArray()
  121. }.GetBytes())));
  122. }
  123. // --------- The actual interesting part of all of this ---------
  124. //
  125. else if (dataMsg.Data[sizeof(uint)] == (byte)SftpMessageTypes.Write)
  126. {
  127. // Fail the 5th write request
  128. var statusCode = ++_numWriteRequests == 5 ? StatusCode.PermissionDenied : StatusCode.Ok;
  129. var responseId = ++_numRequests;
  130. // Dispatch the responses on a different thread to simulate reality.
  131. _ = Task.Run(() =>
  132. {
  133. ChannelDataReceived?.Invoke(
  134. this,
  135. new MessageEventArgs<ChannelDataMessage>(
  136. new ChannelDataMessage(0,
  137. new SftpStatusResponse(3)
  138. {
  139. ResponseId = responseId,
  140. StatusCode = statusCode
  141. }.GetBytes())));
  142. });
  143. }
  144. //
  145. // --------------------------------------------------------------
  146. }
  147. }
  148. public bool IsConnected => false;
  149. public SemaphoreSlim SessionSemaphore { get; } = new(1);
  150. public IChannelSession CreateChannelSession() => new ChannelSession(this, 0, int.MaxValue, int.MaxValue);
  151. public WaitHandle MessageListenerCompleted => throw new NotImplementedException();
  152. public ILoggerFactory SessionLoggerFactory => NullLoggerFactory.Instance;
  153. public void Connect()
  154. {
  155. }
  156. public Task ConnectAsync(CancellationToken cancellationToken) => throw new NotImplementedException();
  157. public IChannelDirectTcpip CreateChannelDirectTcpip() => throw new NotImplementedException();
  158. public IChannelForwardedTcpip CreateChannelForwardedTcpip(uint remoteChannelNumber, uint remoteWindowSize, uint remoteChannelDataPacketSize)
  159. => throw new NotImplementedException();
  160. public void Dispose()
  161. {
  162. }
  163. public void OnDisconnecting()
  164. {
  165. }
  166. public void Disconnect() => throw new NotImplementedException();
  167. public void RegisterMessage(string messageName) => throw new NotImplementedException();
  168. public bool TrySendMessage(Message message) => throw new NotImplementedException();
  169. public WaitResult TryWait(WaitHandle waitHandle, TimeSpan timeout, out Exception exception) => throw new NotImplementedException();
  170. public WaitResult TryWait(WaitHandle waitHandle, TimeSpan timeout) => throw new NotImplementedException();
  171. public void UnRegisterMessage(string messageName) => throw new NotImplementedException();
  172. public void WaitOnHandle(WaitHandle waitHandle)
  173. {
  174. }
  175. public void WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout) => throw new NotImplementedException();
  176. }
  177. private class OneByteStream : Stream
  178. {
  179. private readonly Stream _stream;
  180. public OneByteStream(Stream stream)
  181. {
  182. _stream = stream;
  183. }
  184. public override bool CanRead => _stream.CanRead;
  185. public override bool CanSeek => throw new NotImplementedException();
  186. public override bool CanWrite => throw new NotImplementedException();
  187. public override long Length => _stream.Length;
  188. public override long Position
  189. {
  190. get => throw new NotImplementedException();
  191. set => throw new NotImplementedException();
  192. }
  193. public override void Flush() => throw new NotImplementedException();
  194. public override int Read(byte[] buffer, int offset, int count)
  195. {
  196. return _stream.Read(buffer, offset, Math.Min(1, count));
  197. }
  198. public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException();
  199. public override void SetLength(long value) => throw new NotImplementedException();
  200. public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException();
  201. }
  202. }
  203. }