This repository has been archived by the owner on Jun 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
addr_test.go
99 lines (94 loc) · 1.49 KB
/
addr_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
package turn
import (
"fmt"
"net"
"testing"
)
func TestAddr_FromUDPAddr(t *testing.T) {
u := &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 1234,
}
a := new(Addr)
a.FromUDPAddr(u)
if !u.IP.Equal(a.IP) || u.Port != a.Port || u.String() != a.String() {
t.Error("not equal")
}
if a.Network() != "turn" {
t.Error("unexpected network")
}
}
func TestAddr_EqualIP(t *testing.T) {
a := Addr{
IP: net.IPv4(127, 0, 0, 1),
Port: 1337,
}
b := Addr{
IP: net.IPv4(127, 0, 0, 1),
Port: 1338,
}
if a.Equal(b) {
t.Error("a != b")
}
if !a.EqualIP(b) {
t.Error("a.IP should equal to b.IP")
}
}
func TestFiveTuple_Equal(t *testing.T) {
for _, tc := range []struct {
name string
a, b FiveTuple
v bool
}{
{
name: "blank",
v: true,
},
{
name: "proto",
a: FiveTuple{
Proto: ProtoUDP,
},
},
{
name: "server",
a: FiveTuple{
Server: Addr{
Port: 100,
},
},
},
{
name: "client",
a: FiveTuple{
Client: Addr{
Port: 100,
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
if v := tc.a.Equal(tc.b); v != tc.v {
t.Errorf("%s [%v!=%v] %s",
tc.a, v, tc.v, tc.b,
)
}
})
}
}
func TestFiveTuple_String(t *testing.T) {
s := fmt.Sprint(FiveTuple{
Proto: ProtoUDP,
Server: Addr{
Port: 100,
IP: net.IPv4(127, 0, 0, 1),
},
Client: Addr{
Port: 200,
IP: net.IPv4(127, 0, 0, 1),
},
})
if s != "127.0.0.1:200->127.0.0.1:100 (UDP)" {
t.Error("unexpected stringer output")
}
}