using OpenWaf.Domain.Engine;
using OpenWaf.Domain.Policy;
using OpenWaf.Domain.Security;
namespace OpenWaf.Tests.Unit;
///
/// The netblock watch (OPEN-67): the edge's own live engine bans, grouped into
/// the block they all sit under, so the operator is offered one range instead of
/// twenty rows.
///
/// The arithmetic lives in as pure functions, and these
/// tests are the whole reason it is pure: the claim "these bans are one network"
/// is exactly the kind of claim that is easy to write and easy to get wrong by
/// one bit. A candidate that is one address too wide bans the neighbours of the
/// attacker; a candidate that never appears leaves the operator counting rows by
/// hand.
///
public sealed class NetblockWatchTests
{
private static EdgePolicy Policy(int threshold = 3, int v4 = 24, int v6 = 48) => new()
{
NetblockCandidateBans = threshold,
NetblockPrefixV4 = v4,
NetblockPrefixV6 = v6
};
private static IEnumerable<(string Key, DateTimeOffset CreatedAt)> Bans(params string[] keys)
{
var start = new DateTimeOffset(2026, 10, 5, 4, 0, 0, TimeSpan.Zero);
return keys.Select((k, i) => (k, start.AddMinutes(i)));
}
private static readonly DateTimeOffset Now = new(2026, 10, 5, 5, 0, 0, TimeSpan.Zero);
[Fact]
public void A_block_with_enough_bans_inside_it_is_a_candidate()
{
var found = Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.113.12"), Policy(), NetworkBanIndex.Empty, Now);
var candidate = Assert.Single(found);
Assert.Equal("203.0.113.0/24", candidate.Match);
Assert.Equal(24, candidate.Prefix);
Assert.Equal(3, candidate.Bans);
Assert.False(candidate.AlreadyBanned);
}
[Fact]
public void A_block_below_the_threshold_is_not_offered()
{
// Two bans in a /24 is two unrelated visitors of one hosting provider as
// often as it is a farm, and offering it would train the operator to
// ignore the table.
Assert.Empty(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11"), Policy(), NetworkBanIndex.Empty, Now));
}
[Fact]
public void Addresses_in_different_blocks_never_add_up_to_one_candidate()
{
Assert.Empty(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.114.12"), Policy(), NetworkBanIndex.Empty, Now));
}
[Fact]
public void A_threshold_of_zero_is_the_watch_off_rather_than_a_candidate_for_everything()
{
// 0 has to mean off. Read as "every block with zero or more bans", it
// would offer every /24 the edge has ever seen — the arithmetic version
// of a switch that is not there.
Assert.Empty(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.113.12"), Policy(threshold: 0),
NetworkBanIndex.Empty, Now));
}
[Fact]
public void The_prefix_the_operator_set_is_the_block_the_candidate_is_written_as()
{
var found = Netblocks.Candidates(
Bans("198.51.100.17", "198.51.100.18", "198.51.100.19"), Policy(v4: 16),
NetworkBanIndex.Empty, Now);
Assert.Equal("198.51.0.0/16", Assert.Single(found).Match);
}
[Fact]
public void A_ban_whose_own_key_is_a_wider_block_is_not_evidence_about_one_netblock()
{
// The edge can be configured to key a streak on a block (ipv4KeyPrefixLength).
// Counting one such ban as "one ban inside 203.0.113.0/24" would invent a
// candidate out of a row that already claims more than a /24.
Assert.Empty(Netblocks.Candidates(
Bans("203.0.113.0/8", "203.0.113.0/8", "203.0.113.0/8"), Policy(), NetworkBanIndex.Empty, Now));
}
[Fact]
public void A_key_that_names_no_address_is_skipped_rather_than_grouped()
{
// The "*" a /0 source key becomes is not inside any block.
Assert.Equal(-1, Netblocks.KeyPrefix("*"));
Assert.Empty(Netblocks.Candidates(Bans("*", "*", "*"), Policy(), NetworkBanIndex.Empty, Now));
}
[Fact]
public void v6_bans_are_grouped_one_allocation_up_from_the_key_they_are_stored_under()
{
// The streak key is already a /64, so grouping at /64 would only repeat
// the key. /48 is where a rotating v6 farm first looks like one thing.
var found = Netblocks.Candidates(
Bans("2001:db8:1:1::/64", "2001:db8:1:2::/64", "2001:db8:1:3::/64"),
Policy(), NetworkBanIndex.Empty, Now);
Assert.Equal("2001:db8:1::/48", Assert.Single(found).Match);
}
[Fact]
public void A_range_the_operator_already_wrote_is_said_rather_than_offered_again()
{
var ranges = new NetworkBanIndex(new[]
{
new NetworkBan { Match = "203.0.113.0/24", CreatedAt = Now.AddDays(-1) }
});
var candidate = Assert.Single(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.113.12"), Policy(), ranges, Now));
Assert.True(candidate.AlreadyBanned);
}
[Fact]
public void A_narrower_range_does_not_cover_the_block_it_sits_inside()
{
var ranges = new NetworkBanIndex(new[]
{
new NetworkBan { Match = "203.0.113.16/28", CreatedAt = Now.AddDays(-1) }
});
var candidate = Assert.Single(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.113.12"), Policy(), ranges, Now));
Assert.False(candidate.AlreadyBanned);
}
[Fact]
public void A_broader_range_covers_the_block_and_closes_the_offer()
{
var ranges = new NetworkBanIndex(new[]
{
new NetworkBan { Match = "203.0.0.0/16", CreatedAt = Now.AddDays(-1) }
});
var candidate = Assert.Single(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.113.12"), Policy(), ranges, Now));
Assert.True(candidate.AlreadyBanned);
}
[Fact]
public void The_oldest_and_newest_ban_of_a_block_travel_with_the_count()
{
var candidate = Assert.Single(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.113.12"), Policy(), NetworkBanIndex.Empty, Now));
Assert.Equal(new DateTimeOffset(2026, 10, 5, 4, 0, 0, TimeSpan.Zero), candidate.FirstBanAt);
Assert.Equal(new DateTimeOffset(2026, 10, 5, 4, 2, 0, TimeSpan.Zero), candidate.LastBanAt);
}
[Fact]
public void The_sample_shows_a_few_of_the_bans_and_not_the_whole_block()
{
var keys = Enumerable.Range(10, 12).Select(i => $"203.0.113.{i}").ToArray();
var candidate = Assert.Single(Netblocks.Candidates(Bans(keys), Policy(), NetworkBanIndex.Empty, Now));
Assert.Equal(12, candidate.Bans);
Assert.Equal(Netblocks.SampleSize, candidate.Sample.Count);
Assert.Contains("203.0.113.10", candidate.Sample);
}
[Fact]
public void The_widest_farm_is_offered_first()
{
var keys = new List();
keys.AddRange(Enumerable.Range(10, 5).Select(i => $"203.0.113.{i}"));
keys.AddRange(Enumerable.Range(20, 4).Select(i => $"198.51.100.{i}"));
var found = Netblocks.Candidates(Bans(keys.ToArray()), Policy(), NetworkBanIndex.Empty, Now);
Assert.Equal(2, found.Count);
Assert.Equal("203.0.113.0/24", found[0].Match);
Assert.Equal(5, found[0].Bans);
Assert.Equal("198.51.100.0/24", found[1].Match);
}
[Fact]
public void The_block_a_ban_sits_under_is_the_block_the_block_form_takes()
{
// The string on the surface has to be the string POST /admin-api/blocks
// accepts, or the offer and the control disagree.
var match = Netblocks.Covering("203.0.113.137", Policy());
Assert.Equal("203.0.113.128/25", Netblocks.Covering("203.0.113.137", Policy(v4: 25)));
Assert.Equal("203.0.113.0/24", match);
Assert.True(NetworkBans.TryNormalize(match, out var normalized, out _));
Assert.Equal(match, normalized);
}
[Fact]
public void The_prefix_a_ban_key_names_is_the_prefix_it_was_written_with()
{
Assert.Equal(32, Netblocks.KeyPrefix("203.0.113.10"));
Assert.Equal(64, Netblocks.KeyPrefix("2001:db8:1:2::/64"));
Assert.Equal(24, Netblocks.KeyPrefix("198.51.100.0/24"));
Assert.Equal(-1, Netblocks.KeyPrefix("not-an-address"));
Assert.Equal(-1, Netblocks.KeyPrefix(null));
}
[Fact]
public void The_defaults_are_the_allocation_sizes_this_project_assumes()
{
var policy = new EdgePolicy();
Assert.Equal(5, policy.NetblockCandidateBans);
Assert.Equal(24, policy.NetblockPrefixV4);
Assert.Equal(48, policy.NetblockPrefixV6);
}
[Fact]
public void The_bounds_refuse_a_block_that_is_most_of_the_address_space()
{
Assert.True(PolicyBounds.NetblockCandidateBansOk(0));
Assert.False(PolicyBounds.NetblockCandidateBansOk(-1));
Assert.False(PolicyBounds.NetblockCandidateBansOk(PolicyBounds.NetblockCandidateBansMax + 1));
// /8 is the floor and /0 is not: banning the floor is already a stretch,
// and banning below it is a shutdown dressed as a range — the same reason
// the block form itself refuses 0.0.0.0/0.
Assert.True(PolicyBounds.NetblockPrefixV4Ok(PolicyBounds.NetblockPrefixV4Min));
Assert.False(PolicyBounds.NetblockPrefixV4Ok(0));
Assert.False(PolicyBounds.NetblockPrefixV4Ok(32));
Assert.True(PolicyBounds.NetblockPrefixV6Ok(PolicyBounds.NetblockPrefixV6Min));
Assert.False(PolicyBounds.NetblockPrefixV6Ok(128));
}
[Fact]
public void A_stored_override_outside_the_bounds_is_named_rather_than_obeyed()
{
Assert.True(PolicyBounds.ValueOk("netblockCandidateBans", "0"));
Assert.False(PolicyBounds.ValueOk("netblockCandidateBans", "-3"));
Assert.True(PolicyBounds.ValueOk("netblockPrefixV4", "22"));
Assert.False(PolicyBounds.ValueOk("netblockPrefixV4", "33"));
Assert.False(PolicyBounds.ValueOk("netblockPrefixV6", "not-a-prefix"));
Assert.Contains("..", PolicyBounds.RangeOf("netblockPrefixV4"));
}
}