| 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 testChunkIForwardTSN() []byte { |
| 13 |
return []byte{0xc2, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x3} |
| 14 |
} |
| 15 |
|
| 16 |
func makeDistinctIForwardTSNStreams(count int) []chunkIForwardTSNStream { |
| 17 |
streams := make([]chunkIForwardTSNStream, 0, count) |
| 18 |
for i := range count { |
| 19 |
streams = append(streams, chunkIForwardTSNStream{ |
| 20 |
identifier: uint16(i), //nolint:gosec // count is bounded by maxIForwardTSNStreams in tests. |
| 21 |
messageIdentifier: uint32(i), //nolint:gosec // count is bounded by maxIForwardTSNStreams in tests. |
| 22 |
}) |
| 23 |
} |
| 24 |
|
| 25 |
return streams |
| 26 |
} |
| 27 |
|
| 28 |
func TestChunkIForwardTSN_Success(t *testing.T) { |
| 29 |
tt := []struct { |
| 30 |
binary []byte |
| 31 |
}{ |
| 32 |
{testChunkIForwardTSN()}, |
| 33 |
{[]byte{0xc2, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x3, 0x0, 0x4, 0x0, 0x1, 0x0, 0x0, 0x0, 0x5}}, |
| 34 |
{[]byte{ |
| 35 |
0xc2, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x3, |
| 36 |
0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5, |
| 37 |
0x0, 0x6, 0x0, 0x1, 0x0, 0x0, 0x0, 0x7, |
| 38 |
0x0, 0x8, 0x0, 0x1, 0x0, 0x0, 0x0, 0x9, |
| 39 |
0x0, 0xa, 0x0, 0x1, 0x0, 0x0, 0x0, 0xb, |
| 40 |
0x0, 0xc, 0x0, 0x1, 0x0, 0x0, 0x0, 0xd, |
| 41 |
0x0, 0xe, 0x0, 0x1, 0x0, 0x0, 0x0, 0xf, |
| 42 |
0x0, 0x10, 0x0, 0x1, 0x0, 0x0, 0x0, 0x11, |
| 43 |
}}, |
| 44 |
} |
| 45 |
|
| 46 |
for i, tc := range tt { |
| 47 |
actual := &chunkIForwardTSN{} |
| 48 |
err := actual.unmarshal(tc.binary) |
| 49 |
assert.NoErrorf(t, err, "failed to unmarshal #%d", i) |
| 50 |
|
| 51 |
b, err := actual.marshal() |
| 52 |
assert.NoError(t, err) |
| 53 |
assert.Equalf(t, tc.binary, b, "test %d not equal", i) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestChunkIForwardTSNUnmarshal_NormalizesDuplicateStreams(t *testing.T) { |
| 58 |
actual := &chunkIForwardTSN{} |
| 59 |
err := actual.unmarshal([]byte{ |
| 60 |
0xc2, 0x0, 0x0, 0x18, 0x0, 0x0, 0x0, 0x3, |
| 61 |
0x0, 0x4, 0x0, 0x1, 0x0, 0x0, 0x0, 0x2, |
| 62 |
0x0, 0x4, 0x0, 0x1, 0x0, 0x0, 0x0, 0x5, |
| 63 |
}) |
| 64 |
|
| 65 |
assert.NoError(t, err) |
| 66 |
assert.Equal(t, []chunkIForwardTSNStream{{ |
| 67 |
identifier: 4, |
| 68 |
unordered: true, |
| 69 |
messageIdentifier: 5, |
| 70 |
}}, actual.streams) |
| 71 |
} |
| 72 |
|
| 73 |
func TestChunkIForwardTSNUnmarshal_Failure(t *testing.T) { |
| 74 |
tt := []struct { |
| 75 |
name string |
| 76 |
binary []byte |
| 77 |
}{ |
| 78 |
{"chunk header to short", []byte{0xc2}}, |
| 79 |
{"missing New Cumulative TSN", []byte{0xc2, 0x0, 0x0, 0x4}}, |
| 80 |
{"missing stream info", []byte{0xc2, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x3, 0x0, 0x4, 0x0, 0x1}}, |
| 81 |
} |
| 82 |
|
| 83 |
for i, tc := range tt { |
| 84 |
actual := &chunkIForwardTSN{} |
| 85 |
err := actual.unmarshal(tc.binary) |
| 86 |
assert.Errorf(t, err, "expected unmarshal #%d: '%s' to fail.", i, tc.name) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func TestChunkIForwardTSNUnmarshal_FailureDoesNotKeepPartialStreams(t *testing.T) { |
| 91 |
actual := &chunkIForwardTSN{ |
| 92 |
streams: []chunkIForwardTSNStream{{ |
| 93 |
identifier: 1, |
| 94 |
messageIdentifier: 2, |
| 95 |
}}, |
| 96 |
} |
| 97 |
err := actual.unmarshal([]byte{ |
| 98 |
0xc2, 0x0, 0x0, 0x12, 0x0, 0x0, 0x0, 0x3, |
| 99 |
0x0, 0x4, 0x0, 0x1, 0x0, 0x0, 0x0, 0x5, |
| 100 |
0x0, 0x6, |
| 101 |
}) |
| 102 |
|
| 103 |
assert.Error(t, err) |
| 104 |
assert.ErrorIs(t, err, ErrMarshalStreamFailed) |
| 105 |
assert.ErrorIs(t, err, errIForwardTSNChunkInvalidLength) |
| 106 |
assert.Empty(t, actual.streams) |
| 107 |
} |
| 108 |
|
| 109 |
func TestChunkIForwardTSNCheck(t *testing.T) { |
| 110 |
tt := []struct { |
| 111 |
name string |
| 112 |
chunk *chunkIForwardTSN |
| 113 |
wantAbort bool |
| 114 |
wantErr error |
| 115 |
want []chunkIForwardTSNStream |
| 116 |
}{ |
| 117 |
{ |
| 118 |
name: "valid with ordered and unordered forwards for same stream", |
| 119 |
chunk: &chunkIForwardTSN{ |
| 120 |
newCumulativeTSN: 1, |
| 121 |
streams: []chunkIForwardTSNStream{ |
| 122 |
{identifier: 1, unordered: false, messageIdentifier: 2}, |
| 123 |
{identifier: 1, unordered: true, messageIdentifier: 3}, |
| 124 |
}, |
| 125 |
}, |
| 126 |
}, |
| 127 |
{ |
| 128 |
name: "zero new cumulative tsn", |
| 129 |
chunk: &chunkIForwardTSN{ |
| 130 |
newCumulativeTSN: 0, |
| 131 |
streams: []chunkIForwardTSNStream{ |
| 132 |
{identifier: 1, messageIdentifier: 2}, |
| 133 |
}, |
| 134 |
}, |
| 135 |
}, |
| 136 |
{ |
| 137 |
name: "too many streams", |
| 138 |
chunk: &chunkIForwardTSN{ |
| 139 |
newCumulativeTSN: 1, |
| 140 |
streams: makeDistinctIForwardTSNStreams(maxIForwardTSNStreams + 1), |
| 141 |
}, |
| 142 |
wantAbort: true, |
| 143 |
wantErr: errIForwardTSNTooManyStreams, |
| 144 |
}, |
| 145 |
{ |
| 146 |
name: "duplicate ordered stream forward keeps largest mid", |
| 147 |
chunk: &chunkIForwardTSN{ |
| 148 |
newCumulativeTSN: 1, |
| 149 |
streams: []chunkIForwardTSNStream{ |
| 150 |
{identifier: 1, unordered: false, messageIdentifier: 2}, |
| 151 |
{identifier: 1, unordered: false, messageIdentifier: 3}, |
| 152 |
}, |
| 153 |
}, |
| 154 |
want: []chunkIForwardTSNStream{ |
| 155 |
{identifier: 1, unordered: false, messageIdentifier: 3}, |
| 156 |
}, |
| 157 |
}, |
| 158 |
{ |
| 159 |
name: "duplicate unordered stream forward keeps largest mid", |
| 160 |
chunk: &chunkIForwardTSN{ |
| 161 |
newCumulativeTSN: 1, |
| 162 |
streams: []chunkIForwardTSNStream{ |
| 163 |
{identifier: 1, unordered: true, messageIdentifier: 2}, |
| 164 |
{identifier: 1, unordered: true, messageIdentifier: 3}, |
| 165 |
}, |
| 166 |
}, |
| 167 |
want: []chunkIForwardTSNStream{ |
| 168 |
{identifier: 1, unordered: true, messageIdentifier: 3}, |
| 169 |
}, |
| 170 |
}, |
| 171 |
} |
| 172 |
|
| 173 |
for _, tc := range tt { |
| 174 |
t.Run(tc.name, func(t *testing.T) { |
| 175 |
abort, err := tc.chunk.check() |
| 176 |
assert.Equal(t, tc.wantAbort, abort) |
| 177 |
if tc.wantErr == nil { |
| 178 |
assert.NoError(t, err) |
| 179 |
if tc.want != nil { |
| 180 |
assert.Equal(t, tc.want, tc.chunk.streams) |
| 181 |
} |
| 182 |
|
| 183 |
return |
| 184 |
} |
| 185 |
|
| 186 |
assert.ErrorIs(t, err, tc.wantErr) |
| 187 |
}) |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
func TestChunkIForwardTSNMarshal_NormalizesDuplicateStreams(t *testing.T) { |
| 192 |
b, err := (&chunkIForwardTSN{ |
| 193 |
newCumulativeTSN: 1, |
| 194 |
streams: []chunkIForwardTSNStream{ |
| 195 |
{identifier: 1, messageIdentifier: 2}, |
| 196 |
{identifier: 1, messageIdentifier: 3}, |
| 197 |
}, |
| 198 |
}).marshal() |
| 199 |
|
| 200 |
assert.NoError(t, err) |
| 201 |
assert.Equal(t, []byte{ |
| 202 |
0xc2, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x1, |
| 203 |
0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, |
| 204 |
}, b) |
| 205 |
} |
| 206 |
|
| 207 |
func TestChunkIForwardTSNMarshal_ZeroCumulativeTSN(t *testing.T) { |
| 208 |
_, err := (&chunkIForwardTSN{ |
| 209 |
newCumulativeTSN: 0, |
| 210 |
streams: []chunkIForwardTSNStream{{ |
| 211 |
identifier: 1, |
| 212 |
messageIdentifier: 2, |
| 213 |
}}, |
| 214 |
}).marshal() |
| 215 |
|
| 216 |
assert.NoError(t, err) |
| 217 |
} |