github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/utils/utils_test.go (about)

     1  package utils_test
     2  
     3  import (
     4  	"github.com/TeaOSLab/EdgeNode/internal/re"
     5  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
     6  	"github.com/TeaOSLab/EdgeNode/internal/waf/utils"
     7  	"github.com/iwind/TeaGo/rands"
     8  	"net/http"
     9  	"regexp"
    10  	"runtime"
    11  	"strconv"
    12  	"strings"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  func TestMatchStringCache(t *testing.T) {
    18  	regex := re.MustCompile(`\d+`)
    19  	t.Log(utils.MatchStringCache(regex, "123", utils.CacheShortLife))
    20  	t.Log(utils.MatchStringCache(regex, "123", utils.CacheShortLife))
    21  	t.Log(utils.MatchStringCache(regex, "123", utils.CacheShortLife))
    22  }
    23  
    24  func TestMatchBytesCache(t *testing.T) {
    25  	regex := re.MustCompile(`\d+`)
    26  	t.Log(utils.MatchBytesCache(regex, []byte("123"), utils.CacheShortLife))
    27  	t.Log(utils.MatchBytesCache(regex, []byte("123"), utils.CacheShortLife))
    28  	t.Log(utils.MatchBytesCache(regex, []byte("123"), utils.CacheShortLife))
    29  }
    30  
    31  func TestMatchRemoteCache(t *testing.T) {
    32  	if !testutils.IsSingleTesting() {
    33  		return
    34  	}
    35  
    36  	var client = http.Client{}
    37  	for i := 0; i < 200_0000; i++ {
    38  		req, err := http.NewRequest(http.MethodGet, "http://192.168.2.30:8882/?arg="+strconv.Itoa(i), nil)
    39  		if err != nil {
    40  			t.Fatal(err)
    41  		}
    42  		req.Header.Set("User-Agent", "GoTest/"+strconv.Itoa(i))
    43  		resp, err := client.Do(req)
    44  		if err != nil {
    45  			t.Fatal(err)
    46  		}
    47  		_ = resp.Body.Close()
    48  	}
    49  }
    50  
    51  func TestMatchBytesCache_WithoutCache(t *testing.T) {
    52  	data := []byte(strings.Repeat("HELLO", 512))
    53  	regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
    54  	before := time.Now()
    55  	t.Log(regex.Match(data))
    56  	t.Log(time.Since(before).Seconds()*1000, "ms")
    57  }
    58  
    59  func BenchmarkMatchStringCache(b *testing.B) {
    60  	runtime.GOMAXPROCS(1)
    61  
    62  	var data = strings.Repeat("HELLO", 128)
    63  	var regex = re.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
    64  	//b.Log(regex.Keywords())
    65  	_ = utils.MatchStringCache(regex, data, utils.CacheShortLife)
    66  
    67  	for i := 0; i < b.N; i++ {
    68  		_ = utils.MatchStringCache(regex, data, utils.CacheShortLife)
    69  	}
    70  }
    71  
    72  func BenchmarkMatchStringCache_LowHit(b *testing.B) {
    73  	runtime.GOMAXPROCS(1)
    74  
    75  	var regex = re.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
    76  	//b.Log(regex.Keywords())
    77  
    78  	for i := 0; i < b.N; i++ {
    79  		var data = strings.Repeat("A", rands.Int(0, 100))
    80  		_ = utils.MatchStringCache(regex, data, utils.CacheShortLife)
    81  	}
    82  }
    83  
    84  func BenchmarkMatchStringCache_WithoutCache(b *testing.B) {
    85  	runtime.GOMAXPROCS(1)
    86  
    87  	data := strings.Repeat("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36", 8)
    88  	regex := re.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
    89  	for i := 0; i < b.N; i++ {
    90  		_ = regex.MatchString(data)
    91  	}
    92  }
    93  
    94  func BenchmarkMatchStringCache_WithoutCache2(b *testing.B) {
    95  	runtime.GOMAXPROCS(1)
    96  
    97  	data := strings.Repeat("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36", 8)
    98  	regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
    99  	for i := 0; i < b.N; i++ {
   100  		_ = regex.MatchString(data)
   101  	}
   102  }
   103  
   104  func BenchmarkMatchBytesCache_WithoutCache(b *testing.B) {
   105  	runtime.GOMAXPROCS(1)
   106  
   107  	data := []byte(strings.Repeat("HELLO", 128))
   108  	regex := re.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
   109  
   110  	for i := 0; i < b.N; i++ {
   111  		_ = regex.Match(data)
   112  	}
   113  }