Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// SOFTWARE.

using DemaConsulting.BuildMark.RepoConnectors;
using NSubstitute;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change needed - it's the only change in the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in commit 25322e8. The using statement was added during test development but ended up not being needed.

using Octokit;

namespace DemaConsulting.BuildMark.Tests;
Expand Down
176 changes: 176 additions & 0 deletions test/DemaConsulting.BuildMark.Tests/PathHelpersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// Copyright (c) DEMA Consulting
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please examine https://github.com/demaconsulting/SarifMark/blob/main/test/DemaConsulting.SarifMark.Tests/PathHelpersTests.cs which tests SafePathCombine but doesn't need to do the inconclusive tests for different operating systems. Please use its approach instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored in commit 25322e8 to use the cleaner Assert.Throws pattern from SarifMark. Combined Unix/Windows absolute path tests into a single method with conditional Windows check, eliminating the need for inconclusive tests.

//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

namespace DemaConsulting.BuildMark.Tests;

/// <summary>
/// Tests for the PathHelpers class.
/// </summary>
[TestClass]
public class PathHelpersTests
{
/// <summary>
/// Test that SafePathCombine correctly combines valid paths.
/// </summary>
[TestMethod]
public void PathHelpers_SafePathCombine_ValidPaths_CombinesCorrectly()
{
// Arrange
var basePath = "/home/user/project";
var relativePath = "subfolder/file.txt";

// Act
var result = PathHelpers.SafePathCombine(basePath, relativePath);

// Assert
Assert.AreEqual(Path.Combine(basePath, relativePath), result);
}

/// <summary>
/// Test that SafePathCombine throws ArgumentException for path traversal with double dots.
/// </summary>
[TestMethod]
public void PathHelpers_SafePathCombine_PathTraversalWithDoubleDots_ThrowsArgumentException()
{
// Arrange
var basePath = "/home/user/project";
var relativePath = "../etc/passwd";

ArgumentException? caughtException = null;

try
{
// Act
PathHelpers.SafePathCombine(basePath, relativePath);

// Assert - Fail if no exception is thrown
Assert.Fail("Expected ArgumentException to be thrown");
}
catch (ArgumentException ex)
{
// Store exception for verification
caughtException = ex;
}

// Assert - Verify exception
Assert.IsNotNull(caughtException);
Assert.Contains("Invalid path component", caughtException.Message);
}

/// <summary>
/// Test that SafePathCombine throws ArgumentException for rooted path.
/// </summary>
[TestMethod]
public void PathHelpers_SafePathCombine_RootedPath_ThrowsArgumentException()
{
// Arrange
var basePath = "/home/user/project";
var relativePath = "/etc/passwd";

ArgumentException? caughtException = null;

try
{
// Act
PathHelpers.SafePathCombine(basePath, relativePath);

// Assert - Fail if no exception is thrown
Assert.Fail("Expected ArgumentException to be thrown");
}
catch (ArgumentException ex)
{
// Store exception for verification
caughtException = ex;
}

// Assert - Verify exception
Assert.IsNotNull(caughtException);
Assert.Contains("Invalid path component", caughtException.Message);
}

/// <summary>
/// Test that SafePathCombine throws ArgumentException for Windows rooted path.
/// </summary>
[TestMethod]
public void PathHelpers_SafePathCombine_WindowsRootedPath_ThrowsArgumentException()
{
// Skip test on non-Windows platforms where Windows paths are not considered rooted
if (!OperatingSystem.IsWindows())
{
Assert.Inconclusive("Test only applies on Windows");
return;
}

// Arrange
var basePath = "C:\\Users\\project";
var relativePath = "C:\\Windows\\System32\\file.txt";

ArgumentException? caughtException = null;

try
{
// Act
PathHelpers.SafePathCombine(basePath, relativePath);

// Assert - Fail if no exception is thrown
Assert.Fail("Expected ArgumentException to be thrown");
}
catch (ArgumentException ex)
{
// Store exception for verification
caughtException = ex;
}

// Assert - Verify exception
Assert.IsNotNull(caughtException);
Assert.Contains("Invalid path component", caughtException.Message);
}

/// <summary>
/// Test that SafePathCombine throws ArgumentException for path with double dots in middle.
/// </summary>
[TestMethod]
public void PathHelpers_SafePathCombine_DoubleDotsInMiddle_ThrowsArgumentException()
{
// Arrange
var basePath = "/home/user/project";
var relativePath = "subfolder/../../../etc/passwd";

ArgumentException? caughtException = null;

try
{
// Act
PathHelpers.SafePathCombine(basePath, relativePath);

// Assert - Fail if no exception is thrown
Assert.Fail("Expected ArgumentException to be thrown");
}
catch (ArgumentException ex)
{
// Store exception for verification
caughtException = ex;
}

// Assert - Verify exception
Assert.IsNotNull(caughtException);
Assert.Contains("Invalid path component", caughtException.Message);
}
}
Loading