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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using slskd.Validation;
using Xunit;
namespace slskd.Tests.Unit.Common.Validation;
public class AbsolutePathAttributeTests
{
private static AbsolutePathAttribute CreateAttribute(OperatingSystem os, OperatingSystem? injected = null)
{
var attr = new AbsolutePathAttribute(os);
if (injected.HasValue)
{
var field = typeof(AbsolutePathAttribute)
.GetField("<Injected>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(attr, injected.Value);
}
return attr;
}
private static (bool IsValid, string ErrorMessage) Validate(string value, OperatingSystem os, OperatingSystem? injected = null)
{
var attr = CreateAttribute(os, injected);
var context = new ValidationContext(new object()) { DisplayName = "Field", MemberName = "Field" };
var result = attr.GetValidationResult(value, context);
return (result == null, result?.ErrorMessage);
}
public class Constructor
{
[Fact]
public void AcceptsCurrentOsValue()
{
var ex = Record.Exception(() => _ = new AbsolutePathAttribute(OperatingSystem.Current));
Assert.Null(ex);
}
[Fact]
public void AcceptsAnyOsValue()
{
var ex = Record.Exception(() => _ = new AbsolutePathAttribute(OperatingSystem.Any));
Assert.Null(ex);
}
[Fact]
public void AcceptsAllOsValue()
{
var ex = Record.Exception(() => _ = new AbsolutePathAttribute(OperatingSystem.All));
Assert.Null(ex);
}
[Fact]
public void ThrowsForWindowsOsValue()
{
Assert.Throws<ArgumentException>(() => new AbsolutePathAttribute(OperatingSystem.Windows));
}
[Fact]
public void ThrowsForLinuxOsValue()
{
Assert.Throws<ArgumentException>(() => new AbsolutePathAttribute(OperatingSystem.Linux));
}
[Fact]
public void ThrowsForNoneOsValue()
{
Assert.Throws<ArgumentException>(() => new AbsolutePathAttribute(OperatingSystem.None));
}
}
public class NullOrEmpty
{
[Theory]
[InlineData(null)]
[InlineData("")]
public void Passes_ForCurrentWithWindowsInjected(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.Current, OperatingSystem.Windows);
Assert.True(isValid);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void Passes_ForCurrentWithLinuxInjected(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.Current, OperatingSystem.Linux);
Assert.True(isValid);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void Passes_ForAny(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.Any);
Assert.True(isValid);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void Passes_ForAll(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.All);
Assert.True(isValid);
}
}
// Current mode: validates using only the injected (or actual current) OS
public class Current
{
public class WithWindowsInjected
{
[Theory]
[InlineData("C:\\Music")]
[InlineData("C:/Music")]
[InlineData("C:\\Music\\Artist\\Album")]
[InlineData("C:\\")]
[InlineData("Z:\\")]
[InlineData("\\\\server\\share")]
[InlineData("\\\\server\\share\\folder")]
[InlineData("\\\\192.168.1.1\\share")]
[InlineData("\\\\server")] // bare UNC (server only, no share) — StartsWith("\\") → absolute
public void AbsolutePaths_Pass(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.Current, OperatingSystem.Windows);
Assert.True(isValid);
}
[Theory]
[InlineData("subdir")]
[InlineData("sub/dir")]
[InlineData("sub\\dir")]
[InlineData("Artist\\Album")]
[InlineData("music.flac")]
[InlineData("My Music")]
[InlineData("..")]
[InlineData(".hidden")]
[InlineData("...dir")]
[InlineData("\\Music")] // drive-relative on Windows, not absolute
[InlineData("/Music")] // same
[InlineData("/home/user")] // same — leading slash without drive letter is not absolute on Windows
[InlineData("//server/share")] // forward-slash UNC is not recognized as absolute on Windows
[InlineData("C:")]
[InlineData("C:Music")]
public void NonAbsolutePaths_Fail(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.Current, OperatingSystem.Windows);
Assert.False(isValid);
}
}
public class WithLinuxInjected
{
[Theory]
[InlineData("/")]
[InlineData("/home")]
[InlineData("/home/user")]
[InlineData("/Music")]
[InlineData("//server/share")]
[InlineData("//192.168.1.1/share")]
public void AbsolutePaths_Pass(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.Current, OperatingSystem.Linux);
Assert.True(isValid);
}
[Theory]
[InlineData("subdir")]
[InlineData("sub/dir")]
[InlineData("sub\\dir")]
[InlineData("Artist\\Album")]
[InlineData("music.flac")]
[InlineData("My Music")]
[InlineData("..")]
[InlineData(".hidden")]
[InlineData("...dir")]
[InlineData("C:\\Music")] // backslash paths are relative on Linux
[InlineData("C:/Music")] // starts with 'C', not '/' — relative on Linux
[InlineData("\\\\server\\share")] // backslash UNC is relative on Linux
[InlineData("\\")]
[InlineData("\\home")]
public void NonAbsolutePaths_Fail(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.Current, OperatingSystem.Linux);
Assert.False(isValid);
}
}
}
public class ErrorMessages
{
[Fact]
public void Current_Message_MentionsCurrentOperatingSystem()
{
var (_, msg) = Validate("relative/path", OperatingSystem.Current, OperatingSystem.Windows);
Assert.Contains("current operating system", msg);
}
[Fact]
public void Current_Message_IncludesFieldName()
{
var (_, msg) = Validate("relative/path", OperatingSystem.Current, OperatingSystem.Windows);
Assert.Contains("Field", msg);
}
[Fact]
public void AnyAll_Message_MentionsAllOperatingSystems()
{
var (_, msg) = Validate("/home/user", OperatingSystem.Any);
Assert.Contains("all operating systems", msg);
}
}
// Any and All use identical logic: path must be absolute on both Windows and Linux.
// No common path satisfies this — Windows uses drive letters or \\ UNC, Linux uses /,
// and no single path form is recognized as absolute by both detection rules.
public class Any
{
[Fact]
public void UsesAndLogic_NoPathCanEverPass_BecauseLinuxAndWindowsAbsoluteFormsAreIncompatible()
{
// If Any used OR-logic ("absolute on at least one OS"), a Linux-absolute path would pass.
// Any actually requires absolute on ALL checked OSes (AND-logic), identical to All.
// No path form satisfies both: Linux requires '/', Windows requires a drive letter or '\\'.
var (linuxAbsPath, _) = Validate("/home/user", OperatingSystem.Any);
var (winAbsPath, _) = Validate("C:\\Music", OperatingSystem.Any);
Assert.False(linuxAbsPath);
Assert.False(winAbsPath);
}
[Theory]
[InlineData("subdir")]
[InlineData("sub/dir")]
[InlineData("sub\\dir")]
[InlineData("Artist\\Album")]
[InlineData("music.flac")]
[InlineData("..")]
[InlineData(".hidden")]
public void RelativePaths_Fail(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.Any);
Assert.False(isValid);
}
// These are absolute on Windows but relative on Linux — fail because not absolute on both
[Theory]
[InlineData("C:\\Music")]
[InlineData("C:/Music")]
[InlineData("C:\\")]
[InlineData("\\\\server\\share")]
public void WindowsAbsolutePaths_Fail(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.Any);
Assert.False(isValid);
}
// These are absolute on Linux but relative on Windows — fail because not absolute on both
[Theory]
[InlineData("/home/user")]
[InlineData("/Music")]
[InlineData("//server/share")]
[InlineData("/")]
public void LinuxAbsolutePaths_Fail(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.Any);
Assert.False(isValid);
}
}
public class All
{
[Theory]
[InlineData("subdir")]
[InlineData("sub/dir")]
[InlineData("sub\\dir")]
[InlineData("Artist\\Album")]
[InlineData("music.flac")]
[InlineData("..")]
[InlineData(".hidden")]
public void RelativePaths_Fail(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.All);
Assert.False(isValid);
}
// These are absolute on Windows but relative on Linux — fail because not absolute on both
[Theory]
[InlineData("C:\\Music")]
[InlineData("C:/Music")]
[InlineData("C:\\")]
[InlineData("\\\\server\\share")]
public void WindowsAbsolutePaths_Fail(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.All);
Assert.False(isValid);
}
// These are absolute on Linux but relative on Windows — fail because not absolute on both
[Theory]
[InlineData("/home/user")]
[InlineData("/Music")]
[InlineData("//server/share")]
[InlineData("/")]
public void LinuxAbsolutePaths_Fail(string value)
{
var (isValid, _) = Validate(value, OperatingSystem.All);
Assert.False(isValid);
}
}
}
|