protocol/rtp: wrote TestPayload

Wrote test TestPayload which will check that we can correctly get the payload from an RTP
packet using Payload for a variety of different RTP packet configurations.
This commit is contained in:
Saxon 2019-04-05 16:11:40 +10:30
parent f4e8d38826
commit d51fa5348f
1 changed files with 60 additions and 2 deletions

View File

@ -28,14 +28,14 @@ LICENSE
package rtp
import (
"bytes"
"testing"
)
// TestVersion checks that we can correctly get the version from an RTP packet.
func TestVersion(t *testing.T) {
const expect = 1
pkt := (&Pkt{V: expect}).Bytes(nil)
got := version(pkt)
got := version((&Pkt{V: expect}).Bytes(nil))
if got != expect {
t.Errorf("unexpected version for RTP packet. Got: %v\n Want: %v\n", got, expect)
}
@ -45,11 +45,13 @@ func TestVersion(t *testing.T) {
// RTP packet.
func TestCsrcCount(t *testing.T) {
const ver, expect = 2, 2
pkt := (&Pkt{
V: ver,
CC: expect,
CSRC: make([][4]byte, expect),
}).Bytes(nil)
got := csrcCount(pkt)
if got != expect {
t.Errorf("unexpected csrc count for RTP packet. Got: %v\n Want: %v\n", got, expect)
@ -70,6 +72,7 @@ func TestHasExt(t *testing.T) {
Header: make([][4]byte, 0),
},
}
got := hasExt(pkt.Bytes(nil))
if !got {
t.Error("RTP packet did not have true extension indicator as expected")
@ -86,6 +89,7 @@ func TestHasExt(t *testing.T) {
// TestExtHeaderLen checks for a correct extension header len for an RTP packet.
func TestExtHeaderLen(t *testing.T) {
const ver, expect = 2, 3
pkt := (&Pkt{
V: ver,
X: true,
@ -94,8 +98,62 @@ func TestExtHeaderLen(t *testing.T) {
Header: make([][4]byte, expect),
},
}).Bytes(nil)
got := extHeaderLen(pkt)
if got != expect {
t.Errorf("Unexpected extension header len. Got: %v\n Want: %v\n", got, expect)
}
}
// TestPayload checks that we can correctly get the payload of an RTP packet
// using Payload for a variety of RTP packet configurations.
func TestPayload(t *testing.T) {
const ver = 2
expect := []byte{0x01, 0x02, 0x03, 0x04, 0x05}
testPkts := [][]byte{
(&Pkt{
V: ver,
Payload: expect,
}).Bytes(nil),
(&Pkt{
V: ver,
CC: 3,
CSRC: make([][4]byte, 3),
Payload: expect,
}).Bytes(nil),
(&Pkt{
V: ver,
X: true,
Extension: ExtensionHeader{
ID: 0,
Header: make([][4]byte, 3),
},
Payload: expect,
}).Bytes(nil),
(&Pkt{
V: ver,
CC: 3,
CSRC: make([][4]byte, 3),
Extension: ExtensionHeader{
ID: 0,
Header: make([][4]byte, 3),
},
Payload: expect,
}).Bytes(nil),
}
for i, p := range testPkts {
got, err := Payload(p)
if err != nil {
t.Errorf("unexpected error from Payload with pkt: %v", i)
}
if !bytes.Equal(got, expect) {
t.Errorf("unexpected payload data from RTP packet: %v.\n Got: %v\n Want: %v\n", i, got, expect)
}
}
}