| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package sctp |
| 5 |
|
| 6 |
import ( |
| 7 |
"sync" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/pion/logging" |
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
) |
| 13 |
|
| 14 |
func newTestStream(t *testing.T) *Stream { |
| 15 |
t.Helper() |
| 16 |
|
| 17 |
s := &Stream{ |
| 18 |
reassemblyQueue: newReassemblyQueue(0, 0), |
| 19 |
log: logging.NewDefaultLoggerFactory().NewLogger("sctp-test"), |
| 20 |
} |
| 21 |
s.readNotifier = sync.NewCond(&s.lock) |
| 22 |
|
| 23 |
return s |
| 24 |
} |
| 25 |
|
| 26 |
func newTestPacketizingStream(t *testing.T, useInterleaving bool, maxPayloadSize uint32) *Stream { |
| 27 |
t.Helper() |
| 28 |
|
| 29 |
logger := logging.NewDefaultLoggerFactory().NewLogger("sctp-test") |
| 30 |
assoc := &Association{ |
| 31 |
useInterleaving: useInterleaving, |
| 32 |
maxPayloadSize: maxPayloadSize, |
| 33 |
log: logger, |
| 34 |
streams: map[uint16]*Stream{}, |
| 35 |
} |
| 36 |
assoc.SetMaxMessageSize(1024) |
| 37 |
|
| 38 |
return assoc.createStream(1, false) |
| 39 |
} |
| 40 |
|
| 41 |
func TestSessionBufferedAmount(t *testing.T) { |
| 42 |
t.Run("bufferedAmount", func(t *testing.T) { |
| 43 |
s := &Stream{ |
| 44 |
log: logging.NewDefaultLoggerFactory().NewLogger("sctp-test"), |
| 45 |
} |
| 46 |
|
| 47 |
assert.Equal(t, uint64(0), s.BufferedAmount()) |
| 48 |
assert.Equal(t, uint64(0), s.BufferedAmountLowThreshold()) |
| 49 |
|
| 50 |
s.bufferedAmount = 8192 |
| 51 |
s.SetBufferedAmountLowThreshold(2048) |
| 52 |
assert.Equal(t, uint64(8192), s.BufferedAmount(), "unexpected bufferedAmount") |
| 53 |
assert.Equal(t, uint64(2048), s.BufferedAmountLowThreshold(), "unexpected threshold") |
| 54 |
}) |
| 55 |
|
| 56 |
t.Run("OnBufferedAmountLow", func(t *testing.T) { |
| 57 |
stream := &Stream{ |
| 58 |
log: logging.NewDefaultLoggerFactory().NewLogger("sctp-test"), |
| 59 |
} |
| 60 |
|
| 61 |
stream.bufferedAmount = 4096 |
| 62 |
stream.SetBufferedAmountLowThreshold(2048) |
| 63 |
|
| 64 |
nCbs := 0 |
| 65 |
|
| 66 |
stream.OnBufferedAmountLow(func() { |
| 67 |
nCbs++ |
| 68 |
}) |
| 69 |
|
| 70 |
// Negative value should be ignored (by design) |
| 71 |
stream.onBufferReleased(-32) // bufferedAmount = 3072 |
| 72 |
assert.Equal(t, uint64(4096), stream.BufferedAmount(), "unexpected bufferedAmount") |
| 73 |
assert.Equal(t, 0, nCbs, "callback count mismatch") |
| 74 |
|
| 75 |
// Above to above, no callback |
| 76 |
stream.onBufferReleased(1024) // bufferedAmount = 3072 |
| 77 |
assert.Equal(t, uint64(3072), stream.BufferedAmount(), "unexpected bufferedAmount") |
| 78 |
assert.Equal(t, 0, nCbs, "callback count mismatch") |
| 79 |
|
| 80 |
// Above to equal, callback should be made |
| 81 |
stream.onBufferReleased(1024) // bufferedAmount = 2048 |
| 82 |
assert.Equal(t, uint64(2048), stream.BufferedAmount(), "unexpected bufferedAmount") |
| 83 |
assert.Equal(t, 1, nCbs, "callback count mismatch") |
| 84 |
|
| 85 |
// Eaual to below, no callback |
| 86 |
stream.onBufferReleased(1024) // bufferedAmount = 1024 |
| 87 |
assert.Equal(t, uint64(1024), stream.BufferedAmount(), "unexpected bufferedAmount") |
| 88 |
assert.Equal(t, 1, nCbs, "callback count mismatch") |
| 89 |
|
| 90 |
// Blow to below, no callback |
| 91 |
stream.onBufferReleased(1024) // bufferedAmount = 0 |
| 92 |
assert.Equal(t, uint64(0), stream.BufferedAmount(), "unexpected bufferedAmount") |
| 93 |
assert.Equal(t, 1, nCbs, "callback count mismatch") |
| 94 |
|
| 95 |
// Capped at 0, no callback |
| 96 |
stream.onBufferReleased(1024) // bufferedAmount = 0 |
| 97 |
assert.Equal(t, uint64(0), stream.BufferedAmount(), "unexpected bufferedAmount") |
| 98 |
assert.Equal(t, 1, nCbs, "callback count mismatch") |
| 99 |
}) |
| 100 |
} |
| 101 |
|
| 102 |
func TestStreamHandleForwardTSNForMID(t *testing.T) { |
| 103 |
t.Run("ordered MID forward makes next complete message readable", func(t *testing.T) { |
| 104 |
stream := newTestStream(t) |
| 105 |
orgPpi := PayloadTypeWebRTCBinary |
| 106 |
|
| 107 |
complete := stream.reassemblyQueue.push(&chunkPayloadData{ |
| 108 |
iData: true, |
| 109 |
messageIdentifier: 0, |
| 110 |
beginningFragment: true, |
| 111 |
tsn: 10, |
| 112 |
streamIdentifier: 0, |
| 113 |
payloadType: orgPpi, |
| 114 |
userData: []byte("DROP"), |
| 115 |
}) |
| 116 |
assert.False(t, complete, "MID 0 should remain incomplete") |
| 117 |
|
| 118 |
complete = stream.reassemblyQueue.push(&chunkPayloadData{ |
| 119 |
iData: true, |
| 120 |
messageIdentifier: 1, |
| 121 |
beginningFragment: true, |
| 122 |
endingFragment: true, |
| 123 |
tsn: 11, |
| 124 |
streamIdentifier: 0, |
| 125 |
payloadType: orgPpi, |
| 126 |
userData: []byte("OK"), |
| 127 |
}) |
| 128 |
assert.True(t, complete, "MID 1 should be complete") |
| 129 |
assert.False(t, stream.reassemblyQueue.isReadable(), "MID 1 should be blocked by missing MID 0") |
| 130 |
|
| 131 |
stream.handleForwardTSNForOrderedMID(0) |
| 132 |
|
| 133 |
assert.True(t, stream.reassemblyQueue.isReadable(), "forwarded MID should expose the next complete message") |
| 134 |
|
| 135 |
buf := make([]byte, 4) |
| 136 |
n, ppi, err := stream.ReadSCTP(buf) |
| 137 |
assert.NoError(t, err) |
| 138 |
assert.Equal(t, 2, n, "should read the kept MID") |
| 139 |
assert.Equal(t, orgPpi, ppi, "should preserve payload type") |
| 140 |
assert.Equal(t, "OK", string(buf[:n]), "unexpected payload") |
| 141 |
assert.Equal(t, uint32(2), stream.reassemblyQueue.nextMID, "next MID should stay advanced after read") |
| 142 |
assert.Equal(t, 0, stream.getNumBytesInReassemblyQueue(), "queue should be empty after read") |
| 143 |
}) |
| 144 |
|
| 145 |
t.Run("unordered MID forward keeps complete data and drops old incomplete message", func(t *testing.T) { |
| 146 |
stream := newTestStream(t) |
| 147 |
orgPpi := PayloadTypeWebRTCBinary |
| 148 |
|
| 149 |
complete := stream.reassemblyQueue.push(&chunkPayloadData{ |
| 150 |
iData: true, |
| 151 |
unordered: true, |
| 152 |
messageIdentifier: 1, |
| 153 |
beginningFragment: true, |
| 154 |
endingFragment: true, |
| 155 |
tsn: 10, |
| 156 |
streamIdentifier: 0, |
| 157 |
payloadType: orgPpi, |
| 158 |
userData: []byte("old"), |
| 159 |
}) |
| 160 |
assert.True(t, complete, "MID 1 should be complete") |
| 161 |
|
| 162 |
complete = stream.reassemblyQueue.push(&chunkPayloadData{ |
| 163 |
iData: true, |
| 164 |
unordered: true, |
| 165 |
messageIdentifier: 2, |
| 166 |
beginningFragment: true, |
| 167 |
tsn: 11, |
| 168 |
streamIdentifier: 0, |
| 169 |
payloadType: orgPpi, |
| 170 |
userData: []byte("drop"), |
| 171 |
}) |
| 172 |
assert.False(t, complete, "MID 2 should remain incomplete") |
| 173 |
|
| 174 |
complete = stream.reassemblyQueue.push(&chunkPayloadData{ |
| 175 |
iData: true, |
| 176 |
unordered: true, |
| 177 |
messageIdentifier: 4, |
| 178 |
beginningFragment: true, |
| 179 |
endingFragment: true, |
| 180 |
tsn: 12, |
| 181 |
streamIdentifier: 0, |
| 182 |
payloadType: orgPpi, |
| 183 |
userData: []byte("keep"), |
| 184 |
}) |
| 185 |
assert.True(t, complete, "MID 4 should be complete") |
| 186 |
|
| 187 |
complete = stream.reassemblyQueue.push(&chunkPayloadData{ |
| 188 |
iData: true, |
| 189 |
unordered: true, |
| 190 |
messageIdentifier: 5, |
| 191 |
beginningFragment: true, |
| 192 |
tsn: 13, |
| 193 |
streamIdentifier: 0, |
| 194 |
payloadType: orgPpi, |
| 195 |
userData: []byte("later"), |
| 196 |
}) |
| 197 |
assert.False(t, complete, "MID 5 should remain incomplete") |
| 198 |
|
| 199 |
stream.handleForwardTSNForUnorderedMID(2) |
| 200 |
|
| 201 |
if assert.Len(t, stream.reassemblyQueue.unorderedMID, 2, "complete MIDs should remain queued") { |
| 202 |
assert.Equal(t, uint32(1), stream.reassemblyQueue.unorderedMID[0].mid, "unexpected first queued MID") |
| 203 |
assert.Equal(t, uint32(4), stream.reassemblyQueue.unorderedMID[1].mid, "unexpected second queued MID") |
| 204 |
} |
| 205 |
if assert.Len(t, stream.reassemblyQueue.unorderedMIDMap, 1, "only newer incomplete MID should remain mapped") { |
| 206 |
_, ok := stream.reassemblyQueue.unorderedMIDMap[5] |
| 207 |
assert.True(t, ok, "MID 5 should remain mapped") |
| 208 |
} |
| 209 |
assert.Equal(t, 12, stream.getNumBytesInReassemblyQueue(), "only incomplete forwarded MID bytes should be removed") |
| 210 |
assert.True(t, stream.reassemblyQueue.isReadable(), "complete MID should stay readable") |
| 211 |
|
| 212 |
buf := make([]byte, 8) |
| 213 |
n, ppi, err := stream.ReadSCTP(buf) |
| 214 |
assert.NoError(t, err) |
| 215 |
assert.Equal(t, 3, n, "should read the old complete MID") |
| 216 |
assert.Equal(t, orgPpi, ppi, "should preserve payload type") |
| 217 |
assert.Equal(t, "old", string(buf[:n]), "unexpected payload") |
| 218 |
assert.Equal(t, 9, stream.getNumBytesInReassemblyQueue(), "newer complete and incomplete MID bytes should remain") |
| 219 |
assert.True(t, stream.reassemblyQueue.isReadable(), "newer complete MID should stay readable") |
| 220 |
|
| 221 |
n, ppi, err = stream.ReadSCTP(buf) |
| 222 |
assert.NoError(t, err) |
| 223 |
assert.Equal(t, 4, n, "should read the kept MID") |
| 224 |
assert.Equal(t, orgPpi, ppi, "should preserve payload type") |
| 225 |
assert.Equal(t, "keep", string(buf[:n]), "unexpected payload") |
| 226 |
assert.Equal(t, 5, stream.getNumBytesInReassemblyQueue(), "only the remaining incomplete MID bytes should remain") |
| 227 |
assert.False(t, stream.reassemblyQueue.isReadable(), "only an incomplete MID should remain") |
| 228 |
}) |
| 229 |
} |
| 230 |
|
| 231 |
func TestStreamPacketizeInterleavingMIDAllocation(t *testing.T) { |
| 232 |
stream := newTestPacketizingStream(t, true, 3) |
| 233 |
stream.unordered = true |
| 234 |
|
| 235 |
unorderedChunks, unordered := stream.packetize([]byte("abcdef"), PayloadTypeWebRTCBinary) |
| 236 |
assert.True(t, unordered) |
| 237 |
if assert.Len(t, unorderedChunks, 2) { |
| 238 |
assert.True(t, unorderedChunks[0].beginningFragment) |
| 239 |
assert.False(t, unorderedChunks[0].endingFragment) |
| 240 |
assert.False(t, unorderedChunks[1].beginningFragment) |
| 241 |
assert.True(t, unorderedChunks[1].endingFragment) |
| 242 |
|
| 243 |
for i, chunk := range unorderedChunks { |
| 244 |
assert.True(t, chunk.iData) |
| 245 |
assert.True(t, chunk.unordered) |
| 246 |
assert.Equal(t, uint32(0), chunk.messageIdentifier) |
| 247 |
assert.Equal(t, uint32(i), chunk.fragmentSequenceNumber) |
| 248 |
assert.Equal(t, uint16(0), chunk.streamSequenceNumber) |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
secondUnorderedChunks, unordered := stream.packetize([]byte("xy"), PayloadTypeWebRTCBinary) |
| 253 |
assert.True(t, unordered) |
| 254 |
if assert.Len(t, secondUnorderedChunks, 1) { |
| 255 |
assert.True(t, secondUnorderedChunks[0].iData) |
| 256 |
assert.True(t, secondUnorderedChunks[0].unordered) |
| 257 |
assert.Equal(t, uint32(1), secondUnorderedChunks[0].messageIdentifier) |
| 258 |
assert.Equal(t, uint16(1), secondUnorderedChunks[0].streamSequenceNumber) |
| 259 |
} |
| 260 |
|
| 261 |
orderedChunks, unordered := stream.packetize([]byte("hi"), PayloadTypeWebRTCDCEP) |
| 262 |
assert.False(t, unordered) |
| 263 |
if assert.Len(t, orderedChunks, 1) { |
| 264 |
assert.True(t, orderedChunks[0].iData) |
| 265 |
assert.False(t, orderedChunks[0].unordered) |
| 266 |
assert.Equal(t, uint32(0), orderedChunks[0].messageIdentifier) |
| 267 |
assert.Equal(t, uint16(0), orderedChunks[0].streamSequenceNumber) |
| 268 |
} |
| 269 |
|
| 270 |
secondOrderedChunks, unordered := stream.packetize([]byte("ok"), PayloadTypeWebRTCDCEP) |
| 271 |
assert.False(t, unordered) |
| 272 |
if assert.Len(t, secondOrderedChunks, 1) { |
| 273 |
assert.True(t, secondOrderedChunks[0].iData) |
| 274 |
assert.False(t, secondOrderedChunks[0].unordered) |
| 275 |
assert.Equal(t, uint32(1), secondOrderedChunks[0].messageIdentifier) |
| 276 |
assert.Equal(t, uint16(1), secondOrderedChunks[0].streamSequenceNumber) |
| 277 |
} |
| 278 |
|
| 279 |
assert.Equal(t, uint16(0), stream.sequenceNumber, "interleaving should not advance SSN") |
| 280 |
assert.Equal(t, uint32(2), stream.nextOrderedMID, "ordered MID counter mismatch") |
| 281 |
assert.Equal(t, uint32(2), stream.nextUnorderedMID, "unordered MID counter mismatch") |
| 282 |
} |
| 283 |
|
| 284 |
func TestStreamWriteSCTPRollsBackInterleavingMIDsOnSendError(t *testing.T) { |
| 285 |
testCases := []struct { |
| 286 |
name string |
| 287 |
ppi PayloadProtocolIdentifier |
| 288 |
unordered bool |
| 289 |
}{ |
| 290 |
{ |
| 291 |
name: "ordered", |
| 292 |
ppi: PayloadTypeWebRTCDCEP, |
| 293 |
unordered: false, |
| 294 |
}, |
| 295 |
{ |
| 296 |
name: "unordered", |
| 297 |
ppi: PayloadTypeWebRTCBinary, |
| 298 |
unordered: true, |
| 299 |
}, |
| 300 |
} |
| 301 |
|
| 302 |
for _, tc := range testCases { |
| 303 |
t.Run(tc.name, func(t *testing.T) { |
| 304 |
s := newTestPacketizingStream(t, true, 1200) |
| 305 |
s.unordered = tc.unordered |
| 306 |
|
| 307 |
n, err := s.WriteSCTP([]byte("test"), tc.ppi) |
| 308 |
assert.ErrorIs(t, err, ErrPayloadDataStateNotExist) |
| 309 |
assert.Equal(t, 0, n, "failed write should not report sent bytes") |
| 310 |
assert.Equal(t, uint16(0), s.sequenceNumber) |
| 311 |
assert.Equal(t, uint32(0), s.nextOrderedMID) |
| 312 |
assert.Equal(t, uint32(0), s.nextUnorderedMID) |
| 313 |
assert.Equal(t, uint64(0), s.BufferedAmount()) |
| 314 |
}) |
| 315 |
} |
| 316 |
} |