container/mts: added comments to tests

This commit is contained in:
Saxon 2019-05-07 13:17:33 +09:30
parent a8aec484df
commit c8531b2899
1 changed files with 18 additions and 12 deletions

View File

@ -34,11 +34,14 @@ import (
"github.com/Comcast/gots/packet" "github.com/Comcast/gots/packet"
) )
// TestBytes checks that Packet.Bytes() correctly produces a []byte
// representation of a Packet.
func TestBytes(t *testing.T) { func TestBytes(t *testing.T) {
const payloadLen, payloadChar, stuffingChar = 120, 0x11, 0xff
const stuffingLen = PacketSize - payloadLen - 12
tests := []struct { tests := []struct {
packet Packet packet Packet
payloadLen int
stuffingLen int
expectedHeader []byte expectedHeader []byte
}{ }{
{ {
@ -51,14 +54,12 @@ func TestBytes(t *testing.T) {
PCRF: true, PCRF: true,
PCR: 1, PCR: 1,
}, },
payloadLen: 120,
stuffingLen: 56,
expectedHeader: []byte{ expectedHeader: []byte{
0x47, // Sync byte. 0x47, // Sync byte.
0x40, // TEI=0, PUSI=1, TP=0, PID=00000. 0x40, // TEI=0, PUSI=1, TP=0, PID=00000.
0x01, // PID(Cont)=00000001. 0x01, // PID(Cont)=00000001.
0x34, // TSC=00, AFC=11(adaptation followed by payload), CC=0100(4). 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. 0x50, // DI=0,RAI=1,ESPI=0,PCRF=1,OPCRF=0,SPF=0,TPDF=0, AFEF=0.
0x00, 0x00, 0x00, 0x00, 0x80, 0x00, // PCR. 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, // PCR.
}, },
@ -67,26 +68,24 @@ func TestBytes(t *testing.T) {
for testNum, test := range tests { for testNum, test := range tests {
// Construct payload. // Construct payload.
const payloadChar = 0x11 payload := make([]byte, 0, payloadLen)
payload := make([]byte, 0, test.payloadLen) for i := 0; i < payloadLen; i++ {
for i := 0; i < test.payloadLen; i++ {
payload = append(payload, payloadChar) payload = append(payload, payloadChar)
} }
// Fill the packet payload. // Fill the packet payload.
test.packet.FillPayload(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) expected := make([]byte, len(test.expectedHeader), PacketSize)
copy(expected, test.expectedHeader) copy(expected, test.expectedHeader)
// Append stuffing. // Append stuffing.
const stuffingChar = 0xff for i := 0; i < stuffingLen; i++ {
for i := 0; i < test.stuffingLen; i++ {
expected = append(expected, stuffingChar) expected = append(expected, stuffingChar)
} }
// Copy in payload. // Append payload to expected bytes.
expected = append(expected, payload...) expected = append(expected, payload...)
// Compare got with expected. // 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) { func TestFindPid(t *testing.T) {
const targetPacketNum, numOfPackets, targetPid, stdPid = 6, 15, 1, 0 const targetPacketNum, numOfPackets, targetPid, stdPid = 6, 15, 1, 0
// Prepare the stream of packets.
var stream []byte var stream []byte
for i := 0; i < numOfPackets; i++ { for i := 0; i < numOfPackets; i++ {
pid := uint16(0) pid := uint16(0)
if i == targetPacketNum { if i == targetPacketNum {
pid = 1 pid = 1
} }
p := Packet{ p := Packet{
PID: pid, PID: pid,
AFC: hasPayload | hasAdaptationField, AFC: hasPayload | hasAdaptationField,
@ -114,11 +117,13 @@ func TestFindPid(t *testing.T) {
stream = append(stream, p.Bytes(nil)...) stream = append(stream, p.Bytes(nil)...)
} }
// Try to find the targetPid in the stream.
p, i, err := FindPid(stream, targetPid) p, i, err := FindPid(stream, targetPid)
if err != nil { if err != nil {
t.Fatalf("unexpected error finding PID: %v\n", err) t.Fatalf("unexpected error finding PID: %v\n", err)
} }
// Check the payload.
var _p packet.Packet var _p packet.Packet
copy(_p[:], p) copy(_p[:], p)
payload, err := packet.Payload(&_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) t.Errorf("payload of found packet is not correct.\nGot: %v, Want: %v\n", got, targetPacketNum)
} }
// Check the index.
_got := i / PacketSize _got := i / PacketSize
if _got != targetPacketNum { if _got != targetPacketNum {
t.Errorf("index of found packet is not correct.\nGot: %v, want: %v\n", _got, targetPacketNum) t.Errorf("index of found packet is not correct.\nGot: %v, want: %v\n", _got, targetPacketNum)