| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package sctp |
| 5 |
|
| 6 |
import ( |
| 7 |
"reflect" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
) |
| 12 |
|
| 13 |
func TestChunkErrorUnrecognizedChunkType(t *testing.T) { |
| 14 |
const chunkFlags byte = 0x00 |
| 15 |
orgUnrecognizedChunk := []byte{0xc0, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x3} |
| 16 |
rawIn := append([]byte{byte(ctError), chunkFlags, 0x00, 0x10, 0x00, 0x06, 0x00, 0x0c}, orgUnrecognizedChunk...) |
| 17 |
|
| 18 |
t.Run("unmarshal", func(t *testing.T) { |
| 19 |
c := &chunkError{} |
| 20 |
err := c.unmarshal(rawIn) |
| 21 |
assert.Nil(t, err, "unmarshal should succeed") |
| 22 |
assert.Equal(t, ctError, c.typ, "chunk type should be ERROR") |
| 23 |
assert.Equal(t, 1, len(c.errorCauses), "there should be on errorCause") |
| 24 |
|
| 25 |
ec := c.errorCauses[0] |
| 26 |
assert.Equal(t, unrecognizedChunkType, ec.errorCauseCode(), "cause code should be unrecognizedChunkType") |
| 27 |
ecUnrecognizedChunkType, ok := ec.(*errorCauseUnrecognizedChunkType) |
| 28 |
assert.True(t, ok) |
| 29 |
unrecognizedChunk := ecUnrecognizedChunkType.unrecognizedChunk |
| 30 |
assert.True(t, reflect.DeepEqual(unrecognizedChunk, orgUnrecognizedChunk), "should have valid unrecognizedChunk") |
| 31 |
}) |
| 32 |
|
| 33 |
t.Run("marshal", func(t *testing.T) { |
| 34 |
ecUnrecognizedChunkType := &errorCauseUnrecognizedChunkType{ |
| 35 |
unrecognizedChunk: orgUnrecognizedChunk, |
| 36 |
} |
| 37 |
|
| 38 |
ec := &chunkError{ |
| 39 |
errorCauses: []errorCause{ |
| 40 |
errorCause(ecUnrecognizedChunkType), |
| 41 |
}, |
| 42 |
} |
| 43 |
|
| 44 |
raw, err := ec.marshal() |
| 45 |
assert.Nil(t, err, "marshal should succeed") |
| 46 |
assert.True(t, reflect.DeepEqual(raw, rawIn), "unexpected serialization result") |
| 47 |
}) |
| 48 |
|
| 49 |
t.Run("marshal with cause value being nil", func(t *testing.T) { |
| 50 |
expected := []byte{byte(ctError), chunkFlags, 0x00, 0x08, 0x00, 0x06, 0x00, 0x04} |
| 51 |
ecUnrecognizedChunkType := &errorCauseUnrecognizedChunkType{} |
| 52 |
|
| 53 |
ec := &chunkError{ |
| 54 |
errorCauses: []errorCause{ |
| 55 |
errorCause(ecUnrecognizedChunkType), |
| 56 |
}, |
| 57 |
} |
| 58 |
|
| 59 |
raw, err := ec.marshal() |
| 60 |
assert.Nil(t, err, "marshal should succeed") |
| 61 |
assert.True(t, reflect.DeepEqual(raw, expected), "unexpected serialization result") |
| 62 |
}) |
| 63 |
} |