SftpClientTest.UploadFile.cs 11 KB

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