From 18234fd844cdf2b31b5cb3e39587524f97ddcc26 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 13 May 2026 20:40:44 -0500 Subject: add tests for extensions --- .../Extensions/ToLocalRelativeFilenameTests.cs | 143 ++++++++++++++++++--- 1 file changed, 125 insertions(+), 18 deletions(-) (limited to 'tests/slskd.Tests.Unit/Common/Extensions/ToLocalRelativeFilenameTests.cs') diff --git a/tests/slskd.Tests.Unit/Common/Extensions/ToLocalRelativeFilenameTests.cs b/tests/slskd.Tests.Unit/Common/Extensions/ToLocalRelativeFilenameTests.cs index 1012ddfa..78539142 100644 --- a/tests/slskd.Tests.Unit/Common/Extensions/ToLocalRelativeFilenameTests.cs +++ b/tests/slskd.Tests.Unit/Common/Extensions/ToLocalRelativeFilenameTests.cs @@ -1,6 +1,8 @@ namespace slskd.Tests.Unit.Common.Extensions { using System; + using System.IO; + using System.Linq; using Xunit; public class ToLocalRelativeFilenameTests @@ -18,36 +20,141 @@ namespace slskd.Tests.Unit.Common.Extensions } [Fact] - public void Returns_Localized_Filename_And_Parent_Directory() + public void Returns_Last_Directory_And_Filename_From_Deep_Path() { - if (Environment.OSVersion.Platform == PlatformID.Win32NT) + var expected = "path" + Path.DirectorySeparatorChar + "file.ext"; + Assert.Equal(expected, @"deeply\nested\path\file.ext".ToLocalRelativeFilename()); + } + + [Fact] + public void Returns_Just_Filename_If_No_Directory_Given() + { + Assert.Equal("file.ext", "file.ext".ToLocalRelativeFilename()); + } + + [Fact] + public void Replaces_Invalid_Characters_In_Filename() + { + // Exclude backslash and forward slash — they act as segment separators in the input + // and would cause the dirty string to be split rather than treated as a filename. + var invalidChars = Path.GetInvalidFileNameChars() + .Where(c => c != '\\' && c != '/') + .ToArray(); + + var dirtyName = new string(invalidChars); + var result = $@"a\b\{dirtyName}".ToLocalRelativeFilename(); + var filename = Path.GetFileName(result); + + Assert.All(invalidChars, c => + Assert.False(filename.Contains(c), $"Invalid character U+{(int)c:X4} was not replaced in filename")); + } + + [Fact] + public void Replaces_Invalid_Characters_In_Directory() + { + // Directory segments are sanitized with ReplaceInvalidFileNameCharacters, so the full + // GetInvalidFileNameChars() set applies (excluding path separators, which split the input). + var invalidChars = Path.GetInvalidFileNameChars() + .Where(c => c != '\\' && c != '/') + .ToArray(); + + var dirtyName = new string(invalidChars); + var result = $@"a\{dirtyName}\file.ext".ToLocalRelativeFilename(); + var directory = Path.GetDirectoryName(result); + + Assert.All(invalidChars, c => + Assert.False(directory.Contains(c), $"Invalid character U+{(int)c:X4} was not replaced in directory")); + } + + [Fact] + public void Replaces_Each_Invalid_Filename_Character_Individually() + { + // Tests each invalid filename char in isolation so that failures identify the exact + // offending character rather than only reporting that something in a concatenated + // string was not replaced. + var invalidChars = Path.GetInvalidFileNameChars() + .Where(c => c != '\\' && c != '/') + .ToArray(); + + Assert.All(invalidChars, c => { - Assert.Equal(@"path\file.ext", "deeply/nested/path/file.ext".ToLocalRelativeFilename()); - } - else + var result = $@"a\b\prefix{c}suffix.ext".ToLocalRelativeFilename(); + var filename = Path.GetFileName(result); + Assert.False(filename.Contains(c), $"Invalid character U+{(int)c:X4} was not replaced in filename"); + }); + } + + [Fact] + public void Replaces_Each_Invalid_Filename_Character_In_Directory_Individually() + { + // Tests each invalid filename char in the directory segment in isolation so that + // failures identify the exact offending character. + var invalidChars = Path.GetInvalidFileNameChars() + .Where(c => c != '\\' && c != '/') + .ToArray(); + + Assert.All(invalidChars, c => { - Assert.Equal(@"path/file.ext", @"C:\deeply\nested\path\file.ext".ToLocalRelativeFilename()); - } + var result = $@"a\prefix{c}suffix\file.ext".ToLocalRelativeFilename(); + var directory = Path.GetDirectoryName(result); + Assert.False(directory.Contains(c), $"Invalid character U+{(int)c:X4} was not replaced in directory"); + }); + } + [Fact] + public void Returns_Last_Directory_And_Filename_From_Two_Segment_Path() + { + var expected = "dir" + Path.DirectorySeparatorChar + "file.ext"; + Assert.Equal(expected, @"dir\file.ext".ToLocalRelativeFilename()); } [Fact] - public void Returns_Just_File_If_Only_File_Given() + public void Handles_Forward_Slash_As_Segment_Separator() { - Assert.Equal("file.ext", "file.ext".ToLocalRelativeFilename()); + // LocalizePath normalizes '/' to Path.DirectorySeparatorChar before splitting, + // so forward-slash paths must produce the same result as backslash paths. + var expected = "b" + Path.DirectorySeparatorChar + "c.ext"; + Assert.Equal(expected, "a/b/c.ext".ToLocalRelativeFilename()); } [Fact] - public void Removes_Invalid_Characters_From_Path_And_Filename() + public void Handles_Mixed_Slash_Separators() { - if (Environment.OSVersion.Platform == PlatformID.Win32NT) - { - Assert.Equal(@"p_a_t_h\fi_le.ext", @"p?a|tle.ext".ToLocalRelativeFilename()); - } - else + var expected = "b" + Path.DirectorySeparatorChar + "c.ext"; + Assert.Equal(expected, @"a/b\c.ext".ToLocalRelativeFilename()); + } + + [Fact] + public void Preserves_Spaces_In_Filename() + { + // Spaces are valid in file names and must survive sanitization. + var expected = "dir" + Path.DirectorySeparatorChar + "file name.ext"; + Assert.Equal(expected, @"root\dir\file name.ext".ToLocalRelativeFilename()); + } + + [Fact] + public void Preserves_Spaces_In_Directory() + { + var expected = "my dir" + Path.DirectorySeparatorChar + "file.ext"; + Assert.Equal(expected, @"root\my dir\file.ext".ToLocalRelativeFilename()); + } + + [Fact] + public void Directory_Segment_Contains_No_Characters_Invalid_For_Directory_Names() + { + // All GetInvalidFileNameChars() (minus separators) must be absent from the directory + // segment. Directory segments are sanitized with ReplaceInvalidFileNameCharacters, so + // the full filename-invalid set applies. + var invalidChars = Path.GetInvalidFileNameChars() + .Where(c => c != '\\' && c != '/') + .ToArray(); + + Assert.All(invalidChars, c => { - Assert.Equal(@"_", $"{'\0'}".ToLocalRelativeFilename()); - } + var result = $@"a\prefix{c}suffix\file.ext".ToLocalRelativeFilename(); + var directory = Path.GetDirectoryName(result); + Assert.False(directory.Contains(c), $"Character U+{(int)c:X4} is invalid for directory names but was not replaced"); + }); } } -} +} \ No newline at end of file -- cgit v1.2.3 From 0a30f1080595a877d8c4b30f796e7055302de2cc Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 3 Jun 2026 08:53:45 -0500 Subject: remove deprecated extension methods --- src/slskd/Common/CommonExtensions.cs | 46 ------ .../Extensions/ToLocalRelativeFilenameTests.cs | 160 --------------------- 2 files changed, 206 deletions(-) delete mode 100644 tests/slskd.Tests.Unit/Common/Extensions/ToLocalRelativeFilenameTests.cs (limited to 'tests/slskd.Tests.Unit/Common/Extensions/ToLocalRelativeFilenameTests.cs') diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index c37c1550..95721163 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -381,21 +381,6 @@ namespace slskd .Build() .Deserialize(str); - /// - /// Converts a fully qualified remote filename to a local filename based in the provided - /// , swapping directory characters for those specific to the local OS, removing any - /// characters that are invalid for the local OS, and making the path relative to the remote store (including the - /// filename and the parent folder). - /// - /// The fully qualified remote filename to convert. - /// The base directory for the local filename. - /// The converted filename. - [Obsolete("Find something more intelligent to do instead of this")] - public static string ToLocalFilename(this string remoteFilename, string baseDirectory) - { - return FileSafety.CombineSafely(baseDirectory, remoteFilename.ToLocalRelativeFilename()); - } - /// /// Converts the given path to the normalized format (normalizes path separators to backslashes). /// @@ -429,37 +414,6 @@ namespace slskd return path.Split('\\').TakeLast(1).First(); } - /// - /// Converts a fully qualified remote filename to a local filename, swapping directory characters for those specific - /// to the local OS, removing any characters that are invalid for the local OS, and making the path relative to the - /// remote store (including the filename and the parent folder). - /// - /// The fully qualified remote filename to convert. - /// The converted filename. - [Obsolete("Find something more intelligent to do instead of this")] - public static string ToLocalRelativeFilename(this string remoteFilename) - { - if (string.IsNullOrWhiteSpace(remoteFilename)) - { - throw new ArgumentException($"Invalid remote filename; expected a non-whitespace value, received '{remoteFilename}'", nameof(remoteFilename)); - } - - // normalize path separators - var localizedRemoteFilename = FileSafety.LocalizePath(remoteFilename); - - var parts = localizedRemoteFilename.Split(Path.DirectorySeparatorChar); - - if (parts.Length == 1) - { - return FileSafety.SanitizeFilename(parts.First()); - } - - var file = FileSafety.SanitizeFilename(parts.Last()); - var directory = FileSafety.SanitizeFilename(parts.Reverse().Skip(1).Take(1).Single()); - - return FileSafety.CombineSafely(directory, file); - } - /// /// Converts a ModelStateDictionary into a human-readable format. /// diff --git a/tests/slskd.Tests.Unit/Common/Extensions/ToLocalRelativeFilenameTests.cs b/tests/slskd.Tests.Unit/Common/Extensions/ToLocalRelativeFilenameTests.cs deleted file mode 100644 index 78539142..00000000 --- a/tests/slskd.Tests.Unit/Common/Extensions/ToLocalRelativeFilenameTests.cs +++ /dev/null @@ -1,160 +0,0 @@ -namespace slskd.Tests.Unit.Common.Extensions -{ - using System; - using System.IO; - using System.Linq; - using Xunit; - - public class ToLocalRelativeFilenameTests - { - [Theory] - [InlineData(null)] - [InlineData("")] - [InlineData("\t")] - public void Throws_ArgumentException_Given_Bad_Remote_Filename(string filename) - { - var ex = Record.Exception(() => filename.ToLocalRelativeFilename()); - - Assert.NotNull(ex); - Assert.IsType(ex); - } - - [Fact] - public void Returns_Last_Directory_And_Filename_From_Deep_Path() - { - var expected = "path" + Path.DirectorySeparatorChar + "file.ext"; - Assert.Equal(expected, @"deeply\nested\path\file.ext".ToLocalRelativeFilename()); - } - - [Fact] - public void Returns_Just_Filename_If_No_Directory_Given() - { - Assert.Equal("file.ext", "file.ext".ToLocalRelativeFilename()); - } - - [Fact] - public void Replaces_Invalid_Characters_In_Filename() - { - // Exclude backslash and forward slash — they act as segment separators in the input - // and would cause the dirty string to be split rather than treated as a filename. - var invalidChars = Path.GetInvalidFileNameChars() - .Where(c => c != '\\' && c != '/') - .ToArray(); - - var dirtyName = new string(invalidChars); - var result = $@"a\b\{dirtyName}".ToLocalRelativeFilename(); - var filename = Path.GetFileName(result); - - Assert.All(invalidChars, c => - Assert.False(filename.Contains(c), $"Invalid character U+{(int)c:X4} was not replaced in filename")); - } - - [Fact] - public void Replaces_Invalid_Characters_In_Directory() - { - // Directory segments are sanitized with ReplaceInvalidFileNameCharacters, so the full - // GetInvalidFileNameChars() set applies (excluding path separators, which split the input). - var invalidChars = Path.GetInvalidFileNameChars() - .Where(c => c != '\\' && c != '/') - .ToArray(); - - var dirtyName = new string(invalidChars); - var result = $@"a\{dirtyName}\file.ext".ToLocalRelativeFilename(); - var directory = Path.GetDirectoryName(result); - - Assert.All(invalidChars, c => - Assert.False(directory.Contains(c), $"Invalid character U+{(int)c:X4} was not replaced in directory")); - } - - [Fact] - public void Replaces_Each_Invalid_Filename_Character_Individually() - { - // Tests each invalid filename char in isolation so that failures identify the exact - // offending character rather than only reporting that something in a concatenated - // string was not replaced. - var invalidChars = Path.GetInvalidFileNameChars() - .Where(c => c != '\\' && c != '/') - .ToArray(); - - Assert.All(invalidChars, c => - { - var result = $@"a\b\prefix{c}suffix.ext".ToLocalRelativeFilename(); - var filename = Path.GetFileName(result); - Assert.False(filename.Contains(c), $"Invalid character U+{(int)c:X4} was not replaced in filename"); - }); - } - - [Fact] - public void Replaces_Each_Invalid_Filename_Character_In_Directory_Individually() - { - // Tests each invalid filename char in the directory segment in isolation so that - // failures identify the exact offending character. - var invalidChars = Path.GetInvalidFileNameChars() - .Where(c => c != '\\' && c != '/') - .ToArray(); - - Assert.All(invalidChars, c => - { - var result = $@"a\prefix{c}suffix\file.ext".ToLocalRelativeFilename(); - var directory = Path.GetDirectoryName(result); - Assert.False(directory.Contains(c), $"Invalid character U+{(int)c:X4} was not replaced in directory"); - }); - } - - [Fact] - public void Returns_Last_Directory_And_Filename_From_Two_Segment_Path() - { - var expected = "dir" + Path.DirectorySeparatorChar + "file.ext"; - Assert.Equal(expected, @"dir\file.ext".ToLocalRelativeFilename()); - } - - [Fact] - public void Handles_Forward_Slash_As_Segment_Separator() - { - // LocalizePath normalizes '/' to Path.DirectorySeparatorChar before splitting, - // so forward-slash paths must produce the same result as backslash paths. - var expected = "b" + Path.DirectorySeparatorChar + "c.ext"; - Assert.Equal(expected, "a/b/c.ext".ToLocalRelativeFilename()); - } - - [Fact] - public void Handles_Mixed_Slash_Separators() - { - var expected = "b" + Path.DirectorySeparatorChar + "c.ext"; - Assert.Equal(expected, @"a/b\c.ext".ToLocalRelativeFilename()); - } - - [Fact] - public void Preserves_Spaces_In_Filename() - { - // Spaces are valid in file names and must survive sanitization. - var expected = "dir" + Path.DirectorySeparatorChar + "file name.ext"; - Assert.Equal(expected, @"root\dir\file name.ext".ToLocalRelativeFilename()); - } - - [Fact] - public void Preserves_Spaces_In_Directory() - { - var expected = "my dir" + Path.DirectorySeparatorChar + "file.ext"; - Assert.Equal(expected, @"root\my dir\file.ext".ToLocalRelativeFilename()); - } - - [Fact] - public void Directory_Segment_Contains_No_Characters_Invalid_For_Directory_Names() - { - // All GetInvalidFileNameChars() (minus separators) must be absent from the directory - // segment. Directory segments are sanitized with ReplaceInvalidFileNameCharacters, so - // the full filename-invalid set applies. - var invalidChars = Path.GetInvalidFileNameChars() - .Where(c => c != '\\' && c != '/') - .ToArray(); - - Assert.All(invalidChars, c => - { - var result = $@"a\prefix{c}suffix\file.ext".ToLocalRelativeFilename(); - var directory = Path.GetDirectoryName(result); - Assert.False(directory.Contains(c), $"Character U+{(int)c:X4} is invalid for directory names but was not replaced"); - }); - } - } -} \ No newline at end of file -- cgit v1.2.3