-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_test.go
88 lines (84 loc) · 1.72 KB
/
agent_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
package ice
import (
"net"
"sort"
"testing"
"github.com/gortc/ice/candidate"
)
func newUDPCandidate(t *testing.T, addr HostAddr) (Candidate, func()) {
t.Helper()
zeroPort := net.UDPAddr{
IP: addr.IP,
Port: 0,
}
l, err := net.ListenPacket("udp", zeroPort.String())
if err != nil {
t.Fatal(err)
}
f := func() {
if cErr := l.Close(); cErr != nil {
t.Error(cErr)
}
}
a := l.LocalAddr().(*net.UDPAddr)
c := Candidate{
Base: Addr{
IP: addr.IP,
Port: a.Port,
Proto: candidate.UDP,
},
Type: candidate.Host,
Addr: Addr{
IP: addr.IP,
Port: a.Port,
Proto: candidate.UDP,
},
ComponentID: 1,
}
c.Foundation = Foundation(&c, Addr{})
c.Priority = Priority(TypePreference(c.Type),
addr.LocalPreference, c.ComponentID,
)
return c, f
}
func TestAgentAPI(t *testing.T) {
// 0) Gather interfaces.
addr, err := Gather()
if err != nil {
t.Fatal(err)
}
hostAddr, err := HostAddresses(addr)
if err != nil {
t.Error(err)
}
t.Logf("got host candidates: %d", len(hostAddr))
for _, a := range hostAddr {
t.Logf(" %s (%d)", a.IP, a.LocalPreference)
}
var toClose []func()
defer func() {
for _, f := range toClose {
f()
}
}()
var local, remote Candidates
for _, a := range hostAddr {
l, f := newUDPCandidate(t, a)
toClose = append(toClose, f)
local = append(local, l)
r, fRem := newUDPCandidate(t, a)
remote = append(remote, r)
toClose = append(toClose, fRem)
}
sort.Sort(local)
sort.Sort(remote)
list := new(Checklist)
list.Pairs = NewPairs(local, remote)
list.ComputePriorities(Controlling)
list.Order()
list.Prune()
t.Logf("got %d pairs", len(list.Pairs))
for _, p := range list.Pairs {
t.Logf("%s -> %s [%x]", p.Local.Addr, p.Remote.Addr, p.Foundation())
}
}