package waf import ( "strings" "testing" ) func validSettings() Settings { return Settings{ Enabled: true, BlockStatus: 444, Rate: 20, Burst: 40, ConnPerIP: 30, BadUA: true, Sensitive: true, RetentionDays: 7, } } func TestSettingsValidate(t *testing.T) { ok := validSettings() if err := ok.Validate(); err != nil { t.Fatalf("valid settings rejected: %v", err) } cases := []struct { name string mutate func(*Settings) want string }{ {"status", func(s *Settings) { s.BlockStatus = 500 }, "403 或 444"}, {"rate", func(s *Settings) { s.Rate = 0 }, "限速"}, {"burst", func(s *Settings) { s.Burst = 0 }, "突发"}, {"conn", func(s *Settings) { s.ConnPerIP = -1 }, "并发"}, {"retention", func(s *Settings) { s.RetentionDays = 0 }, "保留"}, {"bad ip", func(s *Settings) { s.Blacklist = []string{"not-an-ip"} }, "IP 格式"}, {"bad ua", func(s *Settings) { s.BadUAExtra = []string{`evil"}`} }, "UA 关键词"}, } for _, c := range cases { st := validSettings() c.mutate(&st) err := st.Validate() if err == nil || !strings.Contains(err.Error(), c.want) { t.Fatalf("%s: got %v, want containing %q", c.name, err, c.want) } } } func TestRuleInputValidate(t *testing.T) { valid := RuleInput{ Name: "block bad path", MatchOn: "uri", Op: "contains", Pattern: "/secret", Action: "block", Enabled: true, } if err := valid.Validate(); err != nil { t.Fatalf("valid rule rejected: %v", err) } bad := []RuleInput{ {Name: "", MatchOn: "uri", Op: "contains", Pattern: "x", Action: "block"}, {Name: "x", MatchOn: "cookie", Op: "contains", Pattern: "x", Action: "block"}, {Name: "x", MatchOn: "uri", Op: "equals", Pattern: "x", Action: "block"}, {Name: "x", MatchOn: "uri", Op: "contains", Pattern: `x"; return 200`, Action: "block"}, {Name: "x", MatchOn: "uri", Op: "contains", Pattern: "x", Action: "log"}, {Name: "x", MatchOn: "method", Op: "regex", Pattern: "GET|POST", Action: "block"}, } for i, r := range bad { if err := r.Validate(); err == nil { t.Fatalf("invalid rule %d accepted: %+v", i, r) } } } func TestAccessLineParsing(t *testing.T) { line := `1.2.3.4 - - [19/Oct/2026:23:13:53 +0800] "GET /wp-login.php HTTP/1.1" 444 0 "-" "sqlmap/1.5" "0.002"` m := accessLine.FindStringSubmatch(line) if m == nil { t.Fatal("access line did not match") } if m[1] != "1.2.3.4" || m[4] != "444" || m[7] != "sqlmap/1.5" || m[8] != "0.002" { t.Fatalf("unexpected groups: %+v", m) } method, uri := splitRequest(m[3]) if method != "GET" || uri != "/wp-login.php" { t.Fatalf("request split: %q %q", method, uri) } } func TestRuleForStatus(t *testing.T) { if ruleForStatus(429) != "rate-limit" || ruleForStatus(403) != "waf-block" || ruleForStatus(444) != "scanner" { t.Fatal("unexpected rule labels") } } func TestCollectorLiveSeries(t *testing.T) { c := &Collector{live: map[int64]*liveAgg{}, offsets: map[string]int64{}} series := c.LiveSeries(6) if len(series["requests"]) != 6 || len(series["rt"]) != 6 { t.Fatalf("series length: %+v", series) } qps, traffic, rt := c.Live() if qps != 0 || traffic != 0 || rt != 0 { t.Fatalf("empty collector: %v %v %v", qps, traffic, rt) } }