SftpClientTest.Download.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Renci.SshNet.Common;
  3. using Renci.SshNet.Tests.Common;
  4. using Renci.SshNet.Tests.Properties;
  5. using System;
  6. using System.IO;
  7. namespace Renci.SshNet.Tests.Classes
  8. {
  9. /// <summary>
  10. /// Implementation of the SSH File Transfer Protocol (SFTP) over SSH.
  11. /// </summary>
  12. public partial class SftpClientTest : TestBase
  13. {
  14. [TestMethod]
  15. [TestCategory("Sftp")]
  16. [ExpectedException(typeof(SftpPermissionDeniedException))]
  17. public void Test_Sftp_Download_Forbidden()
  18. {
  19. if (Resources.USERNAME == "root")
  20. Assert.Fail("Must not run this test as root!");
  21. using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  22. {
  23. sftp.Connect();
  24. string remoteFileName = "/root/.profile";
  25. using (var ms = new MemoryStream())
  26. {
  27. sftp.DownloadFile(remoteFileName, ms);
  28. }
  29. sftp.Disconnect();
  30. }
  31. }
  32. [TestMethod]
  33. [TestCategory("Sftp")]
  34. [ExpectedException(typeof(SftpPathNotFoundException))]
  35. public void Test_Sftp_Download_File_Not_Exists()
  36. {
  37. using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  38. {
  39. sftp.Connect();
  40. string remoteFileName = "/xxx/eee/yyy";
  41. using (var ms = new MemoryStream())
  42. {
  43. sftp.DownloadFile(remoteFileName, ms);
  44. }
  45. sftp.Disconnect();
  46. }
  47. }
  48. [TestMethod]
  49. [TestCategory("Sftp")]
  50. [Description("Test passing null to BeginDownloadFile")]
  51. [ExpectedException(typeof(ArgumentNullException))]
  52. public void Test_Sftp_BeginDownloadFile_StreamIsNull()
  53. {
  54. using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  55. {
  56. sftp.Connect();
  57. sftp.BeginDownloadFile("aaaa", null, null, null);
  58. sftp.Disconnect();
  59. }
  60. }
  61. [TestMethod]
  62. [TestCategory("Sftp")]
  63. [Description("Test passing null to BeginDownloadFile")]
  64. [ExpectedException(typeof(ArgumentException))]
  65. public void Test_Sftp_BeginDownloadFile_FileNameIsWhiteSpace()
  66. {
  67. using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  68. {
  69. sftp.Connect();
  70. sftp.BeginDownloadFile(" ", new MemoryStream(), null, null);
  71. sftp.Disconnect();
  72. }
  73. }
  74. [TestMethod]
  75. [TestCategory("Sftp")]
  76. [Description("Test passing null to BeginDownloadFile")]
  77. [ExpectedException(typeof(ArgumentException))]
  78. public void Test_Sftp_BeginDownloadFile_FileNameIsNull()
  79. {
  80. using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  81. {
  82. sftp.Connect();
  83. sftp.BeginDownloadFile(null, new MemoryStream(), null, null);
  84. sftp.Disconnect();
  85. }
  86. }
  87. [TestMethod]
  88. [TestCategory("Sftp")]
  89. [ExpectedException(typeof(ArgumentException))]
  90. public void Test_Sftp_EndDownloadFile_Invalid_Async_Handle()
  91. {
  92. using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  93. {
  94. sftp.Connect();
  95. var filename = Path.GetTempFileName();
  96. this.CreateTestFile(filename, 1);
  97. sftp.UploadFile(File.OpenRead(filename), "test123");
  98. var async1 = sftp.BeginListDirectory("/", null, null);
  99. var async2 = sftp.BeginDownloadFile("test123", new MemoryStream(), null, null);
  100. sftp.EndDownloadFile(async1);
  101. }
  102. }
  103. }
  104. }