| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package sctp |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
) |
| 11 |
|
| 12 |
func TestAbortChunk(t *testing.T) { |
| 13 |
t.Run("One error cause", func(t *testing.T) { |
| 14 |
abort1 := &chunkAbort{ |
| 15 |
errorCauses: []errorCause{&errorCauseProtocolViolation{ |
| 16 |
errorCauseHeader: errorCauseHeader{code: protocolViolation}, |
| 17 |
}}, |
| 18 |
} |
| 19 |
bytes, err := abort1.marshal() |
| 20 |
assert.NoError(t, err, "should succeed") |
| 21 |
|
| 22 |
abort2 := &chunkAbort{} |
| 23 |
err = abort2.unmarshal(bytes) |
| 24 |
assert.NoError(t, err, "should succeed") |
| 25 |
assert.Equal(t, 1, len(abort2.errorCauses), "should have only one cause") |
| 26 |
assert.Equal(t, |
| 27 |
abort1.errorCauses[0].errorCauseCode(), |
| 28 |
abort2.errorCauses[0].errorCauseCode(), |
| 29 |
"errorCause code should match") |
| 30 |
}) |
| 31 |
|
| 32 |
t.Run("Many error causes", func(t *testing.T) { |
| 33 |
abort1 := &chunkAbort{ |
| 34 |
errorCauses: []errorCause{ |
| 35 |
&errorCauseProtocolViolation{ |
| 36 |
errorCauseHeader: errorCauseHeader{code: invalidMandatoryParameter}, |
| 37 |
}, |
| 38 |
&errorCauseProtocolViolation{ |
| 39 |
errorCauseHeader: errorCauseHeader{code: unrecognizedChunkType}, |
| 40 |
}, |
| 41 |
&errorCauseProtocolViolation{ |
| 42 |
errorCauseHeader: errorCauseHeader{code: protocolViolation}, |
| 43 |
}, |
| 44 |
}, |
| 45 |
} |
| 46 |
bytes, err := abort1.marshal() |
| 47 |
assert.NoError(t, err, "should succeed") |
| 48 |
|
| 49 |
abort2 := &chunkAbort{} |
| 50 |
err = abort2.unmarshal(bytes) |
| 51 |
assert.NoError(t, err, "should succeed") |
| 52 |
assert.Equal(t, 3, len(abort2.errorCauses), "should have only one cause") |
| 53 |
for i, errorCause := range abort1.errorCauses { |
| 54 |
assert.Equal(t, |
| 55 |
errorCause.errorCauseCode(), |
| 56 |
abort2.errorCauses[i].errorCauseCode(), |
| 57 |
"errorCause code should match") |
| 58 |
} |
| 59 |
}) |
| 60 |
|
| 61 |
t.Run("Unrecognized error cause code is accepted as opaque TLV", func(t *testing.T) { |
| 62 |
const unknownCode errorCauseCode = 0xFFFF |
| 63 |
|
| 64 |
// Build a raw ABORT with one unrecognized 4-byte cause (code=0xFFFF, len=4, no value). |
| 65 |
// chunkHeader: type(1) flags(1) length(2) = 4 bytes; cause TLV: 4 bytes → total 8. |
| 66 |
raw := []byte{ |
| 67 |
byte(ctAbort), 0x00, 0x00, 0x08, // chunk header: type, flags, length=8 |
| 68 |
0xFF, 0xFF, 0x00, 0x04, // cause: code=0xFFFF, length=4 (header only) |
| 69 |
} |
| 70 |
|
| 71 |
abort := &chunkAbort{} |
| 72 |
err := abort.unmarshal(raw) |
| 73 |
assert.NoError(t, err, "ABORT with unrecognized cause code should not fail") |
| 74 |
assert.Equal(t, 1, len(abort.errorCauses), "should have one error cause") |
| 75 |
assert.Equal(t, unknownCode, abort.errorCauses[0].errorCauseCode(), "cause code should be preserved") |
| 76 |
}) |
| 77 |
|
| 78 |
t.Run("Unrecognized error cause code mixed with known causes", func(t *testing.T) { |
| 79 |
const unknownCode errorCauseCode = 0x00FF |
| 80 |
|
| 81 |
// ABORT containing: unknown cause first, followed by a known cause (protocolViolation=13). |
| 82 |
abort1 := &chunkAbort{ |
| 83 |
errorCauses: []errorCause{ |
| 84 |
// Unknown cause represented as an opaque errorCauseHeader. |
| 85 |
&errorCauseHeader{code: unknownCode}, |
| 86 |
&errorCauseProtocolViolation{ |
| 87 |
errorCauseHeader: errorCauseHeader{code: protocolViolation}, |
| 88 |
}, |
| 89 |
}, |
| 90 |
} |
| 91 |
bytes, err := abort1.marshal() |
| 92 |
assert.NoError(t, err, "marshal should succeed") |
| 93 |
|
| 94 |
abort2 := &chunkAbort{} |
| 95 |
err = abort2.unmarshal(bytes) |
| 96 |
assert.NoError(t, err, "unmarshal with unknown cause code should not fail") |
| 97 |
assert.Equal(t, 2, len(abort2.errorCauses), "should have two error causes") |
| 98 |
assert.Equal(t, unknownCode, abort2.errorCauses[0].errorCauseCode(), "unknown cause code should be preserved") |
| 99 |
assert.Equal(t, protocolViolation, abort2.errorCauses[1].errorCauseCode(), "second cause code should match") |
| 100 |
}) |
| 101 |
} |