aboutsummaryrefslogtreecommitdiffstats
path: root/tests/slskd.Tests.Unit/Users
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-04-24 09:44:44 -0500
committerJP Dillingham <jp@dillingham.me>2026-04-24 09:44:44 -0500
commit64fb4c12a013ed1b50ddcf57fb4bd16decb928f0 (patch)
tree3d647cf4f912fa6437e0d0030e99f577e6f0a2c7 /tests/slskd.Tests.Unit/Users
parent19fba8293b472939d74ed23232577cbfdf523586 (diff)
parentfc722e4a78aa5369fcd5ca0d8e88bd98d874a1d8 (diff)
downloadslskd-64fb4c12a013ed1b50ddcf57fb4bd16decb928f0.tar.gz
Merge remote-tracking branch 'origin/master' into retries
Diffstat (limited to 'tests/slskd.Tests.Unit/Users')
-rw-r--r--tests/slskd.Tests.Unit/Users/UserServiceTests.cs306
1 files changed, 301 insertions, 5 deletions
diff --git a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs
index 7680ec11..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
+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 @@
}
}
+ 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 @@
}
}
- 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;
}
}
}