Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

global: linter and deprecated #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ install: wireguard-go
test:
go test ./...

benchmark:
go test -bench=. ./...

clean:
rm -f wireguard-go

Expand Down
2 changes: 1 addition & 1 deletion conn/errors_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

package conn

func errShouldDisableUDPGSO(err error) bool {
func errShouldDisableUDPGSO(_ error) bool {
return false
}
2 changes: 1 addition & 1 deletion conn/features_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ package conn

import "net"

func supportsUDPOffload(conn *net.UDPConn) (txOffload, rxOffload bool) {
func supportsUDPOffload(_ *net.UDPConn) (txOffload, rxOffload bool) {
return
}
10 changes: 5 additions & 5 deletions device/allowedips_rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,22 @@ 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{})
}

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])
Expand All @@ -109,15 +109,15 @@ 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 {
t.Errorf("Trie did not match naive implementation, for %v: want %p, got %p", net.IP(addr4[:]), peer1, peer2)
}

var addr6 [16]byte
rand.Read(addr6[:])
rng.Read(addr6[:])
peer1 = slow6.Lookup(addr6[:])
peer2 = allowedIPs.Lookup(addr6[:])
if peer1 != peer2 {
Expand Down
12 changes: 6 additions & 6 deletions device/allowedips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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[:])
}
}
Expand Down
2 changes: 1 addition & 1 deletion device/sticky_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions device/sticky_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
76 changes: 63 additions & 13 deletions ipc/namedpipe/namedpipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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)
Expand Down