Debian Codegraph @

association_options_test.go

raw
1 // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
2 // SPDX-License-Identifier: MIT
3
4 //go:build !js
5
6 package sctp
7
8 import (
9 "net"
10 "testing"
11 "time"
12
13 "github.com/stretchr/testify/assert"
14 )
15
16 func udpPiper(t *testing.T) (net.Conn, net.Conn) {
17 t.Helper()
18
19 return createUDPConnPair()
20 }
21
22 func TestAssociationOptions_ApplyBothSides(t *testing.T) {
23 opts := []AssociationOption{
24 WithName("x"),
25 WithMaxReassemblyQueueEntries(13),
26 }
27
28 var sCfg Config
29 var cCfg Config
30
31 for _, opt := range opts {
32 assert.NoError(t, opt.applyServer(&sCfg))
33 assert.NoError(t, opt.applyClient(&cCfg))
34 }
35
36 assert.Equal(t, "x", sCfg.Name)
37 assert.Equal(t, "x", cCfg.Name)
38 assert.Equal(t, uint32(13), sCfg.maxReassemblyQueueEntries)
39 assert.Equal(t, uint32(13), cCfg.maxReassemblyQueueEntries)
40 }
41
42 func TestConfigComparable(t *testing.T) {
43 _ = map[Config]struct{}{}
44 }
45
46 func TestAssociationOptions_Interleaving(t *testing.T) {
47 ca, cb := udpPiper(t)
48 defer func() {
49 _ = ca.Close()
50 _ = cb.Close()
51 }()
52
53 defaultCfg, err := buildClientConfig(WithNetConn(ca))
54 assert.NoError(t, err)
55 assert.True(t, defaultCfg.enableInterleaving)
56
57 disabledCfg, err := buildServerConfig(WithNetConn(cb), WithEnableInterleaving(false))
58 assert.NoError(t, err)
59 assert.False(t, disabledCfg.enableInterleaving)
60 assert.True(t, disabledCfg.enableInterleavingSet)
61 }
62
63 func TestAssociationOptions_DefaultMTU(t *testing.T) {
64 ca, cb := udpPiper(t)
65 defer func() {
66 _ = ca.Close()
67 _ = cb.Close()
68 }()
69
70 cfg, err := buildClientConfig(WithNetConn(ca))
71 assert.NoError(t, err)
72 assert.Equal(t, uint32(1191), cfg.MTU)
73
74 assoc := createAssociationFromConfigWithTsn(cfg, 1)
75 defer assoc.close() //nolint:errcheck
76 assert.Equal(t, uint32(1191), assoc.MTU())
77 assert.Equal(t, maxPayloadSizeForMTU(initialMTU, false), assoc.maxPayloadSize)
78 }
79
80 func TestAssociationOptions_Validation(t *testing.T) {
81 t.Run("nil logger factory", func(t *testing.T) {
82 var cfg Config
83 err := WithLoggerFactory(nil).applyServer(&cfg)
84 assert.ErrorIs(t, err, errNilLoggerFactory)
85 })
86
87 t.Run("nil net conn", func(t *testing.T) {
88 var cfg Config
89 err := WithNetConn(nil).applyServer(&cfg)
90 assert.ErrorIs(t, err, errNilNetConn)
91 })
92
93 t.Run("mtu zero", func(t *testing.T) {
94 var cfg Config
95 err := WithMTU(0).applyServer(&cfg)
96 assert.ErrorIs(t, err, errZeroMTUOption)
97 })
98
99 t.Run("mtu too small", func(t *testing.T) {
100 minMTU := commonHeaderSize + iDataChunkHeaderSize + paddingMultiple
101
102 var cfg Config
103 err := WithMTU(minMTU - 1).applyServer(&cfg)
104 assert.ErrorIs(t, err, errMTUTooSmallOption)
105
106 err = Config{MTU: minMTU - 1}.applyClient(&cfg)
107 assert.ErrorIs(t, err, errMTUTooSmallOption)
108
109 err = WithMTU(minMTU).applyServer(&cfg)
110 assert.NoError(t, err)
111 assert.Equal(t, minMTU, cfg.MTU)
112 })
113
114 t.Run("max recv buf zero", func(t *testing.T) {
115 var cfg Config
116 err := WithMaxReceiveBufferSize(0).applyServer(&cfg)
117 assert.ErrorIs(t, err, errZeroMaxReceiveBufferOption)
118 })
119
120 t.Run("max msg size zero", func(t *testing.T) {
121 var cfg Config
122 err := WithMaxMessageSize(0).applyServer(&cfg)
123 assert.ErrorIs(t, err, errZeroMaxMessageSize)
124 })
125
126 t.Run("rto max <= 0", func(t *testing.T) {
127 var cfg Config
128 err := WithRTOMax(0).applyServer(&cfg)
129 assert.ErrorIs(t, err, errInvalidRTOMax)
130 })
131
132 t.Run("snap nil arguments", func(t *testing.T) {
133 var cfg Config
134 err := WithSNAP(nil, nil).applyServer(&cfg)
135 assert.ErrorIs(t, err, errInvalidSnapToken)
136 err = WithSNAP([]byte{}, []byte{}).applyServer(&cfg)
137 assert.ErrorIs(t, err, errInvalidSnapToken)
138 })
139
140 t.Run("nil stream scheduler", func(t *testing.T) {
141 var cfg Config
142 err := WithInterleavingOptions(WithInterleavingStreamSchedulerFactory(nil)).applyServer(&cfg)
143 assert.ErrorIs(t, err, errNilStreamScheduler)
144 })
145
146 t.Run("invalid stream scheduler weight", func(t *testing.T) {
147 var cfg Config
148 err := WithInterleavingOptions(
149 WithInterleavingWeightedFairQueueingWeight(1, 0),
150 ).applyServer(&cfg)
151 assert.ErrorIs(t, err, errInvalidStreamSchedulerWeight)
152 })
153 }
154
155 func TestClientWithOptions_ValidatesOptionValues(t *testing.T) {
156 t.Run("missing net conn", func(t *testing.T) {
157 _, err := ClientWithOptions(WithName("no-net-conn"))
158 assert.ErrorIs(t, err, errNilNetConn)
159 })
160
161 t.Run("invalid option value", func(t *testing.T) {
162 ca, cb := udpPiper(t)
163 defer func() {
164 _ = ca.Close()
165 _ = cb.Close()
166 }()
167
168 _, err := ClientWithOptions(WithNetConn(ca), WithMTU(0))
169 assert.ErrorIs(t, err, errZeroMTUOption)
170 })
171 }
172
173 func TestServerWithOptions_ValidatesOptionValues(t *testing.T) {
174 t.Run("missing net conn", func(t *testing.T) {
175 _, err := ServerWithOptions(WithName("no-net-conn"))
176 assert.ErrorIs(t, err, errNilNetConn)
177 })
178
179 t.Run("invalid option value", func(t *testing.T) {
180 ca, cb := udpPiper(t)
181 defer func() {
182 _ = ca.Close()
183 _ = cb.Close()
184 }()
185
186 _, err := ServerWithOptions(WithNetConn(ca), WithRTOMax(0))
187 assert.ErrorIs(t, err, errInvalidRTOMax)
188 })
189 }
190
191 func TestAssociationOptions_ClientAndServer(t *testing.T) {
192 lf := customLogger{
193 expectZeroChecksum: true,
194 t: t,
195 }
196
197 aClient, aServer, err := association(
198 t,
199 udpPiper,
200 WithName("opt-pair"),
201 WithLoggerFactory(lf),
202
203 WithMTU(1200),
204 WithMaxReceiveBufferSize(7777),
205 WithMaxMessageSize(30000),
206 WithMaxReassemblyQueueEntries(13),
207 WithRTOMax(1000),
208 WithMinCwnd(5000),
209 WithFastRtxWnd(6000),
210 WithCwndCAStep(7000),
211 WithBlockWrite(true),
212 WithEnableZeroChecksum(true),
213 WithEnableInterleaving(false),
214 )
215 assert.NoError(t, err)
216 defer func() {
217 _ = aClient.Close()
218 _ = aServer.Close()
219 }()
220
221 _, ok := aClient.log.(customLogger)
222 assert.True(t, ok, "client logger should be customLogger")
223 _, ok = aServer.log.(customLogger)
224 assert.True(t, ok, "server logger should be customLogger")
225
226 assert.Equal(t, uint32(1200), aClient.MTU())
227 assert.Equal(t, uint32(1200), aServer.MTU())
228
229 assert.Equal(t, uint32(7777), aClient.maxReceiveBufferSize)
230 assert.Equal(t, uint32(7777), aServer.maxReceiveBufferSize)
231
232 assert.Equal(t, uint32(30000), aClient.MaxMessageSize())
233 assert.Equal(t, uint32(30000), aServer.MaxMessageSize())
234
235 assert.Equal(t, uint32(13), aClient.maxReassemblyQueueEntries)
236 assert.Equal(t, uint32(13), aServer.maxReassemblyQueueEntries)
237
238 assert.Equal(t, uint32(5000), aClient.minCwnd)
239 assert.Equal(t, uint32(6000), aClient.fastRtxWnd)
240 assert.Equal(t, uint32(7000), aClient.cwndCAStep)
241
242 assert.True(t, aClient.blockWrite)
243 assert.True(t, aServer.blockWrite)
244
245 assert.True(t, aClient.recvZeroChecksum)
246 assert.True(t, aServer.recvZeroChecksum)
247
248 assert.Equal(t, maxPayloadSizeForMTU(1200, false), aClient.maxPayloadSize)
249 assert.Equal(t, maxPayloadSizeForMTU(1200, false), aServer.maxPayloadSize)
250
251 assert.Equal(t, "opt-pair", aClient.name)
252 assert.Equal(t, "opt-pair", aServer.name)
253
254 time.Sleep(10 * time.Millisecond)
255 }