Skip to content

Commit

Permalink
Add testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghao authored and stv0g committed Aug 9, 2023
1 parent 11575ef commit dff6dcc
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
14 changes: 14 additions & 0 deletions full_intra_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ func TestFullIntraRequestUnmarshal(t *testing.T) {
},
WantError: errWrongType,
},
{
Name: "wrong length",
Data: []byte{
// v=2, p=0, FMT=4, PSFB, len=3
0x84, 0xce, 0x00, 0x03,
// ssrc=0x0
0x00, 0x00, 0x00, 0x00,
// ssrc=0x4bc4fcb4
0x4b, 0xc4, 0xfc, 0xb4,
// ssrc=0x12345678
0x12, 0x34, 0x56, 0x78,
},
WantError: errBadLength,
},
} {
var fir FullIntraRequest
err := fir.Unmarshal(test.Data)
Expand Down
5 changes: 4 additions & 1 deletion fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ func FuzzUnmarshal(f *testing.F) {
}

for _, packet := range packets {
packet.Marshal()
_, err = packet.Marshal()
if err != nil {
return
}
}
})
}
13 changes: 13 additions & 0 deletions rfc8888_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,19 @@ func TestCCFeedbackReportBlockUnmarshalMarshal(t *testing.T) {
assert.Error(t, err)
assert.ErrorIs(t, err, errIncorrectNumReports)
})

t.Run("overflowEndSequence", func(t *testing.T) {
var block CCFeedbackReportBlock
data := []byte{
0x00, 0x00, 0x00, 0x01, // SSRC
0xff, 0xfe, 0x00, 0x02, // begin_seq, num_reports
0x9F, 0xFD, 0x9F, 0xFC, // reports[0], reports[1]
0x00, 0x00, 0x00, 0x00, // reports[2], reports[3]
}
err := block.unmarshal(data)
assert.Error(t, err)
assert.ErrorIs(t, err, errIncorrectNumReports)
})
}

func TestCCFeedbackReportUnmarshalMarshal(t *testing.T) {
Expand Down
35 changes: 33 additions & 2 deletions transport_layer_cc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package rtcp

import (
"errors"
"reflect"
"testing"
)
Expand Down Expand Up @@ -634,7 +635,7 @@ func TestTransportLayerCC_Unmarshal(t *testing.T) {
WantError: nil,
},
{
Name: "example3",
Name: "example7",
Data: []byte{
0x8f, 0xcd, 0x0, 0x4,
0x9a, 0xcb, 0x4, 0x42,
Expand All @@ -658,14 +659,44 @@ func TestTransportLayerCC_Unmarshal(t *testing.T) {
},
WantError: nil,
},
{
Name: "example8",
Data: []byte{
0xaf, 0xcd, 0x0, 0x5,
0xfa, 0x17, 0xfa, 0x17,
0x43, 0x3, 0x2f, 0xa0,
0x0, 0x99, 0x0, 0x3,
0x3d, 0xe8, 0x2, 0x17,
0x20, 0x3, 0x94, 0x1,
},
Want: TransportLayerCC{},
WantError: errPacketTooShort,
},
{
Name: "example9",
Data: []byte{
0xaf, 0xcd, 0x0, 0x5,
0xfa, 0x17, 0xfa, 0x17,
0x43, 0x3, 0x2f, 0xa0,
0x0, 0x99, 0x0, 0x2,
0x3d, 0xe8, 0x2, 0x17,
0x40, 0x2, 0x94, 0x1,
},
Want: TransportLayerCC{},
WantError: errPacketTooShort,
},
} {
test := test
t.Run(test.Name, func(t *testing.T) {
var chunk TransportLayerCC
err := chunk.Unmarshal(test.Data)
if got, want := err, test.WantError; !errors.Is(got, want) {
t.Fatalf("Unmarshal %q : err = %v, want %v", test.Name, got, want)
}
if err != nil {
t.Fatalf("Unmarshal err: %v", err)
return
}

if got, want := chunk, test.Want; !reflect.DeepEqual(got, want) {
t.Fatalf("Unmarshal %q : got = %v, want %v", test.Name, got, want)
}
Expand Down
2 changes: 1 addition & 1 deletion transport_layer_nack.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (p *TransportLayerNack) Unmarshal(rawPacket []byte) error {
}

// The FCI field MUST contain at least one and MAY contain more than one Generic NACK
if 4*h.Length <= nackOffset || (4*h.Length-nackOffset)%4 != 0 {
if 4*h.Length <= nackOffset {
return errBadLength
}

Expand Down
12 changes: 12 additions & 0 deletions transport_layer_nack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ func TestTransportLayerNackUnmarshal(t *testing.T) {
},
WantError: errPacketTooShort,
},
{
Name: "bad length",
Data: []byte{
// TransportLayerNack
0x81, 0xcd, 0x0, 0x2,
// sender=0x902f9e2e
0x90, 0x2f, 0x9e, 0x2e,
// media=0x902f9e2e
0x90, 0x2f, 0x9e, 0x2e,
},
WantError: errBadLength,
},
{
Name: "wrong type",
Data: []byte{
Expand Down

0 comments on commit dff6dcc

Please sign in to comment.