1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
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");
});
}
}
}
|