github.com/pion/webrtc/v3@v3.2.24/pkg/media/oggreader/oggreader_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package oggreader 5 6 import ( 7 "bytes" 8 "io" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 // buildOggFile generates a valid oggfile that can 15 // be used for tests 16 func buildOggContainer() []byte { 17 return []byte{ 18 0x4f, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 19 0x00, 0x00, 0x00, 0x00, 0x8e, 0x9b, 0x20, 0xaa, 0x00, 0x00, 20 0x00, 0x00, 0x61, 0xee, 0x61, 0x17, 0x01, 0x13, 0x4f, 0x70, 21 0x75, 0x73, 0x48, 0x65, 0x61, 0x64, 0x01, 0x02, 0x00, 0x0f, 22 0x80, 0xbb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x67, 0x67, 23 0x53, 0x00, 0x00, 0xda, 0x93, 0xc2, 0xd9, 0x00, 0x00, 0x00, 24 0x00, 0x8e, 0x9b, 0x20, 0xaa, 0x02, 0x00, 0x00, 0x00, 0x49, 25 0x97, 0x03, 0x37, 0x01, 0x05, 0x98, 0x36, 0xbe, 0x88, 0x9e, 26 } 27 } 28 29 func TestOggReader_ParseValidHeader(t *testing.T) { 30 reader, header, err := NewWith(bytes.NewReader(buildOggContainer())) 31 assert.NoError(t, err) 32 assert.NotNil(t, reader) 33 assert.NotNil(t, header) 34 35 assert.EqualValues(t, header.ChannelMap, 0) 36 assert.EqualValues(t, header.Channels, 2) 37 assert.EqualValues(t, header.OutputGain, 0) 38 assert.EqualValues(t, header.PreSkip, 0xf00) 39 assert.EqualValues(t, header.SampleRate, 48000) 40 assert.EqualValues(t, header.Version, 1) 41 } 42 43 func TestOggReader_ParseNextPage(t *testing.T) { 44 ogg := bytes.NewReader(buildOggContainer()) 45 46 reader, _, err := NewWith(ogg) 47 assert.NoError(t, err) 48 assert.NotNil(t, reader) 49 50 payload, _, err := reader.ParseNextPage() 51 assert.Equal(t, []byte{0x98, 0x36, 0xbe, 0x88, 0x9e}, payload) 52 assert.NoError(t, err) 53 54 _, _, err = reader.ParseNextPage() 55 assert.Equal(t, err, io.EOF) 56 } 57 58 func TestOggReader_ParseErrors(t *testing.T) { 59 t.Run("Assert that Reader isn't nil", func(t *testing.T) { 60 _, _, err := NewWith(nil) 61 assert.Equal(t, err, errNilStream) 62 }) 63 64 t.Run("Invalid ID Page Header Signature", func(t *testing.T) { 65 ogg := buildOggContainer() 66 ogg[0] = 0 67 68 _, _, err := newWith(bytes.NewReader(ogg), false) 69 assert.Equal(t, err, errBadIDPageSignature) 70 }) 71 72 t.Run("Invalid ID Page Header Type", func(t *testing.T) { 73 ogg := buildOggContainer() 74 ogg[5] = 0 75 76 _, _, err := newWith(bytes.NewReader(ogg), false) 77 assert.Equal(t, err, errBadIDPageType) 78 }) 79 80 t.Run("Invalid ID Page Payload Length", func(t *testing.T) { 81 ogg := buildOggContainer() 82 ogg[27] = 0 83 84 _, _, err := newWith(bytes.NewReader(ogg), false) 85 assert.Equal(t, err, errBadIDPageLength) 86 }) 87 88 t.Run("Invalid ID Page Payload Length", func(t *testing.T) { 89 ogg := buildOggContainer() 90 ogg[35] = 0 91 92 _, _, err := newWith(bytes.NewReader(ogg), false) 93 assert.Equal(t, err, errBadIDPagePayloadSignature) 94 }) 95 96 t.Run("Invalid Page Checksum", func(t *testing.T) { 97 ogg := buildOggContainer() 98 ogg[22] = 0 99 100 _, _, err := NewWith(bytes.NewReader(ogg)) 101 assert.Equal(t, err, errChecksumMismatch) 102 }) 103 }