-
Notifications
You must be signed in to change notification settings - Fork 162
/
candidate_test.go
468 lines (430 loc) · 12.7 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package ice
import (
"net"
"strconv"
"testing"
"time"
"github.com/pion/logging"
"github.com/stretchr/testify/require"
)
const localhostIPStr = "127.0.0.1"
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{}
require.Equal(t, candidate.LastSent(), time.Time{})
now := time.Now()
candidate.setLastSent(now)
require.Equal(t, candidate.LastSent(), now)
}
func TestCandidateLastReceived(t *testing.T) {
candidate := candidateBase{}
require.Equal(t, candidate.LastReceived(), time.Time{})
now := time.Now()
candidate.setLastReceived(now)
require.Equal(t, candidate.LastReceived(), now)
}
func TestCandidateFoundation(t *testing.T) {
// All fields are the same
require.Equal(t,
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
}).Foundation(),
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
}).Foundation())
// Different Address
require.NotEqual(t,
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
}).Foundation(),
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "B",
}).Foundation())
// Different networkType
require.NotEqual(t,
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
}).Foundation(),
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP6,
address: "A",
}).Foundation())
// Different candidateType
require.NotEqual(t,
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
}).Foundation(),
(&candidateBase{
candidateType: CandidateTypePeerReflexive,
networkType: NetworkTypeUDP4,
address: "A",
}).Foundation())
// Port has no effect
require.Equal(t,
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
port: 8080,
}).Foundation(),
(&candidateBase{
candidateType: CandidateTypeHost,
networkType: NetworkTypeUDP4,
address: "A",
port: 80,
}).Foundation())
}
func mustCandidateHost(conf *CandidateHostConfig) Candidate {
cand, err := NewCandidateHost(conf)
if err != nil {
panic(err)
}
return cand
}
func mustCandidateRelay(conf *CandidateRelayConfig) Candidate {
cand, err := NewCandidateRelay(conf)
if err != nil {
panic(err)
}
return cand
}
func mustCandidateServerReflexive(conf *CandidateServerReflexiveConfig) Candidate {
cand, err := NewCandidateServerReflexive(conf)
if err != nil {
panic(err)
}
return cand
}
func TestCandidateMarshal(t *testing.T) {
for idx, test := range []struct {
candidate Candidate
marshaled string
expectError bool
}{
{
mustCandidateHost(&CandidateHostConfig{
Network: NetworkTypeUDP6.String(),
Address: "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a",
Port: 53987,
Priority: 500,
Foundation: "750",
}),
"750 1 udp 500 fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a 53987 typ host",
false,
},
{
mustCandidateHost(&CandidateHostConfig{
Network: NetworkTypeUDP4.String(),
Address: "10.0.75.1",
Port: 53634,
}),
"4273957277 1 udp 2130706431 10.0.75.1 53634 typ host",
false,
},
{
mustCandidateServerReflexive(&CandidateServerReflexiveConfig{
Network: NetworkTypeUDP4.String(),
Address: "191.228.238.68",
Port: 53991,
RelAddr: "192.168.0.274",
RelPort: 53991,
}),
"647372371 1 udp 1694498815 191.228.238.68 53991 typ srflx raddr 192.168.0.274 rport 53991",
false,
},
{
mustCandidateRelay(&CandidateRelayConfig{
Network: NetworkTypeUDP4.String(),
Address: "50.0.0.1",
Port: 5000,
RelAddr: "192.168.0.1",
RelPort: 5001,
}),
"848194626 1 udp 16777215 50.0.0.1 5000 typ relay raddr 192.168.0.1 rport 5001",
false,
},
{
mustCandidateHost(&CandidateHostConfig{
Network: NetworkTypeTCP4.String(),
Address: "192.168.0.196",
Port: 0,
TCPType: TCPTypeActive,
}),
"1052353102 1 tcp 2128609279 192.168.0.196 0 typ host tcptype active",
false,
},
{
mustCandidateHost(&CandidateHostConfig{
Network: NetworkTypeUDP4.String(),
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
{
mustCandidateHost(&CandidateHostConfig{
Network: NetworkTypeUDP4.String(),
Address: localhostIPStr,
Port: 80,
Priority: 500,
Foundation: " ",
}),
" 1 udp 500 " + localhostIPStr + " 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},
} {
t.Run(strconv.Itoa(idx), func(t *testing.T) {
actualCandidate, err := UnmarshalCandidate(test.marshaled)
if test.expectError {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Truef(t, test.candidate.Equal(actualCandidate), "%s != %s", test.candidate.String(), actualCandidate.String())
require.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)
require.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)
require.Error(t, err, "writing to closed conn")
}
func TestMarshalUnmarshalCandidateWithZoneID(t *testing.T) {
candidateWithZoneID := mustCandidateHost(&CandidateHostConfig{
Network: NetworkTypeUDP6.String(),
Address: "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a%Local Connection",
Port: 53987,
Priority: 500,
Foundation: "750",
})
candidateStr := "750 0 udp 500 fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a 53987 typ host"
require.Equal(t, candidateStr, candidateWithZoneID.Marshal())
candidate := mustCandidateHost(&CandidateHostConfig{
Network: NetworkTypeUDP6.String(),
Address: "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a",
Port: 53987,
Priority: 500,
Foundation: "750",
})
candidateWithZoneIDStr := "750 0 udp 500 fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a%eth0 53987 typ host"
candidate2, err := UnmarshalCandidate(candidateWithZoneIDStr)
require.NoError(t, err)
require.Truef(t, candidate.Equal(candidate2), "%s != %s", candidate.String(), candidate2.String())
candidateWithZoneIDStr2 := "750 0 udp 500 fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a%eth0%eth1 53987 typ host"
candidate2, err = UnmarshalCandidate(candidateWithZoneIDStr2)
require.NoError(t, err)
require.Truef(t, candidate.Equal(candidate2), "%s != %s", candidate.String(), candidate2.String())
}