SftpClientTest.Download.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. [ExpectedException(typeof(SftpPermissionDeniedException))]
  12. public void Test_Sftp_Download_Forbidden()
  13. {
  14. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, AdminUser.UserName, AdminUser.Password))
  15. {
  16. sftp.Connect();
  17. string remoteFileName = "/root/.profile";
  18. using (var ms = new MemoryStream())
  19. {
  20. sftp.DownloadFile(remoteFileName, ms);
  21. }
  22. sftp.Disconnect();
  23. }
  24. }
  25. [TestMethod]
  26. [TestCategory("Sftp")]
  27. [ExpectedException(typeof(SftpPathNotFoundException))]
  28. public void Test_Sftp_Download_File_Not_Exists()
  29. {
  30. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  31. {
  32. sftp.Connect();
  33. string remoteFileName = "/xxx/eee/yyy";
  34. using (var ms = new MemoryStream())
  35. {
  36. sftp.DownloadFile(remoteFileName, ms);
  37. }
  38. sftp.Disconnect();
  39. }
  40. }
  41. [TestMethod]
  42. [TestCategory("Sftp")]
  43. [Description("Test passing null to BeginDownloadFile")]
  44. [ExpectedException(typeof(ArgumentNullException))]
  45. public void Test_Sftp_BeginDownloadFile_StreamIsNull()
  46. {
  47. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  48. {
  49. sftp.Connect();
  50. sftp.BeginDownloadFile("aaaa", null, null, null);
  51. sftp.Disconnect();
  52. }
  53. }
  54. [TestMethod]
  55. [TestCategory("Sftp")]
  56. [Description("Test passing null to BeginDownloadFile")]
  57. [ExpectedException(typeof(ArgumentException))]
  58. public void Test_Sftp_BeginDownloadFile_FileNameIsWhiteSpace()
  59. {
  60. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  61. {
  62. sftp.Connect();
  63. sftp.BeginDownloadFile(" ", new MemoryStream(), null, null);
  64. sftp.Disconnect();
  65. }
  66. }
  67. [TestMethod]
  68. [TestCategory("Sftp")]
  69. [Description("Test passing null to BeginDownloadFile")]
  70. [ExpectedException(typeof(ArgumentNullException))]
  71. public void Test_Sftp_BeginDownloadFile_FileNameIsNull()
  72. {
  73. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  74. {
  75. sftp.Connect();
  76. sftp.BeginDownloadFile(null, new MemoryStream(), null, null);
  77. sftp.Disconnect();
  78. }
  79. }
  80. [TestMethod]
  81. [TestCategory("Sftp")]
  82. [ExpectedException(typeof(ArgumentException))]
  83. public void Test_Sftp_EndDownloadFile_Invalid_Async_Handle()
  84. {
  85. using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  86. {
  87. sftp.Connect();
  88. var filename = Path.GetTempFileName();
  89. CreateTestFile(filename, 1);
  90. sftp.UploadFile(File.OpenRead(filename), "test123");
  91. var async1 = sftp.BeginListDirectory("/", null, null);
  92. var async2 = sftp.BeginDownloadFile("test123", new MemoryStream(), null, null);
  93. sftp.EndDownloadFile(async1);
  94. }
  95. }
  96. }
  97. }