prysm-pulse/network/external_ip_test.go
Raul Jordan d077483577
Add V3 Suffix to All Prysm Packages (#11083)
* v3 import renamings

* tidy

* fmt

* rev

* Update beacon-chain/core/epoch/precompute/reward_penalty_test.go

* Update beacon-chain/core/helpers/validators_test.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/iface/BUILD.bazel

* Update beacon-chain/db/kv/kv.go

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/sync/initial-sync/service.go

* fix deps

* fix bad replacements

* fix bad replacements

* change back

* gohashtree version

* fix deps

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2022-08-16 12:20:13 +00:00

50 lines
1.5 KiB
Go

package network_test
import (
"net"
"regexp"
"testing"
"github.com/prysmaticlabs/prysm/v3/network"
"github.com/prysmaticlabs/prysm/v3/testing/assert"
"github.com/prysmaticlabs/prysm/v3/testing/require"
)
func TestExternalIPv4(t *testing.T) {
// Regular expression format for IPv4
IPv4Format := `\.\d{1,3}\.\d{1,3}\b`
test, err := network.ExternalIPv4()
require.NoError(t, err)
valid := regexp.MustCompile(IPv4Format)
assert.Equal(t, true, valid.MatchString(test))
}
func TestRetrieveIP(t *testing.T) {
ip, err := network.ExternalIP()
if err != nil {
t.Fatal(err)
}
retIP := net.ParseIP(ip)
if retIP.To4() == nil && retIP.To16() == nil {
t.Errorf("An invalid IP was retrieved: %s", ip)
}
}
func TestSortAddresses(t *testing.T) {
testAddresses := []net.IP{
{0xff, 0x02, 0xAA, 0, 0x1F, 0, 0, 0, 0, 0, 0x02, 0x2E, 0, 0, 0x36, 0x45},
{0xff, 0x02, 0xAA, 0, 0x1F, 0, 0x2E, 0, 0, 0x36, 0x45, 0, 0, 0, 0, 0x02},
{0xAA, 0x11, 0x33, 0x19},
{0x01, 0xBF, 0x33, 0x10},
{0x03, 0x89, 0x33, 0x13},
}
sortedAddrs := network.SortAddresses(testAddresses)
assert.Equal(t, true, sortedAddrs[0].To4() != nil, "expected ipv4 address")
assert.Equal(t, true, sortedAddrs[1].To4() != nil, "expected ipv4 address")
assert.Equal(t, true, sortedAddrs[2].To4() != nil, "expected ipv4 address")
assert.Equal(t, true, sortedAddrs[3].To16() != nil && sortedAddrs[3].To4() == nil, "expected ipv6 address")
assert.Equal(t, true, sortedAddrs[4].To16() != nil && sortedAddrs[4].To4() == nil, "expected ipv6 address")
}