Files
openwaf/dotnet/OpenWaf.Tests.Unit/LiveStreaksTests.cs
T
Pavlo Ratushnyi 0e2aee6beb A capped listing answered like it was the whole answer, and one said it was capped when it was not
?source= is refused by name now; a limit nobody meant still silently became a
different limit. Measured on the shipped image (openwaf:ci), 1103 sources
recorded:

  GET /admin-api/observations?limit=100000 -> 200, 1000 rows, no total, no
      truncation flag. 103 recorded sources were unreachable and the response
      read like a complete answer.
  GET /admin-api/observations?limit=0 and ?limit=-1 -> 200, one row each.
  GET /admin-api/streaks?limit=1000 -> 200, 2 streaks, trackedSources 3,
      X-Truncated: true. Nothing had been cut: the header compared the page
      against the size of the streak map, and the map keeps a source whose
      streak a clean answer ended while the listing leaves those out on purpose.

The block listing already carried X-Total-Count and X-Truncated, with a comment
saying that is what stops a page reading like a complete answer. The two
listings the console reads most had neither. Now every capped listing carries
both, takes an offset that reaches the rest, and refuses a limit outside its
range by name: "limit must be 1..1000; got 0". /streaks measures truncation
against standing streaks, reports standingStreaks next to trackedSources, and
pages.

The page says what it is showing. The console gate run printed "newest 25 of 42
recorded sources" and "Newest 25 of 274 alerts" — before this change both tables
rendered as if the page were the window, and the volume that survives compose
down is exactly where that matters.

Teeth, each reverted and counted, then restored: headers removed -> 2
integration failures; limit/offset clamped instead of refused -> 13; streak
header counted against the map again -> 1; standingTotal counted as the map size
-> 1 unit + 1 integration.

Gates on this tree: unit 128/128, integration 89/89, smoke passed 19 failed 0,
compose 43 passed 0 failed, console 42 assertions 0 failed.
2026-10-02 02:19:27 +02:00

125 lines
5.1 KiB
C#

using OpenWaf.Domain.Engine;
using OpenWaf.Domain.Policy;
namespace OpenWaf.Tests.Unit;
/// <summary>
/// The operator's question is "who is about to be banned", which is answered by
/// the current consecutive count. The durable observation row only keeps the
/// maximum ever seen, so a list built from it would show a source that has been
/// clean for an hour as if it were still standing at the gate.
/// </summary>
public sealed class LiveStreaksTests
{
private static readonly DateTimeOffset T0 = new(2026, 10, 1, 12, 0, 0, TimeSpan.Zero);
private static readonly EdgePolicy Policy = new()
{
StreakThreshold = 5,
StreakWindow = TimeSpan.FromMinutes(5),
StateIdleTtl = TimeSpan.FromMinutes(10)
};
[Fact]
public void A_clean_answer_takes_the_source_out_of_the_list_even_though_the_record_remains()
{
var t = new StreakTracker();
for (var i = 0; i < 4; i++) t.Observe("1.1.1.1", 404, ResponseClass.Refusal, Policy, T0.AddSeconds(i));
t.Observe("1.1.1.1", 200, ResponseClass.Neutral, Policy, T0.AddSeconds(10));
Assert.Empty(t.LiveStreaks(T0.AddSeconds(11), Policy, 10));
Assert.Equal(4, t.Peek("1.1.1.1")!.MaxConsecutiveRefusals);
}
[Fact]
public void The_list_is_ordered_by_how_close_a_source_is_to_a_ban_and_capped_by_the_limit()
{
var t = new StreakTracker();
for (var i = 0; i < 3; i++) t.Observe("3.3.3.3", 404, ResponseClass.Refusal, Policy, T0.AddSeconds(i));
for (var i = 0; i < 2; i++) t.Observe("2.2.2.2", 404, ResponseClass.Refusal, Policy, T0.AddSeconds(i));
t.Observe("1.1.1.1", 404, ResponseClass.Refusal, Policy, T0);
var live = t.LiveStreaks(T0.AddSeconds(5), Policy, 2);
Assert.Equal(2, live.Count);
Assert.Equal("3.3.3.3", live[0].SourceKey);
Assert.Equal(3, live[0].ConsecutiveRefusals);
Assert.Equal("2.2.2.2", live[1].SourceKey);
Assert.Equal(2, live[1].ConsecutiveRefusals);
}
[Fact]
public void Idle_sources_are_dropped_before_the_list_is_read()
{
var t = new StreakTracker();
t.Observe("9.9.9.9", 404, ResponseClass.Refusal, Policy, T0);
Assert.Single(t.LiveStreaks(T0.AddMinutes(1), Policy, 10));
Assert.Empty(t.LiveStreaks(T0.AddMinutes(30), Policy, 10));
}
[Fact]
public void ResetIfTracked_ends_a_standing_run_and_creates_nothing_for_a_source_it_has_never_seen()
{
var t = new StreakTracker();
t.Observe("1.1.1.1", 404, ResponseClass.Refusal, Policy, T0);
t.ResetIfTracked("9.9.9.9");
Assert.Equal(1, t.TrackedCount);
t.ResetIfTracked("1.1.1.1");
Assert.Empty(t.LiveStreaks(T0.AddSeconds(1), Policy, 10));
Assert.Equal(1, t.Peek("1.1.1.1")!.MaxConsecutiveRefusals);
}
/// <summary>
/// The number a caller needs to answer "is there more" is the number of
/// standing streaks, not the size of the map. The endpoint used to compare
/// its page against TrackedCount and so reported a cut that had not
/// happened: on the shipped image, two standing streaks plus one source whose
/// streak a clean answer ended gave X-Truncated: true at limit=1000.
/// </summary>
[Fact]
public void The_standing_total_counts_what_the_listing_can_show_rather_than_every_record_the_map_holds()
{
var t = new StreakTracker();
for (var i = 0; i < 3; i++) t.Observe("3.3.3.3", 404, ResponseClass.Refusal, Policy, T0.AddSeconds(i));
t.Observe("2.2.2.2", 404, ResponseClass.Refusal, Policy, T0);
t.Observe("1.1.1.1", 404, ResponseClass.Refusal, Policy, T0);
t.Observe("1.1.1.1", 200, ResponseClass.Neutral, Policy, T0.AddSeconds(1));
Assert.Equal(3, t.TrackedCount);
var page = t.LiveStreaks(T0.AddSeconds(2), Policy, 10, 0, out var standing);
Assert.Equal(2, standing);
Assert.Equal(2, page.Count);
}
[Fact]
public void An_offset_walks_the_standing_list_so_a_cap_hides_nothing_a_bigger_limit_could_reach()
{
var t = new StreakTracker();
for (var i = 0; i < 3; i++) t.Observe("3.3.3.3", 404, ResponseClass.Refusal, Policy, T0.AddSeconds(i));
for (var i = 0; i < 2; i++) t.Observe("2.2.2.2", 404, ResponseClass.Refusal, Policy, T0.AddSeconds(i));
t.Observe("1.1.1.1", 404, ResponseClass.Refusal, Policy, T0);
var first = t.LiveStreaks(T0.AddSeconds(5), Policy, 2, 0, out var standing);
var second = t.LiveStreaks(T0.AddSeconds(5), Policy, 2, 2, out _);
Assert.Equal(3, standing);
Assert.Equal(new[] { "3.3.3.3", "2.2.2.2" }, first.Select(x => x.SourceKey).ToArray());
Assert.Equal(new[] { "1.1.1.1" }, second.Select(x => x.SourceKey).ToArray());
}
[Fact]
public void A_server_error_streak_shows_up_because_it_is_the_other_way_a_source_escalates()
{
var t = new StreakTracker();
t.Observe("8.8.8.8", 503, ResponseClass.ServerError, Policy, T0);
var live = t.LiveStreaks(T0.AddSeconds(1), Policy, 10);
Assert.Single(live);
Assert.Equal(0, live[0].ConsecutiveRefusals);
Assert.Equal(1, live[0].ConsecutiveServerErrors);
}
}