7
7
package allocation
8
8
9
9
import (
10
+ "errors"
10
11
"io"
11
12
"math/rand"
12
13
"net"
@@ -19,10 +20,12 @@ import (
19
20
"github.com/stretchr/testify/assert"
20
21
)
21
22
23
+ var errUnexpectedTestUsername = errors .New ("unexpected user name" )
24
+
22
25
func TestManager (t * testing.T ) {
23
26
tt := []struct {
24
27
name string
25
- f func (* testing.T , net.PacketConn )
28
+ f func (* testing.T , net.PacketConn , string )
26
29
}{
27
30
{"CreateInvalidAllocation" , subTestCreateInvalidAllocation },
28
31
{"CreateAllocation" , subTestCreateAllocation },
@@ -42,34 +45,34 @@ func TestManager(t *testing.T) {
42
45
for _ , tc := range tt {
43
46
f := tc .f
44
47
t .Run (tc .name , func (t * testing.T ) {
45
- f (t , turnSocket )
48
+ f (t , turnSocket , "test_user_1" )
46
49
})
47
50
}
48
51
}
49
52
50
53
// Test invalid Allocation creations
51
- func subTestCreateInvalidAllocation (t * testing.T , turnSocket net.PacketConn ) {
52
- m , err := newTestManager ()
54
+ func subTestCreateInvalidAllocation (t * testing.T , turnSocket net.PacketConn , username string ) {
55
+ m , err := newTestManager (username )
53
56
assert .NoError (t , err )
54
57
55
- if a , err := m .CreateAllocation (nil , turnSocket , 0 , proto .DefaultLifetime ); a != nil || err == nil {
58
+ if a , err := m .CreateAllocation (nil , turnSocket , 0 , proto .DefaultLifetime , username ); a != nil || err == nil {
56
59
t .Errorf ("Illegally created allocation with nil FiveTuple" )
57
60
}
58
- if a , err := m .CreateAllocation (randomFiveTuple (), nil , 0 , proto .DefaultLifetime ); a != nil || err == nil {
61
+ if a , err := m .CreateAllocation (randomFiveTuple (), nil , 0 , proto .DefaultLifetime , username ); a != nil || err == nil {
59
62
t .Errorf ("Illegally created allocation with nil turnSocket" )
60
63
}
61
- if a , err := m .CreateAllocation (randomFiveTuple (), turnSocket , 0 , 0 ); a != nil || err == nil {
64
+ if a , err := m .CreateAllocation (randomFiveTuple (), turnSocket , 0 , 0 , username ); a != nil || err == nil {
62
65
t .Errorf ("Illegally created allocation with 0 lifetime" )
63
66
}
64
67
}
65
68
66
69
// Test valid Allocation creations
67
- func subTestCreateAllocation (t * testing.T , turnSocket net.PacketConn ) {
68
- m , err := newTestManager ()
70
+ func subTestCreateAllocation (t * testing.T , turnSocket net.PacketConn , username string ) {
71
+ m , err := newTestManager (username )
69
72
assert .NoError (t , err )
70
73
71
74
fiveTuple := randomFiveTuple ()
72
- if a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime ); a == nil || err != nil {
75
+ if a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime , username ); a == nil || err != nil {
73
76
t .Errorf ("Failed to create allocation %v %v" , a , err )
74
77
}
75
78
@@ -79,26 +82,26 @@ func subTestCreateAllocation(t *testing.T, turnSocket net.PacketConn) {
79
82
}
80
83
81
84
// Test that two allocations can't be created with the same FiveTuple
82
- func subTestCreateAllocationDuplicateFiveTuple (t * testing.T , turnSocket net.PacketConn ) {
83
- m , err := newTestManager ()
85
+ func subTestCreateAllocationDuplicateFiveTuple (t * testing.T , turnSocket net.PacketConn , username string ) {
86
+ m , err := newTestManager (username )
84
87
assert .NoError (t , err )
85
88
86
89
fiveTuple := randomFiveTuple ()
87
- if a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime ); a == nil || err != nil {
90
+ if a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime , username ); a == nil || err != nil {
88
91
t .Errorf ("Failed to create allocation %v %v" , a , err )
89
92
}
90
93
91
- if a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime ); a != nil || err == nil {
94
+ if a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime , username ); a != nil || err == nil {
92
95
t .Errorf ("Was able to create allocation with same FiveTuple twice" )
93
96
}
94
97
}
95
98
96
- func subTestDeleteAllocation (t * testing.T , turnSocket net.PacketConn ) {
97
- m , err := newTestManager ()
99
+ func subTestDeleteAllocation (t * testing.T , turnSocket net.PacketConn , username string ) {
100
+ m , err := newTestManager (username )
98
101
assert .NoError (t , err )
99
102
100
103
fiveTuple := randomFiveTuple ()
101
- if a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime ); a == nil || err != nil {
104
+ if a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime , username ); a == nil || err != nil {
102
105
t .Errorf ("Failed to create allocation %v %v" , a , err )
103
106
}
104
107
@@ -113,8 +116,8 @@ func subTestDeleteAllocation(t *testing.T, turnSocket net.PacketConn) {
113
116
}
114
117
115
118
// Test that allocation should be closed if timeout
116
- func subTestAllocationTimeout (t * testing.T , turnSocket net.PacketConn ) {
117
- m , err := newTestManager ()
119
+ func subTestAllocationTimeout (t * testing.T , turnSocket net.PacketConn , username string ) {
120
+ m , err := newTestManager (username )
118
121
assert .NoError (t , err )
119
122
120
123
allocations := make ([]* Allocation , 5 )
@@ -123,7 +126,7 @@ func subTestAllocationTimeout(t *testing.T, turnSocket net.PacketConn) {
123
126
for index := range allocations {
124
127
fiveTuple := randomFiveTuple ()
125
128
126
- a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , lifetime )
129
+ a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , lifetime , username )
127
130
if err != nil {
128
131
t .Errorf ("Failed to create allocation with %v" , fiveTuple )
129
132
}
@@ -141,15 +144,15 @@ func subTestAllocationTimeout(t *testing.T, turnSocket net.PacketConn) {
141
144
}
142
145
143
146
// Test for manager close
144
- func subTestManagerClose (t * testing.T , turnSocket net.PacketConn ) {
145
- m , err := newTestManager ()
147
+ func subTestManagerClose (t * testing.T , turnSocket net.PacketConn , username string ) {
148
+ m , err := newTestManager (username )
146
149
assert .NoError (t , err )
147
150
148
151
allocations := make ([]* Allocation , 2 )
149
152
150
- a1 , _ := m .CreateAllocation (randomFiveTuple (), turnSocket , 0 , time .Second )
153
+ a1 , _ := m .CreateAllocation (randomFiveTuple (), turnSocket , 0 , time .Second , username )
151
154
allocations [0 ] = a1
152
- a2 , _ := m .CreateAllocation (randomFiveTuple (), turnSocket , 0 , time .Minute )
155
+ a2 , _ := m .CreateAllocation (randomFiveTuple (), turnSocket , 0 , time .Minute , username )
153
156
allocations [1 ] = a2
154
157
155
158
// Make a1 timeout
@@ -174,21 +177,25 @@ func randomFiveTuple() *FiveTuple {
174
177
}
175
178
}
176
179
177
- func newTestManager () (* Manager , error ) {
180
+ func newTestManager (expectedUsername string ) (* Manager , error ) {
178
181
loggerFactory := logging .NewDefaultLoggerFactory ()
179
182
180
183
config := ManagerConfig {
181
184
LeveledLogger : loggerFactory .NewLogger ("test" ),
182
- AllocatePacketConn : func (string , int ) (net.PacketConn , net.Addr , error ) {
185
+ AllocatePacketConn : func (_ string , _ int , username string ) (net.PacketConn , net.Addr , error ) {
186
+ if username != expectedUsername {
187
+ return nil , nil , errUnexpectedTestUsername
188
+ }
183
189
conn , err := net .ListenPacket ("udp4" , "0.0.0.0:0" )
184
190
if err != nil {
185
191
return nil , nil , err
186
192
}
187
193
188
194
return conn , conn .LocalAddr (), nil
189
195
},
190
- AllocateConn : func (string , int ) (net.Conn , net.Addr , error ) { return nil , nil , nil },
196
+ AllocateConn : func (string , int , string ) (net.Conn , net.Addr , error ) { return nil , nil , nil },
191
197
}
198
+
192
199
return NewManager (config )
193
200
}
194
201
@@ -197,11 +204,11 @@ func isClose(conn io.Closer) bool {
197
204
return closeErr != nil && strings .Contains (closeErr .Error (), "use of closed network connection" )
198
205
}
199
206
200
- func subTestGetRandomEvenPort (t * testing.T , _ net.PacketConn ) {
201
- m , err := newTestManager ()
207
+ func subTestGetRandomEvenPort (t * testing.T , _ net.PacketConn , username string ) {
208
+ m , err := newTestManager (username )
202
209
assert .NoError (t , err )
203
210
204
- port , err := m .GetRandomEvenPort ()
211
+ port , err := m .GetRandomEvenPort (username )
205
212
assert .NoError (t , err )
206
213
assert .True (t , port > 0 )
207
214
assert .True (t , port % 2 == 0 )
0 commit comments