Path: blob/dev/pkg/output/stats/waf/waf_test.go
2070 views
package waf12import (3"regexp"4"testing"5)67func TestWAFDetection(t *testing.T) {8detector := NewWafDetector()9if detector == nil {10t.Fatal("expected non-nil wafDetector")11}1213tests := []struct {14name string15content string16expectedWAF string17shouldMatch bool18}{19{20name: "Cloudflare WAF",21content: "Attention Required! | Cloudflare",22expectedWAF: "cloudflare",23shouldMatch: true,24},25{26name: "ModSecurity WAF",27content: "This error was generated by Mod_Security",28expectedWAF: "modsecurity",29shouldMatch: true,30},31{32name: "No WAF",33content: "Regular response with no WAF signature",34expectedWAF: "",35shouldMatch: false,36},37{38name: "Wordfence WAF",39content: "Generated by Wordfence",40expectedWAF: "wordfence",41shouldMatch: true,42},43{44name: "Sucuri WAF",45content: "Access Denied - Sucuri Website Firewall",46expectedWAF: "sucuri",47shouldMatch: true,48},49}5051for _, tt := range tests {52t.Run(tt.name, func(t *testing.T) {53waf, matched := detector.DetectWAF(tt.content)54if matched != tt.shouldMatch {55t.Errorf("expected match=%v, got match=%v", tt.shouldMatch, matched)56}57if matched && waf != tt.expectedWAF {58t.Errorf("expected WAF=%s, got WAF=%s", tt.expectedWAF, waf)59}60})61}62}6364func TestWAFDetectionNilPointerSafety(t *testing.T) {65tests := []struct {66name string67detector *WafDetector68content string69}{70{71name: "nil detector",72detector: nil,73content: "test content",74},75{76name: "nil regexCache",77detector: &WafDetector{78wafs: make(map[string]waf),79regexCache: nil,80},81content: "test content",82},83{84name: "regexCache with nil regex",85detector: &WafDetector{86wafs: make(map[string]waf),87regexCache: map[string]*regexp.Regexp{88"test": nil,89},90},91content: "test content",92},93{94name: "empty regexCache",95detector: &WafDetector{96wafs: make(map[string]waf),97regexCache: make(map[string]*regexp.Regexp),98},99content: "test content",100},101}102103for _, tt := range tests {104t.Run(tt.name, func(t *testing.T) {105defer func() {106if r := recover(); r != nil {107t.Errorf("DetectWAF panicked with nil pointer: %v", r)108}109}()110111waf, matched := tt.detector.DetectWAF(tt.content)112if matched {113t.Errorf("expected no match for nil pointer case, got match=true, waf=%s", waf)114}115if waf != "" {116t.Errorf("expected empty WAF string for nil pointer case, got waf=%s", waf)117}118})119}120}121122123