psi: wrote first tests, for simple pat and pmt tables. Pat writing seems to be working

This commit is contained in:
saxon 2018-12-11 14:52:18 +10:30
parent 43abed9522
commit a5fa6bed5f
2 changed files with 42 additions and 33 deletions

View File

@ -20,7 +20,7 @@ var (
// syntax section // syntax section
0x00, 0x01, // table id extension 0x00, 0x01, // table id extension
0xc1, // reserved bits:3|version:5|use now:1 1100 0001 0xc1, // reserved bits:2|version:5|use now:1 1100 0001
0x00, // section number 0x00, // section number
0x00, // last section number 0x00, // last section number
// table data // table data
@ -141,12 +141,7 @@ func ReadPSI(data []byte) *PSI {
pos := 0 pos := 0
psi.Pf = data[pos] psi.Pf = data[pos]
if psi.Pf != 0 { if psi.Pf != 0 {
psi.Pfb = make([]byte, 0, psi.Pf) panic("No support for pointer filler bytes")
pos++
for i := 0; i < int(psi.Pf); i++ {
psi.Pfb = append(psi.Pfb, data[pos])
pos++
}
} }
psi.Tid = data[pos] psi.Tid = data[pos]
pos++ pos++
@ -242,17 +237,16 @@ func readEssd(data []byte) *ESSD {
// Bytes outputs a byte slice representation of the PSI // Bytes outputs a byte slice representation of the PSI
func (p *PSI) Bytes() []byte { func (p *PSI) Bytes() []byte {
l := 1 + len(p.Pfb) out := make([]byte, 4)
out := make([]byte, l+PSIDefLen)
out[0] = p.Pf out[0] = p.Pf
for i, b := range p.Pfb { if p.Pf != 0 {
out[1+i] = b panic("No support for pointer filler bytes")
} }
out[l] = p.Tid out[1] = p.Tid
out[l+1] = 0x80 | 0x40 | 0x30 | (0x03 & byte(p.Sl>>8)) out[2] = 0x80 | 0x30 | (0x03 & byte(p.Sl>>8))
out[l+2] = byte(p.Sl) out[3] = byte(p.Sl)
out = append(out, p.Tss.Bytes()...) out = append(out, p.Tss.Bytes()...)
crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), out[l:]) crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), out[3:])
out = append(out, make([]byte, 0, 4)...) out = append(out, make([]byte, 0, 4)...)
out = append(out, byte(crc32>>24)) out = append(out, byte(crc32>>24))
out = append(out, byte(crc32>>16)) out = append(out, byte(crc32>>16))

View File

@ -1,6 +1,7 @@
package psi package psi
import ( import (
"bytes"
"testing" "testing"
) )
@ -19,34 +20,48 @@ var (
Lsn: 0, Lsn: 0,
Sd: &PAT{ Sd: &PAT{
Pn: uint16(0x01), Pn: uint16(0x01),
Pmpid: 16, Pmpid: uint16(0x1000),
}, },
}, },
} }
pmtPsi = PSI{ pmtPsi = PSI{
Pf: 0x00, Pf: 0x00,
Tid: 0x02, Tid: 0x02,
Ssi: true, Ssi: true,
Sl: uint16(0x12), Sl: uint16(0x12),
Tss: & TSS{ Tss: &TSS{
Tide: uint16(0x01), Tide: uint16(0x01),
V:0, V: 0,
Cni: true, Cni: true,
Sn: 0, Sn: 0,
Lsn: 0, Lsn: 0,
Sd: &PMT{ Sd: &PMT{
Pcrpid: 16, // wrong Pcrpid: 0x1100, // wrong
Pil: 0, Pil: 0,
Essd: &ESSD{ Essd: &ESSD{
St: 0x1b, St: 0x1b,
Epid: 0x0100,
} Esil: 0x00,
} },
} },
} },
} }
) )
func TestReadPSI1(t *testing.T) { func TestWritePAT1(t *testing.T) {
got := patPsi.Bytes()
// Remove crc32
got = got[:len(got)-4]
if !bytes.Equal(StdPat, got[:len(got)-4]) {
t.Errorf("Incorrect output, wanted: %v, got: %v", StdPat, got)
}
}
func TestWritePMT1(t *testing.T) {
got := pmtPsi.Bytes()
// Remove crc32
got = got[:len(got)-4]
if !bytes.Equal(StdPmt, got[:len(got)-4]) {
t.Errorf("Incorrect output, wanted: %v, got: %v", StdPmt, got)
}
} }