PosixPathTest_CreateAbsoluteOrRelativeFilePath.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Renci.SshNet.Common;
  4. using Renci.SshNet.Tests.Common;
  5. namespace Renci.SshNet.Tests.Classes.Common
  6. {
  7. [TestClass]
  8. public class PosixPathTest_CreateAbsoluteOrRelativeFilePath
  9. {
  10. [TestMethod]
  11. public void Path_Null()
  12. {
  13. const string path = null;
  14. try
  15. {
  16. _ = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  17. Assert.Fail();
  18. }
  19. catch (ArgumentNullException ex)
  20. {
  21. Assert.IsNull(ex.InnerException);
  22. Assert.AreEqual("path", ex.ParamName);
  23. }
  24. }
  25. [TestMethod]
  26. public void Path_Empty()
  27. {
  28. var path = string.Empty;
  29. try
  30. {
  31. _ = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  32. Assert.Fail();
  33. }
  34. catch (ArgumentException ex)
  35. {
  36. Assert.IsNull(ex.InnerException);
  37. ArgumentExceptionAssert.MessageEquals("The path is a zero-length string.", ex);
  38. Assert.AreEqual("path", ex.ParamName);
  39. }
  40. }
  41. [TestMethod]
  42. public void Path_TrailingForwardSlash()
  43. {
  44. var path = "/abc/";
  45. var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  46. Assert.IsNotNull(actual);
  47. Assert.AreEqual("/abc", actual.Directory);
  48. Assert.IsNull(actual.File);
  49. }
  50. [TestMethod]
  51. public void Path_FileWithoutNoDirectory()
  52. {
  53. var path = "abc.log";
  54. var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  55. Assert.IsNotNull(actual);
  56. Assert.AreEqual(".", actual.Directory);
  57. Assert.AreSame(path, actual.File);
  58. }
  59. [TestMethod]
  60. public void Path_FileInRootDirectory()
  61. {
  62. var path = "/abc.log";
  63. var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  64. Assert.IsNotNull(actual);
  65. Assert.AreEqual("/", actual.Directory);
  66. Assert.AreEqual("abc.log", actual.File);
  67. }
  68. [TestMethod]
  69. public void Path_RootDirectoryOnly()
  70. {
  71. var path = "/";
  72. var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  73. Assert.IsNotNull(actual);
  74. Assert.AreEqual("/", actual.Directory);
  75. Assert.IsNull(actual.File);
  76. }
  77. [TestMethod]
  78. public void Path_FileInNonRootDirectory()
  79. {
  80. var path = "/home/sshnet/xyz";
  81. var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  82. Assert.IsNotNull(actual);
  83. Assert.AreEqual("/home/sshnet", actual.Directory);
  84. Assert.AreEqual("xyz", actual.File);
  85. }
  86. [TestMethod]
  87. public void Path_BackslashIsNotConsideredDirectorySeparator()
  88. {
  89. var path = "/home\\abc.log";
  90. var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  91. Assert.IsNotNull(actual);
  92. Assert.AreEqual("/", actual.Directory);
  93. Assert.AreEqual("home\\abc.log", actual.File);
  94. }
  95. [TestMethod]
  96. public void Path_ColonIsNotConsideredPathSeparator()
  97. {
  98. var path = "/home:abc.log";
  99. var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  100. Assert.IsNotNull(actual);
  101. Assert.AreEqual("/", actual.Directory);
  102. Assert.AreEqual("home:abc.log", actual.File);
  103. }
  104. [TestMethod]
  105. public void Path_LeadingWhitespace()
  106. {
  107. var path = " / \tabc";
  108. var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  109. Assert.IsNotNull(actual);
  110. Assert.AreEqual(" ", actual.Directory);
  111. Assert.AreEqual(" \tabc", actual.File);
  112. }
  113. [TestMethod]
  114. public void Path_TrailingWhitespace()
  115. {
  116. var path = "/abc \t ";
  117. var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  118. Assert.IsNotNull(actual);
  119. Assert.AreEqual("/", actual.Directory);
  120. Assert.AreEqual("abc \t ", actual.File);
  121. }
  122. [TestMethod]
  123. public void Path_OnlyWhitespace()
  124. {
  125. var path = " ";
  126. var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  127. Assert.IsNotNull(actual);
  128. Assert.AreEqual(".", actual.Directory);
  129. Assert.AreSame(path, actual.File);
  130. }
  131. [TestMethod]
  132. public void Path_FileNameOnlyWhitespace()
  133. {
  134. var path = "/home/\t ";
  135. var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
  136. Assert.IsNotNull(actual);
  137. Assert.AreEqual("/home", actual.Directory);
  138. Assert.AreEqual("\t ", actual.File);
  139. }
  140. }
  141. }