| 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 testParamHeader() []byte { |
| 13 |
return []byte{0x0, 0x1, 0x0, 0x4} |
| 14 |
} |
| 15 |
|
| 16 |
func TestParamHeader_Success(t *testing.T) { |
| 17 |
tt := []struct { |
| 18 |
binary []byte |
| 19 |
parsed *paramHeader |
| 20 |
}{ |
| 21 |
{ |
| 22 |
testParamHeader(), |
| 23 |
¶mHeader{ |
| 24 |
typ: heartbeatInfo, |
| 25 |
len: 4, |
| 26 |
raw: []byte{}, |
| 27 |
}, |
| 28 |
}, |
| 29 |
} |
| 30 |
|
| 31 |
for i, tc := range tt { |
| 32 |
actual := ¶mHeader{} |
| 33 |
err := actual.unmarshal(tc.binary) |
| 34 |
assert.NoErrorf(t, err, "failed to unmarshal #%d", i) |
| 35 |
assert.Equal(t, tc.parsed, actual) |
| 36 |
|
| 37 |
b, err := actual.marshal() |
| 38 |
assert.NoErrorf(t, err, "failed to marshal #%d", i) |
| 39 |
assert.Equal(t, tc.binary, b) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestParamHeaderUnmarshal_Failure(t *testing.T) { |
| 44 |
tt := []struct { |
| 45 |
name string |
| 46 |
binary []byte |
| 47 |
}{ |
| 48 |
{"header too short", testParamHeader()[:2]}, |
| 49 |
// {"wrong param type", []byte{0x0, 0x0, 0x0, 0x4}}, // Not possible to fail parseParamType atm. |
| 50 |
{"reported length below header length", []byte{0x0, 0xd, 0x0, 0x3}}, |
| 51 |
{"wrong reported length", testChunkReconfigParamA()[:4]}, |
| 52 |
} |
| 53 |
|
| 54 |
for i, tc := range tt { |
| 55 |
actual := ¶mHeader{} |
| 56 |
err := actual.unmarshal(tc.binary) |
| 57 |
assert.Errorf(t, err, "expected unmarshal #%d: '%s' to fail.", i, tc.name) |
| 58 |
} |
| 59 |
} |