FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cloudflared/datagramsession/session_test.go at master · SmartCodeSmith/cloudflared · GitHub
SmartCodeSmith
/
cloudflared
Public
forked from
cloudflare/cloudflared
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cloudflared
/
datagramsession
/
session_test.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
264 lines (229 loc) · 6.57 KB
Breadcrumbs
cloudflared
/
datagramsession
/
session_test.go
Copy path
File metadata and controls
264 lines (229 loc) · 6.57 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package
datagramsession
import
(
"bytes"
"context"
"fmt"
"io"
"net"
"sync"
"testing"
"time"
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"github.com/cloudflare/cloudflared/packet"
)
// TestCloseSession makes sure a session will stop after context is done
func
TestSessionCtxDone
(
t
*
testing.
T
) {
testSessionReturns
(
t
,
closeByContext
,
time
.
Minute
*
2
)
}
// TestCloseSession makes sure a session will stop after close method is called
func
TestCloseSession
(
t
*
testing.
T
) {
testSessionReturns
(
t
,
closeByCallingClose
,
time
.
Minute
*
2
)
}
// TestCloseIdle makess sure a session will stop after there is no read/write for a period defined by closeAfterIdle
func
TestCloseIdle
(
t
*
testing.
T
) {
testSessionReturns
(
t
,
closeByTimeout
,
time
.
Millisecond
*
100
)
}
func
testSessionReturns
(
t
*
testing.
T
,
closeBy
closeMethod
,
closeAfterIdle
time.
Duration
) {
var
(
localCloseReason
=
&
errClosedSession
{
message
:
"connection closed by origin"
,
byRemote
:
false
,
}
)
sessionID
:=
uuid
.
New
()
cfdConn
,
originConn
:=
net
.
Pipe
()
payload
:=
testPayload
(
sessionID
)
log
:=
zerolog
.
Nop
()
mg
:=
NewManager
(
&
log
,
nil
,
nil
)
session
:=
mg
.
newSession
(
sessionID
,
cfdConn
)
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
sessionDone
:=
make
(
chan
struct
{})
go
func
() {
closedByRemote
,
err
:=
session
.
Serve
(
ctx
,
closeAfterIdle
)
switch
closeBy
{
case
closeByContext
:
require
.
Equal
(
t
,
context
.
Canceled
,
err
)
require
.
False
(
t
,
closedByRemote
)
case
closeByCallingClose
:
require
.
Equal
(
t
,
localCloseReason
,
err
)
require
.
Equal
(
t
,
localCloseReason
.
byRemote
,
closedByRemote
)
case
closeByTimeout
:
require
.
Equal
(
t
,
SessionIdleErr
(
closeAfterIdle
),
err
)
require
.
False
(
t
,
closedByRemote
)
}
close
(
sessionDone
)
}()
go
func
() {
n
,
err
:=
session
.
transportToDst
(
payload
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
len
(
payload
),
n
)
}()
readBuffer
:=
make
([]
byte
,
len
(
payload
)
+
1
)
n
,
err
:=
originConn
.
Read
(
readBuffer
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
len
(
payload
),
n
)
lastRead
:=
time
.
Now
()
switch
closeBy
{
case
closeByContext
:
cancel
()
case
closeByCallingClose
:
session
.
close
(
localCloseReason
)
}
<-
sessionDone
if
closeBy
==
closeByTimeout
{
require
.
True
(
t
,
time
.
Now
().
After
(
lastRead
.
Add
(
closeAfterIdle
)))
}
// call cancelled again otherwise the linter will warn about possible context leak
cancel
()
}
type
closeMethod
int
const
(
closeByContext
closeMethod
=
iota
closeByCallingClose
closeByTimeout
)
func
TestWriteToDstSessionPreventClosed
(
t
*
testing.
T
) {
testActiveSessionNotClosed
(
t
,
false
,
true
)
}
func
TestReadFromDstSessionPreventClosed
(
t
*
testing.
T
) {
testActiveSessionNotClosed
(
t
,
true
,
false
)
}
func
testActiveSessionNotClosed
(
t
*
testing.
T
,
readFromDst
bool
,
writeToDst
bool
) {
const
closeAfterIdle
=
time
.
Millisecond
*
100
const
activeTime
=
time
.
Millisecond
*
500
sessionID
:=
uuid
.
New
()
cfdConn
,
originConn
:=
net
.
Pipe
()
payload
:=
testPayload
(
sessionID
)
respChan
:=
make
(
chan
*
packet.
Session
)
sender
:=
newMockTransportSender
(
sessionID
,
payload
)
mg
:=
NewManager
(
&
nopLogger
,
sender
.
muxSession
,
respChan
)
session
:=
mg
.
newSession
(
sessionID
,
cfdConn
)
startTime
:=
time
.
Now
()
activeUntil
:=
startTime
.
Add
(
activeTime
)
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
errGroup
,
ctx
:=
errgroup
.
WithContext
(
ctx
)
errGroup
.
Go
(
func
()
error
{
session
.
Serve
(
ctx
,
closeAfterIdle
)
if
time
.
Now
().
Before
(
startTime
.
Add
(
activeTime
)) {
return
fmt
.
Errorf
(
"session closed while it's still active"
)
}
return
nil
})
if
readFromDst
{
errGroup
.
Go
(
func
()
error
{
for
{
if
time
.
Now
().
After
(
activeUntil
) {
return
nil
}
if
_
,
err
:=
originConn
.
Write
(
payload
);
err
!=
nil
{
return
err
}
time
.
Sleep
(
closeAfterIdle
/
2
)
}
})
}
if
writeToDst
{
errGroup
.
Go
(
func
()
error
{
readBuffer
:=
make
([]
byte
,
len
(
payload
))
for
{
n
,
err
:=
originConn
.
Read
(
readBuffer
)
if
err
!=
nil
{
if
err
==
io
.
EOF
||
err
==
io
.
ErrClosedPipe
{
return
nil
}
return
err
}
if
!
bytes
.
Equal
(
payload
,
readBuffer
[:
n
]) {
return
fmt
.
Errorf
(
"payload %v is not equal to %v"
,
readBuffer
[:
n
],
payload
)
}
}
})
errGroup
.
Go
(
func
()
error
{
for
{
if
time
.
Now
().
After
(
activeUntil
) {
return
nil
}
if
_
,
err
:=
session
.
transportToDst
(
payload
);
err
!=
nil
{
return
err
}
time
.
Sleep
(
closeAfterIdle
/
2
)
}
})
}
require
.
NoError
(
t
,
errGroup
.
Wait
())
cancel
()
}
func
TestMarkActiveNotBlocking
(
t
*
testing.
T
) {
const
concurrentCalls
=
50
mg
:=
NewManager
(
&
nopLogger
,
nil
,
nil
)
session
:=
mg
.
newSession
(
uuid
.
New
(),
nil
)
var
wg
sync.
WaitGroup
wg
.
Add
(
concurrentCalls
)
for
i
:=
0
;
i
<
concurrentCalls
;
i
++
{
go
func
() {
session
.
markActive
()
wg
.
Done
()
}()
}
wg
.
Wait
()
}
// Some UDP application might send 0-size payload.
func
TestZeroBytePayload
(
t
*
testing.
T
) {
sessionID
:=
uuid
.
New
()
cfdConn
,
originConn
:=
net
.
Pipe
()
sender
:=
sendOnceTransportSender
{
baseSender
:
newMockTransportSender
(
sessionID
,
make
([]
byte
,
0
)),
sentChan
:
make
(
chan
struct
{}),
}
mg
:=
NewManager
(
&
nopLogger
,
sender
.
muxSession
,
nil
)
session
:=
mg
.
newSession
(
sessionID
,
cfdConn
)
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
errGroup
,
ctx
:=
errgroup
.
WithContext
(
ctx
)
errGroup
.
Go
(
func
()
error
{
// Read from underlying conn and send to transport
closedByRemote
,
err
:=
session
.
Serve
(
ctx
,
time
.
Minute
*
2
)
require
.
Equal
(
t
,
context
.
Canceled
,
err
)
require
.
False
(
t
,
closedByRemote
)
return
nil
})
errGroup
.
Go
(
func
()
error
{
// Write to underlying connection
n
,
err
:=
originConn
.
Write
([]
byte
{})
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
0
,
n
)
return
nil
})
<-
sender
.
sentChan
cancel
()
require
.
NoError
(
t
,
errGroup
.
Wait
())
}
type
mockTransportSender
struct
{
expectedSessionID
uuid.
UUID
expectedPayload
[]
byte
}
func
newMockTransportSender
(
expectedSessionID
uuid.
UUID
,
expectedPayload
[]
byte
)
*
mockTransportSender
{
return
&
mockTransportSender
{
expectedSessionID
:
expectedSessionID
,
expectedPayload
:
expectedPayload
,
}
}
func
(
mts
*
mockTransportSender
)
muxSession
(
session
*
packet.
Session
)
error
{
if
session
.
ID
!=
mts
.
expectedSessionID
{
return
fmt
.
Errorf
(
"Expect session %s, got %s"
,
mts
.
expectedSessionID
,
session
.
ID
)
}
if
!
bytes
.
Equal
(
session
.
Payload
,
mts
.
expectedPayload
) {
return
fmt
.
Errorf
(
"Expect %v, read %v"
,
mts
.
expectedPayload
,
session
.
Payload
)
}
return
nil
}
type
sendOnceTransportSender
struct
{
baseSender
*
mockTransportSender
sentChan
chan
struct
{}
}
func
(
sots
*
sendOnceTransportSender
)
muxSession
(
session
*
packet.
Session
)
error
{
defer
close
(
sots
.
sentChan
)
return
sots
.
baseSender
.
muxSession
(
session
)
}
Back
|
FazBrowse Home
|
New Git URL