From 3dfc7ed990eeade6ef0d81f6efb8dac09426a3c2 Mon Sep 17 00:00:00 2001 From: Saxon Date: Fri, 5 Apr 2019 16:11:40 +1030 Subject: [PATCH] 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. --- protocol/rtp/parse_test.go | 62 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/protocol/rtp/parse_test.go b/protocol/rtp/parse_test.go index 9af2ed0c..74b770f8 100644 --- a/protocol/rtp/parse_test.go +++ b/protocol/rtp/parse_test.go @@ -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) + } + } +}