diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-04-23 18:40:41 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-23 18:40:41 -0500 |
| commit | fc722e4a78aa5369fcd5ca0d8e88bd98d874a1d8 (patch) | |
| tree | 1ebad506bc42f3bc20e6825bf9f03715031c06ba /tests | |
| parent | 7961741f740abd240754ff6e39cda6d69f727268 (diff) | |
| parent | 2f20ac833dca19e87e7980ac83f94206685409d9 (diff) | |
| download | slskd-fc722e4a78aa5369fcd5ca0d8e88bd98d874a1d8.tar.gz | |
Merge pull request #1713 from slskd/blacklist-regex
Allow blacklisting of username patterns
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/slskd.Tests.Unit/Core/Options/TransfersOptions.GroupsOptions.BlacklistedOptionsTests.cs | 130 | ||||
| -rw-r--r-- | tests/slskd.Tests.Unit/Users/UserServiceTests.cs | 304 |
2 files changed, 430 insertions, 4 deletions
diff --git a/tests/slskd.Tests.Unit/Core/Options/TransfersOptions.GroupsOptions.BlacklistedOptionsTests.cs b/tests/slskd.Tests.Unit/Core/Options/TransfersOptions.GroupsOptions.BlacklistedOptionsTests.cs new file mode 100644 index 00000000..2c77a8ba --- /dev/null +++ b/tests/slskd.Tests.Unit/Core/Options/TransfersOptions.GroupsOptions.BlacklistedOptionsTests.cs @@ -0,0 +1,130 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using Xunit; +using static slskd.Options; + +namespace slskd.Tests.Unit.Core.Options; + +using BlacklistedOptions = TransfersOptions.GroupsOptions.BlacklistedOptions; + +public class BlacklistedOptionsTests +{ + private static List<ValidationResult> Validate(BlacklistedOptions options) + { + var context = new ValidationContext(options); + var results = new List<ValidationResult>(); + Validator.TryValidateObject(options, context, results, validateAllProperties: true); + return results; + } + + public class Patterns + { + [Fact] + public void Valid_regex_produces_no_errors() + { + var options = new BlacklistedOptions { Patterns = [".*", "^foo$", @"\d+"] }; + Assert.Empty(Validate(options)); + } + + [Theory] + [InlineData("[invalid")] + [InlineData("(unclosed")] + [InlineData("*noanchor")] + public void Invalid_regex_produces_error(string pattern) + { + var options = new BlacklistedOptions { Patterns = [pattern] }; + var results = Validate(options); + Assert.Single(results); + Assert.Contains(pattern, results[0].ErrorMessage); + } + + [Fact] + public void Multiple_invalid_regexes_each_produce_an_error() + { + var options = new BlacklistedOptions { Patterns = ["[bad", "(also bad"] }; + Assert.Equal(2, Validate(options).Count); + } + + [Fact] + public void Mixed_valid_and_invalid_regexes_only_errors_on_invalid() + { + var options = new BlacklistedOptions { Patterns = ["valid.*", "[invalid"] }; + var results = Validate(options); + Assert.Single(results); + Assert.Contains("[invalid", results[0].ErrorMessage); + } + + [Fact] + public void Empty_patterns_produces_no_errors() + { + var options = new BlacklistedOptions { Patterns = [] }; + Assert.Empty(Validate(options)); + } + + [Fact] + public void Null_patterns_produces_no_errors() + { + var options = new BlacklistedOptions { Patterns = null }; + Assert.Empty(Validate(options)); + } + } + + public class Cidrs + { + [Theory] + [InlineData("192.168.1.0/24")] + [InlineData("10.0.0.0/8")] + [InlineData("1.2.3.4/32")] + [InlineData("0.0.0.0/0")] + public void Valid_cidr_produces_no_errors(string cidr) + { + var options = new BlacklistedOptions { Cidrs = [cidr] }; + Assert.Empty(Validate(options)); + } + + [Theory] + [InlineData("not-a-cidr")] + [InlineData("999.999.999.999/24")] + [InlineData("192.168.1.0/33")] + public void Invalid_cidr_produces_error(string cidr) + { + var options = new BlacklistedOptions { Cidrs = [cidr] }; + var results = Validate(options); + Assert.Single(results); + Assert.Contains(cidr, results[0].ErrorMessage); + } + + [Theory] + [InlineData("::ffff:192.168.1.1")] + [InlineData("::FFFF:10.0.0.1")] + [InlineData("::ffff:0:0/96")] + public void IPv4_mapped_IPv6_address_produces_error(string cidr) + { + var options = new BlacklistedOptions { Cidrs = [cidr] }; + var results = Validate(options); + Assert.Single(results); + Assert.Contains(cidr, results[0].ErrorMessage); + } + + [Fact] + public void Multiple_invalid_cidrs_each_produce_an_error() + { + var options = new BlacklistedOptions { Cidrs = ["bad1", "bad2"] }; + Assert.Equal(2, Validate(options).Count); + } + + [Fact] + public void Empty_cidrs_produces_no_errors() + { + var options = new BlacklistedOptions { Cidrs = [] }; + Assert.Empty(Validate(options)); + } + + [Fact] + public void Null_cidrs_produces_no_errors() + { + var options = new BlacklistedOptions { Cidrs = null }; + Assert.Empty(Validate(options)); + } + } +} diff --git a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs index 95e17e32..7cd4c492 100644 --- a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs +++ b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs @@ -1,7 +1,13 @@ namespace slskd.Tests.Unit.Users { + using System; using System.Collections.Generic; + using System.IO; + using System.Net; + using System.Threading.Tasks; using AutoFixture.Xunit2; + using Microsoft.Extensions.Caching.Memory; + using Microsoft.Extensions.Internal; using Moq; using slskd.Users; using Soulseek; @@ -56,6 +62,195 @@ namespace slskd.Tests.Unit.Users } } + public class IsBlacklisted + { + [Fact] + public void Throws_If_Username_Is_Null() + { + var (service, _) = GetFixture(); + + Assert.Throws<ArgumentNullException>(() => service.IsBlacklisted(null)); + } + + [Theory, AutoData] + public void Returns_Cached_Value_On_Cache_Hit(string username) + { + var options = new Options() + { + Transfers = new Options.TransfersOptions + { + Groups = new Options.TransfersOptions.GroupsOptions + { + Blacklisted = new Options.TransfersOptions.GroupsOptions.BlacklistedOptions + { + Members = new[] { username }, + } + } + } + }; + + var (service, _) = GetFixture(options); + + // prime the cache with a computed result + service.IsBlacklisted(username); + + // bypassCache = false returns false on a cache miss; if the result is still true, + // the value was returned from cache rather than computed again + Assert.True(service.IsBlacklisted(username, bypassCache: false)); + } + + [Theory, AutoData] + public void Returns_False_On_Cache_Miss_With_BypassCache_False(string username) + { + var options = new Options() + { + Transfers = new Options.TransfersOptions + { + Groups = new Options.TransfersOptions.GroupsOptions + { + Blacklisted = new Options.TransfersOptions.GroupsOptions.BlacklistedOptions + { + Members = new[] { username }, + } + } + } + }; + + var (service, _) = GetFixture(options); + + // the user is blacklisted, but bypassCache = false on a cache miss returns false + // without computing, so the result is false even though the user is blacklisted + Assert.False(service.IsBlacklisted(username, bypassCache: false)); + } + + [Theory, AutoData] + public void Returns_True_If_Username_Is_Blacklisted_And_BypassCache_True(string username) + { + var options = new Options() + { + Transfers = new Options.TransfersOptions + { + Groups = new Options.TransfersOptions.GroupsOptions + { + Blacklisted = new Options.TransfersOptions.GroupsOptions.BlacklistedOptions + { + Members = new[] { username }, + } + } + } + }; + + var (service, _) = GetFixture(options); + + Assert.True(service.IsBlacklisted(username, bypassCache: true)); + } + + [Theory, AutoData] + public void Returns_True_If_Username_Matches_Blacklist_Pattern(string prefix, string suffix) + { + var username = $"{prefix}_blacklisted_{suffix}"; + + var options = new Options() + { + Transfers = new Options.TransfersOptions + { + Groups = new Options.TransfersOptions.GroupsOptions + { + Blacklisted = new Options.TransfersOptions.GroupsOptions.BlacklistedOptions + { + Patterns = new[] { @".*_blacklisted_.*" }, + } + } + } + }; + + var (service, _) = GetFixture(options); + + Assert.True(service.IsBlacklisted(username)); + } + + [Theory, AutoData] + public void Returns_True_If_IP_Address_Is_In_Blacklisted_CIDR(string username) + { + var options = new Options() + { + Transfers = new Options.TransfersOptions + { + Groups = new Options.TransfersOptions.GroupsOptions + { + Blacklisted = new Options.TransfersOptions.GroupsOptions.BlacklistedOptions + { + Cidrs = new[] { "192.168.100.0/24" }, + } + } + } + }; + + var (service, _) = GetFixture(options); + + Assert.True(service.IsBlacklisted(username, IPAddress.Parse("192.168.100.5"))); + } + + [Theory, AutoData] + public async Task Returns_True_If_IP_Address_Is_In_Blacklist_File(string username) + { + var tempFile = Path.GetTempFileName(); + + try + { + System.IO.File.WriteAllText(tempFile, "192.168.200.0/24\n"); + + var (service, _) = GetFixture(); + + var blacklist = new Blacklist(); + await blacklist.Load(tempFile, BlacklistFormat.CIDR); + + service.SetField("<Blacklist>k__BackingField", blacklist); + + Assert.True(service.IsBlacklisted(username, IPAddress.Parse("192.168.200.5"))); + } + finally + { + System.IO.File.Delete(tempFile); + } + } + + [Theory, AutoData] + public void Returns_False_If_Not_Blacklisted_By_Any_Method(string username) + { + var (service, _) = GetFixture(); + + Assert.False(service.IsBlacklisted(username, IPAddress.Parse("10.0.0.1"))); + } + + [Theory, AutoData] + public void Caches_Result_Of_IsBlacklistedInternal(string username) + { + var options = new Options() + { + Transfers = new Options.TransfersOptions + { + Groups = new Options.TransfersOptions.GroupsOptions + { + Blacklisted = new Options.TransfersOptions.GroupsOptions.BlacklistedOptions + { + Members = new[] { username }, + } + } + } + }; + + var (service, _) = GetFixture(options); + + // first call: cache miss, IsBlacklistedInternal is called, result is cached + Assert.True(service.IsBlacklisted(username)); + + // second call with bypassCache = false: returns cached true rather than the + // false that a cache miss would produce, confirming the result was cached + Assert.True(service.IsBlacklisted(username, bypassCache: false)); + } + } + public class Configuration { [Theory, AutoData] @@ -131,25 +326,126 @@ namespace slskd.Tests.Unit.Users } } - private static (UserService service, Mocks mocks) GetFixture(Options options = null) + public class BlacklistDecisionCache { - var mocks = new Mocks(options); + [Theory, AutoData] + public void Clears_Cache_When_Options_Change(string username) + { + var options = new Options() + { + Transfers = new Options.TransfersOptions + { + Groups = new Options.TransfersOptions.GroupsOptions + { + Blacklisted = new Options.TransfersOptions.GroupsOptions.BlacklistedOptions + { + Members = new[] { username }, + } + } + } + }; + + var (service, mocks) = GetFixture(options); + + Assert.True(service.IsBlacklisted(username)); + Assert.True(service.IsBlacklisted(username, bypassCache: false)); + + mocks.OptionsMonitor.RaiseOnChange(new Options()); + + Assert.False(service.IsBlacklisted(username, bypassCache: false)); + } + + [Theory, AutoData] + public void Clears_Cache_When_Options_Change_Verified(string username) + { + var (service, mocks) = GetFixture(); + + var cache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 1000 }); + service.SetField("<BlacklistDecisionCache>k__BackingField", cache); + + cache.Set(username, true, new MemoryCacheEntryOptions { Size = 1 }); + Assert.True(cache.TryGetValue(username, out _)); + + mocks.OptionsMonitor.RaiseOnChange(new Options()); + + Assert.False(cache.TryGetValue(username, out _)); + } + + [Theory, AutoData] + public void Expires_Cache_Entries_After_10_Minutes(string username) + { + var clock = new TestSystemClock(); + + var options = new Options() + { + Transfers = new Options.TransfersOptions + { + Groups = new Options.TransfersOptions.GroupsOptions + { + Blacklisted = new Options.TransfersOptions.GroupsOptions.BlacklistedOptions + { + Members = new[] { username }, + } + } + } + }; + + var (service, _) = GetFixture(options, clock); + + Assert.True(service.IsBlacklisted(username)); + Assert.True(service.IsBlacklisted(username, bypassCache: false)); + + clock.UtcNow = clock.UtcNow.AddMinutes(11); + + Assert.False(service.IsBlacklisted(username, bypassCache: false)); + } + + [Theory, AutoData] + public void Expires_Cache_Entries_After_10_Minutes_Verified(string username) + { + var clock = new TestSystemClock(); + + var (service, _) = GetFixture(); + + var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock, SizeLimit = 1000 }); + service.SetField("<BlacklistDecisionCache>k__BackingField", cache); + + service.IsBlacklisted(username); + Assert.True(cache.TryGetValue(username, out _)); + + clock.UtcNow = clock.UtcNow.AddMinutes(11); + + Assert.False(cache.TryGetValue(username, out _)); + } + } + + private static (UserService service, Mocks mocks) GetFixture(Options options = null, ISystemClock systemClock = null) + { + var mocks = new Mocks(options, systemClock); var service = new UserService( mocks.SoulseekClient.Object, - mocks.OptionsMonitor); + mocks.OptionsMonitor, + mocks.SystemClock); return (service, mocks); } private class Mocks { - public Mocks(Options options = null) + public Mocks(Options options = null, ISystemClock systemClock = null) { OptionsMonitor = new TestOptionsMonitor<Options>(options ?? new Options()); + SystemClock = systemClock; } public Mock<ISoulseekClient> SoulseekClient { get; } = new Mock<ISoulseekClient>(); public TestOptionsMonitor<Options> OptionsMonitor { get; init; } + public ISystemClock SystemClock { get; init; } + } + + private class TestSystemClock : ISystemClock + { + public DateTimeOffset UtcNow { get; set; } = DateTimeOffset.UtcNow; } } } |
