SftpClientTest.Download.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. [Description("Test passing null to BeginDownloadFile")]
  32. public void Test_Sftp_BeginDownloadFile_StreamIsNull()
  33. {
  34. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  35. {
  36. sftp.Connect();
  37. Assert.ThrowsException<ArgumentNullException>(() => sftp.BeginDownloadFile("aaaa", null, null, null));
  38. }
  39. }
  40. [TestMethod]
  41. [TestCategory("Sftp")]
  42. [Description("Test passing null to BeginDownloadFile")]
  43. public void Test_Sftp_BeginDownloadFile_FileNameIsWhiteSpace()
  44. {
  45. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  46. {
  47. sftp.Connect();
  48. Assert.ThrowsException<ArgumentException>(() => sftp.BeginDownloadFile(" ", Stream.Null, null, null));
  49. }
  50. }
  51. [TestMethod]
  52. [TestCategory("Sftp")]
  53. [Description("Test passing null to BeginDownloadFile")]
  54. public void Test_Sftp_BeginDownloadFile_FileNameIsNull()
  55. {
  56. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  57. {
  58. sftp.Connect();
  59. Assert.ThrowsException<ArgumentNullException>(() => sftp.BeginDownloadFile(null, Stream.Null, null, null));
  60. }
  61. }
  62. [TestMethod]
  63. [TestCategory("Sftp")]
  64. public void Test_Sftp_EndDownloadFile_Invalid_Async_Handle()
  65. {
  66. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  67. {
  68. sftp.Connect();
  69. var filename = Path.GetTempFileName();
  70. CreateTestFile(filename, 1);
  71. sftp.UploadFile(File.OpenRead(filename), "test123");
  72. var async1 = sftp.BeginListDirectory("/", null, null);
  73. var async2 = sftp.BeginDownloadFile("test123", new MemoryStream(), null, null);
  74. Assert.ThrowsException<ArgumentException>(() => sftp.EndDownloadFile(async1));
  75. }
  76. }
  77. }
  78. }