Debian Codegraph @

chunk_heartbeat_test.go

raw
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 // helper to build a raw HEARTBEAT chunk with a single HeartbeatInfo param.
13 func buildHeartbeatChunk(t *testing.T, info []byte) []byte {
14 t.Helper()
15
16 p := &paramHeartbeatInfo{heartbeatInformation: info}
17 pp, err := p.marshal()
18 if !assert.NoError(t, err) {
19 return nil
20 }
21
22 ch := chunkHeader{
23 typ: ctHeartbeat,
24 flags: 0,
25 raw: pp,
26 }
27
28 raw, err := ch.marshal()
29 if !assert.NoError(t, err) {
30 return nil
31 }
32
33 return raw
34 }
35
36 func TestChunkHeartbeat_Unmarshal_Success_EmptyBody(t *testing.T) {
37 // HEARTBEAT with no body (header only) should be accepted, params left empty.
38 ch := chunkHeader{
39 typ: ctHeartbeat,
40 flags: 0,
41 raw: nil,
42 }
43 raw, err := ch.marshal()
44 if !assert.NoError(t, err) {
45 return
46 }
47
48 hb := &chunkHeartbeat{}
49 err = hb.unmarshal(raw)
50 assert.NoError(t, err)
51 assert.Equal(t, ctHeartbeat, hb.typ)
52 assert.Len(t, hb.params, 0)
53 }
54
55 func TestChunkHeartbeat_Unmarshal_Success_WithInfo(t *testing.T) {
56 info := []byte{0xde, 0xad, 0xbe, 0xef}
57 raw := buildHeartbeatChunk(t, info)
58 if raw == nil {
59 return
60 }
61
62 hb := &chunkHeartbeat{}
63 err := hb.unmarshal(raw)
64 if !assert.NoError(t, err) {
65 return
66 }
67
68 assert.Equal(t, ctHeartbeat, hb.typ)
69 if assert.Len(t, hb.params, 1) {
70 p, ok := hb.params[0].(*paramHeartbeatInfo)
71 if assert.True(t, ok, "param should be *paramHeartbeatInfo") {
72 assert.Equal(t, info, p.heartbeatInformation)
73 }
74 }
75 }
76
77 func TestChunkHeartbeat_Unmarshal_Success_WithZeroPaddingAfterTLV(t *testing.T) {
78 info := []byte{0xaa, 0xbb, 0xcc, 0xdd}
79 p := &paramHeartbeatInfo{heartbeatInformation: info}
80 pp, err := p.marshal()
81 if !assert.NoError(t, err) {
82 return
83 }
84
85 // add extra zero padding outside the TLV
86 body := append([]byte{}, pp...)
87 body = append(body, 0x00, 0x00, 0x00, 0x00)
88
89 ch := chunkHeader{
90 typ: ctHeartbeat,
91 flags: 0,
92 raw: body,
93 }
94 raw, err := ch.marshal()
95 if !assert.NoError(t, err) {
96 return
97 }
98
99 hb := &chunkHeartbeat{}
100 err = hb.unmarshal(raw)
101 if !assert.NoError(t, err) {
102 return
103 }
104
105 if assert.Len(t, hb.params, 1) {
106 p2, ok := hb.params[0].(*paramHeartbeatInfo)
107 if assert.True(t, ok) {
108 assert.Equal(t, info, p2.heartbeatInformation)
109 }
110 }
111 }
112
113 func TestChunkHeartbeat_Unmarshal_Failure_TrailingNonZeroBytes(t *testing.T) {
114 info := []byte{0x01, 0x02, 0x03, 0x04}
115 p := &paramHeartbeatInfo{heartbeatInformation: info}
116 pp, err := p.marshal()
117 if !assert.NoError(t, err) {
118 return
119 }
120
121 body := append([]byte{}, pp...)
122 body = append(body, 0x00, 0x00, 0x00, 0x01) // non-zero trailing byte
123
124 ch := chunkHeader{
125 typ: ctHeartbeat,
126 flags: 0,
127 raw: body,
128 }
129 raw, err := ch.marshal()
130 if !assert.NoError(t, err) {
131 return
132 }
133
134 hb := &chunkHeartbeat{}
135 err = hb.unmarshal(raw)
136 assert.Error(t, err)
137 assert.ErrorIs(t, err, ErrHeartbeatExtraNonZero)
138 }
139
140 func TestChunkHeartbeat_Unmarshal_Failure_WrongChunkType(t *testing.T) {
141 ch := chunkHeader{
142 typ: ctInit, // not ctHeartbeat
143 flags: 0,
144 raw: nil,
145 }
146 raw, err := ch.marshal()
147 if !assert.NoError(t, err) {
148 return
149 }
150
151 hb := &chunkHeartbeat{}
152 err = hb.unmarshal(raw)
153 assert.Error(t, err)
154 assert.ErrorIs(t, err, ErrChunkTypeNotHeartbeat)
155 }
156
157 func TestChunkHeartbeat_Unmarshal_Failure_TooShortBody(t *testing.T) {
158 // raw shorter than initOptionalVarHeaderLength should fail.
159 ch := chunkHeader{
160 typ: ctHeartbeat,
161 flags: 0,
162 raw: []byte{0x00, 0x01, 0x02},
163 }
164 raw, err := ch.marshal()
165 if !assert.NoError(t, err) {
166 return
167 }
168
169 hb := &chunkHeartbeat{}
170 err = hb.unmarshal(raw)
171 assert.Error(t, err)
172 assert.ErrorIs(t, err, ErrHeartbeatNotLongEnoughInfo)
173 }
174
175 func TestChunkHeartbeat_Unmarshal_Failure_TruncatedParamBody(t *testing.T) {
176 // build a valid HeartbeatInfo TLV, then truncate it so the advertised
177 // length is larger than the available bytes.
178 info := []byte{0x11, 0x22, 0x33, 0x44}
179 p := &paramHeartbeatInfo{heartbeatInformation: info}
180 pp, err := p.marshal()
181 if !assert.NoError(t, err) {
182 return
183 }
184 if !assert.GreaterOrEqual(t, len(pp), initOptionalVarHeaderLength+1) {
185 return
186 }
187
188 truncated := pp[:len(pp)-1] // chop one byte off -> length mismatch
189
190 ch := chunkHeader{
191 typ: ctHeartbeat,
192 flags: 0,
193 raw: truncated,
194 }
195 raw, err := ch.marshal()
196 if !assert.NoError(t, err) {
197 return
198 }
199
200 hb := &chunkHeartbeat{}
201 err = hb.unmarshal(raw)
202 assert.Error(t, err)
203 assert.ErrorIs(t, err, ErrParseParamTypeFailed)
204 }
205
206 func TestChunkHeartbeat_Unmarshal_Failure_ParamTypeNotHeartbeatInfo(t *testing.T) {
207 // use a different param type (e.g., StateCookie) to trigger ErrHeartbeatParam.
208 p := &paramStateCookie{cookie: []byte{0x01, 0x02, 0x03}}
209 pp, err := p.marshal()
210 if !assert.NoError(t, err) {
211 return
212 }
213
214 ch := chunkHeader{
215 typ: ctHeartbeat,
216 flags: 0,
217 raw: pp,
218 }
219 raw, err := ch.marshal()
220 if !assert.NoError(t, err) {
221 return
222 }
223
224 hb := &chunkHeartbeat{}
225 err = hb.unmarshal(raw)
226 assert.Error(t, err)
227 assert.ErrorIs(t, err, ErrHeartbeatParam)
228 }
229
230 func TestChunkHeartbeat_Marshal_RoundTrip(t *testing.T) {
231 info := []byte{0xca, 0xfe, 0xba, 0xbe}
232 hb := &chunkHeartbeat{
233 params: []param{
234 &paramHeartbeatInfo{
235 heartbeatInformation: info,
236 },
237 },
238 }
239
240 raw, err := hb.Marshal()
241 if !assert.NoError(t, err) {
242 return
243 }
244
245 hb2 := &chunkHeartbeat{}
246 err = hb2.unmarshal(raw)
247 if !assert.NoError(t, err) {
248 return
249 }
250
251 if assert.Len(t, hb2.params, 1) {
252 p, ok := hb2.params[0].(*paramHeartbeatInfo)
253 if assert.True(t, ok) {
254 assert.Equal(t, info, p.heartbeatInformation)
255 }
256 }
257 }
258
259 func TestChunkHeartbeat_Marshal_Failure_NoParams(t *testing.T) {
260 hb := &chunkHeartbeat{
261 params: nil,
262 }
263
264 _, err := hb.Marshal()
265 assert.Error(t, err)
266 assert.ErrorIs(t, err, ErrHeartbeatMarshalNoInfo)
267 }
268
269 func TestChunkHeartbeat_Marshal_Failure_MoreThanOneParam(t *testing.T) {
270 hb := &chunkHeartbeat{
271 params: []param{
272 &paramHeartbeatInfo{heartbeatInformation: []byte{0x01}},
273 &paramHeartbeatInfo{heartbeatInformation: []byte{0x02}},
274 },
275 }
276
277 _, err := hb.Marshal()
278 assert.Error(t, err)
279 assert.ErrorIs(t, err, ErrHeartbeatMarshalNoInfo)
280 }
281
282 func TestChunkHeartbeat_Marshal_Failure_WrongParamType(t *testing.T) {
283 // Not a *paramHeartbeatInfo -> ErrHeartbeatParam.
284 hb := &chunkHeartbeat{
285 params: []param{
286 &paramStateCookie{cookie: []byte{0x01}},
287 },
288 }
289
290 _, err := hb.Marshal()
291 assert.Error(t, err)
292 assert.ErrorIs(t, err, ErrHeartbeatParam)
293 }
294
295 func TestChunkHeartbeat_Check(t *testing.T) {
296 hb := &chunkHeartbeat{}
297 abort, err := hb.check()
298 assert.False(t, abort)
299 assert.NoError(t, err)
300 }