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