SftpClientTest.Download.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Renci.SshNet.Common;
  2. namespace Renci.SshNet.IntegrationTests.OldIntegrationTests
  3. {
  4. /// <summary>
  5. /// Implementation of the SSH File Transfer Protocol (SFTP) over SSH.
  6. /// </summary>
  7. public partial class SftpClientTest : IntegrationTestBase
  8. {
  9. [TestMethod]
  10. [TestCategory("Sftp")]
  11. public void Test_Sftp_Download_Forbidden()
  12. {
  13. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, AdminUser.UserName, AdminUser.Password))
  14. {
  15. sftp.Connect();
  16. Assert.ThrowsException<SftpPermissionDeniedException>(() => sftp.DownloadFile("/root/.profile", Stream.Null));
  17. }
  18. }
  19. [TestMethod]
  20. [TestCategory("Sftp")]
  21. public void Test_Sftp_Download_File_Not_Exists()
  22. {
  23. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  24. {
  25. sftp.Connect();
  26. Assert.ThrowsException<SftpPathNotFoundException>(() => sftp.DownloadFile("/xxx/eee/yyy", Stream.Null));
  27. }
  28. }
  29. [TestMethod]
  30. [TestCategory("Sftp")]
  31. public async Task Test_Sftp_DownloadAsync_Forbidden()
  32. {
  33. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, AdminUser.UserName, AdminUser.Password))
  34. {
  35. await sftp.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
  36. await Assert.ThrowsExceptionAsync<SftpPermissionDeniedException>(() => sftp.DownloadFileAsync("/root/.profile", Stream.Null));
  37. }
  38. }
  39. [TestMethod]
  40. [TestCategory("Sftp")]
  41. public async Task Test_Sftp_DownloadAsync_File_Not_Exists()
  42. {
  43. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  44. {
  45. await sftp.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
  46. await Assert.ThrowsExceptionAsync<SftpPathNotFoundException>(() => sftp.DownloadFileAsync("/xxx/eee/yyy", Stream.Null));
  47. }
  48. }
  49. [TestMethod]
  50. [TestCategory("Sftp")]
  51. public async Task Test_Sftp_DownloadAsync_Cancellation_Requested()
  52. {
  53. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  54. {
  55. await sftp.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
  56. var cancelledToken = new CancellationToken(true);
  57. await Assert.ThrowsExceptionAsync<OperationCanceledException>(() => sftp.DownloadFileAsync("/xxx/eee/yyy", Stream.Null, cancelledToken));
  58. }
  59. }
  60. [TestMethod]
  61. [TestCategory("Sftp")]
  62. [Description("Test passing null to BeginDownloadFile")]
  63. public void Test_Sftp_BeginDownloadFile_StreamIsNull()
  64. {
  65. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  66. {
  67. sftp.Connect();
  68. Assert.ThrowsException<ArgumentNullException>(() => sftp.BeginDownloadFile("aaaa", null, null, null));
  69. }
  70. }
  71. [TestMethod]
  72. [TestCategory("Sftp")]
  73. [Description("Test passing null to BeginDownloadFile")]
  74. public void Test_Sftp_BeginDownloadFile_FileNameIsWhiteSpace()
  75. {
  76. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  77. {
  78. sftp.Connect();
  79. Assert.ThrowsException<ArgumentException>(() => sftp.BeginDownloadFile(" ", Stream.Null, null, null));
  80. }
  81. }
  82. [TestMethod]
  83. [TestCategory("Sftp")]
  84. [Description("Test passing null to BeginDownloadFile")]
  85. public void Test_Sftp_BeginDownloadFile_FileNameIsNull()
  86. {
  87. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  88. {
  89. sftp.Connect();
  90. Assert.ThrowsException<ArgumentNullException>(() => sftp.BeginDownloadFile(null, Stream.Null, null, null));
  91. }
  92. }
  93. [TestMethod]
  94. [TestCategory("Sftp")]
  95. public void Test_Sftp_EndDownloadFile_Invalid_Async_Handle()
  96. {
  97. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  98. {
  99. sftp.Connect();
  100. var filename = Path.GetTempFileName();
  101. CreateTestFile(filename, 1);
  102. sftp.UploadFile(File.OpenRead(filename), "test123");
  103. var async1 = sftp.BeginListDirectory("/", null, null);
  104. var async2 = sftp.BeginDownloadFile("test123", new MemoryStream(), null, null);
  105. Assert.ThrowsException<ArgumentException>(() => sftp.EndDownloadFile(async1));
  106. }
  107. }
  108. }
  109. }