Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ linters-settings:
recommendations:
- errors
forbidigo:
analyze-types: true
forbid:
- ^fmt.Print(f|ln)?$
- ^log.(Panic|Fatal|Print)(f|ln)?$
- ^os.Exit$
- ^panic$
- ^print(ln)?$
- p: ^testing.T.(Error|Errorf|Fatal|Fatalf|Fail|FailNow)$
pkg: ^testing$
msg: "use testify/assert instead"
varnamelen:
max-distance: 12
min-name-length: 2
Expand Down Expand Up @@ -128,9 +132,12 @@ issues:
exclude-dirs-use-default: false
exclude-rules:
# Allow complex tests and examples, better to be self contained
- path: (examples|main\.go|_test\.go)
- path: (examples|main\.go)
linters:
- gocognit
- forbidigo
- path: _test\.go
linters:
- gocognit

# Allow forbidden identifiers in CLI commands
Expand Down
39 changes: 9 additions & 30 deletions application_defined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
package rtcp

import (
"errors"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTApplicationPacketUnmarshal(t *testing.T) {
Expand Down Expand Up @@ -128,27 +128,16 @@ func TestTApplicationPacketUnmarshal(t *testing.T) {
} {
var apk ApplicationDefined
err := apk.Unmarshal(test.Data)
if got, want := err, test.WantError; !errors.Is(got, want) {
t.Fatalf("Unmarshal %q result: got = %v, want %v", test.Name, got, want)
}
assert.ErrorIsf(t, err, test.WantError, "Unmarshal %q", test.Name)
if err != nil {
continue
}

if got, want := apk, test.Want; !reflect.DeepEqual(got, want) {
t.Fatalf("Unmarshal %q result: got %v, want %v", test.Name, got, want)
}
assert.Equalf(t, test.Want, apk, "Unmarshal %q", test.Name)

// Check SSRC is matching
if apk.SSRC != 0x4baae1ab {
t.Fatalf("SSRC %q result: got packet SSRC %x instead of %x", test.Name, apk.SSRC, 0x4baae1ab)
}
if apk.SSRC != apk.DestinationSSRC()[0] {
t.Fatalf(
"SSRC %q result: DestinationSSRC() %x doesn't match SSRC field %x",
test.Name, apk.DestinationSSRC()[0], apk.SSRC,
)
}
assert.Equalf(t, uint32(0x4baae1ab), apk.SSRC, "%q SSRC mismatch", test.Name)
assert.Equalf(t, uint32(0x4baae1ab), apk.DestinationSSRC()[0], "%q DestinationSSRC mismatch", test.Name)
}
}

Expand Down Expand Up @@ -246,24 +235,14 @@ func TestTApplicationPacketMarshal(t *testing.T) {
},
} {
rawPacket, err := test.Packet.Marshal()

// Check for expected errors
if got, want := err, test.WantError; !errors.Is(got, want) {
t.Fatalf("Marshal %q result: got = %v, want %v", test.Name, got, want)
}
assert.ErrorIsf(t, err, test.WantError, "Marshal %q", test.Name)
if err != nil {
continue
}

// Check for expected successful result
if got, want := rawPacket, test.Want; !reflect.DeepEqual(got, want) {
t.Fatalf("Marshal %q result: got %v, want %v", test.Name, got, want)
}
assert.Equalf(t, test.Want, rawPacket, "Marshal %q", test.Name)

// Check if MarshalSize() is matching the marshaled bytes
marshalSize := test.Packet.MarshalSize()
if marshalSize != len(rawPacket) {
t.Fatalf("MarshalSize %q result: got %d bytes instead of %d", test.Name, len(rawPacket), marshalSize)
}
assert.Equalf(t, marshalSize, len(rawPacket), "MarshalSize %q", test.Name)
}
}
55 changes: 15 additions & 40 deletions compound_packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package rtcp

import (
"errors"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -39,21 +37,14 @@ func TestBadCompound(t *testing.T) {

// this should return an error,
// it violates the "must start with RR or SR" rule
err = compound.Validate()
assert.ErrorIs(t, compound.Validate(), errBadFirstPacket)
assert.Equal(t, 2, len(compound))

if got, want := err, errBadFirstPacket; !errors.Is(got, want) {
t.Fatalf("Unmarshal(badcompound) err=%v, want %v", got, want)
}
_, ok := compound[0].(*Goodbye)
assert.True(t, ok)

if got, want := len(compound), 2; got != want {
t.Fatalf("Unmarshal(badcompound) len=%d, want %d", got, want)
}
if _, ok := compound[0].(*Goodbye); !ok {
t.Fatalf("Unmarshal(badcompound); first packet = %#v, want Goodbye", compound[0])
}
if _, ok := compound[1].(*PictureLossIndication); !ok {
t.Fatalf("Unmarshal(badcompound); second packet = %#v, want PictureLossIndication", compound[1])
}
_, ok = compound[1].(*PictureLossIndication)
assert.True(t, ok)
}

func TestValidPacket(t *testing.T) {
Expand Down Expand Up @@ -135,9 +126,7 @@ func TestValidPacket(t *testing.T) {
Err: nil,
},
} {
if got, want := test.Packet.Validate(), test.Err; !errors.Is(got, want) {
t.Fatalf("Valid(%s) = %v, want %v", test.Name, got, want)
}
assert.ErrorIsf(t, test.Packet.Validate(), test.Err, "Validate(%s)", test.Name)
}
}

Expand Down Expand Up @@ -214,16 +203,11 @@ func TestCNAME(t *testing.T) {
Text: "cname",
},
} {
if got, want := test.Packet.Validate(), test.Err; !errors.Is(got, want) {
t.Fatalf("Valid(%s) = %v, want %v", test.Name, got, want)
}
assert.ErrorIsf(t, test.Packet.Validate(), test.Err, "Validate(%s)", test.Name)

name, err := test.Packet.CNAME()
if got, want := err, test.Err; !errors.Is(got, want) {
t.Fatalf("CNAME(%s) = %v, want %v", test.Name, got, want)
}
if got, want := name, test.Text; got != want {
t.Fatalf("CNAME(%s) = %v, want %v", test.Name, got, want)
}
assert.ErrorIsf(t, err, test.Err, "CNAME(%s)", test.Name)
assert.Equalf(t, test.Text, name, "CNAME(%s)", test.Name)
}
}

Expand Down Expand Up @@ -254,25 +238,16 @@ func TestCompoundPacketRoundTrip(t *testing.T) {
},
} {
data, err := test.Packet.Marshal()
if got, want := err, test.Err; !errors.Is(got, want) {
t.Fatalf("Marshal(%v) err = %v, want nil", test.Name, err)
}
assert.ErrorIsf(t, err, test.Err, "Marshal(%v)", test.Name)
if err != nil {
continue
}

var c CompoundPacket
if err = c.Unmarshal(data); err != nil {
t.Fatalf("Unmarshal(%v) err = %v, want nil", test.Name, err)
}
assert.NoErrorf(t, c.Unmarshal(data), "Unmarshal(%v)", test.Name)

data2, err := c.Marshal()
if err != nil {
t.Fatalf("Marshal(%v) err = %v, want nil", test.Name, err)
}

if got, want := data, data2; !reflect.DeepEqual(got, want) {
t.Fatalf("Unmarshal(Marshal(%v)) = %v, want %v", test.Name, got, want)
}
assert.NoErrorf(t, err, "Marshal(%v)", test.Name)
assert.Equalf(t, data, data2, "Marshal(%v) mismatch", test.Name)
}
}
41 changes: 11 additions & 30 deletions extended_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ package rtcp

import (
"fmt"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

// Assert that ExtendedReport is a Packet.
Expand Down Expand Up @@ -225,17 +226,11 @@ func TestEncode(t *testing.T) {
expected := encodedPacket()
packet := testPacket()
rawPacket, err := packet.Marshal()
if err != nil {
t.Fatalf("Error marshaling packet: %v", err)
}
if len(rawPacket) != len(expected) {
t.Fatalf("Encoded message is %d bytes; expected is %d", len(rawPacket), len(expected))
}
assert.NoError(t, err)
assert.Equal(t, len(expected), len(rawPacket), "Encoded message length does not match expected length")

for i := 0; i < len(rawPacket); i++ {
if rawPacket[i] != expected[i] {
t.Errorf("Byte %d of encoded packet does not match: expected 0x%02X, got 0x%02X", i, expected[i], rawPacket[i])
}
assert.Equalf(t, expected[i], rawPacket[i], "Byte %d of encoded packet does not match", i)
}
}

Expand All @@ -246,40 +241,26 @@ func TestDecode(t *testing.T) {
// We need to make sure the header has been set up correctly
// before we test for equality
extendedReports, ok := expected.(*ExtendedReport)
if !ok {
t.Fatal("Failed to cast")
}
assert.True(t, ok)

for _, p := range extendedReports.Reports {
p.setupBlockHeader()
}

report := new(ExtendedReport)
err := report.Unmarshal(encoded)
if err != nil {
t.Fatalf("Error unmarshaling packet: %v", err)
}

if !reflect.DeepEqual(report, expected) {
t.Errorf("(deep equal) Decoded packet does not match expected packet")
}
assert.NoError(t, err)
assert.Equal(t, expected, report)

pktStringer, ok := expected.(fmt.Stringer)
if !ok {
t.Fatal("Failed to cast")
}

if report.String() != pktStringer.String() {
t.Errorf("(string compare) Decoded packet does not match expected packet")
}
assert.True(t, ok)
assert.Equal(t, report.String(), pktStringer.String(), "Decoded packet does not match expected packet")

var includeSenderSSRC bool
for _, ssrc := range report.DestinationSSRC() {
if ssrc == report.SenderSSRC {
includeSenderSSRC = true
}
}
if !includeSenderSSRC {
t.Errorf("DestinationSSRC does not include the SenderSSRC")
}
assert.True(t, includeSenderSSRC, "DestinationSSRC does not include the SenderSSRC")
}
33 changes: 9 additions & 24 deletions full_intra_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
package rtcp

import (
"errors"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFullIntraRequestUnmarshal(t *testing.T) {
Expand Down Expand Up @@ -141,16 +141,12 @@ func TestFullIntraRequestUnmarshal(t *testing.T) {
} {
var fir FullIntraRequest
err := fir.Unmarshal(test.Data)
if got, want := err, test.WantError; !errors.Is(got, want) {
t.Fatalf("Unmarshal %q rr: err = %v, want %v", test.Name, got, want)
}
assert.ErrorIsf(t, err, test.WantError, "Unmarshal %q rr mismatch", test.Name)
if err != nil {
continue
}

if got, want := fir, test.Want; !reflect.DeepEqual(got, want) {
t.Fatalf("Unmarshal %q rr: got %v, want %v", test.Name, got, want)
}
assert.Equalf(t, test.Want, fir, "Unmarshal %q rr mismatch", test.Name)
}
}

Expand Down Expand Up @@ -184,21 +180,14 @@ func TestFullIntraRequestRoundTrip(t *testing.T) {
},
} {
data, err := test.Packet.Marshal()
if got, want := err, test.WantError; !errors.Is(got, want) {
t.Fatalf("Marshal %q: err = %v, want %v", test.Name, got, want)
}
assert.ErrorIsf(t, err, test.WantError, "Marshal %q", test.Name)
if err != nil {
continue
}

var decoded FullIntraRequest
if err := decoded.Unmarshal(data); err != nil {
t.Fatalf("Unmarshal %q: %v", test.Name, err)
}

if got, want := decoded, test.Packet; !reflect.DeepEqual(got, want) {
t.Fatalf("%q rr round trip: got %#v, want %#v", test.Name, got, want)
}
assert.NoErrorf(t, decoded.Unmarshal(data), "Unmarshal %q", test.Name)
assert.Equalf(t, test.Packet, decoded, "%q rr header mismatch", test.Name)
}
}

Expand Down Expand Up @@ -232,15 +221,11 @@ func TestFullIntraRequestUnmarshalHeader(t *testing.T) {
} {
var fir FullIntraRequest
err := fir.Unmarshal(test.Data)
if got, want := err, test.WantError; !errors.Is(got, want) {
t.Fatalf("Unmarshal header %q rr: err = %v, want %v", test.Name, got, want)
}
assert.ErrorIsf(t, err, test.WantError, "Unmarshal header %q rr mismatch", test.Name)
if err != nil {
continue
}

if got, want := fir.Header(), test.Want; !reflect.DeepEqual(got, want) {
t.Fatalf("Unmarshal header %q rr: got %v, want %v", test.Name, got, want)
}
assert.Equalf(t, test.Want, fir.Header(), "Unmarshal header %q rr mismatch", test.Name)
}
}
Loading
Loading