From aa947d112cc7ab24b33bab8e7ff0208d7cd14cc1 Mon Sep 17 00:00:00 2001 From: Saxon Date: Mon, 15 Apr 2019 17:35:36 +0930 Subject: [PATCH] protocol/rtcp: wrote test for Client.formPayload --- protocol/rtcp/client.go | 3 ++ protocol/rtcp/client_test.go | 81 ++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/protocol/rtcp/client.go b/protocol/rtcp/client.go index 6659f349..654f56ae 100644 --- a/protocol/rtcp/client.go +++ b/protocol/rtcp/client.go @@ -138,6 +138,9 @@ func (c *client) send() { } } +// formPayload takes a pointer to a ReceiverReport and a pointer to a +// Source Description and calls Bytes on both, writing to the underlying client +// buf. A slice to the combined writtem memory is returned. func (c *client) formPayload(r *ReceiverReport, d *SourceDescription) []byte { rl := len(r.Bytes(c.buf[:])) dl := len(d.Bytes(c.buf[rl:])) diff --git a/protocol/rtcp/client_test.go b/protocol/rtcp/client_test.go index e566aa7c..cc10d4b9 100644 --- a/protocol/rtcp/client_test.go +++ b/protocol/rtcp/client_test.go @@ -1,5 +1,85 @@ package rtcp +import ( + "bytes" + "fmt" + "math" + "testing" +) + +func TestFormPayload(t *testing.T) { + expect := []byte{ + 0x81, 0xc9, 0x00, 0x07, + 0xd6, 0xe0, 0x98, 0xda, + 0x6f, 0xad, 0x40, 0xc6, + 0x00, 0xff, 0xff, 0xff, + 0x00, 0x01, 0x83, 0x08, + 0x00, 0x00, 0x00, 0x20, + 0xb9, 0xe1, 0x25, 0x2a, + 0x00, 0x00, 0x2b, 0xf9, + 0x81, 0xca, 0x00, 0x04, + 0xd6, 0xe0, 0x98, 0xda, + 0x01, 0x08, 0x73, 0x61, + 0x78, 0x6f, 0x6e, 0x2d, + 0x70, 0x63, 0x00, 0x00, + } + + report := ReceiverReport{ + Header: Header{ + Version: 2, + Padding: false, + ReportCount: 1, + Type: typeReceiverReport, + }, + SenderSSRC: 3605043418, + Blocks: []ReportBlock{ + ReportBlock{ + SSRC: 1873625286, + FractionLost: 0, + PacketsLost: math.MaxUint32, + HighestSequence: 99080, + Jitter: 32, + LSR: 3118540074, + DLSR: 11257, + }, + }, + Extensions: nil, + } + + description := SourceDescription{ + Header: Header{ + Version: 2, + Padding: false, + ReportCount: 1, + Type: typeSourceDescription, + }, + Chunks: []Chunk{ + Chunk{ + SSRC: 3605043418, + Items: []SDESItem{ + SDESItem{ + Type: typeCName, + Text: []byte("saxon-pc"), + }, + }, + }, + }, + } + + c := &client{} + p := c.formPayload(&report, &description) + + if !bytes.Equal(p, expect) { + t.Fatalf("unexpected result.\nGot: %v\n Want: %v\n", p, expect) + } + + bufAddr := fmt.Sprintf("%p", c.buf[:]) + pAddr := fmt.Sprintf("%p", p) + if bufAddr != pAddr { + t.Errorf("unexpected result.\nGot: %v\n want: %v\n", pAddr, bufAddr) + } +} + /* func TestReceiveAndSend(t *testing.T) { quit := make(chan struct{}) @@ -24,4 +104,5 @@ func testServer(quit chan struct{}, t *testing.T) { default: } } + */