Skip to content

Commit

Permalink
add fuzz tests to all headers (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Feb 22, 2024
1 parent 55fa72f commit c93d5c5
Show file tree
Hide file tree
Showing 32 changed files with 177 additions and 326 deletions.
159 changes: 29 additions & 130 deletions pkg/headers/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,136 +131,6 @@ func TestRangeUnmarshal(t *testing.T) {
}
}

func TestRangeUnmarshalErrors(t *testing.T) {
for _, ca := range []struct {
name string
hv base.HeaderValue
err string
}{
{
"empty",
base.HeaderValue{},
"value not provided",
},
{
"2 values",
base.HeaderValue{"a", "b"},
"value provided multiple times ([a b])",
},
{
"invalid keys",
base.HeaderValue{`key1="k`},
"apexes not closed (key1=\"k)",
},
{
"value not found",
base.HeaderValue{``},
"value not found ()",
},
{
"smpte without values",
base.HeaderValue{`smpte=`},
"invalid value ()",
},
{
"smtpe end invalid",
base.HeaderValue{`smpte=00:00:01-123`},
"invalid SMPTE time (123)",
},
{
"smpte invalid 1",
base.HeaderValue{`smpte=123-`},
"invalid SMPTE time (123)",
},
{
"smpte invalid 2",
base.HeaderValue{`smpte=aa:00:00-`},
"strconv.ParseUint: parsing \"aa\": invalid syntax",
},
{
"smpte invalid 3",
base.HeaderValue{`smpte=00:aa:00-`},
"strconv.ParseUint: parsing \"aa\": invalid syntax",
},
{
"smpte invalid 4",
base.HeaderValue{`smpte=00:00:aa-`},
"strconv.ParseUint: parsing \"aa\": invalid syntax",
},
{
"smpte invalid 5",
base.HeaderValue{`smpte=00:00:00:aa-`},
"strconv.ParseUint: parsing \"aa\": invalid syntax",
},
{
"smpte invalid 6",
base.HeaderValue{`smpte=00:00:00:aa.00-`},
"strconv.ParseUint: parsing \"aa\": invalid syntax",
},
{
"smpte invalid 7",
base.HeaderValue{`smpte=00:00:00:00.aa-`},
"strconv.ParseUint: parsing \"aa\": invalid syntax",
},
{
"npt without values",
base.HeaderValue{`npt=`},
"invalid value ()",
},
{
"npt end invalid",
base.HeaderValue{`npt=00:00:00-aa`},
"strconv.ParseFloat: parsing \"aa\": invalid syntax",
},
{
"npt invalid 1",
base.HeaderValue{`npt=00:00:00:00-`},
"invalid NPT time (00:00:00:00)",
},
{
"npt invalid 2",
base.HeaderValue{`npt=aa-`},
"strconv.ParseFloat: parsing \"aa\": invalid syntax",
},
{
"npt invalid 3",
base.HeaderValue{`npt=aa:00-`},
"strconv.ParseUint: parsing \"aa\": invalid syntax",
},
{
"npt invalid 4",
base.HeaderValue{`npt=aa:00:00-`},
"strconv.ParseUint: parsing \"aa\": invalid syntax",
},
{
"clock without values",
base.HeaderValue{`clock=`},
"invalid value ()",
},
{
"clock end invalid",
base.HeaderValue{`clock=20060102T150405Z-aa`},
"parsing time \"aa\" as \"20060102T150405Z\": cannot parse \"aa\" as \"2006\"",
},
{
"clock invalid 1",
base.HeaderValue{`clock=aa-`},
"parsing time \"aa\" as \"20060102T150405Z\": cannot parse \"aa\" as \"2006\"",
},
{
"time invalid",
base.HeaderValue{`time=aa`},
"parsing time \"aa\" as \"20060102T150405Z\": cannot parse \"aa\" as \"2006\"",
},
} {
t.Run(ca.name, func(t *testing.T) {
var h Range
err := h.Unmarshal(ca.hv)
require.EqualError(t, err, ca.err)
})
}
}

func TestRangeMarshal(t *testing.T) {
for _, ca := range casesRange {
t.Run(ca.name, func(t *testing.T) {
Expand All @@ -269,3 +139,32 @@ func TestRangeMarshal(t *testing.T) {
})
}
}

func FuzzRangeUnmarshal(f *testing.F) {
for _, ca := range casesRange {
f.Add(ca.vin[0])
}

f.Add("smtpe=")
f.Add("npt=")
f.Add("clock=")

f.Fuzz(func(t *testing.T, b string) {
var h Range
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
})
}

func TestRangeAdditionalErrors(t *testing.T) {
func() {
var h Range
err := h.Unmarshal(base.HeaderValue{})
require.Error(t, err)
}()

func() {
var h Range
err := h.Unmarshal(base.HeaderValue{"a", "b"})
require.Error(t, err)
}()
}
5 changes: 4 additions & 1 deletion pkg/headers/rtpinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ func (h *RTPInfo) Unmarshal(v base.HeaderValue) error {
return err
}

urlReceived := false

for k, v := range kvs {
switch k {
case "url":
e.URL = v
urlReceived = true

case "seq":
vi, err := strconv.ParseUint(v, 10, 16)
Expand All @@ -65,7 +68,7 @@ func (h *RTPInfo) Unmarshal(v base.HeaderValue) error {
}
}

if e.URL == "" {
if !urlReceived {
return fmt.Errorf("URL is missing")
}

Expand Down
70 changes: 25 additions & 45 deletions pkg/headers/rtpinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,51 +142,6 @@ func TestRTPInfoUnmarshal(t *testing.T) {
}
}

func TestRTPInfoUnmarshalErrors(t *testing.T) {
for _, ca := range []struct {
name string
hv base.HeaderValue
err string
}{
{
"empty",
base.HeaderValue{},
"value not provided",
},
{
"2 values",
base.HeaderValue{"a", "b"},
"value provided multiple times ([a b])",
},
{
"invalid key-value",
base.HeaderValue{"test=\"a"},
"apexes not closed (test=\"a)",
},
{
"invalid sequence",
base.HeaderValue{`url=rtsp://127.0.0.1/test.mkv/track1;seq=aa;rtptime=717574556`},
"strconv.ParseUint: parsing \"aa\": invalid syntax",
},
{
"invalid rtptime",
base.HeaderValue{`url=rtsp://127.0.0.1/test.mkv/track1;seq=35243;rtptime=aa`},
"strconv.ParseUint: parsing \"aa\": invalid syntax",
},
{
"missing URL",
base.HeaderValue{`seq=35243;rtptime=717574556`},
"URL is missing",
},
} {
t.Run(ca.name, func(t *testing.T) {
var h RTPInfo
err := h.Unmarshal(ca.hv)
require.EqualError(t, err, ca.err)
})
}
}

func TestRTPInfoMarshal(t *testing.T) {
for _, ca := range casesRTPInfo {
t.Run(ca.name, func(t *testing.T) {
Expand All @@ -195,3 +150,28 @@ func TestRTPInfoMarshal(t *testing.T) {
})
}
}

func FuzzRTPInfoUnmarshal(f *testing.F) {
for _, ca := range casesRTPInfo {
f.Add(ca.vin[0])
}

f.Fuzz(func(t *testing.T, b string) {
var h RTPInfo
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
})
}

func TestRTPInfoAdditionalErrors(t *testing.T) {
func() {
var h RTPInfo
err := h.Unmarshal(base.HeaderValue{})
require.Error(t, err)
}()

func() {
var h RTPInfo
err := h.Unmarshal(base.HeaderValue{"a", "b"})
require.Error(t, err)
}()
}
62 changes: 27 additions & 35 deletions pkg/headers/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,41 +53,6 @@ func TestSessionUnmarshal(t *testing.T) {
}
}

func TestSessionUnmarshalErrors(t *testing.T) {
for _, ca := range []struct {
name string
hv base.HeaderValue
err string
}{
{
"empty",
base.HeaderValue{},
"value not provided",
},
{
"2 values",
base.HeaderValue{"a", "b"},
"value provided multiple times ([a b])",
},
{
"invalid key-value",
base.HeaderValue{"A3eqwsafq3rFASqew;test=\"a"},
"apexes not closed (test=\"a)",
},
{
"invalid timeout",
base.HeaderValue{`A3eqwsafq3rFASqew;timeout=aaa`},
"strconv.ParseUint: parsing \"aaa\": invalid syntax",
},
} {
t.Run(ca.name, func(t *testing.T) {
var h Session
err := h.Unmarshal(ca.hv)
require.EqualError(t, err, ca.err)
})
}
}

func TestSessionMarshal(t *testing.T) {
for _, ca := range casesSession {
t.Run(ca.name, func(t *testing.T) {
Expand All @@ -96,3 +61,30 @@ func TestSessionMarshal(t *testing.T) {
})
}
}

func FuzzSessionUnmarshal(f *testing.F) {
for _, ca := range casesSession {
f.Add(ca.vin[0])
}

f.Add("timeout=")

f.Fuzz(func(t *testing.T, b string) {
var h Session
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
})
}

func TestSessionAdditionalErrors(t *testing.T) {
func() {
var h Session
err := h.Unmarshal(base.HeaderValue{})
require.Error(t, err)
}()

func() {
var h Session
err := h.Unmarshal(base.HeaderValue{"a", "b"})
require.Error(t, err)
}()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("=\"")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("seq")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("0")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("rtptime")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/004943ecb339bfe3
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("clock=-")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/058576b517a900ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=0::-")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/094e8699a2b0498e
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=0:0:0-0:0:0:0.")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/0be015f3a8e9d205
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/15422aae47040b82
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("=\"")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/279c27b15659ed37
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("npt=0-A")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/377ba66160e60e9d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=0:0:0:-")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/48015f85af9b7d4c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("npt=-")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/4b087c92430c42c2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("npt=::-")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/4e251d9d33f8ea08
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=0:0:A-")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/5b813a5623dcc9a2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("npt=:::-")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/631898e223f2e221
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("clock=00000101T000000Z-0")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzRangeUnmarshal/76d86663b6246d0d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=0:0:0-0")
Loading

0 comments on commit c93d5c5

Please sign in to comment.