add fuzz tests to all headers (#522)

This commit is contained in:
Alessandro Ros
2024-02-22 12:08:42 +01:00
committed by GitHub
parent 55fa72f0c2
commit c93d5c54d9
32 changed files with 177 additions and 326 deletions

View File

@@ -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) { func TestRangeMarshal(t *testing.T) {
for _, ca := range casesRange { for _, ca := range casesRange {
t.Run(ca.name, func(t *testing.T) { t.Run(ca.name, func(t *testing.T) {
@@ -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)
}()
}

View File

@@ -39,10 +39,13 @@ func (h *RTPInfo) Unmarshal(v base.HeaderValue) error {
return err return err
} }
urlReceived := false
for k, v := range kvs { for k, v := range kvs {
switch k { switch k {
case "url": case "url":
e.URL = v e.URL = v
urlReceived = true
case "seq": case "seq":
vi, err := strconv.ParseUint(v, 10, 16) vi, err := strconv.ParseUint(v, 10, 16)
@@ -65,7 +68,7 @@ func (h *RTPInfo) Unmarshal(v base.HeaderValue) error {
} }
} }
if e.URL == "" { if !urlReceived {
return fmt.Errorf("URL is missing") return fmt.Errorf("URL is missing")
} }

View File

@@ -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) { func TestRTPInfoMarshal(t *testing.T) {
for _, ca := range casesRTPInfo { for _, ca := range casesRTPInfo {
t.Run(ca.name, func(t *testing.T) { t.Run(ca.name, func(t *testing.T) {
@@ -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)
}()
}

View File

@@ -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) { func TestSessionMarshal(t *testing.T) {
for _, ca := range casesSession { for _, ca := range casesSession {
t.Run(ca.name, func(t *testing.T) { t.Run(ca.name, func(t *testing.T) {
@@ -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)
}()
}

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("=\"")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("seq")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("rtptime")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("clock=-")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=0::-")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=0:0:0-0:0:0:0.")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("=\"")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("npt=0-A")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=0:0:0:-")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("npt=-")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("npt=::-")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=0:0:A-")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("npt=:::-")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("clock=00000101T000000Z-0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=0:0:0-0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("npt=:-")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=::-")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=0:0:0-0:0:0:.0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("smpte=-")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("time")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string(";=\"")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string(";timeout")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("=\"")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
string("source=0:0:0::0%0")

View File

@@ -181,91 +181,6 @@ func TestTransportUnmarshal(t *testing.T) {
} }
} }
func TestTransportUnmarshalErrors(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)",
},
{
"protocol not found",
base.HeaderValue{`invalid;unicast;client_port=14186-14187`},
"protocol not found (invalid;unicast;client_port=14186-14187)",
},
{
"invalid interleaved port",
base.HeaderValue{`RTP/AVP;unicast;interleaved=aa-14187`},
"invalid ports (aa-14187)",
},
{
"invalid ttl",
base.HeaderValue{`RTP/AVP;unicast;ttl=aa`},
"strconv.ParseUint: parsing \"aa\": invalid syntax",
},
{
"invalid destination",
base.HeaderValue{`RTP/AVP;unicast;destination=aa`},
"invalid destination (aa)",
},
{
"invalid ports 1",
base.HeaderValue{`RTP/AVP;unicast;port=aa`},
"invalid ports (aa)",
},
{
"invalid ports 2",
base.HeaderValue{`RTP/AVP;unicast;port=aa-bb-cc`},
"invalid ports (aa-bb-cc)",
},
{
"invalid ports 3",
base.HeaderValue{`RTP/AVP;unicast;port=aa-14187`},
"invalid ports (aa-14187)",
},
{
"invalid ports 4",
base.HeaderValue{`RTP/AVP;unicast;port=14186-aa`},
"invalid ports (14186-aa)",
},
{
"invalid client port",
base.HeaderValue{`RTP/AVP;unicast;client_port=aa-14187`},
"invalid ports (aa-14187)",
},
{
"invalid server port",
base.HeaderValue{`RTP/AVP;unicast;server_port=aa-14187`},
"invalid ports (aa-14187)",
},
{
"invalid mode",
base.HeaderValue{`RTP/AVP;unicast;mode=aa`},
"invalid transport mode: 'aa'",
},
} {
t.Run(ca.name, func(t *testing.T) {
var h Transport
err := h.Unmarshal(ca.hv)
require.EqualError(t, err, ca.err)
})
}
}
func TestTransportMarshal(t *testing.T) { func TestTransportMarshal(t *testing.T) {
for _, ca := range casesTransport { for _, ca := range casesTransport {
t.Run(ca.name, func(t *testing.T) { t.Run(ca.name, func(t *testing.T) {
@@ -312,36 +227,6 @@ func TestTransportsUnmarshal(t *testing.T) {
} }
} }
func TestTransportsUnmarshalErrors(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",
base.HeaderValue{"aasd"},
"protocol not found (aasd)",
},
} {
t.Run(ca.name, func(t *testing.T) {
var h Transports
err := h.Unmarshal(ca.hv)
require.EqualError(t, err, ca.err)
})
}
}
func TestTransportsMarshal(t *testing.T) { func TestTransportsMarshal(t *testing.T) {
for _, ca := range casesTransports { for _, ca := range casesTransports {
t.Run(ca.name, func(t *testing.T) { t.Run(ca.name, func(t *testing.T) {
@@ -350,3 +235,41 @@ func TestTransportsMarshal(t *testing.T) {
}) })
} }
} }
func FuzzTransportsUnmarshal(f *testing.F) {
for _, ca := range casesTransports {
f.Add(ca.vin[0])
}
for _, ca := range casesTransport {
f.Add(ca.vin[0])
}
f.Add("source=aa-14187")
f.Add("destination=aa")
f.Add("interleaved=")
f.Add("ttl=")
f.Add("port=")
f.Add("client_port=")
f.Add("server_port=")
f.Add("mode=")
f.Fuzz(func(t *testing.T, b string) {
var h Transports
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
})
}
func TestTransportAdditionalErrors(t *testing.T) {
func() {
var h Transport
err := h.Unmarshal(base.HeaderValue{})
require.Error(t, err)
}()
func() {
var h Transport
err := h.Unmarshal(base.HeaderValue{"a", "b"})
require.Error(t, err)
}()
}