github.com/pion/webrtc/v4@v4.0.1/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  	assert.Equal(t, int64(47), reader.bytesReadSuccesfully)
    50  
    51  	payload, _, err := reader.ParseNextPage()
    52  	assert.Equal(t, []byte{0x98, 0x36, 0xbe, 0x88, 0x9e}, payload)
    53  	assert.NoError(t, err)
    54  	assert.Equal(t, int64(80), reader.bytesReadSuccesfully)
    55  
    56  	_, _, err = reader.ParseNextPage()
    57  	assert.Equal(t, err, io.EOF)
    58  }
    59  
    60  func TestOggReader_ParseErrors(t *testing.T) {
    61  	t.Run("Assert that Reader isn't nil", func(t *testing.T) {
    62  		_, _, err := NewWith(nil)
    63  		assert.Equal(t, err, errNilStream)
    64  	})
    65  
    66  	t.Run("Invalid ID Page Header Signature", func(t *testing.T) {
    67  		ogg := buildOggContainer()
    68  		ogg[0] = 0
    69  
    70  		_, _, err := newWith(bytes.NewReader(ogg), false)
    71  		assert.Equal(t, err, errBadIDPageSignature)
    72  	})
    73  
    74  	t.Run("Invalid ID Page Header Type", func(t *testing.T) {
    75  		ogg := buildOggContainer()
    76  		ogg[5] = 0
    77  
    78  		_, _, err := newWith(bytes.NewReader(ogg), false)
    79  		assert.Equal(t, err, errBadIDPageType)
    80  	})
    81  
    82  	t.Run("Invalid ID Page Payload Length", func(t *testing.T) {
    83  		ogg := buildOggContainer()
    84  		ogg[27] = 0
    85  
    86  		_, _, err := newWith(bytes.NewReader(ogg), false)
    87  		assert.Equal(t, err, errBadIDPageLength)
    88  	})
    89  
    90  	t.Run("Invalid ID Page Payload Length", func(t *testing.T) {
    91  		ogg := buildOggContainer()
    92  		ogg[35] = 0
    93  
    94  		_, _, err := newWith(bytes.NewReader(ogg), false)
    95  		assert.Equal(t, err, errBadIDPagePayloadSignature)
    96  	})
    97  
    98  	t.Run("Invalid Page Checksum", func(t *testing.T) {
    99  		ogg := buildOggContainer()
   100  		ogg[22] = 0
   101  
   102  		_, _, err := NewWith(bytes.NewReader(ogg))
   103  		assert.Equal(t, err, errChecksumMismatch)
   104  	})
   105  }