|
|
@@ -5,12 +5,15 @@ import (
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/netip"
|
|
|
|
"net/netip"
|
|
|
|
|
|
|
|
"runtime"
|
|
|
|
"slices"
|
|
|
|
"slices"
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
|
|
|
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@@ -302,8 +305,24 @@ func worstCaseBodies() []visitorRequest {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// inspectionTime returns how long crs takes to inspect tc, and fails t unless
|
|
|
|
// threadCPUTime returns the processor time the calling thread has used.
|
|
|
|
// it reads all of tc's body and Coraza parses it.
|
|
|
|
func threadCPUTime(t *testing.T) time.Duration {
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var now unix.Timespec
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := unix.ClockGettime(unix.CLOCK_THREAD_CPUTIME_ID, &now)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatalf("read the thread's processor time: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return time.Duration(now.Nano())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// inspectionTime returns the processor time crs takes to inspect tc, and
|
|
|
|
|
|
|
|
// fails t unless it reads all of tc's body and Coraza parses it. Inspect does
|
|
|
|
|
|
|
|
// its work on the calling goroutine, which stays on one thread meanwhile, so
|
|
|
|
|
|
|
|
// that thread's processor time is the inspection's.
|
|
|
|
func inspectionTime(
|
|
|
|
func inspectionTime(
|
|
|
|
t *testing.T, crs *coreruleset.CoreRuleSet, tc visitorRequest,
|
|
|
|
t *testing.T, crs *coreruleset.CoreRuleSet, tc visitorRequest,
|
|
|
|
) time.Duration {
|
|
|
|
) time.Duration {
|
|
|
@@ -318,11 +337,14 @@ func inspectionTime(
|
|
|
|
req.Header.Set("Content-Type", tc.contentType)
|
|
|
|
req.Header.Set("Content-Type", tc.contentType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
began := time.Now()
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
began := threadCPUTime(t)
|
|
|
|
result, read, _ := crs.Inspect(
|
|
|
|
result, read, _ := crs.Inspect(
|
|
|
|
req, netip.MustParseAddr("192.0.2.1"), strings.NewReader(tc.body),
|
|
|
|
req, netip.MustParseAddr("192.0.2.1"), strings.NewReader(tc.body),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
took := time.Since(began)
|
|
|
|
took := threadCPUTime(t) - began
|
|
|
|
|
|
|
|
|
|
|
|
if string(read) != tc.body {
|
|
|
|
if string(read) != tc.body {
|
|
|
|
t.Errorf("read %d bytes of %d", len(read), len(tc.body))
|
|
|
|
t.Errorf("read %d bytes of %d", len(read), len(tc.body))
|
|
|
@@ -336,10 +358,12 @@ func inspectionTime(
|
|
|
|
return took
|
|
|
|
return took
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime runs one at a time, so
|
|
|
|
// TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime measures processor
|
|
|
|
// that the tests running in parallel do not slow it. Other work on the host
|
|
|
|
// time, not time on the clock, so that the time other work on the host holds
|
|
|
|
// can still slow one try, so a request fails only when three tries in a row
|
|
|
|
// the processor does not count. It runs one at a time, so that the tests
|
|
|
|
// take too long.
|
|
|
|
// running in parallel do not slow it. Other work can still slow one try a
|
|
|
|
|
|
|
|
// little, by sharing the processor's caches, so a request fails only when
|
|
|
|
|
|
|
|
// three tries in a row take too long.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//nolint:paralleltest // one at a time, as above
|
|
|
|
//nolint:paralleltest // one at a time, as above
|
|
|
|
func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) {
|
|
|
|
func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) {
|
|
|
|