-
Notifications
You must be signed in to change notification settings - Fork 0
/
candidate_test.go
439 lines (408 loc) · 11.5 KB
/
candidate_test.go
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package ice
import (
"net"
"testing"
"time"
"github.com/pion/logging"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCandidateTypePreference(t *testing.T) {
r := require.New(t)
hostDefaultPreference := uint16(126)
prflxDefaultPreference := uint16(110)
srflxDefaultPreference := uint16(100)
relayDefaultPreference := uint16(0)
tcpOffsets := []uint16{0, 10}
for _, tcpOffset := range tcpOffsets {
agent := &Agent{
tcpPriorityOffset: tcpOffset,
}
for _, networkType := range supportedNetworkTypes() {
hostCandidate := candidateBase{
candidateType: CandidateTypeHost,
networkType: networkType,
currAgent: agent,
}
prflxCandidate := candidateBase{
candidateType: CandidateTypePeerReflexive,
networkType: networkType,
currAgent: agent,
}
srflxCandidate := candidateBase{
candidateType: CandidateTypeServerReflexive,
networkType: networkType,
currAgent: agent,
}
relayCandidate := candidateBase{
candidateType: CandidateTypeRelay,
networkType: networkType,
currAgent: agent,
}
if networkType.IsTCP() {
r.Equal(hostDefaultPreference-tcpOffset, hostCandidate.TypePreference())
r.Equal(prflxDefaultPreference-tcpOffset, prflxCandidate.TypePreference())
r.Equal(srflxDefaultPreference-tcpOffset, srflxCandidate.TypePreference())
} else {
r.Equal(hostDefaultPreference, hostCandidate.TypePreference())
r.Equal(prflxDefaultPreference, prflxCandidate.TypePreference())
r.Equal(srflxDefaultPreference, srflxCandidate.TypePreference())
}
r.Equal(relayDefaultPreference, relayCandidate.TypePreference())
}
}
}
func TestCandidatePriority(t *testing.T) {
for _, test := range []struct {
Candidate Candidate
WantPriority uint32
}{
{
Candidate: &CandidateHost{
candidateBase: candidateBase{
candidateType: CandidateTypeHost,
component: ComponentRTP,
},
},
WantPriority: 2130706431,
},
{
Candidate: &CandidateHost{
candidateBase: candidateBase{
candidateType: CandidateTypeHost,
component: ComponentRTP,
networkType: NetworkTypeTCP4,
tcpType: TCPTypeActive,
},
},
WantPriority: 1675624447,
},
{
Candidate: &CandidateHost{
candidateBase: candidateBase{
candidateType: CandidateTypeHost,
component: ComponentRTP,
networkType: NetworkTypeTCP4,
tcpType: TCPTypePassive,
},
},
WantPriority: 1671430143,
},
{
Candidate: &CandidateHost{
candidateBase: candidateBase{
candidateType: CandidateTypeHost,
component: ComponentRTP,
networkType: NetworkTypeTCP4,
tcpType: TCPTypeSimultaneousOpen,
},
},
WantPriority: 1667235839,
},
{
Candidate: &CandidatePeerReflexive{
candidateBase: candidateBase{
candidateType: CandidateTypePeerReflexive,
component: ComponentRTP,
},
},
WantPriority: 1862270975,
},
{
Candidate: &CandidatePeerReflexive{
candidateBase: candidateBase{
candidateType: CandidateTypePeerReflexive,
component: ComponentRTP,
networkType: NetworkTypeTCP6,
tcpType: TCPTypeSimultaneousOpen,
},
},
WantPriority: 1407188991,
},
{
Candidate: &CandidatePeerReflexive{
candidateBase: candidateBase{
candidateType: CandidateTypePeerReflexive,
component: ComponentRTP,
networkType: NetworkTypeTCP6,
tcpType: TCPTypeActive,
},
},
WantPriority: 1402994687,
},
{
Candidate: &CandidatePeerReflexive{
candidateBase: candidateBase{
candidateType: CandidateTypePeerReflexive,
component: ComponentRTP,
networkType: NetworkTypeTCP6,
tcpType: TCPTypePassive,
},
},
WantPriority: 1398800383,
},
{
Candidate: &CandidateServerReflexive{
candidateBase: candidateBase{
candidateType: CandidateTypeServerReflexive,
component: ComponentRTP,
},
},
WantPriority: 1694498815,
},
{
Candidate: &CandidateRelay{
candidateBase: candidateBase{
candidateType: CandidateTypeRelay,
component: ComponentRTP,
},
},
WantPriority: 16777215,
},
} {
if got, want := test.Candidate.Priority(), test.WantPriority; got != want {
t.Fatalf("Candidate(%v).Priority() = %d, want %d", test.Candidate, got, want)
}
}
}
func TestCandidateLastSent(t *testing.T) {
candidate := candidateBase{}
assert.Equal(t, candidate.LastSent(), time.Time{})
now := time.Now()
candidate.setLastSent()
// assert.Equal(t, candidate.LastSent(), now)
assert.WithinDuration(t, candidate.LastSent(), now, time.Millisecond)
}
func TestCandidateLastReceived(t *testing.T) {
candidate := candidateBase{}
assert.Equal(t, candidate.LastReceived(), time.Time{})
now := time.Now()
candidate.setLastReceived()
// assert.Equal(t, candidate.LastReceived(), now)
assert.WithinDuration(t, candidate.LastReceived(), now, time.Millisecond)
}
func TestCandidateFoundation(t *testing.T) {
// All fields are the same
assert.Equal(t,
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
}).Foundation(),
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
}).Foundation())
// Different Address
assert.NotEqual(t,
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
}).Foundation(),
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "B",
}).Foundation())
// Different networkType
assert.NotEqual(t,
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
}).Foundation(),
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP6,
address: "A",
}).Foundation())
// Different candidateType
assert.NotEqual(t,
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
}).Foundation(),
(&candidateBase{
candidateType: CandidateTypePeerReflexive,
networkType: NetworkTypeUDP4,
address: "A",
}).Foundation())
// Port has no effect
assert.Equal(t,
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
port: 8080,
}).Foundation(),
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
port: 80,
}).Foundation())
}
func TestCandidateMarshal(t *testing.T) {
for _, test := range []struct {
candidate Candidate
marshaled string
expectError bool
}{
{
&CandidateHost{
candidateBase{
networkType: NetworkTypeUDP6,
candidateType: CandidateTypeHost,
address: "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a",
port: 53987,
priorityOverride: 500,
foundationOverride: "750",
},
"",
},
"750 1 udp 500 fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a 53987 typ host",
false,
},
{
&CandidateHost{
candidateBase{
networkType: NetworkTypeUDP4,
candidateType: CandidateTypeHost,
address: "10.0.75.1",
port: 53634,
},
"",
},
"4273957277 1 udp 2130706431 10.0.75.1 53634 typ host",
false,
},
{
&CandidateServerReflexive{
candidateBase{
networkType: NetworkTypeUDP4,
candidateType: CandidateTypeServerReflexive,
address: "191.228.238.68",
port: 53991,
relatedAddress: &CandidateRelatedAddress{"192.168.0.274", 53991},
},
},
"647372371 1 udp 1694498815 191.228.238.68 53991 typ srflx raddr 192.168.0.274 rport 53991",
false,
},
{
&CandidateRelay{
candidateBase{
networkType: NetworkTypeUDP4,
candidateType: CandidateTypeRelay,
address: "50.0.0.1",
port: 5000,
relatedAddress: &CandidateRelatedAddress{"192.168.0.1", 5001},
},
"",
nil,
},
"848194626 1 udp 16777215 50.0.0.1 5000 typ relay raddr 192.168.0.1 rport 5001",
false,
},
{
&CandidateHost{
candidateBase{
networkType: NetworkTypeTCP4,
candidateType: CandidateTypeHost,
address: "192.168.0.196",
port: 0,
tcpType: TCPTypeActive,
},
"",
},
"1052353102 1 tcp 2128609279 192.168.0.196 0 typ host tcptype active",
false,
},
{
&CandidateHost{
candidateBase{
networkType: NetworkTypeUDP4,
candidateType: CandidateTypeHost,
address: "e2494022-4d9a-4c1e-a750-cc48d4f8d6ee.local",
port: 60542,
},
"",
},
"1380287402 1 udp 2130706431 e2494022-4d9a-4c1e-a750-cc48d4f8d6ee.local 60542 typ host", false,
},
// Missing Foundation
{
&CandidateHost{
candidateBase{
networkType: NetworkTypeUDP4,
candidateType: CandidateTypeHost,
address: "127.0.0.1",
port: 80,
priorityOverride: 500,
foundationOverride: " ",
},
"",
},
" 1 udp 500 127.0.0.1 80 typ host",
false,
},
// Invalid candidates
{nil, "", true},
{nil, "1938809241", true},
{nil, "1986380506 99999999 udp 2122063615 10.0.75.1 53634 typ host generation 0 network-id 2", true},
{nil, "1986380506 1 udp 99999999999 10.0.75.1 53634 typ host", true},
{nil, "4207374051 1 udp 1685790463 191.228.238.68 99999999 typ srflx raddr 192.168.0.278 rport 53991 generation 0 network-id 3", true},
{nil, "4207374051 1 udp 1685790463 191.228.238.68 53991 typ srflx raddr", true},
{nil, "4207374051 1 udp 1685790463 191.228.238.68 53991 typ srflx raddr 192.168.0.278 rport 99999999 generation 0 network-id 3", true},
{nil, "4207374051 INVALID udp 2130706431 10.0.75.1 53634 typ host", true},
{nil, "4207374051 1 udp INVALID 10.0.75.1 53634 typ host", true},
{nil, "4207374051 INVALID udp 2130706431 10.0.75.1 INVALID typ host", true},
{nil, "4207374051 1 udp 2130706431 10.0.75.1 53634 typ INVALID", true},
{nil, "4207374051 1 INVALID 2130706431 10.0.75.1 53634 typ host", true},
} {
actualCandidate, err := UnmarshalCandidate(test.marshaled)
if test.expectError {
assert.Error(t, err)
continue
}
assert.NoError(t, err)
assert.True(t, test.candidate.Equal(actualCandidate))
assert.Equal(t, test.marshaled, actualCandidate.Marshal())
}
}
func TestCandidateWriteTo(t *testing.T) {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: net.IP{127, 0, 0, 1},
Port: 0,
})
require.NoError(t, err, "error creating test TCP listener")
conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
require.NoError(t, err, "error dialing test TCP connection")
loggerFactory := logging.NewDefaultLoggerFactory()
packetConn := newTCPPacketConn(tcpPacketParams{
ReadBuffer: 2048,
Logger: loggerFactory.NewLogger("tcp-packet-conn"),
})
err = packetConn.AddConn(conn, nil)
require.NoError(t, err, "error adding test TCP connection to packet connection")
c1 := &candidateBase{
conn: packetConn,
currAgent: &Agent{
log: loggerFactory.NewLogger("agent"),
},
}
c2 := &candidateBase{
resolvedAddr: listener.Addr(),
}
_, err = c1.writeTo([]byte("test"), c2)
assert.NoError(t, err, "writing to open conn")
err = packetConn.Close()
require.NoError(t, err, "error closing test TCP connection")
_, err = c1.writeTo([]byte("test"), c2)
assert.Error(t, err, "writing to closed conn")
}