SftpExceptionTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Renci.SshNet.Common;
  4. using Renci.SshNet.Sftp;
  5. namespace Renci.SshNet.Tests.Classes.Common
  6. {
  7. [TestClass]
  8. public class SftpExceptionTest
  9. {
  10. [TestMethod]
  11. public void StatusCodes()
  12. {
  13. Assert.AreEqual(StatusCode.BadMessage, new SftpException(StatusCode.BadMessage).StatusCode);
  14. Assert.AreEqual(StatusCode.OperationUnsupported, new SftpException(StatusCode.OperationUnsupported, null).StatusCode);
  15. Assert.AreEqual(StatusCode.Failure, new SftpException(StatusCode.Failure, null, null).StatusCode);
  16. Assert.AreEqual(StatusCode.PermissionDenied, new SftpPermissionDeniedException().StatusCode);
  17. Assert.AreEqual(StatusCode.PermissionDenied, new SftpPermissionDeniedException(null).StatusCode);
  18. Assert.AreEqual(StatusCode.PermissionDenied, new SftpPermissionDeniedException(null, null).StatusCode);
  19. Assert.AreEqual(StatusCode.NoSuchFile, new SftpPathNotFoundException().StatusCode);
  20. Assert.AreEqual(StatusCode.NoSuchFile, new SftpPathNotFoundException(null).StatusCode);
  21. Assert.AreEqual(StatusCode.NoSuchFile, new SftpPathNotFoundException(null, path: null).StatusCode);
  22. Assert.AreEqual(StatusCode.NoSuchFile, new SftpPathNotFoundException(null, innerException: null).StatusCode);
  23. Assert.AreEqual(StatusCode.NoSuchFile, new SftpPathNotFoundException(null, null, null).StatusCode);
  24. }
  25. [TestMethod]
  26. public void Message()
  27. {
  28. Assert.IsFalse(string.IsNullOrWhiteSpace(new SftpException(StatusCode.Failure).Message));
  29. Assert.IsFalse(string.IsNullOrWhiteSpace(new SftpException(StatusCode.Failure, "").Message));
  30. Assert.AreEqual("Custom message", new SftpException(StatusCode.Failure, "Custom message").Message);
  31. Assert.IsFalse(string.IsNullOrWhiteSpace(new SftpPermissionDeniedException().Message));
  32. Assert.IsFalse(string.IsNullOrWhiteSpace(new SftpPermissionDeniedException("").Message));
  33. Assert.IsFalse(string.IsNullOrWhiteSpace(new SftpPermissionDeniedException("", null).Message));
  34. Assert.AreEqual("Custom message1", new SftpPermissionDeniedException("Custom message1").Message);
  35. Assert.AreEqual("Custom message2", new SftpPermissionDeniedException("Custom message2", null).Message);
  36. Assert.IsFalse(string.IsNullOrWhiteSpace(new SftpPathNotFoundException().Message));
  37. Assert.IsFalse(string.IsNullOrWhiteSpace(new SftpPathNotFoundException("").Message));
  38. Assert.IsFalse(string.IsNullOrWhiteSpace(new SftpPathNotFoundException("", path: null).Message));
  39. Assert.AreEqual("Custom message1", new SftpPathNotFoundException("Custom message1").Message);
  40. Assert.AreEqual("Custom message2", new SftpPathNotFoundException("Custom message2", path: null).Message);
  41. Assert.AreEqual("Custom message2", new SftpPathNotFoundException("Custom message2", "path1").Message);
  42. Assert.AreEqual("Custom message3", new SftpPathNotFoundException("Custom message3", innerException: null).Message);
  43. Assert.AreEqual("Custom message4", new SftpPathNotFoundException("Custom message4", null, null).Message);
  44. }
  45. [TestMethod]
  46. public void PathNotFoundException_Path()
  47. {
  48. Assert.IsNull(new SftpPathNotFoundException().Path);
  49. Assert.IsNull(new SftpPathNotFoundException("message").Path);
  50. Assert.AreEqual("path1", new SftpPathNotFoundException("message", "path1").Path);
  51. Assert.AreEqual("path2", new SftpPathNotFoundException(null, "path2", null).Path);
  52. Assert.Contains("Path: 'path3'.", new SftpPathNotFoundException(message: null, path: "path3").Message, StringComparison.Ordinal);
  53. Assert.Contains("Path: 'path4'.", new SftpPathNotFoundException(message: "", path: "path4").Message, StringComparison.Ordinal);
  54. }
  55. }
  56. }