-
Notifications
You must be signed in to change notification settings - Fork 341
/
Copy pathallocation_manager_test.go
226 lines (188 loc) · 6.91 KB
/
allocation_manager_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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
package allocation
import (
"errors"
"io"
"math/rand"
"net"
"strings"
"testing"
"time"
"github.com/pion/logging"
"github.com/pion/turn/v4/internal/proto"
"github.com/stretchr/testify/assert"
)
var (
errUnexpectedTestRealm = errors.New("unexpected test realm")
errUnexpectedTestUsername = errors.New("unexpected user name")
)
func TestManager(t *testing.T) {
tt := []struct {
name string
f func(*testing.T, net.PacketConn, string, string)
}{
{"CreateInvalidAllocation", subTestCreateInvalidAllocation},
{"CreateAllocation", subTestCreateAllocation},
{"CreateAllocationDuplicateFiveTuple", subTestCreateAllocationDuplicateFiveTuple},
{"DeleteAllocation", subTestDeleteAllocation},
{"AllocationTimeout", subTestAllocationTimeout},
{"Close", subTestManagerClose},
{"GetRandomEvenPort", subTestGetRandomEvenPort},
}
network := "udp4"
turnSocket, err := net.ListenPacket(network, "0.0.0.0:0")
if err != nil {
panic(err)
}
for _, tc := range tt {
f := tc.f
t.Run(tc.name, func(t *testing.T) {
f(t, turnSocket, "test_realm_1", "test_user_1")
})
}
}
// Test invalid Allocation creations
func subTestCreateInvalidAllocation(t *testing.T, turnSocket net.PacketConn, realm, username string) {
m, err := newTestManager(realm, username)
assert.NoError(t, err)
metadata := Metadata{Realm: realm, Username: username}
if a, err := m.CreateAllocation(nil, turnSocket, 0, proto.DefaultLifetime, metadata); a != nil || err == nil {
t.Errorf("Illegally created allocation with nil FiveTuple")
}
if a, err := m.CreateAllocation(randomFiveTuple(), nil, 0, proto.DefaultLifetime, metadata); a != nil || err == nil {
t.Errorf("Illegally created allocation with nil turnSocket")
}
if a, err := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, 0, metadata); a != nil || err == nil {
t.Errorf("Illegally created allocation with 0 lifetime")
}
}
// Test valid Allocation creations
func subTestCreateAllocation(t *testing.T, turnSocket net.PacketConn, realm, username string) {
m, err := newTestManager(realm, username)
assert.NoError(t, err)
fiveTuple := randomFiveTuple()
metadata := Metadata{Realm: realm, Username: username}
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime, metadata); a == nil || err != nil {
t.Errorf("Failed to create allocation %v %v", a, err)
}
if a := m.GetAllocation(fiveTuple); a == nil {
t.Errorf("Failed to get allocation right after creation")
}
}
// Test that two allocations can't be created with the same FiveTuple
func subTestCreateAllocationDuplicateFiveTuple(t *testing.T, turnSocket net.PacketConn, realm, username string) {
m, err := newTestManager(realm, username)
assert.NoError(t, err)
fiveTuple := randomFiveTuple()
metadata := Metadata{Realm: realm, Username: username}
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime, metadata); a == nil || err != nil {
t.Errorf("Failed to create allocation %v %v", a, err)
}
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime, metadata); a != nil || err == nil {
t.Errorf("Was able to create allocation with same FiveTuple twice")
}
}
func subTestDeleteAllocation(t *testing.T, turnSocket net.PacketConn, realm, username string) {
m, err := newTestManager(realm, username)
assert.NoError(t, err)
fiveTuple := randomFiveTuple()
metadata := Metadata{Realm: realm, Username: username}
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime, metadata); a == nil || err != nil {
t.Errorf("Failed to create allocation %v %v", a, err)
}
if a := m.GetAllocation(fiveTuple); a == nil {
t.Errorf("Failed to get allocation right after creation")
}
m.DeleteAllocation(fiveTuple)
if a := m.GetAllocation(fiveTuple); a != nil {
t.Errorf("Get allocation with %v should be nil after delete", fiveTuple)
}
}
// Test that allocation should be closed if timeout
func subTestAllocationTimeout(t *testing.T, turnSocket net.PacketConn, realm, username string) {
m, err := newTestManager(realm, username)
assert.NoError(t, err)
allocations := make([]*Allocation, 5)
lifetime := time.Second
metadata := Metadata{Realm: realm, Username: username}
for index := range allocations {
fiveTuple := randomFiveTuple()
a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, lifetime, metadata)
if err != nil {
t.Errorf("Failed to create allocation with %v", fiveTuple)
}
allocations[index] = a
}
// Make sure all allocations timeout
time.Sleep(lifetime + time.Second)
for _, alloc := range allocations {
if !isClose(alloc.RelaySocket) {
t.Error("Allocation relay socket should be closed if lifetime timeout")
}
}
}
// Test for manager close
func subTestManagerClose(t *testing.T, turnSocket net.PacketConn, realm, username string) {
m, err := newTestManager(realm, username)
assert.NoError(t, err)
allocations := make([]*Allocation, 2)
metadata := Metadata{Realm: realm, Username: username}
a1, _ := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, time.Second, metadata)
allocations[0] = a1
a2, _ := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, time.Minute, metadata)
allocations[1] = a2
// Make a1 timeout
time.Sleep(2 * time.Second)
if err := m.Close(); err != nil {
t.Errorf("Manager close with error: %v", err)
}
for _, alloc := range allocations {
if !isClose(alloc.RelaySocket) {
t.Error("Manager's allocations should be closed")
}
}
}
func randomFiveTuple() *FiveTuple {
// nolint
return &FiveTuple{
SrcAddr: &net.UDPAddr{IP: nil, Port: rand.Int()},
DstAddr: &net.UDPAddr{IP: nil, Port: rand.Int()},
}
}
func newTestManager(expectedRealm, expectedUsername string) (*Manager, error) {
loggerFactory := logging.NewDefaultLoggerFactory()
config := ManagerConfig{
LeveledLogger: loggerFactory.NewLogger("test"),
AllocatePacketConn: func(_ string, _ int, metadata Metadata) (net.PacketConn, net.Addr, error) {
if metadata.Realm != expectedRealm {
return nil, nil, errUnexpectedTestRealm
}
if metadata.Username != expectedUsername {
return nil, nil, errUnexpectedTestUsername
}
conn, err := net.ListenPacket("udp4", "0.0.0.0:0")
if err != nil {
return nil, nil, err
}
return conn, conn.LocalAddr(), nil
},
AllocateConn: func(string, int, Metadata) (net.Conn, net.Addr, error) { return nil, nil, nil },
}
return NewManager(config)
}
func isClose(conn io.Closer) bool {
closeErr := conn.Close()
return closeErr != nil && strings.Contains(closeErr.Error(), "use of closed network connection")
}
func subTestGetRandomEvenPort(t *testing.T, _ net.PacketConn, realm, username string) {
m, err := newTestManager(realm, username)
assert.NoError(t, err)
metadata := Metadata{Realm: realm, Username: username}
port, err := m.GetRandomEvenPort(metadata)
assert.NoError(t, err)
assert.True(t, port > 0)
assert.True(t, port%2 == 0)
}