Files
urutau/internal/store/analytics_test.go
T
Lucas RayanandClaude Opus 5.5 9bdfdb491b feat: report preview page, sharp report logo, time to ban by blocker
The Reports page now previews the HTML report itself (same document as the
download and the print), with only Download and Print. The report logo is
the repository logo at 800 px. Survival splits fail2ban and the Urutau
second firewall.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 00:06:29 -03:00

186 lines
6.1 KiB
Go

package store
import (
"math"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
)
func TestGini(t *testing.T) {
if g := anGini(nil); g != 0 {
t.Fatalf("empty %v", g)
}
if g := anGini([]float64{5, 5, 5, 5}); g != 0 {
t.Fatalf("equal %v", g)
}
// one holds everything among 4: (n-1)/n = 0.75
if g := anGini([]float64{0, 0, 0, 100}); math.Abs(g-0.75) > 1e-9 {
t.Fatalf("concentrated %v", g)
}
if g := anGini([]float64{0, 0}); g != 0 {
t.Fatalf("zeros %v", g)
}
}
func TestPareto(t *testing.T) {
items := anPareto([]AnItem{{Key: "a", N: 60}, {Key: "b", N: 30}, {Key: "c", N: 10}}, 100)
if items[0].CumPct != 60 || items[1].CumPct != 90 || items[2].CumPct != 100 {
t.Fatalf("%+v", items)
}
if e := anPareto(nil, 0); len(e) != 0 {
t.Fatal("empty")
}
}
func TestKaplanMeierCensoring(t *testing.T) {
// times 10(ban) 20(censored) 30(ban) 40(ban): S(10)=3/4, at 30 risk=2 -> 3/4*1/2, at 40 risk=1 -> 0
c, med, p90 := anKaplanMeier([]anSubject{{T: 10, Event: true}, {T: 20, Event: false}, {T: 30, Event: true}, {T: 40, Event: true}})
want := []KMPoint{{0, 1}, {10, 0.75}, {30, 0.375}, {40, 0}}
if len(c) != len(want) {
t.Fatalf("%+v", c)
}
for i := range want {
if c[i] != want[i] {
t.Fatalf("%d: %+v want %+v", i, c[i], want[i])
}
}
if med == nil || *med != 30 || p90 == nil || *p90 != 40 {
t.Fatalf("median %v p90 %v", med, p90)
}
// everybody censored: no event, no median
c, med, p90 = anKaplanMeier([]anSubject{{T: 5, Event: false}, {T: 9, Event: false}})
if len(c) != 1 || med != nil || p90 != nil {
t.Fatalf("censored %+v", c)
}
c, med, _ = anKaplanMeier(nil)
if len(c) != 1 || med != nil {
t.Fatal("empty")
}
var many []anSubject
for i := 1; i <= 1000; i++ {
many = append(many, anSubject{T: int64(i), Event: true})
}
if c, _, _ := anKaplanMeier(many); len(c) != anKMPoints || c[len(c)-1].Survival != 0 {
t.Fatalf("downsample %d", len(c))
}
}
func TestAnalyticsEmpty(t *testing.T) {
s := store30("oracle")
f, to := t0.Unix()-86400, t0.Unix()
if e := s.Evolution(f, to, ""); e.Enabled || e.Days == nil {
t.Fatalf("no registry: %+v", e)
}
s = store30("oracle")
s.SetEventLog(openLog(t))
if e := s.Evolution(f, to, ""); !e.Enabled || e.Total != 0 || e.DeltaPct != nil || len(e.Days) < 2 {
t.Fatalf("empty evolution: %+v", e)
}
if c := s.Concentration(f, to, "", "ip"); !c.Enabled || c.Total != 0 || c.Items == nil || c.Gini != 0 {
t.Fatalf("empty concentration: %+v", c)
}
if c := s.Concentration(f, to, "", "bogus"); c.Enabled {
t.Fatal("bad dim")
}
if v := s.Survival(f, to, ""); !v.Enabled || v.MedianToBanS != nil || len(v.KM) != 1 || v.RecidivismPct != 0 {
t.Fatalf("empty survival: %+v", v)
}
}
func TestAnalyticsData(t *testing.T) {
s := store30("oracle")
s.SetEventLog(openLog(t))
at := func(d time.Duration) time.Time { return t0.Add(-d) }
mal := func(ip, path string, cat core.Category, d time.Duration) {
s.Ingest(core.Event{Time: at(d), IP: ip, Cat: cat, Source: "traefik", Domain: "a.example", Method: "GET", Path: path, Status: 404, UA: "curl/8"})
}
// 1.1.1.1 is the heavy hitter now; 2.2.2.2 and 3.3.3.3 are light; 9.9.9.9 only existed the day before
for i := 0; i < 8; i++ {
mal("1.1.1.1", "/.env", core.Scanner, time.Hour+time.Duration(i)*time.Minute)
}
mal("2.2.2.2", "/wp-login.php", core.Scanner, 2*time.Hour)
mal("3.3.3.3", "/xmlrpc.php", core.Scanner, 3*time.Hour)
mal("9.9.9.9", "/.git/config", core.Exploit, 30*time.Hour)
// bans: 1.1.1.1 twice (recidivist), 2.2.2.2 once; 3.3.3.3 never (censored)
ban := func(ip string, d time.Duration) {
s.Ingest(core.Event{Time: at(d), IP: ip, Cat: core.Ban, Source: "fail2ban", Jail: "sshd"})
}
ban("1.1.1.1", 50*time.Minute)
ban("1.1.1.1", 20*time.Minute)
ban("2.2.2.2", 2*time.Hour-300*time.Second)
f, to := t0.Unix()-86400, t0.Unix()
c := s.Concentration(f, to, "", "ip")
if c.Total != 10 || c.Distinct != 3 || c.Items[0].Key != "1.1.1.1" || c.Items[0].CumPct != 80 || c.Gini <= 0.3 {
t.Fatalf("concentration %+v", c)
}
if c.Top10Pct != 100 {
t.Fatalf("top10 %v", c.Top10Pct)
}
if p := s.Concentration(f, to, "", "path"); p.Items[0].Key != "/.env" || p.Items[0].N != 8 {
t.Fatalf("path %+v", p)
}
if d := s.Concentration(f, to, "", "domain"); d.Distinct != 1 || d.Gini != 0 {
t.Fatalf("domain %+v", d)
}
v := s.Survival(f, to, "")
if v.Attackers != 3 || v.Banned != 2 || v.RecidivismPct != 50 || len(v.Recidivists) != 1 || v.Recidivists[0].IP != "1.1.1.1" || v.Recidivists[0].Bans != 2 {
t.Fatalf("survival %+v", v)
}
// 1.1.1.1: first malicious 8m before the ban...: first event at -67m, ban at -50m = 1020 s; 2.2.2.2: 300 s; 3.3.3.3 censored at 3 h
if v.MedianToBanS == nil || *v.MedianToBanS != 1020 {
t.Fatalf("median %v km %+v", v.MedianToBanS, v.KM)
}
if v.P90ToBanS != nil {
t.Fatalf("p90 must be unknown with a censored attacker: %v", *v.P90ToBanS)
}
e := s.Evolution(f, to, "")
if e.Total != 10 || e.Prev != 1 || e.DeltaPct == nil || *e.DeltaPct != 900 {
t.Fatalf("evolution %+v", e)
}
var tot uint32
for _, d := range e.Days {
tot += d.Total
}
if tot != 10 {
t.Fatalf("days sum %d", tot)
}
var hd uint32
for _, r := range e.HourDow {
for _, n := range r {
hd += n
}
}
if hd != 10 {
t.Fatalf("hour_dow %d", hd)
}
}
func TestAnEpisodes(t *testing.T) {
end := int64(100000)
// episode 1: 0..60, banned at 100 (observed 100); episode 2: 200 alone, silent until 10000 (censored 200+1800-200);
// episode 3: 10000, 10100 then end of data (censored at last+30min)
got := anEpisodes([]int64{0, 60, 200, 10000, 10100}, []int64{100}, end)
want := []anSubject{{T: 100, Event: true, Ban: 100}, {T: 1800, Event: false}, {T: 1900, Event: false}}
if len(got) != len(want) {
t.Fatalf("%+v", got)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("%d: %+v want %+v", i, got[i], want[i])
}
}
// a ban long after the last event does not end that episode; censoring is capped at the window end
got = anEpisodes([]int64{0}, []int64{5000}, 600)
if len(got) != 1 || got[0] != (anSubject{T: 600, Event: false}) {
t.Fatalf("%+v", got)
}
if len(anEpisodes(nil, []int64{5}, end)) != 0 {
t.Fatal("ban without events")
}
}