From 3eff1a98b93226cfb5e4471f9b3301c75e670fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= Date: Fri, 1 Dec 2023 13:46:30 +0100 Subject: [PATCH] Add ResolveAddr test cases --- cmd/main.go | 2 +- {pkg/network => network}/network.go | 0 network/network_test.go | 59 +++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) rename {pkg/network => network}/network.go (100%) create mode 100644 network/network_test.go diff --git a/cmd/main.go b/cmd/main.go index 48c241f..86c1e9d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -26,7 +26,7 @@ import ( "github.com/0xPolygon/beethoven/config" "github.com/0xPolygon/beethoven/db" "github.com/0xPolygon/beethoven/etherman" - "github.com/0xPolygon/beethoven/pkg/network" + "github.com/0xPolygon/beethoven/network" "github.com/0xPolygon/beethoven/rpc" ) diff --git a/pkg/network/network.go b/network/network.go similarity index 100% rename from pkg/network/network.go rename to network/network.go diff --git a/network/network_test.go b/network/network_test.go new file mode 100644 index 0000000..2be57ce --- /dev/null +++ b/network/network_test.go @@ -0,0 +1,59 @@ +package network + +import ( + "fmt" + "net" + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_ResolveAddr(t *testing.T) { + t.Parallel() + + tcpAddrBuilder := func(t *testing.T, address string) *net.TCPAddr { + tcpAddr, err := net.ResolveTCPAddr("", address) + require.NoError(t, err) + + return tcpAddr + } + + cases := []struct { + name string + address string + defaultIP string + errMsg string + }{ + { + name: "incorrect address", + address: "Foo Bar", + errMsg: "failed to parse addr", + }, + { + name: "only port provided", + address: ":8080", + defaultIP: "127.0.0.1", + }, + { + name: "both address and port provided", + address: "255.0.255.0:8080", + defaultIP: "", + }, + } + + for _, c := range cases { + c := c + t.Run(c.name, func(t *testing.T) { + t.Parallel() + + ipAddr, err := ResolveAddr(c.address, c.defaultIP) + if c.errMsg != "" { + require.ErrorContains(t, err, c.errMsg) + } else { + require.NoError(t, err) + expectedIPAddr := tcpAddrBuilder(t, fmt.Sprintf("%s%s", c.defaultIP, c.address)) + require.Equal(t, expectedIPAddr, ipAddr) + } + }) + } +}