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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
using System;
using System.IO;
using Xunit;
namespace slskd.Tests.Unit.Common;
public partial class FileSafetyTests
{
public class CombineSafelyTests
{
public static string Base = System.OperatingSystem.IsWindows() ? "C:\\base" : "/base";
public static string ExpectedStartsWith = Base + Path.DirectorySeparatorChar;
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void Throws_ArgumentNullException_Given_NullOrWhiteSpaceRoot(string root)
{
var ex = Record.Exception(() => FileSafety.CombineSafely(root));
Assert.NotNull(ex);
Assert.IsType<ArgumentNullException>(ex);
}
[Theory]
[InlineData(".")]
[InlineData("..")]
[InlineData("./foo")]
[InlineData("../bar")]
[InlineData("foo/../bar")]
[InlineData("foo\\..\\bar")]
public void Throws_ArgumentException_Given_TraversingRoot(string root)
{
var ex = Record.Exception(() => FileSafety.CombineSafely(root));
Assert.NotNull(ex);
Assert.IsType<ArgumentException>(ex);
}
[Fact]
public void Returns_Root_Given_No_Segments()
{
var result = FileSafety.CombineSafely(Base);
Assert.StartsWith(ExpectedStartsWith.TrimEnd(Path.DirectorySeparatorChar), result);
}
[Fact]
public void Drops_Empty_Segments()
{
var result = FileSafety.CombineSafely(Base, string.Empty, "foo", string.Empty, "bar");
Assert.StartsWith(ExpectedStartsWith, result);
Assert.EndsWith(Path.Combine("foo", "bar"), result);
}
[Fact]
public void Throws_Given_Null_Segment()
{
var ex = Record.Exception(() => FileSafety.CombineSafely(Base, "foo", null, "bar"));
Assert.NotNull(ex);
Assert.IsType<ArgumentNullException>(ex);
}
[Fact]
public void Throws_Given_Null_Segments()
{
var ex = Record.Exception(() => FileSafety.CombineSafely(Base, segments: null));
Assert.NotNull(ex);
Assert.IsType<ArgumentNullException>(ex);
}
[Fact]
public void SingleSegment_ReturnsBaseAndSegment()
{
var result = FileSafety.CombineSafely(Base, "subdir");
Assert.Equal(Path.Combine(Base, "subdir"), result);
}
[Fact]
public void MultipleSegments_CombinesAll()
{
var result = FileSafety.CombineSafely(Base, "sub", "dir", "file.flac");
Assert.Equal(Path.Combine(Base, "sub", "dir", "file.flac"), result);
Assert.StartsWith(ExpectedStartsWith, result);
}
[Theory]
[InlineData("subdir")]
[InlineData("sub/dir")]
[InlineData("sub\\dir")]
[InlineData("sub//dir")]
[InlineData("sub\\\\dir")]
[InlineData("My Artist")]
[InlineData("Artist.Name")]
[InlineData("...dir")]
[InlineData("..hidden")]
[InlineData(".hidden")]
[InlineData("dir/..dir")]
[InlineData("Ünïcödé")]
[InlineData("пользователь/Музыка")]
[InlineData("用户/音乐")]
[InlineData("ユーザー/音楽")]
[InlineData("사용자/음악")]
[InlineData("foo\\bar\\baz\\qux\\deeply\\nested")]
[InlineData("foo/bar/baz/qux/deeply/nested")]
[InlineData("foo/bar\\baz/qux\\deeply/nested")]
public void Combines_Safe_Segments(string segment)
{
var result = FileSafety.CombineSafely(Base, segment);
Assert.Equal(Path.Combine(Base, segment), result);
Assert.StartsWith(ExpectedStartsWith, result);
}
[Theory]
[InlineData("/Music")]
[InlineData("//server/share")]
public void Throws_ArgumentException_Given_AbsolutePath_On_Unix(string segment)
{
var ex = Record.Exception(() => FileSafety.CombineSafely(Base, OperatingSystem.Linux, segment));
Assert.NotNull(ex);
Assert.Contains("Absolute", ex.Message);
}
[Theory]
[InlineData("C:\\Music")]
[InlineData("C:/Music")]
[InlineData("\\\\server\\share")]
public void Throws_ArgumentException_Given_AbsolutePath_On_Windows(string segment)
{
var ex = Record.Exception(() => FileSafety.CombineSafely(Base, OperatingSystem.Windows, segment));
Assert.NotNull(ex);
Assert.Contains("Absolute", ex.Message);
}
[Theory]
[InlineData("foo:bar")]
[InlineData("C:relative")]
[InlineData("sub/foo:bar")]
[InlineData("sub/foo/bar:qux")]
public void Throws_ArgumentException_Given_Colon_In_Segment_On_Windows(string segment)
{
var ex = Record.Exception(() => FileSafety.CombineSafely(Base, OperatingSystem.Windows, segment));
Assert.NotNull(ex);
Assert.Contains("Colon", ex.Message);
}
[Theory]
[InlineData("foo:bar")]
[InlineData("sub/foo:bar")]
[InlineData("sub/foo/bar:qux")]
public void Accepts_Colon_In_Segment_On_Linux(string segment)
{
var result = Record.Exception(() => FileSafety.CombineSafely(Base, OperatingSystem.Linux, segment));
Assert.Null(result);
}
[Theory]
[InlineData("C:relative")]
public void Accepts_Colon_In_Segment_On_Linux_When_Running_On_Windows(string segment)
{
var result = FileSafety.CombineSafely(Base, OperatingSystem.Linux, segment);
Assert.Equal($"{Base}/C:relative", result);
}
[Theory]
[InlineData("/Music")]
[InlineData("\\Music")]
public void Throws_ArgumentException_Given_Leading_Slash_In_Segment_On_Windows(string segment)
{
var ex = Record.Exception(() => FileSafety.CombineSafely(Base, OperatingSystem.Windows, segment));
Assert.NotNull(ex);
Assert.Contains("Drive-relative", ex.Message);
}
[Theory]
[InlineData("\\Music")]
public void Accepts_Leading_Backslash_In_Segment_On_Linux(string segment)
{
var result = FileSafety.CombineSafely(Base, OperatingSystem.Linux, segment);
Assert.Equal($"{Base}/\\Music", result);
}
[Fact]
public void Returns_Root_Given_Single_Empty_Segment()
{
var result = FileSafety.CombineSafely(Base, "");
Assert.Equal(Base, result);
}
[Fact]
public void Returns_Root_Given_All_Empty_Segments()
{
var result = FileSafety.CombineSafely(Base, "", "");
Assert.Equal(Base, result);
}
[Fact]
public void Handles_Root_With_Trailing_Separator()
{
var rootWithTrailing = Base + Path.DirectorySeparatorChar;
var result = FileSafety.CombineSafely(rootWithTrailing, "foo");
Assert.StartsWith(ExpectedStartsWith, result);
Assert.EndsWith("foo", result);
}
[Theory]
[InlineData("./foo")]
[InlineData("../bar")]
[InlineData("foo/../bar")]
public void Throws_ArgumentException_Given_TraversingRoot_With_Os_Override(string root)
{
var ex = Record.Exception(() => FileSafety.CombineSafely(root, OperatingSystem.Linux, "segment"));
Assert.NotNull(ex);
Assert.IsType<ArgumentException>(ex);
}
[Theory]
// Bare traversal
[InlineData("..")]
[InlineData(".")]
// Traversal as first component
[InlineData("../escape")]
[InlineData("./dir")]
// Traversal in middle
[InlineData("sub/../escape")]
[InlineData("sub/./dir")]
// Traversal at end
[InlineData("sub/..")]
[InlineData("sub/.")]
// Double traversal
[InlineData("sub/../../escape")]
[InlineData("a/b/c/../../..")]
// Via backslash
[InlineData("sub\\..")]
[InlineData("sub\\.")]
[InlineData("sub\\..\\escape")]
public void Throws_ArgumentException_Given_Traversing_Segment(string segment)
{
var ex = Assert.Throws<ArgumentException>(() => FileSafety.CombineSafely(Base, segment));
Assert.NotNull(ex);
Assert.Contains("traversal", ex.Message);
}
}
}
|