Skip to content

Commit cd4d0ea

Browse files
robkeimfgreinacher
authored andcommitted
Throw ArgumentException when path contains invalid characters (#370)
Fixes #367
1 parent 7a85165 commit cd4d0ea

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,5 +277,20 @@ public void MockFileSystem_AddDirectory_TrailingSlashAllowedButNotRequired(strin
277277

278278
Assert.IsTrue(fileSystem.FileExists(path2));
279279
}
280+
281+
[Test]
282+
public void MockFileSystem_GetFiles_ThrowsArgumentExceptionForInvalidCharacters()
283+
{
284+
// Arrange
285+
const string path = @"c:\";
286+
var fileSystem = new MockFileSystem();
287+
fileSystem.AddDirectory(XFS.Path(path));
288+
289+
// Act
290+
TestDelegate getFilesWithInvalidCharacterInPath = () => fileSystem.Directory.GetFiles($"{path}{'\0'}.txt");
291+
292+
// Assert
293+
Assert.Throws<ArgumentException>(getFilesWithInvalidCharacterInPath);
294+
}
280295
}
281296
}

System.IO.Abstractions.TestingHelpers/MockDirectory.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public override bool Exists(string path)
9797

9898
try
9999
{
100-
101100
path = path.TrimSlashes();
102101
path = mockFileDataAccessor.Path.GetFullPath(path);
103102
return mockFileDataAccessor.AllDirectories.Any(p => p.Equals(path, StringComparison.OrdinalIgnoreCase));
@@ -176,8 +175,15 @@ public override string[] GetFiles(string path, string searchPattern)
176175

177176
public override string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
178177
{
179-
if(path == null)
180-
throw new ArgumentNullException();
178+
if (path == null)
179+
{
180+
throw new ArgumentNullException(nameof(path));
181+
}
182+
183+
if (path.Any(c => Path.GetInvalidPathChars().Contains(c)))
184+
{
185+
throw new ArgumentException("Invalid character(s) in path", nameof(path));
186+
}
181187

182188
if (!Exists(path))
183189
{

0 commit comments

Comments
 (0)