-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathassociation_options_test.go
More file actions
176 lines (140 loc) · 4.38 KB
/
association_options_test.go
File metadata and controls
176 lines (140 loc) · 4.38 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
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
package sctp
import (
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func udpPiper(t *testing.T) (net.Conn, net.Conn) {
t.Helper()
return createUDPConnPair()
}
func TestAssociationOptions_ApplyBothSides(t *testing.T) {
opt := WithName("x")
var sCfg Config
var cCfg Config
assert.NoError(t, opt.applyServer(&sCfg))
assert.NoError(t, opt.applyClient(&cCfg))
assert.Equal(t, "x", sCfg.Name)
assert.Equal(t, "x", cCfg.Name)
}
func TestAssociationOptions_Validation(t *testing.T) {
t.Run("nil logger factory", func(t *testing.T) {
var cfg Config
err := WithLoggerFactory(nil).applyServer(&cfg)
assert.ErrorIs(t, err, errNilLoggerFactory)
})
t.Run("nil net conn", func(t *testing.T) {
var cfg Config
err := WithNetConn(nil).applyServer(&cfg)
assert.ErrorIs(t, err, errNilNetConn)
})
t.Run("mtu zero", func(t *testing.T) {
var cfg Config
err := WithMTU(0).applyServer(&cfg)
assert.ErrorIs(t, err, errZeroMTUOption)
})
t.Run("max recv buf zero", func(t *testing.T) {
var cfg Config
err := WithMaxReceiveBufferSize(0).applyServer(&cfg)
assert.ErrorIs(t, err, errZeroMaxReceiveBufferOption)
})
t.Run("max msg size zero", func(t *testing.T) {
var cfg Config
err := WithMaxMessageSize(0).applyServer(&cfg)
assert.ErrorIs(t, err, errZeroMaxMessageSize)
})
t.Run("rto max <= 0", func(t *testing.T) {
var cfg Config
err := WithRTOMax(0).applyServer(&cfg)
assert.ErrorIs(t, err, errInvalidRTOMax)
})
t.Run("snap nil arguments", func(t *testing.T) {
var cfg Config
err := WithSNAP(nil, nil).applyServer(&cfg)
assert.ErrorIs(t, err, errInvalidSnapToken)
err = WithSNAP([]byte{}, []byte{}).applyServer(&cfg)
assert.ErrorIs(t, err, errInvalidSnapToken)
})
}
func TestClientWithOptions_ValidatesOptionValues(t *testing.T) {
t.Run("missing net conn", func(t *testing.T) {
_, err := ClientWithOptions(WithName("no-net-conn"))
assert.ErrorIs(t, err, errNilNetConn)
})
t.Run("invalid option value", func(t *testing.T) {
ca, cb := udpPiper(t)
defer func() {
_ = ca.Close()
_ = cb.Close()
}()
_, err := ClientWithOptions(WithNetConn(ca), WithMTU(0))
assert.ErrorIs(t, err, errZeroMTUOption)
})
}
func TestServerWithOptions_ValidatesOptionValues(t *testing.T) {
t.Run("missing net conn", func(t *testing.T) {
_, err := ServerWithOptions(WithName("no-net-conn"))
assert.ErrorIs(t, err, errNilNetConn)
})
t.Run("invalid option value", func(t *testing.T) {
ca, cb := udpPiper(t)
defer func() {
_ = ca.Close()
_ = cb.Close()
}()
_, err := ServerWithOptions(WithNetConn(ca), WithRTOMax(0))
assert.ErrorIs(t, err, errInvalidRTOMax)
})
}
func TestAssociationOptions_ClientAndServer(t *testing.T) {
lf := customLogger{
expectZeroChecksum: true,
t: t,
}
aClient, aServer, err := association(
t,
udpPiper,
WithName("opt-pair"),
WithLoggerFactory(lf),
WithMTU(1200),
WithMaxReceiveBufferSize(7777),
WithMaxMessageSize(30000),
WithRTOMax(1000),
WithMinCwnd(5000),
WithFastRtxWnd(6000),
WithCwndCAStep(7000),
WithBlockWrite(true),
WithEnableZeroChecksum(true),
)
assert.NoError(t, err)
defer func() {
_ = aClient.Close()
_ = aServer.Close()
}()
_, ok := aClient.log.(customLogger)
assert.True(t, ok, "client logger should be customLogger")
_, ok = aServer.log.(customLogger)
assert.True(t, ok, "server logger should be customLogger")
assert.Equal(t, uint32(1200), aClient.MTU())
assert.Equal(t, uint32(1200), aServer.MTU())
assert.Equal(t, uint32(7777), aClient.maxReceiveBufferSize)
assert.Equal(t, uint32(7777), aServer.maxReceiveBufferSize)
assert.Equal(t, uint32(30000), aClient.MaxMessageSize())
assert.Equal(t, uint32(30000), aServer.MaxMessageSize())
assert.Equal(t, uint32(5000), aClient.minCwnd)
assert.Equal(t, uint32(6000), aClient.fastRtxWnd)
assert.Equal(t, uint32(7000), aClient.cwndCAStep)
assert.True(t, aClient.blockWrite)
assert.True(t, aServer.blockWrite)
assert.True(t, aClient.recvZeroChecksum)
assert.True(t, aServer.recvZeroChecksum)
assert.Equal(t, uint32(1200)-(commonHeaderSize+dataChunkHeaderSize), aClient.maxPayloadSize)
assert.Equal(t, uint32(1200)-(commonHeaderSize+dataChunkHeaderSize), aServer.maxPayloadSize)
assert.Equal(t, "opt-pair", aClient.name)
assert.Equal(t, "opt-pair", aServer.name)
time.Sleep(10 * time.Millisecond)
}