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 53
/
Copy pathaddr_test.go
146 lines (140 loc) · 3.16 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
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
package stun
import (
"io"
"net"
"testing"
)
func TestMappedAddress(t *testing.T) {
m := new(Message)
addr := &MappedAddress{
IP: net.ParseIP("122.12.34.5"),
Port: 5412,
}
if addr.String() != "122.12.34.5:5412" {
t.Error("bad string", addr)
}
t.Run("Bad length", func(t *testing.T) {
badAddr := &MappedAddress{
IP: net.IP{1, 2, 3},
}
if err := badAddr.AddTo(m); err == nil {
t.Error("should error")
}
})
t.Run("AddTo", func(t *testing.T) {
if err := addr.AddTo(m); err != nil {
t.Error(err)
}
t.Run("GetFrom", func(t *testing.T) {
got := new(MappedAddress)
if err := got.GetFrom(m); err != nil {
t.Error(err)
}
if !got.IP.Equal(addr.IP) {
t.Error("got bad IP: ", got.IP)
}
t.Run("Not found", func(t *testing.T) {
message := new(Message)
if err := got.GetFrom(message); err != ErrAttributeNotFound {
t.Error("should be not found: ", err)
}
})
t.Run("Bad family", func(t *testing.T) {
v, _ := m.Attributes.Get(AttrMappedAddress)
v.Value[0] = 32
if err := got.GetFrom(m); err == nil {
t.Error("should error")
}
})
t.Run("Bad length", func(t *testing.T) {
message := new(Message)
message.Add(AttrMappedAddress, []byte{1, 2, 3})
if err := got.GetFrom(message); err != io.ErrUnexpectedEOF {
t.Errorf("<%s> should be <%s>", err, io.ErrUnexpectedEOF)
}
})
})
})
}
func TestMappedAddressV6(t *testing.T) {
m := new(Message)
addr := &MappedAddress{
IP: net.ParseIP("::"),
Port: 5412,
}
t.Run("AddTo", func(t *testing.T) {
if err := addr.AddTo(m); err != nil {
t.Error(err)
}
t.Run("GetFrom", func(t *testing.T) {
got := new(MappedAddress)
if err := got.GetFrom(m); err != nil {
t.Error(err)
}
if !got.IP.Equal(addr.IP) {
t.Error("got bad IP: ", got.IP)
}
t.Run("Not found", func(t *testing.T) {
message := new(Message)
if err := got.GetFrom(message); err != ErrAttributeNotFound {
t.Error("should be not found: ", err)
}
})
})
})
}
func TestAlternateServer(t *testing.T) {
m := new(Message)
addr := &AlternateServer{
IP: net.ParseIP("122.12.34.5"),
Port: 5412,
}
t.Run("AddTo", func(t *testing.T) {
if err := addr.AddTo(m); err != nil {
t.Error(err)
}
t.Run("GetFrom", func(t *testing.T) {
got := new(AlternateServer)
if err := got.GetFrom(m); err != nil {
t.Error(err)
}
if !got.IP.Equal(addr.IP) {
t.Error("got bad IP: ", got.IP)
}
t.Run("Not found", func(t *testing.T) {
message := new(Message)
if err := got.GetFrom(message); err != ErrAttributeNotFound {
t.Error("should be not found: ", err)
}
})
})
})
}
func BenchmarkMappedAddress_AddTo(b *testing.B) {
m := new(Message)
b.ReportAllocs()
addr := &MappedAddress{
IP: net.ParseIP("122.12.34.5"),
Port: 5412,
}
for i := 0; i < b.N; i++ {
if err := addr.AddTo(m); err != nil {
b.Fatal(err)
}
m.Reset()
}
}
func BenchmarkAlternateServer_AddTo(b *testing.B) {
m := new(Message)
b.ReportAllocs()
addr := &AlternateServer{
IP: net.ParseIP("122.12.34.5"),
Port: 5412,
}
for i := 0; i < b.N; i++ {
if err := addr.AddTo(m); err != nil {
b.Fatal(err)
}
m.Reset()
}
}