diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-06-07 14:45:04 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-06-07 14:45:04 -0500 |
| commit | b747b68ba0ba777be578e73bd1328fbde940e436 (patch) | |
| tree | 160d23aeb8707dc04f81bb52e81913c1b77bb2a4 /tests/slskd.Tests.Unit/Common/Extensions | |
| parent | e06999e5eec1616d93b34ffbab0eade617d1aaf6 (diff) | |
| parent | 2d069bbf1f71f38e1286e27d45c24b478feb11ac (diff) | |
| download | slskd-b747b68ba0ba777be578e73bd1328fbde940e436.tar.gz | |
Merge branch 'master' into dependabot/npm_and_yarn/src/web/launch-editor-2.14.1
Diffstat (limited to 'tests/slskd.Tests.Unit/Common/Extensions')
| -rw-r--r-- | tests/slskd.Tests.Unit/Common/Extensions/ReplaceInvalidFileNameCharactersTests.cs | 144 | ||||
| -rw-r--r-- | tests/slskd.Tests.Unit/Common/Extensions/ToLocalRelativeFilenameTests.cs | 160 |
2 files changed, 0 insertions, 304 deletions
diff --git a/tests/slskd.Tests.Unit/Common/Extensions/ReplaceInvalidFileNameCharactersTests.cs b/tests/slskd.Tests.Unit/Common/Extensions/ReplaceInvalidFileNameCharactersTests.cs deleted file mode 100644 index eff24587..00000000 --- a/tests/slskd.Tests.Unit/Common/Extensions/ReplaceInvalidFileNameCharactersTests.cs +++ /dev/null @@ -1,144 +0,0 @@ -namespace slskd.Tests.Unit.Common.Extensions -{ - using System; - using System.IO; - using System.Linq; - using Xunit; - - public class ReplaceInvalidFileNameCharactersTests - { - [Fact] - public void Throws_NullReferenceException_Given_Null() - { - string path = null; - Assert.Throws<NullReferenceException>(() => path.ReplaceInvalidFileNameCharacters()); - } - - [Fact] - public void Returns_Empty_String_Given_Empty_Input() - { - Assert.Equal(string.Empty, string.Empty.ReplaceInvalidFileNameCharacters()); - } - - [Fact] - public void Returns_Input_Unchanged_When_No_Invalid_Characters_Present() - { - var valid = "valid_filename.ext"; - Assert.Equal(valid, valid.ReplaceInvalidFileNameCharacters()); - } - - [Fact] - public void Preserves_Valid_Characters() - { - // Common valid filename chars that must not be touched - var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._- "; - Assert.Equal(valid, valid.ReplaceInvalidFileNameCharacters()); - } - - [Fact] - public void Default_Replacement_Character_Is_Underscore() - { - var c = Path.GetInvalidFileNameChars().First(); - var result = $"a{c}b".ReplaceInvalidFileNameCharacters(); - Assert.Equal("a_b", result); - } - - [Fact] - public void Replaces_Each_Invalid_Filename_Character_With_Default_Replacement() - { - // Each invalid char is tested individually so failures identify the exact offending char - Assert.All(Path.GetInvalidFileNameChars(), c => - { - var result = $"prefix{c}suffix".ReplaceInvalidFileNameCharacters(); - Assert.False(result.Contains(c), $"Invalid character U+{(int)c:X4} was not replaced"); - Assert.Equal("prefix_suffix", result); - }); - } - - [Fact] - public void Replaces_All_Invalid_Filename_Characters_Simultaneously() - { - var invalidChars = Path.GetInvalidFileNameChars(); - var allInvalid = new string(invalidChars); - var result = allInvalid.ReplaceInvalidFileNameCharacters(); - - Assert.All(invalidChars, c => - Assert.False(result.Contains(c), $"Invalid character U+{(int)c:X4} remained after sanitization")); - } - - [Fact] - public void Result_Contains_Only_Replacement_Character_When_All_Input_Characters_Are_Invalid() - { - var invalidChars = Path.GetInvalidFileNameChars(); - var allInvalid = new string(invalidChars); - var result = allInvalid.ReplaceInvalidFileNameCharacters(); - - Assert.Equal(new string('_', invalidChars.Length), result); - } - - [Fact] - public void Uses_Custom_Replacement_Character() - { - char replacement = '-'; - - Assert.All(Path.GetInvalidFileNameChars(), c => - { - var result = $"a{c}b".ReplaceInvalidFileNameCharacters(replacement); - Assert.False(result.Contains(c), $"Invalid character U+{(int)c:X4} was not replaced"); - Assert.Equal("a-b", result); - }); - } - - [Fact] - public void Replaces_Consecutive_Invalid_Characters() - { - var chars = Path.GetInvalidFileNameChars(); - if (chars.Length < 2) - { - return; - } - - var input = $"a{chars[0]}{chars[1]}b"; - var result = input.ReplaceInvalidFileNameCharacters(); - - Assert.Equal("a__b", result); - } - - [Fact] - public void Replaces_Invalid_Character_At_Start_Of_String() - { - var c = Path.GetInvalidFileNameChars().First(); - var result = $"{c}filename.ext".ReplaceInvalidFileNameCharacters(); - Assert.Equal("_filename.ext", result); - } - - [Fact] - public void Replaces_Invalid_Character_At_End_Of_String() - { - var c = Path.GetInvalidFileNameChars().First(); - var result = $"filename.ext{c}".ReplaceInvalidFileNameCharacters(); - Assert.Equal("filename.ext_", result); - } - - [Fact] - public void Result_Contains_No_Invalid_Filename_Characters() - { - var invalidChars = Path.GetInvalidFileNameChars(); - var input = "valid" + new string(invalidChars) + "also_valid"; - var result = input.ReplaceInvalidFileNameCharacters(); - - Assert.All(invalidChars, c => - Assert.False(result.Contains(c), $"Character U+{(int)c:X4} remained after sanitization")); - } - - [Fact] - public void Valid_Characters_Surrounding_Invalid_Characters_Are_Preserved() - { - var c = Path.GetInvalidFileNameChars().First(); - var result = $"before{c}after".ReplaceInvalidFileNameCharacters(); - - Assert.StartsWith("before", result); - Assert.EndsWith("after", result); - } - } -} 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<ArgumentException>(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 |
