diff --git a/Makefile b/Makefile index 3f6e40759..01f3f63a8 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,9 @@ install: wireguard-go test: go test ./... +benchmark: + go test -bench=. ./... + clean: rm -f wireguard-go diff --git a/conn/errors_default.go b/conn/errors_default.go index f1e5b90e5..f9759ee2d 100644 --- a/conn/errors_default.go +++ b/conn/errors_default.go @@ -7,6 +7,6 @@ package conn -func errShouldDisableUDPGSO(err error) bool { +func errShouldDisableUDPGSO(_ error) bool { return false } diff --git a/conn/features_default.go b/conn/features_default.go index d53ff5f7b..1a98d9086 100644 --- a/conn/features_default.go +++ b/conn/features_default.go @@ -10,6 +10,6 @@ package conn import "net" -func supportsUDPOffload(conn *net.UDPConn) (txOffload, rxOffload bool) { +func supportsUDPOffload(_ *net.UDPConn) (txOffload, rxOffload bool) { return } diff --git a/device/allowedips_rand_test.go b/device/allowedips_rand_test.go index 07065c30a..734686b2c 100644 --- a/device/allowedips_rand_test.go +++ b/device/allowedips_rand_test.go @@ -83,7 +83,7 @@ func TestTrieRandom(t *testing.T) { var peers []*Peer var allowedIPs AllowedIPs - rand.Seed(1) + rng := rand.New(rand.NewSource(1)) for n := 0; n < NumberOfPeers; n++ { peers = append(peers, &Peer{}) @@ -91,14 +91,14 @@ func TestTrieRandom(t *testing.T) { for n := 0; n < NumberOfAddresses; n++ { var addr4 [4]byte - rand.Read(addr4[:]) + rng.Read(addr4[:]) cidr := uint8(rand.Intn(32) + 1) index := rand.Intn(NumberOfPeers) allowedIPs.Insert(netip.PrefixFrom(netip.AddrFrom4(addr4), int(cidr)), peers[index]) slow4 = slow4.Insert(addr4[:], cidr, peers[index]) var addr6 [16]byte - rand.Read(addr6[:]) + rng.Read(addr6[:]) cidr = uint8(rand.Intn(128) + 1) index = rand.Intn(NumberOfPeers) allowedIPs.Insert(netip.PrefixFrom(netip.AddrFrom16(addr6), int(cidr)), peers[index]) @@ -109,7 +109,7 @@ func TestTrieRandom(t *testing.T) { for p = 0; ; p++ { for n := 0; n < NumberOfTests; n++ { var addr4 [4]byte - rand.Read(addr4[:]) + rng.Read(addr4[:]) peer1 := slow4.Lookup(addr4[:]) peer2 := allowedIPs.Lookup(addr4[:]) if peer1 != peer2 { @@ -117,7 +117,7 @@ func TestTrieRandom(t *testing.T) { } var addr6 [16]byte - rand.Read(addr6[:]) + rng.Read(addr6[:]) peer1 = slow6.Lookup(addr6[:]) peer2 = allowedIPs.Lookup(addr6[:]) if peer1 != peer2 { diff --git a/device/allowedips_test.go b/device/allowedips_test.go index cde068ec3..bc13bfc2a 100644 --- a/device/allowedips_test.go +++ b/device/allowedips_test.go @@ -39,12 +39,12 @@ func TestCommonBits(t *testing.T) { } } -func benchmarkTrie(peerNumber, addressNumber, addressLength int, b *testing.B) { +func benchmarkTrie(peerNumber, addressNumber, _ int, b *testing.B) { var trie *trieEntry var peers []*Peer root := parentIndirection{&trie, 2} - rand.Seed(1) + rng := rand.New(rand.NewSource(1)) const AddressLength = 4 @@ -54,15 +54,15 @@ func benchmarkTrie(peerNumber, addressNumber, addressLength int, b *testing.B) { for n := 0; n < addressNumber; n++ { var addr [AddressLength]byte - rand.Read(addr[:]) - cidr := uint8(rand.Uint32() % (AddressLength * 8)) - index := rand.Int() % peerNumber + rng.Read(addr[:]) + cidr := uint8(rng.Uint32() % (AddressLength * 8)) + index := rng.Int() % peerNumber root.insert(addr[:], cidr, peers[index]) } for n := 0; n < b.N; n++ { var addr [AddressLength]byte - rand.Read(addr[:]) + rng.Read(addr[:]) trie.lookup(addr[:]) } } diff --git a/device/sticky_default.go b/device/sticky_default.go index 10382565e..22e1e15b5 100644 --- a/device/sticky_default.go +++ b/device/sticky_default.go @@ -7,6 +7,6 @@ import ( "golang.zx2c4.com/wireguard/rwcancel" ) -func (device *Device) startRouteListener(bind conn.Bind) (*rwcancel.RWCancel, error) { +func (device *Device) startRouteListener(_ conn.Bind) (*rwcancel.RWCancel, error) { return nil, nil } diff --git a/device/sticky_linux.go b/device/sticky_linux.go index 6057ff12a..9a62ec7c9 100644 --- a/device/sticky_linux.go +++ b/device/sticky_linux.go @@ -9,7 +9,7 @@ * * Currently there is no way to achieve this within the net package: * See e.g. https://github.com/golang/go/issues/17930 - * So this code is remains platform dependent. + * So this code remains platform dependent. */ package device @@ -47,7 +47,7 @@ func (device *Device) startRouteListener(bind conn.Bind) (*rwcancel.RWCancel, er return netlinkCancel, nil } -func (device *Device) routineRouteListener(bind conn.Bind, netlinkSock int, netlinkCancel *rwcancel.RWCancel) { +func (device *Device) routineRouteListener(_ conn.Bind, netlinkSock int, netlinkCancel *rwcancel.RWCancel) { type peerEndpointPtr struct { peer *Peer endpoint *conn.Endpoint diff --git a/ipc/namedpipe/namedpipe_test.go b/ipc/namedpipe/namedpipe_test.go index 998453be0..e189bb7e7 100644 --- a/ipc/namedpipe/namedpipe_test.go +++ b/ipc/namedpipe/namedpipe_test.go @@ -43,26 +43,35 @@ func TestPingPong(t *testing.T) { t.Fatalf("unable to listen on pipe: %v", err) } defer listener.Close() + type FormatErr struct { + format string + args any + } + errs := make(chan FormatErr) go func() { incoming, err := listener.Accept() if err != nil { - t.Fatalf("unable to accept pipe connection: %v", err) + errs <- FormatErr{"unable to accept pipe connection: %v", err} } defer incoming.Close() var data [1]byte _, err = incoming.Read(data[:]) if err != nil { - t.Fatalf("unable to read ping from pipe: %v", err) + errs <- FormatErr{"unable to read ping from pipe: %v", err} } if data[0] != ping { - t.Fatalf("expected ping, got %d", data[0]) + errs <- FormatErr{"expected ping, got %d", data[0]} } data[0] = pong _, err = incoming.Write(data[:]) if err != nil { - t.Fatalf("unable to write pong to pipe: %v", err) + errs <- FormatErr{"unable to write pong to pipe: %v", err} } + close(errs) }() + for e := range errs { + t.Fatalf(e.format, e.args) + } client, err := namedpipe.DialTimeout(pipePath, time.Duration(0)) if err != nil { t.Fatalf("unable to dial pipe: %v", err) @@ -402,17 +411,23 @@ func TestTimeoutPendingRead(t *testing.T) { defer l.Close() serverDone := make(chan struct{}) + errs := make(chan error) go func() { s, err := l.Accept() if err != nil { - t.Fatal(err) + errs <- err } time.Sleep(1 * time.Second) s.Close() close(serverDone) + close(errs) }() + for e := range errs { + t.Fatal(e) + } + client, err := namedpipe.DialTimeout(pipePath, time.Duration(0)) if err != nil { t.Fatal(err) @@ -450,17 +465,23 @@ func TestTimeoutPendingWrite(t *testing.T) { defer l.Close() serverDone := make(chan struct{}) + errs := make(chan error) go func() { s, err := l.Accept() if err != nil { - t.Fatal(err) + errs <- err } time.Sleep(1 * time.Second) s.Close() close(serverDone) + close(errs) }() + for e := range errs { + t.Fatal(e) + } + client, err := namedpipe.DialTimeout(pipePath, time.Duration(0)) if err != nil { t.Fatal(err) @@ -506,41 +527,58 @@ func TestEchoWithMessaging(t *testing.T) { listenerDone := make(chan bool) clientDone := make(chan bool) + errs := make(chan error) go func() { // server echo conn, err := l.Accept() if err != nil { - t.Fatal(err) + errs <- err } defer conn.Close() time.Sleep(500 * time.Millisecond) // make *sure* we don't begin to read before eof signal is sent _, err = io.Copy(conn, conn) if err != nil { - t.Fatal(err) + errs <- err } conn.(CloseWriter).CloseWrite() close(listenerDone) + close(errs) }() + for e := range errs { + t.Fatal(e) + } client, err := namedpipe.DialTimeout(pipePath, time.Second) if err != nil { t.Fatal(err) } defer client.Close() + errs = make(chan error) + readErr := make(chan int) + go func() { // client read back bytes := make([]byte, 2) n, e := client.Read(bytes) if e != nil { - t.Fatal(e) + errs <- e } if n != 2 || bytes[0] != 0 || bytes[1] != 1 { - t.Fatalf("expected 2 bytes, got %v", n) + readErr <- n } close(clientDone) + close(errs) + close(readErr) }() + for e := range errs { + t.Fatal(e) + } + for n := range readErr { + t.Fatalf("expected 2 bytes, got %v", n) + } + payload := make([]byte, 2) payload[0] = 0 payload[1] = 1 @@ -564,6 +602,7 @@ func TestConnectRace(t *testing.T) { t.Fatal(err) } defer l.Close() + errs := make(chan error) go func() { for { s, err := l.Accept() @@ -572,12 +611,17 @@ func TestConnectRace(t *testing.T) { } if err != nil { - t.Fatal(err) + errs <- err } s.Close() + close(errs) } }() + for e := range errs { + t.Fatal(e) + } + for i := 0; i < 1000; i++ { c, err := namedpipe.DialTimeout(pipePath, time.Duration(0)) if err != nil { @@ -603,19 +647,25 @@ func TestMessageReadMode(t *testing.T) { msg := ([]byte)("hello world") wg.Add(1) + errs := make(chan error) go func() { defer wg.Done() s, err := l.Accept() if err != nil { - t.Fatal(err) + errs <- err } _, err = s.Write(msg) if err != nil { - t.Fatal(err) + errs <- err } s.Close() + close(errs) }() + for e := range errs { + t.Fatal(e) + } + c, err := namedpipe.DialTimeout(pipePath, time.Duration(0)) if err != nil { t.Fatal(err)