/* NAME descriptor_test.go DESCRIPTION See Readme.md AUTHOR Saxon Nelson-Milton LICENSE descriptor_test.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean) It is free software: you can redistribute it and/or modify them under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with revid in gpl.txt. If not, see http://www.gnu.org/licenses. */ package psi import ( "bytes" "testing" ) const ( errNotExpectedOut = "Did not get expected output: \ngot : %v, \nwant: %v" ) 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, }, }, }, } 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), }, }, Essd: &ESSD{ St: 0x1b, Epid: 0x0100, Esil: 0x00, }, }, }, } func TestHasDescriptor(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 const fakeTag = 236 got = p.HasDescriptor(fakeTag) want = nil 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 if !bytes.Equal(got, want) { t.Errorf(errNotExpectedOut, got, want) } } func TestProgramInfoLen(t *testing.T) { p := PSIBytes(tstPsi1.Bytes()) got := p.ProgramInfoLen() want := 10 if got != want { t.Errorf(errNotExpectedOut, got, want) } } func TestDescriptors(t *testing.T) { // Try psi with descriptors p := PSIBytes(tstPsi1.Bytes()) got := p.descriptors() want := []byte{ TimeDescTag, 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 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) { 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 if !bytes.Equal(want, got) { t.Errorf(errNotExpectedOut, got, want) } } func TestUpdateDescriptor(t *testing.T) { }