45 lines
1.9 KiB
C#
45 lines
1.9 KiB
C#
using System.Net;
|
|
using Flypaper.Core.Models;
|
|
using Flypaper.Storage.Entities;
|
|
using Flypaper.Storage.Queries;
|
|
using Flypaper.Tests.Mcp;
|
|
using Shouldly;
|
|
|
|
namespace Flypaper.Tests.Storage;
|
|
|
|
public sealed class RepeatOffenderLookupTests : IDisposable
|
|
{
|
|
private readonly McpTestData _data = new();
|
|
|
|
[Fact]
|
|
public async Task Finds_only_timeouts_of_the_same_ip_inside_the_window()
|
|
{
|
|
using (var db = _data.Factory.CreateDbContext())
|
|
{
|
|
Add(db, "203.0.113.50", hoursAgo: 48, TrapOutcome.Timeout);
|
|
Add(db, "203.0.113.51", hoursAgo: 2, TrapOutcome.ClientAborted);
|
|
Add(db, "2001:db8::7", hoursAgo: 3, TrapOutcome.Timeout);
|
|
Add(db, "203.0.113.52", hoursAgo: 24 * 30, TrapOutcome.Timeout);
|
|
db.SaveChanges();
|
|
}
|
|
|
|
var lookup = new RepeatOffenderLookup(_data.Factory);
|
|
var since = McpTestData.Now.UtcDateTime.AddDays(-7);
|
|
|
|
(await lookup.HasTimedOutSinceAsync(IPAddress.Parse("203.0.113.50"), since, default)).ShouldBeTrue();
|
|
(await lookup.HasTimedOutSinceAsync(IPAddress.Parse("2001:db8::7"), since, default)).ShouldBeTrue();
|
|
(await lookup.HasTimedOutSinceAsync(IPAddress.Parse("203.0.113.51"), since, default)).ShouldBeFalse();
|
|
(await lookup.HasTimedOutSinceAsync(IPAddress.Parse("203.0.113.52"), since, default)).ShouldBeFalse();
|
|
(await lookup.HasTimedOutSinceAsync(IPAddress.Parse("203.0.113.99"), since, default)).ShouldBeFalse();
|
|
}
|
|
|
|
private static void Add(Flypaper.Storage.FlypaperDbContext db, string ip, int hoursAgo, TrapOutcome outcome)
|
|
{
|
|
var started = McpTestData.Now.AddHours(-hoursAgo);
|
|
db.TrapEvents.Add(TrapEventEntity.From(new TrapEvent(
|
|
Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse(ip), "html-drip", started, started.AddSeconds(3600), 100, outcome, "GET /", null)));
|
|
}
|
|
|
|
public void Dispose() => _data.Dispose();
|
|
}
|