-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathallocation_manager_test.go
More file actions
212 lines (164 loc) · 5.98 KB
/
allocation_manager_test.go
File metadata and controls
212 lines (164 loc) · 5.98 KB
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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
package allocation
import (
"io"
"math/rand"
"net"
"strings"
"testing"
"time"
"github.com/pion/logging"
"github.com/pion/turn/v4/internal/proto"
"github.com/stretchr/testify/assert"
)
func TestManager(t *testing.T) {
tt := []struct {
name string
f func(*testing.T, net.PacketConn)
}{
{"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")
assert.NoError(t, err)
for _, tc := range tt {
f := tc.f
t.Run(tc.name, func(t *testing.T) {
f(t, turnSocket)
})
}
}
// Test invalid Allocation creations.
func subTestCreateInvalidAllocation(t *testing.T, turnSocket net.PacketConn) {
t.Helper()
m, err := newTestManager()
assert.NoError(t, err)
a, err := m.CreateAllocation(nil, turnSocket, 0, proto.DefaultLifetime)
assert.Nil(t, a, "Illegally created allocation with nil FiveTuple")
assert.Error(t, err, "Illegally created allocation with nil FiveTuple")
a, err = m.CreateAllocation(randomFiveTuple(), nil, 0, proto.DefaultLifetime)
assert.Nil(t, a, "Illegally created allocation with nil turnSocket")
assert.Error(t, err, "Illegally created allocation with nil turnSocket")
a, err = m.CreateAllocation(randomFiveTuple(), turnSocket, 0, 0)
assert.Nil(t, a, "Illegally created allocation with 0 lifetime")
assert.Error(t, err, "Illegally created allocation with 0 lifetime")
}
// Test valid Allocation creations.
func subTestCreateAllocation(t *testing.T, turnSocket net.PacketConn) {
t.Helper()
m, err := newTestManager()
assert.NoError(t, err)
fiveTuple := randomFiveTuple()
a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime)
assert.NotNil(t, a, "Failed to create allocation")
assert.NoError(t, err, "Failed to create allocation")
a = m.GetAllocation(fiveTuple)
assert.NotNil(t, a, "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) {
t.Helper()
m, err := newTestManager()
assert.NoError(t, err)
fiveTuple := randomFiveTuple()
a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime)
assert.NotNil(t, a, "Failed to create allocation")
assert.NoError(t, err, "Failed to create allocation")
a, err = m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime)
assert.Nil(t, a, "Was able to create allocation with same FiveTuple twice")
assert.Error(t, err, "Was able to create allocation with same FiveTuple twice")
}
func subTestDeleteAllocation(t *testing.T, turnSocket net.PacketConn) {
t.Helper()
manager, err := newTestManager()
assert.NoError(t, err)
fiveTuple := randomFiveTuple()
a, err := manager.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime)
assert.NotNil(t, a, "Failed to create allocation")
assert.NoError(t, err, "Failed to create allocation")
a = manager.GetAllocation(fiveTuple)
assert.NotNil(t, a, "Failed to get allocation right after creation")
manager.DeleteAllocation(fiveTuple)
a = manager.GetAllocation(fiveTuple)
assert.Nilf(t, a, "Failed to delete allocation %v", fiveTuple)
}
// Test that allocation should be closed if timeout.
func subTestAllocationTimeout(t *testing.T, turnSocket net.PacketConn) {
t.Helper()
m, err := newTestManager()
assert.NoError(t, err)
allocations := make([]*Allocation, 5)
lifetime := time.Second
for index := range allocations {
fiveTuple := randomFiveTuple()
a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, lifetime)
assert.NoErrorf(t, err, "Failed to create allocation with %v", fiveTuple)
allocations[index] = a
}
// Make sure all allocations timeout
time.Sleep(lifetime + time.Second)
for _, alloc := range allocations {
assert.True(t, isClose(alloc.RelaySocket), "Allocation relay socket should be closed if lifetime timeout")
}
}
// Test for manager close.
func subTestManagerClose(t *testing.T, turnSocket net.PacketConn) {
t.Helper()
manager, err := newTestManager()
assert.NoError(t, err)
allocations := make([]*Allocation, 2)
a1, _ := manager.CreateAllocation(randomFiveTuple(), turnSocket, 0, time.Second)
allocations[0] = a1
a2, _ := manager.CreateAllocation(randomFiveTuple(), turnSocket, 0, time.Minute)
allocations[1] = a2
// Make a1 timeout
time.Sleep(2 * time.Second)
assert.NoError(t, manager.Close())
for _, alloc := range allocations {
assert.True(t, isClose(alloc.RelaySocket), "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() (*Manager, error) {
loggerFactory := logging.NewDefaultLoggerFactory()
config := ManagerConfig{
LeveledLogger: loggerFactory.NewLogger("test"),
AllocatePacketConn: func(string, int) (net.PacketConn, net.Addr, error) {
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) (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) {
t.Helper()
m, err := newTestManager()
assert.NoError(t, err)
port, err := m.GetRandomEvenPort()
assert.NoError(t, err)
assert.True(t, port > 0)
assert.True(t, port%2 == 0)
}