Skip to content

Commit

Permalink
feat(netcore): start covering conn.go code (#55)
Browse files Browse the repository at this point in the history
This diff starts adding tests to increase the coverage of code in the
netcore package and specifically in conn.go.

We want to have reasonably good coverage of functionality and error
paths for the netcore package ahead of migrating this code, which now
seems to be stable, to its own package.

While there, upgrade dependencies to silence an otherwise harmless
security advisory (we do not use `x/net/html` here).
  • Loading branch information
bassosimone authored Dec 19, 2024
1 parent 7354fea commit 3a24044
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ require (
github.com/rbmk-project/common v0.14.0
github.com/rbmk-project/dnscore v0.9.0
github.com/rogpeppe/go-internal v1.13.1
github.com/stretchr/testify v1.10.0
golang.org/x/sys v0.28.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/tools v0.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
Expand All @@ -26,5 +26,7 @@ golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
113 changes: 113 additions & 0 deletions netcore/conn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package netcore

import (
"context"
"io"
"log/slog"
"net"
"testing"

"github.com/rbmk-project/common/mocks"
"github.com/stretchr/testify/assert"
)

func TestConnLocalAddr(t *testing.T) {
t.Run("nil connection", func(t *testing.T) {
addr := connLocalAddr(nil)
assert.Equal(t, "", addr.Network())
assert.Equal(t, "", addr.String())
})

t.Run("nil local address", func(t *testing.T) {
conn := &mocks.Conn{
MockLocalAddr: func() net.Addr { return nil },
}
addr := connLocalAddr(conn)
assert.Equal(t, "", addr.Network())
assert.Equal(t, "", addr.String())
})

t.Run("valid address", func(t *testing.T) {
expectedAddr := &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 1234,
}
conn := &mocks.Conn{
MockLocalAddr: func() net.Addr { return expectedAddr },
}
addr := connLocalAddr(conn)
assert.Equal(t, expectedAddr, addr)
})
}

func TestConnRemoteAddr(t *testing.T) {
t.Run("nil connection", func(t *testing.T) {
addr := connRemoteAddr(nil)
assert.Equal(t, "", addr.Network())
assert.Equal(t, "", addr.String())
})

t.Run("nil remote address", func(t *testing.T) {
conn := &mocks.Conn{
MockRemoteAddr: func() net.Addr { return nil },
}
addr := connRemoteAddr(conn)
assert.Equal(t, "", addr.Network())
assert.Equal(t, "", addr.String())
})

t.Run("valid address", func(t *testing.T) {
expectedAddr := &net.TCPAddr{
IP: net.ParseIP("1.1.1.1"),
Port: 443,
}
conn := &mocks.Conn{
MockRemoteAddr: func() net.Addr { return expectedAddr },
}
addr := connRemoteAddr(conn)
assert.Equal(t, expectedAddr, addr)
})
}

func TestMaybeWrapConn(t *testing.T) {
t.Run("nil connection", func(t *testing.T) {
nx := &Network{}
assert.Nil(t, nx.maybeWrapConn(context.Background(), nil))
})

t.Run("no logger configured", func(t *testing.T) {
nx := &Network{}
conn := &mocks.Conn{}
wrapped := nx.maybeWrapConn(context.Background(), conn)
assert.Equal(t, conn, wrapped) // should return unwrapped
})

t.Run("no wrapper configured", func(t *testing.T) {
nx := &Network{
Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
}
conn := &mocks.Conn{}
wrapped := nx.maybeWrapConn(context.Background(), conn)
assert.Equal(t, conn, wrapped) // should return unwrapped
})

t.Run("full wrapping", func(t *testing.T) {
nx := &Network{
Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
WrapConn: WrapConn,
}
conn := &mocks.Conn{
MockLocalAddr: func() net.Addr {
return &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 54321}
},
MockRemoteAddr: func() net.Addr {
return &net.TCPAddr{IP: net.ParseIP("1.1.1.1"), Port: 443}
},
}
wrapped := nx.maybeWrapConn(context.Background(), conn)
assert.NotEqual(t, conn, wrapped) // should return wrapped
assert.IsType(t, &connWrapper{}, wrapped)
})
}

0 comments on commit 3a24044

Please sign in to comment.