WAF增加若干个测试

This commit is contained in:
GoEdgeLab
2021-02-02 14:27:55 +08:00
parent e4766f76ce
commit 992d0560b6

View File

@@ -3,9 +3,11 @@ package utils
import ( import (
"net/http" "net/http"
"regexp" "regexp"
"runtime"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
"time"
) )
func TestMatchStringCache(t *testing.T) { func TestMatchStringCache(t *testing.T) {
@@ -38,7 +40,17 @@ func TestMatchRemoteCache(t *testing.T) {
} }
} }
func TestMatchBytesCache_WithoutCache(t *testing.T) {
data := []byte(strings.Repeat("HELLO", 512))
regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
before := time.Now()
t.Log(regex.Match(data))
t.Log(time.Since(before).Seconds()*1000, "ms")
}
func BenchmarkMatchStringCache(b *testing.B) { func BenchmarkMatchStringCache(b *testing.B) {
runtime.GOMAXPROCS(1)
data := strings.Repeat("HELLO", 512) data := strings.Repeat("HELLO", 512)
regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
_ = MatchStringCache(regex, data) _ = MatchStringCache(regex, data)
@@ -49,6 +61,8 @@ func BenchmarkMatchStringCache(b *testing.B) {
} }
func BenchmarkMatchStringCache_WithoutCache(b *testing.B) { func BenchmarkMatchStringCache_WithoutCache(b *testing.B) {
runtime.GOMAXPROCS(1)
data := strings.Repeat("HELLO", 512) data := strings.Repeat("HELLO", 512)
regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
@@ -56,3 +70,14 @@ func BenchmarkMatchStringCache_WithoutCache(b *testing.B) {
_ = regex.MatchString(data) _ = regex.MatchString(data)
} }
} }
func BenchmarkMatchBytesCache_WithoutCache(b *testing.B) {
runtime.GOMAXPROCS(1)
data := []byte(strings.Repeat("HELLO", 512))
regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
for i := 0; i < b.N; i++ {
_ = regex.Match(data)
}
}