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
|
//go:build !js
// +build !js
package allocation
import (
"io"
"math/rand"
"net"
"strings"
"testing"
"time"
"github.com/pion/logging"
"github.com/pion/turn/v2/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")
if err != nil {
panic(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) {
m, err := newTestManager()
assert.NoError(t, err)
if a, err := m.CreateAllocation(nil, turnSocket, 0, proto.DefaultLifetime); a != nil || err == nil {
t.Errorf("Illegally created allocation with nil FiveTuple")
}
if a, err := m.CreateAllocation(randomFiveTuple(), nil, 0, proto.DefaultLifetime); a != nil || err == nil {
t.Errorf("Illegally created allocation with nil turnSocket")
}
if a, err := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, 0); a != nil || err == nil {
t.Errorf("Illegally created allocation with 0 lifetime")
}
}
// test valid Allocation creations
func subTestCreateAllocation(t *testing.T, turnSocket net.PacketConn) {
m, err := newTestManager()
assert.NoError(t, err)
fiveTuple := randomFiveTuple()
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime); 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) {
m, err := newTestManager()
assert.NoError(t, err)
fiveTuple := randomFiveTuple()
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime); a == nil || err != nil {
t.Errorf("Failed to create allocation %v %v", a, err)
}
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime); a != nil || err == nil {
t.Errorf("Was able to create allocation with same FiveTuple twice")
}
}
func subTestDeleteAllocation(t *testing.T, turnSocket net.PacketConn) {
m, err := newTestManager()
assert.NoError(t, err)
fiveTuple := randomFiveTuple()
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime); 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) {
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)
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) {
m, err := newTestManager()
assert.NoError(t, err)
allocations := make([]*Allocation, 2)
a1, _ := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, time.Second)
allocations[0] = a1
a2, _ := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, time.Minute)
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 {
/* #nosec */
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(network string, requestedPort 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(network string, requestedPort 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, turnSocket net.PacketConn) {
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)
}
|