miaopanel
1package waf
2
3import (
4 "strings"
5 "testing"
6)
7
8func validSettings() Settings {
9 return Settings{
10 Enabled: true, BlockStatus: 444, Rate: 20, Burst: 40, ConnPerIP: 30,
11 BadUA: true, Sensitive: true, RetentionDays: 7,
12 }
13}
14
15func TestSettingsValidate(t *testing.T) {
16 ok := validSettings()
17 if err := ok.Validate(); err != nil {
18 t.Fatalf("valid settings rejected: %v", err)
19 }
20
21 cases := []struct {
22 name string
23 mutate func(*Settings)
24 want string
25 }{
26 {"status", func(s *Settings) { s.BlockStatus = 500 }, "403 或 444"},
27 {"rate", func(s *Settings) { s.Rate = 0 }, "限速"},
28 {"burst", func(s *Settings) { s.Burst = 0 }, "突发"},
29 {"conn", func(s *Settings) { s.ConnPerIP = -1 }, "并发"},
30 {"retention", func(s *Settings) { s.RetentionDays = 0 }, "保留"},
31 {"bad ip", func(s *Settings) { s.Blacklist = []string{"not-an-ip"} }, "IP 格式"},
32 {"bad ua", func(s *Settings) { s.BadUAExtra = []string{`evil"}`} }, "UA 关键词"},
33 }
34 for _, c := range cases {
35 st := validSettings()
36 c.mutate(&st)
37 err := st.Validate()
38 if err == nil || !strings.Contains(err.Error(), c.want) {
39 t.Fatalf("%s: got %v, want containing %q", c.name, err, c.want)
40 }
41 }
42}
43
44func TestRuleInputValidate(t *testing.T) {
45 valid := RuleInput{
46 Name: "block bad path", MatchOn: "uri", Op: "contains",
47 Pattern: "/secret", Action: "block", Enabled: true,
48 }
49 if err := valid.Validate(); err != nil {
50 t.Fatalf("valid rule rejected: %v", err)
51 }
52 bad := []RuleInput{
53 {Name: "", MatchOn: "uri", Op: "contains", Pattern: "x", Action: "block"},
54 {Name: "x", MatchOn: "cookie", Op: "contains", Pattern: "x", Action: "block"},
55 {Name: "x", MatchOn: "uri", Op: "equals", Pattern: "x", Action: "block"},
56 {Name: "x", MatchOn: "uri", Op: "contains", Pattern: `x"; return 200`, Action: "block"},
57 {Name: "x", MatchOn: "uri", Op: "contains", Pattern: "x", Action: "log"},
58 {Name: "x", MatchOn: "method", Op: "regex", Pattern: "GET|POST", Action: "block"},
59 }
60 for i, r := range bad {
61 if err := r.Validate(); err == nil {
62 t.Fatalf("invalid rule %d accepted: %+v", i, r)
63 }
64 }
65}
66
67func TestAccessLineParsing(t *testing.T) {
68 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"`
69 m := accessLine.FindStringSubmatch(line)
70 if m == nil {
71 t.Fatal("access line did not match")
72 }
73 if m[1] != "1.2.3.4" || m[4] != "444" || m[7] != "sqlmap/1.5" || m[8] != "0.002" {
74 t.Fatalf("unexpected groups: %+v", m)
75 }
76 method, uri := splitRequest(m[3])
77 if method != "GET" || uri != "/wp-login.php" {
78 t.Fatalf("request split: %q %q", method, uri)
79 }
80}
81
82func TestRuleForStatus(t *testing.T) {
83 if ruleForStatus(429) != "rate-limit" || ruleForStatus(403) != "waf-block" || ruleForStatus(444) != "scanner" {
84 t.Fatal("unexpected rule labels")
85 }
86}
87
88func TestCollectorLiveSeries(t *testing.T) {
89 c := &Collector{live: map[int64]*liveAgg{}, offsets: map[string]int64{}}
90 series := c.LiveSeries(6)
91 if len(series["requests"]) != 6 || len(series["rt"]) != 6 {
92 t.Fatalf("series length: %+v", series)
93 }
94 qps, traffic, rt := c.Live()
95 if qps != 0 || traffic != 0 || rt != 0 {
96 t.Fatalf("empty collector: %v %v %v", qps, traffic, rt)
97 }
98}