-
Notifications
You must be signed in to change notification settings - Fork 0
Increase code coverage for PathHelpers and Validation #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| // Copyright (c) DEMA Consulting | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.