Add data management, randomized HTML drip and public README
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3671019e60
commit
f5d8ca7484
46 files changed
+1601
-172
No files matched your search
@@ -0,0 +1,144 @@
|
||||
using System.Net;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Storage;
|
||||
using Flypaper.Storage.Entities;
|
||||
using Flypaper.Storage.Maintenance;
|
||||
using Flypaper.Tests.Fakes;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Storage;
|
||||
|
||||
public sealed class TrapDataMaintenanceTests : IDisposable
|
||||
{
|
||||
private static readonly DateTimeOffset Now = new(2026, 11, 14, 12, 0, 0, TimeSpan.Zero);
|
||||
|
||||
private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"flypaper-maintenance-{Guid.NewGuid():N}.db");
|
||||
private readonly IDbContextFactory<FlypaperDbContext> _dbContextFactory;
|
||||
private readonly TrapDataMaintenance _maintenance;
|
||||
|
||||
public TrapDataMaintenanceTests()
|
||||
{
|
||||
_dbContextFactory = new TestDbContextFactory($"Data Source={_dbPath}");
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
db.Database.Migrate();
|
||||
_maintenance = new TrapDataMaintenance(_dbContextFactory);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Deletes_only_events_from_private_networks()
|
||||
{
|
||||
await SeedAsync(
|
||||
("203.0.113.9", 1),
|
||||
("192.168.1.20", 1),
|
||||
("172.19.0.1", 1),
|
||||
("127.0.0.1", 1),
|
||||
("fd00::1", 1),
|
||||
("fe80::1", 1),
|
||||
("172.32.0.1", 1),
|
||||
("8.8.8.8", 1));
|
||||
|
||||
(await _maintenance.DeletePrivateAsync()).ShouldBe(5);
|
||||
|
||||
(await RemainingIpsAsync()).ShouldBe(["203.0.113.9", "172.32.0.1", "8.8.8.8"], ignoreOrder: true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Deletes_all_events_but_keeps_settings()
|
||||
{
|
||||
await SeedAsync(("203.0.113.9", 1), ("8.8.8.8", 2));
|
||||
await using (var db = await _dbContextFactory.CreateDbContextAsync())
|
||||
{
|
||||
db.Settings.Add(new SettingEntity { Key = "Flypaper:MaxConnections", Value = "5" });
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
(await _maintenance.DeleteAllAsync()).ShouldBe(2);
|
||||
await _maintenance.VacuumAsync();
|
||||
|
||||
await using var check = await _dbContextFactory.CreateDbContextAsync();
|
||||
(await check.TrapEvents.CountAsync()).ShouldBe(0);
|
||||
(await check.Settings.CountAsync()).ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Reports_count_oldest_event_and_file_size()
|
||||
{
|
||||
await SeedAsync(("203.0.113.9", 10), ("8.8.8.8", 3));
|
||||
|
||||
var stats = await _maintenance.GetStatsAsync();
|
||||
|
||||
stats.Count.ShouldBe(2);
|
||||
stats.Oldest.ShouldBe(Now.UtcDateTime.AddDays(-10));
|
||||
stats.DatabaseBytes.ShouldNotBeNull().ShouldBeGreaterThan(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Retention_deletes_events_older_than_configured_days()
|
||||
{
|
||||
await SeedAsync(("203.0.113.9", 100), ("203.0.113.10", 91), ("203.0.113.11", 89), ("203.0.113.12", 1));
|
||||
|
||||
(await CreateCleanup(days: 90).CleanupAsync(CancellationToken.None)).ShouldBe(2);
|
||||
|
||||
(await RemainingIpsAsync()).ShouldBe(["203.0.113.11", "203.0.113.12"], ignoreOrder: true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Retention_of_zero_days_keeps_everything()
|
||||
{
|
||||
await SeedAsync(("203.0.113.9", 4000));
|
||||
|
||||
(await CreateCleanup(days: 0).CleanupAsync(CancellationToken.None)).ShouldBe(0);
|
||||
|
||||
(await RemainingIpsAsync()).ShouldBe(["203.0.113.9"]);
|
||||
}
|
||||
|
||||
private RetentionCleanup CreateCleanup(int days) => new(
|
||||
_maintenance,
|
||||
TestOptions.Monitor(new RetentionOptions { Days = days }),
|
||||
new FixedTimeProvider(Now),
|
||||
NullLogger<RetentionCleanup>.Instance);
|
||||
|
||||
private async Task SeedAsync(params (string Ip, int DaysAgo)[] events)
|
||||
{
|
||||
await using var db = await _dbContextFactory.CreateDbContextAsync();
|
||||
foreach (var (ip, daysAgo) in events)
|
||||
{
|
||||
var started = Now.AddDays(-daysAgo);
|
||||
db.TrapEvents.Add(TrapEventEntity.From(new TrapEvent(
|
||||
Guid.NewGuid(),
|
||||
TrapProtocol.Http,
|
||||
IPAddress.Parse(ip),
|
||||
"drip",
|
||||
started,
|
||||
started.AddSeconds(5),
|
||||
5,
|
||||
TrapOutcome.ClientAborted)));
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private async Task<List<string>> RemainingIpsAsync()
|
||||
{
|
||||
await using var db = await _dbContextFactory.CreateDbContextAsync();
|
||||
return await db.TrapEvents.Select(e => e.RemoteIp).ToListAsync();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
SqliteConnection.ClearAllPools();
|
||||
foreach (var suffix in new[] { "", "-wal", "-shm" })
|
||||
{
|
||||
File.Delete(_dbPath + suffix);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class TestDbContextFactory(string connectionString) : IDbContextFactory<FlypaperDbContext>
|
||||
{
|
||||
public FlypaperDbContext CreateDbContext() =>
|
||||
new(new DbContextOptionsBuilder<FlypaperDbContext>().UseSqlite(connectionString).Options);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user