using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using OpenWaf.Edge.Configuration;
using OpenWaf.Infrastructure.Persistence;
namespace OpenWaf.Tests.Integration;
///
/// Retention used to be a constant on the policy record: the operator could
/// neither shorten the privacy window nor lengthen the period an incident stays
/// explainable, and no surface said what the window was (OPEN-19). These tests
/// are the two halves of having a lever — a bad value is refused by name at
/// startup, and a good value is what the prune actually honours.
///
public sealed class RetentionConfigTests
{
[Theory]
[InlineData(0)]
[InlineData(-5)]
[InlineData(400)]
public void A_retention_window_outside_the_bound_is_refused_by_name(int days)
{
var ex = Assert.Throws(() => new EdgePolicyOptions
{ ObservationRetentionDays = days }.ToPolicy());
Assert.Contains("OpenWaf:Policy:ObservationRetentionDays", ex.Message);
Assert.Contains(days.ToString(), ex.Message);
}
[Theory]
[InlineData(1)]
[InlineData(365)]
public void The_bound_is_inclusive_on_both_ends(int days)
{
var policy = new EdgePolicyOptions
{
ObservationRetentionDays = days,
BanRetentionDays = days
}.ToPolicy();
Assert.Equal(TimeSpan.FromDays(days), policy.ObservationRetention);
Assert.Equal(TimeSpan.FromDays(days), policy.BanRetention);
}
[Fact]
public async Task The_two_windows_are_separate_knobs_and_the_default_reports_itself_on_both_surfaces()
{
await using var f = new EdgeFixture(observationRetentionDays: 30, banRetentionDays: 2);
var state = await (await f.Client.GetAsync($"/admin-api/state?token={EdgeFixture.AdminToken}")).Content.ReadAsStringAsync();
Assert.Contains("\"observationRetentionDays\":30", state);
Assert.Contains("\"banRetentionDays\":2", state);
var policy = await (await f.Client.GetAsync($"/admin-api/policy?token={EdgeFixture.AdminToken}")).Content.ReadAsStringAsync();
Assert.Contains("\"observationRetentionDays\":30", policy);
Assert.Contains("\"banRetentionDays\":2", policy);
}
[Fact]
public async Task A_shortened_window_prunes_what_the_default_window_would_have_kept()
{
// The tooth: with the default 7-day window these rows survive, so a
// prune that ignored the configured value would still pass a test that
// only backdated rows by eight days.
await using var f = new EdgeFixture(observationRetentionDays: 1);
foreach (var ip in new[] { "203.0.113.41", "203.0.113.42", "203.0.113.43" })
await f.SendAsync(f.Client, "/", ip);
await f.FlushObservationsAsync();
using (var scope = f.Factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
foreach (var o in await db.Observations.OrderBy(o => o.SourceKey).Take(2).ToListAsync())
o.LastSeenAt = DateTimeOffset.UtcNow.AddDays(-3);
await db.SaveChangesAsync();
}
await f.Service().PruneAsync(CancellationToken.None);
using (var scope = f.Factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
Assert.Equal(1, await db.Observations.CountAsync());
}
}
[Fact]
public async Task A_lengthened_ban_window_keeps_the_evidence_row_the_default_would_have_pruned()
{
await using var f = new EdgeFixture(banRetentionDays: 30);
var now = DateTimeOffset.UtcNow;
using (var scope = f.Factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
db.Blocks.Add(new OpenWaf.Domain.Security.BlockEntry
{
SourceKey = "203.0.113.44",
Source = OpenWaf.Domain.Security.BlockSource.Engine,
ExpiresAt = now.AddDays(-10),
Reason = "engine ban, ten days past expiry"
});
await db.SaveChangesAsync();
}
await f.Service().PruneAsync(CancellationToken.None);
using (var scope = f.Factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
Assert.Equal(1, await db.Blocks.CountAsync(b => b.SourceKey == "203.0.113.44"));
}
}
}