package wire import ( "net/netip" "strings" "testing" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/enforce" ) func TestValidCmdLineAgainstRealFormatters(t *testing.T) { now := time.Date(2026, 11, 15, 10, 20, 30, 0, time.UTC) for _, ip := range []string{"203.0.113.9", "2001:db8::1", "::ffff:198.51.100.7"} { a := netip.MustParseAddr(ip) for _, src := range []string{"local", "peer:oracle", "x y!"} { line, err := enforce.FormatLine(now, a, enforce.ReasonRCE, 87, 90*time.Minute, "host-1", src) if err != nil { t.Fatal(err) } if !ValidCmdLine(CmdDecision, line) { t.Errorf("FormatLine output rejected: %q", line) } if ValidCmdLine(CmdUnban, line) { t.Errorf("decision accepted as unban: %q", line) } } u := enforce.UnbanLine(now, a, 3600) if !ValidCmdLine(CmdUnban, u) { t.Errorf("UnbanLine output rejected: %q", u) } if ValidCmdLine(CmdDecision, u) { t.Errorf("unban accepted as decision: %q", u) } } } func TestValidCmdLineRejects(t *testing.T) { good := "2026-11-15T10:20:30Z urutau decision=ban ip=203.0.113.9 reason=scanner score=87 ttl=300 host=h src=local\n" if !ValidCmdLine(CmdDecision, good) { t.Fatal("baseline rejected") } bad := []string{ strings.TrimSuffix(good, "\n"), // no newline good + good, // two lines strings.Replace(good, "ip=203.0.113.9", "ip=999.1.1.1", 1), strings.Replace(good, "ban", "ban;rm", 1), strings.Replace(good, "src=local", "src=local\nINJECT", 1), strings.Replace(good, "reason=scanner", "reason=a b", 1), strings.Repeat("x", MaxCmdLine+1), "", } for _, l := range bad { if ValidCmdLine(CmdDecision, l) { t.Errorf("accepted %q", l) } } if ValidCmdLine("other", good) { t.Error("unknown kind accepted") } } func TestCheckHubURL(t *testing.T) { ok := []string{"https://hub.example.com", "https://hub.example.com/", "http://127.0.0.1:8790", "http://10.1.2.3:8790", "http://192.168.0.5", "http://urutau:8790", "http://localhost:8790", "http://[::1]:8790"} for _, u := range ok { if _, err := CheckHubURL(u); err != nil { t.Errorf("%s rejected: %v", u, err) } } bad := []string{"", "hub.example.com", "http://hub.example.com", "http://8.8.8.8", "ftp://hub", "https://user:pw@hub.example.com", "https://hub.example.com?x=1", "https://", "http://172.32.0.1"} for _, u := range bad { if _, err := CheckHubURL(u); err == nil { t.Errorf("%s accepted", u) } } u, _ := CheckHubURL("https://hub.example.com///") if u.String() != "https://hub.example.com" { t.Errorf("trailing slashes kept: %s", u) } } func TestValidName(t *testing.T) { for _, s := range []string{"oracle", "a", "host-1", "0abc", strings.Repeat("a", 32)} { if !ValidName(s) { t.Errorf("%q rejected", s) } } for _, s := range []string{"", "-a", "A", "a_b", "a.b", "a/b", "../x", strings.Repeat("a", 33), "a b"} { if ValidName(s) { t.Errorf("%q accepted", s) } } } func TestValidSource(t *testing.T) { for _, s := range Sources { if !ValidSource(s) { t.Errorf("%s", s) } } if ValidSource("nope") || ValidSource("") { t.Error("invalid accepted") } }