From 568840f69180180380ddf9729108fc7085c07a1e Mon Sep 17 00:00:00 2001 From: saxon Date: Tue, 29 Jan 2019 18:22:57 +1030 Subject: [PATCH] stream/mts/psi: cleaned up descriptor_test.go --- stream/mts/psi/descriptor_test.go | 264 ++++++++++++++---------------- 1 file changed, 124 insertions(+), 140 deletions(-) diff --git a/stream/mts/psi/descriptor_test.go b/stream/mts/psi/descriptor_test.go index 99d005c2..dd763b82 100644 --- a/stream/mts/psi/descriptor_test.go +++ b/stream/mts/psi/descriptor_test.go @@ -37,124 +37,123 @@ const ( errUnexpectedErr = "Unexpected error: %v\n" ) -var tstPsi1 = PSI{ - Pf: 0x00, - Tid: 0x02, - Ssi: true, - Sl: 0x1c, - Tss: &TSS{ - Tide: 0x01, - V: 0, - Cni: true, - Sn: 0, - Lsn: 0, - Sd: &PMT{ - Pcrpid: 0x0100, // wrong - Pil: 10, - Pd: []Desc{ - { - Dt: TimeDescTag, - Dl: TimeDataSize, - Dd: make([]byte, TimeDataSize), +var ( + tstPsi1 = PSI{ + Pf: 0x00, + Tid: 0x02, + Ssi: true, + Sl: 0x1c, + Tss: &TSS{ + Tide: 0x01, + V: 0, + Cni: true, + Sn: 0, + Lsn: 0, + Sd: &PMT{ + Pcrpid: 0x0100, // wrong + Pil: 10, + Pd: []Desc{ + { + Dt: TimeDescTag, + Dl: TimeDataSize, + Dd: make([]byte, TimeDataSize), + }, + }, + Essd: &ESSD{ + St: 0x1b, + Epid: 0x0100, + Esil: 0x00, }, }, - Essd: &ESSD{ - St: 0x1b, - Epid: 0x0100, - Esil: 0x00, - }, }, - }, -} + } -var tstPsi2 = PSI{ - Pf: 0x00, - Tid: 0x02, - Ssi: true, - Sl: 0x12, - Tss: &TSS{ - Tide: 0x01, - V: 0, - Cni: true, - Sn: 0, - Lsn: 0, - Sd: &PMT{ - Pcrpid: 0x0100, - Pil: 0, - Essd: &ESSD{ - St: 0x1b, - Epid: 0x0100, - Esil: 0x00, - }, - }, - }, -} - -var tstPsi3 = PSI{ - Pf: 0x00, - Tid: 0x02, - Ssi: true, - Sl: 0x3e, - Tss: &TSS{ - Tide: 0x01, - V: 0, - Cni: true, - Sn: 0, - Lsn: 0, - Sd: &PMT{ - Pcrpid: 0x0100, - Pil: PmtTimeLocationPil, - Pd: []Desc{ - { - Dt: TimeDescTag, - Dl: TimeDataSize, - Dd: make([]byte, TimeDataSize), - }, - { - Dt: LocationDescTag, - Dl: LocationDataSize, - Dd: make([]byte, LocationDataSize), + tstPsi2 = PSI{ + Pf: 0x00, + Tid: 0x02, + Ssi: true, + Sl: 0x12, + Tss: &TSS{ + Tide: 0x01, + V: 0, + Cni: true, + Sn: 0, + Lsn: 0, + Sd: &PMT{ + Pcrpid: 0x0100, + Pil: 0, + Essd: &ESSD{ + St: 0x1b, + Epid: 0x0100, + Esil: 0x00, }, }, - Essd: &ESSD{ - St: 0x1b, - Epid: 0x0100, - Esil: 0x00, + }, + } + + tstPsi3 = PSI{ + Pf: 0x00, + Tid: 0x02, + Ssi: true, + Sl: 0x3e, + Tss: &TSS{ + Tide: 0x01, + V: 0, + Cni: true, + Sn: 0, + Lsn: 0, + Sd: &PMT{ + Pcrpid: 0x0100, + Pil: PmtTimeLocationPil, + Pd: []Desc{ + { + Dt: TimeDescTag, + Dl: TimeDataSize, + Dd: make([]byte, TimeDataSize), + }, + { + Dt: LocationDescTag, + Dl: LocationDataSize, + Dd: make([]byte, LocationDataSize), + }, + }, + Essd: &ESSD{ + St: 0x1b, + Epid: 0x0100, + Esil: 0x00, + }, }, }, - }, -} + } +) -func TestHasDescriptor(t *testing.T) { +func TestHasDescriptorExists(t *testing.T) { p := PSIBytes(tstPsi3.Bytes()) - - // Try getting descriptor that exists _, got := p.HasDescriptor(LocationDescTag) want := []byte{ LocationDescTag, LocationDataSize, } want = append(want, make([]byte, LocationDataSize)...) - if !bytes.Equal(got, want) { t.Errorf(errNotExpectedOut, got, want) } +} - // Try getting descriptor that doesnt exist +func TestHasDescriptorAbsent(t *testing.T) { + p := PSIBytes(tstPsi3.Bytes()) const fakeTag = 236 - _, got = p.HasDescriptor(fakeTag) - want = nil - + _, got := p.HasDescriptor(fakeTag) + var want []byte if !bytes.Equal(got, want) { t.Errorf(errNotExpectedOut, got, want) } +} - // Try getting descriptor from psi that has no descriptors - p = PSIBytes(tstPsi2.Bytes()) - - _, got = p.HasDescriptor(LocationDescTag) - want = nil - +func TestHasDescriptorNone(t *testing.T) { + p := PSIBytes(tstPsi2.Bytes()) + _, got := p.HasDescriptor(LocationDescTag) + var want []byte if !bytes.Equal(got, want) { t.Errorf(errNotExpectedOut, got, want) } @@ -170,7 +169,6 @@ func TestProgramInfoLen(t *testing.T) { } func TestDescriptors(t *testing.T) { - // Try psi with descriptors p := PSIBytes(tstPsi1.Bytes()) got := p.descriptors() want := []byte{ @@ -178,71 +176,57 @@ func TestDescriptors(t *testing.T) { TimeDataSize, } want = append(want, make([]byte, TimeDataSize)...) - - if !bytes.Equal(got, want) { - t.Errorf(errNotExpectedOut, got, want) - } - - // Now try psi with empty descriptors - p = PSIBytes(tstPsi2.Bytes()) - got = p.descriptors() - want = nil if !bytes.Equal(got, want) { t.Errorf(errNotExpectedOut, got, want) } } -func TestCreateDescriptor(t *testing.T) { - // Test with PSI containing no descriptors +func TestDescriptorsNone(t *testing.T) { p := PSIBytes(tstPsi2.Bytes()) - p.createDescriptor(TimeDescTag, make([]byte, TimeDataSize)) - // update crc - noPadding := p.trimPadding() - updateCrc(noPadding[1:]) - want := PSIBytes(tstPsi1.Bytes()) - got := p - - if !bytes.Equal(want, got) { + got := p.descriptors() + var want []byte + if !bytes.Equal(got, want) { t.Errorf(errNotExpectedOut, got, want) } +} - // Test with psi containing descriptor already - p = PSIBytes(tstPsi1.Bytes()) - p.createDescriptor(LocationDescTag, make([]byte, LocationDataSize)) - // update crc - noPadding = p.trimPadding() - updateCrc(noPadding[1:]) - want = PSIBytes(tstPsi3.Bytes()) - got = p - +func TestCreateDescriptorEmpty(t *testing.T) { + got := PSIBytes(tstPsi2.Bytes()) + got.createDescriptor(TimeDescTag, make([]byte, TimeDataSize)) + updateCrc(got[1:]) + want := PSIBytes(tstPsi1.Bytes()) if !bytes.Equal(want, got) { t.Errorf(errNotExpectedOut, got, want) } } -func TestAddDescriptor(t *testing.T) { - // Add descriptor to psi without descriptors - got := PSIBytes(tstPsi2.Bytes()) +func TestCreateDescriptorNotEmpty(t *testing.T) { + got := PSIBytes(tstPsi1.Bytes()) + got.createDescriptor(LocationDescTag, make([]byte, LocationDataSize)) + updateCrc(got[1:]) + want := PSIBytes(tstPsi3.Bytes()) + if !bytes.Equal(want, got) { + t.Errorf(errNotExpectedOut, got, want) + } +} +func TestAddDescriptorEmpty(t *testing.T) { + got := PSIBytes(tstPsi2.Bytes()) if err := got.AddDescriptor(TimeDescTag, make([]byte, TimeDataSize)); err != nil { t.Errorf(errUnexpectedErr, err.Error()) } - want := PSIBytes(tstPsi1.Bytes()) - - if !bytes.Equal(got, want) { - t.Errorf(errNotExpectedOut, got, want) - } - - // Add to psi already with descriptor - got = PSIBytes(tstPsi1.Bytes()) - - if err := got.AddDescriptor(LocationDescTag, make([]byte, LocationDataSize)); err != nil { - t.Errorf(errUnexpectedErr, err.Error()) - } - - want = PSIBytes(tstPsi3.Bytes()) - + if !bytes.Equal(got, want) { + t.Errorf(errNotExpectedOut, got, want) + } +} + +func TestAddDescriptorNonEmpty(t *testing.T) { + got := PSIBytes(tstPsi1.Bytes()) + if err := got.AddDescriptor(LocationDescTag, make([]byte, LocationDataSize)); err != nil { + t.Errorf(errUnexpectedErr, err.Error()) + } + want := PSIBytes(tstPsi3.Bytes()) if !bytes.Equal(got, want) { t.Errorf(errNotExpectedOut, got, want) }