Skip to content

fix(test): Fuzzing testing fail concurrent #1261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions images/dvcr-artifact/fuzz.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,6 @@ for file in ${files}; do
kill "$fuzz_pid" 2>/dev/null || true
wait "$fuzz_pid" 2>/dev/null || true

# kill workers if they are still running
pids=$(ps aux | grep 'fuzzworker' | awk '{print $2}')
if [[ ! -z "$pids" ]]; then
echo "$pids" | xargs kill 2>/dev/null || true
sleep 1 # wait a moment for them to terminate
echo "$pids" | xargs kill -9 2>/dev/null || true
fi

break
fi

Expand Down
57 changes: 23 additions & 34 deletions images/dvcr-artifact/pkg/fuzz/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"fmt"
"net/http"
"os"
"regexp"
"strconv"
"strings"
Expand All @@ -31,19 +30,19 @@ import (
"github.com/hashicorp/go-cleanhttp"
)

func ProcessRequests(t *testing.T, data []byte, addr string, methods ...string) {
t.Helper()
func ProcessRequests(tb *testing.T, data []byte, addr string, methods ...string) {
tb.Helper()

if len(methods) == 0 {
t.Fatalf("no methods specified")
tb.Fatalf("no methods specified")
}
for _, method := range methods {
ProcessRequest(t, data, addr, method)
ProcessRequest(tb, data, addr, method)
}
}

func ProcessRequest(t testing.TB, data []byte, addr, method string) {
t.Helper()
func ProcessRequest(tb testing.TB, data []byte, addr, method string) {
tb.Helper()

switch method {
case
Expand All @@ -57,26 +56,26 @@ func ProcessRequest(t testing.TB, data []byte, addr, method string) {
http.MethodOptions,
http.MethodTrace:

req := newFuzzRequest().Fuzz(t, data, method, addr)
req := newFuzzRequest().Fuzz(tb, data, method, addr)
defer req.Body.Close()

resp := fuzzHTTPRequest(t, req)
resp := fuzzHTTPRequest(tb, req)
if resp != nil {
if resp.StatusCode > 500 {
t.Errorf("resp: %v", resp)
tb.Errorf("resp: %v", resp)
}
defer resp.Body.Close()
}
default:
t.Errorf("Unsupported HTTP method: %s", method)
tb.Errorf("Unsupported HTTP method: %s", method)
}
}

func fuzzHTTPRequest(t testing.TB, fuzzReq *http.Request) *http.Response {
t.Helper()
func fuzzHTTPRequest(tb testing.TB, fuzzReq *http.Request) *http.Response {
tb.Helper()

if fuzzReq == nil {
t.Skip("Skipping test because fuzzReq is nil")
tb.Skip("Skipping test because fuzzReq is nil")
}
client := cleanhttp.DefaultClient()
client.Timeout = time.Second
Expand All @@ -98,11 +97,11 @@ func fuzzHTTPRequest(t testing.TB, fuzzReq *http.Request) *http.Response {
return nil
}

t.Logf("fuzzing request, %s, %s", fuzzReq.Method, fuzzReq.URL)
tb.Logf("fuzzing request, %s, %s", fuzzReq.Method, fuzzReq.URL)

resp, err := client.Do(fuzzReq)
if err != nil && !strings.Contains(err.Error(), "checkRedirect disabled for test") {
t.Logf("err: %s", err)
tb.Logf("err: %s", err)
}

return resp
Expand All @@ -114,14 +113,14 @@ func newFuzzRequest() *fuzzRequest {
return &fuzzRequest{}
}

func (s *fuzzRequest) Fuzz(t testing.TB, data []byte, method, addr string) *http.Request {
t.Helper()
func (s *fuzzRequest) Fuzz(tb testing.TB, data []byte, method, addr string) *http.Request {
tb.Helper()

bodyReader := bytes.NewBuffer(data)

req, err := http.NewRequest(method, addr, bodyReader)
if err != nil {
t.Skipf("Skipping test: not enough data for fuzzing: %s", err.Error())
tb.Skipf("Skipping test: not enough data for fuzzing: %s", err.Error())
}

// Get the address of the local listener in order to attach it to an Origin header.
Expand All @@ -130,10 +129,13 @@ func (s *fuzzRequest) Fuzz(t testing.TB, data []byte, method, addr string) *http

fuzzConsumer := fuzz.NewConsumer(data)
var headersMap map[string]string
fuzzConsumer.FuzzMap(&headersMap)
err = fuzzConsumer.FuzzMap(&headersMap)
if err != nil {
tb.Skipf("Skipping test: not enough data for fuzzing: %s", err.Error())
}

for k, v := range headersMap {
for i := 0; i < len(v); i++ {
for range len(v) {
req.Header.Add(k, v)
}
}
Expand All @@ -144,16 +146,3 @@ func (s *fuzzRequest) Fuzz(t testing.TB, data []byte, method, addr string) *http

return req
}

func GetPortFromEnv(env string, defaultPort int) (port int, err error) {
portEnv := os.Getenv(env)
if portEnv == "" {
return defaultPort, nil
}

port, err = strconv.Atoi(portEnv)
if err != nil {
return 0, fmt.Errorf("failed to parse port env var %s: %w", env, err)
}
return port, nil
}
4 changes: 2 additions & 2 deletions images/dvcr-artifact/pkg/uploader/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ func (app *uploadServerApp) processUpload(irc imageReadCloser, w http.ResponseWr
w.WriteHeader(http.StatusBadRequest)
}

err = app.upload(readCloser, cdiContentType, dvContentType, parseHTTPHeader(r))

app.mutex.Lock()
defer app.mutex.Unlock()

err = app.upload(readCloser, cdiContentType, dvContentType, parseHTTPHeader(r))

if err != nil {
klog.Errorf("Saving stream failed: %s", err)
w.WriteHeader(http.StatusInternalServerError)
Expand Down
Loading