diff --git a/internal/waf/template_test.go b/internal/waf/template_test.go index 4304a7d..7a32b73 100644 --- a/internal/waf/template_test.go +++ b/internal/waf/template_test.go @@ -13,6 +13,7 @@ import ( "mime/multipart" "net/http" "net/url" + "runtime" "strings" "testing" "time" @@ -66,6 +67,8 @@ func Test_Template2(t *testing.T) { } func BenchmarkTemplate(b *testing.B) { + runtime.GOMAXPROCS(4) + wafInstance, err := waf.Template() if err != nil { b.Fatal(err) @@ -73,15 +76,17 @@ func BenchmarkTemplate(b *testing.B) { b.ResetTimer() - for i := 0; i < b.N; i++ { - req, err := http.NewRequest(http.MethodGet, "https://example.com/index.php?id=123", nil) - if err != nil { - b.Fatal(err) - } - req.Header.Set("User-Agent", testUserAgent) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + req, err := http.NewRequest(http.MethodGet, "https://example.com/index.php?id=123"+types.String(rand.Int()%10000), nil) + if err != nil { + b.Fatal(err) + } + req.Header.Set("User-Agent", testUserAgent) - _, _, _, _, _ = wafInstance.MatchRequest(requests.NewTestRequest(req), nil, firewallconfigs.ServerCaptchaTypeNone) - } + _, _, _, _, _ = wafInstance.MatchRequest(requests.NewTestRequest(req), nil, firewallconfigs.ServerCaptchaTypeNone) + } + }) } func testTemplate1010(a *assert.Assertion, t *testing.T, template *waf.WAF) { @@ -430,3 +435,35 @@ func testTemplate20001(a *assert.Assertion, t *testing.T, template *waf.WAF) { } } } + +func BenchmarkTemplatePathTraversal(b *testing.B) { + runtime.GOMAXPROCS(4) + + template, err := waf.Template() + if err != nil { + b.Fatal(err) + } + var group = template.FindRuleGroupWithCode("pathTraversal") + if group == nil { + b.Fatal("group not found") + return + } + + b.ResetTimer() + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + req, err := http.NewRequest(http.MethodPost, "https://example.com/?id=1234"+types.String(rand.Int()%10000)+"&name=lily&time=12345678910", nil) + if err != nil { + b.Fatal(err) + } + req.Header.Set("User-Agent", testUserAgent) + + _, _, result, err := group.MatchRequest(requests.NewTestRequest(req)) + if err != nil { + b.Fatal(err) + } + _ = result + } + }) +}