-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathchunk_heartbeat_ack_test.go
More file actions
230 lines (191 loc) · 5.18 KB
/
Copy pathchunk_heartbeat_ack_test.go
File metadata and controls
230 lines (191 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package sctp
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestChunkHeartbeatAck_UnmarshalMarshal_Success(t *testing.T) {
tt := []struct {
name string
info []byte
}{
{"empty-info", []byte{}},
{"aligned-4", []byte{0x01, 0x02, 0x03, 0x04}},
{"non-aligned-5", []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee}},
}
for _, tc := range tt {
p := ¶mHeartbeatInfo{heartbeatInformation: tc.info}
pp, err := p.marshal()
if !assert.NoErrorf(t, err, "marshal paramHeartbeatInfo for %s", tc.name) {
continue
}
ch := chunkHeader{
typ: ctHeartbeatAck,
flags: 0,
raw: pp,
}
encoded, err := ch.marshal()
if !assert.NoErrorf(t, err, "marshal chunkHeader for %s", tc.name) {
continue
}
hbAck := &chunkHeartbeatAck{}
err = hbAck.unmarshal(encoded)
if !assert.NoErrorf(t, err, "unmarshal HeartbeatAck for %s", tc.name) {
continue
}
assert.Equalf(t, ctHeartbeatAck, hbAck.typ, "chunk type for %s", tc.name)
if assert.Lenf(t, hbAck.params, 1, "params length for %s", tc.name) {
got, ok := hbAck.params[0].(*paramHeartbeatInfo)
if assert.Truef(t, ok, "param type for %s", tc.name) {
assert.Equalf(t, tc.info, got.heartbeatInformation, "heartbeat info for %s", tc.name)
}
}
roundTrip, err := hbAck.marshal()
if !assert.NoErrorf(t, err, "marshal HeartbeatAck (round-trip) for %s", tc.name) {
continue
}
assert.Equalf(t, encoded, roundTrip, "round-trip bytes for %s", tc.name)
}
}
func TestChunkHeartbeatAck_Unmarshal_EmptyBody(t *testing.T) {
ch := chunkHeader{
typ: ctHeartbeatAck,
flags: 0,
raw: nil,
}
raw, err := ch.marshal()
if !assert.NoError(t, err) {
return
}
hbAck := &chunkHeartbeatAck{}
err = hbAck.unmarshal(raw)
if !assert.NoError(t, err) {
return
}
assert.Nil(t, hbAck.params)
}
func TestChunkHeartbeatAck_Unmarshal_Failure_WrongChunkType(t *testing.T) {
p := ¶mHeartbeatInfo{heartbeatInformation: []byte{0x01, 0x02, 0x03, 0x04}}
pp, err := p.marshal()
if !assert.NoError(t, err) {
return
}
ch := chunkHeader{
typ: ctHeartbeat, // wrong type on purpose
flags: 0,
raw: pp,
}
raw, err := ch.marshal()
if !assert.NoError(t, err) {
return
}
hbAck := &chunkHeartbeatAck{}
err = hbAck.unmarshal(raw)
assert.Error(t, err)
assert.ErrorIs(t, err, ErrChunkTypeNotHeartbeatAck)
}
func TestChunkHeartbeatAck_Unmarshal_Failure_BodyTooShort(t *testing.T) {
// less than initOptionalVarHeaderLength bytes in the body.
body := []byte{0xaa, 0xbb, 0xcc}
ch := chunkHeader{
typ: ctHeartbeatAck,
flags: 0,
raw: body,
}
raw, err := ch.marshal()
if !assert.NoError(t, err) {
return
}
hbAck := &chunkHeartbeatAck{}
err = hbAck.unmarshal(raw)
assert.Error(t, err)
assert.ErrorIs(t, err, ErrHeartbeatAckParams)
}
func TestChunkHeartbeatAck_Unmarshal_Failure_TruncatedParamBody(t *testing.T) {
// build a valid HeartbeatInfo TLV, then truncate it so the advertised
// length is larger than the available bytes.
info := []byte{0x11, 0x22, 0x33, 0x44}
p := ¶mHeartbeatInfo{heartbeatInformation: info}
pp, err := p.marshal()
if !assert.NoError(t, err) {
return
}
if !assert.GreaterOrEqual(t, len(pp), initOptionalVarHeaderLength+1) {
return
}
truncated := pp[:len(pp)-1]
ch := chunkHeader{
typ: ctHeartbeatAck,
flags: 0,
raw: truncated,
}
raw, err := ch.marshal()
if !assert.NoError(t, err) {
return
}
hbAck := &chunkHeartbeatAck{}
err = hbAck.unmarshal(raw)
assert.Error(t, err)
assert.ErrorIs(t, err, ErrHeartbeatAckParams)
}
func TestChunkHeartbeatAck_Unmarshal_Failure_WrongParamType(t *testing.T) {
// construct a TLV with a param type that is *not* heartbeatInfo but is otherwise valid.
plen := uint16(initOptionalVarHeaderLength)
body := []byte{
0x00, 0x02, // some param type != heartbeatInfo (which is 1 per RFC)
byte(plen >> 8),
byte(plen & 0xff),
}
ch := chunkHeader{
typ: ctHeartbeatAck,
flags: 0,
raw: body,
}
raw, err := ch.marshal()
if !assert.NoError(t, err) {
return
}
hbAck := &chunkHeartbeatAck{}
err = hbAck.unmarshal(raw)
assert.Error(t, err)
assert.ErrorIs(t, err, ErrHeartbeatAckNotHeartbeatInfo)
}
func TestChunkHeartbeatAck_Unmarshal_Failure_TrailingNonZero(t *testing.T) {
info := []byte{0x01, 0x02, 0x03, 0x04}
p := ¶mHeartbeatInfo{heartbeatInformation: info}
pp, err := p.marshal()
if !assert.NoError(t, err) {
return
}
// append a non-zero byte after the single parameter.
body := append(append([]byte{}, pp...), 0x01)
ch := chunkHeader{
typ: ctHeartbeatAck,
flags: 0,
raw: body,
}
raw, err := ch.marshal()
if !assert.NoError(t, err) {
return
}
hbAck := &chunkHeartbeatAck{}
err = hbAck.unmarshal(raw)
assert.Error(t, err)
assert.ErrorIs(t, err, ErrHeartbeatExtraNonZero)
}
func TestChunkHeartbeatAck_Marshal_Failure_ParamCount(t *testing.T) {
// no params
hbAck := &chunkHeartbeatAck{}
_, err := hbAck.marshal()
assert.Error(t, err)
assert.ErrorIs(t, err, ErrHeartbeatAckParams)
// too many params
p := ¶mHeartbeatInfo{}
hbAck = &chunkHeartbeatAck{
params: []param{p, p},
}
_, err = hbAck.marshal()
assert.Error(t, err)
assert.ErrorIs(t, err, ErrHeartbeatAckParams)
}