diff --git a/.golangci.yml b/.golangci.yml index 10bd9b3..6ce6ccf 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -90,6 +90,9 @@ issues: - linters: [godot] source: "\\/\\/\\s+}" + - linters: [gci] + source: "#nosec" + linters: enable-all: true disable: @@ -102,6 +105,10 @@ linters: - gomnd - goerr113 - testpackage + - errorlint + - wrapcheck + - exhaustivestruct + - nlreturn run: skip-dirs: diff --git a/client_test.go b/client_test.go index 829e365..146ee38 100644 --- a/client_test.go +++ b/client_test.go @@ -442,11 +442,9 @@ func TestDial(t *testing.T) { if err != nil { t.Fatal(err) } - defer func() { - if err = c.Close(); err != nil { - t.Error(err) - } - }() + if err = c.Close(); err != nil { + t.Error(err) + } } func TestDialError(t *testing.T) { @@ -455,6 +453,7 @@ func TestDialError(t *testing.T) { t.Fatal("error expected") } } + func TestClientCloseErr(t *testing.T) { response := MustBuild(TransactionID, BindingSuccess) response.Encode() @@ -472,11 +471,9 @@ func TestClientCloseErr(t *testing.T) { if err != nil { log.Fatal(err) } - defer func() { - if err, ok := c.Close().(CloseErr); !ok || err.AgentErr != io.ErrUnexpectedEOF { - t.Error("unexpected close err") - } - }() + if err, ok := c.Close().(CloseErr); !ok || err.AgentErr != io.ErrUnexpectedEOF { + t.Error("unexpected close err") + } } func TestWithNoConnClose(t *testing.T) { @@ -618,13 +615,15 @@ func TestClientFinalizer(t *testing.T) { } clientFinalizer(c) reader := bufio.NewScanner(buf) - var lines int - var expectedLines = []string{ - "client: called finalizer on non-closed client: client not initialized", - "client: called finalizer on non-closed client", - "client: called finalizer on non-closed client: failed to close: " + - " (connection), unexpected EOF (agent)", - } + var ( + lines int + expectedLines = []string{ + "client: called finalizer on non-closed client: client not initialized", + "client: called finalizer on non-closed client", + "client: called finalizer on non-closed client: failed to close: " + + " (connection), unexpected EOF (agent)", + } + ) for reader.Scan() { if reader.Text() != expectedLines[lines] { t.Error(reader.Text(), "!=", expectedLines[lines]) @@ -855,6 +854,7 @@ func TestClient_DoConcurrent(t *testing.T) { 1, 5, 10, 25, 100, 500, } { t.Run(fmt.Sprintf("%d", concurrency), func(t *testing.T) { + t.Parallel() testClientDoConcurrent(t, concurrency) }) } diff --git a/cmd/stun-bench/main.go b/cmd/stun-bench/main.go index bd662ce..85b8f95 100644 --- a/cmd/stun-bench/main.go +++ b/cmd/stun-bench/main.go @@ -20,7 +20,7 @@ import ( var ( workers = flag.Int("w", runtime.GOMAXPROCS(0), "concurrent workers") - addr = flag.String("addr", fmt.Sprintf("localhost"), "target address") + addr = flag.String("addr", "localhost", "target address") port = flag.Int("port", stun.DefaultPort, "target port") duration = flag.Duration("d", time.Minute, "benchmark duration") network = flag.String("net", "udp", "protocol to use (udp, tcp)") diff --git a/cmd/stun-traversal/main.go b/cmd/stun-traversal/main.go index 8dee571..4183b9d 100644 --- a/cmd/stun-traversal/main.go +++ b/cmd/stun-traversal/main.go @@ -13,9 +13,7 @@ import ( "gortc.io/stun" ) -var ( - server = flag.String("server", fmt.Sprintf("gortc.io:3478"), "Stun server address") -) +var server = flag.String("server", "gortc.io:3478", "Stun server address") const ( udp = "udp4" diff --git a/fuzz.go b/fuzz.go index 71790b1..2cb9b59 100644 --- a/fuzz.go +++ b/fuzz.go @@ -7,9 +7,7 @@ import ( "fmt" ) -var ( - m = New() -) +var m = New() // FuzzMessage is go-fuzz endpoint for message. func FuzzMessage(data []byte) int { @@ -96,7 +94,7 @@ func FuzzSetters(data []byte) int { {new(MappedAddress), AttrMappedAddress}, {new(Realm), AttrRealm}, } - var firstByte = byte(0) + firstByte := byte(0) if len(data) > 0 { firstByte = data[0] } diff --git a/integrity_test.go b/integrity_test.go index c19467c..97f5d72 100644 --- a/integrity_test.go +++ b/integrity_test.go @@ -102,6 +102,7 @@ func BenchmarkMessageIntegrity_AddTo(b *testing.B) { m.Reset() } } + func BenchmarkMessageIntegrity_Check(b *testing.B) { m := new(Message) // TODO: Find a way to make this test zero-alloc without excessive pre-alloc. diff --git a/message.go b/message.go index 1211897..08e0487 100644 --- a/message.go +++ b/message.go @@ -416,7 +416,7 @@ const ( var ( // Binding request message type. BindingRequest = NewType(MethodBinding, ClassRequest) - // Binding success response message type + // Binding success response message type. BindingSuccess = NewType(MethodBinding, ClassSuccessResponse) // Binding error response message type. BindingError = NewType(MethodBinding, ClassErrorResponse) diff --git a/message_test.go b/message_test.go index 32bf0b3..f3720ea 100644 --- a/message_test.go +++ b/message_test.go @@ -64,7 +64,7 @@ func BenchmarkMessage_Write(b *testing.B) { } func TestMessageType_Value(t *testing.T) { - var tests = []struct { + tests := []struct { in MessageType out uint16 }{ @@ -82,7 +82,7 @@ func TestMessageType_Value(t *testing.T) { } func TestMessageType_ReadValue(t *testing.T) { - var tests = []struct { + tests := []struct { in uint16 out MessageType }{ @@ -100,7 +100,7 @@ func TestMessageType_ReadValue(t *testing.T) { } func TestMessageType_ReadWriteValue(t *testing.T) { - var tests = []MessageType{ + tests := []MessageType{ {Method: MethodBinding, Class: ClassRequest}, {Method: MethodBinding, Class: ClassSuccessResponse}, {Method: MethodBinding, Class: ClassErrorResponse}, @@ -197,8 +197,10 @@ func TestMessage_AttrLengthLessThanHeader(t *testing.T) { func TestMessage_AttrSizeLessThanLength(t *testing.T) { mType := MessageType{Method: MethodBinding, Class: ClassRequest} - messageAttribute := RawAttribute{Length: 4, - Value: []byte{1, 2, 3, 4}, Type: 0x1, + messageAttribute := RawAttribute{ + Length: 4, + Value: []byte{1, 2, 3, 4}, + Type: 0x1, } messageAttributes := Attributes{ messageAttribute, @@ -497,7 +499,7 @@ func TestIsMessage(t *testing.T) { NewSoftware("software").AddTo(m) m.WriteHeader() - var tt = [...]struct { + tt := [...]struct { in []byte out bool }{ @@ -506,9 +508,11 @@ func TestIsMessage(t *testing.T) { {[]byte{1, 2, 4}, false}, // 2 {[]byte{1, 2, 4, 5, 6, 7, 8, 9, 20}, false}, // 3 {m.Raw, true}, // 5 - {[]byte{0, 0, 0, 0, 33, 18, + {[]byte{ + 0, 0, 0, 0, 33, 18, 164, 66, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0}, true}, // 6 + 0, 0, 0, 0, 0, 0, 0, 0, + }, true}, // 6 } for i, v := range tt { if got := IsMessage(v.in); got != v.out {