From c8531b2899f78b59e0e56cd9729124a7e7ae0ba5 Mon Sep 17 00:00:00 2001 From: Saxon Date: Tue, 7 May 2019 13:17:33 +0930 Subject: [PATCH] container/mts: added comments to tests --- container/mts/mpegts_test.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/container/mts/mpegts_test.go b/container/mts/mpegts_test.go index 8322df72..9efdfbfc 100644 --- a/container/mts/mpegts_test.go +++ b/container/mts/mpegts_test.go @@ -34,11 +34,14 @@ import ( "github.com/Comcast/gots/packet" ) +// TestBytes checks that Packet.Bytes() correctly produces a []byte +// representation of a Packet. func TestBytes(t *testing.T) { + const payloadLen, payloadChar, stuffingChar = 120, 0x11, 0xff + const stuffingLen = PacketSize - payloadLen - 12 + tests := []struct { packet Packet - payloadLen int - stuffingLen int expectedHeader []byte }{ { @@ -51,14 +54,12 @@ func TestBytes(t *testing.T) { PCRF: true, PCR: 1, }, - payloadLen: 120, - stuffingLen: 56, expectedHeader: []byte{ 0x47, // Sync byte. 0x40, // TEI=0, PUSI=1, TP=0, PID=00000. 0x01, // PID(Cont)=00000001. 0x34, // TSC=00, AFC=11(adaptation followed by payload), CC=0100(4). - 0x3f, // AFL=. + byte(7 + stuffingLen), // AFL=. 0x50, // DI=0,RAI=1,ESPI=0,PCRF=1,OPCRF=0,SPF=0,TPDF=0, AFEF=0. 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, // PCR. }, @@ -67,26 +68,24 @@ func TestBytes(t *testing.T) { for testNum, test := range tests { // Construct payload. - const payloadChar = 0x11 - payload := make([]byte, 0, test.payloadLen) - for i := 0; i < test.payloadLen; i++ { + payload := make([]byte, 0, payloadLen) + for i := 0; i < payloadLen; i++ { payload = append(payload, payloadChar) } // Fill the packet payload. test.packet.FillPayload(payload) - // Create packet data and copy in expected header. + // Create expected packet data and copy in expected header. expected := make([]byte, len(test.expectedHeader), PacketSize) copy(expected, test.expectedHeader) // Append stuffing. - const stuffingChar = 0xff - for i := 0; i < test.stuffingLen; i++ { + for i := 0; i < stuffingLen; i++ { expected = append(expected, stuffingChar) } - // Copy in payload. + // Append payload to expected bytes. expected = append(expected, payload...) // Compare got with expected. @@ -97,15 +96,19 @@ func TestBytes(t *testing.T) { } } +// TestFindPid checks that FindPid can correctly extract the first instance +// of a PID from an MPEGTS stream. func TestFindPid(t *testing.T) { const targetPacketNum, numOfPackets, targetPid, stdPid = 6, 15, 1, 0 + // Prepare the stream of packets. var stream []byte for i := 0; i < numOfPackets; i++ { pid := uint16(0) if i == targetPacketNum { pid = 1 } + p := Packet{ PID: pid, AFC: hasPayload | hasAdaptationField, @@ -114,11 +117,13 @@ func TestFindPid(t *testing.T) { stream = append(stream, p.Bytes(nil)...) } + // Try to find the targetPid in the stream. p, i, err := FindPid(stream, targetPid) if err != nil { t.Fatalf("unexpected error finding PID: %v\n", err) } + // Check the payload. var _p packet.Packet copy(_p[:], p) payload, err := packet.Payload(&_p) @@ -130,6 +135,7 @@ func TestFindPid(t *testing.T) { t.Errorf("payload of found packet is not correct.\nGot: %v, Want: %v\n", got, targetPacketNum) } + // Check the index. _got := i / PacketSize if _got != targetPacketNum { t.Errorf("index of found packet is not correct.\nGot: %v, want: %v\n", _got, targetPacketNum)