This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
pair_test.go
159 lines (154 loc) · 2.94 KB
/
pair_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
package ice
import (
"fmt"
"net"
"sort"
"testing"
)
func TestPairPriority(t *testing.T) {
for _, tc := range []struct {
G, D int
Value int64
}{
{0, 0, 0},
{1, 1, 4294967298},
{1, 2, 4294967300},
{2, 1, 4294967301},
} {
t.Run(fmt.Sprintf("%d_%d", tc.G, tc.D), func(t *testing.T) {
if v := PairPriority(tc.G, tc.D); v != tc.Value {
t.Errorf("%d (got) != %d (expected)", v, tc.Value)
}
})
}
}
func TestPair_Foundation(t *testing.T) {
p := Pair{
Local: Candidate{
Foundation: make([]byte, foundationLength),
},
Remote: Candidate{
Foundation: make([]byte, foundationLength),
},
}
p.SetFoundation()
f := p.Foundation
if len(f) != foundationLength*2 {
t.Error("bad length")
}
}
func TestPairs(t *testing.T) {
pairs := Pairs{
{Priority: 4},
{Priority: 3},
{Priority: 100},
{Priority: 0, ComponentID: 2},
{Priority: 0, ComponentID: 1},
{Priority: 4},
{Priority: 5},
{Priority: 9},
{Priority: 8},
}
sort.Sort(pairs)
expectedOrder := []struct {
priority int64
component int
}{
{100, 0},
{9, 0},
{8, 0},
{5, 0},
{4, 0},
{4, 0},
{3, 0},
{0, 1},
{0, 2},
}
for i, p := range pairs {
if p.Priority != expectedOrder[i].priority {
t.Errorf("p[%d]: %d (got) != %d (expected)", i, p.Priority, expectedOrder[i])
}
if p.ComponentID != expectedOrder[i].component {
t.Errorf("p[%d] component: %d (got) != %d (expected)", i, p.Priority, expectedOrder[i])
}
}
}
func TestNewPairs(t *testing.T) {
for _, tc := range []struct {
Name string
Local Candidates
Remote Candidates
Result Pairs
}{
{
Name: "Blank",
},
{
Name: "No pairs",
Local: Candidates{
{
Addr: Addr{
IP: net.ParseIP("1.1.1.1"),
},
},
},
Remote: Candidates{
{
Addr: Addr{
IP: net.ParseIP("2001:11:12:13:14:15:16:17"),
},
},
},
},
{
Name: "Simple",
Local: Candidates{
{
Addr: Addr{
IP: net.ParseIP("1.1.1.1"),
},
},
},
Remote: Candidates{
{
Addr: Addr{
IP: net.ParseIP("1.1.1.2"),
},
},
},
Result: Pairs{
{
Local: Candidate{
Addr: Addr{
IP: net.ParseIP("1.1.1.1"),
},
},
Remote: Candidate{
Addr: Addr{
IP: net.ParseIP("1.1.1.2"),
},
},
},
},
},
} {
t.Run(tc.Name, func(t *testing.T) {
got := NewPairs(tc.Local, tc.Remote)
if len(got) != len(tc.Result) {
t.Fatalf("bad length: %d (got) != %d (expected)", len(got), len(tc.Result))
}
for i := range tc.Result {
expectedAddr := tc.Result[i].Remote.Addr
gotAddr := got[i].Remote.Addr
if !gotAddr.Equal(expectedAddr) {
t.Errorf("[%d]: remote addr mismatch: %s (got) != %s (expected)", i, gotAddr, expectedAddr)
}
expectedAddr = tc.Result[i].Local.Addr
gotAddr = got[i].Local.Addr
if !gotAddr.Equal(expectedAddr) {
t.Errorf("[%d]: local addr mismatch: %s (got) != %s (expected)", i, gotAddr, expectedAddr)
}
}
})
}
}