Skip to content

Commit

Permalink
improve fuzz tests (#3596)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Aug 1, 2024
1 parent 59ae3ad commit c9a938a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
5 changes: 4 additions & 1 deletion internal/protocols/rtmp/amf0/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ func FuzzUnmarshal(f *testing.F) {
}

f.Fuzz(func(_ *testing.T, b []byte) {
Unmarshal(b) //nolint:errcheck
what, err := Unmarshal(b)
if err == nil {
Marshal(what) //nolint:errcheck
}
})
}
20 changes: 16 additions & 4 deletions internal/protocols/rtmp/chunk/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,27 +160,39 @@ func TestChunkMarshal(t *testing.T) {
func FuzzChunk0Read(f *testing.F) {
f.Fuzz(func(_ *testing.T, b []byte) {
var chunk Chunk0
chunk.Read(bytes.NewReader(b), 65536, false) //nolint:errcheck
err := chunk.Read(bytes.NewReader(b), 65536, false)
if err == nil {
chunk.Marshal(false) //nolint:errcheck
}
})
}

func FuzzChunk1Read(f *testing.F) {
f.Fuzz(func(_ *testing.T, b []byte) {
var chunk Chunk1
chunk.Read(bytes.NewReader(b), 65536, false) //nolint:errcheck
err := chunk.Read(bytes.NewReader(b), 65536, false)
if err == nil {
chunk.Marshal(false) //nolint:errcheck
}
})
}

func FuzzChunk2Read(f *testing.F) {
f.Fuzz(func(_ *testing.T, b []byte) {
var chunk Chunk2
chunk.Read(bytes.NewReader(b), 65536, false) //nolint:errcheck
err := chunk.Read(bytes.NewReader(b), 65536, false)
if err == nil {
chunk.Marshal(false) //nolint:errcheck
}
})
}

func FuzzChunk3Read(f *testing.F) {
f.Fuzz(func(_ *testing.T, b []byte) {
var chunk Chunk3
chunk.Read(bytes.NewReader(b), 65536, true) //nolint:errcheck
err := chunk.Read(bytes.NewReader(b), 65536, true)
if err == nil {
chunk.Marshal(false) //nolint:errcheck
}
})
}
1 change: 1 addition & 0 deletions internal/protocols/rtmp/handshake/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestHandshake(t *testing.T) {

clientInKey, clientOutKey, err := DoClient(rw, ca == "encrypted", true)
require.NoError(t, err)

<-done

if ca == "encrypted" {
Expand Down
15 changes: 12 additions & 3 deletions internal/protocols/rtmp/message/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,18 @@ func FuzzReader(f *testing.F) {
0x01, 0x00, 0x00, 0x00, 0x88, 0x68, 0x76, 0x63,
0x31, 0x01, 0x02, 0x03,
})

f.Fuzz(func(_ *testing.T, b []byte) {
bc := bytecounter.NewReader(bytes.NewReader(b))
r := NewReader(bc, bc, nil)
r.Read() //nolint:errcheck
bcr := bytecounter.NewReader(bytes.NewReader(b))
r := NewReader(bcr, bcr, nil)

var buf bytes.Buffer
bcw := bytecounter.NewWriter(&buf)
w := NewWriter(bcw, bcw, true)

msg, err := r.Read()
if err == nil {
w.Write(msg) //nolint:errcheck
}
})
}
14 changes: 10 additions & 4 deletions internal/protocols/rtmp/rawmessage/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,20 @@ func TestReaderAcknowledge(t *testing.T) {

func FuzzReader(f *testing.F) {
f.Fuzz(func(_ *testing.T, b []byte) {
br := bytecounter.NewReader(bytes.NewReader(b))
r := NewReader(br, br, func(_ uint32) error {
bcr := bytecounter.NewReader(bytes.NewReader(b))
r := NewReader(bcr, bcr, func(_ uint32) error {
return nil
})

var buf bytes.Buffer
bcw := bytecounter.NewWriter(&buf)
w := NewWriter(bcw, bcw, true)

for {
_, err := r.Read()
if err != nil {
msg, err := r.Read()
if err == nil {
w.Write(msg) //nolint:errcheck
} else {
break
}
}
Expand Down

0 comments on commit c9a938a

Please sign in to comment.