Debian Codegraph @

chunk_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 func TestInitChunk(t *testing.T) {
13 pkt := &packet{}
14 rawPkt := []byte{
15 0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0x81, 0x46, 0x9d, 0xfc, 0x01, 0x00, 0x00, 0x56, 0x55, 0xb9,
16 0x64, 0xa5, 0x00, 0x02, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0xe8, 0x6d, 0x10, 0x30, 0xc0, 0x00, 0x00, 0x04,
17 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f, 0xc1, 0x80, 0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0x9f, 0xeb,
18 0xbb, 0x5c, 0x50, 0xc9, 0xbf, 0x75, 0x9c, 0xb1, 0x2c, 0x57, 0x4f, 0xa4, 0x5a, 0x51, 0xba, 0x60, 0x17, 0x78,
19 0x27, 0x94, 0x5c, 0x31, 0xe6, 0x5d, 0x5b, 0x09, 0x47, 0xe2, 0x22, 0x06, 0x80, 0x04, 0x00, 0x06, 0x00, 0x01,
20 0x00, 0x00, 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00,
21 }
22 err := pkt.unmarshal(true, rawPkt)
23 assert.NoError(t, err)
24
25 initChunk, ok := pkt.chunks[0].(*chunkInit)
26 assert.True(t, ok, "Failed to cast Chunk -> Init")
27
28 assert.NoError(t, err, "Unmarshal init Chunk failed")
29 assert.Equalf(t, initChunk.initiateTag, uint32(1438213285),
30 "Unmarshal passed for SCTP packet, but got incorrect initiate tag exp: %d act: %d",
31 1438213285, initChunk.initiateTag)
32 assert.Equalf(t, initChunk.advertisedReceiverWindowCredit, uint32(131072),
33 "Unmarshal passed for SCTP packet, but got incorrect advertisedReceiverWindowCredit exp: %d act: %d",
34 131072, initChunk.advertisedReceiverWindowCredit)
35 assert.Equalf(t, initChunk.numOutboundStreams, uint16(1024),
36 "Unmarshal passed for SCTP packet, but got incorrect numOutboundStreams tag exp: %d act: %d",
37 1024, initChunk.numOutboundStreams)
38 assert.Equalf(t, initChunk.numInboundStreams, uint16(2048),
39 "Unmarshal passed for SCTP packet, but got incorrect numInboundStreams exp: %d act: %d",
40 2048, initChunk.numInboundStreams)
41 assert.Equalf(t, initChunk.initialTSN, uint32(3899461680),
42 "Unmarshal passed for SCTP packet, but got incorrect initialTSN exp: %d act: %d",
43 uint32(3899461680), initChunk.initialTSN)
44 }
45
46 func TestInitAck(t *testing.T) {
47 pkt := &packet{}
48 rawPkt := []byte{
49 0x13, 0x88, 0x13, 0x88, 0xce, 0x15, 0x79, 0xa2, 0x96, 0x19, 0xe8, 0xb2, 0x02, 0x00, 0x00, 0x1c, 0xeb, 0x81,
50 0x4e, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0x50, 0xdf, 0x90, 0xd9, 0x00, 0x07, 0x00, 0x08,
51 0x94, 0x06, 0x2f, 0x93,
52 }
53 err := pkt.unmarshal(true, rawPkt)
54 assert.NoError(t, err)
55
56 _, ok := pkt.chunks[0].(*chunkInitAck)
57 assert.True(t, ok, "Failed to cast Chunk -> InitAck")
58 assert.NoError(t, err)
59 }
60
61 func TestChromeChunk1Init(t *testing.T) {
62 pkt := &packet{}
63 rawPkt := []byte{
64 0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0xbc, 0xb3, 0x45, 0xa2, 0x01, 0x00, 0x00, 0x56, 0xce, 0x15,
65 0x79, 0xa2, 0x00, 0x02, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0x94, 0x57, 0x95, 0xc0, 0xc0, 0x00, 0x00, 0x04,
66 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f, 0xc1, 0x80, 0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0xff, 0x5c,
67 0x49, 0x19, 0x4a, 0x94, 0xe8, 0x2a, 0xec, 0x58, 0x55, 0x62, 0x29, 0x1f, 0x8e, 0x23, 0xcd, 0x7c, 0xe8, 0x46,
68 0xba, 0x58, 0x1b, 0x3d, 0xab, 0xd7, 0x7e, 0x50, 0xf2, 0x41, 0xb1, 0x2e, 0x80, 0x04, 0x00, 0x06, 0x00, 0x01,
69 0x00, 0x00, 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00,
70 }
71 err := pkt.unmarshal(true, rawPkt)
72 assert.NoError(t, err)
73
74 rawPkt2, err := pkt.marshal(true)
75 assert.NoError(t, err)
76
77 assert.Equal(t, rawPkt, rawPkt2)
78 }
79
80 func TestChromeChunk2InitAck(t *testing.T) {
81 pkt := &packet{}
82 rawPkt := []byte{
83 0x13, 0x88, 0x13, 0x88, 0xce, 0x15, 0x79, 0xa2, 0xb5, 0xdb, 0x2d, 0x93, 0x02, 0x00, 0x01, 0x90, 0x9b, 0xd5,
84 0xb3, 0x6f, 0x00, 0x02, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0xef, 0xb4, 0x72, 0x87, 0xc0, 0x00, 0x00, 0x04,
85 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f, 0xc1, 0x80, 0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0x2e, 0xf9,
86 0x9c, 0x10, 0x63, 0x72, 0xed, 0x0d, 0x33, 0xc2, 0xdc, 0x7f, 0x9f, 0xd7, 0xef, 0x1b, 0xc9, 0xc4, 0xa7, 0x41,
87 0x9a, 0x07, 0x68, 0x6b, 0x66, 0xfb, 0x6a, 0x4e, 0x32, 0x5d, 0xe4, 0x25, 0x80, 0x04, 0x00, 0x06, 0x00, 0x01,
88 0x00, 0x00, 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00, 0x00, 0x07, 0x01, 0x38, 0x4b, 0x41, 0x4d, 0x45,
89 0x2d, 0x42, 0x53, 0x44, 0x20, 0x31, 0x2e, 0x31, 0x00, 0x00, 0x00, 0x00, 0x9c, 0x1e, 0x49, 0x5b, 0x00, 0x00,
90 0x00, 0x00, 0xd2, 0x42, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xea, 0x00, 0x00, 0xc4, 0x13, 0x3d, 0xe9,
91 0x86, 0xb1, 0x85, 0x75, 0xa2, 0x79, 0x15, 0xce, 0x9b, 0xd5, 0xb3, 0x6f, 0x20, 0xe0, 0x9f, 0x89, 0xe0, 0x27,
92 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x20, 0xe0, 0x9f, 0x89,
93 0xe0, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
94 0x00, 0x00, 0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
95 0x01, 0x00, 0x00, 0x56, 0xce, 0x15, 0x79, 0xa2, 0x00, 0x02, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0x94, 0x57,
96 0x95, 0xc0, 0xc0, 0x00, 0x00, 0x04, 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f, 0xc1, 0x80, 0x82, 0x00, 0x00, 0x00,
97 0x80, 0x02, 0x00, 0x24, 0xff, 0x5c, 0x49, 0x19, 0x4a, 0x94, 0xe8, 0x2a, 0xec, 0x58, 0x55, 0x62, 0x29, 0x1f,
98 0x8e, 0x23, 0xcd, 0x7c, 0xe8, 0x46, 0xba, 0x58, 0x1b, 0x3d, 0xab, 0xd7, 0x7e, 0x50, 0xf2, 0x41, 0xb1, 0x2e,
99 0x80, 0x04, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00, 0x02, 0x00,
100 0x01, 0x90, 0x9b, 0xd5, 0xb3, 0x6f, 0x00, 0x02, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0xef, 0xb4, 0x72, 0x87,
101 0xc0, 0x00, 0x00, 0x04, 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f, 0xc1, 0x80, 0x82, 0x00, 0x00, 0x00, 0x80, 0x02,
102 0x00, 0x24, 0x2e, 0xf9, 0x9c, 0x10, 0x63, 0x72, 0xed, 0x0d, 0x33, 0xc2, 0xdc, 0x7f, 0x9f, 0xd7, 0xef, 0x1b,
103 0xc9, 0xc4, 0xa7, 0x41, 0x9a, 0x07, 0x68, 0x6b, 0x66, 0xfb, 0x6a, 0x4e, 0x32, 0x5d, 0xe4, 0x25, 0x80, 0x04,
104 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00, 0xca, 0x0c, 0x21, 0x11,
105 0xce, 0xf4, 0xfc, 0xb3, 0x66, 0x99, 0x4f, 0xdb, 0x4f, 0x95, 0x6b, 0x6f, 0x3b, 0xb1, 0xdb, 0x5a,
106 }
107 err := pkt.unmarshal(true, rawPkt)
108 assert.NoError(t, err)
109
110 rawPkt2, err := pkt.marshal(true)
111 assert.NoError(t, err)
112
113 assert.Equal(t, rawPkt, rawPkt2)
114 }
115
116 func TestInitMarshalUnmarshal(t *testing.T) {
117 sctpPacket := &packet{}
118 sctpPacket.destinationPort = 1
119 sctpPacket.sourcePort = 1
120 sctpPacket.verificationTag = 123
121
122 initAck := &chunkInitAck{}
123
124 initAck.initialTSN = 123
125 initAck.numOutboundStreams = 1
126 initAck.numInboundStreams = 1
127 initAck.initiateTag = 123
128 initAck.advertisedReceiverWindowCredit = 1024
129 cookie, ErrRand := newRandomStateCookie()
130 assert.NoError(t, ErrRand, "Failed to generate random state cookie")
131
132 initAck.params = []param{cookie}
133
134 sctpPacket.chunks = []chunk{initAck}
135 rawPkt, err := sctpPacket.marshal(true)
136 assert.NoError(t, err)
137
138 pkt := &packet{}
139 err = pkt.unmarshal(true, rawPkt)
140 assert.NoError(t, err)
141
142 initAckChunk, ok := pkt.chunks[0].(*chunkInitAck)
143 assert.True(t, ok, "Failed to cast Chunk -> InitAck")
144
145 assert.NoError(t, err, "Unmarshal init ack Chunk failed")
146 assert.Equalf(t, initAckChunk.initiateTag, uint32(123),
147 "Unmarshal passed for SCTP packet, but got incorrect initiate tag exp: %d act: %d",
148 123, initAckChunk.initiateTag)
149 assert.Equalf(t, initAckChunk.advertisedReceiverWindowCredit, uint32(1024),
150 "Unmarshal passed for SCTP packet, but got incorrect advertisedReceiverWindowCredit exp: %d act: %d",
151 1024, initAckChunk.advertisedReceiverWindowCredit)
152 assert.Equalf(t, initAckChunk.numOutboundStreams, uint16(1),
153 "Unmarshal passed for SCTP packet, but got incorrect numOutboundStreams tag exp: %d act: %d",
154 1, initAckChunk.numOutboundStreams)
155 assert.Equalf(t, initAckChunk.numInboundStreams, uint16(1),
156 "Unmarshal passed for SCTP packet, but got incorrect numInboundStreams exp: %d act: %d",
157 1, initAckChunk.numInboundStreams)
158 assert.Equalf(t, initAckChunk.initialTSN, uint32(123),
159 "Unmarshal passed for SCTP packet, but got incorrect initialTSN exp: %d act: %d",
160 123, initAckChunk.initialTSN)
161 }
162
163 func TestPayloadDataMarshalUnmarshal(t *testing.T) {
164 pkt := &packet{}
165 rawPkt := []byte{
166 0x13, 0x88, 0x13, 0x88, 0xfc, 0xd6, 0x3f, 0xc6, 0xbe, 0xfa, 0xdc, 0x52, 0x0a, 0x00, 0x00, 0x24, 0x9b, 0x28,
167 0x7e, 0x48, 0xa3, 0x7b, 0xc1, 0x83, 0xc4, 0x4b, 0x41, 0x04, 0xa4, 0xf7, 0xed, 0x4c, 0x93, 0x62, 0xc3, 0x49,
168 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x1f, 0xa8, 0x79,
169 0xa1, 0xc7, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
170 0x00, 0x03, 0x00, 0x00, 0x66, 0x6f, 0x6f, 0x00,
171 }
172 err := pkt.unmarshal(true, rawPkt)
173 assert.NoError(t, err)
174
175 _, ok := pkt.chunks[1].(*chunkPayloadData)
176 assert.True(t, ok, "Failed to cast Chunk -> PayloadData")
177 }
178
179 func TestSelectAckChunk(t *testing.T) {
180 pkt := &packet{}
181 rawPkt := []byte{
182 0x13, 0x88, 0x13, 0x88, 0xc2, 0x98, 0x98, 0x0f, 0x42, 0x31, 0xea,
183 0x78, 0x03, 0x00, 0x00, 0x14, 0x87, 0x73, 0xbd, 0xa4, 0x00, 0x01,
184 0xfe, 0x74, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02,
185 }
186 err := pkt.unmarshal(true, rawPkt)
187 assert.NoError(t, err)
188
189 _, ok := pkt.chunks[0].(*chunkSelectiveAck)
190 assert.True(t, ok, "Failed to cast Chunk -> SelectiveAck")
191 }
192
193 func TestReconfigChunk(t *testing.T) {
194 pkt := &packet{}
195 rawPkt := []byte{
196 0x13, 0x88, 0x13, 0x88, 0xb6, 0xa5, 0x12, 0xe5, 0x75, 0x3b, 0x12, 0xd3, 0x82,
197 0x0, 0x0, 0x16, 0x0, 0xd, 0x0, 0x12, 0x4e, 0x1c, 0xb9, 0xe6, 0x3a, 0x74, 0x8d,
198 0xff, 0x4e, 0x1c, 0xb9, 0xe6, 0x0, 0x1, 0x0, 0x0,
199 }
200 err := pkt.unmarshal(true, rawPkt)
201 assert.NoError(t, err)
202
203 c, ok := pkt.chunks[0].(*chunkReconfig)
204 assert.True(t, ok, "Failed to cast Chunk -> Reconfig")
205
206 iden := c.paramA.(*paramOutgoingResetRequest).streamIdentifiers[0] //nolint:forcetypeassert
207 assert.Equalf(t, iden, uint16(1), "unexpected stream identifier: %d", iden)
208 }
209
210 func TestForwardTSNChunk(t *testing.T) {
211 pkt := &packet{}
212 rawPkt := append(
213 []byte{0x13, 0x88, 0x13, 0x88, 0xb6, 0xa5, 0x12, 0xe5, 0x1f, 0x9d, 0xa0, 0xfb},
214 testChunkForwardTSN()...,
215 )
216 err := pkt.unmarshal(true, rawPkt)
217 assert.NoError(t, err)
218
219 c, ok := pkt.chunks[0].(*chunkForwardTSN)
220 assert.True(t, ok, "Failed to cast Chunk -> Forward TSN")
221 assert.Equalf(t, c.newCumulativeTSN, uint32(3), "unexpected New Cumulative TSN")
222 }