@@ -8,7 +8,6 @@ package device
8
8
import (
9
9
"bytes"
10
10
"encoding/hex"
11
- "errors"
12
11
"fmt"
13
12
"io"
14
13
"math/rand"
@@ -17,11 +16,11 @@ import (
17
16
"runtime/pprof"
18
17
"sync"
19
18
"sync/atomic"
20
- "syscall"
21
19
"testing"
22
20
"time"
23
21
24
22
"golang.zx2c4.com/wireguard/conn"
23
+ "golang.zx2c4.com/wireguard/conn/bindtest"
25
24
"golang.zx2c4.com/wireguard/tun/tuntest"
26
25
)
27
26
@@ -148,8 +147,14 @@ func (pair *testPair) Send(tb testing.TB, ping SendDirection, done chan struct{}
148
147
}
149
148
150
149
// genTestPair creates a testPair.
151
- func genTestPair (tb testing.TB ) (pair testPair ) {
150
+ func genTestPair (tb testing.TB , realSocket bool ) (pair testPair ) {
152
151
cfg , endpointCfg := genConfigs (tb )
152
+ var binds [2 ]conn.Bind
153
+ if realSocket {
154
+ binds [0 ], binds [1 ] = conn .NewDefaultBind (), conn .NewDefaultBind ()
155
+ } else {
156
+ binds = bindtest .NewChannelBinds ()
157
+ }
153
158
// Bring up a ChannelTun for each config.
154
159
for i := range pair {
155
160
p := & pair [i ]
@@ -159,7 +164,7 @@ func genTestPair(tb testing.TB) (pair testPair) {
159
164
if _ , ok := tb .(* testing.B ); ok && ! testing .Verbose () {
160
165
level = LogLevelError
161
166
}
162
- p .dev = NewDevice (p .tun .TUN (), conn . NewDefaultBind () , NewLogger (level , fmt .Sprintf ("dev%d: " , i )))
167
+ p .dev = NewDevice (p .tun .TUN (), binds [ i ] , NewLogger (level , fmt .Sprintf ("dev%d: " , i )))
163
168
if err := p .dev .IpcSet (cfg [i ]); err != nil {
164
169
tb .Errorf ("failed to configure device %d: %v" , i , err )
165
170
p .dev .Close ()
@@ -187,7 +192,7 @@ func genTestPair(tb testing.TB) (pair testPair) {
187
192
188
193
func TestTwoDevicePing (t * testing.T ) {
189
194
goroutineLeakCheck (t )
190
- pair := genTestPair (t )
195
+ pair := genTestPair (t , true )
191
196
t .Run ("ping 1.0.0.1" , func (t * testing.T ) {
192
197
pair .Send (t , Ping , nil )
193
198
})
@@ -198,11 +203,11 @@ func TestTwoDevicePing(t *testing.T) {
198
203
199
204
func TestUpDown (t * testing.T ) {
200
205
goroutineLeakCheck (t )
201
- const itrials = 20
202
- const otrials = 1
206
+ const itrials = 50
207
+ const otrials = 10
203
208
204
209
for n := 0 ; n < otrials ; n ++ {
205
- pair := genTestPair (t )
210
+ pair := genTestPair (t , false )
206
211
for i := range pair {
207
212
for k := range pair [i ].dev .peers .keyMap {
208
213
pair [i ].dev .IpcSet (fmt .Sprintf ("public_key=%s\n persistent_keepalive_interval=1\n " , hex .EncodeToString (k [:])))
@@ -214,17 +219,8 @@ func TestUpDown(t *testing.T) {
214
219
go func (d * Device ) {
215
220
defer wg .Done ()
216
221
for i := 0 ; i < itrials ; i ++ {
217
- start := time .Now ()
218
- for {
219
- if err := d .Up (); err != nil {
220
- if errors .Is (err , syscall .EADDRINUSE ) && time .Now ().Sub (start ) < time .Second * 4 {
221
- // Some other test process is racing with us, so try again.
222
- time .Sleep (time .Millisecond * 10 )
223
- continue
224
- }
225
- t .Errorf ("failed up bring up device: %v" , err )
226
- }
227
- break
222
+ if err := d .Up (); err != nil {
223
+ t .Errorf ("failed up bring up device: %v" , err )
228
224
}
229
225
time .Sleep (time .Duration (rand .Intn (int (time .Nanosecond * (0x10000 - 1 )))))
230
226
if err := d .Down (); err != nil {
@@ -245,7 +241,7 @@ func TestUpDown(t *testing.T) {
245
241
// TestConcurrencySafety does other things concurrently with tunnel use.
246
242
// It is intended to be used with the race detector to catch data races.
247
243
func TestConcurrencySafety (t * testing.T ) {
248
- pair := genTestPair (t )
244
+ pair := genTestPair (t , true )
249
245
done := make (chan struct {})
250
246
251
247
const warmupIters = 10
@@ -315,7 +311,7 @@ func TestConcurrencySafety(t *testing.T) {
315
311
}
316
312
317
313
func BenchmarkLatency (b * testing.B ) {
318
- pair := genTestPair (b )
314
+ pair := genTestPair (b , true )
319
315
320
316
// Establish a connection.
321
317
pair .Send (b , Ping , nil )
@@ -329,7 +325,7 @@ func BenchmarkLatency(b *testing.B) {
329
325
}
330
326
331
327
func BenchmarkThroughput (b * testing.B ) {
332
- pair := genTestPair (b )
328
+ pair := genTestPair (b , true )
333
329
334
330
// Establish a connection.
335
331
pair .Send (b , Ping , nil )
@@ -373,7 +369,7 @@ func BenchmarkThroughput(b *testing.B) {
373
369
}
374
370
375
371
func BenchmarkUAPIGet (b * testing.B ) {
376
- pair := genTestPair (b )
372
+ pair := genTestPair (b , true )
377
373
pair .Send (b , Ping , nil )
378
374
pair .Send (b , Pong , nil )
379
375
b .ReportAllocs ()
0 commit comments