Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
Add ResolveAddr test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Dec 1, 2023
1 parent 3a2fd08 commit 3eff1a9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
File renamed without changes.
59 changes: 59 additions & 0 deletions network/network_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 3eff1a9

Please sign in to comment.