aboutsummaryrefslogtreecommitdiffstats
path: root/tests/slskd.Tests.Unit/Common/Validation/StringAttributeTests.cs
blob: dbf8396f64220292717fd24dc413e3d21ecf35a8 (plain) (blame)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
using System;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using slskd.Validation;
using Xunit;

namespace slskd.Tests.Unit.Common.Validation;

public class StringAttributeTests
{
    private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
    {
        var attr = new StringAttribute();
        configure?.Invoke(attr);
        var context = new ValidationContext(new object()) { DisplayName = "Field", MemberName = "Field" };
        var result = attr.GetValidationResult(value, context);
        return (result == null, result?.ErrorMessage);
    }

    public class Null
    {
        [Fact]
        public void Passes_When_AllowNull()
        {
            var (isValid, message) = Validate(null, a => a.AllowNull = true);
            Assert.True(isValid);
        }

        [Fact]
        public void Fails_When_Not_AllowNull()
        {
            var (isValid, message) = Validate(null, a => a.AllowNull = false);
            Assert.False(isValid);
            Assert.Contains("must not be null", message);
        }

        [Fact]
        public void NonNull_Passes_Regardless()
        {
            var (isValid, message) = Validate("foo", a => a.AllowNull = true);
            Assert.True(isValid);

            var (isValid2, message2) = Validate("foo", a => a.AllowNull = false);
            Assert.True(isValid2);
        }
    }

    public class Empty
    {
        [Fact]
        public void Empty_Passes_When_AllowEmpty_Is_True()
        {
            var (isValid, _) = Validate(string.Empty, a => a.AllowEmpty = true);
            Assert.True(isValid);
        }

        [Fact]
        public void Empty_Fails_When_AllowEmpty_Is_False()
        {
            var (isValid, message) = Validate(string.Empty, a => a.AllowEmpty = false);
            Assert.False(isValid);
            Assert.Contains("at least one character", message);
        }

        [Fact]
        public void NonEmpty_Passes_Regardless()
        {
            var (isValid, _) = Validate("hello", a => a.AllowEmpty = false);
            Assert.True(isValid);

            var (isValid2, _) = Validate("hello", a => a.AllowEmpty = true);
            Assert.True(isValid2);
        }
    }

    public class WhiteSpace
    {
        [Fact]
        public void WhiteSpace_Passes_When_AllowWhiteSpace_Is_True()
        {
            var (isValid, _) = Validate("    ", a => a.AllowWhiteSpace = true);
            Assert.True(isValid);
        }

        [Fact]
        public void WhiteSpace_Fails_When_AllowWhiteSpace_Is_False()
        {
            var (isValid, message) = Validate("   ", a => a.AllowWhiteSpace = false);
            Assert.False(isValid);
            Assert.Contains("whitespace", message);
        }

        [Fact]
        public void NonWhiteSpace_Passes_Regardless()
        {
            var (isValid, _) = Validate("hello", a => a.AllowWhiteSpace = false);
            Assert.True(isValid);

            var (isValid2, _) = Validate("hello", a => a.AllowWhiteSpace = true);
            Assert.True(isValid2);
        }
    }

    public class DisallowedCharacters
    {
        public class Passes
        {
            [Fact]
            public void EmptyString_Passes()
            {
                var (isValid, _) = Validate(string.Empty, a => a.DisallowedCharacters = ['!']);
                Assert.True(isValid);
            }

            [Fact]
            public void NoDisallowedCharsPresent_Passes()
            {
                var (isValid, _) = Validate("hello", a => a.DisallowedCharacters = ['!', '@']);
                Assert.True(isValid);
            }

            [Fact]
            public void EmptyDisallowedSet_Passes()
            {
                var (isValid, _) = Validate("hello!@#", a => a.DisallowedCharacters = []);
                Assert.True(isValid);
            }

            [Fact]
            public void Unicode_AllowedChars_Pass()
            {
                var (isValid, _) = Validate("пользователь/音楽", a => a.DisallowedCharacters = ['!', '@']);
                Assert.True(isValid);
            }

            // Case-insensitive matching only folds the declared disallowed chars, not all letters;
            // a disallowed lower-case letter does not block an entirely different upper-case letter.
            [Fact]
            public void LowerCaseDisallowed_DoesNotBlock_Different_UpperCaseLetter()
            {
                var (isValid, _) = Validate("B", a => a.DisallowedCharacters = ['a']);
                Assert.True(isValid);
            }
        }

        public class Fails
        {
            [Fact]
            public void SingleDisallowedChar_Fails()
            {
                var (isValid, _) = Validate("hello!", a => a.DisallowedCharacters = ['!']);
                Assert.False(isValid);
            }

            [Fact]
            public void DisallowedCharAtStart_Fails()
            {
                var (isValid, _) = Validate("!hello", a => a.DisallowedCharacters = ['!']);
                Assert.False(isValid);
            }

            [Fact]
            public void DisallowedCharAtEnd_Fails()
            {
                var (isValid, _) = Validate("hello!", a => a.DisallowedCharacters = ['!']);
                Assert.False(isValid);
            }

            [Fact]
            public void DisallowedCharInMiddle_Fails()
            {
                var (isValid, _) = Validate("hel!lo", a => a.DisallowedCharacters = ['!']);
                Assert.False(isValid);
            }

            [Fact]
            public void OneOfMultipleDisallowedCharsPresent_Fails()
            {
                var (isValid, _) = Validate("hello!", a => a.DisallowedCharacters = ['!', '@', '#']);
                Assert.False(isValid);
            }

            [Fact]
            public void MultipleDisallowedCharsAllPresent_Fails()
            {
                var (isValid, _) = Validate("h!e@l#lo", a => a.DisallowedCharacters = ['!', '@', '#']);
                Assert.False(isValid);
            }

            [Fact]
            public void DisallowedCharRepeated_Fails()
            {
                var (isValid, _) = Validate("he!lo!", a => a.DisallowedCharacters = ['!']);
                Assert.False(isValid);
            }

            [Fact]
            public void AllCharsDisallowed_Fails()
            {
                var (isValid, _) = Validate("!@#", a => a.DisallowedCharacters = ['!', '@', '#']);
                Assert.False(isValid);
            }

            [Fact]
            public void Unicode_DisallowedChar_Fails()
            {
                var (isValid, _) = Validate("пользователь", a => a.DisallowedCharacters = ['е']);
                Assert.False(isValid);
            }

            // character matching is always case-insensitive
            [Fact]
            public void LowerCaseDisallowed_Blocks_UpperCase()
            {
                var (isValid, _) = Validate("HELLO", a => a.DisallowedCharacters = ['h', 'e', 'l', 'o']);
                Assert.False(isValid);
            }

            [Fact]
            public void UpperCaseDisallowed_Blocks_LowerCase()
            {
                var (isValid, _) = Validate("hello", a => a.DisallowedCharacters = ['H', 'E', 'L', 'O']);
                Assert.False(isValid);
            }

            [Fact]
            public void ExactMatch_Fails()
            {
                var (isValid, _) = Validate("hello", a => a.DisallowedCharacters = ['h']);
                Assert.False(isValid);
            }
        }

        public class ErrorMessage_Contains
        {
            [Fact]
            public void TheDisallowedChar()
            {
                var (_, errorMessage) = Validate("hello!", a => a.DisallowedCharacters = ['!']);
                Assert.Contains("'!'", errorMessage);
            }

            [Fact]
            public void AllPresentDisallowedChars()
            {
                var (_, errorMessage) = Validate("h!@l", a => a.DisallowedCharacters = ['!', '@']);
                Assert.Contains("'!'", errorMessage);
                Assert.Contains("'@'", errorMessage);
            }

            [Fact]
            public void EachOccurrenceOfRepeatedChar()
            {
                // "he!lo!" has two '!' — both occurrences are listed
                var (_, errorMessage) = Validate("he!lo!", a => a.DisallowedCharacters = ['!']);
                Assert.Equal(2, errorMessage.Split("'!'").Length - 1);
            }

            [Fact]
            public void OnlyCharsActuallyPresent_NotAllDisallowedChars()
            {
                // '@' is disallowed but not in the value — must not appear in the message
                var (_, errorMessage) = Validate("hello!", a => a.DisallowedCharacters = ['!', '@']);
                Assert.Contains("'!'", errorMessage);
                Assert.DoesNotContain("'@'", errorMessage);
            }

            [Fact]
            public void ShowsActualChar_NotDisallowedChar()
            {
                // 'a' is disallowed (lowercase); value contains 'A' (uppercase).
                // The message reports what the user supplied ('A'), not the disallowed definition ('a').
                var (_, errorMessage) = Validate("A", a => a.DisallowedCharacters = ['a']);
                Assert.Contains("'A'", errorMessage);
                Assert.DoesNotContain("'a'", errorMessage);
            }
        }
    }

    public class Length
    {
        [Fact]
        public void Within_Range_Passes()
        {
            var (isValid, _) = Validate("hello", a => { a.MinimumLength = 3; a.MaximumLength = 10; });
            Assert.True(isValid);
        }

        [Fact]
        public void ExactlyAtMinimumLength_Passes()
        {
            var (isValid, _) = Validate("hi", a => a.MinimumLength = 2);
            Assert.True(isValid);
        }

        [Fact]
        public void ExactlyAtMaximumLength_Passes()
        {
            var (isValid, _) = Validate("hello", a => a.MaximumLength = 5);
            Assert.True(isValid);
        }

        [Fact]
        public void BelowMinimumLength_Fails()
        {
            var (isValid, _) = Validate("hi", a => a.MinimumLength = 5);
            Assert.False(isValid);
        }

        [Fact]
        public void ExceedsMaximumLength_Fails()
        {
            var (isValid, _) = Validate("hello world", a => a.MaximumLength = 5);
            Assert.False(isValid);
        }

        public class ErrorMessage_Contains
        {
            [Fact]
            public void MinimumLength_Value()
            {
                var (_, message) = Validate("hi", a => { a.MinimumLength = 5; a.MaximumLength = 20; });
                Assert.Contains("5", message);
            }

            [Fact]
            public void MaximumLength_Value()
            {
                var (_, message) = Validate("hi", a => { a.MinimumLength = 5; a.MaximumLength = 20; });
                Assert.Contains("20", message);
            }
        }
    }

    public class Pattern
    {
        [Fact]
        public void NullPattern_Passes()
        {
            // Pattern defaults to null; any value passes the pattern check
            var (isValid, _) = Validate("hello");
            Assert.True(isValid);
        }

        [Fact]
        public void ValueMatchingPattern_Fails()
        {
            var (isValid, _) = Validate("hello1", a => a.Pattern = new Regex("[0-9]"));
            Assert.False(isValid);
        }

        [Fact]
        public void ValueNotMatchingPattern_Passes()
        {
            var (isValid, _) = Validate("hello", a => a.Pattern = new Regex("[0-9]"));
            Assert.True(isValid);
        }

        public class ErrorMessage_Contains
        {
            [Fact]
            public void RegularExpression_Keyword()
            {
                var (_, message) = Validate("hello1", a => a.Pattern = new Regex("[0-9]"));
                Assert.Contains("regular expression", message);
            }
        }
    }

    public class CheckOrdering
    {
        // AllowNull is checked before AllowEmpty:
        // null with both disabled returns the null error, not the empty error.
        [Fact]
        public void Null_ReturnsNullError_Not_EmptyError()
        {
            var (_, message) = Validate(null, a => { a.AllowNull = false; a.AllowEmpty = false; });
            Assert.Contains("null", message);
            Assert.DoesNotContain("character", message);
        }

        // AllowEmpty is checked before AllowWhiteSpace:
        // "" with both disabled returns the empty error, not the whitespace error.
        [Fact]
        public void Empty_ReturnsEmptyError_Not_WhitespaceError()
        {
            var (_, message) = Validate(string.Empty, a => { a.AllowEmpty = false; a.AllowWhiteSpace = false; });
            Assert.Contains("character", message);
            Assert.DoesNotContain("whitespace", message);
        }

        // AllowWhiteSpace is checked before the length check:
        // a whitespace string that is also below MinimumLength returns the whitespace error.
        [Fact]
        public void Whitespace_ReturnsWhitespaceError_Not_LengthError()
        {
            var (_, message) = Validate(" ", a => { a.AllowWhiteSpace = false; a.MinimumLength = 10; });
            Assert.Contains("whitespace", message);
            Assert.DoesNotContain("between", message);
        }

        // Length is checked before DisallowedCharacters:
        // a too-short string that also contains a disallowed character returns the length error.
        [Fact]
        public void TooShort_ReturnsLengthError_Not_DisallowedCharsError()
        {
            var (_, message) = Validate("hi!", a => { a.MinimumLength = 10; a.DisallowedCharacters = ['!']; });
            Assert.Contains("between", message);
            Assert.DoesNotContain("disallowed", message);
        }

        // DisallowedCharacters is checked before Pattern:
        // a string with a disallowed character that also matches the pattern returns the disallowed-chars error.
        [Fact]
        public void DisallowedChar_ReturnsDisallowedError_Not_PatternError()
        {
            var (_, message) = Validate("hello!1", a => { a.DisallowedCharacters = ['!']; a.Pattern = new Regex("[0-9]"); });
            Assert.Contains("disallowed", message);
            Assert.DoesNotContain("expression", message);
        }
    }
}