container/mts: wrote test TestFindPid and corrected bug

Completed test TestFindPid to validate function of FindPid func in mpegts.go. Through this
process, it was found that there was a bug in this func, whereby the returned packet was
not complete due to indexing issues. The bug is fixed and the test passes.
This commit is contained in:
Saxon 2019-05-07 02:20:07 +09:30
parent 52c8a43cb0
commit a8aec484df
2 changed files with 43 additions and 2 deletions

View File

@ -179,12 +179,12 @@ func FindPat(d []byte) ([]byte, int, error) {
// is found, then it is returned along with its index, otherwise nil, -1 and an error is returned.
func FindPid(d []byte, pid uint16) (pkt []byte, i int, err error) {
if len(d) < PacketSize {
return nil, -1, errors.New("Mmpegts data not of valid length")
return nil, -1, errors.New("Mpegts data not of valid length")
}
for i = 0; i < len(d); i += PacketSize {
p := (uint16(d[i+1]&0x1f) << 8) | uint16(d[i+2])
if p == pid {
pkt = d[i+4 : i+PacketSize]
pkt = d[i : i+PacketSize]
return
}
}

View File

@ -30,6 +30,8 @@ package mts
import (
"bytes"
"testing"
"github.com/Comcast/gots/packet"
)
func TestBytes(t *testing.T) {
@ -94,3 +96,42 @@ func TestBytes(t *testing.T) {
}
}
}
func TestFindPid(t *testing.T) {
const targetPacketNum, numOfPackets, targetPid, stdPid = 6, 15, 1, 0
var stream []byte
for i := 0; i < numOfPackets; i++ {
pid := uint16(0)
if i == targetPacketNum {
pid = 1
}
p := Packet{
PID: pid,
AFC: hasPayload | hasAdaptationField,
}
p.FillPayload([]byte{byte(i)})
stream = append(stream, p.Bytes(nil)...)
}
p, i, err := FindPid(stream, targetPid)
if err != nil {
t.Fatalf("unexpected error finding PID: %v\n", err)
}
var _p packet.Packet
copy(_p[:], p)
payload, err := packet.Payload(&_p)
if err != nil {
t.Fatalf("unexpected error getting packet payload: %v\n", err)
}
got := payload[0]
if got != targetPacketNum {
t.Errorf("payload of found packet is not correct.\nGot: %v, Want: %v\n", got, targetPacketNum)
}
_got := i / PacketSize
if _got != targetPacketNum {
t.Errorf("index of found packet is not correct.\nGot: %v, want: %v\n", _got, targetPacketNum)
}
}