Skip to content

Commit

Permalink
#36: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chucker committed Jul 11, 2023
1 parent c628969 commit 78dd809
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions TRENZ.Docs.API.Test/PathTraversalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,46 @@ namespace TRENZ.Docs.API.Test;
public class PathTraversalTests
{
private const string MacOsRoot = "/var/folders/ab/cdefg/T/TRENZ.Docs.API/trenz-docs-test";
private const string DotRoot = "./Data";
private const string WindowsRoot = @"D:\www\docs.my.example";

[DataTestMethod]
[DataRow(MacOsRoot, "public/", $"{MacOsRoot}/public/")]
[DataRow(MacOsRoot, "./", $"{MacOsRoot}/./")]
public void TestAllowsPaths(string root, string path, string expectedFullRoot)
[DataRow(WindowsRoot, @"public\", @$"{WindowsRoot}\public\")]
public void TestResolvesPathAs(string root, string path, string expectedFullRoot)
{
var pathTraversalService = TestHelper.GetPathTraversalService();

Assert.AreEqual(expectedFullRoot, pathTraversalService.Traverse(root, path));
expectedFullRoot = NormalizeDirectorySeparatorChar(expectedFullRoot);

var traversedPath = pathTraversalService.Traverse(root, path);

traversedPath = NormalizeDirectorySeparatorChar(traversedPath);

Assert.AreEqual(expectedFullRoot, traversedPath);

static string NormalizeDirectorySeparatorChar(string expectedFullRoot)
{
foreach (var item in new[] { @"\", "/" })
expectedFullRoot = expectedFullRoot.Replace(item, Path.DirectorySeparatorChar.ToString());
return expectedFullRoot;
}
}

[DataTestMethod]
[DataRow(DotRoot, "./")]
public void TestDoesNotThrowForPath(string root, string path)
{
var pathTraversalService = TestHelper.GetPathTraversalService();

Assert.IsNotNull(pathTraversalService.Traverse(root, path));
}

[DataTestMethod]
[DataRow(MacOsRoot, "../")]
[DataRow(MacOsRoot, "/")]
public void TestThrowsForPaths(string root, string path)
public void TestThrowsForPath(string root, string path)
{
var pathTraversalService = TestHelper.GetPathTraversalService();

Expand Down

0 comments on commit 78dd809

Please sign in to comment.