-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(netcore): start covering conn.go code (#55)
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
1 parent
7354fea
commit 3a24044
Showing
3 changed files
with
122 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |