Skip to content

Commit

Permalink
fuzz Parse against net.SplitHostPort (fixes #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jub0bs committed Aug 30, 2024
1 parent c5fff8e commit 8ce6f33
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions internal/origins/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package origins

import (
"net"
"strconv"
"strings"
"testing"
)
Expand All @@ -26,6 +28,46 @@ func FuzzConsistencyBetweenParsePatternAndParse(f *testing.F) {
})
}

func FuzzConsistencyBetweenParseAndSplitHostPort(f *testing.F) {
for _, c := range parseCases {
f.Add(c.input)
}
f.Fuzz(func(t *testing.T, raw string) {
origin, ok := Parse(raw)
if !ok {
t.Skip()
}
// Elide scheme and scheme-host separator.
_, hostPort, found := strings.Cut(raw, "://")
if !found {
t.Skip()
}
// Check whether the host is valid;
// Parse is lenient about this but,
// if both Parse and parseHostPattern succeed,
// then we expect the host to be valid.
_, _, err := parseHostPattern(hostPort, hostPort)
if err != nil {
t.Skip()
}
// Check the port; if it's absent, skip this case,
// since net.SplitHostPort would choke on it.
if origin.Port == 0 {
t.Skip()
}
wantHost, wantPort, err := net.SplitHostPort(hostPort)
if err != nil {
t.Fatal(err)
}
gotHost := origin.Host.Value
gotPort := strconv.FormatInt(int64(origin.Port), 10)
if gotHost != wantHost || gotPort != wantPort {
const tmpl = "(host, port) of %q: got (%q, %s); want (%q, %s)"
t.Errorf(tmpl, hostPort, gotHost, gotPort, wantHost, wantPort)
}
})
}

func FuzzParsePattern(f *testing.F) {
for _, c := range parsePatternCases {
f.Add(c.input)
Expand Down

0 comments on commit 8ce6f33

Please sign in to comment.