Skip to content

Commit

Permalink
improve fuzz tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Aug 1, 2024
1 parent e2d1e6d commit 4b37152
Show file tree
Hide file tree
Showing 30 changed files with 144 additions and 41 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/bluenviron/gortsplib/v4
go 1.20

require (
github.com/bluenviron/mediacommon v1.12.1
github.com/bluenviron/mediacommon v1.12.2-0.20240801134301-b013c7a52029
github.com/google/uuid v1.6.0
github.com/pion/rtcp v1.2.14
github.com/pion/rtp v1.8.7-0.20240429002300-bc5124c9d0d0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/asticode/go-astikit v0.30.0 h1:DkBkRQRIxYcknlaU7W7ksNfn4gMFsB0tqMJflx
github.com/asticode/go-astikit v0.30.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
github.com/asticode/go-astits v1.13.0 h1:XOgkaadfZODnyZRR5Y0/DWkA9vrkLLPLeeOvDwfKZ1c=
github.com/asticode/go-astits v1.13.0/go.mod h1:QSHmknZ51pf6KJdHKZHJTLlMegIrhega3LPWz3ND/iI=
github.com/bluenviron/mediacommon v1.12.1 h1:sgDJaKV6OXrPCSO0KPp9zi/pwNWtKHenn5/dvjtY+Tg=
github.com/bluenviron/mediacommon v1.12.1/go.mod h1:HDyW2CzjvhYJXtdxstdFPio3G0qSocPhqkhUt/qffec=
github.com/bluenviron/mediacommon v1.12.2-0.20240801134301-b013c7a52029 h1:s9PNLf98P0uRiBqvY6qKrO1pssPZVCVhs17aSNfXuLY=
github.com/bluenviron/mediacommon v1.12.2-0.20240801134301-b013c7a52029/go.mod h1:HDyW2CzjvhYJXtdxstdFPio3G0qSocPhqkhUt/qffec=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
8 changes: 7 additions & 1 deletion pkg/auth/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ func FuzzSender(f *testing.F) {
}

f.Fuzz(func(_ *testing.T, a string) {
NewSender(base.HeaderValue{a}, "myuser", "mypass") //nolint:errcheck
se, err := NewSender(base.HeaderValue{a}, "myuser", "mypass")
if err == nil {
se.AddAuthorization(&base.Request{
Method: base.Setup,
URL: mustParseURL("rtsp://myhost/mypath?key=val/trackID=3"),
})
}
})
}
5 changes: 4 additions & 1 deletion pkg/base/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ func FuzzBodyUnmarshal(f *testing.F) {

f.Fuzz(func(_ *testing.T, a string, b []byte) {
var p body
p.unmarshal( //nolint:errcheck
err := p.unmarshal(
Header{
"Content-Length": HeaderValue{a},
},
bufio.NewReader(bytes.NewReader(b)))
if err == nil {
p.marshal()
}
})
}
5 changes: 4 additions & 1 deletion pkg/base/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func FuzzHeaderUnmarshal(f *testing.F) {

f.Fuzz(func(_ *testing.T, b []byte) {
var h Header
h.unmarshal(bufio.NewReader(bytes.NewBuffer(b))) //nolint:errcheck
err := h.unmarshal(bufio.NewReader(bytes.NewBuffer(b)))
if err == nil {
h.marshal()
}
})
}
5 changes: 4 additions & 1 deletion pkg/base/interleaved_frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func FuzzInterleavedFrameUnmarshal(f *testing.F) {
}
f.Fuzz(func(_ *testing.T, b []byte) {
var f InterleavedFrame
f.Unmarshal(bufio.NewReader(bytes.NewBuffer(b))) //nolint:errcheck
err := f.Unmarshal(bufio.NewReader(bytes.NewBuffer(b)))
if err == nil {
f.Marshal() //nolint:errcheck
}
})
}
5 changes: 4 additions & 1 deletion pkg/base/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ func FuzzRequestUnmarshal(f *testing.F) {

f.Fuzz(func(_ *testing.T, b []byte) {
var req Request
req.Unmarshal(bufio.NewReader(bytes.NewBuffer(b))) //nolint:errcheck
err := req.Unmarshal(bufio.NewReader(bytes.NewBuffer(b)))
if err == nil {
req.Marshal() //nolint:errcheck
}
})
}
5 changes: 4 additions & 1 deletion pkg/base/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ func FuzzResponseUnmarshal(f *testing.F) {

f.Fuzz(func(_ *testing.T, b []byte) {
var res Response
res.Unmarshal(bufio.NewReader(bytes.NewBuffer(b))) //nolint:errcheck
err := res.Unmarshal(bufio.NewReader(bytes.NewBuffer(b)))
if err == nil {
res.Marshal() //nolint:errcheck
}
})
}
10 changes: 4 additions & 6 deletions pkg/description/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ func TestSessionFindFormat(t *testing.T) {
require.Equal(t, tr, forma)
}

func FuzzSessionUnmarshalErrors(f *testing.F) {
func FuzzSessionUnmarshal(f *testing.F) {
for _, ca := range casesSession {
f.Add(ca.in)
}
Expand Down Expand Up @@ -898,11 +898,9 @@ func FuzzSessionUnmarshalErrors(f *testing.F) {
f.Fuzz(func(_ *testing.T, enc string) {
var sd sdp.SessionDescription
err := sd.Unmarshal([]byte(enc))
if err != nil {
return
if err == nil {
var desc Session
desc.Unmarshal(&sd) //nolint:errcheck
}

var desc Session
desc.Unmarshal(&sd) //nolint:errcheck
})
}
6 changes: 5 additions & 1 deletion pkg/format/av1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func FuzzUnmarshalAV1(f *testing.F) {
ma["tier"] = f
}

Unmarshal("video", 96, "AV1/90000", ma) //nolint:errcheck
fo, err := Unmarshal("video", 96, "AV1/90000", ma)
if err == nil {
fo.(*AV1).RTPMap()
fo.(*AV1).FMTP()
}
})
}
6 changes: 5 additions & 1 deletion pkg/format/h264_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func FuzzUnmarshalH264(f *testing.F) {
ma["packetization-mode"] = d
}

Unmarshal("video", 96, "H264/90000", ma) //nolint:errcheck
fo, err := Unmarshal("video", 96, "H264/90000", ma)
if err == nil {
fo.RTPMap()
fo.FMTP()
}
})
}
6 changes: 5 additions & 1 deletion pkg/format/h265_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func FuzzUnmarshalH265(f *testing.F) {
ma["sprop-max-don-diff"] = d
}

Unmarshal("video", 96, "H265/90000", ma) //nolint:errcheck
fo, err := Unmarshal("video", 96, "H265/90000", ma)
if err == nil {
fo.RTPMap()
fo.FMTP()
}
})
}
6 changes: 5 additions & 1 deletion pkg/format/lpcm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func TestLPCMDecEncoder(t *testing.T) {

func FuzzUnmarshalLPCM(f *testing.F) {
f.Fuzz(func(_ *testing.T, a string) {
Unmarshal("audio", 96, "L16/"+a, nil) //nolint:errcheck
fo, err := Unmarshal("audio", 96, "L16/"+a, nil)
if err == nil {
fo.RTPMap()
fo.FMTP()
}
})
}
22 changes: 15 additions & 7 deletions pkg/format/mpeg4_audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ func (f *MPEG4Audio) ClockRate() int {
if !f.LATM {
return f.Config.SampleRate
}
if f.CPresent {
return 16000
}
return f.StreamMuxConfig.Programs[0].Layers[0].AudioSpecificConfig.SampleRate
}

Expand All @@ -199,6 +202,10 @@ func (f *MPEG4Audio) RTPMap() string {
"/" + strconv.FormatInt(int64(channelCount), 10)
}

if f.CPresent {
return "MP4A-LATM/16000/1"
}

aoc := f.StreamMuxConfig.Programs[0].Layers[0].AudioSpecificConfig

sampleRate := aoc.SampleRate
Expand Down Expand Up @@ -251,15 +258,8 @@ func (f *MPEG4Audio) FMTP() map[string]string {
return fmtp
}

enc, err := f.StreamMuxConfig.Marshal()
if err != nil {
return nil
}

fmtp := map[string]string{
"profile-level-id": strconv.FormatInt(int64(f.ProfileLevelID), 10),
"config": hex.EncodeToString(enc),
"object": strconv.FormatInt(int64(f.StreamMuxConfig.Programs[0].Layers[0].AudioSpecificConfig.Type), 10),
}

if f.Bitrate != nil {
Expand All @@ -270,6 +270,14 @@ func (f *MPEG4Audio) FMTP() map[string]string {
fmtp["cpresent"] = "1"
} else {
fmtp["cpresent"] = "0"

enc, err := f.StreamMuxConfig.Marshal()
if err != nil {
return nil
}

fmtp["config"] = hex.EncodeToString(enc)
fmtp["object"] = strconv.FormatInt(int64(f.StreamMuxConfig.Programs[0].Layers[0].AudioSpecificConfig.Type), 10)
}

if f.SBREnabled != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/format/mpeg4_audio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,10 @@ func FuzzUnmarshalMPEG4AudioLATM(f *testing.F) {
ma["sbr-enabled"] = l
}

fo, err := Unmarshal("audio", 96, "MP4A-LATM/48000/2", ma) //nolint:errcheck
fo, err := Unmarshal("audio", 96, "MP4A-LATM/48000/2", ma)
if err == nil {
fo.(*MPEG4Audio).RTPMap()
fo.(*MPEG4Audio).FMTP()
fo.(*MPEG4Audio).GetConfig()
}
})
Expand Down
6 changes: 5 additions & 1 deletion pkg/format/mpeg4_video_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func FuzzUnmarshalMPEG4Video(f *testing.F) {
ma["config"] = d
}

Unmarshal("audio", 96, "MP4V-ES/90000", ma) //nolint:errcheck
fo, err := Unmarshal("audio", 96, "MP4V-ES/90000", ma)
if err == nil {
fo.RTPMap()
fo.FMTP()
}
})
}
12 changes: 10 additions & 2 deletions pkg/format/opus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ func FuzzUnmarshalOpus(f *testing.F) {
f.Add("48000/a")

f.Fuzz(func(_ *testing.T, a string) {
Unmarshal("audio", 96, "Opus/"+a, nil) //nolint:errcheck
fo, err := Unmarshal("audio", 96, "Opus/"+a, nil)
if err == nil {
fo.RTPMap()
fo.FMTP()
}
})
}

func FuzzUnmarshalOpusMulti(f *testing.F) {
f.Add("48000/a")

f.Fuzz(func(_ *testing.T, a string) {
Unmarshal("audio", 96, "multiopus/"+a, nil) //nolint:errcheck
fo, err := Unmarshal("audio", 96, "multiopus/"+a, nil)
if err == nil {
fo.RTPMap()
fo.FMTP()
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
go test fuzz v1
bool(false)
string("0")
bool(true)
string("0")
bool(true)
string("0")
bool(true)
string("70102010")
bool(true)
string("0")
bool(true)
string("0")
6 changes: 5 additions & 1 deletion pkg/format/vorbis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ func TestVorbisAttributes(t *testing.T) {

func FuzzUnmarshalVorbis(f *testing.F) {
f.Fuzz(func(_ *testing.T, a, b string) {
Unmarshal("audio", 96, "Vorbis/"+a, map[string]string{ //nolint:errcheck
fo, err := Unmarshal("audio", 96, "Vorbis/"+a, map[string]string{
"configuration": b,
})
if err == nil {
fo.RTPMap()
fo.FMTP()
}
})
}
6 changes: 5 additions & 1 deletion pkg/format/vp8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func FuzzUnmarshalVP8(f *testing.F) {
ma["max-fs"] = d
}

Unmarshal("audio", 96, "VP8/90000", ma) //nolint:errcheck
fo, err := Unmarshal("audio", 96, "VP8/90000", ma)
if err == nil {
fo.RTPMap()
fo.FMTP()
}
})
}
6 changes: 5 additions & 1 deletion pkg/format/vp9_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func FuzzUnmarshalVP9(f *testing.F) {
ma["profile-id"] = f
}

Unmarshal("audio", 96, "VP9/90000", ma) //nolint:errcheck
fo, err := Unmarshal("audio", 96, "VP9/90000", ma)
if err == nil {
fo.RTPMap()
fo.FMTP()
}
})
}
5 changes: 4 additions & 1 deletion pkg/headers/authenticate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ func FuzzAuthenticateUnmarshal(f *testing.F) {

f.Fuzz(func(_ *testing.T, b string) {
var h Authenticate
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
err := h.Unmarshal(base.HeaderValue{b})
if err == nil {
h.Marshal()
}
})
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/headers/authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ func FuzzAuthorizationUnmarshal(f *testing.F) {

f.Fuzz(func(_ *testing.T, b string) {
var h Authorization
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
err := h.Unmarshal(base.HeaderValue{b})
if err == nil {
h.Marshal()
}
})
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/headers/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ func FuzzRangeUnmarshal(f *testing.F) {

f.Fuzz(func(_ *testing.T, b string) {
var h Range
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
err := h.Unmarshal(base.HeaderValue{b})
if err == nil {
h.Marshal()
}
})
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/headers/rtpinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ func FuzzRTPInfoUnmarshal(f *testing.F) {

f.Fuzz(func(_ *testing.T, b string) {
var h RTPInfo
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
err := h.Unmarshal(base.HeaderValue{b})
if err == nil {
h.Marshal()
}
})
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/headers/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ func FuzzSessionUnmarshal(f *testing.F) {

f.Fuzz(func(_ *testing.T, b string) {
var h Session
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
err := h.Unmarshal(base.HeaderValue{b})
if err == nil {
h.Marshal()
}
})
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/headers/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ func FuzzTransportsUnmarshal(f *testing.F) {

f.Fuzz(func(_ *testing.T, b string) {
var h Transports
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
err := h.Unmarshal(base.HeaderValue{b})
if err == nil {
h.Marshal()
}
})
}

Expand Down
Loading

0 comments on commit 4b37152

Please sign in to comment.