improve fuzz tests (#571)

This commit is contained in:
Alessandro Ros
2024-05-17 19:19:15 +02:00
committed by GitHub
parent f283abc2e7
commit ef60c8c755
20 changed files with 250 additions and 234 deletions

View File

@@ -7,80 +7,82 @@ import (
"github.com/stretchr/testify/require"
)
var casesSender = []struct {
name string
wwwAuthenticate base.HeaderValue
authorization base.HeaderValue
}{
{
"basic",
base.HeaderValue{
"Basic realm=testrealm",
},
base.HeaderValue{
"Basic bXl1c2VyOm15cGFzcw==",
},
},
{
"digest md5 implicit",
base.HeaderValue{
`Digest realm="myrealm", nonce="f49ac6dd0ba708d4becddc9692d1f2ce"`,
},
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", response=\"ba6e9cccbfeb38db775378a0a9067ba5\"",
},
},
{
"digest md5 explicit",
base.HeaderValue{
`Digest realm="myrealm", nonce="f49ac6dd0ba708d4becddc9692d1f2ce", algorithm="MD5"`,
},
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", response=\"ba6e9cccbfeb38db775378a0a9067ba5\", " +
"algorithm=\"MD5\"",
},
},
{
"digest sha256",
base.HeaderValue{
`Digest realm="myrealm", nonce="f49ac6dd0ba708d4becddc9692d1f2ce", algorithm="SHA-256"`,
},
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", " +
"response=\"e298296ce35c9ab79699c8f3f9508944c1be9395e892f8205b6d66f1b8e663ee\", " +
"algorithm=\"SHA-256\"",
},
},
{
"multiple 1",
base.HeaderValue{
"Basic realm=testrealm",
`Digest realm="myrealm", nonce="f49ac6dd0ba708d4becddc9692d1f2ce"`,
},
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", response=\"ba6e9cccbfeb38db775378a0a9067ba5\"",
},
},
{
"multiple 2",
base.HeaderValue{
"Basic realm=testrealm",
`Digest realm="myrealm", nonce="f49ac6dd0ba708d4becddc9692d1f2ce", algorithm="MD5"`,
`Digest realm="myrealm", nonce="f49ac6dd0ba708d4becddc9692d1f2ce", algorithm="SHA-256"`,
},
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", " +
"response=\"e298296ce35c9ab79699c8f3f9508944c1be9395e892f8205b6d66f1b8e663ee\", " +
"algorithm=\"SHA-256\"",
},
},
}
func TestSender(t *testing.T) {
for _, ca := range []struct {
name string
wwwAuthenticate base.HeaderValue
authorization base.HeaderValue
}{
{
"basic",
base.HeaderValue{
"Basic realm=testrealm",
},
base.HeaderValue{
"Basic bXl1c2VyOm15cGFzcw==",
},
},
{
"digest md5 implicit",
base.HeaderValue{
`Digest realm="myrealm", nonce="f49ac6dd0ba708d4becddc9692d1f2ce"`,
},
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", response=\"ba6e9cccbfeb38db775378a0a9067ba5\"",
},
},
{
"digest md5 explicit",
base.HeaderValue{
`Digest realm="myrealm", nonce="f49ac6dd0ba708d4becddc9692d1f2ce", algorithm="MD5"`,
},
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", response=\"ba6e9cccbfeb38db775378a0a9067ba5\", " +
"algorithm=\"MD5\"",
},
},
{
"digest sha256",
base.HeaderValue{
`Digest realm="myrealm", nonce="f49ac6dd0ba708d4becddc9692d1f2ce", algorithm="SHA-256"`,
},
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", " +
"response=\"e298296ce35c9ab79699c8f3f9508944c1be9395e892f8205b6d66f1b8e663ee\", " +
"algorithm=\"SHA-256\"",
},
},
{
"multiple 1",
base.HeaderValue{
"Basic realm=testrealm",
`Digest realm="myrealm", nonce="f49ac6dd0ba708d4becddc9692d1f2ce"`,
},
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", response=\"ba6e9cccbfeb38db775378a0a9067ba5\"",
},
},
{
"multiple 2",
base.HeaderValue{
"Basic realm=testrealm",
`Digest realm="myrealm", nonce="f49ac6dd0ba708d4becddc9692d1f2ce", algorithm="MD5"`,
`Digest realm="myrealm", nonce="f49ac6dd0ba708d4becddc9692d1f2ce", algorithm="SHA-256"`,
},
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", " +
"response=\"e298296ce35c9ab79699c8f3f9508944c1be9395e892f8205b6d66f1b8e663ee\", " +
"algorithm=\"SHA-256\"",
},
},
} {
for _, ca := range casesSender {
t.Run(ca.name, func(t *testing.T) {
se, err := NewSender(ca.wwwAuthenticate, "myuser", "mypass")
require.NoError(t, err)
@@ -97,12 +99,9 @@ func TestSender(t *testing.T) {
}
func FuzzSender(f *testing.F) {
f.Add(`Invalid`)
f.Add(`Digest`)
f.Add(`Digest nonce=123`)
f.Add(`Digest realm=123`)
f.Add(`Basic`)
f.Add(`Basic nonce=123`)
for _, ca := range casesSender {
f.Add(ca.authorization[0])
}
f.Fuzz(func(_ *testing.T, a string) {
NewSender(base.HeaderValue{a}, "myuser", "mypass") //nolint:errcheck

View File

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

View File

@@ -1,56 +1,57 @@
package auth
import (
"fmt"
"testing"
"github.com/bluenviron/gortsplib/v4/pkg/base"
"github.com/stretchr/testify/require"
)
var casesValidate = []struct {
name string
authorization base.HeaderValue
}{
{
"basic",
base.HeaderValue{
"Basic bXl1c2VyOm15cGFzcw==",
},
},
{
"digest md5 implicit",
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", response=\"ba6e9cccbfeb38db775378a0a9067ba5\"",
},
},
{
"digest md5 explicit",
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", response=\"ba6e9cccbfeb38db775378a0a9067ba5\", " +
"algorithm=\"MD5\"",
},
},
{
"digest sha256",
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", " +
"response=\"e298296ce35c9ab79699c8f3f9508944c1be9395e892f8205b6d66f1b8e663ee\", " +
"algorithm=\"SHA-256\"",
},
},
{
"digest vlc",
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/\", response=\"5ca5ceeca20a05e9a3f49ecde4b42655\"",
},
},
}
func TestValidate(t *testing.T) {
for _, ca := range []struct {
name string
authorization base.HeaderValue
}{
{
"basic",
base.HeaderValue{
"Basic bXl1c2VyOm15cGFzcw==",
},
},
{
"digest md5 implicit",
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", response=\"ba6e9cccbfeb38db775378a0a9067ba5\"",
},
},
{
"digest md5 explicit",
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", response=\"ba6e9cccbfeb38db775378a0a9067ba5\", " +
"algorithm=\"MD5\"",
},
},
{
"digest sha256",
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/trackID=3\", " +
"response=\"e298296ce35c9ab79699c8f3f9508944c1be9395e892f8205b6d66f1b8e663ee\", " +
"algorithm=\"SHA-256\"",
},
},
{
"digest vlc",
base.HeaderValue{
"Digest username=\"myuser\", realm=\"myrealm\", nonce=\"f49ac6dd0ba708d4becddc9692d1f2ce\", " +
"uri=\"rtsp://myhost/mypath?key=val/\", response=\"5ca5ceeca20a05e9a3f49ecde4b42655\"",
},
},
} {
for _, ca := range casesValidate {
t.Run(ca.name, func(t *testing.T) {
se, err := NewSender(
GenerateWWWAuthenticate([]ValidateMethod{ValidateMethodDigestMD5}, "myrealm", "f49ac6dd0ba708d4becddc9692d1f2ce"),
@@ -62,7 +63,6 @@ func TestValidate(t *testing.T) {
URL: mustParseURL("rtsp://myhost/mypath?key=val/"),
}
se.AddAuthorization(req1)
fmt.Println(req1.Header)
req := &base.Request{
Method: base.Setup,
@@ -84,14 +84,9 @@ func TestValidate(t *testing.T) {
}
func FuzzValidate(f *testing.F) {
f.Add(`Invalid`)
f.Add(`Digest `)
f.Add(`Digest realm=123`)
f.Add(`Digest realm=123,nonce=123`)
f.Add(`Digest realm=123,nonce=123,username=123`)
f.Add(`Digest realm=123,nonce=123,username=123,uri=123`)
f.Add(`Digest realm=123,nonce=123,username=123,uri=123,response=123`)
f.Add(`Digest realm=123,nonce=abcde,username=123,uri=123,response=123`)
for _, ca := range casesValidate {
f.Add(ca.authorization[0])
}
f.Fuzz(func(_ *testing.T, a string) {
Validate( //nolint:errcheck
@@ -106,7 +101,7 @@ func FuzzValidate(f *testing.F) {
"mypass",
nil,
"IPCAM",
"abcde",
"f49ac6dd0ba708d4becddc9692d1f2ce",
)
})
}

View File

@@ -43,6 +43,10 @@ func TestBodyMarshal(t *testing.T) {
}
func FuzzBodyUnmarshal(f *testing.F) {
for _, ca := range casesBody {
f.Add(ca.h["Content-Length"][0], ca.byts)
}
f.Fuzz(func(_ *testing.T, a string, b []byte) {
var p body
p.unmarshal( //nolint:errcheck

View File

@@ -126,6 +126,10 @@ func TestHeaderWrite(t *testing.T) {
}
func FuzzHeaderUnmarshal(f *testing.F) {
for _, ca := range cases {
f.Add(ca.enc)
}
str := ""
for i := 0; i < 300; i++ {
str += "Key: val\r\n"

View File

@@ -55,6 +55,9 @@ func TestInterleavedFrameMarshal(t *testing.T) {
}
func FuzzInterleavedFrameUnmarshal(f *testing.F) {
for _, ca := range casesInterleavedFrame {
f.Add(ca.enc)
}
f.Fuzz(func(_ *testing.T, b []byte) {
var f InterleavedFrame
f.Unmarshal(bufio.NewReader(bytes.NewBuffer(b))) //nolint:errcheck

View File

@@ -182,12 +182,9 @@ func TestRequestString(t *testing.T) {
}
func FuzzRequestUnmarshal(f *testing.F) {
f.Add([]byte("GET rtsp://testing123/test"))
f.Add([]byte("GET rtsp://testing123/test RTSP/1.0\r\n"))
f.Add([]byte("OPTIONS rtsp://example.com/media.mp4 RTSP/1.0\r\n" +
"Content-Length: 100\r\n" +
"\r\n" +
"testing"))
for _, ca := range casesRequest {
f.Add(ca.byts)
}
f.Fuzz(func(_ *testing.T, b []byte) {
var req Request

View File

@@ -156,12 +156,9 @@ func TestResponseString(t *testing.T) {
}
func FuzzResponseUnmarshal(f *testing.F) {
f.Add([]byte("RTSP/1.0 "))
f.Add([]byte("RTSP/1.0 200 OK\r\n" +
"Content-Length: 100\r\n" +
"\r\n" +
"testing"))
for _, ca := range casesResponse {
f.Add(ca.byts)
}
f.Fuzz(func(_ *testing.T, b []byte) {
var res Response

View File

@@ -0,0 +1,3 @@
go test fuzz v1
string("10")
[]byte("0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0 rtsp:# RTSP/1.0\r\n")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0 rtsp:# RTSP/1.0\r\nContent-Length:\r\n\r\n")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("RTSP/1.0 0 0\r\nContent-Length:\r\n\r\n")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("RTSP/1.0 ")

View File

@@ -836,6 +836,10 @@ func TestSessionFindFormat(t *testing.T) {
}
func FuzzSessionUnmarshalErrors(f *testing.F) {
for _, ca := range casesSession {
f.Add(ca.in)
}
f.Add("v=0\r\n" +
"o=jdoe 2890844526 2890842807 IN IP4 10.47.16.5\r\n" +
"s=SDP Seminar\r\n" +

View File

@@ -0,0 +1,5 @@
go test fuzz v1
string("0")
string("A")
string("\xb3\xbd\xcf\xf4\x10\xa6")
string("")

View File

@@ -6,77 +6,79 @@ import (
"github.com/stretchr/testify/require"
)
var casesKeyVal = []struct {
name string
s string
kvs map[string]string
}{
{
"base",
`key1=v1,key2=v2`,
map[string]string{
"key1": "v1",
"key2": "v2",
},
},
{
"with space",
`key1=v1, key2=v2`,
map[string]string{
"key1": "v1",
"key2": "v2",
},
},
{
"with apexes",
`key1="v1", key2=v2`,
map[string]string{
"key1": "v1",
"key2": "v2",
},
},
{
"with apexes and comma",
`key1="v,1", key2="v2"`,
map[string]string{
"key1": "v,1",
"key2": "v2",
},
},
{
"with apexes and equal",
`key1="v=1", key2="v2"`,
map[string]string{
"key1": "v=1",
"key2": "v2",
},
},
{
"no val key1",
`key1, key2="v2"`,
map[string]string{
"key1": "",
"key2": "v2",
},
},
{
"no val key2",
`key1="v=1", key2`,
map[string]string{
"key1": "v=1",
"key2": "",
},
},
{
"no val key1 nor key2",
`key1, key2`,
map[string]string{
"key1": "",
"key2": "",
},
},
}
func TestKeyValParse(t *testing.T) {
for _, ca := range []struct {
name string
s string
kvs map[string]string
}{
{
"base",
`key1=v1,key2=v2`,
map[string]string{
"key1": "v1",
"key2": "v2",
},
},
{
"with space",
`key1=v1, key2=v2`,
map[string]string{
"key1": "v1",
"key2": "v2",
},
},
{
"with apexes",
`key1="v1", key2=v2`,
map[string]string{
"key1": "v1",
"key2": "v2",
},
},
{
"with apexes and comma",
`key1="v,1", key2="v2"`,
map[string]string{
"key1": "v,1",
"key2": "v2",
},
},
{
"with apexes and equal",
`key1="v=1", key2="v2"`,
map[string]string{
"key1": "v=1",
"key2": "v2",
},
},
{
"no val key1",
`key1, key2="v2"`,
map[string]string{
"key1": "",
"key2": "v2",
},
},
{
"no val key2",
`key1="v=1", key2`,
map[string]string{
"key1": "v=1",
"key2": "",
},
},
{
"no val key1 nor key2",
`key1, key2`,
map[string]string{
"key1": "",
"key2": "",
},
},
} {
for _, ca := range casesKeyVal {
t.Run(ca.name, func(t *testing.T) {
kvs, err := keyValParse(ca.s, ',')
require.NoError(t, err)
@@ -86,6 +88,10 @@ func TestKeyValParse(t *testing.T) {
}
func FuzzKeyValParse(f *testing.F) {
for _, ca := range casesKeyVal {
f.Add(ca.s)
}
f.Fuzz(func(_ *testing.T, b string) {
keyValParse(b, ',') //nolint:errcheck
})

View File

@@ -3065,26 +3065,9 @@ func TestMarshal(t *testing.T) {
}
func FuzzUnmarshal(f *testing.F) {
f.Add("v=0\r\n" +
"t=2873397496 2873404696\r\n" +
"t=3034423619 3042462419\r\n" +
"r=aa bb 0 90000\r\n")
f.Add("v=0\r\n" +
"t=2873397496 2873404696\r\n" +
"t=3034423619 3042462419\r\n" +
"r=123 bb 0 90000\r\n")
f.Add("v=0\r\n" +
"m=audio 49170 RTP/AVP 80000\r\n" +
"i=Vivamus a posuere nisl\r\n" +
"c=IN IP4 203.0.113.1\r\n" +
"b=X-YZ:128\r\n" +
"k=prompt\r\n" +
"a=sendrecv\r\n")
f.Add("v=0\r\n" +
"o = IN \r\n")
for _, c := range cases {
f.Add(string(c.enc))
}
f.Fuzz(func(_ *testing.T, b string) {
desc := SessionDescription{}

View File

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

View File

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

View File

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