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

51 lines
1.7 KiB
Go

package match
import (
"net/netip"
"testing"
)
func TestMatch(t *testing.T) {
m, err := Compile([]string{`from <IP> port`, `client: <IP>,`, `^conn <IP>$`}, []string{`trusted-scanner`})
if err != nil {
t.Fatal(err)
}
cases := map[string]string{
"from 203.0.113.9 port 22": "203.0.113.9",
"from 2001:db8::9 port 22": "2001:db8::9",
"from ::ffff:203.0.113.9 port 22": "203.0.113.9",
"from [2001:db8::9] port 22": "2001:db8::9",
"from fe80::1%eth0 port 22": "fe80::1",
"client: 203.0.113.9, server": "203.0.113.9",
"conn 2001:db8:1:2:3:4:5:6": "2001:db8:1:2:3:4:5:6",
"from 12:34:56 port 22": "", // a time, not an address
"from 999.1.1.1 port 22": "",
"from 203.0.113.9 port 22 trusted-scanner": "", // ignored
"nothing here": "",
}
for line, want := range cases {
a, ok := m.Match(line)
if got := map[bool]string{true: a.String(), false: ""}[ok]; got != want {
t.Errorf("%q: %q, want %q", line, got, want)
}
}
if _, err := Compile([]string{"<IP> and <IP>"}, nil); err == nil {
t.Error("two <IP> accepted")
}
if _, err := Compile([]string{"<IP> (unclosed"}, nil); err == nil {
t.Error("a broken pattern accepted")
}
}
func TestGroup(t *testing.T) {
if g := Group(netip.MustParseAddr("2001:db8:1:2:aa::1"), 64); g.String() != "2001:db8:1:2::/64" {
t.Errorf("/64: %s", g)
}
if g := Group(netip.MustParseAddr("203.0.113.9"), 64); g.String() != "203.0.113.9/32" {
t.Errorf("IPv4: %s", g)
}
if g := Group(netip.MustParseAddr("2001:db8::1"), 128); g.String() != "2001:db8::1/128" {
t.Errorf("/128: %s", g)
}
}