package ingress import ( "fmt" "path/filepath" "strings" "testing" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/wire" ) var t0 = time.Date(2026, 11, 20, 10, 5, 0, 0, time.UTC) func hitLine(marker, src, proto string, dpt int, flags string) string { return fmt.Sprintf("2026-11-20T10:05:00+00:00 host kernel: %s IN=eth0 OUT= MAC=aa SRC=%s DST=10.0.0.5 LEN=60 PROTO=%s SPT=4444 DPT=%d %s", marker, src, proto, dpt, flags) } func newAgg() *Agg { a := New() a.Now = func() time.Time { return t0 } return a } func TestParseHit(t *testing.T) { h, err := ParseHit([]byte(hitLine("URUTAU-IN:", "203.0.113.9", "TCP", 3389, "SYN")), t0) if err != nil || h.Proto != "tcp" || h.DPort != 3389 || h.Open || h.Src.String() != "203.0.113.9" { t.Fatalf("closed tcp: %+v %v", h, err) } h, err = ParseHit([]byte(hitLine("URUTAU-INO:", "203.0.113.9", "TCP", 22, "SYN")), t0) if err != nil || !h.Open { t.Fatalf("open tcp: %+v %v", h, err) } h, err = ParseHit([]byte(hitLine("URUTAU-IN:", "203.0.113.9", "ICMP", 0, "")), t0) if err != nil || h.Proto != "icmp" || h.DPort != 0 { t.Fatalf("icmp: %+v %v", h, err) } } func TestParseHitRejects(t *testing.T) { bad := map[string]string{ "not ours": "Nov 20 kernel: something else", "ack": hitLine("URUTAU-IN:", "203.0.113.9", "TCP", 80, "SYN ACK"), "no syn": hitLine("URUTAU-IN:", "203.0.113.9", "TCP", 80, "ACK"), "no port": hitLine("URUTAU-IN:", "203.0.113.9", "TCP", 0, "SYN"), "private src": hitLine("URUTAU-IN:", "192.168.1.5", "TCP", 80, "SYN"), "loopback src": hitLine("URUTAU-IN:", "127.0.0.1", "TCP", 80, "SYN"), "link-local": hitLine("URUTAU-IN:", "169.254.1.1", "TCP", 80, "SYN"), "multicast": hitLine("URUTAU-IN:", "224.0.0.1", "UDP", 80, ""), "bad src": "kernel: URUTAU-IN: SRC=999.1.1.1 DST=1.2.3.4 PROTO=TCP DPT=1 SYN", "gre": hitLine("URUTAU-IN:", "203.0.113.9", "GRE", 0, ""), "no proto": "kernel: URUTAU-IN: SRC=203.0.113.9 DST=1.2.3.4", "injection": "kernel: URUTAU-IN: SRC=203.0.113.9;rm DST=1.2.3.4 PROTO=TCP DPT=1 SYN", "oversize line": hitLine("URUTAU-IN:", "203.0.113.9", "TCP", 80, "SYN") + strings.Repeat(" X=1", 1000), } for name, l := range bad { if _, err := ParseHit([]byte(l), t0); err == nil { t.Errorf("%s: accepted", name) } } } func TestParseCounters(t *testing.T) { c, err := ParseCounters([]byte("ts=1767225600 total=120 tcp=100 udp=15 icmp=5 open=30 future=7")) if err != nil || c.Total != 120 || c.Open != 30 || c.TS != 1767225600 { t.Fatalf("%+v %v", c, err) } for name, l := range map[string]string{ "empty": "", "missing": "ts=1 total=1 tcp=1 udp=0 icmp=0", "duplicate": "ts=1 total=1 tcp=1 tcp=1 udp=0 icmp=0 open=0", "negative": "ts=1 total=-1 tcp=0 udp=0 icmp=0 open=0", "text": "ts=1 total=abc tcp=0 udp=0 icmp=0 open=0", "huge": "ts=1 total=99999999999999999999 tcp=0 udp=0 icmp=0 open=0", "no equals": "ts=1 total=1 tcp=1 udp=0 icmp=0 open=0 junk", "zero ts": "ts=0 total=1 tcp=1 udp=0 icmp=0 open=0", "open > l4": "ts=5 total=10 tcp=1 udp=0 icmp=0 open=9", "parts > total": "ts=5 total=10 tcp=100000 udp=0 icmp=0 open=0", "oversize": "ts=1 total=1 tcp=1 udp=0 icmp=0 open=0 " + strings.Repeat("x=1 ", 100), } { if _, err := ParseCounters([]byte(l)); err == nil { t.Errorf("%s: accepted", name) } } } func cnt(ts int64, total, tcp, udp, icmp, open int) []byte { return []byte(fmt.Sprintf("ts=%d total=%d tcp=%d udp=%d icmp=%d open=%d", ts, total, tcp, udp, icmp, open)) } func TestExactTotalsFromCounterDeltas(t *testing.T) { a := newAgg() base := t0.Unix() - 600 a.Ingest(wire.SrcIngressCnt, cnt(base, 1000, 900, 90, 10, 100)) // baseline: no delta a.Ingest(wire.SrcIngressCnt, cnt(base+60, 1100, 980, 110, 10, 130)) a.Ingest(wire.SrcIngressCnt, cnt(base+120, 1160, 1030, 120, 10, 140)) a.Ingest(wire.SrcIngressCnt, cnt(base+120, 5000, 1030, 120, 10, 140)) // same ts: ignored r := a.Report(base-3600, t0.Unix()+3600) if r.Totals.Attempts != 160 || r.Totals.TCP != 130 || r.Totals.UDP != 30 || r.Totals.ICMP != 0 || r.Totals.Open != 40 { t.Fatalf("totals %+v", r.Totals) } if r.Totals.Closed != 120 || r.Totals.ClosedPct != 75 { t.Fatalf("closed %+v", r.Totals) } if !r.Enabled || r.Source.State != "following" { t.Fatalf("source %+v", r.Source) } } func TestCounterResetCountsFromZero(t *testing.T) { a := newAgg() base := t0.Unix() - 300 a.Ingest(wire.SrcIngressCnt, cnt(base, 1000, 1000, 0, 0, 0)) a.Ingest(wire.SrcIngressCnt, cnt(base+60, 40, 40, 0, 0, 5)) // table reloaded r := a.Report(base-3600, t0.Unix()+3600) if r.Totals.Attempts != 40 { t.Fatalf("reset: %+v", r.Totals) } } func TestCounterAbsurdDeltaAndTimesDropped(t *testing.T) { a := newAgg() base := t0.Unix() - 300 a.Ingest(wire.SrcIngressCnt, cnt(base, 1, 1, 0, 0, 0)) a.Ingest(wire.SrcIngressCnt, cnt(base+60, 1<<45, 1<<45, 0, 0, 0)) a.Ingest(wire.SrcIngressCnt, cnt(t0.Unix()+86400*5, 10, 10, 0, 0, 0)) // far future a.Ingest(wire.SrcIngressCnt, cnt(t0.Unix()-86400*400, 10, 10, 0, 0, 0)) if r := a.Report(0, t0.Unix()+86400*10); r.Totals.Attempts != 0 { t.Fatalf("absurd: %+v", r.Totals) } if a.Info().Malformed < 3 { t.Fatalf("malformed %d", a.Info().Malformed) } } func TestSampleDetailsRatioAndBannedShare(t *testing.T) { a := newAgg() banned := map[string]bool{"203.0.113.9": true} a.Banned = func(ip string) bool { return banned[ip] } a.Geo = func(ip string) (string, string, string) { return "RU", "AS64500", "Example Net" } base := t0.Unix() - 300 a.Ingest(wire.SrcIngressCnt, cnt(base, 0, 0, 0, 0, 0)) a.Ingest(wire.SrcIngressCnt, cnt(base+60, 1000, 1000, 0, 0, 0)) for i := 0; i < 6; i++ { a.Ingest(wire.SrcIngress, []byte(hitLine("URUTAU-IN:", "203.0.113.9", "TCP", 3389, "SYN"))) } for i := 0; i < 4; i++ { a.Ingest(wire.SrcIngress, []byte(hitLine("URUTAU-INO:", "198.51.100.7", "TCP", 22, "SYN"))) } r := a.Report(base-3600, t0.Unix()+3600) if r.Sample.Logged != 10 || r.Sample.LoggedBanned != 6 || r.Sample.BannedPct != 60 { t.Fatalf("sample %+v", r.Sample) } if r.Sample.Ratio != 0.01 || r.Sample.BannedEst != 600 { t.Fatalf("ratio %+v", r.Sample) } if len(r.Ports) != 2 || r.Ports[0].Port != 3389 || r.Ports[0].Served || r.Ports[0].N != 6 || !r.Ports[1].Served { t.Fatalf("ports %+v", r.Ports) } if len(r.ASNs) != 1 || r.ASNs[0].ASN != "AS64500" || r.ASNs[0].N != 10 || len(r.CCs) != 1 || r.CCs[0].CC != "RU" { t.Fatalf("origins %+v %+v", r.ASNs, r.CCs) } // the ban state is read at the time of the line: unbanning later changes nothing retroactively delete(banned, "203.0.113.9") if r2 := a.Report(base-3600, t0.Unix()+3600); r2.Sample.LoggedBanned != 6 { t.Fatalf("retroactive: %+v", r2.Sample) } } func TestBucketsAreBounded(t *testing.T) { a := newAgg() a.Geo = func(ip string) (string, string, string) { return "", "", "" } for i := 0; i < maxPorts*3; i++ { a.Ingest(wire.SrcIngress, []byte(hitLine("URUTAU-IN:", "203.0.113.9", "TCP", 1000+i, "SYN"))) } a.mu.Lock() n := len(a.hours[hourOf(t0.Unix())].Ports) a.mu.Unlock() if n != maxPorts { t.Fatalf("ports kept %d, want %d", n, maxPorts) } } func TestHostileLinesNeverPanic(t *testing.T) { a := newAgg() for _, l := range []string{"", "\x00\x01", strings.Repeat("A", 100000), "URUTAU-IN:", "URUTAU-INO: SRC= DST= PROTO=", "URUTAU-IN: SRC=::ffff:203.0.113.1 DST=1.1.1.1 PROTO=TCP DPT=99999 SYN"} { a.Ingest(wire.SrcIngress, []byte(l)) a.Ingest(wire.SrcIngressCnt, []byte(l)) } a.Ingest("bogus", []byte("x")) } func TestSaveLoadAndRetention(t *testing.T) { a := newAgg() base := t0.Unix() - 300 a.Ingest(wire.SrcIngressCnt, cnt(base, 0, 0, 0, 0, 0)) a.Ingest(wire.SrcIngressCnt, cnt(base+60, 50, 50, 0, 0, 0)) p := filepath.Join(t.TempDir(), "ingress.json") if err := a.Save(p); err != nil { t.Fatal(err) } b := newAgg() if err := b.Load(p); err != nil { t.Fatal(err) } if r := b.Report(0, t0.Unix()+3600); r.Totals.Attempts != 50 { t.Fatalf("loaded %+v", r.Totals) } // a later clock prunes the old hour b.Now = func() time.Time { return t0.Add(Retention + 2*time.Hour) } b.Ingest(wire.SrcIngressCnt, cnt(b.Now().Unix(), 1, 1, 0, 0, 0)) if r := b.Report(0, b.Now().Unix()+3600); r.Totals.Attempts != 1 { // only the new hour (the counters restarted) t.Fatalf("not pruned: %+v", r.Totals) } } func TestReportOfMergesHosts(t *testing.T) { mk := func(n int) *Agg { a := newAgg() base := t0.Unix() - 300 a.Ingest(wire.SrcIngressCnt, cnt(base, 0, 0, 0, 0, 0)) a.Ingest(wire.SrcIngressCnt, cnt(base+60, n, n, 0, 0, 0)) return a } r := ReportOf([]*Agg{mk(10), mk(32), nil}, 0, t0.Unix()+3600) if r.Totals.Attempts != 42 || len(r.Hourly) != 1 { t.Fatalf("%+v", r) } } func TestWaitingWithoutData(t *testing.T) { r := newAgg().Report(0, t0.Unix()) if r.Enabled || r.Source.State != "waiting" || r.Hourly == nil || r.Ports == nil { t.Fatalf("%+v", r) } }