Files
abrown38andClaude Opus 5.5 d06bf8d531 Guard: watch logs, ban misbehaving addresses in Housecarl
Files and the journal, patterns with one <IP>, bounties within a window,
bans in a Housecarl ban list with repeat offenders' bans growing; presets
for sshd, jarl, housecarl, steward, bind-xfr, postfix-sasl, dovecot,
nginx-auth and housecarl-drops; guard run/check/test/status/bans/ban/unban;
a Debian package with a sandboxed unit; an end-to-end test with Housecarl.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 16:29:37 +01:00

31 lines
859 B
Go

package bounty
import (
"net/netip"
"testing"
"time"
)
func TestTracker(t *testing.T) {
tr := New(3, time.Minute)
p := netip.MustParsePrefix("203.0.113.9/32")
t0 := time.Now()
if n, r := tr.Add(p, t0); n != 1 || r {
t.Errorf("first: %d %v", n, r)
}
// Two minutes later the first is out of the window.
tr.Add(p, t0.Add(2*time.Minute))
if n, r := tr.Add(p, t0.Add(2*time.Minute+time.Second)); n != 2 || r {
t.Errorf("window: %d %v", n, r)
}
if top := tr.Top(5, t0.Add(2*time.Minute+time.Second)); len(top) != 1 || top[0].Bounty != 2 || top[0].Address != "203.0.113.9" {
t.Errorf("top %+v", top)
}
if n, r := tr.Add(p, t0.Add(2*time.Minute+2*time.Second)); n != 3 || !r {
t.Errorf("reached: %d %v", n, r)
}
if top := tr.Top(5, t0.Add(3*time.Minute)); len(top) != 0 {
t.Errorf("after the ban the bounty starts again: %+v", top)
}
}