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 package rtp
import ( import (
"bytes"
"testing" "testing"
) )
// TestVersion checks that we can correctly get the version from an RTP packet. // TestVersion checks that we can correctly get the version from an RTP packet.
func TestVersion(t *testing.T) { func TestVersion(t *testing.T) {
const expect = 1 const expect = 1
pkt := (&Pkt{V: expect}).Bytes(nil) got := version((&Pkt{V: expect}).Bytes(nil))
got := version(pkt)
if got != expect { if got != expect {
t.Errorf("unexpected version for RTP packet. Got: %v\n Want: %v\n", 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. // RTP packet.
func TestCsrcCount(t *testing.T) { func TestCsrcCount(t *testing.T) {
const ver, expect = 2, 2 const ver, expect = 2, 2
pkt := (&Pkt{ pkt := (&Pkt{
V: ver, V: ver,
CC: expect, CC: expect,
CSRC: make([][4]byte, expect), CSRC: make([][4]byte, expect),
}).Bytes(nil) }).Bytes(nil)
got := csrcCount(pkt) got := csrcCount(pkt)
if got != expect { if got != expect {
t.Errorf("unexpected csrc count for RTP packet. Got: %v\n Want: %v\n", 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), Header: make([][4]byte, 0),
}, },
} }
got := hasExt(pkt.Bytes(nil)) got := hasExt(pkt.Bytes(nil))
if !got { if !got {
t.Error("RTP packet did not have true extension indicator as expected") 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. // TestExtHeaderLen checks for a correct extension header len for an RTP packet.
func TestExtHeaderLen(t *testing.T) { func TestExtHeaderLen(t *testing.T) {
const ver, expect = 2, 3 const ver, expect = 2, 3
pkt := (&Pkt{ pkt := (&Pkt{
V: ver, V: ver,
X: true, X: true,
@ -94,8 +98,62 @@ func TestExtHeaderLen(t *testing.T) {
Header: make([][4]byte, expect), Header: make([][4]byte, expect),
}, },
}).Bytes(nil) }).Bytes(nil)
got := extHeaderLen(pkt) got := extHeaderLen(pkt)
if got != expect { if got != expect {
t.Errorf("Unexpected extension header len. Got: %v\n Want: %v\n", 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)
}
}
}