diff options
7 files changed, 63 insertions, 2 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs index 650c082b..9819209f 100644 --- a/src/slskd/Common/FileSafety.cs +++ b/src/slskd/Common/FileSafety.cs @@ -323,6 +323,11 @@ public static class FileSafety /// <returns>The converted path.</returns> public static string LocalizePath(string path, OSPlatform? os = null) { + if (path is null) + { + return null; + } + os ??= Compute.OSPlatform(); var sep = os.Value == OSPlatform.Windows ? '\\' : '/'; @@ -340,6 +345,11 @@ public static class FileSafety /// <returns>The sanitized filename.</returns> public static string SanitizeFilename(string filename, char replacement = '_', OSPlatform? os = null) { + if (filename is null) + { + return null; + } + if (replacement is '/' or '\\') { throw new ArgumentException($"The provided replacement character '{replacement}' is invalid in filenames"); @@ -412,6 +422,11 @@ public static class FileSafety /// <returns>The sanitized path.</returns> public static string SanitizePath(string path, char replacement = '_', bool retainRoot = false, OSPlatform? os = null) { + if (path is null) + { + return null; + } + os ??= Compute.OSPlatform(); var sep = os.Value == OSPlatform.Windows ? '\\' : '/'; @@ -421,7 +436,7 @@ public static class FileSafety if (!retainRoot) { // strip C:\ or //server, if present (regardless of slash variant, etc) - path = StripPathRoot(path); + path = StripPathRoot(path, os); } // for each segment, drop nulls (created by double slashes), sanitize, and replace traversal strings @@ -443,6 +458,11 @@ public static class FileSafety /// <returns>The path with the root stripped, if one was present.</returns> public static string StripPathRoot(string path, OSPlatform? os = null) { + if (path is null) + { + return null; + } + // flip slashes the correct way path = LocalizePath(path, os); diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/GetDirectoryNameSafelyTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/GetDirectoryNameSafelyTests.cs index ff64e90d..3d89e9f5 100644 --- a/tests/slskd.Tests.Unit/Common/FileSafety/GetDirectoryNameSafelyTests.cs +++ b/tests/slskd.Tests.Unit/Common/FileSafety/GetDirectoryNameSafelyTests.cs @@ -112,7 +112,7 @@ public partial class FileSafetyTests [InlineData("\\\\server\\file.txt", "\\\\server")] [InlineData("@@abcde/file.txt", "@@abcde")] [InlineData("@@abcde\\file.txt", "@@abcde")] - public void Returns_Null_When_File_Is_Directly_In_Root_With_RetainRoot_True_Windows(string input, string expected) + public void Returns_Sanitized_Root_When_File_Is_Directly_In_Root_With_RetainRoot_True_Windows(string input, string expected) { var result = FileSafety.GetDirectoryNameSafely(input, retainRoot: true, os: OSPlatform.Windows); diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/LocalizePathTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/LocalizePathTests.cs index 160f2a72..0b3eb81e 100644 --- a/tests/slskd.Tests.Unit/Common/FileSafety/LocalizePathTests.cs +++ b/tests/slskd.Tests.Unit/Common/FileSafety/LocalizePathTests.cs @@ -54,5 +54,13 @@ public partial class FileSafetyTests Assert.False(string.IsNullOrEmpty(result)); } + + [Fact] + public void Returns_Null_Given_Null_Path() + { + var result = FileSafety.LocalizePath(null); + + Assert.Null(result); + } } } diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/SanitizeFilenameTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/SanitizeFilenameTests.cs index 34e969d4..c0334d9b 100644 --- a/tests/slskd.Tests.Unit/Common/FileSafety/SanitizeFilenameTests.cs +++ b/tests/slskd.Tests.Unit/Common/FileSafety/SanitizeFilenameTests.cs @@ -152,5 +152,13 @@ public partial class FileSafetyTests Assert.Equal(expected, result); } + + [Fact] + public void Returns_Null_Given_Null() + { + var result = FileSafety.SanitizeFilename(null); + + Assert.Null(result); + } } } diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/SanitizePathSegmentTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/SanitizePathSegmentTests.cs index 27dcf925..d322179b 100644 --- a/tests/slskd.Tests.Unit/Common/FileSafety/SanitizePathSegmentTests.cs +++ b/tests/slskd.Tests.Unit/Common/FileSafety/SanitizePathSegmentTests.cs @@ -95,5 +95,13 @@ public partial class FileSafetyTests Assert.Equal(expected, result); } + + [Fact] + public void Returns_Null_Given_Null() + { + var result = FileSafety.SanitizePathSegment(null); + + Assert.Null(result); + } } } diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/SanitizePathTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/SanitizePathTests.cs index 2f712334..92a9e62c 100644 --- a/tests/slskd.Tests.Unit/Common/FileSafety/SanitizePathTests.cs +++ b/tests/slskd.Tests.Unit/Common/FileSafety/SanitizePathTests.cs @@ -163,5 +163,14 @@ public partial class FileSafetyTests Assert.Equal("foo/-/bar", result); } + + [Fact] + public void Returns_Null_Given_Null_Path() + { + var result = FileSafety.SanitizePath(null); + + Assert.Null(result); + } + } } diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/StripPathRootTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/StripPathRootTests.cs index f3638c9e..1ec2021d 100644 --- a/tests/slskd.Tests.Unit/Common/FileSafety/StripPathRootTests.cs +++ b/tests/slskd.Tests.Unit/Common/FileSafety/StripPathRootTests.cs @@ -15,6 +15,14 @@ public partial class FileSafetyTests Assert.NotNull(result); } + [Fact] + public void Returns_Null_Given_Null_Path() + { + var result = FileSafety.StripPathRoot(null); + + Assert.Null(result); + } + [Theory] [InlineData("C:\\Music\\Artist", "Music/Artist")] [InlineData("C:/Music/Artist", "Music/Artist")] |
